Use recv() instead of recvfrom() (removes some other differences between
[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 37 struct sockaddr_in dest_addr;
7a91333f
HZ
38#else
39 struct sockaddr_storage dest_addr;
7a91333f 40#endif
0ec12335 41 size_t dest_addr_len;
de6d9b64
FB
42} UDPContext;
43
44#define UDP_TX_BUF_SIZE 32768
f18cae4d 45#define UDP_MAX_PKT_SIZE 65536
de6d9b64 46
9cdcb04f 47static int udp_set_multicast_ttl(int sockfd, int mcastTTL, struct sockaddr *addr) {
a8bde059 48#ifdef IP_MULTICAST_TTL
7a91333f
HZ
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 }
a8bde059
LA
55#endif
56#ifdef CONFIG_IPV6
7a91333f
HZ
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 }
a8bde059 63#endif
7a91333f
HZ
64 return 0;
65}
66
9cdcb04f 67static int udp_join_multicast_group(int sockfd, struct sockaddr *addr) {
a8bde059 68#ifdef IP_ADD_MEMBERSHIP
7a91333f 69 if (addr->sa_family == AF_INET) {
a8bde059
LA
70 struct ip_mreq mreq;
71
7a91333f
HZ
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 }
a8bde059
LA
79#endif
80#ifdef CONFIG_IPV6
7a91333f 81 if (addr->sa_family == AF_INET6) {
a8bde059
LA
82 struct ipv6_mreq mreq6;
83
7a91333f
HZ
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 }
a8bde059 91#endif
7a91333f
HZ
92 return 0;
93}
94
9cdcb04f 95static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr) {
a8bde059 96#ifdef IP_DROP_MEMBERSHIP
7a91333f 97 if (addr->sa_family == AF_INET) {
a8bde059
LA
98 struct ip_mreq mreq;
99
7a91333f
HZ
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 }
a8bde059
LA
107#endif
108#ifdef CONFIG_IPV6
7a91333f 109 if (addr->sa_family == AF_INET6) {
a8bde059
LA
110 struct ipv6_mreq mreq6;
111
7a91333f
HZ
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 }
a8bde059 119#endif
7a91333f
HZ
120 return 0;
121}
122
a8bde059 123#ifdef CONFIG_IPV6
7b49ce2e 124static struct addrinfo* udp_ipv6_resolve_host(const char *hostname, int port, int type, int family, int flags) {
7a91333f
HZ
125 struct addrinfo hints, *res = 0;
126 int error;
127 char sport[16];
d607861c 128 const char *node = 0, *service = "0";
7a91333f
HZ
129
130 if (port > 0) {
2fc8ea24 131 snprintf(sport, sizeof(sport), "%d", port);
7a91333f
HZ
132 service = sport;
133 }
134 if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) {
135 node = hostname;
136 }
7d8576c2
LA
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
7a91333f
HZ
145 return res;
146}
147
7b49ce2e 148static int udp_ipv6_set_remote_url(URLContext *h, const char *uri) {
7a91333f
HZ
149 UDPContext *s = h->priv_data;
150 char hostname[256];
151 int port;
152 struct addrinfo *res0;
153 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
154 res0 = udp_ipv6_resolve_host(hostname, port, SOCK_DGRAM, AF_UNSPEC, 0);
6f3e0b21 155 if (res0 == 0) return AVERROR(EIO);
7a91333f
HZ
156 memcpy(&s->dest_addr, res0->ai_addr, res0->ai_addrlen);
157 s->dest_addr_len = res0->ai_addrlen;
158 freeaddrinfo(res0);
159 return 0;
160}
161
7b49ce2e 162static int udp_ipv6_set_local(URLContext *h) {
7a91333f
HZ
163 UDPContext *s = h->priv_data;
164 int udp_fd = -1;
165 struct sockaddr_storage clientaddr;
166 socklen_t addrlen;
167 char sbuf[NI_MAXSERV];
168 char hbuf[NI_MAXHOST];
276358c1 169 struct addrinfo *res0 = NULL, *res = NULL;
51844e6c 170 int family = AF_UNSPEC;
115329f1 171
51844e6c
RB
172 if (((struct sockaddr *) &s->dest_addr)->sa_family)
173 family = ((struct sockaddr *) &s->dest_addr)->sa_family;
174 res0 = udp_ipv6_resolve_host(0, s->local_port, SOCK_DGRAM, family, AI_PASSIVE);
7d8576c2
LA
175 if (res0 == 0)
176 goto fail;
177 for (res = res0; res; res=res->ai_next) {
178 udp_fd = socket(res->ai_family, SOCK_DGRAM, 0);
179 if (udp_fd > 0) break;
180 perror("socket");
181 }
276358c1
HZ
182
183 if (udp_fd < 0)
7a91333f 184 goto fail;
115329f1 185
7d8576c2
LA
186 if (bind(udp_fd, res0->ai_addr, res0->ai_addrlen) < 0) {
187 perror("bind");
188 goto fail;
189 }
190 freeaddrinfo(res0);
88730be6 191 res0 = NULL;
7a91333f
HZ
192
193 addrlen = sizeof(clientaddr);
194 if (getsockname(udp_fd, (struct sockaddr *)&clientaddr, &addrlen) < 0) {
195 perror("getsockname");
196 goto fail;
197 }
198
199 if (getnameinfo((struct sockaddr *)&clientaddr, addrlen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
200 perror("getnameinfo");
201 goto fail;
202 }
203
204 s->local_port = strtol(sbuf, NULL, 10);
115329f1 205
7a91333f 206 return udp_fd;
115329f1 207
7a91333f
HZ
208 fail:
209 if (udp_fd >= 0)
7a91333f 210 closesocket(udp_fd);
88730be6
MR
211 if(res0)
212 freeaddrinfo(res0);
7a91333f
HZ
213 return -1;
214}
215
0a4f20c6 216#endif /* CONFIG_IPV6 */
7a91333f
HZ
217
218
2d225591
FB
219/**
220 * If no filename is given to av_open_input_file because you want to
221 * get the local port first, then you must call this function to set
222 * the remote server address.
223 *
224 * url syntax: udp://host:port[?option=val...]
115329f1 225 * option: 'multicast=1' : enable multicast
2d225591
FB
226 * 'ttl=n' : set the ttl value (for multicast only)
227 * 'localport=n' : set the local port
9899efb4 228 * 'pkt_size=n' : set max packet size
34ecc397 229 * 'reuse=1' : enable reusing the socket
2d225591
FB
230 *
231 * @param s1 media file context
232 * @param uri of the remote server
233 * @return zero if no error.
234 */
235int udp_set_remote_url(URLContext *h, const char *uri)
236{
7a91333f
HZ
237#ifdef CONFIG_IPV6
238 return udp_ipv6_set_remote_url(h, uri);
239#else
2d225591
FB
240 UDPContext *s = h->priv_data;
241 char hostname[256];
242 int port;
115329f1 243
6ba5cbc6 244 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
de6d9b64 245
2d225591
FB
246 /* set the destination address */
247 if (resolve_host(&s->dest_addr.sin_addr, hostname) < 0)
6f3e0b21 248 return AVERROR(EIO);
2d225591
FB
249 s->dest_addr.sin_family = AF_INET;
250 s->dest_addr.sin_port = htons(port);
0ec12335 251 s->dest_addr_len = sizeof(s->dest_addr);
2d225591 252 return 0;
7a91333f 253#endif
2d225591
FB
254}
255
256/**
257 * Return the local port used by the UDP connexion
258 * @param s1 media file context
259 * @return the local port number
260 */
261int udp_get_local_port(URLContext *h)
262{
263 UDPContext *s = h->priv_data;
264 return s->local_port;
265}
266
267/**
268 * Return the udp file handle for select() usage to wait for several RTP
269 * streams at the same time.
270 * @param h media file context
271 */
272int udp_get_file_handle(URLContext *h)
273{
274 UDPContext *s = h->priv_data;
275 return s->udp_fd;
276}
277
278/* put it in UDP context */
de6d9b64
FB
279/* return non zero if error */
280static int udp_open(URLContext *h, const char *uri, int flags)
281{
de6d9b64 282 char hostname[1024];
2d225591
FB
283 int port, udp_fd = -1, tmp;
284 UDPContext *s = NULL;
88730be6 285 int is_output;
2d225591
FB
286 const char *p;
287 char buf[256];
88730be6
MR
288#ifndef CONFIG_IPV6
289 struct sockaddr_in my_addr, my_addr1;
290 int len;
291#endif
de6d9b64
FB
292
293 h->is_streamed = 1;
9899efb4 294 h->max_packet_size = 1472;
de6d9b64 295
2d225591 296 is_output = (flags & URL_WRONLY);
115329f1 297
51844e6c 298 s = av_mallocz(sizeof(UDPContext));
2d225591 299 if (!s)
8fa36ae0 300 return AVERROR(ENOMEM);
2d225591
FB
301
302 h->priv_data = s;
303 s->ttl = 16;
304 s->is_multicast = 0;
cc74aafb 305 s->local_port = 0;
34ecc397 306 s->reuse_socket = 0;
2d225591
FB
307 p = strchr(uri, '?');
308 if (p) {
309 s->is_multicast = find_info_tag(buf, sizeof(buf), "multicast", p);
34ecc397 310 s->reuse_socket = find_info_tag(buf, sizeof(buf), "reuse", p);
2d225591
FB
311 if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
312 s->ttl = strtol(buf, NULL, 10);
313 }
314 if (find_info_tag(buf, sizeof(buf), "localport", p)) {
315 s->local_port = strtol(buf, NULL, 10);
316 }
9899efb4
MK
317 if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
318 h->max_packet_size = strtol(buf, NULL, 10);
319 }
de6d9b64 320 }
2d225591
FB
321
322 /* fill the dest addr */
6ba5cbc6 323 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
115329f1 324
2d225591
FB
325 /* XXX: fix url_split */
326 if (hostname[0] == '\0' || hostname[0] == '?') {
327 /* only accepts null hostname if input */
328 if (s->is_multicast || (flags & URL_WRONLY))
329 goto fail;
330 } else {
331 udp_set_remote_url(h, uri);
332 }
de6d9b64 333
1642cb6b
RP
334 if(!ff_network_init())
335 return AVERROR(EIO);
336
7a91333f 337#ifndef CONFIG_IPV6
b5cccea0 338 udp_fd = socket(AF_INET, SOCK_DGRAM, 0);
2d225591
FB
339 if (udp_fd < 0)
340 goto fail;
341
772956c5
FB
342 my_addr.sin_family = AF_INET;
343 my_addr.sin_addr.s_addr = htonl (INADDR_ANY);
2d225591 344 if (s->is_multicast && !(h->flags & URL_WRONLY)) {
772956c5
FB
345 /* special case: the bind must be done on the multicast address port */
346 my_addr.sin_port = s->dest_addr.sin_port;
2d225591 347 } else {
2d225591 348 my_addr.sin_port = htons(s->local_port);
2d225591 349 }
de6d9b64 350
34ecc397
T
351 if (s->reuse_socket)
352 if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
353 goto fail;
354
de6d9b64 355 /* the bind is needed to give a port to the socket now */
115329f1 356 if (bind(udp_fd,(struct sockaddr *)&my_addr, sizeof(my_addr)) < 0)
de6d9b64
FB
357 goto fail;
358
2d225591
FB
359 len = sizeof(my_addr1);
360 getsockname(udp_fd, (struct sockaddr *)&my_addr1, &len);
361 s->local_port = ntohs(my_addr1.sin_port);
7a91333f
HZ
362#else
363 if (s->is_multicast && !(h->flags & URL_WRONLY))
364 s->local_port = port;
365 udp_fd = udp_ipv6_set_local(h);
366 if (udp_fd < 0)
367 goto fail;
a8bde059 368#endif /* CONFIG_IPV6 */
7a91333f
HZ
369 if (s->is_multicast) {
370 if (h->flags & URL_WRONLY) {
a8bde059 371 /* output */
9cdcb04f 372 if (udp_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
7a91333f
HZ
373 goto fail;
374 } else {
a8bde059 375 /* input */
9cdcb04f 376 if (udp_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0)
7a91333f
HZ
377 goto fail;
378 }
379 }
de6d9b64 380
2d225591
FB
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 }
f18cae4d
RP
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));
2d225591
FB
393 }
394
395 s->udp_fd = udp_fd;
de6d9b64
FB
396 return 0;
397 fail:
2d225591 398 if (udp_fd >= 0)
9ddd71fc 399 closesocket(udp_fd);
2d225591 400 av_free(s);
6f3e0b21 401 return AVERROR(EIO);
de6d9b64
FB
402}
403
0c1a9eda 404static int udp_read(URLContext *h, uint8_t *buf, int size)
de6d9b64
FB
405{
406 UDPContext *s = h->priv_data;
191e8ca7 407 int len;
2d225591
FB
408
409 for(;;) {
4c7d5764 410 len = recv(s->udp_fd, buf, size, 0);
2d225591 411 if (len < 0) {
8da4034f
AB
412 if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
413 ff_neterrno() != FF_NETERROR(EINTR))
6f3e0b21 414 return AVERROR(EIO);
2d225591
FB
415 } else {
416 break;
417 }
418 }
419 return len;
de6d9b64
FB
420}
421
0c1a9eda 422static int udp_write(URLContext *h, uint8_t *buf, int size)
de6d9b64
FB
423{
424 UDPContext *s = h->priv_data;
2d225591
FB
425 int ret;
426
427 for(;;) {
115329f1 428 ret = sendto (s->udp_fd, buf, size, 0,
2d225591 429 (struct sockaddr *) &s->dest_addr,
7a91333f 430 s->dest_addr_len);
2d225591 431 if (ret < 0) {
8da4034f
AB
432 if (ff_neterrno() != FF_NETERROR(EINTR) &&
433 ff_neterrno() != FF_NETERROR(EAGAIN))
6f3e0b21 434 return AVERROR(EIO);
2d225591
FB
435 } else {
436 break;
437 }
de6d9b64 438 }
2d225591
FB
439 return size;
440}
441
442static int udp_close(URLContext *h)
443{
444 UDPContext *s = h->priv_data;
445
7a91333f 446 if (s->is_multicast && !(h->flags & URL_WRONLY))
9cdcb04f 447 udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
9ddd71fc 448 closesocket(s->udp_fd);
1642cb6b 449 ff_network_close();
2d225591
FB
450 av_free(s);
451 return 0;
de6d9b64
FB
452}
453
454URLProtocol udp_protocol = {
455 "udp",
456 udp_open,
2d225591 457 udp_read,
de6d9b64
FB
458 udp_write,
459 NULL, /* seek */
460 udp_close,
461};