Pass the correct parameters for configuration (gateway, DNS, etc.)
[invirt/packages/invirt-dhcp.git] / dhcpserver.py
index af21b16..23f45aa 100644 (file)
@@ -1,19 +1,26 @@
 #!/usr/bin/python
-import sys.path
+import sys
 sys.path.append('pydhcplib/')
 import pydhcplib
 import pydhcplib.dhcp_network
 from pydhcplib.dhcp_packet import *
 from pydhcplib.type_hw_addr import hwmac
 from pydhcplib.type_ipv4 import ipv4
+from pydhcplib.type_strlist import strlist
 
 import event_logger
 if '__main__' == __name__:
-    event_logger.init("stdout", 'INFO', {})
+    event_logger.init("stdout", 'DEBUG', {})
 from event_logger import Log
 
 import sipb_xen_database
 
+dhcp_options = {'subnet_mask': '255.255.0.0',
+                'router': '18.181.0.1',
+                'domain_name_server': '18.70.0.160,18.71.0.151,18.72.0.3',
+                'domain_name': 'mit.edu'}
+    
+
 class DhcpBackend:
     def __init__(self, database=None):
         if database is not None:
@@ -26,6 +33,44 @@ class DhcpBackend:
         if ip is None:  #Deactivated?
             return None
         return ip
+    def getParameters(self):
+        options = {}
+        for parameter, value in dhcp_options.iteritems():
+            option_type = DhcpOptionsTypes[DhcpOptions[parameter]]
+
+            if option_type == "ipv4" :
+                # this is a single ip address
+                options[parameter] = map(int,value.split("."))
+            elif option_type == "ipv4+" :
+                # this is multiple ip address
+                iplist = value.split(",")
+                opt = []
+                for single in iplist :
+                    opt.append(ipv4(single).list())
+                options[parameter] = opt
+            elif option_type == "32-bits" :
+                # This is probably a number...
+                digit = int(value)
+                options[parameter] = [digit>>24&0xFF,(digit>>16)&0xFF,(digit>>8)&0xFF,digit&0xFF]
+            elif option_type == "16-bits" :
+                digit = int(value)
+                options[parameter] = [(digit>>8)&0xFF,digit&0xFF]
+
+            elif option_type == "char" :
+                digit = int(value)
+                options[parameter] = [digit&0xFF]
+
+            elif option_type == "bool" :
+                if value=="False" or value=="false" or value==0 :
+                    options[parameter] = [0]
+                else : options[parameter] = [1]
+                    
+            elif option_type == "string" :
+                options[parameter] = strlist(value).list()
+                
+            else :
+                options[parameter] = strlist(value).list()
+        return options
 
     def Discover(self, packet):
         Log.Output(Log.debug,"dhcp_backend : Discover ")
@@ -34,7 +79,7 @@ class DhcpBackend:
         if ip is not None:
             ip = ipv4(ip)
             Log.Output(Log.debug,"dhcp_backend : Discover result = "+str(ip))
-            packet_parameters = {}
+            packet_parameters = self.getParameters()
 
             # FIXME: Other offer parameters go here
             packet_parameters["yiaddr"] = ip.list()
@@ -104,7 +149,7 @@ class DhcpServer(pydhcplib.dhcp_network.DhcpServer):
         offer = DhcpPacket()
         offer.CreateDhcpOfferPacketFrom(packet)
         
-        if self.backend.Discover(offer) :
+        if self.backend.Discover(offer):
             self.SendPacket(offer)
         # FIXME : what if false ?