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
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
29 #ifndef IPV6_ADD_MEMBERSHIP
30 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
31 #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
42 struct sockaddr_in dest_addr;
44 struct sockaddr_storage dest_addr;
49 #define UDP_TX_BUF_SIZE 32768
53 static int udp_ipv6_is_multicast_address(const struct sockaddr *addr) {
54 if (addr->sa_family == AF_INET)
55 return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
56 if (addr->sa_family == AF_INET6)
57 return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
61 static int udp_ipv6_set_multicast_ttl(int sockfd, int mcastTTL, struct sockaddr *addr) {
62 if (addr->sa_family == AF_INET) {
63 if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) {
64 perror("setsockopt(IP_MULTICAST_TTL)");
68 if (addr->sa_family == AF_INET6) {
69 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) {
70 perror("setsockopt(IPV6_MULTICAST_HOPS)");
77 static int udp_ipv6_join_multicast_group(int sockfd, struct sockaddr *addr) {
79 struct ipv6_mreq mreq6;
80 if (addr->sa_family == AF_INET) {
81 mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
82 mreq.imr_interface.s_addr= INADDR_ANY;
83 if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
84 perror("setsockopt(IP_ADD_MEMBERSHIP)");
88 if (addr->sa_family == AF_INET6) {
89 memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
90 mreq6.ipv6mr_interface= 0;
91 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
92 perror("setsockopt(IPV6_ADD_MEMBERSHIP)");
99 static int udp_ipv6_leave_multicast_group(int sockfd, struct sockaddr *addr) {
101 struct ipv6_mreq mreq6;
102 if (addr->sa_family == AF_INET) {
103 mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
104 mreq.imr_interface.s_addr= INADDR_ANY;
105 if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
106 perror("setsockopt(IP_DROP_MEMBERSHIP)");
110 if (addr->sa_family == AF_INET6) {
111 memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
112 mreq6.ipv6mr_interface= 0;
113 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
114 perror("setsockopt(IPV6_DROP_MEMBERSHIP)");
121 static struct addrinfo* udp_ipv6_resolve_host(const char *hostname, int port, int type, int family, int flags) {
122 struct addrinfo hints, *res = 0;
125 const char *node = 0, *service = 0;
128 snprintf(sport, sizeof(sport), "%d", port);
131 if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) {
134 if ((node) || (service)) {
135 memset(&hints, 0, sizeof(hints));
136 hints.ai_socktype = type;
137 hints.ai_family = family;
138 hints.ai_flags = flags;
139 if ((error = getaddrinfo(node, service, &hints, &res))) {
140 av_log(NULL, AV_LOG_ERROR, "udp_ipv6_resolve_host: %s\n", gai_strerror(error));
146 static int udp_ipv6_set_remote_url(URLContext *h, const char *uri) {
147 UDPContext *s = h->priv_data;
150 struct addrinfo *res0;
151 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
152 res0 = udp_ipv6_resolve_host(hostname, port, SOCK_DGRAM, AF_UNSPEC, 0);
153 if (res0 == 0) return AVERROR_IO;
154 memcpy(&s->dest_addr, res0->ai_addr, res0->ai_addrlen);
155 s->dest_addr_len = res0->ai_addrlen;
160 static int udp_ipv6_set_local(URLContext *h) {
161 UDPContext *s = h->priv_data;
163 struct sockaddr_storage clientaddr;
165 char sbuf[NI_MAXSERV];
166 char hbuf[NI_MAXHOST];
167 struct addrinfo *res0 = NULL, *res = NULL;
169 if (s->local_port != 0) {
170 res0 = udp_ipv6_resolve_host(0, s->local_port, SOCK_DGRAM, AF_UNSPEC, AI_PASSIVE);
173 for (res = res0; res; res=res->ai_next) {
174 udp_fd = socket(res->ai_family, SOCK_DGRAM, 0);
175 if (udp_fd > 0) break;
179 udp_fd = socket(s->dest_addr.ss_family, SOCK_DGRAM, 0);
187 if (s->local_port != 0) {
188 if (bind(udp_fd, res0->ai_addr, res0->ai_addrlen) < 0) {
196 addrlen = sizeof(clientaddr);
197 if (getsockname(udp_fd, (struct sockaddr *)&clientaddr, &addrlen) < 0) {
198 perror("getsockname");
202 if (getnameinfo((struct sockaddr *)&clientaddr, addrlen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
203 perror("getnameinfo");
207 s->local_port = strtol(sbuf, NULL, 10);
223 * If no filename is given to av_open_input_file because you want to
224 * get the local port first, then you must call this function to set
225 * the remote server address.
227 * url syntax: udp://host:port[?option=val...]
228 * option: 'multicast=1' : enable multicast
229 * 'ttl=n' : set the ttl value (for multicast only)
230 * 'localport=n' : set the local port
231 * 'pkt_size=n' : set max packet size
232 * 'reuse=1' : enable reusing the socket
234 * @param s1 media file context
235 * @param uri of the remote server
236 * @return zero if no error.
238 int udp_set_remote_url(URLContext *h, const char *uri)
241 return udp_ipv6_set_remote_url(h, uri);
243 UDPContext *s = h->priv_data;
247 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
249 /* set the destination address */
250 if (resolve_host(&s->dest_addr.sin_addr, hostname) < 0)
252 s->dest_addr.sin_family = AF_INET;
253 s->dest_addr.sin_port = htons(port);
259 * Return the local port used by the UDP connexion
260 * @param s1 media file context
261 * @return the local port number
263 int udp_get_local_port(URLContext *h)
265 UDPContext *s = h->priv_data;
266 return s->local_port;
270 * Return the udp file handle for select() usage to wait for several RTP
271 * streams at the same time.
272 * @param h media file context
274 int udp_get_file_handle(URLContext *h)
276 UDPContext *s = h->priv_data;
280 /* put it in UDP context */
281 /* return non zero if error */
282 static int udp_open(URLContext *h, const char *uri, int flags)
285 int port, udp_fd = -1, tmp;
286 UDPContext *s = NULL;
291 struct sockaddr_in my_addr, my_addr1;
296 h->max_packet_size = 1472;
298 is_output = (flags & URL_WRONLY);
300 s = av_malloc(sizeof(UDPContext));
309 p = strchr(uri, '?');
311 s->is_multicast = find_info_tag(buf, sizeof(buf), "multicast", p);
312 s->reuse_socket = find_info_tag(buf, sizeof(buf), "reuse", p);
313 if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
314 s->ttl = strtol(buf, NULL, 10);
316 if (find_info_tag(buf, sizeof(buf), "localport", p)) {
317 s->local_port = strtol(buf, NULL, 10);
319 if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
320 h->max_packet_size = strtol(buf, NULL, 10);
324 /* fill the dest addr */
325 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
327 /* XXX: fix url_split */
328 if (hostname[0] == '\0' || hostname[0] == '?') {
329 /* only accepts null hostname if input */
330 if (s->is_multicast || (flags & URL_WRONLY))
333 udp_set_remote_url(h, uri);
337 udp_fd = socket(PF_INET, SOCK_DGRAM, 0);
341 my_addr.sin_family = AF_INET;
342 my_addr.sin_addr.s_addr = htonl (INADDR_ANY);
343 if (s->is_multicast && !(h->flags & URL_WRONLY)) {
344 /* special case: the bind must be done on the multicast address port */
345 my_addr.sin_port = s->dest_addr.sin_port;
347 my_addr.sin_port = htons(s->local_port);
351 if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
354 /* the bind is needed to give a port to the socket now */
355 if (bind(udp_fd,(struct sockaddr *)&my_addr, sizeof(my_addr)) < 0)
358 len = sizeof(my_addr1);
359 getsockname(udp_fd, (struct sockaddr *)&my_addr1, &len);
360 s->local_port = ntohs(my_addr1.sin_port);
362 #ifdef IP_MULTICAST_TTL
363 if (s->is_multicast) {
364 if (h->flags & URL_WRONLY) {
366 if (setsockopt(udp_fd, IPPROTO_IP, IP_MULTICAST_TTL,
367 &s->ttl, sizeof(s->ttl)) < 0) {
368 perror("IP_MULTICAST_TTL");
373 memset(&s->mreq, 0, sizeof(s->mreq));
374 s->mreq.imr_multiaddr = s->dest_addr.sin_addr;
375 s->mreq.imr_interface.s_addr = htonl (INADDR_ANY);
376 if (setsockopt(udp_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
377 &s->mreq, sizeof(s->mreq)) < 0) {
378 perror("rtp: IP_ADD_MEMBERSHIP");
385 if (s->is_multicast && !(h->flags & URL_WRONLY))
386 s->local_port = port;
387 udp_fd = udp_ipv6_set_local(h);
390 if (s->is_multicast) {
391 if (h->flags & URL_WRONLY) {
392 if (udp_ipv6_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
395 if (udp_ipv6_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0)
402 /* limit the tx buf size to limit latency */
403 tmp = UDP_TX_BUF_SIZE;
404 if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
405 perror("setsockopt sndbuf");
419 static int udp_read(URLContext *h, uint8_t *buf, int size)
421 UDPContext *s = h->priv_data;
423 struct sockaddr_in from;
425 struct sockaddr_storage from;
431 from_len = sizeof(from);
432 len = recvfrom (s->udp_fd, buf, size, 0,
433 (struct sockaddr *)&from, &from_len);
435 if (errno != EAGAIN && errno != EINTR)
444 static int udp_write(URLContext *h, uint8_t *buf, int size)
446 UDPContext *s = h->priv_data;
450 ret = sendto (s->udp_fd, buf, size, 0,
451 (struct sockaddr *) &s->dest_addr,
453 sizeof (s->dest_addr));
458 if (errno != EINTR && errno != EAGAIN)
467 static int udp_close(URLContext *h)
469 UDPContext *s = h->priv_data;
472 #ifdef IP_DROP_MEMBERSHIP
473 if (s->is_multicast && !(h->flags & URL_WRONLY)) {
474 if (setsockopt(s->udp_fd, IPPROTO_IP, IP_DROP_MEMBERSHIP,
475 &s->mreq, sizeof(s->mreq)) < 0) {
476 perror("IP_DROP_MEMBERSHIP");
481 if (s->is_multicast && !(h->flags & URL_WRONLY))
482 udp_ipv6_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
484 closesocket(s->udp_fd);
489 URLProtocol udp_protocol = {