2 * UDP prototype streaming system
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
25 # include <arpa/inet.h>
27 # include "barpainet.h"
31 #ifndef IPV6_ADD_MEMBERSHIP
32 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
33 #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
43 struct sockaddr_in dest_addr;
45 struct sockaddr_storage dest_addr;
50 #define UDP_TX_BUF_SIZE 32768
54 int udp_ipv6_is_multicast_address(const struct sockaddr *addr) {
55 if (addr->sa_family == AF_INET)
56 return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
57 if (addr->sa_family == AF_INET6)
58 return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
62 int udp_ipv6_set_multicast_ttl(int sockfd, int mcastTTL, struct sockaddr *addr) {
63 if (addr->sa_family == AF_INET) {
64 if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) {
65 perror("setsockopt(IP_MULTICAST_TTL)");
69 if (addr->sa_family == AF_INET6) {
70 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) {
71 perror("setsockopt(IPV6_MULTICAST_HOPS)");
78 int udp_ipv6_join_multicast_group(int sockfd, struct sockaddr *addr) {
80 struct ipv6_mreq mreq6;
81 if (addr->sa_family == AF_INET) {
82 mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
83 mreq.imr_interface.s_addr= INADDR_ANY;
84 if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
85 perror("setsockopt(IP_ADD_MEMBERSHIP)");
89 if (addr->sa_family == AF_INET6) {
90 memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
91 mreq6.ipv6mr_interface= 0;
92 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
93 perror("setsockopt(IPV6_ADD_MEMBERSHIP)");
100 int udp_ipv6_leave_multicast_group(int sockfd, struct sockaddr *addr) {
102 struct ipv6_mreq mreq6;
103 if (addr->sa_family == AF_INET) {
104 mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
105 mreq.imr_interface.s_addr= INADDR_ANY;
106 if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
107 perror("setsockopt(IP_DROP_MEMBERSHIP)");
111 if (addr->sa_family == AF_INET6) {
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)");
122 struct addrinfo* udp_ipv6_resolve_host(const char *hostname, int port, int type, int family, int flags) {
123 struct addrinfo hints, *res = 0;
126 const char *node = 0, *service = 0;
129 snprintf(sport, sizeof(sport), "%d", port);
132 if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) {
135 if ((node) || (service)) {
136 memset(&hints, 0, sizeof(hints));
137 hints.ai_socktype = type;
138 hints.ai_family = family;
139 hints.ai_flags = flags;
140 if ((error = getaddrinfo(node, service, &hints, &res))) {
141 av_log(NULL, AV_LOG_ERROR, "udp_ipv6_resolve_host: %s\n", gai_strerror(error));
147 int udp_ipv6_set_remote_url(URLContext *h, const char *uri) {
148 UDPContext *s = h->priv_data;
151 struct addrinfo *res0;
152 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
153 res0 = udp_ipv6_resolve_host(hostname, port, SOCK_DGRAM, AF_UNSPEC, 0);
154 if (res0 == 0) return AVERROR_IO;
155 memcpy(&s->dest_addr, res0->ai_addr, res0->ai_addrlen);
156 s->dest_addr_len = res0->ai_addrlen;
161 int udp_ipv6_set_local(URLContext *h) {
162 UDPContext *s = h->priv_data;
164 struct sockaddr_storage clientaddr;
166 char sbuf[NI_MAXSERV];
167 char hbuf[NI_MAXHOST];
168 struct addrinfo *res0 = NULL, *res = NULL;
170 if (s->local_port != 0) {
171 res0 = udp_ipv6_resolve_host(0, s->local_port, SOCK_DGRAM, AF_UNSPEC, AI_PASSIVE);
174 for (res = res0; res; res=res->ai_next) {
175 udp_fd = socket(res->ai_family, SOCK_DGRAM, 0);
176 if (udp_fd > 0) break;
180 udp_fd = socket(s->dest_addr.ss_family, SOCK_DGRAM, 0);
188 if (s->local_port != 0) {
189 if (bind(udp_fd, res0->ai_addr, res0->ai_addrlen) < 0) {
197 addrlen = sizeof(clientaddr);
198 if (getsockname(udp_fd, (struct sockaddr *)&clientaddr, &addrlen) < 0) {
199 perror("getsockname");
203 if (getnameinfo((struct sockaddr *)&clientaddr, addrlen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
204 perror("getnameinfo");
208 s->local_port = strtol(sbuf, NULL, 10);
214 #ifdef CONFIG_BEOS_NETSERVER
228 * If no filename is given to av_open_input_file because you want to
229 * get the local port first, then you must call this function to set
230 * the remote server address.
232 * url syntax: udp://host:port[?option=val...]
233 * option: 'multicast=1' : enable multicast
234 * 'ttl=n' : set the ttl value (for multicast only)
235 * 'localport=n' : set the local port
236 * 'pkt_size=n' : set max packet size
238 * @param s1 media file context
239 * @param uri of the remote server
240 * @return zero if no error.
242 int udp_set_remote_url(URLContext *h, const char *uri)
245 return udp_ipv6_set_remote_url(h, uri);
247 UDPContext *s = h->priv_data;
251 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
253 /* set the destination address */
254 if (resolve_host(&s->dest_addr.sin_addr, hostname) < 0)
256 s->dest_addr.sin_family = AF_INET;
257 s->dest_addr.sin_port = htons(port);
263 * Return the local port used by the UDP connexion
264 * @param s1 media file context
265 * @return the local port number
267 int udp_get_local_port(URLContext *h)
269 UDPContext *s = h->priv_data;
270 return s->local_port;
274 * Return the udp file handle for select() usage to wait for several RTP
275 * streams at the same time.
276 * @param h media file context
278 int udp_get_file_handle(URLContext *h)
280 UDPContext *s = h->priv_data;
284 /* put it in UDP context */
285 /* return non zero if error */
286 static int udp_open(URLContext *h, const char *uri, int flags)
289 int port, udp_fd = -1, tmp;
290 UDPContext *s = NULL;
295 struct sockaddr_in my_addr, my_addr1;
300 h->max_packet_size = 1472;
302 is_output = (flags & URL_WRONLY);
304 s = av_malloc(sizeof(UDPContext));
312 p = strchr(uri, '?');
314 s->is_multicast = find_info_tag(buf, sizeof(buf), "multicast", p);
315 if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
316 s->ttl = strtol(buf, NULL, 10);
318 if (find_info_tag(buf, sizeof(buf), "localport", p)) {
319 s->local_port = strtol(buf, NULL, 10);
321 if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
322 h->max_packet_size = strtol(buf, NULL, 10);
326 /* fill the dest addr */
327 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
329 /* XXX: fix url_split */
330 if (hostname[0] == '\0' || hostname[0] == '?') {
331 /* only accepts null hostname if input */
332 if (s->is_multicast || (flags & URL_WRONLY))
335 udp_set_remote_url(h, uri);
339 udp_fd = socket(PF_INET, SOCK_DGRAM, 0);
343 my_addr.sin_family = AF_INET;
344 my_addr.sin_addr.s_addr = htonl (INADDR_ANY);
345 if (s->is_multicast && !(h->flags & URL_WRONLY)) {
346 /* special case: the bind must be done on the multicast address port */
347 my_addr.sin_port = s->dest_addr.sin_port;
349 my_addr.sin_port = htons(s->local_port);
352 /* the bind is needed to give a port to the socket now */
353 if (bind(udp_fd,(struct sockaddr *)&my_addr, sizeof(my_addr)) < 0)
356 len = sizeof(my_addr1);
357 getsockname(udp_fd, (struct sockaddr *)&my_addr1, &len);
358 s->local_port = ntohs(my_addr1.sin_port);
360 #ifndef CONFIG_BEOS_NETSERVER
361 if (s->is_multicast) {
362 if (h->flags & URL_WRONLY) {
364 if (setsockopt(udp_fd, IPPROTO_IP, IP_MULTICAST_TTL,
365 &s->ttl, sizeof(s->ttl)) < 0) {
366 perror("IP_MULTICAST_TTL");
371 memset(&s->mreq, 0, sizeof(s->mreq));
372 s->mreq.imr_multiaddr = s->dest_addr.sin_addr;
373 s->mreq.imr_interface.s_addr = htonl (INADDR_ANY);
374 if (setsockopt(udp_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
375 &s->mreq, sizeof(s->mreq)) < 0) {
376 perror("rtp: IP_ADD_MEMBERSHIP");
383 if (s->is_multicast && !(h->flags & URL_WRONLY))
384 s->local_port = port;
385 udp_fd = udp_ipv6_set_local(h);
388 #ifndef CONFIG_BEOS_NETSERVER
389 if (s->is_multicast) {
390 if (h->flags & URL_WRONLY) {
391 if (udp_ipv6_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
394 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");
414 #ifdef CONFIG_BEOS_NETSERVER
423 static int udp_read(URLContext *h, uint8_t *buf, int size)
425 UDPContext *s = h->priv_data;
427 struct sockaddr_in from;
429 struct sockaddr_storage from;
434 from_len = sizeof(from);
435 len = recvfrom (s->udp_fd, buf, size, 0,
436 (struct sockaddr *)&from, &from_len);
438 if (errno != EAGAIN && errno != EINTR)
447 static int udp_write(URLContext *h, uint8_t *buf, int size)
449 UDPContext *s = h->priv_data;
453 ret = sendto (s->udp_fd, buf, size, 0,
454 (struct sockaddr *) &s->dest_addr,
456 sizeof (s->dest_addr));
461 if (errno != EINTR && errno != EAGAIN)
470 static int udp_close(URLContext *h)
472 UDPContext *s = h->priv_data;
474 #ifndef CONFIG_BEOS_NETSERVER
476 if (s->is_multicast && !(h->flags & URL_WRONLY)) {
477 if (setsockopt(s->udp_fd, IPPROTO_IP, IP_DROP_MEMBERSHIP,
478 &s->mreq, sizeof(s->mreq)) < 0) {
479 perror("IP_DROP_MEMBERSHIP");
483 if (s->is_multicast && !(h->flags & URL_WRONLY))
484 udp_ipv6_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
488 closesocket(s->udp_fd);
494 URLProtocol udp_protocol = {