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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
/*
Copyright (C) 2006 Renaissance Technologies Corp.
main developer: HP Wei <hp@rentec.com>
This file collects functions related to setting multicast
for multicatcher. They are IPv4 and IPv6 ready.
By default, we use IPv4.
To use IPv6, we need to specify -DIPv6 in Makefile.
The functions in this file are collected from
Richard Stevens' Networking bible: Unix Network programming
I added Mcast_join().
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING.
If not, write to the Free Software Foundation,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "main.h"
#include <net/if.h>
#ifdef _SUN
#include <sys/sockio.h> /* define SIOCGIFADDR */
#else
#include <linux/sockios.h>
#endif
#define SA struct sockaddr
int mcast_join(int sockfd, const SA *sa, socklen_t salen,
const char *ifname, u_int ifindex)
{
switch (sa->sa_family) {
case AF_INET: {
struct ip_mreq mreq;
struct ifreq ifreq;
memcpy(&mreq.imr_multiaddr,
&((struct sockaddr_in *) sa)->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_MEMBERSHIP,
&mreq, sizeof(mreq)));
}
#ifdef IPV6
case AF_INET6: {
struct ipv6_mreq mreq6;
memcpy(&mreq6.ipv6mr_multiaddr,
&((struct sockaddr_in6 *) sa)->sin6_addr,
sizeof(struct in6_addr));
if (ifindex > 0)
mreq6.ipv6mr_interface = ifindex;
else if (ifname != NULL)
if ( (mreq6.ipv6mr_interface = if_nametoindex(ifname)) == 0) {
errno = ENXIO; /* i/f name not found */
return(-1);
}
else
mreq6.ipv6mr_interface = 0;
return(setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
&mreq6, sizeof(mreq6)));
}
#endif
default:
errno = EPROTONOSUPPORT;
return(-1);
}
}
int Mcast_join(int sockfd, const char *mcast_addr,
const char *ifname, u_int ifindex)
{
#ifndef IPV6
/* IPv4 */
struct sockaddr_in sa;
sa.sin_family = AF_INET;
inet_pton(AF_INET, mcast_addr, &sa.sin_addr);
#else
struct sockaddr_in6 sa;
sa.sin6_family = AF_INET6;
inet_pton(AF_INET6, mcast_addr, &sa.sin6_addr);
#endif
return (mcast_join(sockfd, (struct sockaddr *) &sa, sizeof(sa),
ifname, ifindex));
}
void sock_set_addr(struct sockaddr *sa, socklen_t salen, const void *addr)
{
switch (sa->sa_family) {
case AF_INET: {
struct sockaddr_in *sin = (struct sockaddr_in *) sa;
memcpy(&sin->sin_addr, addr, sizeof(struct in_addr));
return;
}
#ifdef IPV6
case AF_INET6: {
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) sa;
memcpy(&sin6->sin6_addr, addr, sizeof(struct in6_addr));
return;
}
#endif
}
return;
}
|