Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receiving broadcast packet addressed to 255.255.255.255 in C++

I have a device that is discovered by sending a broadcast packet to 255.255.255.255 on port 4930 and the device responds by sending a packet back to 255.255.255.255 on port 4930.

I have a snippet of C++ code which can send a packet to 255.255.255.255 on port 4930 (both source and destination port), but it can't receive a packet back from the broadcast address 255.255.255.255.

I can see the device is working fine, wireshark can see the packets coming back and forth and the propriety software supplied with the device can discover the device just fine, the problem is with the C++ program so please keep on topic with your responses.

Now, as I have said I can send a packet just find, but firstly I can't bind to the IP address 255.255.255.255 to receive the packets. I can change the multicast address to 239.255.255.250 and the socket will bind but I need the address 255.255.255.255.

My snippet of code is below, I am using VC++2010

bool CPTUProgramDlg::FindPTU(u_short port, const char * Destaddress){
    {
        //Data to send
        char packet_data[10] = {0x44,0x43,0x55,0x44,0x5f,0x50,0x49,0x4e,0x47,0x00};
        int packet_size=10;

        SOCKET sock;
        struct sockaddr_in addr;

        sock = socket(AF_INET, SOCK_DGRAM, 0);

        // set SO_BROADCAST on a socket to true (1): (so we can transmit to 255 addr)
        //In order to use broadcast the options of socket must change
        char broadcastON = 1;
        setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &broadcastON, sizeof broadcastON);

        if (sock < 0)
            return false;

        addr.sin_family = AF_INET;
        addr.sin_port = htons(port);
        addr.sin_addr.s_addr = inet_addr(Destaddress); // Specify dest IP

        sendto(sock, packet_data, packet_size, 0, (struct sockaddr*)&addr, sizeof(addr));

        if (bind(sock,(struct sockaddr*) &addr,sizeof(addr)) != -1){
            char Buff[512];
            recv(sock,Buff,512,0);
        }

        closesocket(sock);
    }

    return 1;
}

Wireshark screenshot to prove packets are being send:

enter image description here

like image 671
ALM865 Avatar asked Oct 02 '22 16:10

ALM865


1 Answers

From the wireshark output its seen that the special device is using broadcast to communicate and will use the same port number as source and destination.

Normal socket communication will require using matching port numbers but broadcast messages cannot be exchanged over the same socket, especially when the port numbers do not match as seen with wireshark.

Binding on 255.255.255.255 (INADDR_BROADCAST) should generally work but may be limited by your OS privileges and permissions.

You may try to solve the problem by using two sockets - one for receiving and one for sending. Of course the listening socket have to be setup first and bound to 0.0.0.0 (INADDR_ANY) and port 4930. In this case there is no easy way to filter by destination address (as I wrongly written in my comment) because most standard socket APIs do not provide a way to get the destination addess from the socket. On Linux there is an exception - IP_PKTINFO at SOL_IP...

By using recvfrom you will get the source unicast address of the responding device(s). You have to note that if you have more that one such device on your network you will get more than one response.

like image 78
bbonev Avatar answered Oct 05 '22 13:10

bbonev