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;
59 char *psz_buffer; /* to be freed */
62 /*****************************************************************************
64 *****************************************************************************
65 * option : if != 0 then path is split at this char
67 * format [protocol://[login[:password]@]][host[:port]]/path[OPTIONoption]
68 *****************************************************************************/
69 static inline void vlc_UrlParse( vlc_url_t *url, const char *psz_url,
76 url->psz_protocol = NULL;
77 url->psz_username = NULL;
78 url->psz_password = NULL;
82 url->psz_option = NULL;
86 url->psz_buffer = NULL;
89 url->psz_buffer = psz_parse = psz_dup = strdup( psz_url );
91 p = strstr( psz_parse, ":/" );
94 /* we have a protocol */
100 url->psz_protocol = psz_parse;
103 p = strchr( psz_parse, '@' );
106 /* We have a login */
107 url->psz_username = psz_parse;
110 psz_parse = strchr( psz_parse, ':' );
111 if( psz_parse != NULL )
113 /* We have a password */
115 url->psz_password = psz_parse;
122 p = strchr( psz_parse, '/' );
123 if( !p || psz_parse < p )
127 /* We have a host[:port] */
128 url->psz_host = strdup( psz_parse );
131 url->psz_host[p - psz_parse] = '\0';
134 if( *url->psz_host == '[' )
137 p2 = strchr( url->psz_host, ']' );
140 p2 = strchr( p2, ':' );
145 p2 = strchr( url->psz_host, ':' );
150 url->i_port = atoi( p2 );
155 /* Now parse psz_path and psz_option */
158 url->psz_path = psz_parse;
161 p = strchr( url->psz_path, option );
171 /*****************************************************************************
173 *****************************************************************************
175 *****************************************************************************/
176 static inline void vlc_UrlClean( vlc_url_t *url )
178 if( url->psz_buffer ) free( url->psz_buffer );
179 if( url->psz_host ) free( url->psz_host );
181 url->psz_protocol = NULL;
182 url->psz_username = NULL;
183 url->psz_password = NULL;
184 url->psz_host = NULL;
186 url->psz_path = NULL;
187 url->psz_option = NULL;
189 url->psz_buffer = NULL;
192 /*****************************************************************************
194 *****************************************************************************
195 * perform URL encoding
196 * (you do NOT want to do URL decoding - it is not reversible - do NOT do it)
197 *****************************************************************************/
198 static inline char *vlc_UrlEncode( const char *psz_url )
203 psz_enc = (char *)malloc( 3 * strlen( psz_url ) + 1 );
204 if( psz_enc == NULL )
208 for( in = psz_url; *in; in++ )
212 if( ( c <= 32 ) || ( c == '%' ) || ( c == '?' ) || ( c == '&' )
216 *out++ = ( ( c >> 4 ) >= 0xA ) ? 'A' + ( c >> 4 ) - 0xA
218 *out++ = ( ( c & 0xf ) >= 0xA ) ? 'A' + ( c & 0xf ) - 0xA
226 return (char *)realloc( psz_enc, out - psz_enc );
229 /*****************************************************************************
230 * vlc_UrlIsNotEncoded:
231 *****************************************************************************
232 * check if given string is not a valid URL and must hence be encoded
233 *****************************************************************************/
236 static inline int vlc_UrlIsNotEncoded( const char *psz_url )
240 for( ptr = psz_url; *ptr; ptr++ )
246 if( !isxdigit( ptr[1] ) || !isxdigit( ptr[2] ) )
247 return 1; /* not encoded */
254 return 0; /* looks fine - but maybe it is not encoded */
257 /*****************************************************************************
259 *****************************************************************************
261 *****************************************************************************/
262 static inline char *vlc_b64_encode( char *src )
264 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
266 char *dst = (char *)malloc( strlen( src ) * 4 / 3 + 12 );
269 unsigned i_shift = 0;
275 i_bits = ( i_bits << 8 )|( *src++ );
278 else if( i_shift > 0 )
280 i_bits <<= 6 - i_shift;
289 while( i_shift >= 6 )
292 *dst++ = b64[(i_bits >> i_shift)&0x3f];
301 /* Portable networking layer communication */
302 #define net_OpenTCP(a, b, c) __net_OpenTCP(VLC_OBJECT(a), b, c)
303 VLC_EXPORT( int, __net_OpenTCP, ( vlc_object_t *p_this, const char *psz_host, int i_port ) );
305 #define net_ListenTCP(a, b, c) __net_ListenTCP(VLC_OBJECT(a), b, c)
306 VLC_EXPORT( int *, __net_ListenTCP, ( vlc_object_t *, const char *, int ) );
308 #define net_Accept(a, b, c) __net_Accept(VLC_OBJECT(a), b, c)
309 VLC_EXPORT( int, __net_Accept, ( vlc_object_t *, int *, mtime_t ) );
311 #define net_OpenUDP(a, b, c, d, e ) __net_OpenUDP(VLC_OBJECT(a), b, c, d, e)
312 VLC_EXPORT( int, __net_OpenUDP, ( vlc_object_t *p_this, char *psz_bind, int i_bind, char *psz_server, int i_server ) );
314 VLC_EXPORT( void, net_Close, ( int fd ) );
315 VLC_EXPORT( void, net_ListenClose, ( int *fd ) );
318 /* Functions to read from or write to the networking layer */
319 struct virtual_socket_t
322 int (*pf_recv) ( void *, void *, int );
323 int (*pf_send) ( void *, const void *, int );
326 #define net_Read(a,b,c,d,e,f) __net_Read(VLC_OBJECT(a),b,c,d,e,f)
327 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 ) );
329 #define net_ReadNonBlock(a,b,c,d,e,f) __net_ReadNonBlock(VLC_OBJECT(a),b,c,d,e,f)
330 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 ) );
332 #define net_Select(a,b,c,d,e,f,g) __net_Select(VLC_OBJECT(a),b,c,d,e,f,g)
333 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 ) );
335 #define net_Write(a,b,c,d,e) __net_Write(VLC_OBJECT(a),b,c,d,e)
336 VLC_EXPORT( int, __net_Write, ( vlc_object_t *p_this, int fd, v_socket_t *, uint8_t *p_data, int i_data ) );
338 #define net_Gets(a,b,c) __net_Gets(VLC_OBJECT(a),b,c)
339 VLC_EXPORT( char *, __net_Gets, ( vlc_object_t *p_this, int fd, v_socket_t * ) );
341 VLC_EXPORT( int, net_Printf, ( vlc_object_t *p_this, int fd, v_socket_t *, const char *psz_fmt, ... ) );
343 #define net_vaPrintf(a,b,c,d,e) __net_vaPrintf(VLC_OBJECT(a),b,c,d,e)
344 VLC_EXPORT( int, __net_vaPrintf, ( vlc_object_t *p_this, int fd, v_socket_t *, const char *psz_fmt, va_list args ) );
346 /*****************************************************************************
348 *****************************************************************************
349 * Wrappers for shutdown()
350 *****************************************************************************/
351 #if defined (SHUT_WR)
352 /* the standard way */
353 # define net_StopSend( fd ) shutdown( fd, SHUT_WR )
354 # define net_StopRecv( fd ) shutdown( fd, SHUT_RD )
355 #elif defined (SD_SEND)
356 /* the Microsoft seemingly-purposedly-different-for-the-sake-of-it way */
357 # define net_StopSend( fd ) shutdown( fd, SD_SEND )
358 # define net_StopRecv( fd ) shutdown( fd, SD_RECEIVE )
360 # warning FIXME: implement shutdown on your platform!
361 # define net_StopSend( fd ) void(0)
362 # define net_StopRecv( fd ) void(0)
365 #define net_CheckIP(a,b,c,d) __net_CheckIP(VLC_OBJECT(a),b,c,d)
366 VLC_EXPORT( int, __net_CheckIP, ( vlc_object_t *p_this, char *psz_ip, char **ppsz_hosts, int i_hosts ) );
368 /* Portable network names/addresses resolution layer */
370 /* GAI error codes */
371 # ifndef EAI_BADFLAGS
372 # define EAI_BADFLAGS -1
375 # define EAI_NONAME -2
378 # define EAI_AGAIN -3
384 # define EAI_NODATA -5
387 # define EAI_FAMILY -6
389 # ifndef EAI_SOCKTYPE
390 # define EAI_SOCKTYPE -7
393 # define EAI_SERVICE -8
395 # ifndef EAI_ADDRFAMILY
396 # define EAI_ADDRFAMILY -9
399 # define EAI_MEMORY -10
402 # define EAI_SYSTEM -11
407 # define NI_MAXHOST 1025
408 # define NI_MAXSERV 32
410 # define NI_MAXNUMERICHOST 48
412 # ifndef NI_NUMERICHOST
413 # define NI_NUMERICHOST 0x01
414 # define NI_NUMERICSERV 0x02
415 # define NI_NOFQDN 0x04
416 # define NI_NAMEREQD 0x08
417 # define NI_DGRAM 0x10
420 # ifndef HAVE_STRUCT_ADDRINFO
428 struct sockaddr *ai_addr;
430 struct addrinfo *ai_next;
432 # define AI_PASSIVE 1
433 # define AI_CANONNAME 2
434 # define AI_NUMERICHOST 4
435 # endif /* if !HAVE_STRUCT_ADDRINFO */
437 /*** libidn support ***/
440 # define AI_CANONIDN 0
443 VLC_EXPORT( const char *, vlc_gai_strerror, ( int ) );
444 VLC_EXPORT( int, vlc_getnameinfo, ( const struct sockaddr *, int, char *, int, int *, int ) );
445 VLC_EXPORT( int, vlc_getaddrinfo, ( vlc_object_t *, const char *, int, const struct addrinfo *, struct addrinfo ** ) );
446 VLC_EXPORT( void, vlc_freeaddrinfo, ( struct addrinfo * ) );