forked from unpbook/unpv13e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcast_set_if.c
63 lines (56 loc) · 1.38 KB
/
mcast_set_if.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "unp.h"
#include <net/if.h>
int
mcast_set_if(int sockfd, const char *ifname, u_int ifindex)
{
switch (sockfd_to_family(sockfd)) {
case AF_INET: {
struct in_addr inaddr;
struct ifreq ifreq;
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(&inaddr,
&((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr,
sizeof(struct in_addr));
} else
inaddr.s_addr = htonl(INADDR_ANY); /* remove prev. set default */
return(setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_IF,
&inaddr, sizeof(struct in_addr)));
}
#ifdef IPV6
case AF_INET6: {
u_int idx;
if ( (idx = ifindex) == 0) {
if (ifname == NULL) {
errno = EINVAL; /* must supply either index or name */
return(-1);
}
if ( (idx = if_nametoindex(ifname)) == 0) {
errno = ENXIO; /* i/f name not found */
return(-1);
}
}
return(setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
&idx, sizeof(idx)));
}
#endif
default:
errno = EAFNOSUPPORT;
return(-1);
}
}
void
Mcast_set_if(int sockfd, const char *ifname, u_int ifindex)
{
if (mcast_set_if(sockfd, ifname, ifindex) < 0)
err_sys("mcast_set_if error");
}