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