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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
/*
* Author: Lon Hohberger <lhh at redhat.com>
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/time.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
/* Local includes */
#include "bcast.h"
#include "debug.h"
LOGSYS_DECLARE_SUBSYS ("XVM", SYSLOGLEVEL);
/**
Sets up a multicast receive socket
*/
int
ipv4_bcast_recv_sk(char *addr, int port)
{
int sock, val;
struct sockaddr_in sin;
/* Store broadcast address */
memset(&sin, 0, sizeof(sin));
sin.sin_family = PF_INET;
sin.sin_port = htons(port);
if (inet_pton(PF_INET, addr,
(void *)&sin.sin_addr.s_addr) < 0) {
log_printf(LOG_ERR, "Invalid broadcast address: %s\n", addr);
return -1;
}
dbg_printf(4, "Setting up ipv4 broadcast receive (%s:%d)\n", addr, port);
sock = socket(PF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
log_printf(LOG_ERR, "socket: %s\n", strerror(errno));
close(sock);
sock = -1;
return 1;
}
val = 1;
if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, 1, sizeof(val)) < 0) {
log_printf(LOG_ERR, "setsockopt: %s\n", strerror(errno));
close(sock);
return 1;
}
/*
* Bind broadcast address
*/
if (bind(sock, (struct sockaddr *) &sin,
sizeof(struct sockaddr_in)) < 0) {
printf("bind failed: %s\n", strerror(errno));
close(sock);
return -1;
}
dbg_printf(4, "%s: success, fd = %d\n", __FUNCTION__, sock);
return sock;
}
/**
Set up multicast send socket
*/
int
ipv4_bcast_send_sk(char *send_addr, char *addr, int port, struct sockaddr *tgt,
socklen_t tgt_len, int ttl)
{
int val;
struct ip_mreq mreq;
struct sockaddr_in mcast;
struct sockaddr_in src;
int sock;
if (tgt_len < sizeof(struct sockaddr_in)) {
errno = EINVAL;
return -1;
}
/* Store multicast address */
mcast.sin_family = PF_INET;
mcast.sin_port = htons(port);
if (inet_pton(PF_INET, addr,
(void *)&mcast.sin_addr.s_addr) < 0) {
printf("Invalid multicast address: %s\n", addr);
return -1;
}
mreq.imr_multiaddr.s_addr = mcast.sin_addr.s_addr;
/* Store sending address */
src.sin_family = PF_INET;
src.sin_port = htons(port);
if (inet_pton(PF_INET, send_addr,
(void *)&src.sin_addr.s_addr) < 0) {
printf("Invalid source address: %s\n", send_addr);
return -1;
}
mreq.imr_interface.s_addr = src.sin_addr.s_addr;
/*************************
* SET UP MULTICAST SEND *
*************************/
dbg_printf(4, "Setting up ipv4 multicast send (%s:%d)\n", addr, port);
sock = socket(PF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
perror("socket");
return -1;
}
/*
* Join Multicast group.
*/
dbg_printf(4, "Joining IP Multicast group (pass 1)\n");
if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,
sizeof(mreq)) == -1) {
printf("Failed to add multicast membership to transmit "
"socket %s: %s\n", addr, strerror(errno));
close(sock);
return -1;
}
/*
* Join Multicast group.
*/
dbg_printf(4, "Joining IP Multicast group (pass 2)\n");
if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, &src.sin_addr,
sizeof(src.sin_addr)) == -1) {
printf("Failed to bind multicast transmit socket to "
"%s: %s\n", addr, strerror(errno));
close(sock);
return -1;
}
/*
* set time to live to 2 hops.
*/
dbg_printf(4, "Setting TTL to %d for fd%d\n", ttl, sock);
val = ttl;
if (setsockopt(sock, SOL_IP, IP_MULTICAST_TTL, &val,
sizeof(val)))
printf("warning: setting TTL failed %s\n", strerror(errno));
memcpy((struct sockaddr_in *)tgt, &mcast, sizeof(struct sockaddr_in));
dbg_printf(4, "%s: success, fd = %d\n", __FUNCTION__, sock);
return sock;
}
/**
Sets up a multicast receive (ipv6) socket
*/
int
ipv6_recv_sk(char *addr, int port)
{
int sock, val;
struct ipv6_mreq mreq;
struct sockaddr_in6 sin;
memset(&mreq, 0, sizeof(mreq));
memset(&sin, 0, sizeof(sin));
sin.sin6_family = PF_INET6;
sin.sin6_port = htons(port);
if (inet_pton(PF_INET6, addr,
(void *)&sin.sin6_addr) < 0) {
printf("Invalid multicast address: %s\n", addr);
return -1;
}
memcpy(&mreq.ipv6mr_multiaddr, &sin.sin6_addr,
sizeof(struct in6_addr));
/********************************
* SET UP MULTICAST RECV SOCKET *
********************************/
dbg_printf(4, "Setting up ipv6 multicast receive (%s:%d)\n", addr, port);
sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
if (sock < 0) {
printf("socket: %s\n", strerror(errno));
close(sock);
sock = -1;
return 1;
}
/*
* When using Multicast, bind to the LOCAL address, not the MULTICAST
* address.
*/
memset(&sin, 0, sizeof(sin));
sin.sin6_family = PF_INET6;
sin.sin6_port = htons(port);
sin.sin6_addr = in6addr_any;
if (bind(sock, (struct sockaddr *) &sin,
sizeof(struct sockaddr_in6)) < 0) {
printf("bind failed: %s\n", strerror(errno));
close(sock);
return -1;
}
dbg_printf(4, "Disabling IP Multicast loopback\n");
val = 1;
if (setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val,
sizeof(val)) != 0) {
printf("Failed to disable multicast loopback\n");
close(sock);
return -1;
}
/*
* Join multicast group
*/
dbg_printf(4, "Joining IP Multicast group\n");
if (setsockopt(sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq,
sizeof(mreq)) == -1) {
printf("Failed to add multicast to socket %s: %s\n",
addr, strerror(errno));
close(sock);
return -1;
}
dbg_printf(4, "%s: success, fd = %d\n", __FUNCTION__, sock);
return sock;
}
/**
Set up ipv6 multicast send socket
*/
int
ipv6_send_sk(char *send_addr, char *addr, int port, struct sockaddr *tgt,
socklen_t tgt_len, int ttl)
{
int val;
struct ipv6_mreq mreq;
struct sockaddr_in6 mcast;
struct sockaddr_in6 src;
int sock;
if (tgt_len < sizeof(struct sockaddr_in6)) {
errno = EINVAL;
return -1;
}
memset(&mreq, 0, sizeof(mreq));
/* Store multicast address */
mcast.sin6_family = PF_INET6;
mcast.sin6_port = htons(port);
if (inet_pton(PF_INET6, addr,
(void *)&mcast.sin6_addr) < 0) {
printf("Invalid multicast address: %s\n", addr);
return -1;
}
memcpy(&mreq.ipv6mr_multiaddr, &mcast.sin6_addr,
sizeof(struct in6_addr));
/* Store sending address */
src.sin6_family = PF_INET6;
src.sin6_port = htons(port);
if (inet_pton(PF_INET6, send_addr,
(void *)&src.sin6_addr) < 0) {
printf("Invalid source address: %s\n", send_addr);
return -1;
}
/*************************
* SET UP MULTICAST SEND *
*************************/
dbg_printf(4, "Setting up ipv6 multicast send (%s:%d)\n", addr, port);
sock = socket(PF_INET6, SOCK_DGRAM, 0);
if (sock < 0) {
perror("socket");
return -1;
}
dbg_printf(4, "Disabling IP Multicast loopback\n");
val = 1;
if (setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val,
sizeof(val)) != 0) {
printf("Failed to disable multicast loopback\n");
close(sock);
return -1;
}
/*
* Join Multicast group.
*/
dbg_printf(4, "Joining IP Multicast group\n");
if (setsockopt(sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq,
sizeof(mreq)) == -1) {
printf("Failed to add multicast membership to transmit "
"socket %s: %s\n", addr, strerror(errno));
close(sock);
return -1;
}
/*
* Join Multicast group (part 2)
*/
/*
if (setsockopt(sock, IPPROTO_IPV6, IP_MULTICAST_IF, &src.sin6_addr,
sizeof(src.sin6_addr)) == -1) {
printf("Failed to bind multicast transmit socket to "
"%s: %s\n", addr, strerror(errno));
close(sock);
return -1;
}
*/
/*
* set time to live to 2 hops.
*/
val = ttl;
if (setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val,
sizeof(val)))
printf("warning: setting TTL failed %s\n", strerror(errno));
memcpy((struct sockaddr_in *)tgt, &mcast, sizeof(struct sockaddr_in6));
dbg_printf(4, "%s: success, fd = %d\n", __FUNCTION__, sock);
return sock;
}
|