Remove the useless variable "my_addr1"
[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
d05cb726 148static int udp_set_url(struct sockaddr_storage *addr, const char *hostname, int port) {
7a91333f 149 struct addrinfo *res0;
d05cb726
LA
150 int addr_len;
151
7a91333f 152 res0 = udp_ipv6_resolve_host(hostname, port, SOCK_DGRAM, AF_UNSPEC, 0);
6f3e0b21 153 if (res0 == 0) return AVERROR(EIO);
d05cb726
LA
154 memcpy(addr, res0->ai_addr, res0->ai_addrlen);
155 addr_len = res0->ai_addrlen;
7a91333f 156 freeaddrinfo(res0);
d05cb726
LA
157
158 return addr_len;
7a91333f
HZ
159}
160
7b49ce2e 161static int udp_ipv6_set_local(URLContext *h) {
7a91333f
HZ
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];
276358c1 168 struct addrinfo *res0 = NULL, *res = NULL;
51844e6c 169 int family = AF_UNSPEC;
115329f1 170
51844e6c
RB
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);
7d8576c2
LA
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 }
276358c1
HZ
181
182 if (udp_fd < 0)
7a91333f 183 goto fail;
115329f1 184
7d8576c2
LA
185 if (bind(udp_fd, res0->ai_addr, res0->ai_addrlen) < 0) {
186 perror("bind");
187 goto fail;
188 }
189 freeaddrinfo(res0);
88730be6 190 res0 = NULL;
7a91333f
HZ
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);
115329f1 204
7a91333f 205 return udp_fd;
115329f1 206
7a91333f
HZ
207 fail:
208 if (udp_fd >= 0)
7a91333f 209 closesocket(udp_fd);
88730be6
MR
210 if(res0)
211 freeaddrinfo(res0);
7a91333f
HZ
212 return -1;
213}
214
d05cb726
LA
215#else
216
217static 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
0a4f20c6 228#endif /* CONFIG_IPV6 */
7a91333f
HZ
229
230
2d225591
FB
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...]
115329f1 237 * option: 'multicast=1' : enable multicast
2d225591
FB
238 * 'ttl=n' : set the ttl value (for multicast only)
239 * 'localport=n' : set the local port
9899efb4 240 * 'pkt_size=n' : set max packet size
34ecc397 241 * 'reuse=1' : enable reusing the socket
2d225591
FB
242 *
243 * @param s1 media file context
244 * @param uri of the remote server
245 * @return zero if no error.
246 */
247int udp_set_remote_url(URLContext *h, const char *uri)
248{
249 UDPContext *s = h->priv_data;
250 char hostname[256];
251 int port;
115329f1 252
6ba5cbc6 253 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
de6d9b64 254
2d225591 255 /* set the destination address */
d05cb726
LA
256 s->dest_addr_len = udp_set_url(&s->dest_addr, hostname, port);
257 if (s->dest_addr_len < 0) {
6f3e0b21 258 return AVERROR(EIO);
d05cb726
LA
259 }
260
2d225591
FB
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 */
269int 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 */
280int 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 */
de6d9b64
FB
287/* return non zero if error */
288static int udp_open(URLContext *h, const char *uri, int flags)
289{
de6d9b64 290 char hostname[1024];
2d225591
FB
291 int port, udp_fd = -1, tmp;
292 UDPContext *s = NULL;
88730be6 293 int is_output;
2d225591
FB
294 const char *p;
295 char buf[256];
88730be6 296#ifndef CONFIG_IPV6
d4936869 297 struct sockaddr_in my_addr;
88730be6
MR
298 int len;
299#endif
de6d9b64
FB
300
301 h->is_streamed = 1;
9899efb4 302 h->max_packet_size = 1472;
de6d9b64 303
2d225591 304 is_output = (flags & URL_WRONLY);
115329f1 305
51844e6c 306 s = av_mallocz(sizeof(UDPContext));
2d225591 307 if (!s)
8fa36ae0 308 return AVERROR(ENOMEM);
2d225591
FB
309
310 h->priv_data = s;
311 s->ttl = 16;
2d225591
FB
312 p = strchr(uri, '?');
313 if (p) {
314 s->is_multicast = find_info_tag(buf, sizeof(buf), "multicast", p);
34ecc397 315 s->reuse_socket = find_info_tag(buf, sizeof(buf), "reuse", p);
2d225591
FB
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 }
9899efb4
MK
322 if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
323 h->max_packet_size = strtol(buf, NULL, 10);
324 }
de6d9b64 325 }
2d225591
FB
326
327 /* fill the dest addr */
6ba5cbc6 328 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
115329f1 329
2d225591
FB
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 }
de6d9b64 338
1642cb6b
RP
339 if(!ff_network_init())
340 return AVERROR(EIO);
341
882d00f2
LA
342 if (s->is_multicast && !(h->flags & URL_WRONLY))
343 s->local_port = port;
7a91333f 344#ifndef CONFIG_IPV6
b5cccea0 345 udp_fd = socket(AF_INET, SOCK_DGRAM, 0);
2d225591
FB
346 if (udp_fd < 0)
347 goto fail;
348
772956c5
FB
349 my_addr.sin_family = AF_INET;
350 my_addr.sin_addr.s_addr = htonl (INADDR_ANY);
882d00f2 351 my_addr.sin_port = htons(s->local_port);
de6d9b64 352
34ecc397
T
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
de6d9b64 357 /* the bind is needed to give a port to the socket now */
115329f1 358 if (bind(udp_fd,(struct sockaddr *)&my_addr, sizeof(my_addr)) < 0)
de6d9b64
FB
359 goto fail;
360
d4936869
LA
361 len = sizeof(my_addr);
362 getsockname(udp_fd, (struct sockaddr *)&my_addr, &len);
363 s->local_port = ntohs(my_addr.sin_port);
7a91333f 364#else
7a91333f
HZ
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};