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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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"
38 struct sockaddr_in dest_addr;
40 struct sockaddr_storage dest_addr;
45 #define UDP_TX_BUF_SIZE 32768
49 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 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 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 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 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 sprintf(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 fprintf(stderr, "udp_ipv6_resolve_host: %s\n", gai_strerror(error));
142 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 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;
166 if (s->local_port != 0) {
167 res0 = udp_ipv6_resolve_host(0, s->local_port, SOCK_DGRAM, AF_UNSPEC, AI_PASSIVE);
168 if (res0 == 0) return -1;
169 family = res0->ai_family;
172 family = s->dest_addr.ss_family;
175 udp_fd = socket(family, SOCK_DGRAM, 0);
181 if (s->local_port != 0) {
182 if (bind(udp_fd, res0->ai_addr, res0->ai_addrlen) < 0) {
188 addrlen = sizeof(clientaddr);
189 if (getsockname(udp_fd, (struct sockaddr *)&clientaddr, &addrlen) < 0) {
190 perror("getsockname");
194 if (getnameinfo((struct sockaddr *)&clientaddr, addrlen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
195 perror("getnameinfo");
199 s->local_port = strtol(sbuf, NULL, 10);
205 #ifdef CONFIG_BEOS_NETSERVER
217 * If no filename is given to av_open_input_file because you want to
218 * get the local port first, then you must call this function to set
219 * the remote server address.
221 * url syntax: udp://host:port[?option=val...]
222 * option: 'multicast=1' : enable multicast
223 * 'ttl=n' : set the ttl value (for multicast only)
224 * 'localport=n' : set the local port
225 * 'pkt_size=n' : set max packet size
227 * @param s1 media file context
228 * @param uri of the remote server
229 * @return zero if no error.
231 int udp_set_remote_url(URLContext *h, const char *uri)
234 return udp_ipv6_set_remote_url(h, uri);
236 UDPContext *s = h->priv_data;
240 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
242 /* set the destination address */
243 if (resolve_host(&s->dest_addr.sin_addr, hostname) < 0)
245 s->dest_addr.sin_family = AF_INET;
246 s->dest_addr.sin_port = htons(port);
252 * Return the local port used by the UDP connexion
253 * @param s1 media file context
254 * @return the local port number
256 int udp_get_local_port(URLContext *h)
258 UDPContext *s = h->priv_data;
259 return s->local_port;
263 * Return the udp file handle for select() usage to wait for several RTP
264 * streams at the same time.
265 * @param h media file context
267 int udp_get_file_handle(URLContext *h)
269 UDPContext *s = h->priv_data;
273 /* put it in UDP context */
274 /* return non zero if error */
275 static int udp_open(URLContext *h, const char *uri, int flags)
277 struct sockaddr_in my_addr, my_addr1;
279 int port, udp_fd = -1, tmp;
280 UDPContext *s = NULL;
286 h->max_packet_size = 1472;
288 is_output = (flags & URL_WRONLY);
290 s = av_malloc(sizeof(UDPContext));
298 p = strchr(uri, '?');
300 s->is_multicast = find_info_tag(buf, sizeof(buf), "multicast", p);
301 if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
302 s->ttl = strtol(buf, NULL, 10);
304 if (find_info_tag(buf, sizeof(buf), "localport", p)) {
305 s->local_port = strtol(buf, NULL, 10);
307 if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
308 h->max_packet_size = strtol(buf, NULL, 10);
312 /* fill the dest addr */
313 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
315 /* XXX: fix url_split */
316 if (hostname[0] == '\0' || hostname[0] == '?') {
317 /* only accepts null hostname if input */
318 if (s->is_multicast || (flags & URL_WRONLY))
321 udp_set_remote_url(h, uri);
325 udp_fd = socket(PF_INET, SOCK_DGRAM, 0);
329 my_addr.sin_family = AF_INET;
330 my_addr.sin_addr.s_addr = htonl (INADDR_ANY);
331 if (s->is_multicast && !(h->flags & URL_WRONLY)) {
332 /* special case: the bind must be done on the multicast address port */
333 my_addr.sin_port = s->dest_addr.sin_port;
335 my_addr.sin_port = htons(s->local_port);
338 /* the bind is needed to give a port to the socket now */
339 if (bind(udp_fd,(struct sockaddr *)&my_addr, sizeof(my_addr)) < 0)
342 len = sizeof(my_addr1);
343 getsockname(udp_fd, (struct sockaddr *)&my_addr1, &len);
344 s->local_port = ntohs(my_addr1.sin_port);
346 #ifndef CONFIG_BEOS_NETSERVER
347 if (s->is_multicast) {
348 if (h->flags & URL_WRONLY) {
350 if (setsockopt(udp_fd, IPPROTO_IP, IP_MULTICAST_TTL,
351 &s->ttl, sizeof(s->ttl)) < 0) {
352 perror("IP_MULTICAST_TTL");
357 memset(&s->mreq, 0, sizeof(s->mreq));
358 s->mreq.imr_multiaddr = s->dest_addr.sin_addr;
359 s->mreq.imr_interface.s_addr = htonl (INADDR_ANY);
360 if (setsockopt(udp_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
361 &s->mreq, sizeof(s->mreq)) < 0) {
362 perror("rtp: IP_ADD_MEMBERSHIP");
369 if (s->is_multicast && !(h->flags & URL_WRONLY))
370 s->local_port = port;
371 udp_fd = udp_ipv6_set_local(h);
374 #ifndef CONFIG_BEOS_NETSERVER
375 if (s->is_multicast) {
376 if (h->flags & URL_WRONLY) {
377 if (udp_ipv6_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
380 if (udp_ipv6_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0)
388 /* limit the tx buf size to limit latency */
389 tmp = UDP_TX_BUF_SIZE;
390 if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
391 perror("setsockopt sndbuf");
400 #ifdef CONFIG_BEOS_NETSERVER
409 static int udp_read(URLContext *h, uint8_t *buf, int size)
411 UDPContext *s = h->priv_data;
413 struct sockaddr_in from;
415 struct sockaddr_storage from;
420 from_len = sizeof(from);
421 len = recvfrom (s->udp_fd, buf, size, 0,
422 (struct sockaddr *)&from, &from_len);
424 if (errno != EAGAIN && errno != EINTR)
433 static int udp_write(URLContext *h, uint8_t *buf, int size)
435 UDPContext *s = h->priv_data;
439 ret = sendto (s->udp_fd, buf, size, 0,
440 (struct sockaddr *) &s->dest_addr,
442 sizeof (s->dest_addr));
447 if (errno != EINTR && errno != EAGAIN)
456 static int udp_close(URLContext *h)
458 UDPContext *s = h->priv_data;
460 #ifndef CONFIG_BEOS_NETSERVER
462 if (s->is_multicast && !(h->flags & URL_WRONLY)) {
463 if (setsockopt(s->udp_fd, IPPROTO_IP, IP_DROP_MEMBERSHIP,
464 &s->mreq, sizeof(s->mreq)) < 0) {
465 perror("IP_DROP_MEMBERSHIP");
469 if (s->is_multicast && !(h->flags & URL_WRONLY))
470 udp_ipv6_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
474 closesocket(s->udp_fd);
480 URLProtocol udp_protocol = {