Sign in to follow this  

Sending out a broadcast packet in linux.

This topic is 3661 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;
} 

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 3661 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this