6d44fdf3bfd15a80c21beb8dd796f74b4345c018
[libav.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 sockaddr_in dest_addr;
38 #else
39 struct sockaddr_storage dest_addr;
40 #endif
41 size_t dest_addr_len;
42 } UDPContext;
43
44 #define UDP_TX_BUF_SIZE 32768
45 #define UDP_MAX_PKT_SIZE 65536
46
47 static int udp_set_multicast_ttl(int sockfd, int mcastTTL, struct sockaddr *addr) {
48 #ifdef IP_MULTICAST_TTL
49 if (addr->sa_family == AF_INET) {
50 if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) {
51 perror("setsockopt(IP_MULTICAST_TTL)");
52 return -1;
53 }
54 }
55 #endif
56 #ifdef CONFIG_IPV6
57 if (addr->sa_family == AF_INET6) {
58 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) {
59 perror("setsockopt(IPV6_MULTICAST_HOPS)");
60 return -1;
61 }
62 }
63 #endif
64 return 0;
65 }
66
67 static int udp_join_multicast_group(int sockfd, struct sockaddr *addr) {
68 #ifdef IP_ADD_MEMBERSHIP
69 if (addr->sa_family == AF_INET) {
70 struct ip_mreq mreq;
71
72 mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
73 mreq.imr_interface.s_addr= INADDR_ANY;
74 if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
75 perror("setsockopt(IP_ADD_MEMBERSHIP)");
76 return -1;
77 }
78 }
79 #endif
80 #ifdef CONFIG_IPV6
81 if (addr->sa_family == AF_INET6) {
82 struct ipv6_mreq mreq6;
83
84 memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
85 mreq6.ipv6mr_interface= 0;
86 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
87 perror("setsockopt(IPV6_ADD_MEMBERSHIP)");
88 return -1;
89 }
90 }
91 #endif
92 return 0;
93 }
94
95 static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr) {
96 #ifdef IP_DROP_MEMBERSHIP
97 if (addr->sa_family == AF_INET) {
98 struct ip_mreq mreq;
99
100 mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
101 mreq.imr_interface.s_addr= INADDR_ANY;
102 if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
103 perror("setsockopt(IP_DROP_MEMBERSHIP)");
104 return -1;
105 }
106 }
107 #endif
108 #ifdef CONFIG_IPV6
109 if (addr->sa_family == AF_INET6) {
110 struct ipv6_mreq mreq6;
111
112 memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
113 mreq6.ipv6mr_interface= 0;
114 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
115 perror("setsockopt(IPV6_DROP_MEMBERSHIP)");
116 return -1;
117 }
118 }
119 #endif
120 return 0;
121 }
122
123 #ifdef CONFIG_IPV6
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 memset(&hints, 0, sizeof(hints));
138 hints.ai_socktype = type;
139 hints.ai_family = family;
140 hints.ai_flags = flags;
141 if ((error = getaddrinfo(node, service, &hints, &res))) {
142 av_log(NULL, AV_LOG_ERROR, "udp_ipv6_resolve_host: %s\n", gai_strerror(error));
143 }
144
145 return res;
146 }
147
148 static int udp_set_url(struct sockaddr_storage *addr, const char *hostname, int port) {
149 struct addrinfo *res0;
150 int addr_len;
151
152 res0 = udp_ipv6_resolve_host(hostname, port, SOCK_DGRAM, AF_UNSPEC, 0);
153 if (res0 == 0) return AVERROR(EIO);
154 memcpy(addr, res0->ai_addr, res0->ai_addrlen);
155 addr_len = res0->ai_addrlen;
156 freeaddrinfo(res0);
157
158 return addr_len;
159 }
160
161 static int udp_ipv6_set_local(URLContext *h) {
162 UDPContext *s = h->priv_data;
163 int udp_fd = -1;
164 struct sockaddr_storage clientaddr;
165 socklen_t addrlen;
166 char sbuf[NI_MAXSERV];
167 char hbuf[NI_MAXHOST];
168 struct addrinfo *res0 = NULL, *res = NULL;
169 int family = AF_UNSPEC;
170
171 if (((struct sockaddr *) &s->dest_addr)->sa_family)
172 family = ((struct sockaddr *) &s->dest_addr)->sa_family;
173 res0 = udp_ipv6_resolve_host(0, s->local_port, SOCK_DGRAM, family, 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
182 if (udp_fd < 0)
183 goto fail;
184
185 if (bind(udp_fd, res0->ai_addr, res0->ai_addrlen) < 0) {
186 perror("bind");
187 goto fail;
188 }
189 freeaddrinfo(res0);
190 res0 = NULL;
191
192 addrlen = sizeof(clientaddr);
193 if (getsockname(udp_fd, (struct sockaddr *)&clientaddr, &addrlen) < 0) {
194 perror("getsockname");
195 goto fail;
196 }
197
198 if (getnameinfo((struct sockaddr *)&clientaddr, addrlen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
199 perror("getnameinfo");
200 goto fail;
201 }
202
203 s->local_port = strtol(sbuf, NULL, 10);
204
205 return udp_fd;
206
207 fail:
208 if (udp_fd >= 0)
209 closesocket(udp_fd);
210 if(res0)
211 freeaddrinfo(res0);
212 return -1;
213 }
214
215 #else
216
217 static int udp_set_url(struct sockaddr_in *addr, const char *hostname, int port)
218 {
219 /* set the destination address */
220 if (resolve_host(&addr->sin_addr, hostname) < 0)
221 return AVERROR(EIO);
222 addr->sin_family = AF_INET;
223 addr->sin_port = htons(port);
224
225 return sizeof(struct sockaddr_in);
226 }
227
228 #endif /* CONFIG_IPV6 */
229
230
231 /**
232 * If no filename is given to av_open_input_file because you want to
233 * get the local port first, then you must call this function to set
234 * the remote server address.
235 *
236 * url syntax: udp://host:port[?option=val...]
237 * option: 'multicast=1' : enable multicast
238 * 'ttl=n' : set the ttl value (for multicast only)
239 * 'localport=n' : set the local port
240 * 'pkt_size=n' : set max packet size
241 * 'reuse=1' : enable reusing the socket
242 *
243 * @param s1 media file context
244 * @param uri of the remote server
245 * @return zero if no error.
246 */
247 int udp_set_remote_url(URLContext *h, const char *uri)
248 {
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 s->dest_addr_len = udp_set_url(&s->dest_addr, hostname, port);
257 if (s->dest_addr_len < 0) {
258 return AVERROR(EIO);
259 }
260
261 return 0;
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;
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_mallocz(sizeof(UDPContext));
307 if (!s)
308 return AVERROR(ENOMEM);
309
310 h->priv_data = s;
311 s->ttl = 16;
312 p = strchr(uri, '?');
313 if (p) {
314 s->is_multicast = find_info_tag(buf, sizeof(buf), "multicast", p);
315 s->reuse_socket = find_info_tag(buf, sizeof(buf), "reuse", p);
316 if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
317 s->ttl = strtol(buf, NULL, 10);
318 }
319 if (find_info_tag(buf, sizeof(buf), "localport", p)) {
320 s->local_port = strtol(buf, NULL, 10);
321 }
322 if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
323 h->max_packet_size = strtol(buf, NULL, 10);
324 }
325 }
326
327 /* fill the dest addr */
328 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
329
330 /* XXX: fix url_split */
331 if (hostname[0] == '\0' || hostname[0] == '?') {
332 /* only accepts null hostname if input */
333 if (s->is_multicast || (flags & URL_WRONLY))
334 goto fail;
335 } else {
336 udp_set_remote_url(h, uri);
337 }
338
339 if(!ff_network_init())
340 return AVERROR(EIO);
341
342 if (s->is_multicast && !(h->flags & URL_WRONLY))
343 s->local_port = port;
344 #ifndef CONFIG_IPV6
345 udp_fd = socket(AF_INET, SOCK_DGRAM, 0);
346 if (udp_fd < 0)
347 goto fail;
348
349 my_addr.sin_family = AF_INET;
350 my_addr.sin_addr.s_addr = htonl (INADDR_ANY);
351 my_addr.sin_port = htons(s->local_port);
352
353 if (s->reuse_socket)
354 if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
355 goto fail;
356
357 /* the bind is needed to give a port to the socket now */
358 if (bind(udp_fd,(struct sockaddr *)&my_addr, sizeof(my_addr)) < 0)
359 goto fail;
360
361 len = sizeof(my_addr);
362 getsockname(udp_fd, (struct sockaddr *)&my_addr, &len);
363 s->local_port = ntohs(my_addr.sin_port);
364 #else
365 udp_fd = udp_ipv6_set_local(h);
366 if (udp_fd < 0)
367 goto fail;
368 #endif /* CONFIG_IPV6 */
369 if (s->is_multicast) {
370 if (h->flags & URL_WRONLY) {
371 /* output */
372 if (udp_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
373 goto fail;
374 } else {
375 /* input */
376 if (udp_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0)
377 goto fail;
378 }
379 }
380
381 if (is_output) {
382 /* limit the tx buf size to limit latency */
383 tmp = UDP_TX_BUF_SIZE;
384 if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
385 perror("setsockopt sndbuf");
386 goto fail;
387 }
388 } else {
389 /* set udp recv buffer size to the largest possible udp packet size to
390 * avoid losing data on OSes that set this too low by default. */
391 tmp = UDP_MAX_PKT_SIZE;
392 setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp));
393 }
394
395 s->udp_fd = udp_fd;
396 return 0;
397 fail:
398 if (udp_fd >= 0)
399 closesocket(udp_fd);
400 av_free(s);
401 return AVERROR(EIO);
402 }
403
404 static int udp_read(URLContext *h, uint8_t *buf, int size)
405 {
406 UDPContext *s = h->priv_data;
407 int len;
408
409 for(;;) {
410 len = recv(s->udp_fd, buf, size, 0);
411 if (len < 0) {
412 if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
413 ff_neterrno() != FF_NETERROR(EINTR))
414 return AVERROR(EIO);
415 } else {
416 break;
417 }
418 }
419 return len;
420 }
421
422 static int udp_write(URLContext *h, uint8_t *buf, int size)
423 {
424 UDPContext *s = h->priv_data;
425 int ret;
426
427 for(;;) {
428 ret = sendto (s->udp_fd, buf, size, 0,
429 (struct sockaddr *) &s->dest_addr,
430 s->dest_addr_len);
431 if (ret < 0) {
432 if (ff_neterrno() != FF_NETERROR(EINTR) &&
433 ff_neterrno() != FF_NETERROR(EAGAIN))
434 return AVERROR(EIO);
435 } else {
436 break;
437 }
438 }
439 return size;
440 }
441
442 static int udp_close(URLContext *h)
443 {
444 UDPContext *s = h->priv_data;
445
446 if (s->is_multicast && !(h->flags & URL_WRONLY))
447 udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
448 closesocket(s->udp_fd);
449 ff_network_close();
450 av_free(s);
451 return 0;
452 }
453
454 URLProtocol udp_protocol = {
455 "udp",
456 udp_open,
457 udp_read,
458 udp_write,
459 NULL, /* seek */
460 udp_close,
461 };