1 /*****************************************************************************
2 * network.h: interface to communicate with network plug-ins
3 *****************************************************************************
4 * Copyright (C) 2002-2005 the VideoLAN team
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Laurent Aimar <fenrir@via.ecp.fr>
9 * RĂ©mi Denis-Courmont <rem # videolan.org>
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.
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.
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 *****************************************************************************/
26 #ifndef __VLC_NETWORK_H
27 # define __VLC_NETWORK_H
30 # if defined(UNDER_CE) && defined(sockaddr_storage)
31 # undef sockaddr_storage
33 # include <winsock2.h>
34 # include <ws2tcpip.h>
36 # if HAVE_SYS_SOCKET_H
37 # include <sys/socket.h>
39 # if HAVE_NETINET_IN_H
40 # include <netinet/in.h>
43 # include <arpa/inet.h>
44 # elif defined( SYS_BEOS )
45 # include <net/netdb.h>
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
60 const char *psz_bind_addr;
63 const char *psz_server_addr;
87 char *psz_buffer; /* to be freed */
90 /*****************************************************************************
92 *****************************************************************************
93 * option : if != 0 then path is split at this char
95 * format [protocol://[login[:password]@]][host[:port]]/path[OPTIONoption]
96 *****************************************************************************/
97 static inline void vlc_UrlParse( vlc_url_t *url, const char *psz_url,
104 url->psz_protocol = NULL;
105 url->psz_username = NULL;
106 url->psz_password = NULL;
107 url->psz_host = NULL;
109 url->psz_path = NULL;
110 url->psz_option = NULL;
112 if( psz_url == NULL )
114 url->psz_buffer = NULL;
117 url->psz_buffer = psz_parse = psz_dup = strdup( psz_url );
119 p = strstr( psz_parse, ":/" );
122 /* we have a protocol */
128 url->psz_protocol = psz_parse;
131 p = strchr( psz_parse, '@' );
134 /* We have a login */
135 url->psz_username = psz_parse;
138 psz_parse = strchr( psz_parse, ':' );
139 if( psz_parse != NULL )
141 /* We have a password */
143 url->psz_password = psz_parse;
149 p = strchr( psz_parse, '/' );
150 if( !p || psz_parse < p )
154 /* We have a host[:port] */
155 url->psz_host = strdup( psz_parse );
158 url->psz_host[p - psz_parse] = '\0';
161 if( *url->psz_host == '[' )
164 p2 = strchr( url->psz_host, ']' );
167 p2 = strchr( p2, ':' );
172 p2 = strchr( url->psz_host, ':' );
177 url->i_port = atoi( p2 );
182 /* Now parse psz_path and psz_option */
185 url->psz_path = psz_parse;
188 p = strchr( url->psz_path, option );
198 /*****************************************************************************
200 *****************************************************************************
202 *****************************************************************************/
203 static inline void vlc_UrlClean( vlc_url_t *url )
205 if( url->psz_buffer ) free( url->psz_buffer );
206 if( url->psz_host ) free( url->psz_host );
208 url->psz_protocol = NULL;
209 url->psz_username = NULL;
210 url->psz_password = NULL;
211 url->psz_host = NULL;
213 url->psz_path = NULL;
214 url->psz_option = NULL;
216 url->psz_buffer = NULL;
219 static inline int isurlsafe( int c )
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.
228 || ( strchr( "-_.", c ) != NULL );
231 /*****************************************************************************
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 )
240 const unsigned char *in;
242 psz_enc = (char *)malloc( 3 * strlen( psz_url ) + 1 );
243 if( psz_enc == NULL )
247 for( in = (const unsigned char *)psz_url; *in; in++ )
249 unsigned char c = *in;
256 *out++ = ( ( c >> 4 ) >= 0xA ) ? 'A' + ( c >> 4 ) - 0xA
258 *out++ = ( ( c & 0xf ) >= 0xA ) ? 'A' + ( c & 0xf ) - 0xA
264 return (char *)realloc( psz_enc, out - psz_enc );
267 /*****************************************************************************
268 * vlc_UrlIsNotEncoded:
269 *****************************************************************************
270 * check if given string is not a valid URL and must hence be encoded
271 *****************************************************************************/
274 static inline int vlc_UrlIsNotEncoded( const char *psz_url )
278 for( ptr = psz_url; *ptr; ptr++ )
284 if( !isxdigit( ptr[1] ) || !isxdigit( ptr[2] ) )
285 return 1; /* not encoded */
289 if( !isurlsafe( c ) )
292 return 0; /* looks fine - but maybe it is not encoded */
295 /*****************************************************************************
297 *****************************************************************************
299 *****************************************************************************/
300 static inline char *vlc_b64_encode( char *src )
302 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
304 char *dst = (char *)malloc( strlen( src ) * 4 / 3 + 12 );
307 unsigned i_shift = 0;
313 i_bits = ( i_bits << 8 )|( *src++ );
316 else if( i_shift > 0 )
318 i_bits <<= 6 - i_shift;
327 while( i_shift >= 6 )
330 *dst++ = b64[(i_bits >> i_shift)&0x3f];
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 ) );
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 ) );
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 ) );
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 ) );
352 VLC_EXPORT( void, net_Close, ( int fd ) );
353 VLC_EXPORT( void, net_ListenClose, ( int *fd ) );
356 /* Functions to read from or write to the networking layer */
357 struct virtual_socket_t
360 int (*pf_recv) ( void *, void *, int );
361 int (*pf_send) ( void *, const void *, int );
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 ) );
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 ) );
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 ) );
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 ) );
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 * ) );
379 VLC_EXPORT( int, net_Printf, ( vlc_object_t *p_this, int fd, v_socket_t *, const char *psz_fmt, ... ) );
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 ) );
386 /* only in core, so no need for C++ extern "C" */
387 int inet_pton(int af, const char *src, void *dst);
391 /*****************************************************************************
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 )
405 # ifndef SYS_BEOS /* R5 just doesn't have a working shutdown() */
406 # warning FIXME: implement shutdown on your platform!
408 # define net_StopSend( fd ) (void)0
409 # define net_StopRecv( fd ) (void)0
412 /* Portable network names/addresses resolution layer */
414 /* GAI error codes */
415 # ifndef EAI_BADFLAGS
416 # define EAI_BADFLAGS -1
419 # define EAI_NONAME -2
422 # define EAI_AGAIN -3
428 # define EAI_NODATA -5
431 # define EAI_FAMILY -6
433 # ifndef EAI_SOCKTYPE
434 # define EAI_SOCKTYPE -7
437 # define EAI_SERVICE -8
439 # ifndef EAI_ADDRFAMILY
440 # define EAI_ADDRFAMILY -9
443 # define EAI_MEMORY -10
446 # define EAI_SYSTEM -11
451 # define NI_MAXHOST 1025
452 # define NI_MAXSERV 32
454 # define NI_MAXNUMERICHOST 64
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
464 # ifndef HAVE_STRUCT_ADDRINFO
472 struct sockaddr *ai_addr;
474 struct addrinfo *ai_next;
476 # define AI_PASSIVE 1
477 # define AI_CANONNAME 2
478 # define AI_NUMERICHOST 4
479 # endif /* if !HAVE_STRUCT_ADDRINFO */
481 /*** libidn support ***/
484 # define AI_CANONIDN 0
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 * ) );
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 )
498 struct addrinfo hints, *res;
499 vlc_bool_t b_multicast = VLC_FALSE;
502 memset( &hints, 0, sizeof( hints ) );
503 hints.ai_socktype = SOCK_DGRAM; /* UDP */
504 hints.ai_flags = AI_NUMERICHOST;
506 i = vlc_getaddrinfo( p_object, psz_addr, 0,
510 msg_Err( p_object, "Invalid node for net_AddressIsMulticast: %s : %s",
511 psz_addr, vlc_gai_strerror( i ) );
515 if( res->ai_family == AF_INET )
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 );
523 #if defined( WIN32 ) || defined( HAVE_GETADDRINFO )
524 else if( res->ai_family == AF_INET6 )
526 struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)res->ai_addr;
527 b_multicast = IN6_IS_ADDR_MULTICAST( &v6->sin6_addr );
531 vlc_freeaddrinfo( res );
535 static inline int net_GetSockAddress( int fd, char *address, int *port )
537 struct sockaddr_storage addr;
538 socklen_t addrlen = sizeof( addr );
540 return getpeername( fd, (struct sockaddr *)&addr, &addrlen )
541 || vlc_getnameinfo( (struct sockaddr *)&addr, addrlen, address,
542 NI_MAXNUMERICHOST, port, NI_NUMERICHOST )
546 static inline int net_GetPeerAddress( int fd, char *address, int *port )
548 struct sockaddr_storage addr;
549 socklen_t addrlen = sizeof( addr );
551 return getpeername( fd, (struct sockaddr *)&addr, &addrlen )
552 || vlc_getnameinfo( (struct sockaddr *)&addr, addrlen, address,
553 NI_MAXNUMERICHOST, port, NI_NUMERICHOST )