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 #elif HAVE_SYS_SOCKET_H
34 # include <sys/socket.h>
37 /*****************************************************************************
38 * network_socket_t: structure passed to a network plug-in to define the
39 * kind of socket we want
40 *****************************************************************************/
41 struct network_socket_t
46 char * psz_server_addr;
68 char *psz_buffer; /* to be freed */
71 /*****************************************************************************
73 *****************************************************************************
74 * option : if != 0 then path is split at this char
76 * format [protocol://[login[:password]@]][host[:port]]/path[OPTIONoption]
77 *****************************************************************************/
78 static inline void vlc_UrlParse( vlc_url_t *url, const char *psz_url,
85 url->psz_protocol = NULL;
86 url->psz_username = NULL;
87 url->psz_password = NULL;
91 url->psz_option = NULL;
95 url->psz_buffer = NULL;
98 url->psz_buffer = psz_parse = psz_dup = strdup( psz_url );
100 p = strstr( psz_parse, ":/" );
103 /* we have a protocol */
109 url->psz_protocol = psz_parse;
112 p = strchr( psz_parse, '@' );
115 /* We have a login */
116 url->psz_username = psz_parse;
119 psz_parse = strchr( psz_parse, ':' );
120 if( psz_parse != NULL )
122 /* We have a password */
124 url->psz_password = psz_parse;
131 p = strchr( psz_parse, '/' );
132 if( !p || psz_parse < p )
136 /* We have a host[:port] */
137 url->psz_host = strdup( psz_parse );
140 url->psz_host[p - psz_parse] = '\0';
143 if( *url->psz_host == '[' )
146 p2 = strchr( url->psz_host, ']' );
149 p2 = strchr( p2, ':' );
154 p2 = strchr( url->psz_host, ':' );
159 url->i_port = atoi( p2 );
164 /* Now parse psz_path and psz_option */
167 url->psz_path = psz_parse;
170 p = strchr( url->psz_path, option );
180 /*****************************************************************************
182 *****************************************************************************
184 *****************************************************************************/
185 static inline void vlc_UrlClean( vlc_url_t *url )
187 if( url->psz_buffer ) free( url->psz_buffer );
188 if( url->psz_host ) free( url->psz_host );
190 url->psz_protocol = NULL;
191 url->psz_username = NULL;
192 url->psz_password = NULL;
193 url->psz_host = NULL;
195 url->psz_path = NULL;
196 url->psz_option = NULL;
198 url->psz_buffer = NULL;
201 /*****************************************************************************
203 *****************************************************************************
204 * perform URL encoding
205 * (you do NOT want to do URL decoding - it is not reversible - do NOT do it)
206 *****************************************************************************/
207 static inline char *vlc_UrlEncode( const char *psz_url )
212 psz_enc = (char *)malloc( 3 * strlen( psz_url ) + 1 );
213 if( psz_enc == NULL )
217 for( in = psz_url; *in; in++ )
221 if( ( c <= 32 ) || ( c == '%' ) || ( c == '?' ) || ( c == '&' )
225 *out++ = ( ( c >> 4 ) >= 0xA ) ? 'A' + ( c >> 4 ) - 0xA
227 *out++ = ( ( c & 0xf ) >= 0xA ) ? 'A' + ( c & 0xf ) - 0xA
235 return (char *)realloc( psz_enc, out - psz_enc );
238 /*****************************************************************************
239 * vlc_UrlIsNotEncoded:
240 *****************************************************************************
241 * check if given string is not a valid URL and must hence be encoded
242 *****************************************************************************/
245 static inline int vlc_UrlIsNotEncoded( const char *psz_url )
249 for( ptr = psz_url; *ptr; ptr++ )
255 if( !isxdigit( ptr[1] ) || !isxdigit( ptr[2] ) )
256 return 1; /* not encoded */
263 return 0; /* looks fine - but maybe it is not encoded */
266 /*****************************************************************************
268 *****************************************************************************
270 *****************************************************************************/
271 static inline char *vlc_b64_encode( char *src )
273 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
275 char *dst = (char *)malloc( strlen( src ) * 4 / 3 + 12 );
278 unsigned i_shift = 0;
284 i_bits = ( i_bits << 8 )|( *src++ );
287 else if( i_shift > 0 )
289 i_bits <<= 6 - i_shift;
298 while( i_shift >= 6 )
301 *dst++ = b64[(i_bits >> i_shift)&0x3f];
310 /* Portable networking layer communication */
311 #define net_OpenTCP(a, b, c) __net_OpenTCP(VLC_OBJECT(a), b, c)
312 VLC_EXPORT( int, __net_OpenTCP, ( vlc_object_t *p_this, const char *psz_host, int i_port ) );
314 #define net_ListenTCP(a, b, c) __net_ListenTCP(VLC_OBJECT(a), b, c)
315 VLC_EXPORT( int *, __net_ListenTCP, ( vlc_object_t *, const char *, int ) );
317 #define net_Accept(a, b, c) __net_Accept(VLC_OBJECT(a), b, c)
318 VLC_EXPORT( int, __net_Accept, ( vlc_object_t *, int *, mtime_t ) );
320 #define net_OpenUDP(a, b, c, d, e ) __net_OpenUDP(VLC_OBJECT(a), b, c, d, e)
321 VLC_EXPORT( int, __net_OpenUDP, ( vlc_object_t *p_this, char *psz_bind, int i_bind, char *psz_server, int i_server ) );
323 VLC_EXPORT( void, net_Close, ( int fd ) );
324 VLC_EXPORT( void, net_ListenClose, ( int *fd ) );
327 /* Functions to read from or write to the networking layer */
328 struct virtual_socket_t
331 int (*pf_recv) ( void *, void *, int );
332 int (*pf_send) ( void *, const void *, int );
335 #define net_Read(a,b,c,d,e,f) __net_Read(VLC_OBJECT(a),b,c,d,e,f)
336 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 ) );
338 #define net_ReadNonBlock(a,b,c,d,e,f) __net_ReadNonBlock(VLC_OBJECT(a),b,c,d,e,f)
339 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 ) );
341 #define net_Select(a,b,c,d,e,f,g) __net_Select(VLC_OBJECT(a),b,c,d,e,f,g)
342 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 ) );
344 #define net_Write(a,b,c,d,e) __net_Write(VLC_OBJECT(a),b,c,d,e)
345 VLC_EXPORT( int, __net_Write, ( vlc_object_t *p_this, int fd, v_socket_t *, uint8_t *p_data, int i_data ) );
347 #define net_Gets(a,b,c) __net_Gets(VLC_OBJECT(a),b,c)
348 VLC_EXPORT( char *, __net_Gets, ( vlc_object_t *p_this, int fd, v_socket_t * ) );
350 VLC_EXPORT( int, net_Printf, ( vlc_object_t *p_this, int fd, v_socket_t *, const char *psz_fmt, ... ) );
352 #define net_vaPrintf(a,b,c,d,e) __net_vaPrintf(VLC_OBJECT(a),b,c,d,e)
353 VLC_EXPORT( int, __net_vaPrintf, ( vlc_object_t *p_this, int fd, v_socket_t *, const char *psz_fmt, va_list args ) );
355 /*****************************************************************************
357 *****************************************************************************
358 * Wrappers for shutdown()
359 *****************************************************************************/
360 #if defined (SHUT_WR)
361 /* the standard way */
362 # define net_StopSend( fd ) (void)shutdown( fd, SHUT_WR )
363 # define net_StopRecv( fd ) (void)shutdown( fd, SHUT_RD )
364 #elif defined (SD_SEND)
365 /* the Microsoft seemingly-purposedly-different-for-the-sake-of-it way */
366 # define net_StopSend( fd ) (void)shutdown( fd, SD_SEND )
367 # define net_StopRecv( fd ) (void)shutdown( fd, SD_RECEIVE )
369 # warning FIXME: implement shutdown on your platform!
370 # define net_StopSend( fd ) (void)0
371 # define net_StopRecv( fd ) (void)0
374 #define net_CheckIP(a,b,c,d) __net_CheckIP(VLC_OBJECT(a),b,c,d)
375 VLC_EXPORT( int, __net_CheckIP, ( vlc_object_t *p_this, char *psz_ip, char **ppsz_hosts, int i_hosts ) );
377 /* Portable network names/addresses resolution layer */
379 /* GAI error codes */
380 # ifndef EAI_BADFLAGS
381 # define EAI_BADFLAGS -1
384 # define EAI_NONAME -2
387 # define EAI_AGAIN -3
393 # define EAI_NODATA -5
396 # define EAI_FAMILY -6
398 # ifndef EAI_SOCKTYPE
399 # define EAI_SOCKTYPE -7
402 # define EAI_SERVICE -8
404 # ifndef EAI_ADDRFAMILY
405 # define EAI_ADDRFAMILY -9
408 # define EAI_MEMORY -10
411 # define EAI_SYSTEM -11
416 # define NI_MAXHOST 1025
417 # define NI_MAXSERV 32
419 # define NI_MAXNUMERICHOST 48
421 # ifndef NI_NUMERICHOST
422 # define NI_NUMERICHOST 0x01
423 # define NI_NUMERICSERV 0x02
424 # define NI_NOFQDN 0x04
425 # define NI_NAMEREQD 0x08
426 # define NI_DGRAM 0x10
429 # ifndef HAVE_STRUCT_ADDRINFO
437 struct sockaddr *ai_addr;
439 struct addrinfo *ai_next;
441 # define AI_PASSIVE 1
442 # define AI_CANONNAME 2
443 # define AI_NUMERICHOST 4
444 # endif /* if !HAVE_STRUCT_ADDRINFO */
446 /*** libidn support ***/
449 # define AI_CANONIDN 0
452 VLC_EXPORT( const char *, vlc_gai_strerror, ( int ) );
453 VLC_EXPORT( int, vlc_getnameinfo, ( const struct sockaddr *, int, char *, int, int *, int ) );
454 VLC_EXPORT( int, vlc_getaddrinfo, ( vlc_object_t *, const char *, int, const struct addrinfo *, struct addrinfo ** ) );
455 VLC_EXPORT( void, vlc_freeaddrinfo, ( struct addrinfo * ) );