Solved

UDP Broadcast Message in C using Linux

Posted on 2004-09-09
7
1,800 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
Comment Utility
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
Comment Utility
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
Comment Utility
I mean I use Ethereal to monitor the packets
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
linux rpm install 5 184
A "Helper" for python 2 84
generating rpm using noarch parameter 1 35
Issues with Ports in Linux (Hyper-V Guest) 2 76
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 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

11 Experts available now in Live!

Get 1:1 Help Now