]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavformat/udp.c
Give better names to multicast functions (they are not IPv6-only)
[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 #endif
42     size_t dest_addr_len;
43 } UDPContext;
44
45 #define UDP_TX_BUF_SIZE 32768
46 #define UDP_MAX_PKT_SIZE 65536
47
48 static int udp_set_multicast_ttl(int sockfd, int mcastTTL, struct sockaddr *addr) {
49 #ifdef IP_MULTICAST_TTL
50     if (addr->sa_family == AF_INET) {
51         if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) {
52             perror("setsockopt(IP_MULTICAST_TTL)");
53             return -1;
54         }
55     }
56 #endif
57 #ifdef CONFIG_IPV6
58     if (addr->sa_family == AF_INET6) {
59         if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) {
60             perror("setsockopt(IPV6_MULTICAST_HOPS)");
61             return -1;
62         }
63     }
64 #endif
65     return 0;
66 }
67
68 static int udp_join_multicast_group(int sockfd, struct sockaddr *addr) {
69 #ifdef IP_ADD_MEMBERSHIP
70     if (addr->sa_family == AF_INET) {
71         struct ip_mreq mreq;
72
73         mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
74         mreq.imr_interface.s_addr= INADDR_ANY;
75         if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
76             perror("setsockopt(IP_ADD_MEMBERSHIP)");
77             return -1;
78         }
79     }
80 #endif
81 #ifdef CONFIG_IPV6
82     if (addr->sa_family == AF_INET6) {
83         struct ipv6_mreq mreq6;
84
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 #endif
93     return 0;
94 }
95
96 static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr) {
97 #ifdef IP_DROP_MEMBERSHIP
98     if (addr->sa_family == AF_INET) {
99         struct ip_mreq mreq;
100
101         mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
102         mreq.imr_interface.s_addr= INADDR_ANY;
103         if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
104             perror("setsockopt(IP_DROP_MEMBERSHIP)");
105             return -1;
106         }
107     }
108 #endif
109 #ifdef CONFIG_IPV6
110     if (addr->sa_family == AF_INET6) {
111         struct ipv6_mreq mreq6;
112
113         memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
114         mreq6.ipv6mr_interface= 0;
115         if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
116             perror("setsockopt(IPV6_DROP_MEMBERSHIP)");
117             return -1;
118         }
119     }
120 #endif
121     return 0;
122 }
123
124 #ifdef CONFIG_IPV6
125 static struct addrinfo* udp_ipv6_resolve_host(const char *hostname, int port, int type, int family, int flags) {
126     struct addrinfo hints, *res = 0;
127     int error;
128     char sport[16];
129     const char *node = 0, *service = "0";
130
131     if (port > 0) {
132         snprintf(sport, sizeof(sport), "%d", port);
133         service = sport;
134     }
135     if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) {
136         node = hostname;
137     }
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(EIO);
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     int family = AF_UNSPEC;
172
173     if (((struct sockaddr *) &s->dest_addr)->sa_family)
174         family = ((struct sockaddr *) &s->dest_addr)->sa_family;
175     res0 = udp_ipv6_resolve_host(0, s->local_port, SOCK_DGRAM, family, AI_PASSIVE);
176     if (res0 == 0)
177         goto fail;
178     for (res = res0; res; res=res->ai_next) {
179         udp_fd = socket(res->ai_family, SOCK_DGRAM, 0);
180         if (udp_fd > 0) break;
181         perror("socket");
182     }
183
184     if (udp_fd < 0)
185         goto fail;
186
187     if (bind(udp_fd, res0->ai_addr, res0->ai_addrlen) < 0) {
188         perror("bind");
189         goto fail;
190     }
191     freeaddrinfo(res0);
192         res0 = NULL;
193
194     addrlen = sizeof(clientaddr);
195     if (getsockname(udp_fd, (struct sockaddr *)&clientaddr, &addrlen) < 0) {
196         perror("getsockname");
197         goto fail;
198     }
199
200     if (getnameinfo((struct sockaddr *)&clientaddr, addrlen, hbuf, sizeof(hbuf),  sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
201         perror("getnameinfo");
202         goto fail;
203     }
204
205     s->local_port = strtol(sbuf, NULL, 10);
206
207     return udp_fd;
208
209  fail:
210     if (udp_fd >= 0)
211         closesocket(udp_fd);
212     if(res0)
213         freeaddrinfo(res0);
214     return -1;
215 }
216
217 #endif /* CONFIG_IPV6 */
218
219
220 /**
221  * If no filename is given to av_open_input_file because you want to
222  * get the local port first, then you must call this function to set
223  * the remote server address.
224  *
225  * url syntax: udp://host:port[?option=val...]
226  * option: 'multicast=1' : enable multicast
227  *         'ttl=n'       : set the ttl value (for multicast only)
228  *         'localport=n' : set the local port
229  *         'pkt_size=n'  : set max packet size
230  *         'reuse=1'     : enable reusing the socket
231  *
232  * @param s1 media file context
233  * @param uri of the remote server
234  * @return zero if no error.
235  */
236 int udp_set_remote_url(URLContext *h, const char *uri)
237 {
238 #ifdef CONFIG_IPV6
239     return udp_ipv6_set_remote_url(h, uri);
240 #else
241     UDPContext *s = h->priv_data;
242     char hostname[256];
243     int port;
244
245     url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
246
247     /* set the destination address */
248     if (resolve_host(&s->dest_addr.sin_addr, hostname) < 0)
249         return AVERROR(EIO);
250     s->dest_addr.sin_family = AF_INET;
251     s->dest_addr.sin_port = htons(port);
252     s->dest_addr_len = sizeof(s->dest_addr);
253     return 0;
254 #endif
255 }
256
257 /**
258  * Return the local port used by the UDP connexion
259  * @param s1 media file context
260  * @return the local port number
261  */
262 int udp_get_local_port(URLContext *h)
263 {
264     UDPContext *s = h->priv_data;
265     return s->local_port;
266 }
267
268 /**
269  * Return the udp file handle for select() usage to wait for several RTP
270  * streams at the same time.
271  * @param h media file context
272  */
273 int udp_get_file_handle(URLContext *h)
274 {
275     UDPContext *s = h->priv_data;
276     return s->udp_fd;
277 }
278
279 /* put it in UDP context */
280 /* return non zero if error */
281 static int udp_open(URLContext *h, const char *uri, int flags)
282 {
283     char hostname[1024];
284     int port, udp_fd = -1, tmp;
285     UDPContext *s = NULL;
286     int is_output;
287     const char *p;
288     char buf[256];
289 #ifndef CONFIG_IPV6
290     struct sockaddr_in my_addr, my_addr1;
291     int len;
292 #endif
293
294     h->is_streamed = 1;
295     h->max_packet_size = 1472;
296
297     is_output = (flags & URL_WRONLY);
298
299     s = av_mallocz(sizeof(UDPContext));
300     if (!s)
301         return AVERROR(ENOMEM);
302
303     h->priv_data = s;
304     s->ttl = 16;
305     s->is_multicast = 0;
306     s->local_port = 0;
307     s->reuse_socket = 0;
308     p = strchr(uri, '?');
309     if (p) {
310         s->is_multicast = find_info_tag(buf, sizeof(buf), "multicast", p);
311         s->reuse_socket = find_info_tag(buf, sizeof(buf), "reuse", p);
312         if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
313             s->ttl = strtol(buf, NULL, 10);
314         }
315         if (find_info_tag(buf, sizeof(buf), "localport", p)) {
316             s->local_port = strtol(buf, NULL, 10);
317         }
318         if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
319             h->max_packet_size = strtol(buf, NULL, 10);
320         }
321     }
322
323     /* fill the dest addr */
324     url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
325
326     /* XXX: fix url_split */
327     if (hostname[0] == '\0' || hostname[0] == '?') {
328         /* only accepts null hostname if input */
329         if (s->is_multicast || (flags & URL_WRONLY))
330             goto fail;
331     } else {
332         udp_set_remote_url(h, uri);
333     }
334
335     if(!ff_network_init())
336         return AVERROR(EIO);
337
338 #ifndef CONFIG_IPV6
339     udp_fd = socket(AF_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     if (s->reuse_socket)
353         if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
354             goto fail;
355
356     /* the bind is needed to give a port to the socket now */
357     if (bind(udp_fd,(struct sockaddr *)&my_addr, sizeof(my_addr)) < 0)
358         goto fail;
359
360     len = sizeof(my_addr1);
361     getsockname(udp_fd, (struct sockaddr *)&my_addr1, &len);
362     s->local_port = ntohs(my_addr1.sin_port);
363 #else
364     if (s->is_multicast && !(h->flags & URL_WRONLY))
365         s->local_port = port;
366     udp_fd = udp_ipv6_set_local(h);
367     if (udp_fd < 0)
368         goto fail;
369 #endif /* CONFIG_IPV6 */
370     if (s->is_multicast) {
371         if (h->flags & URL_WRONLY) {
372             /* output */
373             if (udp_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
374                 goto fail;
375         } else {
376             /* input */
377             if (udp_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0)
378                 goto fail;
379         }
380     }
381
382     if (is_output) {
383         /* limit the tx buf size to limit latency */
384         tmp = UDP_TX_BUF_SIZE;
385         if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
386             perror("setsockopt sndbuf");
387             goto fail;
388         }
389     } else {
390         /* set udp recv buffer size to the largest possible udp packet size to
391          * avoid losing data on OSes that set this too low by default. */
392         tmp = UDP_MAX_PKT_SIZE;
393         setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp));
394     }
395
396     s->udp_fd = udp_fd;
397     return 0;
398  fail:
399     if (udp_fd >= 0)
400         closesocket(udp_fd);
401     av_free(s);
402     return AVERROR(EIO);
403 }
404
405 static int udp_read(URLContext *h, uint8_t *buf, int size)
406 {
407     UDPContext *s = h->priv_data;
408 #ifndef CONFIG_IPV6
409     struct sockaddr_in from;
410 #else
411     struct sockaddr_storage from;
412 #endif
413     socklen_t from_len;
414     int len;
415
416     for(;;) {
417         from_len = sizeof(from);
418         len = recvfrom (s->udp_fd, buf, size, 0,
419                         (struct sockaddr *)&from, &from_len);
420         if (len < 0) {
421             if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
422                 ff_neterrno() != FF_NETERROR(EINTR))
423                 return AVERROR(EIO);
424         } else {
425             break;
426         }
427     }
428     return len;
429 }
430
431 static int udp_write(URLContext *h, uint8_t *buf, int size)
432 {
433     UDPContext *s = h->priv_data;
434     int ret;
435
436     for(;;) {
437         ret = sendto (s->udp_fd, buf, size, 0,
438                       (struct sockaddr *) &s->dest_addr,
439                       s->dest_addr_len);
440         if (ret < 0) {
441             if (ff_neterrno() != FF_NETERROR(EINTR) &&
442                 ff_neterrno() != FF_NETERROR(EAGAIN))
443                 return AVERROR(EIO);
444         } else {
445             break;
446         }
447     }
448     return size;
449 }
450
451 static int udp_close(URLContext *h)
452 {
453     UDPContext *s = h->priv_data;
454
455     if (s->is_multicast && !(h->flags & URL_WRONLY))
456         udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
457     closesocket(s->udp_fd);
458     ff_network_close();
459     av_free(s);
460     return 0;
461 }
462
463 URLProtocol udp_protocol = {
464     "udp",
465     udp_open,
466     udp_read,
467     udp_write,
468     NULL, /* seek */
469     udp_close,
470 };