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