3 sys.path.append('pydhcplib/')
5 import pydhcplib.dhcp_network
6 from pydhcplib.dhcp_packet import *
7 from pydhcplib.type_hw_addr import hwmac
8 from pydhcplib.type_ipv4 import ipv4
9 from pydhcplib.type_strlist import strlist
14 if '__main__' == __name__:
15 event_logger.init("stdout", 'DEBUG', {})
16 from event_logger import Log
20 import sipb_xen_database
21 from sqlalchemy import create_engine
23 dhcp_options = {'subnet_mask': '255.255.0.0',
24 'router': '18.181.0.1',
25 'domain_name_server': '18.70.0.160,18.71.0.151,18.72.0.3',
26 'domain_name': 'mit.edu',
27 'ip_address_lease_time': 60*60*24}
30 def __init__(self, database=None):
31 if database is not None:
32 self.database = database
33 sipb_xen_database.connect(create_engine(database))
34 def findNIC(self, mac):
35 sipb_xen_database.clear_cache()
38 value = sipb_xen_database.NIC.get_by(mac_addr=mac)
39 except psycopg2.OperationalError:
41 if i == 2: #Try twice to reconnect.
43 #Sigh. SQLAlchemy should do this itself.
44 sipb_xen_database.connect(create_engine(self.database))
48 def find_interface(self, packet):
49 chaddr = hwmac(packet.GetHardwareAddress())
50 nic = self.findNIC(str(chaddr))
51 if nic is None or nic.ip is None:
52 return ("18.181.0.60", None)
53 ipstr = ''.join(reversed(['%02X' % i for i in ipv4(nic.ip).list()]))
54 for line in open('/proc/net/route'):
57 Log.Output(Log.debug, "find_interface found "+str(nic.ip)+" on "+parts[0])
58 return ("18.181.0.60", parts[0])
59 return ("18.181.0.60", None)
61 def getParameters(self, **extra):
62 all_options=dict(dhcp_options)
63 all_options.update(extra)
65 for parameter, value in all_options.iteritems():
68 option_type = DhcpOptionsTypes[DhcpOptions[parameter]]
70 if option_type == "ipv4" :
71 # this is a single ip address
72 options[parameter] = map(int,value.split("."))
73 elif option_type == "ipv4+" :
74 # this is multiple ip address
75 iplist = value.split(",")
77 for single in iplist :
78 opt.extend(ipv4(single).list())
79 options[parameter] = opt
80 elif option_type == "32-bits" :
81 # This is probably a number...
83 options[parameter] = [digit>>24&0xFF,(digit>>16)&0xFF,(digit>>8)&0xFF,digit&0xFF]
84 elif option_type == "16-bits" :
86 options[parameter] = [(digit>>8)&0xFF,digit&0xFF]
88 elif option_type == "char" :
90 options[parameter] = [digit&0xFF]
92 elif option_type == "bool" :
93 if value=="False" or value=="false" or value==0 :
94 options[parameter] = [0]
95 else : options[parameter] = [1]
97 elif option_type == "string" :
98 options[parameter] = strlist(value).list()
101 options[parameter] = strlist(value).list()
104 def Discover(self, packet):
105 Log.Output(Log.debug,"dhcp_backend : Discover ")
106 chaddr = hwmac(packet.GetHardwareAddress())
107 nic = self.findNIC(str(chaddr))
108 if nic is None or nic.machine is None:
111 if ip is None: #Deactivated?
113 if nic.hostname and '.' in nic.hostname:
114 hostname = nic.hostname
115 elif nic.machine.name:
116 hostname = nic.machine.name + '.servers.csail.mit.edu'
121 Log.Output(Log.debug,"dhcp_backend : Discover result = "+str(ip))
122 packet_parameters = self.getParameters(host_name=hostname)
124 # FIXME: Other offer parameters go here
125 packet_parameters["yiaddr"] = ip.list()
127 packet.SetMultipleOptions(packet_parameters)
131 def Request(self, packet):
132 Log.Output(Log.debug, "dhcp_backend : Request")
134 discover = self.Discover(packet)
136 chaddr = hwmac(packet.GetHardwareAddress())
137 request = packet.GetOption("request_ip_address")
139 request = packet.GetOption("ciaddr")
140 yiaddr = packet.GetOption("yiaddr")
143 Log.Output(Log.info,"Unknown MAC address: "+str(chaddr))
146 if yiaddr!="0.0.0.0" and yiaddr == request :
147 Log.Output(Log.info,"Ack ip "+str(yiaddr)+" for "+str(chaddr))
150 Log.Output(Log.info,"Requested ip "+str(request)+" not available for "+str(chaddr))
153 def Decline(self, packet):
155 def Release(self, packet):
159 class DhcpServer(pydhcplib.dhcp_network.DhcpServer):
160 def __init__(self, backend, options = {'client_listenport':68,'server_listenport':67}):
161 pydhcplib.dhcp_network.DhcpServer.__init__(self,"0.0.0.0",options["client_listen_port"],options["server_listen_port"],)
162 self.backend = backend
163 Log.Output(Log.debug, "__init__ DhcpServer")
165 def SendDhcpPacketTo(self, To, packet):
166 (ip, intf) = self.backend.find_interface(packet)
168 out_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
169 out_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST,1)
170 out_socket.setsockopt(socket.SOL_SOCKET, IN.SO_BINDTODEVICE, intf)
171 #out_socket.bind((ip, self.listen_port))
172 ret = out_socket.sendto(packet.EncodePacket(), (To,self.emit_port))
176 return self.dhcp_socket.sendto(packet.EncodePacket(),(To,self.emit_port))
178 def SendPacket(self, packet):
179 """Encode and send the packet."""
181 giaddr = packet.GetOption('giaddr')
183 # in all case, if giaddr is set, send packet to relay_agent
184 # network address defines by giaddr
185 if giaddr!=[0,0,0,0] :
186 agent_ip = ".".join(map(str,giaddr))
187 self.SendDhcpPacketTo(agent_ip,packet)
188 Log.Output(Log.debug, "SendPacket to agent : "+agent_ip)
190 # FIXME: This shouldn't broadcast if it has an IP address to send
191 # it to instead. See RFC2131 part 4.1 for full details
193 Log.Output(Log.debug, "No agent, broadcast packet.")
194 self.SendDhcpPacketTo("255.255.255.255",packet)
197 def HandleDhcpDiscover(self, packet):
198 """Build and send DHCPOFFER packet in response to DHCPDISCOVER
201 logmsg = "Get DHCPDISCOVER packet from " + hwmac(packet.GetHardwareAddress()).str()
203 Log.Output(Log.info, logmsg)
205 offer.CreateDhcpOfferPacketFrom(packet)
207 if self.backend.Discover(offer):
208 self.SendPacket(offer)
209 # FIXME : what if false ?
212 def HandleDhcpRequest(self, packet):
213 """Build and send DHCPACK or DHCPNACK packet in response to
214 DHCPREQUEST packet. 4 types of DHCPREQUEST exists."""
216 ip = packet.GetOption("request_ip_address")
217 sid = packet.GetOption("server_identifier")
218 ciaddr = packet.GetOption("ciaddr")
219 #packet.PrintHeaders()
220 #packet.PrintOptions()
222 if sid != [0,0,0,0] and ciaddr == [0,0,0,0] :
223 Log.Output(Log.info, "Get DHCPREQUEST_SELECTING_STATE packet")
225 elif sid == [0,0,0,0] and ciaddr == [0,0,0,0] and ip :
226 Log.Output(Log.info, "Get DHCPREQUEST_INITREBOOT_STATE packet")
228 elif sid == [0,0,0,0] and ciaddr != [0,0,0,0] and not ip :
229 Log.Output(Log.info,"Get DHCPREQUEST_INITREBOOT_STATE packet")
231 else : Log.Output(Log.info,"Get DHCPREQUEST_UNKNOWN_STATE packet : not implemented")
233 if self.backend.Request(packet) : packet.TransformToDhcpAckPacket()
234 else : packet.TransformToDhcpNackPacket()
236 self.SendPacket(packet)
240 # FIXME: These are not yet implemented.
241 def HandleDhcpDecline(self, packet):
242 Log.Output(Log.info, "Get DHCPDECLINE packet")
243 self.backend.Decline(packet)
245 def HandleDhcpRelease(self, packet):
246 Log.Output(Log.info,"Get DHCPRELEASE packet")
247 self.backend.Release(packet)
249 def HandleDhcpInform(self, packet):
250 Log.Output(Log.info, "Get DHCPINFORM packet")
252 if self.backend.Request(packet) :
253 packet.TransformToDhcpAckPacket()
254 # FIXME : Remove lease_time from options
255 self.SendPacket(packet)
257 # FIXME : what if false ?
259 if '__main__' == __name__:
260 options = { "server_listen_port":67,
261 "client_listen_port":68,
262 "listen_address":"0.0.0.0"}
263 backend = DhcpBackend('postgres://sipb-xen@sipb-xen-dev/sipb_xen')
264 server = DhcpServer(backend, options)
266 while True : server.GetNextDhcpPacket()