uPnP Discovery via UDP

Hey there. I'm trying to use this UDP library (http://bitbucket.org/bjoern/arduino_osc/src/tip/libraries/Ethernet/) to send a uPnP M-SEARCH message.

This is what I have so far:

#include <Ethernet.h>
#include <UdpRaw.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0x47, 0x67 }; //MAC address to use
byte ip[] = { 192, 168, 1, 92 }; // Arduino's IP address
byte gw[] = { 192, 168, 1, 1 };   // Gateway IP address

/* uPnP */
byte targetIp[] = { 239, 255, 255, 250};
int targetPort = 1900;

#define MAX_SIZE 256 // maximum packet size (running MEGA)
byte packetBuffer[MAX_SIZE]; //buffer to hold incoming packet
int packetSize; // holds received packet size
byte remoteIp[4]; // holds recieved packet's originating IP
unsigned int remotePort; // holds received packet's originating port

void setup() {
  Ethernet.begin(mac,ip,gw);
  Serial.begin(115200);
  UdpRaw.begin(1900);
  delay(1000);
   sendevent();
}

void loop() {

   // if there's data available, read a packet
  if(UdpRaw.available()) {
    packetSize = UdpRaw.readPacket(packetBuffer,MAX_SIZE,remoteIp,(uint16_t *)&remotePort);

      Serial.print("Received packet of size ");
      Serial.println(abs(packetSize));

      Serial.print("From IP ");
      for(int i=0; i<3; i++) {
        Serial.print(remoteIp[i],DEC);
        Serial.print(".");
      }
      Serial.print(remoteIp[3],DEC);

      Serial.print(" Port ");
      Serial.println(remotePort);

      if(packetSize < 0) {
        // if return value <0 the packet was truncated to fit into our buffer
        Serial.print("ERROR: Packet was truncated from ");
        Serial.print(packetSize*-1);
        Serial.print(" to ");
        Serial.print(MAX_SIZE);
        Serial.println(" bytes.");
      }

      Serial.println("Contents:");
      for(int i=0; i<min(MAX_SIZE,abs(packetSize)); i++) {
        Serial.print(packetBuffer[i],BYTE);
      }
      Serial.println("");

  }
}

void sendevent(){
  Serial.println("send");
  UdpRaw.sendPacket("M-SEARCH * HTTP/1.1\r\nHOST: 239.255.255.250:1900\r\nMAN: \"ssdp:discover\"\r\nMX: 5\r\nST: upnp:rootdevice\r\n\r\n",targetIp,targetPort);
}

I can listen to global events, however, I cannot seem to send any out so I can responses to the adruino's IP (sniffing using wireshark to all 239.255.255.250 traffic from my arduino's IP)... any ideas where this could be failing? This should be pretty simple but it's been sucking a lot of my time.

Also, sending the UDP message to a computer and sniffing it does come through... just not via the Multicast IP... does Multicast require something special in the setup of the library?

P.S. This example only sends 1 event per reset.

Danny

I bet multicast is disabled in the ethernet implimentation judging by the WIZNET 5100 datasheet (http://www.sparkfun.com/datasheets/DevTools/WIZnet/W5100_Datasheet_v1_1_8.pdf) page 25:

Multicasting
0 : disable Multicasting
1 : enable Multicasting 

It is applied only in case of UDP. For using multicasting, write multicast group address to Socket n Destination IP and multicast group port number to Socket n Destination Port Register, before OPEN command.

Looking at the SOCKET.H/.CPP files I see that Sn_MR(s) is being set... but I'm not sure if it is enabled. Anyone have a better idea as to how to enable mutlicasting or even check to see if the socket code already enables multicast? Or am I reading to deep into this and I've made another error keeping my example from working?

Danny

So I have confirmed that it is indeed a multicast vs unicast issue. Now, how does one enable BIT7 (page 25 in the datasheet)? To recap, direct (unicast UDP works fine), however, to multicast IP it does not.