Maintaining a broadcast UDP packet up to 255.255.255.255

first poster, hope this is easy:

I need to send a broadcast packet to a piece of equipment that, when it is turned on, is on a different subnet than my machine, in order to tell it to reset its IP address in what's on my network. But I can’t transfer my own subnet unless I use DHCP, and ultimately I can’t do it. There is no router on the network, just a simple switch between my machine and the box I'm trying to talk to, plus a couple of other Linux machines on the network.

So basically this is the WORKS code example on Fedora 19 in a test environment (on a larger network where I have DHCP enabled) until I try to statically set my IP address:

int main(int argc, char *argv[]) { int sock; if( (sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { perror("socket : "); return -1; } int broadcast = 1; if( setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast)) != 0 ) { perror("setsockopt : "); close(sock); return -1; } char *ip = "255.255.255.255"; char * msg = "Hello World!"; struct sockaddr_in si; si.sin_family = AF_INET; si.sin_port = htons( 4444 ); inet_aton( ip, &si.sin_addr.s_addr ); /* send data */ size_t nBytes = sendto(sock, msg, strlen(msg), 0, (struct sockaddr*) &si, sizeof(si)); printf("Sent msg: %s, %d bytes with socket %d to %s\n", msg, nBytes, sock, ip); return 0; } 

If I use DHCP, the output is:

 Sent msg: Hello World!, 12 bytes with socket 3 to 255.255.255.255 

And I see how the package goes to Wireshark.

Then, if I set my IP statically, 192.168.100.1, I get:

 Sent msg: Hello World!, -1 bytes with socket 3 to 255.255.255.255 

And I do not see the package in Wireshark. And I can confirm that the number of TX packets displayed in ifconfig is not increasing. I tried disabling the firewall:

 sudo iptables -F 

but it did nothing. Does anyone see what I am missing? I can broadcast to 192.168.100.255, but it will not reach the mailbox I need to talk to, for example, by default it can be 192.168.200.10, 255.255.255.0. I could make it work by changing the network settings of everything else on the network, but this is not really an option.

EDIT: As for some comments and answers, please note that I am not using a router and cannot replicate behavior with anything but the wire between my computer and another field. So, the real question is why does Linux not send packets? I don’t know, obviously, but I suspect that Linux itself transmits broadcast packets to the cross-subnet if it cannot delegate this solution to another authority on the network. In any case, given that my network is so small, I will need to get around it.

+7
source share
3 answers

I just ran into this problem. Everything worked fine until I removed the gateway, and then I get a network inaccessibility error when I try to send from the socket.

Since I use broadcast messages to configure the network settings of my window, there is no alternative for me to configure the gateway address for it to work. However, since my box has only one network interface, I found the following solution:

// Bind to the proper interface to be able to send without default gateway char netif[] = "eth0"; setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, netif, sizeof(netif));

With this, I can send the address on the socket to the broadcast address 255.255.255.255, regardless of any default gateway. This solution should work with multiple network interfaces if you know which interface you want to use.

+4
source

Broadcast by definition is limited to one subnet. It would be terrible to see the broadcast coming out of the same subnet and overwhelming the Internet. If you are looking for a way to cross-subnet, you should take a look at Multicast . Also check out. Good luck

+2
source

Please check your default gateway. If the gateway's IP address is on a different subnet, you will receive a "Network is unavailable" error message. after some time of broadcasting to the address 255.255.255.255, by the way, sending directly to the broadcasting of the subnet (for example, 192.168.1.255 if your ip is 192.168.1.1) will work correctly, even if the IP address of the gateway is on a different subnet.

+1
source

Source: https://habr.com/ru/post/957353/


All Articles