This is the mail archive of the ecos-discuss@sourceware.org mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

How to send UDP broadcast to 255.255.255.255?


I've been asked by one of my internal customers how to send a
UDP broadcast packet to IP address 255.255.255.255.

I tried setting the SO_BROADCAST option on the socket and then
using sendto() with a destination address of 255.255.255.255,
but it sends to the subnet broadcast address (in my case
10.255.255.255) not to the global broadcast address of
255.255.255.255 that I specified in the sendto() call.

The exact same code works fine on Linux (the destination
address in the packet on the wire is 255.255.255.255), so this
is apparently platform-dependent.  The code I'm using is shown
below.  I've tried it both with and without the "bind()" call,
and it does the same thing in both cases.

Any ideas?  Is this normal BSD stack behavior?  If so, on BSD
how does one send a global broadcast?


#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>

static void sendBroadcastUdp(void)
{
  int socket_fd;
  struct sockaddr_in encoder_addr;
  struct sockaddr_in my_addr;
  char send_buf[20];
  int broadcast=1;
  int packetSize = 5;
  int bytesSent;

  if ((socket_fd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
    {
      diag_printf("socket() failed\n");
      return;
    }

  my_addr.sin_family = AF_INET;
  my_addr.sin_port = htons(12345);
  my_addr.sin_addr.s_addr = INADDR_ANY;
  memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);

  #if 0
  if (bind(socket_fd, (struct sockaddr *)&my_addr, sizeof my_addr))
    {
      diag_printf("bind() failed\n");
      close(socket_fd);
      return;
    }
  #endif

  //mark the socket for broadcasting

  if (setsockopt(socket_fd, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof broadcast) == -1)
    {
      diag_printf("setsockopt (SO_BROADCAST) failed\n");
      close(socket_fd);
      return;
    }

  encoder_addr.sin_family = AF_INET;
  encoder_addr.sin_port = htons(12345);
  encoder_addr.sin_addr.s_addr = INADDR_BROADCAST;
  memset(encoder_addr.sin_zero, '\0', sizeof encoder_addr.sin_zero);

  if ((bytesSent = sendto(socket_fd, send_buf, packetSize, 0, (struct sockaddr *)&encoder_addr, sizeof encoder_addr)) == -1)
    {
      diag_printf("sendto() failed\n");
      close(socket_fd);
      return;
    }

  diag_printf("sent %d bytes to %s\n", bytesSent, inet_ntoa(encoder_addr.sin_addr));
  close(socket_fd);
}


-- 
Grant Edwards                   grante             Yow! I know th'MAMBO!!
                                  at               I have a TWO-TONE CHEMISTRY
                               visi.com            SET!!


-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]