]> git.sesse.net Git - vlc/blob - include/network.h
mkv.cpp: add some debugging messages for the users and tests for unsupported tracks
[vlc] / include / network.h
1 /*****************************************************************************
2  * network.h: interface to communicate with network plug-ins
3  *****************************************************************************
4  * Copyright (C) 2002-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Laurent Aimar <fenrir@via.ecp.fr>
9  *          RĂ©mi Denis-Courmont <rem # videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 #ifndef __VLC_NETWORK_H
27 # define __VLC_NETWORK_H
28
29 #if defined( WIN32 )
30 #   if defined(UNDER_CE) && defined(sockaddr_storage)
31 #       undef sockaddr_storage
32 #   endif
33 #   if defined(UNDER_CE)
34 #       define HAVE_STRUCT_ADDRINFO
35 #   else
36 #       include <io.h>
37 #   endif
38 #   include <winsock2.h>
39 #   include <ws2tcpip.h>
40 #   define ENETUNREACH WSAENETUNREACH
41 #else
42 #   if HAVE_SYS_SOCKET_H
43 #      include <sys/socket.h>
44 #   endif
45 #   if HAVE_NETINET_IN_H
46 #      include <netinet/in.h>
47 #   endif
48 #   if HAVE_ARPA_INET_H
49 #      include <arpa/inet.h>
50 #   elif defined( SYS_BEOS )
51 #      include <net/netdb.h>
52 #   endif
53 #   include <netdb.h>
54 #endif
55
56 # ifdef __cplusplus
57 extern "C" {
58 # endif
59
60 /*****************************************************************************
61  * network_socket_t: structure passed to a network plug-in to define the
62  *                   kind of socket we want
63  *****************************************************************************/
64 struct network_socket_t
65 {
66     const char *psz_bind_addr;
67     int i_bind_port;
68
69     const char *psz_server_addr;
70     int i_server_port;
71
72     int i_ttl;
73
74     int v6only;
75
76     /* Return values */
77     int i_handle;
78     size_t i_mtu;
79 };
80
81 typedef struct
82 {
83     char *psz_protocol;
84     char *psz_username;
85     char *psz_password;
86     char *psz_host;
87     int  i_port;
88
89     char *psz_path;
90
91     char *psz_option;
92     
93     char *psz_buffer; /* to be freed */
94 } vlc_url_t;
95
96 /*****************************************************************************
97  * vlc_UrlParse:
98  *****************************************************************************
99  * option : if != 0 then path is split at this char
100  *
101  * format [protocol://[login[:password]@]][host[:port]]/path[OPTIONoption]
102  *****************************************************************************/
103 static inline void vlc_UrlParse( vlc_url_t *url, const char *psz_url,
104                                  char option )
105 {
106     char *psz_dup;
107     char *psz_parse;
108     char *p;
109
110     url->psz_protocol = NULL;
111     url->psz_username = NULL;
112     url->psz_password = NULL;
113     url->psz_host     = NULL;
114     url->i_port       = 0;
115     url->psz_path     = NULL;
116     url->psz_option   = NULL;
117     
118     if( psz_url == NULL )
119     {
120         url->psz_buffer = NULL;
121         return;
122     }
123     url->psz_buffer = psz_parse = psz_dup = strdup( psz_url );
124
125     p  = strstr( psz_parse, ":/" );
126     if( p != NULL )
127     {
128         /* we have a protocol */
129
130         /* skip :// */
131         *p++ = '\0';
132         if( p[1] == '/' )
133             p += 2;
134         url->psz_protocol = psz_parse;
135         psz_parse = p;
136     }
137     p = strchr( psz_parse, '@' );
138     if( p != NULL )
139     {
140         /* We have a login */
141         url->psz_username = psz_parse;
142         *p++ = '\0';
143
144         psz_parse = strchr( psz_parse, ':' );
145         if( psz_parse != NULL )
146         {
147             /* We have a password */
148             *psz_parse++ = '\0';
149             url->psz_password = psz_parse;
150         }
151
152         psz_parse = p;
153     }
154
155     p = strchr( psz_parse, '/' );
156     if( !p || psz_parse < p )
157     {
158         char *p2;
159
160         /* We have a host[:port] */
161         url->psz_host = strdup( psz_parse );
162         if( p )
163         {
164             url->psz_host[p - psz_parse] = '\0';
165         }
166
167         if( *url->psz_host == '[' )
168         {
169             /* Ipv6 address */
170             p2 = strchr( url->psz_host, ']' );
171             if( p2 )
172             {
173                 p2 = strchr( p2, ':' );
174             }
175         }
176         else
177         {
178             p2 = strchr( url->psz_host, ':' );
179         }
180         if( p2 )
181         {
182             *p2++ = '\0';
183             url->i_port = atoi( p2 );
184         }
185     }
186     psz_parse = p;
187
188     /* Now parse psz_path and psz_option */
189     if( psz_parse )
190     {
191         url->psz_path = psz_parse;
192         if( option != '\0' )
193         {
194             p = strchr( url->psz_path, option );
195             if( p )
196             {
197                 *p++ = '\0';
198                 url->psz_option = p;
199             }
200         }
201     }
202 }
203
204 /*****************************************************************************
205  * vlc_UrlClean:
206  *****************************************************************************
207  *
208  *****************************************************************************/
209 static inline void vlc_UrlClean( vlc_url_t *url )
210 {
211     if( url->psz_buffer ) free( url->psz_buffer );
212     if( url->psz_host )   free( url->psz_host );
213
214     url->psz_protocol = NULL;
215     url->psz_username = NULL;
216     url->psz_password = NULL;
217     url->psz_host     = NULL;
218     url->i_port       = 0;
219     url->psz_path     = NULL;
220     url->psz_option   = NULL;
221
222     url->psz_buffer   = NULL;
223 }
224
225 static inline int isurlsafe( int c )
226 {
227     return ( (unsigned char)( c - 'a' ) < 26 )
228         || ( (unsigned char)( c - 'A' ) < 26 )
229         || ( (unsigned char)( c - '0' ) < 10 )
230         /* Hmm, we should not encode character that are allowed in URLs
231          * (even if they are not URL-safe), nor URL-safe characters.
232          * We still encode some of them because of Microsoft's crap browser.
233          */
234         || ( strchr( "-_.", c ) != NULL );
235 }
236
237 /*****************************************************************************
238  * vlc_UrlEncode: 
239  *****************************************************************************
240  * perform URL encoding
241  * (you do NOT want to do URL decoding - it is not reversible - do NOT do it)
242  *****************************************************************************/
243 static inline char *vlc_UrlEncode( const char *psz_url )
244 {
245     char *psz_enc, *out;
246     const unsigned char *in;
247
248     psz_enc = (char *)malloc( 3 * strlen( psz_url ) + 1 );
249     if( psz_enc == NULL )
250         return NULL;
251
252     out = psz_enc;
253     for( in = (const unsigned char *)psz_url; *in; in++ )
254     {
255         unsigned char c = *in;
256
257         if( isurlsafe( c ) )
258             *out++ = (char)c;
259         else
260         {
261             *out++ = '%';
262             *out++ = ( ( c >> 4 ) >= 0xA ) ? 'A' + ( c >> 4 ) - 0xA
263                                            : '0' + ( c >> 4 );
264             *out++ = ( ( c & 0xf ) >= 0xA ) ? 'A' + ( c & 0xf ) - 0xA
265                                            : '0' + ( c & 0xf );
266         }
267     }
268     *out++ = '\0';
269
270     return (char *)realloc( psz_enc, out - psz_enc );
271 }
272
273 /*****************************************************************************
274  * vlc_UrlIsNotEncoded:
275  *****************************************************************************
276  * check if given string is not a valid URL and must hence be encoded
277  *****************************************************************************/
278 #include <ctype.h>
279
280 static inline int vlc_UrlIsNotEncoded( const char *psz_url )
281 {
282     const char *ptr;
283
284     for( ptr = psz_url; *ptr; ptr++ )
285     {
286         char c = *ptr;
287
288         if( c == '%' )
289         {
290             if( !isxdigit( ptr[1] ) || !isxdigit( ptr[2] ) )
291                 return 1; /* not encoded */
292             ptr += 2;
293         }
294         else
295         if( !isurlsafe( c ) )
296             return 1;
297     }
298     return 0; /* looks fine - but maybe it is not encoded */
299 }
300
301 /*****************************************************************************
302  * vlc_b64_encode:
303  *****************************************************************************
304  *
305  *****************************************************************************/
306 static inline char *vlc_b64_encode( char *src )
307 {
308     static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
309     size_t len = strlen( src );
310
311     char *ret;
312     char *dst = (char *)malloc( ( len + 4 ) * 4 / 3 );
313     if( dst == NULL )
314         return NULL;
315
316     ret = dst;
317
318     while( len > 0 )
319     {
320         /* pops (up to) 3 bytes of input */
321         uint32_t v = *src++ << 24;
322
323         if( len >= 2 )
324         {
325             v |= *src++ << 16;
326             if( len >= 3 )
327                 v |= *src++ << 8;
328         }
329
330         /* pushes (up to) 4 bytes of output */
331         while( v )
332         {
333             *dst++ = b64[v >> 26];
334             v = v << 6;
335         }
336
337         switch( len )
338         {
339             case 1:
340                 *dst++ = '=';
341                 *dst++ = '=';
342                 len--;
343                 break;
344
345             case 2:
346                 *dst++ = '=';
347                 len -= 2;
348                 break;
349
350             default:
351                 len -= 3;
352         }
353     }
354
355     *dst = '\0';
356
357     return ret;
358 }
359
360 /* Portable networking layer communication */
361 #define net_OpenTCP(a, b, c) __net_OpenTCP(VLC_OBJECT(a), b, c)
362 VLC_EXPORT( int, __net_OpenTCP, ( vlc_object_t *p_this, const char *psz_host, int i_port ) );
363
364 #define net_ListenTCP(a, b, c) __net_ListenTCP(VLC_OBJECT(a), b, c)
365 VLC_EXPORT( int *, __net_ListenTCP, ( vlc_object_t *, const char *, int ) );
366
367 #define net_Accept(a, b, c) __net_Accept(VLC_OBJECT(a), b, c)
368 VLC_EXPORT( int, __net_Accept, ( vlc_object_t *, int *, mtime_t ) );
369
370 #define net_OpenUDP(a, b, c, d, e ) __net_OpenUDP(VLC_OBJECT(a), b, c, d, e)
371 VLC_EXPORT( int, __net_OpenUDP, ( vlc_object_t *p_this, const char *psz_bind, int i_bind, const char *psz_server, int i_server ) );
372
373 VLC_EXPORT( void, net_Close, ( int fd ) );
374 VLC_EXPORT( void, net_ListenClose, ( int *fd ) );
375
376
377 /* Functions to read from or write to the networking layer */
378 struct virtual_socket_t
379 {
380     void *p_sys;
381     int (*pf_recv) ( void *, void *, int );
382     int (*pf_send) ( void *, const void *, int );
383 };
384
385 #define net_Read(a,b,c,d,e,f) __net_Read(VLC_OBJECT(a),b,c,d,e,f)
386 VLC_EXPORT( int, __net_Read, ( vlc_object_t *p_this, int fd, v_socket_t *, uint8_t *p_data, int i_data, vlc_bool_t b_retry ) );
387
388 #define net_ReadNonBlock(a,b,c,d,e,f) __net_ReadNonBlock(VLC_OBJECT(a),b,c,d,e,f)
389 VLC_EXPORT( int, __net_ReadNonBlock, ( vlc_object_t *p_this, int fd, v_socket_t *, uint8_t *p_data, int i_data, mtime_t i_wait ) );
390
391 #define net_Select(a,b,c,d,e,f,g) __net_Select(VLC_OBJECT(a),b,c,d,e,f,g)
392 VLC_EXPORT( int, __net_Select, ( vlc_object_t *p_this, int *pi_fd, v_socket_t **, int i_fd, uint8_t *p_data, int i_data, mtime_t i_wait ) );
393
394 #define net_Write(a,b,c,d,e) __net_Write(VLC_OBJECT(a),b,c,d,e)
395 VLC_EXPORT( int, __net_Write, ( vlc_object_t *p_this, int fd, v_socket_t *, const uint8_t *p_data, int i_data ) );
396
397 #define net_Gets(a,b,c) __net_Gets(VLC_OBJECT(a),b,c)
398 VLC_EXPORT( char *, __net_Gets, ( vlc_object_t *p_this, int fd, v_socket_t * ) );
399
400 VLC_EXPORT( int, net_Printf, ( vlc_object_t *p_this, int fd, v_socket_t *, const char *psz_fmt, ... ) );
401
402 #define net_vaPrintf(a,b,c,d,e) __net_vaPrintf(VLC_OBJECT(a),b,c,d,e)
403 VLC_EXPORT( int, __net_vaPrintf, ( vlc_object_t *p_this, int fd, v_socket_t *, const char *psz_fmt, va_list args ) );
404
405
406 #if !HAVE_INET_PTON
407 /* only in core, so no need for C++ extern "C" */
408 int inet_pton(int af, const char *src, void *dst);
409 #endif
410
411
412 /*****************************************************************************
413  * net_StopRecv/Send
414  *****************************************************************************
415  * Wrappers for shutdown()
416  *****************************************************************************/
417 #if defined (SHUT_WR)
418 /* the standard way */
419 # define net_StopSend( fd ) (void)shutdown( fd, SHUT_WR )
420 # define net_StopRecv( fd ) (void)shutdown( fd, SHUT_RD )
421 #elif defined (SD_SEND)
422 /* the Microsoft seemingly-purposedly-different-for-the-sake-of-it way */
423 # define net_StopSend( fd ) (void)shutdown( fd, SD_SEND )
424 # define net_StopRecv( fd ) (void)shutdown( fd, SD_RECEIVE )
425 #else
426 # ifndef SYS_BEOS /* R5 just doesn't have a working shutdown() */
427 #  warning FIXME: implement shutdown on your platform!
428 # endif
429 # define net_StopSend( fd ) (void)0
430 # define net_StopRecv( fd ) (void)0
431 #endif
432
433 /* Portable network names/addresses resolution layer */
434
435 /* GAI error codes */
436 # ifndef EAI_BADFLAGS
437 #  define EAI_BADFLAGS -1
438 # endif
439 # ifndef EAI_NONAME
440 #  define EAI_NONAME -2
441 # endif
442 # ifndef EAI_AGAIN
443 #  define EAI_AGAIN -3
444 # endif
445 # ifndef EAI_FAIL
446 #  define EAI_FAIL -4
447 # endif
448 # ifndef EAI_NODATA
449 #  define EAI_NODATA -5
450 # endif
451 # ifndef EAI_FAMILY
452 #  define EAI_FAMILY -6
453 # endif
454 # ifndef EAI_SOCKTYPE
455 #  define EAI_SOCKTYPE -7
456 # endif
457 # ifndef EAI_SERVICE
458 #  define EAI_SERVICE -8
459 # endif
460 # ifndef EAI_ADDRFAMILY
461 #  define EAI_ADDRFAMILY -9
462 # endif
463 # ifndef EAI_MEMORY
464 #  define EAI_MEMORY -10
465 # endif
466 # ifndef EAI_SYSTEM
467 #  define EAI_SYSTEM -11
468 # endif
469
470
471 # ifndef NI_MAXHOST
472 #  define NI_MAXHOST 1025
473 #  define NI_MAXSERV 32
474 # endif
475 # define NI_MAXNUMERICHOST 64
476
477 # ifndef NI_NUMERICHOST
478 #  define NI_NUMERICHOST 0x01
479 #  define NI_NUMERICSERV 0x02
480 #  define NI_NOFQDN      0x04
481 #  define NI_NAMEREQD    0x08
482 #  define NI_DGRAM       0x10
483 # endif
484
485 # ifndef HAVE_STRUCT_ADDRINFO
486 struct addrinfo
487 {
488     int ai_flags;
489     int ai_family;
490     int ai_socktype;
491     int ai_protocol;
492     size_t ai_addrlen;
493     struct sockaddr *ai_addr;
494     char *ai_canonname;
495     struct addrinfo *ai_next;
496 };
497 #  define AI_PASSIVE     1
498 #  define AI_CANONNAME   2
499 #  define AI_NUMERICHOST 4
500 # endif /* if !HAVE_STRUCT_ADDRINFO */
501
502 VLC_EXPORT( const char *, vlc_gai_strerror, ( int ) );
503 VLC_EXPORT( int, vlc_getnameinfo, ( const struct sockaddr *, int, char *, int, int *, int ) );
504 VLC_EXPORT( int, vlc_getaddrinfo, ( vlc_object_t *, const char *, int, const struct addrinfo *, struct addrinfo ** ) );
505 VLC_EXPORT( void, vlc_freeaddrinfo, ( struct addrinfo * ) );
506
507 /*****************************************************************************
508  * net_AddressIsMulticast: This function returns VLC_FALSE if the psz_addr does
509  * not specify a multicast address or if the address is not a valid address.
510  *****************************************************************************/
511 static inline vlc_bool_t net_AddressIsMulticast( vlc_object_t *p_object, const char *psz_addr )
512 {
513     struct addrinfo hints, *res;
514     vlc_bool_t b_multicast = VLC_FALSE;
515     int i;
516
517     memset( &hints, 0, sizeof( hints ) );
518     hints.ai_socktype = SOCK_DGRAM; /* UDP */
519     hints.ai_flags = AI_NUMERICHOST;
520
521     i = vlc_getaddrinfo( p_object, psz_addr, 0,
522                          &hints, &res );
523     if( i )
524     {
525         msg_Err( p_object, "Invalid node for net_AddressIsMulticast: %s : %s",
526                  psz_addr, vlc_gai_strerror( i ) );
527         return VLC_FALSE;
528     }
529     
530     if( res->ai_family == AF_INET )
531     {
532 #if !defined( SYS_BEOS )
533         struct sockaddr_in *v4 = (struct sockaddr_in *) res->ai_addr;
534         b_multicast = ( ntohl( v4->sin_addr.s_addr ) >= 0xe0000000 )
535                    && ( ntohl( v4->sin_addr.s_addr ) <= 0xefffffff );
536 #endif
537     }
538 #if defined( WIN32 ) || defined( HAVE_GETADDRINFO )
539     else if( res->ai_family == AF_INET6 )
540     {
541         struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)res->ai_addr;
542         b_multicast = IN6_IS_ADDR_MULTICAST( &v6->sin6_addr );
543     }
544 #endif
545     
546     vlc_freeaddrinfo( res );
547     return b_multicast;
548 }
549
550 static inline int net_GetSockAddress( int fd, char *address, int *port )
551 {
552     struct sockaddr_storage addr;
553     socklen_t addrlen = sizeof( addr );
554
555     return getsockname( fd, (struct sockaddr *)&addr, &addrlen )
556         || vlc_getnameinfo( (struct sockaddr *)&addr, addrlen, address,
557                             NI_MAXNUMERICHOST, port, NI_NUMERICHOST )
558         ? VLC_EGENERIC : 0;
559 }
560
561 static inline int net_GetPeerAddress( int fd, char *address, int *port )
562 {
563     struct sockaddr_storage addr;
564     socklen_t addrlen = sizeof( addr );
565
566     return getpeername( fd, (struct sockaddr *)&addr, &addrlen )
567         || vlc_getnameinfo( (struct sockaddr *)&addr, addrlen, address,
568                             NI_MAXNUMERICHOST, port, NI_NUMERICHOST )
569         ? VLC_EGENERIC : 0;
570 }
571
572 # ifdef __cplusplus
573 }
574 # endif
575
576 #endif