LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


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

Rep: Reputation: 0
UDP Broadcast Issue


Hi,

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:49 AM.
 
Old 02-06-2009, 07:28 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,703

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate.

continue here:
http://www.linuxquestions.org/questi...-issue-702710/
 
Old 02-06-2009, 07:48 AM   #3
raviskar
LQ Newbie
 
Registered: Feb 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks. I will continue in the other thread.
 
  


Closed Thread



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
broadcast using udp kingraja84 Programming 1 05-23-2007 05:59 AM
Problem with Broadcast UDP in two ethernet machine inakizi Linux - Networking 1 03-28-2007 03:56 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 > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 01:41 PM.

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