1 /*****************************************************************************
2 * network.h: interface to communicate with network plug-ins
3 *****************************************************************************
4 * Copyright (C) 2002-2005 VideoLAN
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 #ifdef HAVE_SYS_SOCKET_H
30 #include <sys/socket.h>
33 /*****************************************************************************
34 * network_socket_t: structure passed to a network plug-in to define the
35 * kind of socket we want
36 *****************************************************************************/
37 struct network_socket_t
42 char * psz_server_addr;
64 char *psz_buffer; /* to be freed */
67 /*****************************************************************************
69 *****************************************************************************
70 * option : if != 0 then path is split at this char
72 * format [protocol://[login[:password]@]][host[:port]]/path[OPTIONoption]
73 *****************************************************************************/
74 static inline void vlc_UrlParse( vlc_url_t *url, const char *psz_url,
81 url->psz_protocol = NULL;
82 url->psz_username = NULL;
83 url->psz_password = NULL;
87 url->psz_option = NULL;
91 url->psz_buffer = NULL;
94 url->psz_buffer = psz_parse = psz_dup = strdup( psz_url );
96 p = strstr( psz_parse, ":/" );
99 /* we have a protocol */
105 url->psz_protocol = psz_parse;
108 p = strchr( psz_parse, '@' );
111 /* We have a login */
112 url->psz_username = psz_parse;
115 psz_parse = strchr( psz_parse, ':' );
116 if( psz_parse != NULL )
118 /* We have a password */
120 url->psz_password = psz_parse;
127 p = strchr( psz_parse, '/' );
128 if( !p || psz_parse < p )
132 /* We have a host[:port] */
133 url->psz_host = strdup( psz_parse );
136 url->psz_host[p - psz_parse] = '\0';
139 if( *url->psz_host == '[' )
142 p2 = strchr( url->psz_host, ']' );
145 p2 = strchr( p2, ':' );
150 p2 = strchr( url->psz_host, ':' );
155 url->i_port = atoi( p2 );
160 /* Now parse psz_path and psz_option */
163 url->psz_path = psz_parse;
166 p = strchr( url->psz_path, option );
176 /*****************************************************************************
178 *****************************************************************************
180 *****************************************************************************/
181 static inline void vlc_UrlClean( vlc_url_t *url )
183 if( url->psz_buffer ) free( url->psz_buffer );
184 if( url->psz_host ) free( url->psz_host );
186 url->psz_protocol = NULL;
187 url->psz_username = NULL;
188 url->psz_password = NULL;
189 url->psz_host = NULL;
191 url->psz_path = NULL;
192 url->psz_option = NULL;
194 url->psz_buffer = NULL;
197 /*****************************************************************************
199 *****************************************************************************
200 * perform URL encoding
201 * (you do NOT want to do URL decoding - it is not reversible - do NOT do it)
202 *****************************************************************************/
203 static inline char *vlc_UrlEncode( const char *psz_url )
208 psz_enc = (char *)malloc( 3 * strlen( psz_url ) + 1 );
209 if( psz_enc == NULL )
213 for( in = psz_url; *in; in++ )
217 if( ( c <= 32 ) || ( c == '%' ) || ( c == '?' ) || ( c == '&' )
221 *out++ = ( ( c >> 4 ) >= 0xA ) ? 'A' + ( c >> 4 ) - 0xA
223 *out++ = ( ( c & 0xf ) >= 0xA ) ? 'A' + ( c & 0xf ) - 0xA
231 return (char *)realloc( psz_enc, out - psz_enc );
234 /*****************************************************************************
235 * vlc_UrlIsNotEncoded:
236 *****************************************************************************
237 * check if given string is not a valid URL and must hence be encoded
238 *****************************************************************************/
241 static inline int vlc_UrlIsNotEncoded( const char *psz_url )
245 for( ptr = psz_url; *ptr; ptr++ )
251 if( !isxdigit( ptr[1] ) || !isxdigit( ptr[2] ) )
252 return 1; /* not encoded */
259 return 0; /* looks fine - but maybe it is not encoded */
262 /*****************************************************************************
264 *****************************************************************************
266 *****************************************************************************/
267 static inline char *vlc_b64_encode( char *src )
269 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
271 char *dst = (char *)malloc( strlen( src ) * 4 / 3 + 12 );
274 unsigned i_shift = 0;
280 i_bits = ( i_bits << 8 )|( *src++ );
283 else if( i_shift > 0 )
285 i_bits <<= 6 - i_shift;
294 while( i_shift >= 6 )
297 *dst++ = b64[(i_bits >> i_shift)&0x3f];
306 /* Portable networking layer communication */
307 #define net_OpenTCP(a, b, c) __net_OpenTCP(VLC_OBJECT(a), b, c)
308 VLC_EXPORT( int, __net_OpenTCP, ( vlc_object_t *p_this, const char *psz_host, int i_port ) );
310 #define net_ListenTCP(a, b, c) __net_ListenTCP(VLC_OBJECT(a), b, c)
311 VLC_EXPORT( int *, __net_ListenTCP, ( vlc_object_t *, const char *, int ) );
313 #define net_Accept(a, b, c) __net_Accept(VLC_OBJECT(a), b, c)
314 VLC_EXPORT( int, __net_Accept, ( vlc_object_t *, int *, mtime_t ) );
316 #define net_OpenUDP(a, b, c, d, e ) __net_OpenUDP(VLC_OBJECT(a), b, c, d, e)
317 VLC_EXPORT( int, __net_OpenUDP, ( vlc_object_t *p_this, char *psz_bind, int i_bind, char *psz_server, int i_server ) );
319 VLC_EXPORT( void, net_Close, ( int fd ) );
320 VLC_EXPORT( void, net_ListenClose, ( int *fd ) );
323 /* Functions to read from or write to the networking layer */
324 struct virtual_socket_t
327 int (*pf_recv) ( void *, void *, int );
328 int (*pf_send) ( void *, const void *, int );
331 #define net_Read(a,b,c,d,e,f) __net_Read(VLC_OBJECT(a),b,c,d,e,f)
332 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 ) );
334 #define net_ReadNonBlock(a,b,c,d,e,f) __net_ReadNonBlock(VLC_OBJECT(a),b,c,d,e,f)
335 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 ) );
337 #define net_Select(a,b,c,d,e,f,g) __net_Select(VLC_OBJECT(a),b,c,d,e,f,g)
338 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 ) );
340 #define net_Write(a,b,c,d,e) __net_Write(VLC_OBJECT(a),b,c,d,e)
341 VLC_EXPORT( int, __net_Write, ( vlc_object_t *p_this, int fd, v_socket_t *, uint8_t *p_data, int i_data ) );
343 #define net_Gets(a,b,c) __net_Gets(VLC_OBJECT(a),b,c)
344 VLC_EXPORT( char *, __net_Gets, ( vlc_object_t *p_this, int fd, v_socket_t * ) );
346 VLC_EXPORT( int, net_Printf, ( vlc_object_t *p_this, int fd, v_socket_t *, const char *psz_fmt, ... ) );
348 #define net_vaPrintf(a,b,c,d,e) __net_vaPrintf(VLC_OBJECT(a),b,c,d,e)
349 VLC_EXPORT( int, __net_vaPrintf, ( vlc_object_t *p_this, int fd, v_socket_t *, const char *psz_fmt, va_list args ) );
351 /*****************************************************************************
353 *****************************************************************************
354 * Wrappers for shutdown()
355 *****************************************************************************/
356 #if defined (SHUT_WR)
357 /* the standard way */
358 # define net_StopSend( fd ) shutdown( fd, SHUT_WR )
359 # define net_StopRecv( fd ) shutdown( fd, SHUT_RD )
360 #elif defined (SD_SEND)
361 /* the Microsoft seemingly-purposedly-different-for-the-sake-of-it way */
362 # define net_StopSend( fd ) shutdown( fd, SD_SEND )
363 # define net_StopRecv( fd ) shutdown( fd, SD_RECEIVE )
365 # warning FIXME: implement shutdown on your platform!
366 # define net_StopSend( fd ) return(-1)
367 # define net_StopRecv( fd ) return(-1)
370 #define net_CheckIP(a,b,c,d) __net_CheckIP(VLC_OBJECT(a),b,c,d)
371 VLC_EXPORT( int, __net_CheckIP, ( vlc_object_t *p_this, char *psz_ip, char **ppsz_hosts, int i_hosts ) );
373 /* Portable network names/addresses resolution layer */
375 /* GAI error codes */
376 # ifndef EAI_BADFLAGS
377 # define EAI_BADFLAGS -1
380 # define EAI_NONAME -2
383 # define EAI_AGAIN -3
389 # define EAI_NODATA -5
392 # define EAI_FAMILY -6
394 # ifndef EAI_SOCKTYPE
395 # define EAI_SOCKTYPE -7
398 # define EAI_SERVICE -8
400 # ifndef EAI_ADDRFAMILY
401 # define EAI_ADDRFAMILY -9
404 # define EAI_MEMORY -10
407 # define EAI_SYSTEM -11
412 # define NI_MAXHOST 1025
413 # define NI_MAXSERV 32
415 # define NI_MAXNUMERICHOST 48
417 # ifndef NI_NUMERICHOST
418 # define NI_NUMERICHOST 0x01
419 # define NI_NUMERICSERV 0x02
420 # define NI_NOFQDN 0x04
421 # define NI_NAMEREQD 0x08
422 # define NI_DGRAM 0x10
425 # ifndef HAVE_STRUCT_ADDRINFO
433 struct sockaddr *ai_addr;
435 struct addrinfo *ai_next;
437 # define AI_PASSIVE 1
438 # define AI_CANONNAME 2
439 # define AI_NUMERICHOST 4
440 # endif /* if !HAVE_STRUCT_ADDRINFO */
442 /*** libidn support ***/
445 # define AI_CANONIDN 0
448 VLC_EXPORT( const char *, vlc_gai_strerror, ( int ) );
449 VLC_EXPORT( int, vlc_getnameinfo, ( const struct sockaddr *, int, char *, int, int *, int ) );
450 VLC_EXPORT( int, vlc_getaddrinfo, ( vlc_object_t *, const char *, int, const struct addrinfo *, struct addrinfo ** ) );
451 VLC_EXPORT( void, vlc_freeaddrinfo, ( struct addrinfo * ) );