title: Is there a way to distinguish unicast and multicast when receiving UDP datagram from same socket?
body: <p>I have an application that meeds to:</p>
  <ul>
  <li>talks in UDP</li>
  <li>support optionally disabling multicast.</li>
  <li>treat multicast message the same way as unicast message but with few restrictions</li>
  </ul>
  <p>I thought I should use same UDP socket to receive both unicast and multicast datagrams.</p>
  <pre><code># udp_multicast_recv.py
  # Python version used: 3.9.1
  import socket
  import struct
  
  host = &quot;224.1.1.1&quot;
  # host = &quot;0.0.0.0&quot;
  listen_all = True
  port = 5683
  
  s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  if listen_all:
      s.bind((&quot;&quot;, port))
  else:
      s.bind((host, port))
  mreq = struct.pack(&quot;4sl&quot;, socket.inet_aton(host), socket.INADDR_ANY)
  s.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
  
  data = None
  print(&quot;Listening&quot;)
  while True:
      # data, addr = s.recvfrom(1024)
      data, ancdata, msg_flags, addr = s.recvmsg(1024)
      print(addr, &quot;:&quot;, data, ancdata, msg_flags)
  
  </code></pre>
  <pre><code># udp_multicast_send.py
  
  import socket
  
  dest = &quot;224.1.1.1&quot;
  dest = &quot;192.168.86.188&quot;
  port = 5683
  
  s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  s.sendto(&quot;Hello world&quot;.encode(), (dest, port))
  
  </code></pre>
  <p>I soon noticed I need a way to distinguish multicast datagram from unicast datagram. But I was unable to find a solution nor related question on how to achieve this on the internet.</p>
  <p>Hence the question: When receiving UDP datagram from same socket, is there a way to tell whether datagram is unicast or multicast?</p>