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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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 # if defined(UNDER_CE)
34 # define HAVE_STRUCT_ADDRINFO
38 # include <winsock2.h>
39 # include <ws2tcpip.h>
40 # define ENETUNREACH WSAENETUNREACH
42 # if HAVE_SYS_SOCKET_H
43 # include <sys/socket.h>
45 # if HAVE_NETINET_IN_H
46 # include <netinet/in.h>
49 # include <arpa/inet.h>
50 # elif defined( SYS_BEOS )
51 # include <net/netdb.h>
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
66 const char *psz_bind_addr;
69 const char *psz_server_addr;
93 char *psz_buffer; /* to be freed */
96 /*****************************************************************************
98 *****************************************************************************
99 * option : if != 0 then path is split at this char
101 * format [protocol://[login[:password]@]][host[:port]]/path[OPTIONoption]
102 *****************************************************************************/
103 static inline void vlc_UrlParse( vlc_url_t *url, const char *psz_url,
110 url->psz_protocol = NULL;
111 url->psz_username = NULL;
112 url->psz_password = NULL;
113 url->psz_host = NULL;
115 url->psz_path = NULL;
116 url->psz_option = NULL;
118 if( psz_url == NULL )
120 url->psz_buffer = NULL;
123 url->psz_buffer = psz_parse = psz_dup = strdup( psz_url );
125 p = strstr( psz_parse, ":/" );
128 /* we have a protocol */
134 url->psz_protocol = psz_parse;
137 p = strchr( psz_parse, '@' );
140 /* We have a login */
141 url->psz_username = psz_parse;
144 psz_parse = strchr( psz_parse, ':' );
145 if( psz_parse != NULL )
147 /* We have a password */
149 url->psz_password = psz_parse;
155 p = strchr( psz_parse, '/' );
156 if( !p || psz_parse < p )
160 /* We have a host[:port] */
161 url->psz_host = strdup( psz_parse );
164 url->psz_host[p - psz_parse] = '\0';
167 if( *url->psz_host == '[' )
170 p2 = strchr( url->psz_host, ']' );
173 p2 = strchr( p2, ':' );
178 p2 = strchr( url->psz_host, ':' );
183 url->i_port = atoi( p2 );
188 /* Now parse psz_path and psz_option */
191 url->psz_path = psz_parse;
194 p = strchr( url->psz_path, option );
204 /*****************************************************************************
206 *****************************************************************************
208 *****************************************************************************/
209 static inline void vlc_UrlClean( vlc_url_t *url )
211 if( url->psz_buffer ) free( url->psz_buffer );
212 if( url->psz_host ) free( url->psz_host );
214 url->psz_protocol = NULL;
215 url->psz_username = NULL;
216 url->psz_password = NULL;
217 url->psz_host = NULL;
219 url->psz_path = NULL;
220 url->psz_option = NULL;
222 url->psz_buffer = NULL;
225 static inline int isurlsafe( int c )
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.
234 || ( strchr( "-_.", c ) != NULL );
237 /*****************************************************************************
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 )
246 const unsigned char *in;
248 psz_enc = (char *)malloc( 3 * strlen( psz_url ) + 1 );
249 if( psz_enc == NULL )
253 for( in = (const unsigned char *)psz_url; *in; in++ )
255 unsigned char c = *in;
262 *out++ = ( ( c >> 4 ) >= 0xA ) ? 'A' + ( c >> 4 ) - 0xA
264 *out++ = ( ( c & 0xf ) >= 0xA ) ? 'A' + ( c & 0xf ) - 0xA
270 return (char *)realloc( psz_enc, out - psz_enc );
273 /*****************************************************************************
274 * vlc_UrlIsNotEncoded:
275 *****************************************************************************
276 * check if given string is not a valid URL and must hence be encoded
277 *****************************************************************************/
280 static inline int vlc_UrlIsNotEncoded( const char *psz_url )
284 for( ptr = psz_url; *ptr; ptr++ )
290 if( !isxdigit( ptr[1] ) || !isxdigit( ptr[2] ) )
291 return 1; /* not encoded */
295 if( !isurlsafe( c ) )
298 return 0; /* looks fine - but maybe it is not encoded */
301 /*****************************************************************************
303 *****************************************************************************
305 *****************************************************************************/
306 static inline char *vlc_b64_encode( char *src )
308 static const char b64[] =
309 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
310 size_t len = strlen( src );
313 char *dst = (char *)malloc( ( len + 4 ) * 4 / 3 );
321 /* pops (up to) 3 bytes of input */
322 uint32_t v = *src++ << 24;
331 /* pushes (up to) 4 bytes of output */
334 *dst++ = b64[v >> 26];
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 ) );
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 ) );
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 ) );
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 ) );
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 ) );
378 VLC_EXPORT( void, net_Close, ( int fd ) );
379 VLC_EXPORT( void, net_ListenClose, ( int *fd ) );
382 /* Functions to read from or write to the networking layer */
383 struct virtual_socket_t
386 int (*pf_recv) ( void *, void *, int );
387 int (*pf_send) ( void *, const void *, int );
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 ) );
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 ) );
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 ) );
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 ) );
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 * ) );
405 VLC_EXPORT( int, net_Printf, ( vlc_object_t *p_this, int fd, v_socket_t *, const char *psz_fmt, ... ) );
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 ) );
412 /* only in core, so no need for C++ extern "C" */
413 int inet_pton(int af, const char *src, void *dst);
417 /*****************************************************************************
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 )
431 # ifndef SYS_BEOS /* R5 just doesn't have a working shutdown() */
432 # warning FIXME: implement shutdown on your platform!
434 # define net_StopSend( fd ) (void)0
435 # define net_StopRecv( fd ) (void)0
438 /* Portable network names/addresses resolution layer */
440 /* GAI error codes */
441 # ifndef EAI_BADFLAGS
442 # define EAI_BADFLAGS -1
445 # define EAI_NONAME -2
448 # define EAI_AGAIN -3
454 # define EAI_NODATA -5
457 # define EAI_FAMILY -6
459 # ifndef EAI_SOCKTYPE
460 # define EAI_SOCKTYPE -7
463 # define EAI_SERVICE -8
465 # ifndef EAI_ADDRFAMILY
466 # define EAI_ADDRFAMILY -9
469 # define EAI_MEMORY -10
472 # define EAI_SYSTEM -11
477 # define NI_MAXHOST 1025
478 # define NI_MAXSERV 32
480 # define NI_MAXNUMERICHOST 64
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
490 # ifndef HAVE_STRUCT_ADDRINFO
498 struct sockaddr *ai_addr;
500 struct addrinfo *ai_next;
502 # define AI_PASSIVE 1
503 # define AI_CANONNAME 2
504 # define AI_NUMERICHOST 4
505 # endif /* if !HAVE_STRUCT_ADDRINFO */
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 * ) );
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 )
518 struct addrinfo hints, *res;
519 vlc_bool_t b_multicast = VLC_FALSE;
522 memset( &hints, 0, sizeof( hints ) );
523 hints.ai_socktype = SOCK_DGRAM; /* UDP */
524 hints.ai_flags = AI_NUMERICHOST;
526 i = vlc_getaddrinfo( p_object, psz_addr, 0,
530 msg_Err( p_object, "Invalid node for net_AddressIsMulticast: %s : %s",
531 psz_addr, vlc_gai_strerror( i ) );
535 if( res->ai_family == AF_INET )
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 );
543 #if defined( WIN32 ) || defined( HAVE_GETADDRINFO )
544 else if( res->ai_family == AF_INET6 )
546 struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)res->ai_addr;
547 b_multicast = IN6_IS_ADDR_MULTICAST( &v6->sin6_addr );
551 vlc_freeaddrinfo( res );
555 static inline int net_GetSockAddress( int fd, char *address, int *port )
557 struct sockaddr_storage addr;
558 socklen_t addrlen = sizeof( addr );
560 return getsockname( fd, (struct sockaddr *)&addr, &addrlen )
561 || vlc_getnameinfo( (struct sockaddr *)&addr, addrlen, address,
562 NI_MAXNUMERICHOST, port, NI_NUMERICHOST )
566 static inline int net_GetPeerAddress( int fd, char *address, int *port )
568 struct sockaddr_storage addr;
569 socklen_t addrlen = sizeof( addr );
571 return getpeername( fd, (struct sockaddr *)&addr, &addrlen )
572 || vlc_getnameinfo( (struct sockaddr *)&addr, addrlen, address,
573 NI_MAXNUMERICHOST, port, NI_NUMERICHOST )