views:

46

answers:

2

Hello,

I need to receive a multicast stream but filter incoming packets by source MAC address on CentOS 5.5. I'm planning to use libpcap library. Is it possible to join/leave multicast group using libpcap? If yes, how to do that?

Thanks

+1  A: 

Sure, just construct and send the appropriate IGMP packets.

EJP
How to do that? What API I need to use?
Dima
The answer above is better than my suggestion. But if you can relate source MAC addresses to IP addresses, you would be better off doing it *all* in the application, no libpcap at all. Just connect the MC socket to the desred source IP address, that will filter out everything else. If there are more than one desired sources, you can run multiple MC sokcets on the same port.
EJP
I need not to receive a packets that my application is generating. I created the filter that pass all packets except the packets that coming from my own MAC address
Dima
There's a socket option for that. You're really doing this the hard way! Do it all with a UDP socket, it's easy. Well, easier.
EJP
What the option? Do you mean to use setsockopt()? I checked all possible options and did not found anything that can help
Dima
IP_MULTICAST_LOOP or IPv6_MULTICAST_LOOP. But even if you don't have it, it would be a lot simpler to check the IP address the incoming datagram was received from in your application code and forget about libpcap altogether. Multicast is hard enough already ;-)
EJP
thanks, it's working!
Dima
A: 

1.Create dummy socket: sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

2.Bind it: rc = bind(sd, (sockaddr*) &addr, sizeof(sockaddr_in));

3.Join multicast group:

ip_mreq mreq;
mreq.imr_interface.s_addr = htonl(InterfaceIp);
mreq.imr_multiaddr.s_addr = htonl(DestIp);
if (setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
  close(sd);
  // Error handle...
}

Don't send or receive packets using dummy socket

4.open pcap using pcap_open_live()

The general idea is use regular socket in order to "tell" kernel to send IGMP join packet, and after use pcap in order to capture packets.

Dima