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
48 const char *psz_bind_addr;
51 const 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, const char *psz_bind, int i_bind, const 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 ) );
361 /* only in core, so no need for C++ extern "C" */
362 int inet_pton(int af, const char *src, void *dst);
366 /*****************************************************************************
368 *****************************************************************************
369 * Wrappers for shutdown()
370 *****************************************************************************/
371 #if defined (SHUT_WR)
372 /* the standard way */
373 # define net_StopSend( fd ) (void)shutdown( fd, SHUT_WR )
374 # define net_StopRecv( fd ) (void)shutdown( fd, SHUT_RD )
375 #elif defined (SD_SEND)
376 /* the Microsoft seemingly-purposedly-different-for-the-sake-of-it way */
377 # define net_StopSend( fd ) (void)shutdown( fd, SD_SEND )
378 # define net_StopRecv( fd ) (void)shutdown( fd, SD_RECEIVE )
380 # warning FIXME: implement shutdown on your platform!
381 # define net_StopSend( fd ) (void)0
382 # define net_StopRecv( fd ) (void)0
385 /* Portable network names/addresses resolution layer */
387 /* GAI error codes */
388 # ifndef EAI_BADFLAGS
389 # define EAI_BADFLAGS -1
392 # define EAI_NONAME -2
395 # define EAI_AGAIN -3
401 # define EAI_NODATA -5
404 # define EAI_FAMILY -6
406 # ifndef EAI_SOCKTYPE
407 # define EAI_SOCKTYPE -7
410 # define EAI_SERVICE -8
412 # ifndef EAI_ADDRFAMILY
413 # define EAI_ADDRFAMILY -9
416 # define EAI_MEMORY -10
419 # define EAI_SYSTEM -11
424 # define NI_MAXHOST 1025
425 # define NI_MAXSERV 32
427 # define NI_MAXNUMERICHOST 64
429 # ifndef NI_NUMERICHOST
430 # define NI_NUMERICHOST 0x01
431 # define NI_NUMERICSERV 0x02
432 # define NI_NOFQDN 0x04
433 # define NI_NAMEREQD 0x08
434 # define NI_DGRAM 0x10
437 # ifndef HAVE_STRUCT_ADDRINFO
445 struct sockaddr *ai_addr;
447 struct addrinfo *ai_next;
449 # define AI_PASSIVE 1
450 # define AI_CANONNAME 2
451 # define AI_NUMERICHOST 4
452 # endif /* if !HAVE_STRUCT_ADDRINFO */
454 /*** libidn support ***/
457 # define AI_CANONIDN 0
460 VLC_EXPORT( const char *, vlc_gai_strerror, ( int ) );
461 VLC_EXPORT( int, vlc_getnameinfo, ( const struct sockaddr *, int, char *, int, int *, int ) );
462 VLC_EXPORT( int, vlc_getaddrinfo, ( vlc_object_t *, const char *, int, const struct addrinfo *, struct addrinfo ** ) );
463 VLC_EXPORT( void, vlc_freeaddrinfo, ( struct addrinfo * ) );
465 /*****************************************************************************
466 * net_AddressIsMulticast: This function returns VLC_FALSE if the psz_addr does
467 * not specify a multicast address or if the address is not a valid address.
468 *****************************************************************************/
469 static inline vlc_bool_t net_AddressIsMulticast( vlc_object_t *p_object, char *psz_addr )
471 struct addrinfo hints, *res;
472 vlc_bool_t b_multicast = VLC_FALSE;
475 memset( &hints, 0, sizeof( hints ) );
476 hints.ai_socktype = SOCK_DGRAM; /* UDP */
477 hints.ai_flags = AI_NUMERICHOST;
479 i = vlc_getaddrinfo( p_object, psz_addr, 0,
483 msg_Err( p_object, "Invalid node for net_AddressIsMulticast: %s : %s",
484 psz_addr, vlc_gai_strerror( i ) );
488 if( res->ai_family == AF_INET )
490 #if !defined( SYS_BEOS )
491 struct sockaddr_in *v4 = (struct sockaddr_in *) res->ai_addr;
492 b_multicast = ( ntohl( v4->sin_addr.s_addr ) >= 0xe0000000 )
493 && ( ntohl( v4->sin_addr.s_addr ) <= 0xefffffff );
496 else if( res->ai_family == AF_INET6 )
498 #if defined( WIN32 ) || defined( HAVE_GETADDRINFO )
499 struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)res->ai_addr;
500 b_multicast = IN6_IS_ADDR_MULTICAST( &v6->sin6_addr );
504 vlc_freeaddrinfo( res );
508 static inline int net_GetSockAddress( int fd, char *address, int *port )
510 struct sockaddr_storage addr;
511 socklen_t addrlen = sizeof( addr );
513 return getpeername( fd, (struct sockaddr *)&addr, &addrlen )
514 || vlc_getnameinfo( (struct sockaddr *)&addr, addrlen, address,
515 NI_MAXNUMERICHOST, port, NI_NUMERICHOST )
519 static inline int net_GetPeerAddress( int fd, char *address, int *port )
521 struct sockaddr_storage addr;
522 socklen_t addrlen = sizeof( addr );
524 return getpeername( fd, (struct sockaddr *)&addr, &addrlen )
525 || vlc_getnameinfo( (struct sockaddr *)&addr, addrlen, address,
526 NI_MAXNUMERICHOST, port, NI_NUMERICHOST )