#include <errno.h> #include <net/if.h> #include <sys/socket.h> #define SA struct sockaddr int mcast_join_source_group(int sockfd, const SA *src, socklen_t srclen,const SA *grp, socklen_t grplen, const char *ifname, u_int ifindex) { #ifdef MCAST_JOIN_SOURCE_GROUP struct group_source_req req; if (ifindex > 0) { req.gsr_interface = ifindex; } else if (ifname != NULL) { if ( (req.gsr_interface = if_nametoindex(ifname)) == 0) { errno = ENXIO; /* i/f name not found */ return(-1); } } else { req.gsr_interface = 0; } if (grplen > sizeof(req.gsr_group) || srclen > sizeof(req.gsr_source)) { errno = EINVAL; return -1; } memcpy(&req.gsr_group, grp, grplen); memcpy(&req.gsr_source, src, srclen); return (setsockopt(sockfd, family_to_level(grp->sa_family), MCAST_JOIN_SOURCE_GROUP, &req, sizeof(req))); #else switch (grp->sa_family) { #ifdef IP_ADD_SOURCE_MEMBERSHIP case AF_INET: { struct ip_mreq_source mreq; struct ifreq ifreq; memcpy(&mreq.imr_multiaddr, &((struct sockaddr_in *) grp)->sin_addr, sizeof(struct in_addr)); memcpy(&mreq.imr_sourceaddr, &((struct sockaddr_in *) src)->sin_addr, sizeof(struct in_addr)); if (ifindex > 0) { if (if_indextoname(ifindex, ifreq.ifr_name) == NULL) { errno = ENXIO; /* i/f index not found */ return(-1); } goto doioctl; } else if (ifname != NULL) { strncpy(ifreq.ifr_name, ifname, IFNAMSIZ); doioctl: if (ioctl(sockfd, SIOCGIFADDR, &ifreq) < 0) { return(-1); } memcpy(&mreq.imr_interface, &((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr, sizeof(struct in_addr)); } else { mreq.imr_interface.s_addr = htonl(INADDR_ANY); } return(setsockopt(sockfd, IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP,&mreq, sizeof(mreq))); } #endif #ifdef IPV6 /* IPv6 source-specific API is MCAST_JOIN_SOURCE_GROUP */ case AF_INET6: {} #endif default: { errno = EAFNOSUPPORT; return(-1); } } #endif }