Solved

UDP Broadcast Message in C using Linux

Posted on 2004-09-09
7
1,808 Views
Last Modified: 2009-12-11
Hello
Can somebody give me a simple example on sending and receving a UDP broadcast message?
I am using C and Linux.
0
Comment
Question by:sctccomm
  • 3
  • 2
7 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 12024136
Its nearly the same as sending normal UDP messages. .. you need to use setsockopt for setting the socket to broadcast

SO_BROADCAST
Set or get the broadcast flag. When enabled, datagram sockets receive packets sent to a broadcast address and they are allowed to send packets to a broadcast address. This option has no effect on stream-oriented sockets.

man 2 setsockopt
man 7 socket

here is some theory on it
http://elqui.dcsc.utfsm.cl/util/redes/www.cisco.com/univercd/cc/td/doc/cisintwk/ics/cs006.htm

Source code for an irdp daemon is available by anonymous FTP at ftp.cisco.com.
0
 

Author Comment

by:sctccomm
ID: 12029178
This is my code.  I use Ethernel to monitor the packet and I got a "Malformed Packet WCCP" on the packet I send

#include <stdio.h>            /* Standard input/output definitions */
#include <stdlib.h>
#include <string.h>            /* String function definitions */
#include <errno.h>
#include <unistd.h>            /* UNIX standard function definitions */
#include <fcntl.h>            /* File control definitions */

#include <signal.h>
#include <sys/time.h>
#include <sys/msg.h>      /* Message Queue */

#include <sys/types.h>
#include <sys/socket.h>      /* Ethernet socket */
#include <netinet/in.h>      
#include <arpa/inet.h>

#define LOCAL_PORT      3820
#define REMOTE_PORT      3820

int remote_sock_fd;                  //Ethernet port file descriptor
struct sockaddr_in remote_addr;      
int remote_addr_len, rx_addr_len;

unsigned int number=0;

void send_data(int signal)
{
      char tx_buf[128];
      int send_size;

      printf("Send Number:%d\n",number);        
      tx_buf[0] = number++;
      
      send_size = sendto(remote_sock_fd, tx_buf, 1, 0, (struct sockaddr *)&remote_addr, remote_addr_len);
      printf("Send Size:%d\n",send_size);
}


int main(int argc, char *argv[])
{
      const int on = 1;
      struct itimerval itval;

      char rx_buf[128];
      int rx_size;
      
      bzero(&remote_addr, sizeof(remote_addr));
      remote_addr.sin_family = AF_INET;                 // host byte order
      remote_addr.sin_port = htons(REMOTE_PORT);       // short, network byte order
      remote_addr.sin_addr.s_addr = inet_addr("192.168.254.255");
      remote_addr_len = sizeof(remote_addr);

      remote_sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
      if (remote_sock_fd == -1)
      {
          perror("Socket Error");
          exit(1);
        }
      
      setsockopt(remote_sock_fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
      
      signal(SIGALRM, send_data);

      itval.it_interval.tv_sec = 2;
      itval.it_interval.tv_usec = 000000;

      itval.it_value.tv_sec = 2;
      itval.it_value.tv_usec = 000000;
      
      setitimer(ITIMER_REAL,&itval,0);
      
      while(1)
      {
            pause();
/*            rx_size = recvfrom(remote_sock_fd, rx_buf, 1, 0,  (struct sockaddr *)&remote_addr, &rx_addr_len);
            if(rx_size > 0)
            {
                  printf("Receive Number:%d\n",rx_buf[0]);
            }
*/
      }
      
      exit(EXIT_SUCCESS);
}
0
 

Author Comment

by:sctccomm
ID: 12029188
I mean I use Ethereal to monitor the packets
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 45

Expert Comment

by:sunnycoder
ID: 12051557
Hi sctccomm,

Were you able to solve the problem? If yes, then pls post the solution. If no and you are still looking for a solution, I would suggest leaving it open for a while.

GhostMod,

In case of no response, I am ok with delete/Refund

sunnycoder
0
 

Author Comment

by:sctccomm
ID: 12055763
I found that I need to "bind" the socket and after that, everything runs fine.
0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
ID: 12083904
Please read entire thread.  Asker solved or content important.
Closed, 500 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Admin
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Equivalent solution for gettimezoneinformation 12 116
benchmark write speed to disk on linux 2 66
fsstat very high only on nfs client side rhel 10 101
Openwrt vnstat 9 155
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 Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now