Broadcast

What is broadcast

Broadcast is sent to everybody that wants to receive it, but he MUST be on the same physical network, because by default routers/gateways do not forward BROADCAST messages.
Another limitation of broadcast is the MTU of interface, by default a broadcast packet cannot exceed the max MTU size of an interface (for ethernet is 1460 (1500 - 20 (IP Header) - 20 (UDP Header)).

Broadcast address could be 255.255.255.255 or the network broadcast address. (old broadcast address was 0.0.0.0 now this is known as the network address)
For example network 192.168.20.0 with netmask 255.255.255.0 has a broadcast address of 192.168.20.255 (192.168.20.0 might work for backward compatibility)

Do you know your IP address, do you have a netmask, and you want to compute your network/broadcast address ?
Your network address is: < IP Address > & Netmask
Your Broadcast address is: < IP Address > | ( ˜ Netmask )

!!! This table might be wrong and is for sure not complete !!!

NetworkNetmask Broadcast address
10.0.0.0FF000000 (255.0.0.0) (8 bit) 10.255.255.255
10.0.0.0FF800000 (255.128.0.0)(9 bit) 10.127.255.255
10.0.0.0FFD00000 (255.192.0.0)(10 bit) 10.63.255.255
10.0.0.0FFE0000 (255.224.0.0)(11 bit) 10.31.255.255
10.0.0.0FFF00000 (255.240.0.0)(12 bit) 10.15.255.255
10.0.0.0FFF80000 (255.248.0.0)(13 bit) 10.7.255.255
10.0.0.0FFFD0000 (255.252.0.0)(14 bit) 10.3.255.255
10.0.0.0FFFE0000 (255.254.0.0)(15 bit) 10.1.255.255
10.0.0.0FFFF0000 (255.255.0.0)(16 bit) 10.0.255.255
10.0.0.0FFFF8000 (255.255.128.0)(17 bit) 10.0.127.255
10.0.0.0FFFFD000 (255.255.192.0)(18 bit) 10.0.63.255
10.0.0.0FFFFE000 (255.255.224.0)(19 bit) 10.0.31.255
10.0.0.0FFFFF000 (255.255.240.0)(20 bit) 10.0.15.255
10.0.0.0FFFFF800 (255.255.248.0)(21 bit) 10.0.7.255
10.0.0.0FFFFFD00 (255.255.252.0)(22 bit) 10.0.3.255
10.0.0.0FFFFFE00 (255.255.254.0)(23 bit) 10.0.1.255
10.0.0.0FFFFFF00 (255.255.255.0)(24 bit) 10.0.0.255
10.0.0.0FFFFFF80 (255.255.255.128)(25 bit) 10.0.0.127
10.0.0.0FFFFFFD0 (255.255.255.192)(26 bit) 10.0.0.63
10.0.0.0FFFFFFE0 (255.255.255.224)(27 bit) 10.0.0.31
10.0.0.0FFFFFFF0 (255.255.255.240)(28 bit) 10.0.0.15
10.0.0.0FFFFFFF8 (255.255.255.248)(29 bit) 10.0.0.7
10.0.0.0FFFFFFFD (255.255.255.252)(30 bit) 10.0.0.3

Sample code

This is a sample broadcast server without error handling
----------- cut here ------------------
/* $Id$ */

/*
 * build instructions:
 *
 * gcc -o bserver bserver.c
 *
 * Usage:
 * ./bserver
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#define MAXBUF 65536

int main()
{
  int sock, status, buflen;
  unsigned sinlen;
  char buffer[MAXBUF];
  struct sockaddr_in sock_in;
  int yes = 1;

  sinlen = sizeof(struct sockaddr_in);
  memset(&sock_in, 0, sinlen);

  sock = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP);

  sock_in.sin_addr.s_addr = htonl(INADDR_ANY);
  sock_in.sin_port = htons(0);
  sock_in.sin_family = PF_INET;

  status = bind(sock, (struct sockaddr *)&sock_in, sinlen);
  printf("Bind Status = %d\n", status);

  status = getsockname(sock, (struct sockaddr *)&sock_in, &sinlen);
  printf("Sock port %d\n",htons(sock_in.sin_port));

  buflen = MAXBUF;
  memset(buffer, 0, buflen);
  status = recvfrom(sock, buffer, buflen, 0, (struct sockaddr *)&sock_in, &sinlen);
  printf("sendto Status = %d\n", status);

  shutdown(sock, 2);
  close(sock);
}
----------- cut here ------------------
This is a sample broadcast client without error handling
----------- cut here ------------------
/* $Id$ */

/*
 * build instructions
 *
 * gcc -o bclient bclient.c
 *
 * Usage:
 * ./bclient <serverport>
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#define MAXBUF 65536

int main(int argc, char*argv[])
{
  int sock, status, buflen, sinlen;
  char buffer[MAXBUF];
  struct sockaddr_in sock_in;
  int yes = 1;

  sinlen = sizeof(struct sockaddr_in);
  memset(&sock_in, 0, sinlen);
  buflen = MAXBUF;

  sock = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP);

  sock_in.sin_addr.s_addr = htonl(INADDR_ANY);
  sock_in.sin_port = htons(0);
  sock_in.sin_family = PF_INET;

  status = bind(sock, (struct sockaddr *)&sock_in, sinlen);
  printf("Bind Status = %d\n", status);

  status = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &yes, sizeof(int) );
  printf("Setsockopt Status = %d\n", status);

  /* -1 = 255.255.255.255 this is a BROADCAST address,
     a local broadcast address could also be used.
     you can comput the local broadcat using NIC address and its NETMASK 
  */ 

  sock_in.sin_addr.s_addr=htonl(-1); /* send message to 255.255.255.255 */
  sock_in.sin_port = htons(atoi(argv[1])); /* port number */
  sock_in.sin_family = PF_INET;

  sprintf(buffer, "Ciao");
  buflen = strlen(buffer);
  status = sendto(sock, buffer, buflen, 0, (struct sockaddr *)&sock_in, sinlen);
  printf("sendto Status = %d\n", status);

  shutdown(sock, 2);
  close(sock);
}
----------- cut here ------------------