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
29 #if defined( UNDER_CE )
31 #elif defined( WIN32 )
32 # include <winsock2.h>
33 # include <ws2tcpip.h>
37 # include <sys/socket.h>
42 /*****************************************************************************
43 * network_socket_t: structure passed to a network plug-in to define the
44 * kind of socket we want
45 *****************************************************************************/
46 struct network_socket_t
51 char * psz_server_addr;
73 char *psz_buffer; /* to be freed */
76 /*****************************************************************************
78 *****************************************************************************
79 * option : if != 0 then path is split at this char
81 * format [protocol://[login[:password]@]][host[:port]]/path[OPTIONoption]
82 *****************************************************************************/
83 static inline void vlc_UrlParse( vlc_url_t *url, const char *psz_url,
90 url->psz_protocol = NULL;
91 url->psz_username = NULL;
92 url->psz_password = NULL;
96 url->psz_option = NULL;
100 url->psz_buffer = NULL;
103 url->psz_buffer = psz_parse = psz_dup = strdup( psz_url );
105 p = strstr( psz_parse, ":/" );
108 /* we have a protocol */
114 url->psz_protocol = psz_parse;
117 p = strchr( psz_parse, '@' );
120 /* We have a login */
121 url->psz_username = psz_parse;
124 psz_parse = strchr( psz_parse, ':' );
125 if( psz_parse != NULL )
127 /* We have a password */
129 url->psz_password = psz_parse;
135 p = strchr( psz_parse, '/' );
136 if( !p || psz_parse < p )
140 /* We have a host[:port] */
141 url->psz_host = strdup( psz_parse );
144 url->psz_host[p - psz_parse] = '\0';
147 if( *url->psz_host == '[' )
150 p2 = strchr( url->psz_host, ']' );
153 p2 = strchr( p2, ':' );
158 p2 = strchr( url->psz_host, ':' );
163 url->i_port = atoi( p2 );
168 /* Now parse psz_path and psz_option */
171 url->psz_path = psz_parse;
174 p = strchr( url->psz_path, option );
184 /*****************************************************************************
186 *****************************************************************************
188 *****************************************************************************/
189 static inline void vlc_UrlClean( vlc_url_t *url )
191 if( url->psz_buffer ) free( url->psz_buffer );
192 if( url->psz_host ) free( url->psz_host );
194 url->psz_protocol = NULL;
195 url->psz_username = NULL;
196 url->psz_password = NULL;
197 url->psz_host = NULL;
199 url->psz_path = NULL;
200 url->psz_option = NULL;
202 url->psz_buffer = NULL;
205 /*****************************************************************************
207 *****************************************************************************
208 * perform URL encoding
209 * (you do NOT want to do URL decoding - it is not reversible - do NOT do it)
210 *****************************************************************************/
211 static inline char *vlc_UrlEncode( const char *psz_url )
216 psz_enc = (char *)malloc( 3 * strlen( psz_url ) + 1 );
217 if( psz_enc == NULL )
221 for( in = psz_url; *in; in++ )
225 if( ( c <= 32 ) || ( c == '%' ) || ( c == '?' ) || ( c == '&' )
229 *out++ = ( ( c >> 4 ) >= 0xA ) ? 'A' + ( c >> 4 ) - 0xA
231 *out++ = ( ( c & 0xf ) >= 0xA ) ? 'A' + ( c & 0xf ) - 0xA
239 return (char *)realloc( psz_enc, out - psz_enc );
242 /*****************************************************************************
243 * vlc_UrlIsNotEncoded:
244 *****************************************************************************
245 * check if given string is not a valid URL and must hence be encoded
246 *****************************************************************************/
249 static inline int vlc_UrlIsNotEncoded( const char *psz_url )
253 for( ptr = psz_url; *ptr; ptr++ )
259 if( !isxdigit( ptr[1] ) || !isxdigit( ptr[2] ) )
260 return 1; /* not encoded */
267 return 0; /* looks fine - but maybe it is not encoded */
270 /*****************************************************************************
272 *****************************************************************************
274 *****************************************************************************/
275 static inline char *vlc_b64_encode( char *src )
277 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
279 char *dst = (char *)malloc( strlen( src ) * 4 / 3 + 12 );
282 unsigned i_shift = 0;
288 i_bits = ( i_bits << 8 )|( *src++ );
291 else if( i_shift > 0 )
293 i_bits <<= 6 - i_shift;
302 while( i_shift >= 6 )
305 *dst++ = b64[(i_bits >> i_shift)&0x3f];
314 /* Portable networking layer communication */
315 #define net_OpenTCP(a, b, c) __net_OpenTCP(VLC_OBJECT(a), b, c)
316 VLC_EXPORT( int, __net_OpenTCP, ( vlc_object_t *p_this, const char *psz_host, int i_port ) );
318 #define net_ListenTCP(a, b, c) __net_ListenTCP(VLC_OBJECT(a), b, c)
319 VLC_EXPORT( int *, __net_ListenTCP, ( vlc_object_t *, const char *, int ) );
321 #define net_Accept(a, b, c) __net_Accept(VLC_OBJECT(a), b, c)
322 VLC_EXPORT( int, __net_Accept, ( vlc_object_t *, int *, mtime_t ) );
324 #define net_OpenUDP(a, b, c, d, e ) __net_OpenUDP(VLC_OBJECT(a), b, c, d, e)
325 VLC_EXPORT( int, __net_OpenUDP, ( vlc_object_t *p_this, char *psz_bind, int i_bind, char *psz_server, int i_server ) );
327 VLC_EXPORT( void, net_Close, ( int fd ) );
328 VLC_EXPORT( void, net_ListenClose, ( int *fd ) );
331 /* Functions to read from or write to the networking layer */
332 struct virtual_socket_t
335 int (*pf_recv) ( void *, void *, int );
336 int (*pf_send) ( void *, const void *, int );
339 #define net_Read(a,b,c,d,e,f) __net_Read(VLC_OBJECT(a),b,c,d,e,f)
340 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 ) );
342 #define net_ReadNonBlock(a,b,c,d,e,f) __net_ReadNonBlock(VLC_OBJECT(a),b,c,d,e,f)
343 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 ) );
345 #define net_Select(a,b,c,d,e,f,g) __net_Select(VLC_OBJECT(a),b,c,d,e,f,g)
346 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 ) );
348 #define net_Write(a,b,c,d,e) __net_Write(VLC_OBJECT(a),b,c,d,e)
349 VLC_EXPORT( int, __net_Write, ( vlc_object_t *p_this, int fd, v_socket_t *, uint8_t *p_data, int i_data ) );
351 #define net_Gets(a,b,c) __net_Gets(VLC_OBJECT(a),b,c)
352 VLC_EXPORT( char *, __net_Gets, ( vlc_object_t *p_this, int fd, v_socket_t * ) );
354 VLC_EXPORT( int, net_Printf, ( vlc_object_t *p_this, int fd, v_socket_t *, const char *psz_fmt, ... ) );
356 #define net_vaPrintf(a,b,c,d,e) __net_vaPrintf(VLC_OBJECT(a),b,c,d,e)
357 VLC_EXPORT( int, __net_vaPrintf, ( vlc_object_t *p_this, int fd, v_socket_t *, const char *psz_fmt, va_list args ) );
359 #define net_GetSockAddress(a,b,c,d) __net_GetAddress(VLC_OBJECT(a),VLC_FALSE,b,c,d)
360 #define net_GetPeerAddress(a,b,c,d) __net_GetAddress(VLC_OBJECT(a),VLC_TRUE,b,c,d)
361 VLC_EXPORT( int, __net_GetAddress, ( vlc_object_t *p_this, vlc_bool_t peer, int fd, char *address, int *port ) );
363 /*****************************************************************************
365 *****************************************************************************
366 * Wrappers for shutdown()
367 *****************************************************************************/
368 #if defined (SHUT_WR)
369 /* the standard way */
370 # define net_StopSend( fd ) (void)shutdown( fd, SHUT_WR )
371 # define net_StopRecv( fd ) (void)shutdown( fd, SHUT_RD )
372 #elif defined (SD_SEND)
373 /* the Microsoft seemingly-purposedly-different-for-the-sake-of-it way */
374 # define net_StopSend( fd ) (void)shutdown( fd, SD_SEND )
375 # define net_StopRecv( fd ) (void)shutdown( fd, SD_RECEIVE )
377 # warning FIXME: implement shutdown on your platform!
378 # define net_StopSend( fd ) (void)0
379 # define net_StopRecv( fd ) (void)0
382 /* Portable network names/addresses resolution layer */
384 /* GAI error codes */
385 # ifndef EAI_BADFLAGS
386 # define EAI_BADFLAGS -1
389 # define EAI_NONAME -2
392 # define EAI_AGAIN -3
398 # define EAI_NODATA -5
401 # define EAI_FAMILY -6
403 # ifndef EAI_SOCKTYPE
404 # define EAI_SOCKTYPE -7
407 # define EAI_SERVICE -8
409 # ifndef EAI_ADDRFAMILY
410 # define EAI_ADDRFAMILY -9
413 # define EAI_MEMORY -10
416 # define EAI_SYSTEM -11
421 # define NI_MAXHOST 1025
422 # define NI_MAXSERV 32
424 # define NI_MAXNUMERICHOST 48
426 # ifndef NI_NUMERICHOST
427 # define NI_NUMERICHOST 0x01
428 # define NI_NUMERICSERV 0x02
429 # define NI_NOFQDN 0x04
430 # define NI_NAMEREQD 0x08
431 # define NI_DGRAM 0x10
434 # ifndef HAVE_STRUCT_ADDRINFO
442 struct sockaddr *ai_addr;
444 struct addrinfo *ai_next;
446 # define AI_PASSIVE 1
447 # define AI_CANONNAME 2
448 # define AI_NUMERICHOST 4
449 # endif /* if !HAVE_STRUCT_ADDRINFO */
451 /*** libidn support ***/
454 # define AI_CANONIDN 0
457 VLC_EXPORT( const char *, vlc_gai_strerror, ( int ) );
458 VLC_EXPORT( int, vlc_getnameinfo, ( const struct sockaddr *, int, char *, int, int *, int ) );
459 VLC_EXPORT( int, vlc_getaddrinfo, ( vlc_object_t *, const char *, int, const struct addrinfo *, struct addrinfo ** ) );
460 VLC_EXPORT( void, vlc_freeaddrinfo, ( struct addrinfo * ) );
462 /*****************************************************************************
463 * net_AddressIsMulticast: This function returns VLC_FALSE if the psz_addr does
464 * not specify a multicast address or if the address is not a valid address.
465 * FIXME: Only the first returned address is checked. This might be problematic.
466 *****************************************************************************/
467 static inline vlc_bool_t net_AddressIsMulticast( vlc_object_t *p_object, char *psz_addr )
469 struct addrinfo hints, *res;
470 vlc_bool_t b_multicast = VLC_FALSE;
473 if( psz_addr == NULL )
475 msg_Err( p_object, "*FIXME* Unexpected NULL URI for net_AddressIsMulticast" );
476 msg_Err( p_object, "This should not happen. VLC needs fixing." );
480 memset( &hints, 0, sizeof( hints ) );
481 hints.ai_socktype = SOCK_DGRAM; /* UDP */
482 hints.ai_flags = AI_NUMERICHOST;
484 i = vlc_getaddrinfo( p_object, psz_addr, 0,
487 i = vlc_getnameinfo( res->ai_addr, res->ai_addrlen, psz_buf,
488 sizeof( psz_buf ), NULL, NI_NUMERICHOST );*/
491 msg_Err( p_object, "Invalid node for net_AddressIsMulticast: %s : %s",
492 psz_addr, vlc_gai_strerror( i ) );
496 if( res->ai_family == AF_INET )
498 #if !defined( SYS_BEOS )
499 struct sockaddr_in *v4 = (struct sockaddr_in *) res->ai_addr;
500 b_multicast = ( ntohl( v4->sin_addr.s_addr ) > 0xe1000000 && ntohl( v4->sin_addr.s_addr ) <= 0xEFFFFFFF );
503 else if( res->ai_family == AF_INET6 )
505 #if defined( WIN32 ) || defined( HAVE_IF_NAMETOINDEX )
506 struct sockaddr_in6 *v6 = (struct sockaddr_in6 *) res->ai_addr;
507 b_multicast = IN6_IS_ADDR_MULTICAST( &v6->sin6_addr);
511 vlc_freeaddrinfo( res );