Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Multicast UDP deamon and VLAN interfaces

Reply
Thread Tools

Multicast UDP deamon and VLAN interfaces

 
 
pietro.cerutti@gmail.com
Guest
Posts: n/a
 
      12-01-2006
Hi guys,
I have a daemon running on Debian and listening for multicast packets
sent to 224.0.0.251:5353 (ZeroConf Multicast DNS queries).
The server is plugged into a VLAN trunk with eth0 and joins several
VLANs using virtual interfaces (i.e. eth0.xxx). It should be able to
capture the UDP packets on any interfaces, so it spawns a thread for
each interface specified in a config file, and for each thread it
creates a socket:

/************************************************** ************************************/
// Inside a function whose parameters are:
// int *sfd - the socket file descriptor
// struct in_addr bound_ip - the IP on which to listen

#define PORT 5353
#define MGRP "224.0.0.0.251"

struct sockaddr_in addr;
struct ip_mreq mc;

*sfd = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );

bzero( &addr, sizeof( addr ) );
addr.sin_family = AF_INET;
addr.sin_port = htons( PORT );
addr.sin_addr = bound_ip;

bind( *sfd, (struct sockaddr *) &addr, sizeof(addr));

int flag = 1;
mc.imr_multiaddr.s_addr = inet_addr( MGRP );
mc.imr_interface = bound_ip;

setsockopt( *sfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mc, sizeof(mc));
setsockopt( *sfd, SOL_SOCKET, SO_REUSEADDR, (char *) &flag,
sizeof(flag));
setsockopt( *sfd, SOL_SOCKET, SO_REUSEPORT, (char *) &flag,
sizeof(flag));

/************************************************** ************************************/

The problem is that the server captures the UDP packets sent to the
Multicast group only when bound_ip is set to 0 (INADDR_ANY), and even
then, only when the packets are coming on the first interface (eth0).

If I see right, the problem could be that I want to capture packets
sent to 224.0.0.251 while listening on IP, say, 192.168.2.103. But even
then, how could one specify on which interface to listen for a
Multicast packet?

What I'd like is to
- be able to specify several interfaces and spawn one daemon for each
one
- receive Multicast UDP packets only on the specified interfaces

Any hints are appreciated

Thank you,

Regards

 
Reply With Quote
 
 
 
 
Maxim Yegorushkin
Guest
Posts: n/a
 
      12-01-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> Hi guys,
> I have a daemon running on Debian and listening for multicast packets
> sent to 224.0.0.251:5353 (ZeroConf Multicast DNS queries).
> The server is plugged into a VLAN trunk with eth0 and joins several
> VLANs using virtual interfaces (i.e. eth0.xxx). It should be able to
> capture the UDP packets on any interfaces, so it spawns a thread for
> each interface specified in a config file, and for each thread it
> creates a socket:
>
> /************************************************** ************************************/
> // Inside a function whose parameters are:
> // int *sfd - the socket file descriptor
> // struct in_addr bound_ip - the IP on which to listen
>
> #define PORT 5353
> #define MGRP "224.0.0.0.251"
>
> struct sockaddr_in addr;
> struct ip_mreq mc;
>
> *sfd = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
>
> bzero( &addr, sizeof( addr ) );
> addr.sin_family = AF_INET;
> addr.sin_port = htons( PORT );
> addr.sin_addr = bound_ip;
>
> bind( *sfd, (struct sockaddr *) &addr, sizeof(addr));
>
> int flag = 1;
> mc.imr_multiaddr.s_addr = inet_addr( MGRP );
> mc.imr_interface = bound_ip;
>
> setsockopt( *sfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mc, sizeof(mc));
> setsockopt( *sfd, SOL_SOCKET, SO_REUSEADDR, (char *) &flag,
> sizeof(flag));
> setsockopt( *sfd, SOL_SOCKET, SO_REUSEPORT, (char *) &flag,
> sizeof(flag));
>
> /************************************************** ************************************/
>
> The problem is that the server captures the UDP packets sent to the
> Multicast group only when bound_ip is set to 0 (INADDR_ANY), and even
> then, only when the packets are coming on the first interface (eth0).


UNIX® Network Programming Volume 1 by R.W.Stevens says:

If the local interface is specified as the wildcard address for IPv4
(INADDR_ANY) or as an index of 0 for IPv6, then a single local
interface is chosen by the kernel.

> If I see right, the problem could be that I want to capture packets
> sent to 224.0.0.251 while listening on IP, say, 192.168.2.103. But even
> then, how could one specify on which interface to listen for a
> Multicast packet?
>
> What I'd like is to
> - be able to specify several interfaces and spawn one daemon for each
> one
> - receive Multicast UDP packets only on the specified interfaces


man ip(7)

IP_ADD_MEMBERSHIP
Join a multicast group. Argument is an ip_mreqn
structure.

struct ip_mreqn {
struct in_addr imr_multiaddr; /* IP multicast group
address */
struct in_addr imr_address; /* IP address of local
interface */
int imr_ifindex; /* interface index */
};

imr_multiaddr contains the address of the multicast
group the
application wants to join or leave. It must be a valid
multi-
cast address. imr_address is the address of the local
interface
with which the system should join the multicast group; if
it is
equal to INADDR_ANY an appropriate interface is chosen
by the
system. imr_ifindex is the interface index of the
interface
that should join/leave the imr_multiaddr group, or 0 to
indicate
any interface.

For compatibility, the old ip_mreq structure is still
supported.
It differs from ip_mreqn only by not including the
imr_ifindex
field. Only valid as a setsockopt(2).

 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      12-01-2006
(E-Mail Removed) wrote:
>
> I have a daemon running on Debian and listening for multicast packets
> sent to 224.0.0.251:5353 (ZeroConf Multicast DNS queries).
> The server is plugged into a VLAN trunk with eth0 and joins several
> VLANs using virtual interfaces (i.e. eth0.xxx). It should be able to
> capture the UDP packets on any interfaces, so it spawns a thread for
> each interface specified in a config file, and for each thread it
> creates a socket:


Off topic. C knows nothing about multicast, servers, eth0, VLANs,
UDP, packets, threads, sockets, etc. Find a newsgroup that deals
with your system. comp.unix.programmer is one possibility, another
is something that has 'debian' in its name.

--
Some informative links:
<news:news.announce.newusers
<http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>


 
Reply With Quote
 
pietro.cerutti@gmail.com
Guest
Posts: n/a
 
      12-01-2006

Maxim Yegorushkin ha scritto:

> (E-Mail Removed) wrote:


> man ip(7)


> struct ip_mreqn {
> struct in_addr imr_multiaddr; /* IP multicast group
> address */
> struct in_addr imr_address; /* IP address of local
> interface */
> int imr_ifindex; /* interface index */
> };


Ok, i passed from ip_mreq to ip_mreqn, but that wasn't my problem:
it seems that the interface doesn't even join the multicast group:

Using this:
struct ip_mreqn mc;
mc.imr_multiaddr = conf.multicast_group;
mc.imr_address = bound_ip;
mc.imr_ifindex = 0;
setsockopt( *sfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mc, sizeof(mc))

I still can't see any packet come into the daemon, even when netcat'ing
to 224.0.0.251:5353 using UDP.
# nc -u 224.0.0.251 5353

It just seems that the daemon doesn't capture packets sent to the
multicast group.

Thanx in advance,

 
Reply With Quote
 
Maxim Yegorushkin
Guest
Posts: n/a
 
      12-01-2006

(E-Mail Removed) wrote:

> Maxim Yegorushkin ha scritto:
>
> > (E-Mail Removed) wrote:

>
> > man ip(7)

>
> > struct ip_mreqn {
> > struct in_addr imr_multiaddr; /* IP multicast group
> > address */
> > struct in_addr imr_address; /* IP address of local
> > interface */
> > int imr_ifindex; /* interface index */
> > };

>
> Ok, i passed from ip_mreq to ip_mreqn, but that wasn't my problem:
> it seems that the interface doesn't even join the multicast group:
>
> Using this:
> struct ip_mreqn mc;
> mc.imr_multiaddr = conf.multicast_group;
> mc.imr_address = bound_ip;
> mc.imr_ifindex = 0;
> setsockopt( *sfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mc, sizeof(mc))


Your new code is equivalent to the code you posted before. What happens
is that you let the kernel determine which one interface joins the
group. This is why you receive datagrams from one interface only. To
join a multicast group on a specific interface set imr_address as the
local address of that interface, rather than INADDR_ANY.

> I still can't see any packet come into the daemon, even when netcat'ing
> to 224.0.0.251:5353 using UDP.
> # nc -u 224.0.0.251 5353
>
> It just seems that the daemon doesn't capture packets sent to the
> multicast group.


Use netstat -gn to see which interface joined which group.

 
Reply With Quote
 
Pietro Cerutti
Guest
Posts: n/a
 
      12-01-2006


On 1 Dic, 14:29, "Maxim Yegorushkin" <(E-Mail Removed)>
wrote:
> (E-Mail Removed) wrote:
> > Maxim Yegorushkin ha scritto:

>
> > > (E-Mail Removed) wrote:

>
> > > man ip(7)

>
> > > struct ip_mreqn {
> > > struct in_addr imr_multiaddr; /* IP multicast group
> > > address */
> > > struct in_addr imr_address; /* IP address of local
> > > interface */
> > > int imr_ifindex; /* interface index */
> > > };

>
> > Ok, i passed from ip_mreq to ip_mreqn, but that wasn't my problem:
> > it seems that the interface doesn't even join the multicast group:

>
> > Using this:
> > struct ip_mreqn mc;
> > mc.imr_multiaddr = conf.multicast_group;
> > mc.imr_address = bound_ip;
> > mc.imr_ifindex = 0;
> > setsockopt( *sfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mc, sizeof(mc))


>Your new code is equivalent to the code you posted before.

Please note that bound_ip is actually the IP address of the interface
this thread is responsible to.

>
> > It just seems that the daemon doesn't capture packets sent to the
> > multicast group.Use netstat -gn to see which interface joined which group.


Thanx

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-01-2006
CBFalconer <(E-Mail Removed)> writes:
> (E-Mail Removed) wrote:
>> I have a daemon running on Debian and listening for multicast packets
>> sent to 224.0.0.251:5353 (ZeroConf Multicast DNS queries).
>> The server is plugged into a VLAN trunk with eth0 and joins several
>> VLANs using virtual interfaces (i.e. eth0.xxx). It should be able to
>> capture the UDP packets on any interfaces, so it spawns a thread for
>> each interface specified in a config file, and for each thread it
>> creates a socket:

>
> Off topic. C knows nothing about multicast, servers, eth0, VLANs,
> UDP, packets, threads, sockets, etc. Find a newsgroup that deals
> with your system. comp.unix.programmer is one possibility, another
> is something that has 'debian' in its name.


Note that this thread is also cross-posted to comp.unix.programmer,
comp.protocols.tcp-ip, and comp.os.linux.networking. But yes, it's
off-topic for comp.lang.c.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      12-01-2006
Keith Thompson wrote:
>
> CBFalconer <(E-Mail Removed)> writes:
> > (E-Mail Removed) wrote:
> >> I have a daemon running on Debian and listening for multicast packets
> >> sent to 224.0.0.251:5353 (ZeroConf Multicast DNS queries).
> >> The server is plugged into a VLAN trunk with eth0 and joins several
> >> VLANs using virtual interfaces (i.e. eth0.xxx). It should be able to
> >> capture the UDP packets on any interfaces, so it spawns a thread for
> >> each interface specified in a config file, and for each thread it
> >> creates a socket:

> >
> > Off topic. C knows nothing about multicast, servers, eth0, VLANs,
> > UDP, packets, threads, sockets, etc. Find a newsgroup that deals
> > with your system. comp.unix.programmer is one possibility, another
> > is something that has 'debian' in its name.

>
> Note that this thread is also cross-posted to comp.unix.programmer,
> comp.protocols.tcp-ip, and comp.os.linux.networking. But yes, it's
> off-topic for comp.lang.c.


True. I rarely look at the cross-post list. If I had I would have
set follow-ups, as I have done now.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
UDP multicast problem with winsock Gary C C++ 2 05-19-2010 12:18 PM
UDP Multicast Question neilsolent Java 2 01-22-2009 04:49 AM
Python sockets UDP broadcast multicast question?? inorlando Python 3 08-29-2008 05:49 AM
udp multicast server and client skpopu@gmail.com C Programming 3 04-25-2008 07:00 PM
Multicast UDP deamon and VLAN interfaces pietro.cerutti@gmail.com C Programming 0 12-01-2006 08:38 AM



Advertisments