LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   UDP Broadcast Issue (https://www.linuxquestions.org/questions/linux-networking-3/udp-broadcast-issue-702707/)

raviskar 02-06-2009 06:31 AM

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

michaelk 02-06-2009 07:28 AM

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/

raviskar 02-06-2009 07:48 AM

Thanks. I will continue in the other thread.


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