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