]> git.sesse.net Git - vlc/blob - include/network.h
FSF address change.
[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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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[] =
309            "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
310     size_t len = strlen( src );
311
312     char *ret;
313     char *dst = (char *)malloc( ( len + 4 ) * 4 / 3 );
314     if( dst == NULL )
315         return NULL;
316
317     ret = dst;
318
319     while( len > 0 )
320     {
321         /* pops (up to) 3 bytes of input */
322         uint32_t v = *src++ << 24;
323
324         if( len >= 2 )
325         {
326             v |= *src++ << 16;
327             if( len >= 3 )
328                 v |= *src++ << 8;
329         }
330
331         /* pushes (up to) 4 bytes of output */
332         while( v )
333         {
334             *dst++ = b64[v >> 26];
335             v = v << 6;
336         }
337
338         switch( len )
339         {
340             case 1:
341                 *dst++ = '=';
342                 *dst++ = '=';
343                 len--;
344                 break;
345
346             case 2:
347                 *dst++ = '=';
348                 len -= 2;
349                 break;
350
351             default:
352                 len -= 3;
353         }
354     }
355
356     *dst = '\0';
357
358     return ret;
359 }
360
361 /* Portable networking layer communication */
362 #define net_ConnectTCP(a, b, c) __net_ConnectTCP(VLC_OBJECT(a), b, c)
363 #define net_OpenTCP(a, b, c) __net_ConnectTCP(VLC_OBJECT(a), b, c)
364 VLC_EXPORT( int, __net_ConnectTCP, ( vlc_object_t *p_this, const char *psz_host, int i_port ) );
365
366 #define net_ListenTCP(a, b, c) __net_ListenTCP(VLC_OBJECT(a), b, c)
367 VLC_EXPORT( int *, __net_ListenTCP, ( vlc_object_t *, const char *, int ) );
368
369 #define net_Accept(a, b, c) __net_Accept(VLC_OBJECT(a), b, c)
370 VLC_EXPORT( int, __net_Accept, ( vlc_object_t *, int *, mtime_t ) );
371
372 #define net_ConnectUDP(a, b, c, d ) __net_ConnectUDP(VLC_OBJECT(a), b, c, d)
373 VLC_EXPORT( int, __net_ConnectUDP, ( vlc_object_t *p_this, const char *psz_host, int i_port, int hlim ) );
374
375 #define net_OpenUDP(a, b, c, d, e ) __net_OpenUDP(VLC_OBJECT(a), b, c, d, e)
376 VLC_EXPORT( int, __net_OpenUDP, ( vlc_object_t *p_this, const char *psz_bind, int i_bind, const char *psz_server, int i_server ) );
377
378 VLC_EXPORT( void, net_Close, ( int fd ) );
379 VLC_EXPORT( void, net_ListenClose, ( int *fd ) );
380
381
382 /* Functions to read from or write to the networking layer */
383 struct virtual_socket_t
384 {
385     void *p_sys;
386     int (*pf_recv) ( void *, void *, int );
387     int (*pf_send) ( void *, const void *, int );
388 };
389
390 #define net_Read(a,b,c,d,e,f) __net_Read(VLC_OBJECT(a),b,c,d,e,f)
391 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 ) );
392
393 #define net_ReadNonBlock(a,b,c,d,e,f) __net_ReadNonBlock(VLC_OBJECT(a),b,c,d,e,f)
394 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 ) );
395
396 #define net_Select(a,b,c,d,e,f,g) __net_Select(VLC_OBJECT(a),b,c,d,e,f,g)
397 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 ) );
398
399 #define net_Write(a,b,c,d,e) __net_Write(VLC_OBJECT(a),b,c,d,e)
400 VLC_EXPORT( int, __net_Write, ( vlc_object_t *p_this, int fd, v_socket_t *, const uint8_t *p_data, int i_data ) );
401
402 #define net_Gets(a,b,c) __net_Gets(VLC_OBJECT(a),b,c)
403 VLC_EXPORT( char *, __net_Gets, ( vlc_object_t *p_this, int fd, v_socket_t * ) );
404
405 VLC_EXPORT( int, net_Printf, ( vlc_object_t *p_this, int fd, v_socket_t *, const char *psz_fmt, ... ) );
406
407 #define net_vaPrintf(a,b,c,d,e) __net_vaPrintf(VLC_OBJECT(a),b,c,d,e)
408 VLC_EXPORT( int, __net_vaPrintf, ( vlc_object_t *p_this, int fd, v_socket_t *, const char *psz_fmt, va_list args ) );
409
410
411 #if !HAVE_INET_PTON
412 /* only in core, so no need for C++ extern "C" */
413 int inet_pton(int af, const char *src, void *dst);
414 #endif
415
416
417 /*****************************************************************************
418  * net_StopRecv/Send
419  *****************************************************************************
420  * Wrappers for shutdown()
421  *****************************************************************************/
422 #if defined (SHUT_WR)
423 /* the standard way */
424 # define net_StopSend( fd ) (void)shutdown( fd, SHUT_WR )
425 # define net_StopRecv( fd ) (void)shutdown( fd, SHUT_RD )
426 #elif defined (SD_SEND)
427 /* the Microsoft seemingly-purposedly-different-for-the-sake-of-it way */
428 # define net_StopSend( fd ) (void)shutdown( fd, SD_SEND )
429 # define net_StopRecv( fd ) (void)shutdown( fd, SD_RECEIVE )
430 #else
431 # ifndef SYS_BEOS /* R5 just doesn't have a working shutdown() */
432 #  warning FIXME: implement shutdown on your platform!
433 # endif
434 # define net_StopSend( fd ) (void)0
435 # define net_StopRecv( fd ) (void)0
436 #endif
437
438 /* Portable network names/addresses resolution layer */
439
440 /* GAI error codes */
441 # ifndef EAI_BADFLAGS
442 #  define EAI_BADFLAGS -1
443 # endif
444 # ifndef EAI_NONAME
445 #  define EAI_NONAME -2
446 # endif
447 # ifndef EAI_AGAIN
448 #  define EAI_AGAIN -3
449 # endif
450 # ifndef EAI_FAIL
451 #  define EAI_FAIL -4
452 # endif
453 # ifndef EAI_NODATA
454 #  define EAI_NODATA -5
455 # endif
456 # ifndef EAI_FAMILY
457 #  define EAI_FAMILY -6
458 # endif
459 # ifndef EAI_SOCKTYPE
460 #  define EAI_SOCKTYPE -7
461 # endif
462 # ifndef EAI_SERVICE
463 #  define EAI_SERVICE -8
464 # endif
465 # ifndef EAI_ADDRFAMILY
466 #  define EAI_ADDRFAMILY -9
467 # endif
468 # ifndef EAI_MEMORY
469 #  define EAI_MEMORY -10
470 # endif
471 # ifndef EAI_SYSTEM
472 #  define EAI_SYSTEM -11
473 # endif
474
475
476 # ifndef NI_MAXHOST
477 #  define NI_MAXHOST 1025
478 #  define NI_MAXSERV 32
479 # endif
480 # define NI_MAXNUMERICHOST 64
481
482 # ifndef NI_NUMERICHOST
483 #  define NI_NUMERICHOST 0x01
484 #  define NI_NUMERICSERV 0x02
485 #  define NI_NOFQDN      0x04
486 #  define NI_NAMEREQD    0x08
487 #  define NI_DGRAM       0x10
488 # endif
489
490 # ifndef HAVE_STRUCT_ADDRINFO
491 struct addrinfo
492 {
493     int ai_flags;
494     int ai_family;
495     int ai_socktype;
496     int ai_protocol;
497     size_t ai_addrlen;
498     struct sockaddr *ai_addr;
499     char *ai_canonname;
500     struct addrinfo *ai_next;
501 };
502 #  define AI_PASSIVE     1
503 #  define AI_CANONNAME   2
504 #  define AI_NUMERICHOST 4
505 # endif /* if !HAVE_STRUCT_ADDRINFO */
506
507 VLC_EXPORT( const char *, vlc_gai_strerror, ( int ) );
508 VLC_EXPORT( int, vlc_getnameinfo, ( const struct sockaddr *, int, char *, int, int *, int ) );
509 VLC_EXPORT( int, vlc_getaddrinfo, ( vlc_object_t *, const char *, int, const struct addrinfo *, struct addrinfo ** ) );
510 VLC_EXPORT( void, vlc_freeaddrinfo, ( struct addrinfo * ) );
511
512 /*****************************************************************************
513  * net_AddressIsMulticast: This function returns VLC_FALSE if the psz_addr does
514  * not specify a multicast address or if the address is not a valid address.
515  *****************************************************************************/
516 static inline vlc_bool_t net_AddressIsMulticast( vlc_object_t *p_object, const char *psz_addr )
517 {
518     struct addrinfo hints, *res;
519     vlc_bool_t b_multicast = VLC_FALSE;
520     int i;
521
522     memset( &hints, 0, sizeof( hints ) );
523     hints.ai_socktype = SOCK_DGRAM; /* UDP */
524     hints.ai_flags = AI_NUMERICHOST;
525
526     i = vlc_getaddrinfo( p_object, psz_addr, 0,
527                          &hints, &res );
528     if( i )
529     {
530         msg_Err( p_object, "Invalid node for net_AddressIsMulticast: %s : %s",
531                  psz_addr, vlc_gai_strerror( i ) );
532         return VLC_FALSE;
533     }
534
535     if( res->ai_family == AF_INET )
536     {
537 #if !defined( SYS_BEOS )
538         struct sockaddr_in *v4 = (struct sockaddr_in *) res->ai_addr;
539         b_multicast = ( ntohl( v4->sin_addr.s_addr ) >= 0xe0000000 )
540                    && ( ntohl( v4->sin_addr.s_addr ) <= 0xefffffff );
541 #endif
542     }
543 #if defined( WIN32 ) || defined( HAVE_GETADDRINFO )
544     else if( res->ai_family == AF_INET6 )
545     {
546         struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)res->ai_addr;
547         b_multicast = IN6_IS_ADDR_MULTICAST( &v6->sin6_addr );
548     }
549 #endif
550
551     vlc_freeaddrinfo( res );
552     return b_multicast;
553 }
554
555 static inline int net_GetSockAddress( int fd, char *address, int *port )
556 {
557     struct sockaddr_storage addr;
558     socklen_t addrlen = sizeof( addr );
559
560     return getsockname( fd, (struct sockaddr *)&addr, &addrlen )
561         || vlc_getnameinfo( (struct sockaddr *)&addr, addrlen, address,
562                             NI_MAXNUMERICHOST, port, NI_NUMERICHOST )
563         ? VLC_EGENERIC : 0;
564 }
565
566 static inline int net_GetPeerAddress( int fd, char *address, int *port )
567 {
568     struct sockaddr_storage addr;
569     socklen_t addrlen = sizeof( addr );
570
571     return getpeername( fd, (struct sockaddr *)&addr, &addrlen )
572         || vlc_getnameinfo( (struct sockaddr *)&addr, addrlen, address,
573                             NI_MAXNUMERICHOST, port, NI_NUMERICHOST )
574         ? VLC_EGENERIC : 0;
575 }
576
577 # ifdef __cplusplus
578 }
579 # endif
580
581 #endif