2 * UDP prototype streaming system
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
5 * This file is part of FFmpeg.
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.
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.
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
25 #ifndef IPV6_ADD_MEMBERSHIP
26 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
27 #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
38 struct sockaddr_in dest_addr;
40 struct sockaddr_storage dest_addr;
45 #define UDP_TX_BUF_SIZE 32768
49 static int udp_ipv6_is_multicast_address(const struct sockaddr *addr) {
50 if (addr->sa_family == AF_INET)
51 return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
52 if (addr->sa_family == AF_INET6)
53 return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
57 static int udp_ipv6_set_multicast_ttl(int sockfd, int mcastTTL, struct sockaddr *addr) {
58 if (addr->sa_family == AF_INET) {
59 if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) {
60 perror("setsockopt(IP_MULTICAST_TTL)");
64 if (addr->sa_family == AF_INET6) {
65 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) {
66 perror("setsockopt(IPV6_MULTICAST_HOPS)");
73 static int udp_ipv6_join_multicast_group(int sockfd, struct sockaddr *addr) {
75 struct ipv6_mreq mreq6;
76 if (addr->sa_family == AF_INET) {
77 mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
78 mreq.imr_interface.s_addr= INADDR_ANY;
79 if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
80 perror("setsockopt(IP_ADD_MEMBERSHIP)");
84 if (addr->sa_family == AF_INET6) {
85 memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
86 mreq6.ipv6mr_interface= 0;
87 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
88 perror("setsockopt(IPV6_ADD_MEMBERSHIP)");
95 static int udp_ipv6_leave_multicast_group(int sockfd, struct sockaddr *addr) {
97 struct ipv6_mreq mreq6;
98 if (addr->sa_family == AF_INET) {
99 mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
100 mreq.imr_interface.s_addr= INADDR_ANY;
101 if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
102 perror("setsockopt(IP_DROP_MEMBERSHIP)");
106 if (addr->sa_family == AF_INET6) {
107 memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
108 mreq6.ipv6mr_interface= 0;
109 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
110 perror("setsockopt(IPV6_DROP_MEMBERSHIP)");
117 static struct addrinfo* udp_ipv6_resolve_host(const char *hostname, int port, int type, int family, int flags) {
118 struct addrinfo hints, *res = 0;
121 const char *node = 0, *service = 0;
124 snprintf(sport, sizeof(sport), "%d", port);
127 if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) {
130 if ((node) || (service)) {
131 memset(&hints, 0, sizeof(hints));
132 hints.ai_socktype = type;
133 hints.ai_family = family;
134 hints.ai_flags = flags;
135 if ((error = getaddrinfo(node, service, &hints, &res))) {
136 av_log(NULL, AV_LOG_ERROR, "udp_ipv6_resolve_host: %s\n", gai_strerror(error));
142 static int udp_ipv6_set_remote_url(URLContext *h, const char *uri) {
143 UDPContext *s = h->priv_data;
146 struct addrinfo *res0;
147 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
148 res0 = udp_ipv6_resolve_host(hostname, port, SOCK_DGRAM, AF_UNSPEC, 0);
149 if (res0 == 0) return AVERROR_IO;
150 memcpy(&s->dest_addr, res0->ai_addr, res0->ai_addrlen);
151 s->dest_addr_len = res0->ai_addrlen;
156 static int udp_ipv6_set_local(URLContext *h) {
157 UDPContext *s = h->priv_data;
159 struct sockaddr_storage clientaddr;
161 char sbuf[NI_MAXSERV];
162 char hbuf[NI_MAXHOST];
163 struct addrinfo *res0 = NULL, *res = NULL;
165 if (s->local_port != 0) {
166 res0 = udp_ipv6_resolve_host(0, s->local_port, SOCK_DGRAM, AF_UNSPEC, AI_PASSIVE);
169 for (res = res0; res; res=res->ai_next) {
170 udp_fd = socket(res->ai_family, SOCK_DGRAM, 0);
171 if (udp_fd > 0) break;
175 udp_fd = socket(s->dest_addr.ss_family, SOCK_DGRAM, 0);
183 if (s->local_port != 0) {
184 if (bind(udp_fd, res0->ai_addr, res0->ai_addrlen) < 0) {
192 addrlen = sizeof(clientaddr);
193 if (getsockname(udp_fd, (struct sockaddr *)&clientaddr, &addrlen) < 0) {
194 perror("getsockname");
198 if (getnameinfo((struct sockaddr *)&clientaddr, addrlen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
199 perror("getnameinfo");
203 s->local_port = strtol(sbuf, NULL, 10);
219 * If no filename is given to av_open_input_file because you want to
220 * get the local port first, then you must call this function to set
221 * the remote server address.
223 * url syntax: udp://host:port[?option=val...]
224 * option: 'multicast=1' : enable multicast
225 * 'ttl=n' : set the ttl value (for multicast only)
226 * 'localport=n' : set the local port
227 * 'pkt_size=n' : set max packet size
228 * 'reuse=1' : enable reusing the socket
230 * @param s1 media file context
231 * @param uri of the remote server
232 * @return zero if no error.
234 int udp_set_remote_url(URLContext *h, const char *uri)
237 return udp_ipv6_set_remote_url(h, uri);
239 UDPContext *s = h->priv_data;
243 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
245 /* set the destination address */
246 if (resolve_host(&s->dest_addr.sin_addr, hostname) < 0)
248 s->dest_addr.sin_family = AF_INET;
249 s->dest_addr.sin_port = htons(port);
255 * Return the local port used by the UDP connexion
256 * @param s1 media file context
257 * @return the local port number
259 int udp_get_local_port(URLContext *h)
261 UDPContext *s = h->priv_data;
262 return s->local_port;
266 * Return the udp file handle for select() usage to wait for several RTP
267 * streams at the same time.
268 * @param h media file context
270 int udp_get_file_handle(URLContext *h)
272 UDPContext *s = h->priv_data;
276 /* put it in UDP context */
277 /* return non zero if error */
278 static int udp_open(URLContext *h, const char *uri, int flags)
281 int port, udp_fd = -1, tmp;
282 UDPContext *s = NULL;
287 struct sockaddr_in my_addr, my_addr1;
292 h->max_packet_size = 1472;
294 is_output = (flags & URL_WRONLY);
296 s = av_malloc(sizeof(UDPContext));
298 return AVERROR(ENOMEM);
305 p = strchr(uri, '?');
307 s->is_multicast = find_info_tag(buf, sizeof(buf), "multicast", p);
308 s->reuse_socket = find_info_tag(buf, sizeof(buf), "reuse", p);
309 if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
310 s->ttl = strtol(buf, NULL, 10);
312 if (find_info_tag(buf, sizeof(buf), "localport", p)) {
313 s->local_port = strtol(buf, NULL, 10);
315 if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
316 h->max_packet_size = strtol(buf, NULL, 10);
320 /* fill the dest addr */
321 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
323 /* XXX: fix url_split */
324 if (hostname[0] == '\0' || hostname[0] == '?') {
325 /* only accepts null hostname if input */
326 if (s->is_multicast || (flags & URL_WRONLY))
329 udp_set_remote_url(h, uri);
333 udp_fd = socket(AF_INET, SOCK_DGRAM, 0);
337 my_addr.sin_family = AF_INET;
338 my_addr.sin_addr.s_addr = htonl (INADDR_ANY);
339 if (s->is_multicast && !(h->flags & URL_WRONLY)) {
340 /* special case: the bind must be done on the multicast address port */
341 my_addr.sin_port = s->dest_addr.sin_port;
343 my_addr.sin_port = htons(s->local_port);
347 if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
350 /* the bind is needed to give a port to the socket now */
351 if (bind(udp_fd,(struct sockaddr *)&my_addr, sizeof(my_addr)) < 0)
354 len = sizeof(my_addr1);
355 getsockname(udp_fd, (struct sockaddr *)&my_addr1, &len);
356 s->local_port = ntohs(my_addr1.sin_port);
358 #ifdef IP_MULTICAST_TTL
359 if (s->is_multicast) {
360 if (h->flags & URL_WRONLY) {
362 if (setsockopt(udp_fd, IPPROTO_IP, IP_MULTICAST_TTL,
363 &s->ttl, sizeof(s->ttl)) < 0) {
364 perror("IP_MULTICAST_TTL");
369 memset(&s->mreq, 0, sizeof(s->mreq));
370 s->mreq.imr_multiaddr = s->dest_addr.sin_addr;
371 s->mreq.imr_interface.s_addr = htonl (INADDR_ANY);
372 if (setsockopt(udp_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
373 &s->mreq, sizeof(s->mreq)) < 0) {
374 perror("rtp: IP_ADD_MEMBERSHIP");
381 if (s->is_multicast && !(h->flags & URL_WRONLY))
382 s->local_port = port;
383 udp_fd = udp_ipv6_set_local(h);
386 if (s->is_multicast) {
387 if (h->flags & URL_WRONLY) {
388 if (udp_ipv6_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
391 if (udp_ipv6_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0)
398 /* limit the tx buf size to limit latency */
399 tmp = UDP_TX_BUF_SIZE;
400 if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
401 perror("setsockopt sndbuf");
415 static int udp_read(URLContext *h, uint8_t *buf, int size)
417 UDPContext *s = h->priv_data;
419 struct sockaddr_in from;
421 struct sockaddr_storage from;
427 from_len = sizeof(from);
428 len = recvfrom (s->udp_fd, buf, size, 0,
429 (struct sockaddr *)&from, &from_len);
431 if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
432 ff_neterrno() != FF_NETERROR(EINTR))
441 static int udp_write(URLContext *h, uint8_t *buf, int size)
443 UDPContext *s = h->priv_data;
447 ret = sendto (s->udp_fd, buf, size, 0,
448 (struct sockaddr *) &s->dest_addr,
450 sizeof (s->dest_addr));
455 if (ff_neterrno() != FF_NETERROR(EINTR) &&
456 ff_neterrno() != FF_NETERROR(EAGAIN))
465 static int udp_close(URLContext *h)
467 UDPContext *s = h->priv_data;
470 #ifdef IP_DROP_MEMBERSHIP
471 if (s->is_multicast && !(h->flags & URL_WRONLY)) {
472 if (setsockopt(s->udp_fd, IPPROTO_IP, IP_DROP_MEMBERSHIP,
473 &s->mreq, sizeof(s->mreq)) < 0) {
474 perror("IP_DROP_MEMBERSHIP");
479 if (s->is_multicast && !(h->flags & URL_WRONLY))
480 udp_ipv6_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
482 closesocket(s->udp_fd);
487 URLProtocol udp_protocol = {