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