00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef __VLC_NETWORK_H
00027 # define __VLC_NETWORK_H
00028
00029 #if defined( WIN32 )
00030 # if defined(UNDER_CE) && defined(sockaddr_storage)
00031 # undef sockaddr_storage
00032 # endif
00033 # if defined(UNDER_CE)
00034 # define HAVE_STRUCT_ADDRINFO
00035 # else
00036 # include <io.h>
00037 # endif
00038 # include <winsock2.h>
00039 # include <ws2tcpip.h>
00040 # define ENETUNREACH WSAENETUNREACH
00041 #else
00042 # if HAVE_SYS_SOCKET_H
00043 # include <sys/socket.h>
00044 # endif
00045 # if HAVE_NETINET_IN_H
00046 # include <netinet/in.h>
00047 # endif
00048 # if HAVE_ARPA_INET_H
00049 # include <arpa/inet.h>
00050 # elif defined( SYS_BEOS )
00051 # include <net/netdb.h>
00052 # endif
00053 # include <netdb.h>
00054 #endif
00055
00056 # ifdef __cplusplus
00057 extern "C" {
00058 # endif
00059
00060
00061
00062
00063
00064 struct network_socket_t
00065 {
00066 const char *psz_bind_addr;
00067 int i_bind_port;
00068
00069 const char *psz_server_addr;
00070 int i_server_port;
00071
00072 int i_ttl;
00073
00074 int v6only;
00075
00076
00077 int i_handle;
00078 size_t i_mtu;
00079 };
00080
00081 typedef struct
00082 {
00083 char *psz_protocol;
00084 char *psz_username;
00085 char *psz_password;
00086 char *psz_host;
00087 int i_port;
00088
00089 char *psz_path;
00090
00091 char *psz_option;
00092
00093 char *psz_buffer;
00094 } vlc_url_t;
00095
00096
00097
00098
00099
00100
00101
00102
00103 static inline void vlc_UrlParse( vlc_url_t *url, const char *psz_url,
00104 char option )
00105 {
00106 char *psz_dup;
00107 char *psz_parse;
00108 char *p;
00109
00110 url->psz_protocol = NULL;
00111 url->psz_username = NULL;
00112 url->psz_password = NULL;
00113 url->psz_host = NULL;
00114 url->i_port = 0;
00115 url->psz_path = NULL;
00116 url->psz_option = NULL;
00117
00118 if( psz_url == NULL )
00119 {
00120 url->psz_buffer = NULL;
00121 return;
00122 }
00123 url->psz_buffer = psz_parse = psz_dup = strdup( psz_url );
00124
00125 p = strstr( psz_parse, ":/" );
00126 if( p != NULL )
00127 {
00128
00129
00130
00131 *p++ = '\0';
00132 if( p[1] == '/' )
00133 p += 2;
00134 url->psz_protocol = psz_parse;
00135 psz_parse = p;
00136 }
00137 p = strchr( psz_parse, '@' );
00138 if( p != NULL )
00139 {
00140
00141 url->psz_username = psz_parse;
00142 *p++ = '\0';
00143
00144 psz_parse = strchr( psz_parse, ':' );
00145 if( psz_parse != NULL )
00146 {
00147
00148 *psz_parse++ = '\0';
00149 url->psz_password = psz_parse;
00150 }
00151
00152 psz_parse = p;
00153 }
00154
00155 p = strchr( psz_parse, '/' );
00156 if( !p || psz_parse < p )
00157 {
00158 char *p2;
00159
00160
00161 url->psz_host = strdup( psz_parse );
00162 if( p )
00163 {
00164 url->psz_host[p - psz_parse] = '\0';
00165 }
00166
00167 if( *url->psz_host == '[' )
00168 {
00169
00170 p2 = strchr( url->psz_host, ']' );
00171 if( p2 )
00172 {
00173 p2 = strchr( p2, ':' );
00174 }
00175 }
00176 else
00177 {
00178 p2 = strchr( url->psz_host, ':' );
00179 }
00180 if( p2 )
00181 {
00182 *p2++ = '\0';
00183 url->i_port = atoi( p2 );
00184 }
00185 }
00186 psz_parse = p;
00187
00188
00189 if( psz_parse )
00190 {
00191 url->psz_path = psz_parse;
00192 if( option != '\0' )
00193 {
00194 p = strchr( url->psz_path, option );
00195 if( p )
00196 {
00197 *p++ = '\0';
00198 url->psz_option = p;
00199 }
00200 }
00201 }
00202 }
00203
00204
00205
00206
00207
00208
00209 static inline void vlc_UrlClean( vlc_url_t *url )
00210 {
00211 if( url->psz_buffer ) free( url->psz_buffer );
00212 if( url->psz_host ) free( url->psz_host );
00213
00214 url->psz_protocol = NULL;
00215 url->psz_username = NULL;
00216 url->psz_password = NULL;
00217 url->psz_host = NULL;
00218 url->i_port = 0;
00219 url->psz_path = NULL;
00220 url->psz_option = NULL;
00221
00222 url->psz_buffer = NULL;
00223 }
00224
00225 static inline int isurlsafe( int c )
00226 {
00227 return ( (unsigned char)( c - 'a' ) < 26 )
00228 || ( (unsigned char)( c - 'A' ) < 26 )
00229 || ( (unsigned char)( c - '0' ) < 10 )
00230
00231
00232
00233
00234 || ( strchr( "-_.", c ) != NULL );
00235 }
00236
00237
00238
00239
00240
00241
00242
00243 static inline char *vlc_UrlEncode( const char *psz_url )
00244 {
00245 char *psz_enc, *out;
00246 const unsigned char *in;
00247
00248 psz_enc = (char *)malloc( 3 * strlen( psz_url ) + 1 );
00249 if( psz_enc == NULL )
00250 return NULL;
00251
00252 out = psz_enc;
00253 for( in = (const unsigned char *)psz_url; *in; in++ )
00254 {
00255 unsigned char c = *in;
00256
00257 if( isurlsafe( c ) )
00258 *out++ = (char)c;
00259 else
00260 {
00261 *out++ = '%';
00262 *out++ = ( ( c >> 4 ) >= 0xA ) ? 'A' + ( c >> 4 ) - 0xA
00263 : '0' + ( c >> 4 );
00264 *out++ = ( ( c & 0xf ) >= 0xA ) ? 'A' + ( c & 0xf ) - 0xA
00265 : '0' + ( c & 0xf );
00266 }
00267 }
00268 *out++ = '\0';
00269
00270 return (char *)realloc( psz_enc, out - psz_enc );
00271 }
00272
00273
00274
00275
00276
00277
00278 #include <ctype.h>
00279
00280 static inline int vlc_UrlIsNotEncoded( const char *psz_url )
00281 {
00282 const char *ptr;
00283
00284 for( ptr = psz_url; *ptr; ptr++ )
00285 {
00286 char c = *ptr;
00287
00288 if( c == '%' )
00289 {
00290 if( !isxdigit( ptr[1] ) || !isxdigit( ptr[2] ) )
00291 return 1;
00292 ptr += 2;
00293 }
00294 else
00295 if( !isurlsafe( c ) )
00296 return 1;
00297 }
00298 return 0;
00299 }
00300
00301
00302
00303
00304
00305
00306 static inline char *vlc_b64_encode( char *src )
00307 {
00308 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
00309 size_t len = strlen( src );
00310
00311 char *ret;
00312 char *dst = (char *)malloc( ( len + 4 ) * 4 / 3 );
00313 if( dst == NULL )
00314 return NULL;
00315
00316 ret = dst;
00317
00318 while( len > 0 )
00319 {
00320
00321 uint32_t v = *src++ << 24;
00322
00323 if( len >= 2 )
00324 {
00325 v |= *src++ << 16;
00326 if( len >= 3 )
00327 v |= *src++ << 8;
00328 }
00329
00330
00331 while( v )
00332 {
00333 *dst++ = b64[v >> 26];
00334 v = v << 6;
00335 }
00336
00337 switch( len )
00338 {
00339 case 1:
00340 *dst++ = '=';
00341 *dst++ = '=';
00342 len--;
00343 break;
00344
00345 case 2:
00346 *dst++ = '=';
00347 len -= 2;
00348 break;
00349
00350 default:
00351 len -= 3;
00352 }
00353 }
00354
00355 *dst = '\0';
00356
00357 return ret;
00358 }
00359
00360
00361 #define net_OpenTCP(a, b, c) __net_OpenTCP(VLC_OBJECT(a), b, c)
00362 VLC_EXPORT( int, __net_OpenTCP, ( vlc_object_t *p_this, const char *psz_host, int i_port ) );
00363
00364 #define net_ListenTCP(a, b, c) __net_ListenTCP(VLC_OBJECT(a), b, c)
00365 VLC_EXPORT( int *, __net_ListenTCP, ( vlc_object_t *, const char *, int ) );
00366
00367 #define net_Accept(a, b, c) __net_Accept(VLC_OBJECT(a), b, c)
00368 VLC_EXPORT( int, __net_Accept, ( vlc_object_t *, int *, mtime_t ) );
00369
00370 #define net_OpenUDP(a, b, c, d, e ) __net_OpenUDP(VLC_OBJECT(a), b, c, d, e)
00371 VLC_EXPORT( int, __net_OpenUDP, ( vlc_object_t *p_this, const char *psz_bind, int i_bind, const char *psz_server, int i_server ) );
00372
00373 VLC_EXPORT( void, net_Close, ( int fd ) );
00374 VLC_EXPORT( void, net_ListenClose, ( int *fd ) );
00375
00376
00377
00378 struct virtual_socket_t
00379 {
00380 void *p_sys;
00381 int (*pf_recv) ( void *, void *, int );
00382 int (*pf_send) ( void *, const void *, int );
00383 };
00384
00385 #define net_Read(a,b,c,d,e,f) __net_Read(VLC_OBJECT(a),b,c,d,e,f)
00386 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 ) );
00387
00388 #define net_ReadNonBlock(a,b,c,d,e,f) __net_ReadNonBlock(VLC_OBJECT(a),b,c,d,e,f)
00389 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 ) );
00390
00391 #define net_Select(a,b,c,d,e,f,g) __net_Select(VLC_OBJECT(a),b,c,d,e,f,g)
00392 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 ) );
00393
00394 #define net_Write(a,b,c,d,e) __net_Write(VLC_OBJECT(a),b,c,d,e)
00395 VLC_EXPORT( int, __net_Write, ( vlc_object_t *p_this, int fd, v_socket_t *, const uint8_t *p_data, int i_data ) );
00396
00397 #define net_Gets(a,b,c) __net_Gets(VLC_OBJECT(a),b,c)
00398 VLC_EXPORT( char *, __net_Gets, ( vlc_object_t *p_this, int fd, v_socket_t * ) );
00399
00400 VLC_EXPORT( int, net_Printf, ( vlc_object_t *p_this, int fd, v_socket_t *, const char *psz_fmt, ... ) );
00401
00402 #define net_vaPrintf(a,b,c,d,e) __net_vaPrintf(VLC_OBJECT(a),b,c,d,e)
00403 VLC_EXPORT( int, __net_vaPrintf, ( vlc_object_t *p_this, int fd, v_socket_t *, const char *psz_fmt, va_list args ) );
00404
00405
00406 #if !HAVE_INET_PTON
00407
00408 int inet_pton(int af, const char *src, void *dst);
00409 #endif
00410
00411
00412
00413
00414
00415
00416
00417 #if defined (SHUT_WR)
00418
00419 # define net_StopSend( fd ) (void)shutdown( fd, SHUT_WR )
00420 # define net_StopRecv( fd ) (void)shutdown( fd, SHUT_RD )
00421 #elif defined (SD_SEND)
00422
00423 # define net_StopSend( fd ) (void)shutdown( fd, SD_SEND )
00424 # define net_StopRecv( fd ) (void)shutdown( fd, SD_RECEIVE )
00425 #else
00426 # ifndef SYS_BEOS
00427 # warning FIXME: implement shutdown on your platform!
00428 # endif
00429 # define net_StopSend( fd ) (void)0
00430 # define net_StopRecv( fd ) (void)0
00431 #endif
00432
00433
00434
00435
00436 # ifndef EAI_BADFLAGS
00437 # define EAI_BADFLAGS -1
00438 # endif
00439 # ifndef EAI_NONAME
00440 # define EAI_NONAME -2
00441 # endif
00442 # ifndef EAI_AGAIN
00443 # define EAI_AGAIN -3
00444 # endif
00445 # ifndef EAI_FAIL
00446 # define EAI_FAIL -4
00447 # endif
00448 # ifndef EAI_NODATA
00449 # define EAI_NODATA -5
00450 # endif
00451 # ifndef EAI_FAMILY
00452 # define EAI_FAMILY -6
00453 # endif
00454 # ifndef EAI_SOCKTYPE
00455 # define EAI_SOCKTYPE -7
00456 # endif
00457 # ifndef EAI_SERVICE
00458 # define EAI_SERVICE -8
00459 # endif
00460 # ifndef EAI_ADDRFAMILY
00461 # define EAI_ADDRFAMILY -9
00462 # endif
00463 # ifndef EAI_MEMORY
00464 # define EAI_MEMORY -10
00465 # endif
00466 # ifndef EAI_SYSTEM
00467 # define EAI_SYSTEM -11
00468 # endif
00469
00470
00471 # ifndef NI_MAXHOST
00472 # define NI_MAXHOST 1025
00473 # define NI_MAXSERV 32
00474 # endif
00475 # define NI_MAXNUMERICHOST 64
00476
00477 # ifndef NI_NUMERICHOST
00478 # define NI_NUMERICHOST 0x01
00479 # define NI_NUMERICSERV 0x02
00480 # define NI_NOFQDN 0x04
00481 # define NI_NAMEREQD 0x08
00482 # define NI_DGRAM 0x10
00483 # endif
00484
00485 # ifndef HAVE_STRUCT_ADDRINFO
00486 struct addrinfo
00487 {
00488 int ai_flags;
00489 int ai_family;
00490 int ai_socktype;
00491 int ai_protocol;
00492 size_t ai_addrlen;
00493 struct sockaddr *ai_addr;
00494 char *ai_canonname;
00495 struct addrinfo *ai_next;
00496 };
00497 # define AI_PASSIVE 1
00498 # define AI_CANONNAME 2
00499 # define AI_NUMERICHOST 4
00500 # endif
00501
00502 VLC_EXPORT( const char *, vlc_gai_strerror, ( int ) );
00503 VLC_EXPORT( int, vlc_getnameinfo, ( const struct sockaddr *, int, char *, int, int *, int ) );
00504 VLC_EXPORT( int, vlc_getaddrinfo, ( vlc_object_t *, const char *, int, const struct addrinfo *, struct addrinfo ** ) );
00505 VLC_EXPORT( void, vlc_freeaddrinfo, ( struct addrinfo * ) );
00506
00507
00508
00509
00510
00511 static inline vlc_bool_t net_AddressIsMulticast( vlc_object_t *p_object, const char *psz_addr )
00512 {
00513 struct addrinfo hints, *res;
00514 vlc_bool_t b_multicast = VLC_FALSE;
00515 int i;
00516
00517 memset( &hints, 0, sizeof( hints ) );
00518 hints.ai_socktype = SOCK_DGRAM;
00519 hints.ai_flags = AI_NUMERICHOST;
00520
00521 i = vlc_getaddrinfo( p_object, psz_addr, 0,
00522 &hints, &res );
00523 if( i )
00524 {
00525 msg_Err( p_object, "Invalid node for net_AddressIsMulticast: %s : %s",
00526 psz_addr, vlc_gai_strerror( i ) );
00527 return VLC_FALSE;
00528 }
00529
00530 if( res->ai_family == AF_INET )
00531 {
00532 #if !defined( SYS_BEOS )
00533 struct sockaddr_in *v4 = (struct sockaddr_in *) res->ai_addr;
00534 b_multicast = ( ntohl( v4->sin_addr.s_addr ) >= 0xe0000000 )
00535 && ( ntohl( v4->sin_addr.s_addr ) <= 0xefffffff );
00536 #endif
00537 }
00538 #if defined( WIN32 ) || defined( HAVE_GETADDRINFO )
00539 else if( res->ai_family == AF_INET6 )
00540 {
00541 struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)res->ai_addr;
00542 b_multicast = IN6_IS_ADDR_MULTICAST( &v6->sin6_addr );
00543 }
00544 #endif
00545
00546 vlc_freeaddrinfo( res );
00547 return b_multicast;
00548 }
00549
00550 static inline int net_GetSockAddress( int fd, char *address, int *port )
00551 {
00552 struct sockaddr_storage addr;
00553 socklen_t addrlen = sizeof( addr );
00554
00555 return getsockname( fd, (struct sockaddr *)&addr, &addrlen )
00556 || vlc_getnameinfo( (struct sockaddr *)&addr, addrlen, address,
00557 NI_MAXNUMERICHOST, port, NI_NUMERICHOST )
00558 ? VLC_EGENERIC : 0;
00559 }
00560
00561 static inline int net_GetPeerAddress( int fd, char *address, int *port )
00562 {
00563 struct sockaddr_storage addr;
00564 socklen_t addrlen = sizeof( addr );
00565
00566 return getpeername( fd, (struct sockaddr *)&addr, &addrlen )
00567 || vlc_getnameinfo( (struct sockaddr *)&addr, addrlen, address,
00568 NI_MAXNUMERICHOST, port, NI_NUMERICHOST )
00569 ? VLC_EGENERIC : 0;
00570 }
00571
00572 # ifdef __cplusplus
00573 }
00574 # endif
00575
00576 #endif