-
Notifications
You must be signed in to change notification settings - Fork 0
/
infocast_network.cpp
79 lines (66 loc) · 2.47 KB
/
infocast_network.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
/*
* infocast - Make robots visible
* Copyright 2012-2013 Tobias Kalbitz.
* Subject to the AGPL, version 3.
*/
#include "infocast_network.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ifaddrs.h>
std::list<int> open_send_multicast_socket(struct sockaddr_in& saddr,
const char* ip, short port)
{
int status;
static const int on = 1;
std::list<int> sockets;
memset(&saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_port = htons(port);
saddr.sin_addr.s_addr = inet_addr(ip);
struct ifaddrs *addrs = NULL;
int result = getifaddrs(&addrs);
if (result < 0) {
perror("Couldn't get network interfaces!");
fprintf(stderr, "Error: getting iface list in file %s line %d. Exiting.\n", __FILE__, __LINE__);
exit(1);
}
const struct ifaddrs *cursor = addrs;
while (cursor != NULL) {
if (cursor->ifa_addr != NULL && cursor->ifa_addr->sa_family == AF_INET
&& !(cursor->ifa_flags & IFF_POINTOPOINT)
&& (cursor->ifa_flags & IFF_MULTICAST)
&& (cursor->ifa_flags & IFF_UP)) {
int sock = socket(AF_INET, SOCK_DGRAM, 0);
if(sock < 0) {
perror("Error creating socket");
fprintf(stderr, "Error: creating socket in file %s line %d. Exiting.\n", __FILE__, __LINE__);
exit(EXIT_FAILURE);
}
status = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on));
if(status) {
perror("setsockopt reuseaddr:");
fprintf(stderr, "Error: setsockopt failed in file %s line %d. Exiting.\n", __FILE__, __LINE__);
exit(EXIT_FAILURE);
}
status = setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF,
&((struct sockaddr_in *)cursor->ifa_addr)->sin_addr,
sizeof(struct in_addr));
if (status) {
perror("Couldn't set multicast interfaces");
fprintf(stderr, "Error: setsockopt failed in file %s line %d. Exiting.\n", __FILE__, __LINE__);
exit(EXIT_FAILURE);
}
sockets.push_front(sock);
}
cursor = cursor->ifa_next;
}
freeifaddrs(addrs);
return sockets;
}