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
|
#include "multicast.h"
#include "multicast_configuration.h"
#include "configuration.h"
#include "info.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/param.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <errno.h>
Multicast::Multicast(mcastconf_t &mcclientconf)
{
udp_buffer = NULL;
multicast_audio = mcclientconf.with_audio;
if(!UDPOpen(mcclientconf.addr.c_str(), mcclientconf.port))
MIaV::info->error("Error creating socket %s:%d",
mcclientconf.addr.c_str(),
mcclientconf.port);
int mtu;
if(MIaV::config->get("udp_packet_size", &mtu))
MIaV::info->error("Could not read the symbol [udp_packet_size] from the conf file!");
udp_buffer_size = mtu - 28;
if(udp_buffer_size < 1) udp_buffer_size = 1;
udp_buffer = new char[udp_buffer_size];
udp_buffer_pointer = udp_buffer;
MIaV::info->info("UDP packet buffer size %db", udp_buffer_size);
}
Multicast::~Multicast()
{
if(udp_buffer) delete udp_buffer;
}
int Multicast::Write(void* buf, int size)
{
if(!udp_buffer) return 0;
char *p = (char*)buf;
int left = udp_buffer_size - (udp_buffer_pointer - udp_buffer);
while(size) {
int to_copy = size > left ? left : size;
memcpy(udp_buffer_pointer, p, to_copy);
left-=to_copy;
udp_buffer_pointer += to_copy;
p+=to_copy;
size-=to_copy;
if(left == 0) {
write(sock, udp_buffer, udp_buffer_size);
left = udp_buffer_size;
udp_buffer_pointer = udp_buffer;
}
}
return size;
}
bool Multicast::is_address_multicast(unsigned long address)
{
if((address & 255) >= 224 && (address & 255) <= 239) {
MIaV::info->info("Address is multicast.");
return true;
}
MIaV::info->info("Address is NOT multicast.");
return false;
}
bool Multicast::UDPOpen(const char *address, int port)
{
int enable = 1L;
struct sockaddr_in stAddr;
struct sockaddr_in stLclAddr;
struct hostent * host;
stAddr.sin_family = AF_INET;
stAddr.sin_port = htons(port);
if((host = gethostbyname(address)) == NULL) return false;
stAddr.sin_addr = *((struct in_addr *) host->h_addr_list[0]);
if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
return false;
if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &enable, sizeof(unsigned long int)) < 0)
return false;
if(is_address_multicast(stAddr.sin_addr.s_addr)) {
struct ip_mreq stMreq;
stLclAddr.sin_family = AF_INET;
stLclAddr.sin_addr.s_addr = htonl(INADDR_ANY);
stLclAddr.sin_port = stAddr.sin_port;
if(bind(sock, (struct sockaddr*) & stLclAddr, sizeof(stLclAddr)) < 0) return false;
stMreq.imr_multiaddr.s_addr = stAddr.sin_addr.s_addr;
stMreq.imr_interface.s_addr = INADDR_ANY;
if(setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) & stMreq, sizeof(stMreq)) < 0)
return false;
} else {
stLclAddr.sin_family = AF_INET;
stLclAddr.sin_addr.s_addr = htonl(INADDR_ANY);
stLclAddr.sin_port = htons(0);
if(bind(sock, (struct sockaddr*) & stLclAddr, sizeof(stLclAddr)) < 0)
return false;
}
connect(sock, (struct sockaddr*) & stAddr, sizeof(stAddr));
return true;
}
|