We have several embedded systems that work on a small network, and these run some physical displays, part of which shows the network status for each embedded system. We are no longer able to get the wireless bridges we have been using up until now, and have switched to new ones, which no longer work with the old code used to detect other machines / wireless bridges on the network. The old code does the following:
char broadcast[27] = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 10, 10, 60, 213, 69, 120, 1, 0, 192, 1, 1, 0, 1, 0, 1, 0, 1 };
sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
addr.sin_addr.s_addr = htonl(-1);
addr.sin_port = htons(PORT);
addr.sin_family = AF_INET;
status = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &yes, sizeof(int));
status = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
status = bind(sock, (struct sockaddr *) &addr, sinlen); // bind socket
status = getsockname(sock, (struct sockaddr *) &addr, &sinlen);
status = sendto(sock, broadcast, sizeof(broadcast), 0, (struct sockaddr *) &addr, sinlen);
fd_set fds;
struct timeval timeout;
timeout.tv_sec = sec;
timeout.tv_usec = usec;
FD_ZERO(&fds);
FD_SET(socket, &fds);
select(socket+1, &fds, 0, 0, &timeout);
status = recvfrom(sock, buffer, buflen, 0, (struct sockaddr *) &addr, &sinlen);
some_addr = inet_ntoa(addr.sin_addr);
I understand that it's sending a UDP broadcast packet to the broadcast address for the subnet, but what I don't get is what the f#$@ is the magic payload that got this to work and why doesn't it work with our new equipment ?
To replace this code, I'm looking to essentially implement a ping 192.168.1.0 -b
command to ping the broadcast address using an ICMP packet. I've tried the code that was linked to at this question, but it doesn't work for broadcast addresses (presumably because it's not configured to do so). My question then is, how do I configure the code linked from the above question to sendto
a broadcast address and receive results from each machine on the network ?