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