LinuxQuestions.org
Review your favorite Linux distribution.
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-09-2012, 06:19 AM   #1
jonshouse
LQ Newbie
 
Registered: Jan 2012
Posts: 9

Rep: Reputation: Disabled
UDP broadcast from multiple IPs on interface


To test an application I want to send UDP broadcast packets that look like they are are generated from mutliple computers.

I've setup a number of IPs for an interface

ifconfig eth0:1 10.10.10.221
ifconfig eth0:2 10.10.10.222
ifconfig eth0:3 10.10.10.223 etc

In code I want to send udp broadcast data from each interface (address)

In C I have an array interface_ips containing the IP addresses 10.10.10.221 etc, then in code I do this :

Code:
        int broadcast=1;
        struct sockaddr_in sendaddr[32];
        // Now we have a table of ip addresses we need to open a sending socket for each "interface" (address)
        for (i=0;i<NUM_SENDING_IPS;i++)
        {
                if((sockfd[i] = socket(PF_INET,SOCK_DGRAM,0)) == -1)
                {
                        perror("sockfd");
                        exit(1);
                }
                if((setsockopt(sockfd[i],SOL_SOCKET,SO_BROADCAST,&broadcast,sizeof broadcast)) == -1)
                {
                        perror("setsockopt - SO_SOCKET ");
                        exit(1);
                }

                // Fill in an array of this structure
                sendaddr[i].sin_family = AF_INET;
                sendaddr[i].sin_port = htons(PORT);
                sendaddr[i].sin_addr.s_addr = inet_addr(interface_ips[i]);
                memset(sendaddr[i].sin_zero,'\0',sizeof(sendaddr[i].sin_zero));
        }

Later in the code I send a packet to each socket
Code:
                // for now send same packet to each interface (silence)
                for (i=0;i<NUM_SENDING_IPS;i++)
                {
                        numbytes = sendto(sockfd[i], audio, sizeof(audio), 0, (struct sockaddr *)&sendaddr[i], sizeof(sendaddr[i]));
                        if (numbytes<=0)
                        {
                                perror("sendto error");
                                exit(1);
                        }
                }

If I run the receiving code on the same machine I get data, but the data never hits the LAN ?

Anyone know my mistake/magic setting. I've tried fiddling with ifconfig and broadcast addresses but it does not seem to help.

Thanks,
Jon
 
Old 02-09-2012, 10:03 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
If you want the message that you are sending to be broadcasted, you should be sending it to INADDR_BROADCAST. Sending the message to a particular interface (IP address) will, obviously, send the message only to one interface.
 
Old 02-09-2012, 10:22 AM   #3
jonshouse
LQ Newbie
 
Registered: Jan 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
>If you want the message that you are sending to be broadcasted, you should be sending it to INADDR_BROADCAST. Sending >the message to a particular interface (IP address) will, obviously, send the message only to one interface.

Yes, ok .. and as I predicted all frames now originate from the lowest IP (eth0) ... I'm now sending 258 packets a second from one IP not 43 packets per second from 6 ips.

How do I send broadcast frames originating from eth0:1 eth0:2 etc, is it even possible?

I've tried setsockopt SO_BINDTODEVICE but that just rejects eth0:1
 
Old 02-11-2012, 04:45 AM   #4
jonshouse
LQ Newbie
 
Registered: Jan 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
Just for reference this is not possible with linux UDP sockets. I resorted to crafting raw ethernet frames with UDP broadcast content inside.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Getting things straight: Apache, SSL, Multiple External IPs / Internal IPs robin.com.au Linux - Server 21 10-13-2007 11:39 PM
broadcast using udp kingraja84 Programming 1 05-23-2007 05:59 AM
Adding Multiple Ips to 1 Interface kemplej Slackware 1 12-27-2004 09:18 AM
how to define a specific range of IPs and/or multiple IPs in an iptables rule?... TheHellsMaster Linux - Security 9 09-20-2004 10:06 AM
multiple ips, multiple gateways, one interface drpixel Linux - Networking 6 12-04-2002 12:56 AM

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

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