upd broadcast in C

Hi

I want to send out udp broadcast to the whole subnet in the simplest way possible. I'm not familiar with socket.h to figure out what to do. Can someone give me some pointer? Sorry for the small amount of point, that's all i have for now. I'll add more once i get more. Thanx
LVL 3
1ceAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jcaldwelCommented:
A slightly simplified example:

#include <unistd.h>
#include <sys/socket.h>

int s, on;
struct sockaddr dst;
struct ifreq *ifr;
char sendbuff[ 1024 ];

/* Create And Set Up Socket */
s = socket(AF_INET, SOCK_DGRAM, 0);

on = 1;
if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof on)
== -1) {
  perror(LOG_ERR, "setsockopt SO_BROADCAST");
  return;
}

/* Get The Broadcast address */
if (ioctl(s, SIOCGIFBRDADDR, (char *) ifr) < 0) {
  perror( "ioctl" );
  return;
}

memcpy((char *) &dst, (char *) &ifr->ifr_broadaddr, sizeof ifr->ifr_broadaddr);


/* Send the message */

sendto(s, sendbuf, 1024, 0, (struct sockaddr *)&dst, sizeof (dst));


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
1ceAuthor Commented:
Hi
when i compile this give an error:

if (ioctl(s, SIOCGIFBRDADDR, (char *) ifr) < 0) {

sizeof applied to an incomplete types

i'm on freebsd 4.7
0
jcaldwelCommented:
Try changing:

memcpy((char *) &dst, (char *) &ifr->ifr_broadaddr, sizeof ifr->ifr_broadaddr);
to:
memcpy((char *) &dst, (char *) &ifr->ifr_broadaddr, sizeof(struct sockaddr_in));

and

if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof on)

To
if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(int))


Add add an "#include <sys/ioctl.h>" to the top
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

1ceAuthor Commented:
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>

int main(int argc, char **argv){
   int s, on;
   struct sockaddr dst;
   struct ifreq *ifr;
   char sendbuff[ 1024 ]="balbalbal";
   //char *sendaddr="10.10.255.255";
   /* Create And Set Up Socket */
   s = socket(AF_INET, SOCK_DGRAM, 0);
   on = 1;

   if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(int)) == -1) {
      printf("setsockopt SO_BROADCAST");
      return 0;
   }

   if (ioctl(s,SIOCGIFBRDADDR, (char *) ifr) < 0){
      printf( "ioctl error" );
      return;
   }
   memcpy((char *) &dst, (char *)&ifr->ifr_broadaddr,sizeof(struct sockaddr_in));
   sendto(s, sendbuff, 1024, 0, (struct sockaddr *)&dst, sizeof (dst));

}

----------------------------------------------

It still giving the same error msg when compile. =(

>>if (ioctl(s,SIOCGIFBRDADDR, (char *) ifr) < 0){
socket_udp_bd.c:21: sizeof applied to an incomplete type

>>memcpy((char *) &dst, (char *)&ifr->ifr_broadaddr,sizeof(struct sockaddr_in));
socket_udp_bd.c:25: dereferencing pointer to incomplete type
socket_udp_bd.c:25: sizeof applied to an incomplete type

0
1ceAuthor Commented:
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>

int main(int argc, char **argv){
   int s, on;
   struct sockaddr dst;
   struct ifreq *ifr;
   char sendbuff[ 1024 ]="balbalbal";
   //char *sendaddr="10.10.255.255";
   /* Create And Set Up Socket */
   s = socket(AF_INET, SOCK_DGRAM, 0);
   on = 1;

   if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(int)) == -1) {
      printf("setsockopt SO_BROADCAST");
      return 0;
   }

   if (ioctl(s,SIOCGIFBRDADDR, (char *) ifr) < 0){
      printf( "ioctl error" );
      return;
   }
   memcpy((char *) &dst, (char *)&ifr->ifr_broadaddr,sizeof(struct sockaddr_in));
   sendto(s, sendbuff, 1024, 0, (struct sockaddr *)&dst, sizeof (dst));

}

----------------------------------------------

It still giving the same error msg when compile. =(

>>if (ioctl(s,SIOCGIFBRDADDR, (char *) ifr) < 0){
socket_udp_bd.c:21: sizeof applied to an incomplete type

>>memcpy((char *) &dst, (char *)&ifr->ifr_broadaddr,sizeof(struct sockaddr_in));
socket_udp_bd.c:25: dereferencing pointer to incomplete type
socket_udp_bd.c:25: sizeof applied to an incomplete type

0
1ceAuthor Commented:
anyhow, i got it figured out, thanx
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux OS Dev

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.