-
Notifications
You must be signed in to change notification settings - Fork 1
/
McastModuleInterface.cpp
296 lines (261 loc) · 8.13 KB
/
McastModuleInterface.cpp
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include "McastModuleInterface.h"
McastModuleInterface::McastModuleInterface(const vector<IfaceData>& ifaces,
const vector<string>& mcastAddresses, int mcastPort, bool useIpV6) :
mIfaces(ifaces), mMcastAddresses(mcastAddresses), mMcastPort(mcastPort), mIsIpV6(useIpV6)
{
string ipVer = (useIpV6)? "IPV6" : "IPV4";
// Print out all mcast addresses
std::stringstream stm;
stm << "(";
for (unsigned i = 0; i < mcastAddresses.size(); ++i)
{
if (0==i)
{
stm << mcastAddresses[i];
}
else
{
stm << "," << mcastAddresses[i];
}
}
stm << ")";
cout << "MCAST with " << ipVer << " " << stm.str() << " port " << mcastPort << endl;
mAckPort = mMcastPort + 1;
}
bool McastModuleInterface::isIpV6() const
{
return mIsIpV6;
}
bool McastModuleInterface::associateMcastWithIfaceName(int fd,
const char* ifaceName, bool isLoopBack)
{
// Set the TTL hop count
int opt = 6;
int res = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, (void*) &opt, sizeof(opt));
if (0 > res)
{
LOG_ERROR("sockopt TTL: " << strerror(errno));
return false;
}
// Allow broadcast on this socket.
opt = 1;
res = setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (void*) &opt, sizeof(opt));
if (0 > res)
{
LOG_ERROR("sockopt SO_BROADCAST: " << strerror(errno));
return false;
}
// Enable reuse ip/port
if (-1 == Common::setReuseSocket(fd))
{
LOG_ERROR("sockopt cannot reuse socket " << fd);
return false;
}
// Enable loop back if set.
opt = isLoopBack;
res = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, (void*) &opt, sizeof(opt));
if (0 > res)
{
LOG_ERROR("sockopt IP_MULTICAST_LOOP: " << strerror(errno));
return false;
}
// Next, bind socket to a fix port
in_addr_t meSinAddr = htonl(INADDR_ANY);
vector<string> meIps;
if (0 == Common::getIfaceIPFromIfaceName(ifaceName, meIps, false))
{
meSinAddr = inet_addr(meIps[0].c_str());
}
struct sockaddr_in me_addr;
memset((char*) &me_addr, 0, sizeof(me_addr));
me_addr.sin_port = htons(mMcastPort);
me_addr.sin_family = AF_INET;
me_addr.sin_addr.s_addr = meSinAddr;
if (-1 == ::bind(fd, (struct sockaddr*)&me_addr, sizeof(me_addr)))
{
LOG_ERROR("sockopt bind: " << strerror(errno));
return false;
}
// If iface name specified, set iface name for the socket
if (0 < strlen(ifaceName))
{
struct ip_mreqn ifaddrn;
memset(&ifaddrn, 0, sizeof(ifaddrn));
ifaddrn.imr_ifindex = if_nametoindex(ifaceName);
if (0 != (res = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &ifaddrn, sizeof(ifaddrn))))
{
LOG_ERROR("sockopt IP_MULTICAST_IF: " << strerror(errno));
}
}
return true;
}
bool McastModuleInterface::associateMcastV6WithIfaceName(int fd,
const char* ifaceName, bool isLoopBack)
{
// Set the TTL hop count
int opt = 6;
int res = setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, (void*) &opt, sizeof(opt));
if (0 > res)
{
LOG_ERROR("sockopt TTL: " << strerror(errno));
return false;
}
// Allow broadcast on this socket.
opt = 1;
res = setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (void*) &opt, sizeof(opt));
if (0 > res)
{
LOG_ERROR("sockopt SO_BROADCAST: " << strerror(errno));
return false;
}
// Enable reuse ip/port
if (-1 == Common::setReuseSocket(fd))
{
LOG_ERROR("sockopt cannot reuse socket " << fd);
return false;
}
// Enable loop back if set.
opt = isLoopBack;
res = setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, (void*) &opt, sizeof(opt));
if (0 > res)
{
LOG_ERROR("sockopt IPV6_MULTICAST_LOOP: " << strerror(errno));
return false;
}
// Bind the socket to the multicast port.
struct sockaddr_in6 bindAddr6;
memset(&bindAddr6, 0, sizeof(bindAddr6));
vector<string> meIps;
if (0 == Common::getIfaceIPFromIfaceName(ifaceName, meIps, true))
{
if (1 != inet_pton(AF_INET6, meIps[0].c_str(), &(bindAddr6.sin6_addr)))
{
LOG_ERROR("Error parsing address for " << ifaceName);
return -1;
}
}
bindAddr6.sin6_family = AF_INET6;
bindAddr6.sin6_port = htons(mMcastPort);
res = ::bind(fd, (struct sockaddr *) &bindAddr6, sizeof(bindAddr6));
if (0 > res)
{
LOG_ERROR("bind: " << strerror(errno));
return -1;
}
// If iface name specified, set iface name for the socket
if (0 < strlen(ifaceName))
{
unsigned ifIndex = if_nametoindex(ifaceName);
if (0 != (res = setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &ifIndex, sizeof(ifIndex))))
{
LOG_ERROR("sockopt IPV6_MULTICAST_IF: " << strerror(errno));
return false;
}
}
return true;
}
int McastModuleInterface::joinMcastIface(int sock, const char* ifaceName)
{
// Set the recv buffer size.
uint32_t buffSz = MCAST_BUFF_LEN;
int res = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &buffSz, sizeof(buffSz));
if (0 > res)
{
LOG_ERROR("sockopt BuffSz: " << strerror(errno));
return -1;
}
// Let's set reuse port & address to on to allow multiple binds per host.
if (-1 == Common::setReuseSocket(sock))
{
LOG_ERROR("can't reuse socket");
return -1;
}
// Bind the socket to the multicast port.
struct sockaddr_in bindAddr;
memset(&bindAddr, 0, sizeof(bindAddr));
bindAddr.sin_family = AF_INET;
bindAddr.sin_addr.s_addr = htonl(INADDR_ANY);
bindAddr.sin_port = htons(mMcastPort);
res = ::bind(sock, (struct sockaddr *) &bindAddr, sizeof(bindAddr));
if (0 > res && errno != EINVAL)
// ignore EINVAL for allowing multiple mcast interfaces in same socket
{
LOG_ERROR("bind: " << strerror(errno));
return -1;
}
// If ifaceName is specified, bind directly to that iface,
// otherwise bind to general interface
for (unsigned i = 0; i < mMcastAddresses.size(); ++i)
{
const string& mcastAddress = mMcastAddresses[i];
if (0 == strlen(ifaceName))
{
struct ip_mreq mcastReq;
mcastReq.imr_multiaddr.s_addr = inet_addr(mcastAddress.c_str());
mcastReq.imr_interface.s_addr = htonl(INADDR_ANY);
res = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void*) &mcastReq, sizeof(mcastReq));
}
else
{
struct ip_mreqn mcastReqn;
mcastReqn.imr_multiaddr.s_addr = inet_addr(mcastAddress.c_str());
mcastReqn.imr_ifindex = if_nametoindex(ifaceName);
res = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void*) &mcastReqn, sizeof(mcastReqn));
}
if (0 != res)
{
printf("Error: join mcast group<%s> interface<%s>: %s\n",
mcastAddress.c_str(), ifaceName, strerror(errno));
}
}
return res;
}
int McastModuleInterface::joinMcastIfaceV6(int sock, const char* ifaceName)
{
// Set the recv buffer size.
uint32_t buffSz = MCAST_BUFF_LEN;
int res = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &buffSz, sizeof(buffSz));
if (0 > res)
{
LOG_ERROR("sockopt BuffSz: " << strerror(errno));
return -1;
}
// Let's set reuse port to on to allow multiple binds per host.
if (-1 == Common::setReuseSocket(sock))
{
LOG_ERROR("can't reuse socket");
return -1;
}
// Bind the socket to the multicast port.
struct sockaddr_in6 bindAddr;
memset(&bindAddr, 0, sizeof(bindAddr));
bindAddr.sin6_family = AF_INET6;
bindAddr.sin6_port = htons(mMcastPort);
res = ::bind(sock, (struct sockaddr *) &bindAddr, sizeof(bindAddr));
if (0 > res && errno != EINVAL)
// ignore EINVAL for allowing multiple mcast interfaces in same socket
{
LOG_ERROR("bind: " << strerror(errno));
return -1;
}
// If ifaceName is specified, bind directly to that iface,
// otherwise bind to general interface
for (unsigned i = 0; i < mMcastAddresses.size(); ++i)
{
const string& mcastAddress = mMcastAddresses[i];
struct ipv6_mreq mcastReq;
if (inet_pton(AF_INET6, mcastAddress.c_str(), &mcastReq.ipv6mr_multiaddr) != 1)
{
LOG_ERROR("Error parsing address for " << mcastAddress);
return -1;
}
mcastReq.ipv6mr_interface = if_nametoindex(ifaceName); // no need to check since it will return 0 on error
res = setsockopt(sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (void*) &mcastReq, sizeof(mcastReq));
if (0 != res)
{
printf("ERR: join mcast GRP<%s> INF<%s> ERR<%s>\n", mcastAddress.c_str(), ifaceName,
strerror(errno));
}
}
return res;
}