LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-06-2009, 06:42 AM   #1
raviskar
LQ Newbie
 
Registered: Feb 2009
Posts: 4

Rep: Reputation: 0
UDP Broadcast Issue


Hi,

I have posted the same question in Linux-Networking section, but thought I will post this in programming section also.

I have a problem in receiving UDP broadcast data. My machine has 2 network interfaces. I have a simple application which listens for the UDP broadcast data from the external servers. Our application does not receive any data in the sockets, but we can see the broadcast data in tcpdump capture.

Is there any possible reason on why our sockets cannot receive the UDP broadcast, but the data can be seen in the capture?

Also we have iptables enabled with default set of rules(we have not edited or added any rule). Whether the UDP broadcast is dropped by default by iptables? Do we need to have any explicit rule configured for accepting UDP broadcast.

I have given our sample application code below. Please help.

Thanks,
Ravi.

Code:
-----
#define PORT 6515

int main(int argc, char *argv[])
{
int sockfd;
char buf[30];
struct sockaddr_in sendaddr;
struct sockaddr_in recvaddr;
int numbytes;
socklen_t addr_len;
int broadcast=1;
int reuse=1;

if ((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}
if ((setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
&reuse, sizeof reuse)) == -1) {
perror("setsockopt - SO_SOCKET ");
exit(1);
}
if ((setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST,
&broadcast, sizeof broadcast)) == -1) {
perror("setsockopt - SO_SOCKET ");
exit(1);
}
printf("Socket created\n");
memset(&recvaddr, 0, sizeof recvaddr);
recvaddr.sin_family = AF_INET;
recvaddr.sin_port = htons(PORT);
recvaddr.sin_addr.s_addr = INADDR_ANY;
if (bind(sockfd, (struct sockaddr*)&recvaddr, sizeof recvaddr) == -1) {
perror("bind");
exit(1);
}
for (; {
int n;
fd_set set;
struct timeval time_500ms = { 0, 500*1000 };
FD_ZERO(&set);
FD_SET(sockfd, &set);

n = select(sockfd+1, &set, NULL, NULL, &time_500ms);
if (n < 0) {
perror("select");
break;
}
else if (n == 0) {
printf("sleep(5)\n");
sleep(5);
}
else if (!FD_ISSET(sockfd, &set)) {
perror("FD_ISSET");
break;
}
else {
addr_len = sizeof sendaddr;
if ((numbytes = recvfrom(sockfd, buf, sizeof buf, 0,
(struct sockaddr *)&sendaddr, &addr_len)) > 0)
{
time_t now = time(NULL);
printf("recvfrom: '%.*s' at %s\n", numbytes, buf, ctime(&now));
}
else
perror("recvfrom");
}
}
close(sockfd);
return 0;
}


IPtables default configuration:
------------------------------

# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
RH-Firewall-1-INPUT all -- anywhere anywhere
Chain FORWARD (policy ACCEPT)
target prot opt source destination
RH-Firewall-1-INPUT all -- anywhere anywhere
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain RH-Firewall-1-INPUT (2 references)
target prot opt source destination
ACCEPT all -- anywhere anywhere
ACCEPT icmp -- anywhere anywhere icmp any
ACCEPT esp -- anywhere anywhere
ACCEPT ah -- anywhere anywhere
ACCEPT udp -- anywhere 224.0.0.251 udp dpt:mdns
ACCEPT udp -- anywhere anywhere udp dpt:ipp
ACCEPT tcp -- anywhere anywhere tcp dpt:ipp
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh
REJECT all -- anywhere anywhere reject-with icmp-host-prohibited

Last edited by raviskar; 02-06-2009 at 06:50 AM.
 
Old 02-07-2009, 07:49 PM   #2
krizzz
Member
 
Registered: Oct 2004
Location: NY
Distribution: Slackware
Posts: 200

Rep: Reputation: 30
You may also need to set SO_BINDTODEVICE option to bind to particular interface. I'm not sure but you can try.
 
Old 02-09-2009, 05:41 AM   #3
raviskar
LQ Newbie
 
Registered: Feb 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Hi Krizzz,

Many thanks for your response. We got it working. It turned out that iptables Accept rule was missing for the second interface.

The iptables --list shows the same line as follows

ACCEPT all -- anywhere anywhere

for any of the following config

-A RH-Firewall-1-INPUT -j ACCEPT
-A RH-Firewall-1-INPUT -i lo -j ACCEPT
-A RH-Firewall-1-INPUT -i eth0 -j ACCEPT

We understood wrongly from the iptables --list output that ACCEPT rule was configured for all the interfaces, but it was configured only for one of the interfaces. After adding the same rule for the second interface, we got the data correctly.

Thanks,
Ravi.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
UDP Broadcast Issue raviskar Linux - Networking 2 02-06-2009 07:48 AM
broadcast using udp kingraja84 Programming 1 05-23-2007 05:59 AM
UDP broadcast receiver estratos Programming 1 03-14-2007 07:58 AM
udp broadcast from one subnet to another thru a linux router siva_bhavani Linux - Networking 1 12-30-2004 10:20 AM
how to broadcast UDP datagrams sokar Programming 2 02-14-2004 05:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration