def SendDhcpPacketTo(self, To, 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)
- out_socket.setsockopt(socket.SOL_SOCKET, IN.SO_BINDTODEVICE, intf)
- #out_socket.bind((ip, self.listen_port))
- ret = out_socket.sendto(packet.EncodePacket(), (To,self.emit_port))
- out_socket.close()
+ self.dhcp_socket.setsockopt(socket.SOL_SOCKET, IN.SO_BINDTODEVICE, intf)
+ ret = self.dhcp_socket.sendto(packet.EncodePacket(), (To,self.emit_port))
+ self.dhcp_socket.setsockopt(socket.SOL_SOCKET, IN.SO_BINDTODEVICE, '')
return ret
else:
return self.dhcp_socket.sendto(packet.EncodePacket(),(To,self.emit_port))
else : s.syslog(s.LOG_INFO,"Get DHCPREQUEST_UNKNOWN_STATE packet : not implemented")
- if self.backend.Request(packet) : packet.TransformToDhcpAckPacket()
- else : packet.TransformToDhcpNackPacket()
-
- self.SendPacket(packet)
-
-
+ if self.backend.Request(packet):
+ packet.TransformToDhcpAckPacket()
+ self.SendPacket(packet)
+ elif self.backend.Discover(packet):
+ packet.TransformToDhcpNackPacket()
+ self.SendPacket(packet)
+ else:
+ pass # We aren't authoritative, so don't reply if we don't know them.
# FIXME: These are not yet implemented.
def HandleDhcpDecline(self, packet):
options = { "server_listen_port":67,
"client_listen_port":68,
"listen_address":"0.0.0.0"}
+
+ myip = socket.gethostbyname(socket.gethostname())
+ if not myip:
+ print "invirt-dhcpserver: cannot determine local IP address by looking up %s" % socket.gethostname()
+ sys.exit(1)
+
+ dhcp_options['server_identifier'] = myip
+
backend = DhcpBackend()
server = DhcpServer(backend, options)