0
votes

Please look at the following python code. I created a Server class to listen on port 10000 to receive UDP broadcast messages.

If IP address is configured in the system, it can receive UDP broadcast messages. If no ip address configured, it cannot receive any messages.

Could you tell me why?

import socket
import sys

class Server:

    class Handler:

        def handle(self, message):
            pass

        def __init__(self, serialNo):
            self.serialNo = serialNo

        def _setAddress(self, socket, message, address):
            self.message = message
            self.address = address
            self.socket = socket

        def send(self, message):
            self.socket.sendto(message, self.address)

        def getSerialNo(self):
            return self.serialNo

    def __init__(self, port, handler):
        self.ss = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.ss.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.ss.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
        self.handler = handler

        try:
            self.ss.bind(('<broadcast>', port))
        except:
            self.ss.close()
            raise RuntimeError("Create socket error")
        self.ss.setblocking(1)

    def loop(self):
        while True:
            try:
                print "Listening for broadcast..."
                message, address = self.ss.recvfrom(8192)
                print "Got request from %s:%s" % (address, message)
                self.handler._setAddress(self.ss, message, address)
                self.handler.handle(message)
            except (KeyboardInterrupt, SystemExit):
                raise
            except:
                sys.exc_info()[0]
1
What do you mean by 'IP configured in the system'? IP is one layer in the network stack, while UDP is a network protocol?Trev Davies
If you have no ip address and not connected to any network, what messages do you expect to get?syntonym
@syntonym, expect to get some broadcast messages when no ip address configured.Jeff Cai
@JeffCai UDP is defined on top of IP. If you can't get IP messages (which you can't if you have no IP) you can't get UDP messages. Though you should get ARP or NDP broadcast messages.syntonym
@syntonym, when no ip address configured, a host still can receive DHCP Offer message, a kind of IP packet.Jeff Cai

1 Answers

0
votes

After referring to pydhcp client code, I made following changes:

import socket
import sys
import select

class Server:

    class Handler:

        def handle(self, message):
            pass

        def __init__(self, serialNo):
            self.serialNo = serialNo

        def _setAddress(self, socket, message, address):
            self.message = message
            self.address = address
            self.socket = socket

        def send(self, message):
            self.socket.sendto(message, self.address)

        def getSerialNo(self):
            return self.serialNo

        def __init__(self, port, handler):
            self.ss = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            self.ss.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            self.ss.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
            self.handler = handler

        try:
            self.ss.bind(("0.0.0.0", int(port)))
        except:
            self.ss.close()
            raise RuntimeError("Create socket error")

        def loop(self):
            while True:
            try:
                print "Listening for broadcast..."
                data_input,data_output,data_except =    select.select([self.ss],[],[], 60)
            if (data_input != []):
               (message, address) = self.ss.recvfrom(2048)
               print "Got request from %s:%s" % (address, message)
               self.handler._setAddress(self.ss, message, address)
               self.handler.handle(message)
            else:
               print "no data within 60 seconds"
        except (KeyboardInterrupt, SystemExit):
            raise
        except:
            sys.exc_info()[0]

Now it can receive the broadcasting packets, but it cannot work on RedHat.