[invirt-dhcp] Improve standards compliance
[invirt/packages/invirt-dhcp.git] / invirt-dhcpserver
index 04376d0..d4e40e0 100755 (executable)
@@ -162,12 +162,9 @@ class DhcpServer(pydhcplib.dhcp_network.DhcpServer):
     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))
@@ -257,6 +254,14 @@ if '__main__' == __name__:
     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)