0

I am writing a python 2.7 program to discover specific devices via mDNS. I'm only able to use standard modules. Because of that, I have to implement the discovery using the socket module only.

My current implementation, given below, does not result in a telegram recorded by Wireshark. So, it seems no message is leaving my machine.

No error message is printed out.

My current implementation is:

# configure socket
mcast_port = 5353
mcast_grp = ('224.0.0.251', mcast_port)

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.settimeout(5)

ttl = struct.pack('b', 5)
sock.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_TTL, ttl)
sock.setsockopt(socket.SOL_IP, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_LOOP, 1)

try:
  bytes_send = sock.sendto(query_msg, mcast_grp)
    if bytes_send != len(query_msg):
      print("Something wrong here")
except socket.error as e:
  self.log_data("Socket Error", "discover: " + str(e))
  sock.shutdown(socket.SHUT_RDWR)
  sock.close()
except Exception as e:
  self.log_data("Error", "discover: " + str(e))
  sock.shutdown(socket.SHUT_RDWR)
  sock.close()

Anyone sees, what I'm not seeing?

Tob
  • 286
  • 1
  • 17
  • Any reason to write new code for an obsolete language that was discontinued 2.5+ years ago? – Mark Setchell Aug 14 '22 at 16:50
  • @MarkSetchell an industrial graded target machine, requiring that stuff due to ++years of granted compatibility :) – Tob Aug 14 '22 at 16:53
  • How is your kernel and routing configured? Do you have firewall enabled? – Keith Aug 14 '22 at 17:18
  • @Keith it's a windows 11 machine, dns-sd is doing it's job and showing up in Wireshark. The python IDE, doing the testing, is not blocked by the firewall. Other requests (not mDNS) from the code are doing a desired. – Tob Aug 14 '22 at 17:35
  • So it's Windows 11 yet you have to use Python 2.7? – Keith Aug 14 '22 at 17:36
  • 1
    Does this answer your question? https://stackoverflow.com/questions/603852/how-do-you-udp-multicast-in-python – Keith Aug 14 '22 at 17:37
  • @Keith I use Windows 11 for the development the target machine runs a debian and is provided as it is with the given restrictions. I have no chance to change this. As mentioned it is a industrial server: https://www.gira.com/uk/en/products/lighting-control/app-controlled-lighting/gira-homeserver# – Tob Aug 14 '22 at 17:40
  • @Keith Do you want to post my answer/solution as yours so that I can give you the deserved credit for your help? – Tob Aug 14 '22 at 18:09
  • You can answer your own question. That's fine. – Keith Aug 14 '22 at 18:27

1 Answers1

1

As suggested in the comments of the ticket linked by @Keith, a local host interface might be chosen to send the request. This was the problem in my case. Forcing the socket to use the correct interface was the solution:

sock.bind(("192.168.0.123", 5354))

While "192.168.0.123" was the IP address of the interface.

Tob
  • 286
  • 1
  • 17