2

I'm unable to receive the multicast packets sent by the server. I could see the packets being received via tcpdump. Can anyone please let me know what am I doing wrong here.Thanks.

struct ipv6_mreq mreq;
struct sockaddr_in6 servaddr;

sock = socket(AF_INET6,SOCK_DGRAM,0);

servaddr.sin6_family = AF_INET6;
servaddr.sin6_port = htons(61624);
servaddr.sin6_addr = in6addr_any;

inet_pton(AF_INET6,"ff38:40:2001::1",&mreq.ipv6mr_multiaddr);

mreq.ipv6mr_interface = 0;
setsockopt(sock,IPPROTO_IPV6,IPV6_JOIN_GROUP, &mreq,sizeof(mreq));
bind(sock,(struct sockaddr *)&servaddr,sizeof(servaddr));
/* using poll to receive data */
foo_l
  • 591
  • 2
  • 10
  • 28
  • Are you checking any of the function calls for error return values? – Remy Lebeau Nov 06 '12 at 08:09
  • Thanks fr the reply. Yes I have added the check for return values of setsockopt and bind. The setsockopt returned -1 . perror has thrown Invalid Arguement. – foo_l Nov 06 '12 at 10:21
  • Now after changing the line mreq.ipv6mr_multiaddr = servaddr.sin6_addr; to inet_pton(AF_INET6,"ff38:40:2001::1",&mreq.ipv6mr_multiaddr); I dont see the Invalid arguement error. But still not able to see the packets receiving. – foo_l Nov 06 '12 at 13:03

1 Answers1

1

Zero is invalid supposed to be "hey kernel, select one for me" interface index. This does not work for you, most probably because your routing table does not have explicit entries that match given multicast group, and default route goes over different interface.

Use if_nametoindex(3) to resolve interface name and store it into ipv6mr_interface member of struct ipv6_mreq.

Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
  • Thanks a lot. It worked. But from the web I saw that the setting of the interface index to zero will make the kernel choose default interface, is it not true? – foo_l Nov 06 '12 at 13:59
  • Yes, that's how it usually works. Check your routing table, you probably were binding the wrong interface. – Nikolai Fetissov Nov 06 '12 at 14:10
  • 1
    If you have multiple NICs installed, you definately should not be relying on the kernel picking the default interface for you. You need to be explicit about which interface you are interested in using. – Remy Lebeau Nov 06 '12 at 18:49