+ self.database = database
+ sipb_xen_database.connect(create_engine(database))
+ def findNIC(self, mac):
+ for i in range(3):
+ try:
+ value = sipb_xen_database.NIC.get_by(mac_addr=mac)
+ except psycopg2.OperationalError:
+ time.sleep(0.5)
+ if i == 2: #Try twice to reconnect.
+ raise
+ #Sigh. SQLAlchemy should do this itself.
+ sipb_xen_database.connect(create_engine(self.database))
+ else:
+ break
+ return value
+ def find_interface(self, packet):
+ chaddr = hwmac(packet.GetHardwareAddress())
+ nic = self.findNIC(str(chaddr))
+ if nic is None or nic.ip is None:
+ return ("18.181.0.60", None)
+ ipstr = ''.join(reversed(['%02X' % i for i in ipv4(nic.ip).list()]))
+ for line in open('/proc/net/route'):
+ parts = line.split()
+ if parts[1] == ipstr:
+ Log.Output(Log.debug, "find_interface found "+str(nic.ip)+" on "+parts[0])
+ return ("18.181.0.60", parts[0])
+ return ("18.181.0.60", None)
+
+ def getParameters(self, **extra):
+ all_options=dict(dhcp_options)
+ all_options.update(extra)
+ options = {}
+ for parameter, value in all_options.iteritems():
+ if value is None:
+ continue
+ 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.extend(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