LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 01-17-2014, 03:09 AM   #1
ArshDeep
LQ Newbie
 
Registered: Jan 2014
Posts: 2

Rep: Reputation: Disabled
Post Socket does not respond to broadcasted packets !


Hi Team,

I am facing an issue related to UDP sockets on Linux. When a socket is bound to specif IP/Interface address rather than INADDR_ANY, does not send/receive broad casted packets. It can send/receive network packets addressed/destined to that particular IP only, broadcasted packets are discarded. So, broadcasting is totally blocked for such kind of applications/services.

What could be solution to this problem so that while using particular IP be able to listen/send broadcast packets ?

Any quick response is appreciated.

Regards,
Arsh
 
Old 01-17-2014, 09:55 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Can you show the relevant sections of your code here? Here's an example of socket() and bind() and then you use sendto() and recvfrom(). Are you getting good returns from all your system calls? Try binding to port 0.

Code:
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>

void main(void)
{
    int sd, ret;
    struct sockaddr_in addr;

    sd = socket(AF_INET, SOCK_DGRAM, 0);

    printf("Socket descriptor: %d\n", sd);

    if(sd < 0) {
        printf("Socket call error: %d\n", errno);
        return;
    }

    memset((char *)&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    addr.sin_port = htons(0);

    ret = bind(sd, (struct sockaddr *)&addr, sizeof(addr));

    printf("Return from bind: %d\n", ret);

    if(ret < 0) {
        printf("Bind call error: %d\n", errno);
        return;
    }
}
 
Old 01-21-2014, 11:14 PM   #3
ArshDeep
LQ Newbie
 
Registered: Jan 2014
Posts: 2

Original Poster
Rep: Reputation: Disabled
UDP socket bind to specific IP does not receive BROADCASTED PACKETS.

Thanks for your update.
But, please note that I am not using IN_ADDR_ANY in socket.
Please find below code snippet:

--------------------
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>

#define BUFLEN 512
#define PORT 9930

void check(char *s)
{
perror(s);
exit(1);
}

int main(void)
{
struct sockaddr_in si_me, si_other;
int s, i, slen=sizeof(si_other);
char buf[BUFLEN];
char serverIP[50];
int val = 1;

memset(&serverIP, 0, sizeof(serverIP));

if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
check("socket");

setsockopt(s, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val));

memset((char *) &si_me, 0, sizeof(si_me));
si_me.sin_family = AF_INET;
si_me.sin_port = htons(PORT);
strcpy(serverIP, "172.26.13.56");
si_me.sin_addr.s_addr = inet_addr(serverIP);
if (bind(s, &si_me, sizeof(si_me))==-1)
check("bind");

while(1)
{
if (recvfrom(s, buf, BUFLEN, 0, &si_other, &slen)==-1)
check("recvfrom()");
printf("Received packet from %s:%d\nData: %s\n\n",
inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf);
}

close(s);
return 0;
}
-----------

Please let us know if there is any solution to this problem.
 
Old 01-22-2014, 09:13 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
You have to be root and I believe you may have to be INADDR_ANY.
 
  


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
Broadcast packets failed to receive : socket beginner srieindhoven Linux - Networking 2 03-07-2011 04:17 AM
Wireshark UDP Packets Socket not set robtard Programming 3 10-27-2010 05:45 PM
QOS for packets received on a server socket scottbiker Programming 1 06-19-2008 02:55 AM
tcp socket takes ages to respond apfel Linux - Networking 1 12-07-2006 09:44 AM
droped packets? problem with socket program... kamel Programming 0 06-01-2004 10:58 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 01:21 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