Sending out a broadcast packet in linux.

Started by
3 comments, last by Ademan555 16 years, 4 months ago
I'm trying to send out a broadcast packet. I'm getting this error and i've been bangking my head on this for a while now. The following code below compiles but when I run it I get this error. 192.168.0.255 sendto:: Invalid argument As you can see the dst contains the correct information but sendto function keeps giving me the Invalid argument. All the arguments are valid and correct it seems (ie, no null). Thanks for your help. Hopefully this makes sense.

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <string.h>
#include <stdio.h>

int main()
{
  int s, on = 1;
  struct sockaddr_in sin;
  struct sockaddr_in dst;
  struct ifconf ifc;
  struct ifreq *ifr;
  char buf[2048];
  char buf2[] = "HELLO MOTHER";
  int n;

  s = socket(AF_INET, SOCK_DGRAM, 0);
  if(s < 0)
    perror("socket:");

  if(setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0)
    perror("setsockopt:");

  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr = htonl(INADDR_ANY);
  sin.sin_port = htons(2190);

  if(bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0)
    perror("bind:");

  ifc.ifc_len = sizeof(buf);
  ifc.ifc_buf = buf;
  if(ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0)
    perror("ioctl-conf:");

  ifr = ifc.ifc_req;
  for (n=ifc.ifc_len/sizeof(struct ifreq); --n >= 0; ifr++)
  {
    if(!strcmp(ifr->ifr_name, "eth0"))
    {
      if(ioctl(s, SIOCGIFFLAGS, (char *)ifr) < 0)
        perror("ioctl-flag:");
      break;
    }
  }

  if (ifr->ifr_flags & IFF_BROADCAST)
  {
    if(ioctl(s, SIOCGIFBRDADDR, (char *) ifr) < 0)
      perror("ioctl-addr:");
    memcpy((char *) &dst, (char *) &ifr->ifr_broadaddr, sizeof(ifr->ifr_broadaddr));
  } else
    printf("Not able to broadcast");

  printf("%s\n", inet_ntoa(dst.sin_addr));

  if(sendto(s, buf2, strlen(buf2), 0, (struct sockaddr *)&dst, sizeof(dst)) < 0)
    perror("sendto:");

  return 0;
} 
Advertisement
You forgot to set dst.sin_port. In a debug build it would be 0x00, so it would be an invalid argument.
That's... interesting. Usually, you just set SO_BROADCAST, and send to the Mars address (ff.ff.ff.ff) which translates to the local subnet (because it's filtered at the gateway/router level). That's a lot more portable, and doesn't need OS-specific ioctls. It also doesn't have permission problems -- some OSes don't allow non-root to do ifconfig, or send raw messages, for example.
enum Bool { True, False, FileNotFound };
Have u specified "dst"'s port?
In terms of UDP broadcast, u need specify a port number in order to send datagram.
And, UDP broadcast has a special broadcasting address: 255.255.255.255. That define is INADDR_BROADCAST, u haven't refered in ur program.

Here is a simple UDP broadcast code as follows.

SOCKET s;
BOOL bBroadcast;
char* sMsg = "I love u";
SOCKADDR_IN dst;

s = socket(AF_INET, SOCK_DGRAM, 0);
bBroadcast = TRUE;
setsockopt(s, SOL_SOCKET, SO_BROADCAST, (char*)&bBroadcast, sizeof(BOOL));
dst.sin_family = AF_INET;
dst.sin_adddr.s_addr = inet_addr(INADDR_BROADCAST);
dst.sin_port = htons(8000);
sendto(s, sMsg, strlen(sMsg), 0, (SOCKADDR*)&dst, sizeof(dst));

In fact, I myself don't like Broadcast, if concurrently a lot of processes send broadcast message, the network immediately slows to a standstill and everyone gets frustrated.
If I may ask, why are you sending broadcast packets? Most uses I can think of revolve around network discovery, and libavahi handles that exceptionally well.
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out his new home he rounded a corner and saw someone in Marine Dress Blues. He ran back to St. Peter and yelled "You lied to me! There are Marines in heaven!" St. Peter said "Who him? That's just God. He wishes he were a Marine."

This topic is closed to new replies.

Advertisement