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
|
/* GStreamer UDP network utility functions
* Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
* Copyright (C) 2006 Joni Valtanen <joni.valtanen@movial.fi>
* Copyright (C) 2009 Jarkko Palviainen <jarkko.palviainen@sesca.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <gst/gst.h>
#include "gstudpnetutils.h"
/* EAI_ADDRFAMILY was obsoleted in BSD at some point */
#ifndef EAI_ADDRFAMILY
#define EAI_ADDRFAMILY 1
#endif
#ifdef G_OS_WIN32
gboolean
gst_udp_net_utils_win32_wsa_startup (GstObject * obj)
{
WSADATA w;
int error;
error = WSAStartup (0x0202, &w);
if (error) {
GST_WARNING_OBJECT (obj, "WSAStartup error: %d", error);
return FALSE;
}
if (w.wVersion != 0x0202) {
WSACleanup ();
GST_WARNING_OBJECT (obj, "Winsock version wrong : 0x%x", w.wVersion);
return FALSE;
}
return TRUE;
}
#endif
int
gst_udp_get_sockaddr_length (struct sockaddr_storage *addr)
{
/* MacOS is picky about passing precisely the correct length,
* so we calculate it here for the given socket type.
*/
switch (addr->ss_family) {
case AF_INET:
return sizeof (struct sockaddr_in);
case AF_INET6:
return sizeof (struct sockaddr_in6);
default:
/* don't know, Screw MacOS and use the full length */
return sizeof (*addr);
}
}
int
gst_udp_get_addr (const char *hostname, int port, struct sockaddr_storage *addr)
{
struct addrinfo hints, *res = NULL, *nres;
char service[NI_MAXSERV];
int ret;
memset (&hints, 0, sizeof (hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
g_snprintf (service, sizeof (service) - 1, "%d", port);
service[sizeof (service) - 1] = '\0';
if ((ret = getaddrinfo (hostname, (port == -1) ? NULL : service, &hints,
&res)) < 0) {
goto beach;
}
nres = res;
while (nres) {
if (nres->ai_family == AF_INET || nres->ai_family == AF_INET6)
break;
nres = nres->ai_next;
}
if (nres) {
memcpy (addr, nres->ai_addr, nres->ai_addrlen);
} else {
ret = EAI_ADDRFAMILY;
}
freeaddrinfo (res);
beach:
return ret;
}
int
gst_udp_set_loop (int sockfd, guint16 ss_family, gboolean loop)
{
int ret = -1;
int l = (loop == FALSE) ? 0 : 1;
switch (ss_family) {
case AF_INET:
{
ret = setsockopt (sockfd, IPPROTO_IP, IP_MULTICAST_LOOP, &l, sizeof (l));
if (ret < 0)
return ret;
break;
}
case AF_INET6:
{
ret =
setsockopt (sockfd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &l,
sizeof (l));
if (ret < 0)
return ret;
break;
}
default:
#ifdef G_OS_WIN32
WSASetLastError (WSAEAFNOSUPPORT);
#else
errno = EAFNOSUPPORT;
#endif
}
return ret;
}
int
gst_udp_set_ttl (int sockfd, guint16 ss_family, int ttl, gboolean is_multicast)
{
int optname = -1;
int ret = -1;
switch (ss_family) {
case AF_INET:
{
optname = (is_multicast == TRUE) ? IP_MULTICAST_TTL : IP_TTL;
ret = setsockopt (sockfd, IPPROTO_IP, optname, &ttl, sizeof (ttl));
if (ret < 0)
return ret;
break;
}
case AF_INET6:
{
optname =
(is_multicast == TRUE) ? IPV6_MULTICAST_HOPS : IPV6_UNICAST_HOPS;
ret = setsockopt (sockfd, IPPROTO_IPV6, optname, &ttl, sizeof (ttl));
if (ret < 0)
return ret;
/* When using IPV4 address with IPV6 socket, both TTL values
must be set in order to actually use the given value.
Has no effect when IPV6 address is used. */
optname = (is_multicast == TRUE) ? IP_MULTICAST_TTL : IP_TTL;
ret = setsockopt (sockfd, IPPROTO_IP, optname, &ttl, sizeof (ttl));
if (ret < 0)
return ret;
break;
}
default:
#ifdef G_OS_WIN32
WSASetLastError (WSAEAFNOSUPPORT);
#else
errno = EAFNOSUPPORT;
#endif
}
return ret;
}
/* FIXME: Add interface selection for windows hosts. */
int
gst_udp_join_group (int sockfd, struct sockaddr_storage *addr, gchar * iface)
{
int ret = -1;
switch (addr->ss_family) {
case AF_INET:
{
#ifdef HAVE_IP_MREQN
struct ip_mreqn mreq4;
#else
struct ip_mreq mreq4;
#endif
memset (&mreq4, 0, sizeof (mreq4));
mreq4.imr_multiaddr.s_addr =
((struct sockaddr_in *) addr)->sin_addr.s_addr;
#ifdef HAVE_IP_MREQN
if (iface)
mreq4.imr_ifindex = if_nametoindex (iface);
else
mreq4.imr_ifindex = 0; /* Pick any. */
#else
mreq4.imr_interface.s_addr = INADDR_ANY;
#endif
if ((ret =
setsockopt (sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
(const void *) &mreq4, sizeof (mreq4))) < 0)
return ret;
break;
}
case AF_INET6:
{
struct ipv6_mreq mreq6;
memset (&mreq6, 0, sizeof (mreq6));
memcpy (&mreq6.ipv6mr_multiaddr,
&(((struct sockaddr_in6 *) addr)->sin6_addr),
sizeof (struct in6_addr));
mreq6.ipv6mr_interface = 0;
#if !defined(G_OS_WIN32)
if (iface)
mreq6.ipv6mr_interface = if_nametoindex (iface);
#endif
if ((ret =
setsockopt (sockfd, IPPROTO_IPV6, IPV6_JOIN_GROUP,
(const void *) &mreq6, sizeof (mreq6))) < 0)
return ret;
break;
}
default:
#ifdef G_OS_WIN32
WSASetLastError (WSAEAFNOSUPPORT);
#else
errno = EAFNOSUPPORT;
#endif
}
return ret;
}
int
gst_udp_leave_group (int sockfd, struct sockaddr_storage *addr)
{
int ret = -1;
switch (addr->ss_family) {
case AF_INET:
{
struct ip_mreq mreq4;
memset (&mreq4, 0, sizeof (mreq4));
mreq4.imr_multiaddr.s_addr =
((struct sockaddr_in *) addr)->sin_addr.s_addr;
mreq4.imr_interface.s_addr = INADDR_ANY;
if ((ret =
setsockopt (sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP,
(const void *) &mreq4, sizeof (mreq4))) < 0)
return ret;
}
break;
case AF_INET6:
{
struct ipv6_mreq mreq6;
memset (&mreq6, 0, sizeof (mreq6));
memcpy (&mreq6.ipv6mr_multiaddr,
&(((struct sockaddr_in6 *) addr)->sin6_addr),
sizeof (struct in6_addr));
mreq6.ipv6mr_interface = 0;
if ((ret =
setsockopt (sockfd, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
(const void *) &mreq6, sizeof (mreq6))) < 0)
return ret;
}
break;
default:
#ifdef G_OS_WIN32
WSASetLastError (WSAEAFNOSUPPORT);
#else
errno = EAFNOSUPPORT;
#endif
}
return ret;
}
int
gst_udp_is_multicast (struct sockaddr_storage *addr)
{
int ret = -1;
switch (addr->ss_family) {
case AF_INET:
{
struct sockaddr_in *addr4 = (struct sockaddr_in *) addr;
ret = IN_MULTICAST (g_ntohl (addr4->sin_addr.s_addr));
}
break;
case AF_INET6:
{
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) addr;
ret = IN6_IS_ADDR_MULTICAST (&addr6->sin6_addr);
}
break;
default:
#ifdef G_OS_WIN32
WSASetLastError (WSAEAFNOSUPPORT);
#else
errno = EAFNOSUPPORT;
#endif
}
return ret;
}
void
gst_udp_uri_init (GstUDPUri * uri, const gchar * host, gint port)
{
uri->host = NULL;
uri->port = -1;
gst_udp_uri_update (uri, host, port);
}
int
gst_udp_uri_update (GstUDPUri * uri, const gchar * host, gint port)
{
if (host) {
g_free (uri->host);
uri->host = g_strdup (host);
if (strchr (host, ':'))
uri->is_ipv6 = TRUE;
else
uri->is_ipv6 = FALSE;
}
if (port != -1)
uri->port = port;
return 0;
}
int
gst_udp_parse_uri (const gchar * uristr, GstUDPUri * uri)
{
gchar *protocol, *location_start;
gchar *location, *location_end;
gchar *colptr;
protocol = gst_uri_get_protocol (uristr);
if (strcmp (protocol, "udp") != 0)
goto wrong_protocol;
g_free (protocol);
location_start = gst_uri_get_location (uristr);
if (!location_start)
return FALSE;
GST_DEBUG ("got location '%s'", location_start);
/* VLC compatibility, strip everything before the @ sign. VLC uses that as the
* remote address. */
location = g_strstr_len (location_start, -1, "@");
if (location == NULL)
location = location_start;
else
location += 1;
if (location[0] == '[') {
GST_DEBUG ("parse IPV6 address '%s'", location);
location_end = strchr (location, ']');
if (location_end == NULL)
goto wrong_address;
uri->is_ipv6 = TRUE;
g_free (uri->host);
uri->host = g_strndup (location + 1, location_end - location - 1);
colptr = strrchr (location_end, ':');
} else {
GST_DEBUG ("parse IPV4 address '%s'", location);
uri->is_ipv6 = FALSE;
colptr = strrchr (location, ':');
g_free (uri->host);
if (colptr != NULL) {
uri->host = g_strndup (location, colptr - location);
} else {
uri->host = g_strdup (location);
}
}
GST_DEBUG ("host set to '%s'", uri->host);
if (colptr != NULL) {
uri->port = atoi (colptr + 1);
}
g_free (location_start);
return 0;
/* ERRORS */
wrong_protocol:
{
GST_ERROR ("error parsing uri %s: wrong protocol (%s != udp)", uristr,
protocol);
g_free (protocol);
return -1;
}
wrong_address:
{
GST_ERROR ("error parsing uri %s", uristr);
g_free (location);
return -1;
}
}
gchar *
gst_udp_uri_string (GstUDPUri * uri)
{
gchar *result;
if (uri->is_ipv6) {
result = g_strdup_printf ("udp://[%s]:%d", uri->host, uri->port);
} else {
result = g_strdup_printf ("udp://%s:%d", uri->host, uri->port);
}
return result;
}
void
gst_udp_uri_free (GstUDPUri * uri)
{
g_free (uri->host);
uri->host = NULL;
uri->port = -1;
}
|