ffmpeg / libavformat / udp.c @ 5509bffa
History | View | Annotate | Download (14 KB)
1 |
/*
|
---|---|
2 |
* UDP prototype streaming system
|
3 |
* Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
|
4 |
*
|
5 |
* This library is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU Lesser General Public
|
7 |
* License as published by the Free Software Foundation; either
|
8 |
* version 2 of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This library is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
13 |
* Lesser General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU Lesser General Public
|
16 |
* License along with this library; if not, write to the Free Software
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
18 |
*/
|
19 |
#include "avformat.h" |
20 |
#include <unistd.h> |
21 |
#include <sys/types.h> |
22 |
#include <sys/socket.h> |
23 |
#include <netinet/in.h> |
24 |
#ifndef __BEOS__
|
25 |
# include <arpa/inet.h> |
26 |
#else
|
27 |
# include "barpainet.h" |
28 |
#endif
|
29 |
#include <netdb.h> |
30 |
|
31 |
#ifndef IPV6_ADD_MEMBERSHIP
|
32 |
#define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
|
33 |
#define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
|
34 |
#endif
|
35 |
|
36 |
typedef struct { |
37 |
int udp_fd;
|
38 |
int ttl;
|
39 |
int is_multicast;
|
40 |
int local_port;
|
41 |
#ifndef CONFIG_IPV6
|
42 |
struct ip_mreq mreq;
|
43 |
struct sockaddr_in dest_addr;
|
44 |
#else
|
45 |
struct sockaddr_storage dest_addr;
|
46 |
size_t dest_addr_len; |
47 |
#endif
|
48 |
} UDPContext; |
49 |
|
50 |
#define UDP_TX_BUF_SIZE 32768 |
51 |
|
52 |
#ifdef CONFIG_IPV6
|
53 |
|
54 |
int udp_ipv6_is_multicast_address(const struct sockaddr *addr) { |
55 |
if (addr->sa_family == AF_INET)
|
56 |
return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr)); |
57 |
if (addr->sa_family == AF_INET6)
|
58 |
return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr); |
59 |
return -1; |
60 |
} |
61 |
|
62 |
int udp_ipv6_set_multicast_ttl(int sockfd, int mcastTTL, struct sockaddr *addr) { |
63 |
if (addr->sa_family == AF_INET) {
|
64 |
if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) { |
65 |
perror("setsockopt(IP_MULTICAST_TTL)");
|
66 |
return -1; |
67 |
} |
68 |
} |
69 |
if (addr->sa_family == AF_INET6) {
|
70 |
if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) { |
71 |
perror("setsockopt(IPV6_MULTICAST_HOPS)");
|
72 |
return -1; |
73 |
} |
74 |
} |
75 |
return 0; |
76 |
} |
77 |
|
78 |
int udp_ipv6_join_multicast_group(int sockfd, struct sockaddr *addr) { |
79 |
struct ip_mreq mreq;
|
80 |
struct ipv6_mreq mreq6;
|
81 |
if (addr->sa_family == AF_INET) {
|
82 |
mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
|
83 |
mreq.imr_interface.s_addr= INADDR_ANY; |
84 |
if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) { |
85 |
perror("setsockopt(IP_ADD_MEMBERSHIP)");
|
86 |
return -1; |
87 |
} |
88 |
} |
89 |
if (addr->sa_family == AF_INET6) {
|
90 |
memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr)); |
91 |
mreq6.ipv6mr_interface= 0;
|
92 |
if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) { |
93 |
perror("setsockopt(IPV6_ADD_MEMBERSHIP)");
|
94 |
return -1; |
95 |
} |
96 |
} |
97 |
return 0; |
98 |
} |
99 |
|
100 |
int udp_ipv6_leave_multicast_group(int sockfd, struct sockaddr *addr) { |
101 |
struct ip_mreq mreq;
|
102 |
struct ipv6_mreq mreq6;
|
103 |
if (addr->sa_family == AF_INET) {
|
104 |
mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
|
105 |
mreq.imr_interface.s_addr= INADDR_ANY; |
106 |
if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) { |
107 |
perror("setsockopt(IP_DROP_MEMBERSHIP)");
|
108 |
return -1; |
109 |
} |
110 |
} |
111 |
if (addr->sa_family == AF_INET6) {
|
112 |
memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr)); |
113 |
mreq6.ipv6mr_interface= 0;
|
114 |
if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) { |
115 |
perror("setsockopt(IPV6_DROP_MEMBERSHIP)");
|
116 |
return -1; |
117 |
} |
118 |
} |
119 |
return 0; |
120 |
} |
121 |
|
122 |
struct addrinfo* udp_ipv6_resolve_host(const char *hostname, int port, int type, int family, int flags) { |
123 |
struct addrinfo hints, *res = 0; |
124 |
int error;
|
125 |
char sport[16]; |
126 |
const char *node = 0, *service = 0; |
127 |
|
128 |
if (port > 0) { |
129 |
snprintf(sport, sizeof(sport), "%d", port); |
130 |
service = sport; |
131 |
} |
132 |
if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) { |
133 |
node = hostname; |
134 |
} |
135 |
if ((node) || (service)) {
|
136 |
memset(&hints, 0, sizeof(hints)); |
137 |
hints.ai_socktype = type; |
138 |
hints.ai_family = family; |
139 |
hints.ai_flags = flags; |
140 |
if ((error = getaddrinfo(node, service, &hints, &res))) {
|
141 |
av_log(NULL, AV_LOG_ERROR, "udp_ipv6_resolve_host: %s\n", gai_strerror(error)); |
142 |
} |
143 |
} |
144 |
return res;
|
145 |
} |
146 |
|
147 |
int udp_ipv6_set_remote_url(URLContext *h, const char *uri) { |
148 |
UDPContext *s = h->priv_data; |
149 |
char hostname[256]; |
150 |
int port;
|
151 |
struct addrinfo *res0;
|
152 |
url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri); |
153 |
res0 = udp_ipv6_resolve_host(hostname, port, SOCK_DGRAM, AF_UNSPEC, 0);
|
154 |
if (res0 == 0) return AVERROR_IO; |
155 |
memcpy(&s->dest_addr, res0->ai_addr, res0->ai_addrlen); |
156 |
s->dest_addr_len = res0->ai_addrlen; |
157 |
freeaddrinfo(res0); |
158 |
return 0; |
159 |
} |
160 |
|
161 |
int udp_ipv6_set_local(URLContext *h) {
|
162 |
UDPContext *s = h->priv_data; |
163 |
int udp_fd = -1; |
164 |
struct sockaddr_storage clientaddr;
|
165 |
socklen_t addrlen; |
166 |
char sbuf[NI_MAXSERV];
|
167 |
char hbuf[NI_MAXHOST];
|
168 |
struct addrinfo *res0 = NULL, *res = NULL; |
169 |
|
170 |
if (s->local_port != 0) { |
171 |
res0 = udp_ipv6_resolve_host(0, s->local_port, SOCK_DGRAM, AF_UNSPEC, AI_PASSIVE);
|
172 |
if (res0 == 0) |
173 |
goto fail;
|
174 |
for (res = res0; res; res=res->ai_next) {
|
175 |
udp_fd = socket(res->ai_family, SOCK_DGRAM, 0);
|
176 |
if (udp_fd > 0) break; |
177 |
perror("socket");
|
178 |
} |
179 |
} else {
|
180 |
udp_fd = socket(s->dest_addr.ss_family, SOCK_DGRAM, 0);
|
181 |
if (udp_fd < 0) |
182 |
perror("socket");
|
183 |
} |
184 |
|
185 |
if (udp_fd < 0) |
186 |
goto fail;
|
187 |
|
188 |
if (s->local_port != 0) { |
189 |
if (bind(udp_fd, res0->ai_addr, res0->ai_addrlen) < 0) { |
190 |
perror("bind");
|
191 |
goto fail;
|
192 |
} |
193 |
freeaddrinfo(res0); |
194 |
res0 = NULL;
|
195 |
} |
196 |
|
197 |
addrlen = sizeof(clientaddr);
|
198 |
if (getsockname(udp_fd, (struct sockaddr *)&clientaddr, &addrlen) < 0) { |
199 |
perror("getsockname");
|
200 |
goto fail;
|
201 |
} |
202 |
|
203 |
if (getnameinfo((struct sockaddr *)&clientaddr, addrlen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) != 0) { |
204 |
perror("getnameinfo");
|
205 |
goto fail;
|
206 |
} |
207 |
|
208 |
s->local_port = strtol(sbuf, NULL, 10); |
209 |
|
210 |
return udp_fd;
|
211 |
|
212 |
fail:
|
213 |
if (udp_fd >= 0) |
214 |
#ifdef CONFIG_BEOS_NETSERVER
|
215 |
closesocket(udp_fd); |
216 |
#else
|
217 |
close(udp_fd); |
218 |
#endif
|
219 |
if(res0)
|
220 |
freeaddrinfo(res0); |
221 |
return -1; |
222 |
} |
223 |
|
224 |
#endif
|
225 |
|
226 |
|
227 |
/**
|
228 |
* If no filename is given to av_open_input_file because you want to
|
229 |
* get the local port first, then you must call this function to set
|
230 |
* the remote server address.
|
231 |
*
|
232 |
* url syntax: udp://host:port[?option=val...]
|
233 |
* option: 'multicast=1' : enable multicast
|
234 |
* 'ttl=n' : set the ttl value (for multicast only)
|
235 |
* 'localport=n' : set the local port
|
236 |
* 'pkt_size=n' : set max packet size
|
237 |
*
|
238 |
* @param s1 media file context
|
239 |
* @param uri of the remote server
|
240 |
* @return zero if no error.
|
241 |
*/
|
242 |
int udp_set_remote_url(URLContext *h, const char *uri) |
243 |
{ |
244 |
#ifdef CONFIG_IPV6
|
245 |
return udp_ipv6_set_remote_url(h, uri);
|
246 |
#else
|
247 |
UDPContext *s = h->priv_data; |
248 |
char hostname[256]; |
249 |
int port;
|
250 |
|
251 |
url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri); |
252 |
|
253 |
/* set the destination address */
|
254 |
if (resolve_host(&s->dest_addr.sin_addr, hostname) < 0) |
255 |
return AVERROR_IO;
|
256 |
s->dest_addr.sin_family = AF_INET; |
257 |
s->dest_addr.sin_port = htons(port); |
258 |
return 0; |
259 |
#endif
|
260 |
} |
261 |
|
262 |
/**
|
263 |
* Return the local port used by the UDP connexion
|
264 |
* @param s1 media file context
|
265 |
* @return the local port number
|
266 |
*/
|
267 |
int udp_get_local_port(URLContext *h)
|
268 |
{ |
269 |
UDPContext *s = h->priv_data; |
270 |
return s->local_port;
|
271 |
} |
272 |
|
273 |
/**
|
274 |
* Return the udp file handle for select() usage to wait for several RTP
|
275 |
* streams at the same time.
|
276 |
* @param h media file context
|
277 |
*/
|
278 |
int udp_get_file_handle(URLContext *h)
|
279 |
{ |
280 |
UDPContext *s = h->priv_data; |
281 |
return s->udp_fd;
|
282 |
} |
283 |
|
284 |
/* put it in UDP context */
|
285 |
/* return non zero if error */
|
286 |
static int udp_open(URLContext *h, const char *uri, int flags) |
287 |
{ |
288 |
char hostname[1024]; |
289 |
int port, udp_fd = -1, tmp; |
290 |
UDPContext *s = NULL;
|
291 |
int is_output;
|
292 |
const char *p; |
293 |
char buf[256]; |
294 |
#ifndef CONFIG_IPV6
|
295 |
struct sockaddr_in my_addr, my_addr1;
|
296 |
int len;
|
297 |
#endif
|
298 |
|
299 |
h->is_streamed = 1;
|
300 |
h->max_packet_size = 1472;
|
301 |
|
302 |
is_output = (flags & URL_WRONLY); |
303 |
|
304 |
s = av_malloc(sizeof(UDPContext));
|
305 |
if (!s)
|
306 |
return -ENOMEM;
|
307 |
|
308 |
h->priv_data = s; |
309 |
s->ttl = 16;
|
310 |
s->is_multicast = 0;
|
311 |
s->local_port = 0;
|
312 |
p = strchr(uri, '?');
|
313 |
if (p) {
|
314 |
s->is_multicast = find_info_tag(buf, sizeof(buf), "multicast", p); |
315 |
if (find_info_tag(buf, sizeof(buf), "ttl", p)) { |
316 |
s->ttl = strtol(buf, NULL, 10); |
317 |
} |
318 |
if (find_info_tag(buf, sizeof(buf), "localport", p)) { |
319 |
s->local_port = strtol(buf, NULL, 10); |
320 |
} |
321 |
if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) { |
322 |
h->max_packet_size = strtol(buf, NULL, 10); |
323 |
} |
324 |
} |
325 |
|
326 |
/* fill the dest addr */
|
327 |
url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri); |
328 |
|
329 |
/* XXX: fix url_split */
|
330 |
if (hostname[0] == '\0' || hostname[0] == '?') { |
331 |
/* only accepts null hostname if input */
|
332 |
if (s->is_multicast || (flags & URL_WRONLY))
|
333 |
goto fail;
|
334 |
} else {
|
335 |
udp_set_remote_url(h, uri); |
336 |
} |
337 |
|
338 |
#ifndef CONFIG_IPV6
|
339 |
udp_fd = socket(PF_INET, SOCK_DGRAM, 0);
|
340 |
if (udp_fd < 0) |
341 |
goto fail;
|
342 |
|
343 |
my_addr.sin_family = AF_INET; |
344 |
my_addr.sin_addr.s_addr = htonl (INADDR_ANY); |
345 |
if (s->is_multicast && !(h->flags & URL_WRONLY)) {
|
346 |
/* special case: the bind must be done on the multicast address port */
|
347 |
my_addr.sin_port = s->dest_addr.sin_port; |
348 |
} else {
|
349 |
my_addr.sin_port = htons(s->local_port); |
350 |
} |
351 |
|
352 |
/* the bind is needed to give a port to the socket now */
|
353 |
if (bind(udp_fd,(struct sockaddr *)&my_addr, sizeof(my_addr)) < 0) |
354 |
goto fail;
|
355 |
|
356 |
len = sizeof(my_addr1);
|
357 |
getsockname(udp_fd, (struct sockaddr *)&my_addr1, &len);
|
358 |
s->local_port = ntohs(my_addr1.sin_port); |
359 |
|
360 |
#ifndef CONFIG_BEOS_NETSERVER
|
361 |
if (s->is_multicast) {
|
362 |
if (h->flags & URL_WRONLY) {
|
363 |
/* output */
|
364 |
if (setsockopt(udp_fd, IPPROTO_IP, IP_MULTICAST_TTL,
|
365 |
&s->ttl, sizeof(s->ttl)) < 0) { |
366 |
perror("IP_MULTICAST_TTL");
|
367 |
goto fail;
|
368 |
} |
369 |
} else {
|
370 |
/* input */
|
371 |
memset(&s->mreq, 0, sizeof(s->mreq)); |
372 |
s->mreq.imr_multiaddr = s->dest_addr.sin_addr; |
373 |
s->mreq.imr_interface.s_addr = htonl (INADDR_ANY); |
374 |
if (setsockopt(udp_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
|
375 |
&s->mreq, sizeof(s->mreq)) < 0) { |
376 |
perror("rtp: IP_ADD_MEMBERSHIP");
|
377 |
goto fail;
|
378 |
} |
379 |
} |
380 |
} |
381 |
#endif
|
382 |
#else
|
383 |
if (s->is_multicast && !(h->flags & URL_WRONLY))
|
384 |
s->local_port = port; |
385 |
udp_fd = udp_ipv6_set_local(h); |
386 |
if (udp_fd < 0) |
387 |
goto fail;
|
388 |
#ifndef CONFIG_BEOS_NETSERVER
|
389 |
if (s->is_multicast) {
|
390 |
if (h->flags & URL_WRONLY) {
|
391 |
if (udp_ipv6_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0) |
392 |
goto fail;
|
393 |
} else {
|
394 |
if (udp_ipv6_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0) |
395 |
goto fail;
|
396 |
} |
397 |
} |
398 |
#endif
|
399 |
#endif
|
400 |
|
401 |
if (is_output) {
|
402 |
/* limit the tx buf size to limit latency */
|
403 |
tmp = UDP_TX_BUF_SIZE; |
404 |
if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) { |
405 |
perror("setsockopt sndbuf");
|
406 |
goto fail;
|
407 |
} |
408 |
} |
409 |
|
410 |
s->udp_fd = udp_fd; |
411 |
return 0; |
412 |
fail:
|
413 |
if (udp_fd >= 0) |
414 |
#ifdef CONFIG_BEOS_NETSERVER
|
415 |
closesocket(udp_fd); |
416 |
#else
|
417 |
close(udp_fd); |
418 |
#endif
|
419 |
av_free(s); |
420 |
return AVERROR_IO;
|
421 |
} |
422 |
|
423 |
static int udp_read(URLContext *h, uint8_t *buf, int size) |
424 |
{ |
425 |
UDPContext *s = h->priv_data; |
426 |
#ifndef CONFIG_IPV6
|
427 |
struct sockaddr_in from;
|
428 |
#else
|
429 |
struct sockaddr_storage from;
|
430 |
#endif
|
431 |
int from_len, len;
|
432 |
|
433 |
for(;;) {
|
434 |
from_len = sizeof(from);
|
435 |
len = recvfrom (s->udp_fd, buf, size, 0,
|
436 |
(struct sockaddr *)&from, &from_len);
|
437 |
if (len < 0) { |
438 |
if (errno != EAGAIN && errno != EINTR)
|
439 |
return AVERROR_IO;
|
440 |
} else {
|
441 |
break;
|
442 |
} |
443 |
} |
444 |
return len;
|
445 |
} |
446 |
|
447 |
static int udp_write(URLContext *h, uint8_t *buf, int size) |
448 |
{ |
449 |
UDPContext *s = h->priv_data; |
450 |
int ret;
|
451 |
|
452 |
for(;;) {
|
453 |
ret = sendto (s->udp_fd, buf, size, 0,
|
454 |
(struct sockaddr *) &s->dest_addr,
|
455 |
#ifndef CONFIG_IPV6
|
456 |
sizeof (s->dest_addr));
|
457 |
#else
|
458 |
s->dest_addr_len); |
459 |
#endif
|
460 |
if (ret < 0) { |
461 |
if (errno != EINTR && errno != EAGAIN)
|
462 |
return AVERROR_IO;
|
463 |
} else {
|
464 |
break;
|
465 |
} |
466 |
} |
467 |
return size;
|
468 |
} |
469 |
|
470 |
static int udp_close(URLContext *h) |
471 |
{ |
472 |
UDPContext *s = h->priv_data; |
473 |
|
474 |
#ifndef CONFIG_BEOS_NETSERVER
|
475 |
#ifndef CONFIG_IPV6
|
476 |
if (s->is_multicast && !(h->flags & URL_WRONLY)) {
|
477 |
if (setsockopt(s->udp_fd, IPPROTO_IP, IP_DROP_MEMBERSHIP,
|
478 |
&s->mreq, sizeof(s->mreq)) < 0) { |
479 |
perror("IP_DROP_MEMBERSHIP");
|
480 |
} |
481 |
} |
482 |
#else
|
483 |
if (s->is_multicast && !(h->flags & URL_WRONLY))
|
484 |
udp_ipv6_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
|
485 |
#endif
|
486 |
close(s->udp_fd); |
487 |
#else
|
488 |
closesocket(s->udp_fd); |
489 |
#endif
|
490 |
av_free(s); |
491 |
return 0; |
492 |
} |
493 |
|
494 |
URLProtocol udp_protocol = { |
495 |
"udp",
|
496 |
udp_open, |
497 |
udp_read, |
498 |
udp_write, |
499 |
NULL, /* seek */ |
500 |
udp_close, |
501 |
}; |