Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

upd broadcast in C

Posted on 2003-03-12
6
Medium Priority
?
333 Views
Last Modified: 2010-05-18
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
0
Comment
Question by:1ce
  • 4
  • 2
6 Comments
 
LVL 1

Accepted Solution

by:
jcaldwel earned 120 total points
ID: 8123231
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
 
LVL 3

Author Comment

by:1ce
ID: 8123866
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
 
LVL 1

Expert Comment

by:jcaldwel
ID: 8123900
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:1ce
ID: 8124920
#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
 
LVL 3

Author Comment

by:1ce
ID: 8124921
#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
 
LVL 3

Author Comment

by:1ce
ID: 8125193
anyhow, i got it figured out, thanx
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question