summaryrefslogtreecommitdiff
path: root/src/mcast.c
blob: 70205d8683696a618a4983f5bf89b0920f8054af (plain)
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/*
 * (C) 2006-2009 by Pablo Neira Ayuso <pablo@netfilter.org>
 * 
 * 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 of the License, 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; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Description: multicast socket library
 */

#include "mcast.h"
#include "debug.h"

#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <errno.h>

struct mcast_sock *mcast_server_create(struct mcast_conf *conf)
{
	int yes = 1;
	union {
		struct ip_mreq ipv4;
		struct ipv6_mreq ipv6;
	} mreq;
	struct mcast_sock *m;
	socklen_t socklen = sizeof(int);

	m = (struct mcast_sock *) malloc(sizeof(struct mcast_sock));
	if (!m)
		return NULL;
	memset(m, 0, sizeof(struct mcast_sock));

	m->interface_idx = conf->interface_idx;

	switch(conf->ipproto) {
	case AF_INET:
		mreq.ipv4.imr_multiaddr.s_addr = conf->in.inet_addr.s_addr;
		mreq.ipv4.imr_interface.s_addr =conf->ifa.interface_addr.s_addr;

	        m->addr.ipv4.sin_family = AF_INET;
	        m->addr.ipv4.sin_port = htons(conf->port);
	        m->addr.ipv4.sin_addr.s_addr = htonl(INADDR_ANY);

		m->sockaddr_len = sizeof(struct sockaddr_in); 
		break;

	case AF_INET6:
		memcpy(&mreq.ipv6.ipv6mr_multiaddr, &conf->in.inet_addr6,
		       sizeof(uint32_t) * 4);
		mreq.ipv6.ipv6mr_interface = conf->ifa.interface_index6;

		m->addr.ipv6.sin6_family = AF_INET6;
		m->addr.ipv6.sin6_port = htons(conf->port);
		m->addr.ipv6.sin6_addr = in6addr_any;

		m->sockaddr_len = sizeof(struct sockaddr_in6);
		break;
	}

	if ((m->fd = socket(conf->ipproto, SOCK_DGRAM, 0)) == -1) {
		debug("mcast_sock_server_create:socket");
		free(m);
		return NULL;
	}

	if(conf->iface[0]) {
		struct ifreq ifr;

		strncpy(ifr.ifr_name, conf->iface, sizeof(ifr.ifr_name));

		if (ioctl(m->fd, SIOCGIFMTU, &ifr) == -1) {
			debug("ioctl");
			close(m->fd);
			free(m);
			return NULL;
		}
		conf->mtu = ifr.ifr_mtu;
	}

	if (setsockopt(m->fd, SOL_SOCKET, SO_REUSEADDR, &yes, 
				sizeof(int)) == -1) {
		debug("mcast_sock_server_create:setsockopt1");
		close(m->fd);
		free(m);
		return NULL;
	}

#ifndef SO_RCVBUFFORCE
#define SO_RCVBUFFORCE 33
#endif

	if (conf->rcvbuf &&
	    setsockopt(m->fd, SOL_SOCKET, SO_RCVBUFFORCE, &conf->rcvbuf,
				sizeof(int)) == -1) {
		/* not supported in linux kernel < 2.6.14 */
		if (errno != ENOPROTOOPT) {
			debug("mcast_sock_server_create:setsockopt2");
			close(m->fd);
			free(m);
			return NULL;
		}
	}

	getsockopt(m->fd, SOL_SOCKET, SO_RCVBUF, &conf->rcvbuf, &socklen);

	if (bind(m->fd, (struct sockaddr *) &m->addr, m->sockaddr_len) == -1) {
		debug("mcast_sock_server_create:bind");
		close(m->fd);
		free(m);
		return NULL;
	}

	switch(conf->ipproto) {
	case AF_INET:
		if (setsockopt(m->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
			       &mreq.ipv4, sizeof(mreq.ipv4)) < 0) {
			debug("mcast_sock_server_create:setsockopt2");
			close(m->fd);
			free(m);
			return NULL;
		}
		break;
	case AF_INET6:
		if (setsockopt(m->fd, IPPROTO_IPV6, IPV6_JOIN_GROUP,
			       &mreq.ipv6, sizeof(mreq.ipv6)) < 0) {
			debug("mcast_sock_server_create:setsockopt2");
			close(m->fd);
			free(m);
			return NULL;
		}
		break;
	}

	return m;
}

struct mcast_sock_multi *
mcast_server_create_multi(struct mcast_conf *conf, int conf_len)
{
	struct mcast_sock_multi *m;
	int i, j;

	if (conf_len <= 0 || conf_len > MCAST_LINKS_MAX)
		return NULL;

	m = calloc(sizeof(struct mcast_sock_multi), 1);
	if (m == NULL)
		return NULL;

	m->max_mtu = INT_MAX;
	for (i=0; i<conf_len; i++) {
		m->multi[i] = mcast_server_create(&conf[i]);
		if (m->multi[i] == NULL) {
			for (j=0; j<i; j++) {
				mcast_server_destroy(m->multi[j]);
			}
			free(m);
			return NULL;
		}
		if (m->max_mtu > conf[i].mtu)
			m->max_mtu = conf[i].mtu;
	}
	m->num_links = conf_len;

	return m;
}

void mcast_server_destroy(struct mcast_sock *m)
{
	close(m->fd);
	free(m);
}

void mcast_server_destroy_multi(struct mcast_sock_multi *m)
{
	int i;

	for (i=0; i<m->num_links; i++)
		mcast_server_destroy(m->multi[i]);
	free(m);
}

static int 
__mcast_client_create_ipv4(struct mcast_sock *m, struct mcast_conf *conf)
{
	int no = 0;

	m->addr.ipv4.sin_family = AF_INET;
	m->addr.ipv4.sin_port = htons(conf->port);
	m->addr.ipv4.sin_addr = conf->in.inet_addr;
	m->sockaddr_len = sizeof(struct sockaddr_in); 

	if (setsockopt(m->fd, IPPROTO_IP, IP_MULTICAST_LOOP, &no,
		       sizeof(int)) < 0) {
		debug("mcast_sock_client_create:setsockopt2");
		close(m->fd);
		return -1;
	}

	if (setsockopt(m->fd, IPPROTO_IP, IP_MULTICAST_IF,
		       &conf->ifa.interface_addr,
		       sizeof(struct in_addr)) == -1) {
		debug("mcast_sock_client_create:setsockopt3");
		close(m->fd);
		return -1;
	}

	return 0;
}

static int 
__mcast_client_create_ipv6(struct mcast_sock *m, struct mcast_conf *conf)
{
	int no = 0;

	m->addr.ipv6.sin6_family = AF_INET6;
	m->addr.ipv6.sin6_port = htons(conf->port);
	memcpy(&m->addr.ipv6.sin6_addr,
	       &conf->in.inet_addr6,
	       sizeof(struct in6_addr));
	m->sockaddr_len = sizeof(struct sockaddr_in6); 

	if (setsockopt(m->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &no,
		       sizeof(int)) < 0) {
		debug("mcast_sock_client_create:setsockopt2");
		close(m->fd);
		return -1;
	}

	if (setsockopt(m->fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
		       &conf->ifa.interface_index6,
		       sizeof(unsigned int)) == -1) {
		debug("mcast_sock_client_create:setsockopt3");
		close(m->fd);
		return -1;
	}

	return 0;
}

struct mcast_sock *mcast_client_create(struct mcast_conf *conf)
{
	int ret;
	struct mcast_sock *m;
	socklen_t socklen = sizeof(int);

	m = (struct mcast_sock *) malloc(sizeof(struct mcast_sock));
	if (!m)
		return NULL;
	memset(m, 0, sizeof(struct mcast_sock));

	m->interface_idx = conf->interface_idx;

	if ((m->fd = socket(conf->ipproto, SOCK_DGRAM, 0)) == -1) {
		debug("mcast_sock_client_create:socket");
		free(m);
		return NULL;
	}

	if (setsockopt(m->fd, SOL_SOCKET, SO_NO_CHECK, &conf->checksum, 
				sizeof(int)) == -1) {
		debug("mcast_sock_client_create:setsockopt1");
		close(m->fd);
		free(m);
		return NULL;
	}

#ifndef SO_SNDBUFFORCE
#define SO_SNDBUFFORCE 32
#endif

	if (conf->sndbuf &&
	    setsockopt(m->fd, SOL_SOCKET, SO_SNDBUFFORCE, &conf->sndbuf,
				sizeof(int)) == -1) {
		/* not supported in linux kernel < 2.6.14 */
		if (errno != ENOPROTOOPT) {
			debug("mcast_sock_server_create:setsockopt2");
			close(m->fd);
			free(m);
			return NULL;
		}
	}

	getsockopt(m->fd, SOL_SOCKET, SO_SNDBUF, &conf->sndbuf, &socklen);

	switch(conf->ipproto) {
		case AF_INET:
			ret = __mcast_client_create_ipv4(m, conf);
			break;
		case AF_INET6:
			ret = __mcast_client_create_ipv6(m, conf);
			break;
		default:
			ret = 0;
			break;
	}

	if (ret == -1) {
		close(m->fd);
		free(m);
		m = NULL;
	}

	return m;
}

struct mcast_sock_multi *
mcast_client_create_multi(struct mcast_conf *conf, int conf_len)
{
	struct mcast_sock_multi *m;
	int i, j;

	if (conf_len <= 0 || conf_len > MCAST_LINKS_MAX)
		return NULL;

	m = calloc(sizeof(struct mcast_sock_multi), 1);
	if (m == NULL)
		return NULL;

	m->max_mtu = INT_MAX;
	for (i=0; i<conf_len; i++) {
		m->multi[i] = mcast_client_create(&conf[i]);
		if (m->multi[i] == NULL) {
			for (j=0; j<i; j++) {
				mcast_client_destroy(m->multi[j]);
			}
			free(m);
			return NULL;
		}
		if (m->max_mtu > conf[i].mtu)
			m->max_mtu = conf[i].mtu;
	}
	m->num_links = conf_len;

	return m;
}

void mcast_client_destroy(struct mcast_sock *m)
{
	close(m->fd);
	free(m);
}

void mcast_client_destroy_multi(struct mcast_sock_multi *m)
{
	int i;

	for (i=0; i<m->num_links; i++)
		mcast_client_destroy(m->multi[i]);
	free(m);
}

ssize_t mcast_send(struct mcast_sock *m, void *data, int size)
{
	ssize_t ret;
	
	ret = sendto(m->fd, 
		     data,
		     size,
		     0,
		     (struct sockaddr *) &m->addr,
		     m->sockaddr_len);
	if (ret == -1) {
		debug("mcast_sock_send");
		m->stats.error++;
		return ret;
	}

	m->stats.bytes += ret;
	m->stats.messages++;  

	return ret;
}

ssize_t mcast_recv(struct mcast_sock *m, void *data, int size)
{
	ssize_t ret;
	socklen_t sin_size = sizeof(struct sockaddr_in);

        ret = recvfrom(m->fd,
		       data, 
		       size,
		       0,
		       (struct sockaddr *)&m->addr,
		       &sin_size);
	if (ret == -1) {
		debug("mcast_sock_recv");
		m->stats.error++;
		return ret;
	}

	m->stats.bytes += ret;
	m->stats.messages++;

	return ret;
}

void mcast_set_current_link(struct mcast_sock_multi *m, int i)
{
	m->current_link = m->multi[i];
}

struct mcast_sock *mcast_get_current_link(struct mcast_sock_multi *m)
{
	return m->current_link;
}

int mcast_get_fd(struct mcast_sock *m)
{
	return m->fd;
}

int mcast_get_current_ifidx(struct mcast_sock_multi *m)
{
	return m->current_link->interface_idx;
}

int mcast_get_ifidx(struct mcast_sock_multi *m, int i)
{
	return m->multi[i]->interface_idx;
}

static int
mcast_snprintf_stats(char *buf, size_t buflen, char *ifname,
		     struct mcast_stats *s, struct mcast_stats *r)
{
	size_t size;

	size = snprintf(buf, buflen, "multicast traffic (active device=%s):\n"
				     "%20llu Bytes sent "
				     "%20llu Bytes recv\n"
				     "%20llu Pckts sent "
				     "%20llu Pckts recv\n"
				     "%20llu Error send "
				     "%20llu Error recv\n\n",
				     ifname,
				     (unsigned long long)s->bytes,
				     (unsigned long long)r->bytes,
				     (unsigned long long)s->messages,
				     (unsigned long long)r->messages,
				     (unsigned long long)s->error,
				     (unsigned long long)r->error);
	return size;
}

static int
mcast_snprintf_stats2(char *buf, size_t buflen, const char *ifname, 
		      const char *status, int active,
		      struct mcast_stats *s, struct mcast_stats *r)
{
	size_t size;

	size = snprintf(buf, buflen, 
			"multicast traffic device=%s status=%s role=%s:\n"
			"%20llu Bytes sent "
			"%20llu Bytes recv\n"
			"%20llu Pckts sent "
			"%20llu Pckts recv\n"
			"%20llu Error send "
			"%20llu Error recv\n\n",
			ifname, status, active ? "ACTIVE" : "BACKUP",
			(unsigned long long)s->bytes,
			(unsigned long long)r->bytes,
			(unsigned long long)s->messages,
			(unsigned long long)r->messages,
			(unsigned long long)s->error,
			(unsigned long long)r->error);
	return size;
}

void
mcast_dump_stats(int fd,
		 const struct mcast_sock_multi *s,
		 const struct mcast_sock_multi *r)
{
	int i;
	struct mcast_stats snd = { 0, 0, 0};
	struct mcast_stats rcv = { 0, 0, 0};
	char ifname[IFNAMSIZ], buf[512];
	int size;

	/* it is the same for the receiver, no need to do it twice */
	if_indextoname(s->current_link->interface_idx, ifname);

	for (i=0; i<s->num_links && i<r->num_links; i++) {
		snd.bytes	+= s->multi[i]->stats.bytes;
		snd.messages	+= s->multi[i]->stats.messages;
		snd.error	+= s->multi[i]->stats.error;
		rcv.bytes	+= r->multi[i]->stats.bytes;
		rcv.messages	+= r->multi[i]->stats.messages;
		rcv.error	+= r->multi[i]->stats.error;
	}
	size = mcast_snprintf_stats(buf, sizeof(buf), ifname, &snd, &rcv);
	send(fd, buf, size, 0);
}

void
mcast_dump_stats_extended(int fd,
			  const struct mcast_sock_multi *s,
			  const struct mcast_sock_multi *r,
			  const struct nlif_handle *h)
{
	int i;
	char buf[4096];
	int size = 0;

	for (i=0; i<s->num_links && i<r->num_links; i++) {
		int idx = s->multi[i]->interface_idx, active;
		unsigned int flags;
		char ifname[IFNAMSIZ];
		const char *status;

		if_indextoname(idx, ifname);
		nlif_get_ifflags(h, idx, &flags);
		active = (s->multi[i] == s->current_link);
		/* 
		 * IFF_UP shows administrative status
		 * IFF_RUNNING shows carrier status
		 */
		if (flags & IFF_UP) {
			if (!(flags & IFF_RUNNING))
				status = "NO-CARRIER";
			else
				status = "RUNNING";
		} else {
			status = "DOWN";
		}
		size += mcast_snprintf_stats2(buf+size, sizeof(buf),
					      ifname, status, active,
					      &s->multi[i]->stats,
					      &r->multi[i]->stats);
	}
	send(fd, buf, size, 0);
}