]> git.sesse.net Git - ffmpeg/blob - libavformat/udp.c
b95f238d26a5356cd204cee2bf0562be19669f10
[ffmpeg] / libavformat / udp.c
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 typedef struct {
32     int udp_fd;
33     int ttl;
34     int is_multicast;
35     int local_port;
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 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 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 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 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 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             fprintf(stderr, "udp_ipv6_resolve_host: %s\n", gai_strerror(error));
137         }
138     }
139     return res;
140 }
141
142 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 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;
164     int family;
165                 
166     if (s->local_port != 0) {       
167         res0 = udp_ipv6_resolve_host(0, s->local_port, SOCK_DGRAM, AF_UNSPEC, AI_PASSIVE);
168         if (res0 == 0) return -1;
169         family = res0->ai_family;
170         freeaddrinfo(res0);
171     } else {
172         family = s->dest_addr.ss_family;
173     }
174     
175     udp_fd = socket(family, SOCK_DGRAM, 0);
176     if (udp_fd < 0) {
177         perror("socket");
178         goto fail;
179     }
180    
181     if (s->local_port != 0) {
182         if (bind(udp_fd, res0->ai_addr, res0->ai_addrlen) < 0) {
183             perror("bind");
184             goto fail;
185         }
186     } 
187
188     addrlen = sizeof(clientaddr);
189     if (getsockname(udp_fd, (struct sockaddr *)&clientaddr, &addrlen) < 0) {
190         perror("getsockname");
191         goto fail;
192     }
193
194     if (getnameinfo((struct sockaddr *)&clientaddr, addrlen, hbuf, sizeof(hbuf),  sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
195         perror("getnameinfo");
196         goto fail;
197     }
198
199     s->local_port = strtol(sbuf, NULL, 10);
200     
201     return udp_fd;
202     
203  fail:
204     if (udp_fd >= 0)
205 #ifdef CONFIG_BEOS_NETSERVER
206         closesocket(udp_fd);
207 #else
208         close(udp_fd);
209 #endif
210     return -1;
211 }
212
213 #endif
214
215
216 /**
217  * If no filename is given to av_open_input_file because you want to
218  * get the local port first, then you must call this function to set
219  * the remote server address.
220  *
221  * url syntax: udp://host:port[?option=val...]
222  * option: 'multicast=1' : enable multicast 
223  *         'ttl=n'       : set the ttl value (for multicast only)
224  *         'localport=n' : set the local port
225  *         'pkt_size=n'  : set max packet size
226  *
227  * @param s1 media file context
228  * @param uri of the remote server
229  * @return zero if no error.
230  */
231 int udp_set_remote_url(URLContext *h, const char *uri)
232 {
233 #ifdef CONFIG_IPV6
234     return udp_ipv6_set_remote_url(h, uri);
235 #else
236     UDPContext *s = h->priv_data;
237     char hostname[256];
238     int port;
239     
240     url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
241
242     /* set the destination address */
243     if (resolve_host(&s->dest_addr.sin_addr, hostname) < 0)
244         return AVERROR_IO;
245     s->dest_addr.sin_family = AF_INET;
246     s->dest_addr.sin_port = htons(port);
247     return 0;
248 #endif
249 }
250
251 /**
252  * Return the local port used by the UDP connexion
253  * @param s1 media file context
254  * @return the local port number
255  */
256 int udp_get_local_port(URLContext *h)
257 {
258     UDPContext *s = h->priv_data;
259     return s->local_port;
260 }
261
262 /**
263  * Return the udp file handle for select() usage to wait for several RTP
264  * streams at the same time.
265  * @param h media file context
266  */
267 int udp_get_file_handle(URLContext *h)
268 {
269     UDPContext *s = h->priv_data;
270     return s->udp_fd;
271 }
272
273 /* put it in UDP context */
274 /* return non zero if error */
275 static int udp_open(URLContext *h, const char *uri, int flags)
276 {
277     struct sockaddr_in my_addr, my_addr1;
278     char hostname[1024];
279     int port, udp_fd = -1, tmp;
280     UDPContext *s = NULL;
281     int is_output, len;
282     const char *p;
283     char buf[256];
284
285     h->is_streamed = 1;
286     h->max_packet_size = 1472;
287
288     is_output = (flags & URL_WRONLY);
289     
290     s = av_malloc(sizeof(UDPContext));
291     if (!s)
292         return -ENOMEM;
293
294     h->priv_data = s;
295     s->ttl = 16;
296     s->is_multicast = 0;
297     s->local_port = 0;
298     p = strchr(uri, '?');
299     if (p) {
300         s->is_multicast = find_info_tag(buf, sizeof(buf), "multicast", p);
301         if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
302             s->ttl = strtol(buf, NULL, 10);
303         }
304         if (find_info_tag(buf, sizeof(buf), "localport", p)) {
305             s->local_port = strtol(buf, NULL, 10);
306         }
307         if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
308             h->max_packet_size = strtol(buf, NULL, 10);
309         }
310     }
311
312     /* fill the dest addr */
313     url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
314     
315     /* XXX: fix url_split */
316     if (hostname[0] == '\0' || hostname[0] == '?') {
317         /* only accepts null hostname if input */
318         if (s->is_multicast || (flags & URL_WRONLY))
319             goto fail;
320     } else {
321         udp_set_remote_url(h, uri);
322     }
323
324 #ifndef CONFIG_IPV6
325     udp_fd = socket(PF_INET, SOCK_DGRAM, 0);
326     if (udp_fd < 0)
327         goto fail;
328
329     my_addr.sin_family = AF_INET;
330     my_addr.sin_addr.s_addr = htonl (INADDR_ANY);
331     if (s->is_multicast && !(h->flags & URL_WRONLY)) {
332         /* special case: the bind must be done on the multicast address port */
333         my_addr.sin_port = s->dest_addr.sin_port;
334     } else {
335         my_addr.sin_port = htons(s->local_port);
336     }
337
338     /* the bind is needed to give a port to the socket now */
339     if (bind(udp_fd,(struct sockaddr *)&my_addr, sizeof(my_addr)) < 0) 
340         goto fail;
341
342     len = sizeof(my_addr1);
343     getsockname(udp_fd, (struct sockaddr *)&my_addr1, &len);
344     s->local_port = ntohs(my_addr1.sin_port);
345
346 #ifndef CONFIG_BEOS_NETSERVER
347     if (s->is_multicast) {
348         if (h->flags & URL_WRONLY) {
349             /* output */
350             if (setsockopt(udp_fd, IPPROTO_IP, IP_MULTICAST_TTL, 
351                            &s->ttl, sizeof(s->ttl)) < 0) {
352                 perror("IP_MULTICAST_TTL");
353                 goto fail;
354             }
355         } else {
356             /* input */
357             memset(&s->mreq, 0, sizeof(s->mreq));
358             s->mreq.imr_multiaddr = s->dest_addr.sin_addr;
359             s->mreq.imr_interface.s_addr = htonl (INADDR_ANY);
360             if (setsockopt(udp_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, 
361                            &s->mreq, sizeof(s->mreq)) < 0) {
362                 perror("rtp: IP_ADD_MEMBERSHIP");
363                 goto fail;
364             }
365         }
366     }
367 #endif
368 #else
369     if (s->is_multicast && !(h->flags & URL_WRONLY))
370         s->local_port = port;
371     udp_fd = udp_ipv6_set_local(h);
372     if (udp_fd < 0)
373         goto fail;
374 #ifndef CONFIG_BEOS_NETSERVER
375     if (s->is_multicast) {
376         if (h->flags & URL_WRONLY) {
377             if (udp_ipv6_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
378                 goto fail;
379         } else {
380             if (udp_ipv6_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0)
381                 goto fail;
382         }
383     }
384 #endif          
385 #endif
386
387     if (is_output) {
388         /* limit the tx buf size to limit latency */
389         tmp = UDP_TX_BUF_SIZE;
390         if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
391             perror("setsockopt sndbuf");
392             goto fail;
393         }
394     }
395
396     s->udp_fd = udp_fd;
397     return 0;
398  fail:
399     if (udp_fd >= 0)
400 #ifdef CONFIG_BEOS_NETSERVER
401         closesocket(udp_fd);
402 #else
403         close(udp_fd);
404 #endif
405     av_free(s);
406     return AVERROR_IO;
407 }
408
409 static int udp_read(URLContext *h, uint8_t *buf, int size)
410 {
411     UDPContext *s = h->priv_data;
412 #ifndef CONFIG_IPV6
413     struct sockaddr_in from;
414 #else
415     struct sockaddr_storage from;
416 #endif
417     int from_len, len;
418
419     for(;;) {
420         from_len = sizeof(from);
421         len = recvfrom (s->udp_fd, buf, size, 0,
422                         (struct sockaddr *)&from, &from_len);
423         if (len < 0) {
424             if (errno != EAGAIN && errno != EINTR)
425                 return AVERROR_IO;
426         } else {
427             break;
428         }
429     }
430     return len;
431 }
432
433 static int udp_write(URLContext *h, uint8_t *buf, int size)
434 {
435     UDPContext *s = h->priv_data;
436     int ret;
437
438     for(;;) {
439         ret = sendto (s->udp_fd, buf, size, 0, 
440                       (struct sockaddr *) &s->dest_addr,
441 #ifndef CONFIG_IPV6
442                       sizeof (s->dest_addr));
443 #else
444                       s->dest_addr_len);
445 #endif
446         if (ret < 0) {
447             if (errno != EINTR && errno != EAGAIN)
448                 return AVERROR_IO;
449         } else {
450             break;
451         }
452     }
453     return size;
454 }
455
456 static int udp_close(URLContext *h)
457 {
458     UDPContext *s = h->priv_data;
459
460 #ifndef CONFIG_BEOS_NETSERVER
461 #ifndef CONFIG_IPV6
462     if (s->is_multicast && !(h->flags & URL_WRONLY)) {
463         if (setsockopt(s->udp_fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, 
464                        &s->mreq, sizeof(s->mreq)) < 0) {
465             perror("IP_DROP_MEMBERSHIP");
466         }
467     }
468 #else
469     if (s->is_multicast && !(h->flags & URL_WRONLY))
470         udp_ipv6_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
471 #endif
472     close(s->udp_fd);
473 #else
474     closesocket(s->udp_fd);
475 #endif
476     av_free(s);
477     return 0;
478 }
479
480 URLProtocol udp_protocol = {
481     "udp",
482     udp_open,
483     udp_read,
484     udp_write,
485     NULL, /* seek */
486     udp_close,
487 };