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 ); 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.