X-Git-Url: http://xvm.mit.edu/gitweb/invirt/packages/invirt-dhcp.git/blobdiff_plain/4ac9701f4fd2847f00970f3b1447de95a8a6d655..ab0ebb05d4cab27398b59d029e7d3e3addd82c55:/code/dhcpserver.py diff --git a/code/dhcpserver.py b/code/dhcpserver.py index 3133643..634cad6 100755 --- a/code/dhcpserver.py +++ b/code/dhcpserver.py @@ -1,6 +1,5 @@ #!/usr/bin/python import sys -sys.path.append('pydhcplib/') import pydhcplib import pydhcplib.dhcp_network from pydhcplib.dhcp_packet import * @@ -17,30 +16,28 @@ from event_logger import Log import psycopg2 import time -import sipb_xen_database -from sqlalchemy import create_engine +from invirt import database +from invirt.config import structs as config -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', +dhcp_options = {'subnet_mask': config.dhcp.netmask, + 'router': config.dhcp.gateway, + 'domain_name_server': ','.join(config.dhcp.dns), 'ip_address_lease_time': 60*60*24} class DhcpBackend: - def __init__(self, database=None): - if database is not None: - self.database = database - sipb_xen_database.connect(create_engine(database)) + def __init__(self): + database.connect() def findNIC(self, mac): + database.clear_cache() for i in range(3): try: - value = sipb_xen_database.NIC.get_by(mac_addr=mac) + value = database.NIC.query().filter_by(mac_addr=mac).one() 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)) + database.connect() else: break return value @@ -48,14 +45,14 @@ class DhcpBackend: chaddr = hwmac(packet.GetHardwareAddress()) nic = self.findNIC(str(chaddr)) if nic is None or nic.ip is None: - return ("18.181.0.60", None) + return 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) + return parts[0] + return def getParameters(self, **extra): all_options=dict(dhcp_options) @@ -95,7 +92,16 @@ class DhcpBackend: elif option_type == "string" : options[parameter] = strlist(value).list() + + elif option_type == "RFC3397" : + parsed_value = "" + for item in value: + components = item.split('.') + item_fmt = "".join(chr(len(elt)) + elt for elt in components) + "\x00" + parsed_value += item_fmt + options[parameter] = strlist(parsed_value).list() + else : options[parameter] = strlist(value).list() return options @@ -109,16 +115,23 @@ class DhcpBackend: ip = nic.ip if ip is None: #Deactivated? return False + + options = {} if nic.hostname and '.' in nic.hostname: - hostname = nic.hostname + options['host_name'], options['domain_name'] = nic.hostname.split('.', 1) elif nic.machine.name: - hostname = nic.machine.name + '.servers.csail.mit.edu' + options['host_name'] = nic.machine.name + options['domain_name'] = config.dns.domains[0] else: hostname = None + if DhcpOptions['domain_search'] in packet.GetOption('parameter_request_list'): + options['host_name'] += '.' + options['domain_name'] + del options['domain_name'] + options['domain_search'] = [config.dhcp.search_domain] if ip is not None: ip = ipv4(ip) Log.Output(Log.debug,"dhcp_backend : Discover result = "+str(ip)) - packet_parameters = self.getParameters(host_name=hostname) + packet_parameters = self.getParameters(**options) # FIXME: Other offer parameters go here packet_parameters["yiaddr"] = ip.list() @@ -162,7 +175,7 @@ class DhcpServer(pydhcplib.dhcp_network.DhcpServer): Log.Output(Log.debug, "__init__ DhcpServer") def SendDhcpPacketTo(self, To, packet): - (ip, intf) = self.backend.find_interface(packet) + intf = self.backend.find_interface(packet) if intf: out_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) out_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST,1) @@ -259,7 +272,7 @@ if '__main__' == __name__: options = { "server_listen_port":67, "client_listen_port":68, "listen_address":"0.0.0.0"} - backend = DhcpBackend('postgres://sipb-xen@sipb-xen-dev/sipb_xen') + backend = DhcpBackend() server = DhcpServer(backend, options) while True : server.GetNextDhcpPacket()