Use recv() instead of recvfrom() (removes some other differences between
[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_ipv6_set_remote_url(URLContext *h, const char *uri) {
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);
155 if (res0 == 0) return AVERROR(EIO);
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
162 static int udp_ipv6_set_local(URLContext *h) {
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];
169 struct addrinfo *res0 = NULL, *res = NULL;
170 int family = AF_UNSPEC;
171
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);
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 }
182
183 if (udp_fd < 0)
184 goto fail;
185
186 if (bind(udp_fd, res0->ai_addr, res0->ai_addrlen) < 0) {
187 perror("bind");
188 goto fail;
189 }
190 freeaddrinfo(res0);
191 res0 = NULL;
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);
205
206 return udp_fd;
207
208 fail:
209 if (udp_fd >= 0)
210 closesocket(udp_fd);
211 if(res0)
212 freeaddrinfo(res0);
213 return -1;
214 }
215
216 #endif /* CONFIG_IPV6 */
217
218
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...]
225 * option: 'multicast=1' : enable multicast
226 * 'ttl=n' : set the ttl value (for multicast only)
227 * 'localport=n' : set the local port
228 * 'pkt_size=n' : set max packet size
229 * 'reuse=1' : enable reusing the socket
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(EIO);
249 s->dest_addr.sin_family = AF_INET;
250 s->dest_addr.sin_port = htons(port);
251 s->dest_addr_len = sizeof(s->dest_addr);
252 return 0;
253 #endif
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 */
261 int 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 */
272 int 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 */
279 /* return non zero if error */
280 static int udp_open(URLContext *h, const char *uri, int flags)
281 {
282 char hostname[1024];
283 int port, udp_fd = -1, tmp;
284 UDPContext *s = NULL;
285 int is_output;
286 const char *p;
287 char buf[256];
288 #ifndef CONFIG_IPV6
289 struct sockaddr_in my_addr, my_addr1;
290 int len;
291 #endif
292
293 h->is_streamed = 1;
294 h->max_packet_size = 1472;
295
296 is_output = (flags & URL_WRONLY);
297
298 s = av_mallocz(sizeof(UDPContext));
299 if (!s)
300 return AVERROR(ENOMEM);
301
302 h->priv_data = s;
303 s->ttl = 16;
304 s->is_multicast = 0;
305 s->local_port = 0;
306 s->reuse_socket = 0;
307 p = strchr(uri, '?');
308 if (p) {
309 s->is_multicast = find_info_tag(buf, sizeof(buf), "multicast", p);
310 s->reuse_socket = find_info_tag(buf, sizeof(buf), "reuse", p);
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 }
317 if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
318 h->max_packet_size = strtol(buf, NULL, 10);
319 }
320 }
321
322 /* fill the dest addr */
323 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
324
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 }
333
334 if(!ff_network_init())
335 return AVERROR(EIO);
336
337 #ifndef CONFIG_IPV6
338 udp_fd = socket(AF_INET, SOCK_DGRAM, 0);
339 if (udp_fd < 0)
340 goto fail;
341
342 my_addr.sin_family = AF_INET;
343 my_addr.sin_addr.s_addr = htonl (INADDR_ANY);
344 if (s->is_multicast && !(h->flags & URL_WRONLY)) {
345 /* special case: the bind must be done on the multicast address port */
346 my_addr.sin_port = s->dest_addr.sin_port;
347 } else {
348 my_addr.sin_port = htons(s->local_port);
349 }
350
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
355 /* the bind is needed to give a port to the socket now */
356 if (bind(udp_fd,(struct sockaddr *)&my_addr, sizeof(my_addr)) < 0)
357 goto fail;
358
359 len = sizeof(my_addr1);
360 getsockname(udp_fd, (struct sockaddr *)&my_addr1, &len);
361 s->local_port = ntohs(my_addr1.sin_port);
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;
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 };