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>
51 /*****************************************************************************
52 * network_socket_t: structure passed to a network plug-in to define the
53 * kind of socket we want
54 *****************************************************************************/
55 struct network_socket_t
57 const char *psz_bind_addr;
60 const char *psz_server_addr;
84 char *psz_buffer; /* to be freed */
87 /*****************************************************************************
89 *****************************************************************************
90 * option : if != 0 then path is split at this char
92 * format [protocol://[login[:password]@]][host[:port]]/path[OPTIONoption]
93 *****************************************************************************/
94 static inline void vlc_UrlParse( vlc_url_t *url, const char *psz_url,
101 url->psz_protocol = NULL;
102 url->psz_username = NULL;
103 url->psz_password = NULL;
104 url->psz_host = NULL;
106 url->psz_path = NULL;
107 url->psz_option = NULL;
109 if( psz_url == NULL )
111 url->psz_buffer = NULL;
114 url->psz_buffer = psz_parse = psz_dup = strdup( psz_url );
116 p = strstr( psz_parse, ":/" );
119 /* we have a protocol */
125 url->psz_protocol = psz_parse;
128 p = strchr( psz_parse, '@' );
131 /* We have a login */
132 url->psz_username = psz_parse;
135 psz_parse = strchr( psz_parse, ':' );
136 if( psz_parse != NULL )
138 /* We have a password */
140 url->psz_password = psz_parse;
146 p = strchr( psz_parse, '/' );
147 if( !p || psz_parse < p )
151 /* We have a host[:port] */
152 url->psz_host = strdup( psz_parse );
155 url->psz_host[p - psz_parse] = '\0';
158 if( *url->psz_host == '[' )
161 p2 = strchr( url->psz_host, ']' );
164 p2 = strchr( p2, ':' );
169 p2 = strchr( url->psz_host, ':' );
174 url->i_port = atoi( p2 );
179 /* Now parse psz_path and psz_option */
182 url->psz_path = psz_parse;
185 p = strchr( url->psz_path, option );
195 /*****************************************************************************
197 *****************************************************************************
199 *****************************************************************************/
200 static inline void vlc_UrlClean( vlc_url_t *url )
202 if( url->psz_buffer ) free( url->psz_buffer );
203 if( url->psz_host ) free( url->psz_host );
205 url->psz_protocol = NULL;
206 url->psz_username = NULL;
207 url->psz_password = NULL;
208 url->psz_host = NULL;
210 url->psz_path = NULL;
211 url->psz_option = NULL;
213 url->psz_buffer = NULL;
216 static inline int isurlsafe( int c )
218 return ( (unsigned char)( c - 'a' ) < 26 )
219 || ( (unsigned char)( c - 'A' ) < 26 )
220 || ( (unsigned char)( c - '0' ) < 10 )
221 /* Hmm, we should not encode character that are allowed in URLs
222 * (even if they are not URL-safe), nor URL-safe characters.
223 * We still encode some of them because of Microsoft's crap browser.
225 || ( strchr( "/:.[]@-_.", c ) != NULL );
228 /*****************************************************************************
230 *****************************************************************************
231 * perform URL encoding
232 * (you do NOT want to do URL decoding - it is not reversible - do NOT do it)
233 *****************************************************************************/
234 static inline char *vlc_UrlEncode( const char *psz_url )
237 const unsigned char *in;
239 psz_enc = (char *)malloc( 3 * strlen( psz_url ) + 1 );
240 if( psz_enc == NULL )
244 for( in = (const unsigned char *)psz_url; *in; in++ )
246 unsigned char c = *in;
253 *out++ = ( ( c >> 4 ) >= 0xA ) ? 'A' + ( c >> 4 ) - 0xA
255 *out++ = ( ( c & 0xf ) >= 0xA ) ? 'A' + ( c & 0xf ) - 0xA
261 return (char *)realloc( psz_enc, out - psz_enc );
264 /*****************************************************************************
265 * vlc_UrlIsNotEncoded:
266 *****************************************************************************
267 * check if given string is not a valid URL and must hence be encoded
268 *****************************************************************************/
271 static inline int vlc_UrlIsNotEncoded( const char *psz_url )
275 for( ptr = psz_url; *ptr; ptr++ )
281 if( !isxdigit( ptr[1] ) || !isxdigit( ptr[2] ) )
282 return 1; /* not encoded */
286 if( !isurlsafe( c ) )
289 return 0; /* looks fine - but maybe it is not encoded */
292 /*****************************************************************************
294 *****************************************************************************
296 *****************************************************************************/
297 static inline char *vlc_b64_encode( char *src )
299 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
301 char *dst = (char *)malloc( strlen( src ) * 4 / 3 + 12 );
304 unsigned i_shift = 0;
310 i_bits = ( i_bits << 8 )|( *src++ );
313 else if( i_shift > 0 )
315 i_bits <<= 6 - i_shift;
324 while( i_shift >= 6 )
327 *dst++ = b64[(i_bits >> i_shift)&0x3f];
336 /* Portable networking layer communication */
337 #define net_OpenTCP(a, b, c) __net_OpenTCP(VLC_OBJECT(a), b, c)
338 VLC_EXPORT( int, __net_OpenTCP, ( vlc_object_t *p_this, const char *psz_host, int i_port ) );
340 #define net_ListenTCP(a, b, c) __net_ListenTCP(VLC_OBJECT(a), b, c)
341 VLC_EXPORT( int *, __net_ListenTCP, ( vlc_object_t *, const char *, int ) );
343 #define net_Accept(a, b, c) __net_Accept(VLC_OBJECT(a), b, c)
344 VLC_EXPORT( int, __net_Accept, ( vlc_object_t *, int *, mtime_t ) );
346 #define net_OpenUDP(a, b, c, d, e ) __net_OpenUDP(VLC_OBJECT(a), b, c, d, e)
347 VLC_EXPORT( int, __net_OpenUDP, ( vlc_object_t *p_this, const char *psz_bind, int i_bind, const char *psz_server, int i_server ) );
349 VLC_EXPORT( void, net_Close, ( int fd ) );
350 VLC_EXPORT( void, net_ListenClose, ( int *fd ) );
353 /* Functions to read from or write to the networking layer */
354 struct virtual_socket_t
357 int (*pf_recv) ( void *, void *, int );
358 int (*pf_send) ( void *, const void *, int );
361 #define net_Read(a,b,c,d,e,f) __net_Read(VLC_OBJECT(a),b,c,d,e,f)
362 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 ) );
364 #define net_ReadNonBlock(a,b,c,d,e,f) __net_ReadNonBlock(VLC_OBJECT(a),b,c,d,e,f)
365 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 ) );
367 #define net_Select(a,b,c,d,e,f,g) __net_Select(VLC_OBJECT(a),b,c,d,e,f,g)
368 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 ) );
370 #define net_Write(a,b,c,d,e) __net_Write(VLC_OBJECT(a),b,c,d,e)
371 VLC_EXPORT( int, __net_Write, ( vlc_object_t *p_this, int fd, v_socket_t *, uint8_t *p_data, int i_data ) );
373 #define net_Gets(a,b,c) __net_Gets(VLC_OBJECT(a),b,c)
374 VLC_EXPORT( char *, __net_Gets, ( vlc_object_t *p_this, int fd, v_socket_t * ) );
376 VLC_EXPORT( int, net_Printf, ( vlc_object_t *p_this, int fd, v_socket_t *, const char *psz_fmt, ... ) );
378 #define net_vaPrintf(a,b,c,d,e) __net_vaPrintf(VLC_OBJECT(a),b,c,d,e)
379 VLC_EXPORT( int, __net_vaPrintf, ( vlc_object_t *p_this, int fd, v_socket_t *, const char *psz_fmt, va_list args ) );
383 /* only in core, so no need for C++ extern "C" */
384 int inet_pton(int af, const char *src, void *dst);
388 /*****************************************************************************
390 *****************************************************************************
391 * Wrappers for shutdown()
392 *****************************************************************************/
393 #if defined (SHUT_WR)
394 /* the standard way */
395 # define net_StopSend( fd ) (void)shutdown( fd, SHUT_WR )
396 # define net_StopRecv( fd ) (void)shutdown( fd, SHUT_RD )
397 #elif defined (SD_SEND)
398 /* the Microsoft seemingly-purposedly-different-for-the-sake-of-it way */
399 # define net_StopSend( fd ) (void)shutdown( fd, SD_SEND )
400 # define net_StopRecv( fd ) (void)shutdown( fd, SD_RECEIVE )
402 # warning FIXME: implement shutdown on your platform!
403 # define net_StopSend( fd ) (void)0
404 # define net_StopRecv( fd ) (void)0
407 /* Portable network names/addresses resolution layer */
409 /* GAI error codes */
410 # ifndef EAI_BADFLAGS
411 # define EAI_BADFLAGS -1
414 # define EAI_NONAME -2
417 # define EAI_AGAIN -3
423 # define EAI_NODATA -5
426 # define EAI_FAMILY -6
428 # ifndef EAI_SOCKTYPE
429 # define EAI_SOCKTYPE -7
432 # define EAI_SERVICE -8
434 # ifndef EAI_ADDRFAMILY
435 # define EAI_ADDRFAMILY -9
438 # define EAI_MEMORY -10
441 # define EAI_SYSTEM -11
446 # define NI_MAXHOST 1025
447 # define NI_MAXSERV 32
449 # define NI_MAXNUMERICHOST 64
451 # ifndef NI_NUMERICHOST
452 # define NI_NUMERICHOST 0x01
453 # define NI_NUMERICSERV 0x02
454 # define NI_NOFQDN 0x04
455 # define NI_NAMEREQD 0x08
456 # define NI_DGRAM 0x10
459 # ifndef HAVE_STRUCT_ADDRINFO
467 struct sockaddr *ai_addr;
469 struct addrinfo *ai_next;
471 # define AI_PASSIVE 1
472 # define AI_CANONNAME 2
473 # define AI_NUMERICHOST 4
474 # endif /* if !HAVE_STRUCT_ADDRINFO */
476 /*** libidn support ***/
479 # define AI_CANONIDN 0
482 VLC_EXPORT( const char *, vlc_gai_strerror, ( int ) );
483 VLC_EXPORT( int, vlc_getnameinfo, ( const struct sockaddr *, int, char *, int, int *, int ) );
484 VLC_EXPORT( int, vlc_getaddrinfo, ( vlc_object_t *, const char *, int, const struct addrinfo *, struct addrinfo ** ) );
485 VLC_EXPORT( void, vlc_freeaddrinfo, ( struct addrinfo * ) );
487 /*****************************************************************************
488 * net_AddressIsMulticast: This function returns VLC_FALSE if the psz_addr does
489 * not specify a multicast address or if the address is not a valid address.
490 *****************************************************************************/
491 static inline vlc_bool_t net_AddressIsMulticast( vlc_object_t *p_object, const char *psz_addr )
493 struct addrinfo hints, *res;
494 vlc_bool_t b_multicast = VLC_FALSE;
497 memset( &hints, 0, sizeof( hints ) );
498 hints.ai_socktype = SOCK_DGRAM; /* UDP */
499 hints.ai_flags = AI_NUMERICHOST;
501 i = vlc_getaddrinfo( p_object, psz_addr, 0,
505 msg_Err( p_object, "Invalid node for net_AddressIsMulticast: %s : %s",
506 psz_addr, vlc_gai_strerror( i ) );
510 if( res->ai_family == AF_INET )
512 #if !defined( SYS_BEOS )
513 struct sockaddr_in *v4 = (struct sockaddr_in *) res->ai_addr;
514 b_multicast = ( ntohl( v4->sin_addr.s_addr ) >= 0xe0000000 )
515 && ( ntohl( v4->sin_addr.s_addr ) <= 0xefffffff );
518 else if( res->ai_family == AF_INET6 )
520 #if defined( WIN32 ) || defined( HAVE_GETADDRINFO )
521 struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)res->ai_addr;
522 b_multicast = IN6_IS_ADDR_MULTICAST( &v6->sin6_addr );
526 vlc_freeaddrinfo( res );
530 static inline int net_GetSockAddress( int fd, char *address, int *port )
532 struct sockaddr_storage addr;
533 socklen_t addrlen = sizeof( addr );
535 return getpeername( fd, (struct sockaddr *)&addr, &addrlen )
536 || vlc_getnameinfo( (struct sockaddr *)&addr, addrlen, address,
537 NI_MAXNUMERICHOST, port, NI_NUMERICHOST )
541 static inline int net_GetPeerAddress( int fd, char *address, int *port )
543 struct sockaddr_storage addr;
544 socklen_t addrlen = sizeof( addr );
546 return getpeername( fd, (struct sockaddr *)&addr, &addrlen )
547 || vlc_getnameinfo( (struct sockaddr *)&addr, addrlen, address,
548 NI_MAXNUMERICHOST, port, NI_NUMERICHOST )