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>
34 #elif HAVE_SYS_SOCKET_H
35 # include <sys/socket.h>
38 /*****************************************************************************
39 * network_socket_t: structure passed to a network plug-in to define the
40 * kind of socket we want
41 *****************************************************************************/
42 struct network_socket_t
47 char * psz_server_addr;
69 char *psz_buffer; /* to be freed */
72 /*****************************************************************************
74 *****************************************************************************
75 * option : if != 0 then path is split at this char
77 * format [protocol://[login[:password]@]][host[:port]]/path[OPTIONoption]
78 *****************************************************************************/
79 static inline void vlc_UrlParse( vlc_url_t *url, const char *psz_url,
86 url->psz_protocol = NULL;
87 url->psz_username = NULL;
88 url->psz_password = NULL;
92 url->psz_option = NULL;
96 url->psz_buffer = NULL;
99 url->psz_buffer = psz_parse = psz_dup = strdup( psz_url );
101 p = strstr( psz_parse, ":/" );
104 /* we have a protocol */
110 url->psz_protocol = psz_parse;
113 p = strchr( psz_parse, '@' );
116 /* We have a login */
117 url->psz_username = psz_parse;
120 psz_parse = strchr( psz_parse, ':' );
121 if( psz_parse != NULL )
123 /* We have a password */
125 url->psz_password = psz_parse;
132 p = strchr( psz_parse, '/' );
133 if( !p || psz_parse < p )
137 /* We have a host[:port] */
138 url->psz_host = strdup( psz_parse );
141 url->psz_host[p - psz_parse] = '\0';
144 if( *url->psz_host == '[' )
147 p2 = strchr( url->psz_host, ']' );
150 p2 = strchr( p2, ':' );
155 p2 = strchr( url->psz_host, ':' );
160 url->i_port = atoi( p2 );
165 /* Now parse psz_path and psz_option */
168 url->psz_path = psz_parse;
171 p = strchr( url->psz_path, option );
181 /*****************************************************************************
183 *****************************************************************************
185 *****************************************************************************/
186 static inline void vlc_UrlClean( vlc_url_t *url )
188 if( url->psz_buffer ) free( url->psz_buffer );
189 if( url->psz_host ) free( url->psz_host );
191 url->psz_protocol = NULL;
192 url->psz_username = NULL;
193 url->psz_password = NULL;
194 url->psz_host = NULL;
196 url->psz_path = NULL;
197 url->psz_option = NULL;
199 url->psz_buffer = NULL;
202 /*****************************************************************************
204 *****************************************************************************
205 * perform URL encoding
206 * (you do NOT want to do URL decoding - it is not reversible - do NOT do it)
207 *****************************************************************************/
208 static inline char *vlc_UrlEncode( const char *psz_url )
213 psz_enc = (char *)malloc( 3 * strlen( psz_url ) + 1 );
214 if( psz_enc == NULL )
218 for( in = psz_url; *in; in++ )
222 if( ( c <= 32 ) || ( c == '%' ) || ( c == '?' ) || ( c == '&' )
226 *out++ = ( ( c >> 4 ) >= 0xA ) ? 'A' + ( c >> 4 ) - 0xA
228 *out++ = ( ( c & 0xf ) >= 0xA ) ? 'A' + ( c & 0xf ) - 0xA
236 return (char *)realloc( psz_enc, out - psz_enc );
239 /*****************************************************************************
240 * vlc_UrlIsNotEncoded:
241 *****************************************************************************
242 * check if given string is not a valid URL and must hence be encoded
243 *****************************************************************************/
246 static inline int vlc_UrlIsNotEncoded( const char *psz_url )
250 for( ptr = psz_url; *ptr; ptr++ )
256 if( !isxdigit( ptr[1] ) || !isxdigit( ptr[2] ) )
257 return 1; /* not encoded */
264 return 0; /* looks fine - but maybe it is not encoded */
267 /*****************************************************************************
269 *****************************************************************************
271 *****************************************************************************/
272 static inline char *vlc_b64_encode( char *src )
274 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
276 char *dst = (char *)malloc( strlen( src ) * 4 / 3 + 12 );
279 unsigned i_shift = 0;
285 i_bits = ( i_bits << 8 )|( *src++ );
288 else if( i_shift > 0 )
290 i_bits <<= 6 - i_shift;
299 while( i_shift >= 6 )
302 *dst++ = b64[(i_bits >> i_shift)&0x3f];
311 /* Portable networking layer communication */
312 #define net_OpenTCP(a, b, c) __net_OpenTCP(VLC_OBJECT(a), b, c)
313 VLC_EXPORT( int, __net_OpenTCP, ( vlc_object_t *p_this, const char *psz_host, int i_port ) );
315 #define net_ListenTCP(a, b, c) __net_ListenTCP(VLC_OBJECT(a), b, c)
316 VLC_EXPORT( int *, __net_ListenTCP, ( vlc_object_t *, const char *, int ) );
318 #define net_Accept(a, b, c) __net_Accept(VLC_OBJECT(a), b, c)
319 VLC_EXPORT( int, __net_Accept, ( vlc_object_t *, int *, mtime_t ) );
321 #define net_OpenUDP(a, b, c, d, e ) __net_OpenUDP(VLC_OBJECT(a), b, c, d, e)
322 VLC_EXPORT( int, __net_OpenUDP, ( vlc_object_t *p_this, char *psz_bind, int i_bind, char *psz_server, int i_server ) );
324 VLC_EXPORT( void, net_Close, ( int fd ) );
325 VLC_EXPORT( void, net_ListenClose, ( int *fd ) );
328 /* Functions to read from or write to the networking layer */
329 struct virtual_socket_t
332 int (*pf_recv) ( void *, void *, int );
333 int (*pf_send) ( void *, const void *, int );
336 #define net_Read(a,b,c,d,e,f) __net_Read(VLC_OBJECT(a),b,c,d,e,f)
337 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 ) );
339 #define net_ReadNonBlock(a,b,c,d,e,f) __net_ReadNonBlock(VLC_OBJECT(a),b,c,d,e,f)
340 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 ) );
342 #define net_Select(a,b,c,d,e,f,g) __net_Select(VLC_OBJECT(a),b,c,d,e,f,g)
343 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 ) );
345 #define net_Write(a,b,c,d,e) __net_Write(VLC_OBJECT(a),b,c,d,e)
346 VLC_EXPORT( int, __net_Write, ( vlc_object_t *p_this, int fd, v_socket_t *, uint8_t *p_data, int i_data ) );
348 #define net_Gets(a,b,c) __net_Gets(VLC_OBJECT(a),b,c)
349 VLC_EXPORT( char *, __net_Gets, ( vlc_object_t *p_this, int fd, v_socket_t * ) );
351 VLC_EXPORT( int, net_Printf, ( vlc_object_t *p_this, int fd, v_socket_t *, const char *psz_fmt, ... ) );
353 #define net_vaPrintf(a,b,c,d,e) __net_vaPrintf(VLC_OBJECT(a),b,c,d,e)
354 VLC_EXPORT( int, __net_vaPrintf, ( vlc_object_t *p_this, int fd, v_socket_t *, const char *psz_fmt, va_list args ) );
356 #define net_GetSockAddress(a,b,c,d) __net_GetAddress(VLC_OBJECT(a),VLC_FALSE,b,c,d)
357 #define net_GetPeerAddress(a,b,c,d) __net_GetAddress(VLC_OBJECT(a),VLC_TRUE,b,c,d)
358 VLC_EXPORT( int, __net_GetAddress, ( vlc_object_t *p_this, vlc_bool_t peer, int fd, char *address, int *port ) );
360 /*****************************************************************************
362 *****************************************************************************
363 * Wrappers for shutdown()
364 *****************************************************************************/
365 #if defined (SHUT_WR)
366 /* the standard way */
367 # define net_StopSend( fd ) (void)shutdown( fd, SHUT_WR )
368 # define net_StopRecv( fd ) (void)shutdown( fd, SHUT_RD )
369 #elif defined (SD_SEND)
370 /* the Microsoft seemingly-purposedly-different-for-the-sake-of-it way */
371 # define net_StopSend( fd ) (void)shutdown( fd, SD_SEND )
372 # define net_StopRecv( fd ) (void)shutdown( fd, SD_RECEIVE )
374 # warning FIXME: implement shutdown on your platform!
375 # define net_StopSend( fd ) (void)0
376 # define net_StopRecv( fd ) (void)0
379 /* Portable network names/addresses resolution layer */
381 /* GAI error codes */
382 # ifndef EAI_BADFLAGS
383 # define EAI_BADFLAGS -1
386 # define EAI_NONAME -2
389 # define EAI_AGAIN -3
395 # define EAI_NODATA -5
398 # define EAI_FAMILY -6
400 # ifndef EAI_SOCKTYPE
401 # define EAI_SOCKTYPE -7
404 # define EAI_SERVICE -8
406 # ifndef EAI_ADDRFAMILY
407 # define EAI_ADDRFAMILY -9
410 # define EAI_MEMORY -10
413 # define EAI_SYSTEM -11
418 # define NI_MAXHOST 1025
419 # define NI_MAXSERV 32
421 # define NI_MAXNUMERICHOST 48
423 # ifndef NI_NUMERICHOST
424 # define NI_NUMERICHOST 0x01
425 # define NI_NUMERICSERV 0x02
426 # define NI_NOFQDN 0x04
427 # define NI_NAMEREQD 0x08
428 # define NI_DGRAM 0x10
431 # ifndef HAVE_STRUCT_ADDRINFO
439 struct sockaddr *ai_addr;
441 struct addrinfo *ai_next;
443 # define AI_PASSIVE 1
444 # define AI_CANONNAME 2
445 # define AI_NUMERICHOST 4
446 # endif /* if !HAVE_STRUCT_ADDRINFO */
448 /*** libidn support ***/
451 # define AI_CANONIDN 0
454 VLC_EXPORT( const char *, vlc_gai_strerror, ( int ) );
455 VLC_EXPORT( int, vlc_getnameinfo, ( const struct sockaddr *, int, char *, int, int *, int ) );
456 VLC_EXPORT( int, vlc_getaddrinfo, ( vlc_object_t *, const char *, int, const struct addrinfo *, struct addrinfo ** ) );
457 VLC_EXPORT( void, vlc_freeaddrinfo, ( struct addrinfo * ) );