Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 8 Jan 2009 10:10:44 +0100
From:      Pieter de Goeje <pieter@degoeje.nl>
To:        freebsd-questions@freebsd.org
Cc:        Peter Steele <psteele@maxiscale.com>
Subject:   Re: Do UDP broadcasts work in FreeBSD?
Message-ID:  <200901081010.45049.pieter@degoeje.nl>
In-Reply-To: <2ACA3DE8F9758A48B8BE2C7A847F91F2479DF3@polaris.maxiscale.com>
References:  <2ACA3DE8F9758A48B8BE2C7A847F91F2479DF3@polaris.maxiscale.com>

Next in thread | Previous in thread | Raw E-Mail | Index | Archive | Help
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




Want to link to this message? Use this URL: <http://docs.FreeBSD.org/cgi/mid.cgi?200901081010.45049.pieter>