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
28 /*****************************************************************************
29 * network_socket_t: structure passed to a network plug-in to define the
30 * kind of socket we want
31 *****************************************************************************/
32 struct network_socket_t
37 char * psz_server_addr;
58 /*****************************************************************************
60 *****************************************************************************
61 * option : if != 0 then path is split at this char
63 * format [protocol://][host[:port]]/path[OPTIONoption]
64 *****************************************************************************/
65 static inline void vlc_UrlParse( vlc_url_t *url, char *psz_url, char option )
67 char *psz_dup = psz_url ? strdup( psz_url ) : 0;
68 char *psz_parse = psz_dup;
71 url->psz_protocol = NULL;
75 url->psz_option = NULL;
77 if( !psz_url ) return;
79 if( ( p = strstr( psz_parse, ":/" ) ) )
81 /* we have a protocol */
85 if( p[0] == '/' && p[1] == '/' )
89 url->psz_protocol = strdup( psz_dup );
94 p = strchr( psz_parse, '/' );
95 if( !p || psz_parse < p )
99 /* We have a host[:port] */
100 url->psz_host = strdup( psz_parse );
103 url->psz_host[p - psz_parse] = '\0';
106 if( *url->psz_host == '[' )
109 p2 = strchr( url->psz_host, ']' );
112 p2 = strchr( p2, ':' );
117 p2 = strchr( url->psz_host, ':' );
122 url->i_port = atoi( p2 );
127 /* Now parse psz_path and psz_option */
130 url->psz_path = strdup( psz_parse );
133 p = strchr( url->psz_path, option );
137 url->psz_option = strdup( p );
144 /*****************************************************************************
146 *****************************************************************************
148 *****************************************************************************/
149 static inline void vlc_UrlClean( vlc_url_t *url )
151 if( url->psz_protocol ) free( url->psz_protocol );
152 if( url->psz_host ) free( url->psz_host );
153 if( url->psz_path ) free( url->psz_path );
154 if( url->psz_option ) free( url->psz_option );
156 url->psz_protocol = NULL;
157 url->psz_host = NULL;
159 url->psz_path = NULL;
160 url->psz_option = NULL;
163 /*****************************************************************************
165 *****************************************************************************
166 * perform URL encoding
167 * (you do NOT want to do URL decoding - it is not reversible - do NOT do it)
168 *****************************************************************************/
169 static inline char *vlc_UrlEncode( const char *psz_url )
174 psz_enc = (char *)malloc( 3 * strlen( psz_url ) + 1 );
175 if( psz_enc == NULL )
179 for( in = psz_url; *in; in++ )
183 if( ( c <= 32 ) || ( c == '%' ) || ( c == '?' ) || ( c == '&' )
187 *out++ = ( ( c >> 4 ) >= 0xA ) ? 'A' + ( c >> 4 ) - 0xA
189 *out++ = ( ( c & 0xf ) >= 0xA ) ? 'A' + ( c & 0xf ) - 0xA
197 return (char *)realloc( psz_enc, out - psz_enc );
200 /*****************************************************************************
201 * vlc_UrlIsNotEncoded:
202 *****************************************************************************
203 * check if given string is not a valid URL and must hence be encoded
204 *****************************************************************************/
207 static inline int vlc_UrlIsNotEncoded( const char *psz_url )
211 for( ptr = psz_url; *ptr; ptr++ )
217 if( !isxdigit( ptr[1] ) || !isxdigit( ptr[2] ) )
218 return 1; /* not encoded */
225 return 0; /* looks fine - but maybe it is not encoded */
228 /*****************************************************************************
230 *****************************************************************************
232 *****************************************************************************/
233 static inline char *vlc_b64_encode( char *src )
235 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
237 char *dst = (char *)malloc( strlen( src ) * 4 / 3 + 12 );
240 unsigned i_shift = 0;
246 i_bits = ( i_bits << 8 )|( *src++ );
249 else if( i_shift > 0 )
251 i_bits <<= 6 - i_shift;
260 while( i_shift >= 6 )
263 *dst++ = b64[(i_bits >> i_shift)&0x3f];
272 /* Portable networking layer communication */
273 #define net_OpenTCP(a, b, c) __net_OpenTCP(VLC_OBJECT(a), b, c)
274 VLC_EXPORT( int, __net_OpenTCP, ( vlc_object_t *p_this, const char *psz_host, int i_port ) );
276 #define net_ListenTCP(a, b, c) __net_ListenTCP(VLC_OBJECT(a), b, c)
277 VLC_EXPORT( int *, __net_ListenTCP, ( vlc_object_t *, const char *, int ) );
279 #define net_Accept(a, b, c) __net_Accept(VLC_OBJECT(a), b, c)
280 VLC_EXPORT( int, __net_Accept, ( vlc_object_t *, int *, mtime_t ) );
282 #define net_OpenUDP(a, b, c, d, e ) __net_OpenUDP(VLC_OBJECT(a), b, c, d, e)
283 VLC_EXPORT( int, __net_OpenUDP, ( vlc_object_t *p_this, char *psz_bind, int i_bind, char *psz_server, int i_server ) );
285 VLC_EXPORT( void, net_Close, ( int fd ) );
286 VLC_EXPORT( void, net_ListenClose, ( int *fd ) );
289 /* Functions to read from or write to the networking layer */
290 struct virtual_socket_t
293 int (*pf_recv) ( void *, void *, int );
294 int (*pf_send) ( void *, const void *, int );
297 #define net_Read(a,b,c,d,e,f) __net_Read(VLC_OBJECT(a),b,c,d,e,f)
298 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 ) );
300 #define net_ReadNonBlock(a,b,c,d,e,f) __net_ReadNonBlock(VLC_OBJECT(a),b,c,d,e,f)
301 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 ) );
303 #define net_Select(a,b,c,d,e,f,g) __net_Select(VLC_OBJECT(a),b,c,d,e,f,g)
304 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 ) );
306 #define net_Write(a,b,c,d,e) __net_Write(VLC_OBJECT(a),b,c,d,e)
307 VLC_EXPORT( int, __net_Write, ( vlc_object_t *p_this, int fd, v_socket_t *, uint8_t *p_data, int i_data ) );
309 #define net_Gets(a,b,c) __net_Gets(VLC_OBJECT(a),b,c)
310 VLC_EXPORT( char *, __net_Gets, ( vlc_object_t *p_this, int fd, v_socket_t * ) );
312 VLC_EXPORT( int, net_Printf, ( vlc_object_t *p_this, int fd, v_socket_t *, const char *psz_fmt, ... ) );
314 #define net_vaPrintf(a,b,c,d,e) __net_vaPrintf(VLC_OBJECT(a),b,c,d,e)
315 VLC_EXPORT( int, __net_vaPrintf, ( vlc_object_t *p_this, int fd, v_socket_t *, const char *psz_fmt, va_list args ) );
317 /* Portable network names/addresses resolution layer */
319 /* GAI error codes */
320 # ifndef EAI_BADFLAGS
321 # define EAI_BADFLAGS -1
324 # define EAI_NONAME -2
327 # define EAI_AGAIN -3
333 # define EAI_NODATA -5
336 # define EAI_FAMILY -6
338 # ifndef EAI_SOCKTYPE
339 # define EAI_SOCKTYPE -7
342 # define EAI_SERVICE -8
344 # ifndef EAI_ADDRFAMILY
345 # define EAI_ADDRFAMILY -9
348 # define EAI_MEMORY -10
351 # define EAI_SYSTEM -11
356 # define NI_MAXHOST 1025
357 # define NI_MAXSERV 32
360 # ifndef NI_NUMERICHOST
361 # define NI_NUMERICHOST 0x01
362 # define NI_NUMERICSERV 0x02
363 # define NI_NOFQDN 0x04
364 # define NI_NAMEREQD 0x08
365 # define NI_DGRAM 0x10
368 # ifndef HAVE_STRUCT_ADDRINFO
376 struct sockaddr *ai_addr;
378 struct addrinfo *ai_next;
380 # define AI_PASSIVE 1
381 # define AI_CANONNAME 2
382 # define AI_NUMERICHOST 4
383 # endif /* if !HAVE_STRUCT_ADDRINFO */
385 /*** libidn support ***/
388 # define AI_CANONIDN 0
391 VLC_EXPORT( const char *, vlc_gai_strerror, ( int ) );
392 VLC_EXPORT( int, vlc_getnameinfo, ( vlc_object_t *, const struct sockaddr *, int, char *, int, char *, int, int ) );
393 VLC_EXPORT( int, vlc_getaddrinfo, ( vlc_object_t *, const char *, const char *, const struct addrinfo *, struct addrinfo ** ) );
394 VLC_EXPORT( void, vlc_freeaddrinfo, ( struct addrinfo * ) );