Do UDP broadcasts work in FreeBSD?

Pieter de Goeje pieter at degoeje.nl
Thu Jan 8 01:22:53 PST 2009


On Tuesday 06 January 2009 17:49:49 Peter Steele wrote:
> Our efforts so far indicate the answer is no, which baffles us. We want
> to send a limited broadcast to 255.255.255.255 but the message never
> arrives. The same code works fine under Linux. Is there a trick for
> doing this kind of thing under FreeBSD?

Did you enable SO_BROADCAST and IP_ONESBCAST on the socket? I remember needing 
this on FreeBSD but not on Linux. I know UDP broadcasting works fine, but is 
somewhat more involved:

addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("130.89.191.255");
addr.sin_port = htons(UDP_PORT_ET);

optval = 1;
if(setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &optval, sizeof optval) == -1)
	err(1, "setsockopt");

optval = 1;
if(setsockopt(sock, IPPROTO_IP, IP_ONESBCAST, &optval, sizeof optval) == -1)
	err(1, "setsockopt");

const char data[] = "report";

if(sendto(sock, data, sizeof data, 0, (struct sockaddr*)&addr, addrlen) == -1)
	warn("sendto");


This code will send a packet with destination address 255.255.255.255, on the 
interface with broadcast address 130.89.191.255. netintro(4) talks about how 
to discover these addresses. SO_ONESBCAST is documented in ip(4), SO_BROADCAST 
in getsockopt(4).

-- 
Pieter de Goeje



More information about the freebsd-questions mailing list