Subject: Broadcast Packet
To: NetBSD Tech-Net <tech-net@netbsd.org>
From: Mike Pelley <mike@pelley.com>
List: tech-net
Date: 03/17/1999 18:43:58
I've written a little C program (see below) that sends a broadcast packet.
I set the destination address to INADDR_BROADCAST (or 255.255.255.255) so I
would hit all machines.  However, when my machine (NetBSD 1.3.2/i386) sends
the packet, it doesn't flag the packet as broadcast on the ethernet level,
so nobody hears me (i.e. tcpdump on another machine shows the packet, but
tcpdump -p doesn't).  The same program on a Windows machine does flag the
packet as broadcast on the ethernet level.

Have I left something out to make the packet properly broadcast?  If I set
the destination address to the broadcast address of my network, the packet
is set with the ethernet broadcast on.  This is not good for my application
as sometimes the destination machine is not on my subnet.

Thanks!

Mike.

---

#include    <stdio.h>
#include    <unistd.h>
#include    <sys/types.h>
#include    <sys/socket.h>
#include    <netinet/in.h>
#include    <err.h>

void main(int argc, char **argv)
{
    struct sockaddr_in  SocketAddress;
    int                 ClientSocket;
    int                 OpVal = 1, count;
    char              buffer[256];

    strcpy (buffer, "Anybody out there?");   /*other more complicated code
removed */

    SocketAddress.sin_family            = AF_INET;
    SocketAddress.sin_port              = htons((unsigned short)11111);
    SocketAddress.sin_addr.s_addr       = INADDR_BROADCAST;

    ClientSocket = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);

    count = setsockopt (ClientSocket, SOL_SOCKET, SO_BROADCAST, (void
*)&OpVal, sizeof(int));
    count = sendto (ClientSocket, buffer, strlen(buffer), 0, (struct
sockaddr *)&SocketAddress, sizeof(struct sockaddr));
    if (count == -1)
        err(1, "%s", "sendto");

    if (shutdown (ClientSocket, 2)) {
        fprintf(stderr, "Error: could not shutdown\n");
    }
    close (ClientSocket);
}