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>
27 # include <arpa/inet.h>
29 # include "barpainet.h"
33 #ifndef IPV6_ADD_MEMBERSHIP
34 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
35 #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
45 struct sockaddr_in dest_addr;
47 struct sockaddr_storage dest_addr;
52 #define UDP_TX_BUF_SIZE 32768
56 static int udp_ipv6_is_multicast_address(const struct sockaddr *addr) {
57 if (addr->sa_family == AF_INET)
58 return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
59 if (addr->sa_family == AF_INET6)
60 return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
64 static int udp_ipv6_set_multicast_ttl(int sockfd, int mcastTTL, struct sockaddr *addr) {
65 if (addr->sa_family == AF_INET) {
66 if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) {
67 perror("setsockopt(IP_MULTICAST_TTL)");
71 if (addr->sa_family == AF_INET6) {
72 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) {
73 perror("setsockopt(IPV6_MULTICAST_HOPS)");
80 static int udp_ipv6_join_multicast_group(int sockfd, struct sockaddr *addr) {
82 struct ipv6_mreq mreq6;
83 if (addr->sa_family == AF_INET) {
84 mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
85 mreq.imr_interface.s_addr= INADDR_ANY;
86 if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
87 perror("setsockopt(IP_ADD_MEMBERSHIP)");
91 if (addr->sa_family == AF_INET6) {
92 memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
93 mreq6.ipv6mr_interface= 0;
94 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
95 perror("setsockopt(IPV6_ADD_MEMBERSHIP)");
102 static int udp_ipv6_leave_multicast_group(int sockfd, struct sockaddr *addr) {
104 struct ipv6_mreq mreq6;
105 if (addr->sa_family == AF_INET) {
106 mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
107 mreq.imr_interface.s_addr= INADDR_ANY;
108 if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
109 perror("setsockopt(IP_DROP_MEMBERSHIP)");
113 if (addr->sa_family == AF_INET6) {
114 memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
115 mreq6.ipv6mr_interface= 0;
116 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
117 perror("setsockopt(IPV6_DROP_MEMBERSHIP)");
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;
128 const char *node = 0, *service = 0;
131 snprintf(sport, sizeof(sport), "%d", port);
134 if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) {
137 if ((node) || (service)) {
138 memset(&hints, 0, sizeof(hints));
139 hints.ai_socktype = type;
140 hints.ai_family = family;
141 hints.ai_flags = flags;
142 if ((error = getaddrinfo(node, service, &hints, &res))) {
143 av_log(NULL, AV_LOG_ERROR, "udp_ipv6_resolve_host: %s\n", gai_strerror(error));
149 static int udp_ipv6_set_remote_url(URLContext *h, const char *uri) {
150 UDPContext *s = h->priv_data;
153 struct addrinfo *res0;
154 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
155 res0 = udp_ipv6_resolve_host(hostname, port, SOCK_DGRAM, AF_UNSPEC, 0);
156 if (res0 == 0) return AVERROR_IO;
157 memcpy(&s->dest_addr, res0->ai_addr, res0->ai_addrlen);
158 s->dest_addr_len = res0->ai_addrlen;
163 static int udp_ipv6_set_local(URLContext *h) {
164 UDPContext *s = h->priv_data;
166 struct sockaddr_storage clientaddr;
168 char sbuf[NI_MAXSERV];
169 char hbuf[NI_MAXHOST];
170 struct addrinfo *res0 = NULL, *res = NULL;
172 if (s->local_port != 0) {
173 res0 = udp_ipv6_resolve_host(0, s->local_port, SOCK_DGRAM, AF_UNSPEC, AI_PASSIVE);
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;
182 udp_fd = socket(s->dest_addr.ss_family, SOCK_DGRAM, 0);
190 if (s->local_port != 0) {
191 if (bind(udp_fd, res0->ai_addr, res0->ai_addrlen) < 0) {
199 addrlen = sizeof(clientaddr);
200 if (getsockname(udp_fd, (struct sockaddr *)&clientaddr, &addrlen) < 0) {
201 perror("getsockname");
205 if (getnameinfo((struct sockaddr *)&clientaddr, addrlen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
206 perror("getnameinfo");
210 s->local_port = strtol(sbuf, NULL, 10);
216 #ifdef CONFIG_BEOS_NETSERVER
230 * If no filename is given to av_open_input_file because you want to
231 * get the local port first, then you must call this function to set
232 * the remote server address.
234 * url syntax: udp://host:port[?option=val...]
235 * option: 'multicast=1' : enable multicast
236 * 'ttl=n' : set the ttl value (for multicast only)
237 * 'localport=n' : set the local port
238 * 'pkt_size=n' : set max packet size
240 * @param s1 media file context
241 * @param uri of the remote server
242 * @return zero if no error.
244 int udp_set_remote_url(URLContext *h, const char *uri)
247 return udp_ipv6_set_remote_url(h, uri);
249 UDPContext *s = h->priv_data;
253 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
255 /* set the destination address */
256 if (resolve_host(&s->dest_addr.sin_addr, hostname) < 0)
258 s->dest_addr.sin_family = AF_INET;
259 s->dest_addr.sin_port = htons(port);
265 * Return the local port used by the UDP connexion
266 * @param s1 media file context
267 * @return the local port number
269 int udp_get_local_port(URLContext *h)
271 UDPContext *s = h->priv_data;
272 return s->local_port;
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
280 int udp_get_file_handle(URLContext *h)
282 UDPContext *s = h->priv_data;
286 /* put it in UDP context */
287 /* return non zero if error */
288 static int udp_open(URLContext *h, const char *uri, int flags)
291 int port, udp_fd = -1, tmp;
292 UDPContext *s = NULL;
297 struct sockaddr_in my_addr, my_addr1;
302 h->max_packet_size = 1472;
304 is_output = (flags & URL_WRONLY);
306 s = av_malloc(sizeof(UDPContext));
314 p = strchr(uri, '?');
316 s->is_multicast = find_info_tag(buf, sizeof(buf), "multicast", p);
317 if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
318 s->ttl = strtol(buf, NULL, 10);
320 if (find_info_tag(buf, sizeof(buf), "localport", p)) {
321 s->local_port = strtol(buf, NULL, 10);
323 if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
324 h->max_packet_size = strtol(buf, NULL, 10);
328 /* fill the dest addr */
329 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
331 /* XXX: fix url_split */
332 if (hostname[0] == '\0' || hostname[0] == '?') {
333 /* only accepts null hostname if input */
334 if (s->is_multicast || (flags & URL_WRONLY))
337 udp_set_remote_url(h, uri);
341 udp_fd = socket(PF_INET, SOCK_DGRAM, 0);
345 my_addr.sin_family = AF_INET;
346 my_addr.sin_addr.s_addr = htonl (INADDR_ANY);
347 if (s->is_multicast && !(h->flags & URL_WRONLY)) {
348 /* special case: the bind must be done on the multicast address port */
349 my_addr.sin_port = s->dest_addr.sin_port;
351 my_addr.sin_port = htons(s->local_port);
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 #ifndef CONFIG_BEOS_NETSERVER
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 #ifndef CONFIG_BEOS_NETSERVER
391 if (s->is_multicast) {
392 if (h->flags & URL_WRONLY) {
393 if (udp_ipv6_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
396 if (udp_ipv6_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0)
404 /* limit the tx buf size to limit latency */
405 tmp = UDP_TX_BUF_SIZE;
406 if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
407 perror("setsockopt sndbuf");
416 #ifdef CONFIG_BEOS_NETSERVER
425 static int udp_read(URLContext *h, uint8_t *buf, int size)
427 UDPContext *s = h->priv_data;
429 struct sockaddr_in from;
431 struct sockaddr_storage from;
437 from_len = sizeof(from);
438 len = recvfrom (s->udp_fd, buf, size, 0,
439 (struct sockaddr *)&from, &from_len);
441 if (errno != EAGAIN && errno != EINTR)
450 static int udp_write(URLContext *h, uint8_t *buf, int size)
452 UDPContext *s = h->priv_data;
456 ret = sendto (s->udp_fd, buf, size, 0,
457 (struct sockaddr *) &s->dest_addr,
459 sizeof (s->dest_addr));
464 if (errno != EINTR && errno != EAGAIN)
473 static int udp_close(URLContext *h)
475 UDPContext *s = h->priv_data;
477 #ifndef CONFIG_BEOS_NETSERVER
479 if (s->is_multicast && !(h->flags & URL_WRONLY)) {
480 if (setsockopt(s->udp_fd, IPPROTO_IP, IP_DROP_MEMBERSHIP,
481 &s->mreq, sizeof(s->mreq)) < 0) {
482 perror("IP_DROP_MEMBERSHIP");
486 if (s->is_multicast && !(h->flags & URL_WRONLY))
487 udp_ipv6_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
491 closesocket(s->udp_fd);
497 URLProtocol udp_protocol = {