broadcast oddity

Daniel Braniss danny at cs.huji.ac.il
Mon Jul 18 14:41:13 UTC 2011


this code behaves correctly when run from a diskless host which booted via PXE,
but fails on a host that was booted from disk.
hint: the non working sends a packet with a non ethernet broadcast address and
an ip address of 255.255.255.255, the working version sets the ethernet address
to 0xffffffff and the ip to the network broadcast address.

what am I doing wrong?

danny
PS: what is the correct way to obtain the network broadcast address?

#include <sys/types.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdio.h>

void
bcast()
{
     int so, on;
     char msg[BUFSIZ];
     struct timespec t2;
     struct sockaddr_in	soin;

     if((so = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
	  perror("socket");
	  exit(-1);
     }
     on = 1;
     if(setsockopt(so, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on))) {
	  perror("setsockopt");
	  exit(-1);
     }

     bzero(&soin, sizeof(struct sockaddr_in));
     soin.sin_len = sizeof(struct sockaddr_in);
     soin.sin_family = AF_INET;
     soin.sin_addr.s_addr = INADDR_BROADCAST;
     soin.sin_port = htons(12345);
     while(1) {
	  clock_gettime(CLOCK_REALTIME, &t2);
	  sprintf(msg, "0x%016x", t2.tv_sec);
	  if(sendto(so, msg, strlen(msg)+1,
		    0, (struct sockaddr *)&soin, sizeof(struct sockaddr)) < 0) {
	       perror("sendto");
	       break;
	  }
	  sleep(10);
     }
}

main(int cc, char **vv)
{
     bcast();
}




More information about the freebsd-net mailing list