Changeset 202
- Timestamp:
- Oct 18, 2007, 9:49:09 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/dhcp/dhcpserver.py
r191 r202 8 8 from pydhcplib.type_ipv4 import ipv4 9 9 from pydhcplib.type_strlist import strlist 10 import socket 11 import IN 10 12 11 13 import event_logger … … 14 16 from event_logger import Log 15 17 18 import psycopg2 19 import time 16 20 import sipb_xen_database 21 from sqlalchemy import create_engine 17 22 18 23 dhcp_options = {'subnet_mask': '255.255.0.0', 19 24 'router': '18.181.0.1', 20 25 'domain_name_server': '18.70.0.160,18.71.0.151,18.72.0.3', 21 'domain_name': 'mit.edu' }22 26 'domain_name': 'mit.edu', 27 'ip_address_lease_time': 60*60*24} 23 28 24 29 class DhcpBackend: 25 30 def __init__(self, database=None): 26 31 if database is not None: 27 sipb_xen_database.connect(database) 28 def findIP(self, mac): 29 value = sipb_xen_database.NIC.get_by(mac_addr=mac) 30 if value is None: 31 return None 32 ip = value.ip 33 if ip is None: #Deactivated? 34 return None 35 return ip 36 def getParameters(self): 32 self.database = database 33 sipb_xen_database.connect(create_engine(database)) 34 def findNIC(self, mac): 35 for i in range(3): 36 try: 37 value = sipb_xen_database.NIC.get_by(mac_addr=mac) 38 except psycopg2.OperationalError: 39 time.sleep(0.5) 40 if i == 2: #Try twice to reconnect. 41 raise 42 #Sigh. SQLAlchemy should do this itself. 43 sipb_xen_database.connect(create_engine(self.database)) 44 else: 45 break 46 return value 47 def find_interface(self, packet): 48 chaddr = hwmac(packet.GetHardwareAddress()) 49 nic = self.findNIC(str(chaddr)) 50 if nic is None or nic.ip is None: 51 return ("18.181.0.60", None) 52 ipstr = ''.join(reversed(['%02X' % i for i in ipv4(nic.ip).list()])) 53 for line in open('/proc/net/route'): 54 parts = line.split() 55 if parts[1] == ipstr: 56 Log.Output(Log.debug, "find_interface found "+str(nic.ip)+" on "+parts[0]) 57 return ("18.181.0.60", parts[0]) 58 return ("18.181.0.60", None) 59 60 def getParameters(self, **extra): 61 all_options=dict(dhcp_options) 62 all_options.update(extra) 37 63 options = {} 38 for parameter, value in dhcp_options.iteritems(): 64 for parameter, value in all_options.iteritems(): 65 if value is None: 66 continue 39 67 option_type = DhcpOptionsTypes[DhcpOptions[parameter]] 40 68 … … 47 75 opt = [] 48 76 for single in iplist : 49 opt. append(ipv4(single).list())77 opt.extend(ipv4(single).list()) 50 78 options[parameter] = opt 51 79 elif option_type == "32-bits" : … … 76 104 Log.Output(Log.debug,"dhcp_backend : Discover ") 77 105 chaddr = hwmac(packet.GetHardwareAddress()) 78 ip = self.findIP(str(chaddr)) 106 nic = self.findNIC(str(chaddr)) 107 if nic is None: 108 return False 109 ip = nic.ip 110 if ip is None: #Deactivated? 111 return False 112 hostname = nic.hostname 113 if hostname is not None: 114 hostname += ".servers.csail.mit.edu" 79 115 if ip is not None: 80 116 ip = ipv4(ip) 81 117 Log.Output(Log.debug,"dhcp_backend : Discover result = "+str(ip)) 82 packet_parameters = self.getParameters( )118 packet_parameters = self.getParameters(host_name=hostname) 83 119 84 120 # FIXME: Other offer parameters go here … … 96 132 chaddr = hwmac(packet.GetHardwareAddress()) 97 133 request = packet.GetOption("request_ip_address") 134 if not request: 135 request = packet.GetOption("ciaddr") 98 136 yiaddr = packet.GetOption("yiaddr") 99 137 … … 121 159 Log.Output(Log.debug, "__init__ DhcpServer") 122 160 161 def SendDhcpPacketTo(self, To, packet): 162 (ip, intf) = self.backend.find_interface(packet) 163 if intf: 164 out_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 165 out_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST,1) 166 out_socket.setsockopt(socket.SOL_SOCKET, IN.SO_BINDTODEVICE, intf) 167 #out_socket.bind((ip, self.listen_port)) 168 ret = out_socket.sendto(packet.EncodePacket(), (To,self.emit_port)) 169 out_socket.close() 170 return ret 171 else: 172 return self.dhcp_socket.sendto(packet.EncodePacket(),(To,self.emit_port)) 173 123 174 def SendPacket(self, packet): 124 175 """Encode and send the packet.""" … … 162 213 sid = packet.GetOption("server_identifier") 163 214 ciaddr = packet.GetOption("ciaddr") 215 #packet.PrintHeaders() 216 #packet.PrintOptions() 164 217 165 218 if sid != [0,0,0,0] and ciaddr == [0,0,0,0] :
Note: See TracChangeset
for help on using the changeset viewer.