Statistics
| Branch: | Revision:

ffmpeg / libavformat / udp.c @ 649b918c

History | View | Annotate | Download (13.8 KB)

1 de6d9b64 Fabrice Bellard
/*
2
 * UDP prototype streaming system
3 2d225591 Fabrice Bellard
 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
4 de6d9b64 Fabrice Bellard
 *
5 19720f15 Fabrice Bellard
 * 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.
9 de6d9b64 Fabrice Bellard
 *
10 19720f15 Fabrice Bellard
 * This library is distributed in the hope that it will be useful,
11 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 19720f15 Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14 de6d9b64 Fabrice Bellard
 *
15 19720f15 Fabrice Bellard
 * 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
18 de6d9b64 Fabrice Bellard
 */
19 8be1c656 Fabrice Bellard
#include "avformat.h"
20 de6d9b64 Fabrice Bellard
#include <unistd.h>
21
#include <sys/types.h>
22 b8a78f41 Michael Niedermayer
#include <sys/socket.h>
23 de6d9b64 Fabrice Bellard
#include <netinet/in.h>
24 9ddd71fc François Revol
#ifndef __BEOS__
25
# include <arpa/inet.h>
26
#else
27
# include "barpainet.h"
28
#endif
29 de6d9b64 Fabrice Bellard
#include <netdb.h>
30
31
typedef struct {
32 2d225591 Fabrice Bellard
    int udp_fd;
33
    int ttl;
34
    int is_multicast;
35
    int local_port;
36 7a91333f Hans Zandbelt
#ifndef CONFIG_IPV6
37 2d225591 Fabrice Bellard
    struct ip_mreq mreq;
38
    struct sockaddr_in dest_addr;
39 7a91333f Hans Zandbelt
#else
40
    struct sockaddr_storage dest_addr;
41
    size_t dest_addr_len;
42
#endif
43 de6d9b64 Fabrice Bellard
} UDPContext;
44
45
#define UDP_TX_BUF_SIZE 32768
46
47 7a91333f Hans Zandbelt
#ifdef CONFIG_IPV6
48
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);    
54
    return -1;
55
}
56
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)");
61
            return -1;
62
        }
63
    }
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)");
67
            return -1;
68
        }
69
    }
70
    return 0;
71
}
72
73
int udp_ipv6_join_multicast_group(int sockfd, struct sockaddr *addr) {
74
    struct ip_mreq   mreq;
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)");
81
            return -1;
82
        }
83
    }
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)");
89
            return -1;
90
        }
91
    }
92
    return 0;
93
}
94
95
int udp_ipv6_leave_multicast_group(int sockfd, struct sockaddr *addr) {
96
    struct ip_mreq   mreq;
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)");
103
            return -1;
104
        }
105
    }
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)");
111
            return -1;
112
        }
113
    }
114
    return 0;
115
}
116
117
struct addrinfo* udp_ipv6_resolve_host(const char *hostname, int port, int type, int family, int flags) {
118
    struct addrinfo hints, *res = 0;
119
    int error;
120
    char sport[16];
121
    const char *node = 0, *service = 0;
122
123
    if (port > 0) {
124 2fc8ea24 Michael Niedermayer
        snprintf(sport, sizeof(sport), "%d", port);
125 7a91333f Hans Zandbelt
        service = sport;
126
    }
127
    if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) {
128
        node = hostname;
129
    }
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));
137
        }
138
    }
139
    return res;
140
}
141
142
int udp_ipv6_set_remote_url(URLContext *h, const char *uri) {
143
    UDPContext *s = h->priv_data;
144
    char hostname[256];
145
    int port;
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;
152
    freeaddrinfo(res0);
153
    return 0;
154
}
155
156
int udp_ipv6_set_local(URLContext *h) {
157
    UDPContext *s = h->priv_data;
158
    int udp_fd = -1;
159
    struct sockaddr_storage clientaddr;
160
    socklen_t addrlen;
161
    char sbuf[NI_MAXSERV];
162
    char hbuf[NI_MAXHOST];
163 88730be6 Måns Rullgård
    struct addrinfo *res0 = NULL;
164 7a91333f Hans Zandbelt
    int family;
165
                
166
    if (s->local_port != 0) {       
167
        res0 = udp_ipv6_resolve_host(0, s->local_port, SOCK_DGRAM, AF_UNSPEC, AI_PASSIVE);
168 88730be6 Måns Rullgård
        if (res0 == 0)
169
            goto fail;
170 7a91333f Hans Zandbelt
        family = res0->ai_family;
171
    } else {
172
        family = s->dest_addr.ss_family;
173
    }
174
    
175
    udp_fd = socket(family, SOCK_DGRAM, 0);
176
    if (udp_fd < 0) {
177
        perror("socket");
178
        goto fail;
179
    }
180
   
181
    if (s->local_port != 0) {
182
        if (bind(udp_fd, res0->ai_addr, res0->ai_addrlen) < 0) {
183
            perror("bind");
184
            goto fail;
185
        }
186 88730be6 Måns Rullgård
        freeaddrinfo(res0);
187
        res0 = NULL;
188 7a91333f Hans Zandbelt
    } 
189
190
    addrlen = sizeof(clientaddr);
191
    if (getsockname(udp_fd, (struct sockaddr *)&clientaddr, &addrlen) < 0) {
192
        perror("getsockname");
193
        goto fail;
194
    }
195
196
    if (getnameinfo((struct sockaddr *)&clientaddr, addrlen, hbuf, sizeof(hbuf),  sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
197
        perror("getnameinfo");
198
        goto fail;
199
    }
200
201
    s->local_port = strtol(sbuf, NULL, 10);
202
    
203
    return udp_fd;
204
    
205
 fail:
206
    if (udp_fd >= 0)
207
#ifdef CONFIG_BEOS_NETSERVER
208
        closesocket(udp_fd);
209
#else
210
        close(udp_fd);
211
#endif
212 88730be6 Måns Rullgård
    if(res0)
213
        freeaddrinfo(res0);
214 7a91333f Hans Zandbelt
    return -1;
215
}
216
217
#endif
218
219
220 2d225591 Fabrice Bellard
/**
221
 * If no filename is given to av_open_input_file because you want to
222
 * get the local port first, then you must call this function to set
223
 * the remote server address.
224
 *
225
 * url syntax: udp://host:port[?option=val...]
226
 * option: 'multicast=1' : enable multicast 
227
 *         'ttl=n'       : set the ttl value (for multicast only)
228
 *         'localport=n' : set the local port
229 9899efb4 Max Krasnyansky
 *         'pkt_size=n'  : set max packet size
230 2d225591 Fabrice Bellard
 *
231
 * @param s1 media file context
232
 * @param uri of the remote server
233
 * @return zero if no error.
234
 */
235
int udp_set_remote_url(URLContext *h, const char *uri)
236
{
237 7a91333f Hans Zandbelt
#ifdef CONFIG_IPV6
238
    return udp_ipv6_set_remote_url(h, uri);
239
#else
240 2d225591 Fabrice Bellard
    UDPContext *s = h->priv_data;
241
    char hostname[256];
242
    int port;
243
    
244 6ba5cbc6 Petr Doubek
    url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
245 de6d9b64 Fabrice Bellard
246 2d225591 Fabrice Bellard
    /* set the destination address */
247
    if (resolve_host(&s->dest_addr.sin_addr, hostname) < 0)
248 0bd586c5 Mike Melanson
        return AVERROR_IO;
249 2d225591 Fabrice Bellard
    s->dest_addr.sin_family = AF_INET;
250
    s->dest_addr.sin_port = htons(port);
251
    return 0;
252 7a91333f Hans Zandbelt
#endif
253 2d225591 Fabrice Bellard
}
254
255
/**
256
 * Return the local port used by the UDP connexion
257
 * @param s1 media file context
258
 * @return the local port number
259
 */
260
int udp_get_local_port(URLContext *h)
261
{
262
    UDPContext *s = h->priv_data;
263
    return s->local_port;
264
}
265
266
/**
267
 * Return the udp file handle for select() usage to wait for several RTP
268
 * streams at the same time.
269
 * @param h media file context
270
 */
271
int udp_get_file_handle(URLContext *h)
272
{
273
    UDPContext *s = h->priv_data;
274
    return s->udp_fd;
275
}
276
277
/* put it in UDP context */
278 de6d9b64 Fabrice Bellard
/* return non zero if error */
279
static int udp_open(URLContext *h, const char *uri, int flags)
280
{
281
    char hostname[1024];
282 2d225591 Fabrice Bellard
    int port, udp_fd = -1, tmp;
283
    UDPContext *s = NULL;
284 88730be6 Måns Rullgård
    int is_output;
285 2d225591 Fabrice Bellard
    const char *p;
286
    char buf[256];
287 88730be6 Måns Rullgård
#ifndef CONFIG_IPV6
288
    struct sockaddr_in my_addr, my_addr1;
289
    int len;
290
#endif
291 de6d9b64 Fabrice Bellard
292
    h->is_streamed = 1;
293 9899efb4 Max Krasnyansky
    h->max_packet_size = 1472;
294 de6d9b64 Fabrice Bellard
295 2d225591 Fabrice Bellard
    is_output = (flags & URL_WRONLY);
296 de6d9b64 Fabrice Bellard
    
297 2d225591 Fabrice Bellard
    s = av_malloc(sizeof(UDPContext));
298
    if (!s)
299
        return -ENOMEM;
300
301
    h->priv_data = s;
302
    s->ttl = 16;
303
    s->is_multicast = 0;
304 cc74aafb Michael Niedermayer
    s->local_port = 0;
305 2d225591 Fabrice Bellard
    p = strchr(uri, '?');
306
    if (p) {
307
        s->is_multicast = find_info_tag(buf, sizeof(buf), "multicast", p);
308
        if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
309
            s->ttl = strtol(buf, NULL, 10);
310
        }
311
        if (find_info_tag(buf, sizeof(buf), "localport", p)) {
312
            s->local_port = strtol(buf, NULL, 10);
313
        }
314 9899efb4 Max Krasnyansky
        if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
315
            h->max_packet_size = strtol(buf, NULL, 10);
316
        }
317 de6d9b64 Fabrice Bellard
    }
318 2d225591 Fabrice Bellard
319
    /* fill the dest addr */
320 6ba5cbc6 Petr Doubek
    url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
321 de6d9b64 Fabrice Bellard
    
322 2d225591 Fabrice Bellard
    /* XXX: fix url_split */
323
    if (hostname[0] == '\0' || hostname[0] == '?') {
324
        /* only accepts null hostname if input */
325
        if (s->is_multicast || (flags & URL_WRONLY))
326
            goto fail;
327
    } else {
328
        udp_set_remote_url(h, uri);
329
    }
330 de6d9b64 Fabrice Bellard
331 7a91333f Hans Zandbelt
#ifndef CONFIG_IPV6
332 2d225591 Fabrice Bellard
    udp_fd = socket(PF_INET, SOCK_DGRAM, 0);
333
    if (udp_fd < 0)
334
        goto fail;
335
336 772956c5 Fabrice Bellard
    my_addr.sin_family = AF_INET;
337
    my_addr.sin_addr.s_addr = htonl (INADDR_ANY);
338 2d225591 Fabrice Bellard
    if (s->is_multicast && !(h->flags & URL_WRONLY)) {
339 772956c5 Fabrice Bellard
        /* special case: the bind must be done on the multicast address port */
340
        my_addr.sin_port = s->dest_addr.sin_port;
341 2d225591 Fabrice Bellard
    } else {
342
        my_addr.sin_port = htons(s->local_port);
343
    }
344 de6d9b64 Fabrice Bellard
345
    /* the bind is needed to give a port to the socket now */
346 2d225591 Fabrice Bellard
    if (bind(udp_fd,(struct sockaddr *)&my_addr, sizeof(my_addr)) < 0) 
347 de6d9b64 Fabrice Bellard
        goto fail;
348
349 2d225591 Fabrice Bellard
    len = sizeof(my_addr1);
350
    getsockname(udp_fd, (struct sockaddr *)&my_addr1, &len);
351
    s->local_port = ntohs(my_addr1.sin_port);
352 de6d9b64 Fabrice Bellard
353 9ddd71fc François Revol
#ifndef CONFIG_BEOS_NETSERVER
354 2d225591 Fabrice Bellard
    if (s->is_multicast) {
355
        if (h->flags & URL_WRONLY) {
356
            /* output */
357
            if (setsockopt(udp_fd, IPPROTO_IP, IP_MULTICAST_TTL, 
358
                           &s->ttl, sizeof(s->ttl)) < 0) {
359
                perror("IP_MULTICAST_TTL");
360
                goto fail;
361
            }
362
        } else {
363
            /* input */
364
            memset(&s->mreq, 0, sizeof(s->mreq));
365
            s->mreq.imr_multiaddr = s->dest_addr.sin_addr;
366
            s->mreq.imr_interface.s_addr = htonl (INADDR_ANY);
367
            if (setsockopt(udp_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, 
368
                           &s->mreq, sizeof(s->mreq)) < 0) {
369
                perror("rtp: IP_ADD_MEMBERSHIP");
370
                goto fail;
371
            }
372
        }
373 de6d9b64 Fabrice Bellard
    }
374 9ddd71fc François Revol
#endif
375 7a91333f Hans Zandbelt
#else
376
    if (s->is_multicast && !(h->flags & URL_WRONLY))
377
        s->local_port = port;
378
    udp_fd = udp_ipv6_set_local(h);
379
    if (udp_fd < 0)
380
        goto fail;
381
#ifndef CONFIG_BEOS_NETSERVER
382
    if (s->is_multicast) {
383
        if (h->flags & URL_WRONLY) {
384
            if (udp_ipv6_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
385
                goto fail;
386
        } else {
387
            if (udp_ipv6_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0)
388
                goto fail;
389
        }
390
    }
391
#endif          
392
#endif
393 de6d9b64 Fabrice Bellard
394 2d225591 Fabrice Bellard
    if (is_output) {
395
        /* limit the tx buf size to limit latency */
396
        tmp = UDP_TX_BUF_SIZE;
397
        if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
398
            perror("setsockopt sndbuf");
399
            goto fail;
400
        }
401
    }
402
403
    s->udp_fd = udp_fd;
404 de6d9b64 Fabrice Bellard
    return 0;
405
 fail:
406 2d225591 Fabrice Bellard
    if (udp_fd >= 0)
407 9ddd71fc François Revol
#ifdef CONFIG_BEOS_NETSERVER
408
        closesocket(udp_fd);
409
#else
410 2d225591 Fabrice Bellard
        close(udp_fd);
411 9ddd71fc François Revol
#endif
412 2d225591 Fabrice Bellard
    av_free(s);
413 0bd586c5 Mike Melanson
    return AVERROR_IO;
414 de6d9b64 Fabrice Bellard
}
415
416 0c1a9eda Zdenek Kabelac
static int udp_read(URLContext *h, uint8_t *buf, int size)
417 de6d9b64 Fabrice Bellard
{
418
    UDPContext *s = h->priv_data;
419 7a91333f Hans Zandbelt
#ifndef CONFIG_IPV6
420 2d225591 Fabrice Bellard
    struct sockaddr_in from;
421 7a91333f Hans Zandbelt
#else
422
    struct sockaddr_storage from;
423
#endif
424 2d225591 Fabrice Bellard
    int from_len, len;
425
426
    for(;;) {
427
        from_len = sizeof(from);
428
        len = recvfrom (s->udp_fd, buf, size, 0,
429
                        (struct sockaddr *)&from, &from_len);
430
        if (len < 0) {
431
            if (errno != EAGAIN && errno != EINTR)
432 0bd586c5 Mike Melanson
                return AVERROR_IO;
433 2d225591 Fabrice Bellard
        } else {
434
            break;
435
        }
436
    }
437
    return len;
438 de6d9b64 Fabrice Bellard
}
439
440 0c1a9eda Zdenek Kabelac
static int udp_write(URLContext *h, uint8_t *buf, int size)
441 de6d9b64 Fabrice Bellard
{
442
    UDPContext *s = h->priv_data;
443 2d225591 Fabrice Bellard
    int ret;
444
445
    for(;;) {
446
        ret = sendto (s->udp_fd, buf, size, 0, 
447
                      (struct sockaddr *) &s->dest_addr,
448 7a91333f Hans Zandbelt
#ifndef CONFIG_IPV6
449 2d225591 Fabrice Bellard
                      sizeof (s->dest_addr));
450 7a91333f Hans Zandbelt
#else
451
                      s->dest_addr_len);
452
#endif
453 2d225591 Fabrice Bellard
        if (ret < 0) {
454
            if (errno != EINTR && errno != EAGAIN)
455 0bd586c5 Mike Melanson
                return AVERROR_IO;
456 2d225591 Fabrice Bellard
        } else {
457
            break;
458
        }
459 de6d9b64 Fabrice Bellard
    }
460 2d225591 Fabrice Bellard
    return size;
461
}
462
463
static int udp_close(URLContext *h)
464
{
465
    UDPContext *s = h->priv_data;
466
467 9ddd71fc François Revol
#ifndef CONFIG_BEOS_NETSERVER
468 7a91333f Hans Zandbelt
#ifndef CONFIG_IPV6
469 2d225591 Fabrice Bellard
    if (s->is_multicast && !(h->flags & URL_WRONLY)) {
470
        if (setsockopt(s->udp_fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, 
471
                       &s->mreq, sizeof(s->mreq)) < 0) {
472
            perror("IP_DROP_MEMBERSHIP");
473
        }
474
    }
475 7a91333f Hans Zandbelt
#else
476
    if (s->is_multicast && !(h->flags & URL_WRONLY))
477
        udp_ipv6_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
478
#endif
479 2d225591 Fabrice Bellard
    close(s->udp_fd);
480 9ddd71fc François Revol
#else
481
    closesocket(s->udp_fd);
482
#endif
483 2d225591 Fabrice Bellard
    av_free(s);
484
    return 0;
485 de6d9b64 Fabrice Bellard
}
486
487
URLProtocol udp_protocol = {
488
    "udp",
489
    udp_open,
490 2d225591 Fabrice Bellard
    udp_read,
491 de6d9b64 Fabrice Bellard
    udp_write,
492
    NULL, /* seek */
493
    udp_close,
494
};