Resolve hosts and bind sockets even when the local_port is not set (0)
[libav.git] / libavformat / udp.c
CommitLineData
de6d9b64
FB
1/*
2 * UDP prototype streaming system
2d225591 3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
de6d9b64 4 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
19720f15
FB
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
de6d9b64 11 *
b78e7197 12 * FFmpeg is distributed in the hope that it will be useful,
de6d9b64 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
de6d9b64 16 *
19720f15 17 * You should have received a copy of the GNU Lesser General Public
b78e7197 18 * License along with FFmpeg; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64 20 */
8be1c656 21#include "avformat.h"
de6d9b64 22#include <unistd.h>
42572ef5 23#include "network.h"
de6d9b64 24
9c633e9a
AB
25#ifndef IPV6_ADD_MEMBERSHIP
26#define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
27#define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
28#endif
29
de6d9b64 30typedef struct {
2d225591
FB
31 int udp_fd;
32 int ttl;
33 int is_multicast;
34 int local_port;
34ecc397 35 int reuse_socket;
7a91333f 36#ifndef CONFIG_IPV6
2d225591
FB
37 struct ip_mreq mreq;
38 struct sockaddr_in dest_addr;
7a91333f
HZ
39#else
40 struct sockaddr_storage dest_addr;
41 size_t dest_addr_len;
42#endif
de6d9b64
FB
43} UDPContext;
44
45#define UDP_TX_BUF_SIZE 32768
f18cae4d 46#define UDP_MAX_PKT_SIZE 65536
de6d9b64 47
7a91333f
HZ
48#ifdef CONFIG_IPV6
49
7b49ce2e 50static int udp_ipv6_set_multicast_ttl(int sockfd, int mcastTTL, struct sockaddr *addr) {
7a91333f
HZ
51 if (addr->sa_family == AF_INET) {
52 if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) {
53 perror("setsockopt(IP_MULTICAST_TTL)");
54 return -1;
55 }
56 }
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 return 0;
64}
65
7b49ce2e 66static int udp_ipv6_join_multicast_group(int sockfd, struct sockaddr *addr) {
7a91333f 67 struct ip_mreq mreq;
115329f1 68 struct ipv6_mreq mreq6;
7a91333f
HZ
69 if (addr->sa_family == AF_INET) {
70 mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
71 mreq.imr_interface.s_addr= INADDR_ANY;
72 if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
73 perror("setsockopt(IP_ADD_MEMBERSHIP)");
74 return -1;
75 }
76 }
77 if (addr->sa_family == AF_INET6) {
78 memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
79 mreq6.ipv6mr_interface= 0;
80 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
81 perror("setsockopt(IPV6_ADD_MEMBERSHIP)");
82 return -1;
83 }
84 }
85 return 0;
86}
87
7b49ce2e 88static int udp_ipv6_leave_multicast_group(int sockfd, struct sockaddr *addr) {
7a91333f 89 struct ip_mreq mreq;
115329f1 90 struct ipv6_mreq mreq6;
7a91333f
HZ
91 if (addr->sa_family == AF_INET) {
92 mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
93 mreq.imr_interface.s_addr= INADDR_ANY;
94 if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
95 perror("setsockopt(IP_DROP_MEMBERSHIP)");
96 return -1;
97 }
98 }
99 if (addr->sa_family == AF_INET6) {
100 memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
101 mreq6.ipv6mr_interface= 0;
102 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
103 perror("setsockopt(IPV6_DROP_MEMBERSHIP)");
104 return -1;
105 }
106 }
107 return 0;
108}
109
7b49ce2e 110static struct addrinfo* udp_ipv6_resolve_host(const char *hostname, int port, int type, int family, int flags) {
7a91333f
HZ
111 struct addrinfo hints, *res = 0;
112 int error;
113 char sport[16];
d607861c 114 const char *node = 0, *service = "0";
7a91333f
HZ
115
116 if (port > 0) {
2fc8ea24 117 snprintf(sport, sizeof(sport), "%d", port);
7a91333f
HZ
118 service = sport;
119 }
120 if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) {
121 node = hostname;
122 }
7a91333f
HZ
123 memset(&hints, 0, sizeof(hints));
124 hints.ai_socktype = type;
125 hints.ai_family = family;
115329f1 126 hints.ai_flags = flags;
7a91333f 127 if ((error = getaddrinfo(node, service, &hints, &res))) {
58379c06 128 av_log(NULL, AV_LOG_ERROR, "udp_ipv6_resolve_host: %s\n", gai_strerror(error));
7a91333f 129 }
7a91333f
HZ
130 return res;
131}
132
7b49ce2e 133static int udp_ipv6_set_remote_url(URLContext *h, const char *uri) {
7a91333f
HZ
134 UDPContext *s = h->priv_data;
135 char hostname[256];
136 int port;
137 struct addrinfo *res0;
138 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
139 res0 = udp_ipv6_resolve_host(hostname, port, SOCK_DGRAM, AF_UNSPEC, 0);
6f3e0b21 140 if (res0 == 0) return AVERROR(EIO);
7a91333f
HZ
141 memcpy(&s->dest_addr, res0->ai_addr, res0->ai_addrlen);
142 s->dest_addr_len = res0->ai_addrlen;
143 freeaddrinfo(res0);
144 return 0;
145}
146
7b49ce2e 147static int udp_ipv6_set_local(URLContext *h) {
7a91333f
HZ
148 UDPContext *s = h->priv_data;
149 int udp_fd = -1;
150 struct sockaddr_storage clientaddr;
151 socklen_t addrlen;
152 char sbuf[NI_MAXSERV];
153 char hbuf[NI_MAXHOST];
276358c1 154 struct addrinfo *res0 = NULL, *res = NULL;
115329f1 155
7a91333f 156 res0 = udp_ipv6_resolve_host(0, s->local_port, SOCK_DGRAM, AF_UNSPEC, AI_PASSIVE);
88730be6
MR
157 if (res0 == 0)
158 goto fail;
115329f1 159 for (res = res0; res; res=res->ai_next) {
276358c1
HZ
160 udp_fd = socket(res->ai_family, SOCK_DGRAM, 0);
161 if (udp_fd > 0) break;
162 perror("socket");
163 }
276358c1
HZ
164
165 if (udp_fd < 0)
7a91333f 166 goto fail;
115329f1 167
7a91333f
HZ
168 if (bind(udp_fd, res0->ai_addr, res0->ai_addrlen) < 0) {
169 perror("bind");
170 goto fail;
171 }
88730be6
MR
172 freeaddrinfo(res0);
173 res0 = NULL;
7a91333f
HZ
174
175 addrlen = sizeof(clientaddr);
176 if (getsockname(udp_fd, (struct sockaddr *)&clientaddr, &addrlen) < 0) {
177 perror("getsockname");
178 goto fail;
179 }
180
181 if (getnameinfo((struct sockaddr *)&clientaddr, addrlen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
182 perror("getnameinfo");
183 goto fail;
184 }
185
186 s->local_port = strtol(sbuf, NULL, 10);
115329f1 187
7a91333f 188 return udp_fd;
115329f1 189
7a91333f
HZ
190 fail:
191 if (udp_fd >= 0)
7a91333f 192 closesocket(udp_fd);
88730be6
MR
193 if(res0)
194 freeaddrinfo(res0);
7a91333f
HZ
195 return -1;
196}
197
0a4f20c6 198#endif /* CONFIG_IPV6 */
7a91333f
HZ
199
200
2d225591
FB
201/**
202 * If no filename is given to av_open_input_file because you want to
203 * get the local port first, then you must call this function to set
204 * the remote server address.
205 *
206 * url syntax: udp://host:port[?option=val...]
115329f1 207 * option: 'multicast=1' : enable multicast
2d225591
FB
208 * 'ttl=n' : set the ttl value (for multicast only)
209 * 'localport=n' : set the local port
9899efb4 210 * 'pkt_size=n' : set max packet size
34ecc397 211 * 'reuse=1' : enable reusing the socket
2d225591
FB
212 *
213 * @param s1 media file context
214 * @param uri of the remote server
215 * @return zero if no error.
216 */
217int udp_set_remote_url(URLContext *h, const char *uri)
218{
7a91333f
HZ
219#ifdef CONFIG_IPV6
220 return udp_ipv6_set_remote_url(h, uri);
221#else
2d225591
FB
222 UDPContext *s = h->priv_data;
223 char hostname[256];
224 int port;
115329f1 225
6ba5cbc6 226 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
de6d9b64 227
2d225591
FB
228 /* set the destination address */
229 if (resolve_host(&s->dest_addr.sin_addr, hostname) < 0)
6f3e0b21 230 return AVERROR(EIO);
2d225591
FB
231 s->dest_addr.sin_family = AF_INET;
232 s->dest_addr.sin_port = htons(port);
233 return 0;
7a91333f 234#endif
2d225591
FB
235}
236
237/**
238 * Return the local port used by the UDP connexion
239 * @param s1 media file context
240 * @return the local port number
241 */
242int udp_get_local_port(URLContext *h)
243{
244 UDPContext *s = h->priv_data;
245 return s->local_port;
246}
247
248/**
249 * Return the udp file handle for select() usage to wait for several RTP
250 * streams at the same time.
251 * @param h media file context
252 */
253int udp_get_file_handle(URLContext *h)
254{
255 UDPContext *s = h->priv_data;
256 return s->udp_fd;
257}
258
259/* put it in UDP context */
de6d9b64
FB
260/* return non zero if error */
261static int udp_open(URLContext *h, const char *uri, int flags)
262{
de6d9b64 263 char hostname[1024];
2d225591
FB
264 int port, udp_fd = -1, tmp;
265 UDPContext *s = NULL;
88730be6 266 int is_output;
2d225591
FB
267 const char *p;
268 char buf[256];
88730be6
MR
269#ifndef CONFIG_IPV6
270 struct sockaddr_in my_addr, my_addr1;
271 int len;
272#endif
de6d9b64
FB
273
274 h->is_streamed = 1;
9899efb4 275 h->max_packet_size = 1472;
de6d9b64 276
2d225591 277 is_output = (flags & URL_WRONLY);
115329f1 278
2d225591
FB
279 s = av_malloc(sizeof(UDPContext));
280 if (!s)
8fa36ae0 281 return AVERROR(ENOMEM);
2d225591
FB
282
283 h->priv_data = s;
284 s->ttl = 16;
285 s->is_multicast = 0;
cc74aafb 286 s->local_port = 0;
34ecc397 287 s->reuse_socket = 0;
2d225591
FB
288 p = strchr(uri, '?');
289 if (p) {
290 s->is_multicast = find_info_tag(buf, sizeof(buf), "multicast", p);
34ecc397 291 s->reuse_socket = find_info_tag(buf, sizeof(buf), "reuse", p);
2d225591
FB
292 if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
293 s->ttl = strtol(buf, NULL, 10);
294 }
295 if (find_info_tag(buf, sizeof(buf), "localport", p)) {
296 s->local_port = strtol(buf, NULL, 10);
297 }
9899efb4
MK
298 if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
299 h->max_packet_size = strtol(buf, NULL, 10);
300 }
de6d9b64 301 }
2d225591
FB
302
303 /* fill the dest addr */
6ba5cbc6 304 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
115329f1 305
2d225591
FB
306 /* XXX: fix url_split */
307 if (hostname[0] == '\0' || hostname[0] == '?') {
308 /* only accepts null hostname if input */
309 if (s->is_multicast || (flags & URL_WRONLY))
310 goto fail;
311 } else {
312 udp_set_remote_url(h, uri);
313 }
de6d9b64 314
1642cb6b
RP
315 if(!ff_network_init())
316 return AVERROR(EIO);
317
7a91333f 318#ifndef CONFIG_IPV6
b5cccea0 319 udp_fd = socket(AF_INET, SOCK_DGRAM, 0);
2d225591
FB
320 if (udp_fd < 0)
321 goto fail;
322
772956c5
FB
323 my_addr.sin_family = AF_INET;
324 my_addr.sin_addr.s_addr = htonl (INADDR_ANY);
2d225591 325 if (s->is_multicast && !(h->flags & URL_WRONLY)) {
772956c5
FB
326 /* special case: the bind must be done on the multicast address port */
327 my_addr.sin_port = s->dest_addr.sin_port;
2d225591 328 } else {
2d225591 329 my_addr.sin_port = htons(s->local_port);
2d225591 330 }
de6d9b64 331
34ecc397
T
332 if (s->reuse_socket)
333 if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
334 goto fail;
335
de6d9b64 336 /* the bind is needed to give a port to the socket now */
115329f1 337 if (bind(udp_fd,(struct sockaddr *)&my_addr, sizeof(my_addr)) < 0)
de6d9b64
FB
338 goto fail;
339
2d225591
FB
340 len = sizeof(my_addr1);
341 getsockname(udp_fd, (struct sockaddr *)&my_addr1, &len);
342 s->local_port = ntohs(my_addr1.sin_port);
de6d9b64 343
c9c1a8e7 344#ifdef IP_MULTICAST_TTL
2d225591
FB
345 if (s->is_multicast) {
346 if (h->flags & URL_WRONLY) {
347 /* output */
115329f1 348 if (setsockopt(udp_fd, IPPROTO_IP, IP_MULTICAST_TTL,
2d225591
FB
349 &s->ttl, sizeof(s->ttl)) < 0) {
350 perror("IP_MULTICAST_TTL");
351 goto fail;
352 }
353 } else {
354 /* input */
355 memset(&s->mreq, 0, sizeof(s->mreq));
356 s->mreq.imr_multiaddr = s->dest_addr.sin_addr;
357 s->mreq.imr_interface.s_addr = htonl (INADDR_ANY);
115329f1 358 if (setsockopt(udp_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
2d225591
FB
359 &s->mreq, sizeof(s->mreq)) < 0) {
360 perror("rtp: IP_ADD_MEMBERSHIP");
361 goto fail;
362 }
363 }
de6d9b64 364 }
9ddd71fc 365#endif
7a91333f
HZ
366#else
367 if (s->is_multicast && !(h->flags & URL_WRONLY))
368 s->local_port = port;
369 udp_fd = udp_ipv6_set_local(h);
370 if (udp_fd < 0)
371 goto fail;
7a91333f
HZ
372 if (s->is_multicast) {
373 if (h->flags & URL_WRONLY) {
374 if (udp_ipv6_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
375 goto fail;
376 } else {
377 if (udp_ipv6_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0)
378 goto fail;
379 }
380 }
0a4f20c6 381#endif /* CONFIG_IPV6 */
de6d9b64 382
2d225591
FB
383 if (is_output) {
384 /* limit the tx buf size to limit latency */
385 tmp = UDP_TX_BUF_SIZE;
386 if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
387 perror("setsockopt sndbuf");
388 goto fail;
389 }
f18cae4d
RP
390 } else {
391 /* set udp recv buffer size to the largest possible udp packet size to
392 * avoid losing data on OSes that set this too low by default. */
393 tmp = UDP_MAX_PKT_SIZE;
394 setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp));
2d225591
FB
395 }
396
397 s->udp_fd = udp_fd;
de6d9b64
FB
398 return 0;
399 fail:
2d225591 400 if (udp_fd >= 0)
9ddd71fc 401 closesocket(udp_fd);
2d225591 402 av_free(s);
6f3e0b21 403 return AVERROR(EIO);
de6d9b64
FB
404}
405
0c1a9eda 406static int udp_read(URLContext *h, uint8_t *buf, int size)
de6d9b64
FB
407{
408 UDPContext *s = h->priv_data;
7a91333f 409#ifndef CONFIG_IPV6
2d225591 410 struct sockaddr_in from;
7a91333f
HZ
411#else
412 struct sockaddr_storage from;
413#endif
191e8ca7
MR
414 socklen_t from_len;
415 int len;
2d225591
FB
416
417 for(;;) {
418 from_len = sizeof(from);
419 len = recvfrom (s->udp_fd, buf, size, 0,
420 (struct sockaddr *)&from, &from_len);
421 if (len < 0) {
8da4034f
AB
422 if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
423 ff_neterrno() != FF_NETERROR(EINTR))
6f3e0b21 424 return AVERROR(EIO);
2d225591
FB
425 } else {
426 break;
427 }
428 }
429 return len;
de6d9b64
FB
430}
431
0c1a9eda 432static int udp_write(URLContext *h, uint8_t *buf, int size)
de6d9b64
FB
433{
434 UDPContext *s = h->priv_data;
2d225591
FB
435 int ret;
436
437 for(;;) {
115329f1 438 ret = sendto (s->udp_fd, buf, size, 0,
2d225591 439 (struct sockaddr *) &s->dest_addr,
7a91333f 440#ifndef CONFIG_IPV6
2d225591 441 sizeof (s->dest_addr));
7a91333f
HZ
442#else
443 s->dest_addr_len);
444#endif
2d225591 445 if (ret < 0) {
8da4034f
AB
446 if (ff_neterrno() != FF_NETERROR(EINTR) &&
447 ff_neterrno() != FF_NETERROR(EAGAIN))
6f3e0b21 448 return AVERROR(EIO);
2d225591
FB
449 } else {
450 break;
451 }
de6d9b64 452 }
2d225591
FB
453 return size;
454}
455
456static int udp_close(URLContext *h)
457{
458 UDPContext *s = h->priv_data;
459
7a91333f 460#ifndef CONFIG_IPV6
e9d511dc 461#ifdef IP_DROP_MEMBERSHIP
2d225591 462 if (s->is_multicast && !(h->flags & URL_WRONLY)) {
115329f1 463 if (setsockopt(s->udp_fd, IPPROTO_IP, IP_DROP_MEMBERSHIP,
2d225591
FB
464 &s->mreq, sizeof(s->mreq)) < 0) {
465 perror("IP_DROP_MEMBERSHIP");
466 }
467 }
e9d511dc 468#endif
7a91333f
HZ
469#else
470 if (s->is_multicast && !(h->flags & URL_WRONLY))
471 udp_ipv6_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
472#endif
9ddd71fc 473 closesocket(s->udp_fd);
1642cb6b 474 ff_network_close();
2d225591
FB
475 av_free(s);
476 return 0;
de6d9b64
FB
477}
478
479URLProtocol udp_protocol = {
480 "udp",
481 udp_open,
2d225591 482 udp_read,
de6d9b64
FB
483 udp_write,
484 NULL, /* seek */
485 udp_close,
486};