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 <courmisch # via.ecp.fr>
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
39 char * psz_server_addr;
52 #define NETWORK_TCP_PASSIVE 3
66 /*****************************************************************************
68 *****************************************************************************
69 * option : if != 0 then path is split at this char
71 * format [protocol://][host[:port]]/path[OPTIONoption]
72 *****************************************************************************/
73 static inline void vlc_UrlParse( vlc_url_t *url, char *psz_url, char option )
75 char *psz_dup = psz_url ? strdup( psz_url ) : 0;
76 char *psz_parse = psz_dup;
79 url->psz_protocol = NULL;
83 url->psz_option = NULL;
85 if( !psz_url ) return;
87 if( ( p = strstr( psz_parse, ":/" ) ) )
89 /* we have a protocol */
93 if( p[0] == '/' && p[1] == '/' )
97 url->psz_protocol = strdup( psz_dup );
102 p = strchr( psz_parse, '/' );
103 if( !p || psz_parse < p )
107 /* We have a host[:port] */
108 url->psz_host = strdup( psz_parse );
111 url->psz_host[p - psz_parse] = '\0';
114 if( *url->psz_host == '[' )
117 p2 = strchr( url->psz_host, ']' );
120 p2 = strchr( p2, ':' );
125 p2 = strchr( url->psz_host, ':' );
130 url->i_port = atoi( p2 );
135 /* Now parse psz_path and psz_option */
138 url->psz_path = strdup( psz_parse );
141 p = strchr( url->psz_path, option );
145 url->psz_option = strdup( p );
152 /*****************************************************************************
154 *****************************************************************************
156 *****************************************************************************/
157 static inline void vlc_UrlClean( vlc_url_t *url )
159 if( url->psz_protocol ) free( url->psz_protocol );
160 if( url->psz_host ) free( url->psz_host );
161 if( url->psz_path ) free( url->psz_path );
162 if( url->psz_option ) free( url->psz_option );
164 url->psz_protocol = NULL;
165 url->psz_host = NULL;
167 url->psz_path = NULL;
168 url->psz_option = NULL;
171 /*****************************************************************************
173 *****************************************************************************
174 * perform URL encoding
175 * (you do NOT want to do URL decoding - it is not reversible - do NOT do it)
176 *****************************************************************************/
177 static inline char *vlc_UrlEncode( const char *psz_url )
182 psz_enc = (char *)malloc( 3 * strlen( psz_url ) + 1 );
183 if( psz_enc == NULL )
187 for( in = psz_url; *in; in++ )
191 if( ( c <= 32 ) || ( c == '%' ) || ( c == '?' ) || ( c == '&' )
195 *out++ = ( ( c >> 4 ) >= 0xA ) ? 'A' + ( c >> 4 ) - 0xA
197 *out++ = ( ( c & 0xf ) >= 0xA ) ? 'A' + ( c & 0xf ) - 0xA
205 return (char *)realloc( psz_enc, out - psz_enc );
208 /*****************************************************************************
209 * vlc_UrlIsNotEncoded:
210 *****************************************************************************
211 * check if given string is not a valid URL and must hence be encoded
212 *****************************************************************************/
215 static inline int vlc_UrlIsNotEncoded( const char *psz_url )
219 for( ptr = psz_url; *ptr; ptr++ )
225 if( !isxdigit( ptr[1] ) || !isxdigit( ptr[2] ) )
226 return 1; /* not encoded */
233 return 0; /* looks fine - but maybe it is not encoded */
236 /*****************************************************************************
238 *****************************************************************************
240 *****************************************************************************/
241 static inline char *vlc_b64_encode( char *src )
243 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
245 char *dst = (char *)malloc( strlen( src ) * 4 / 3 + 12 );
248 unsigned i_shift = 0;
254 i_bits = ( i_bits << 8 )|( *src++ );
257 else if( i_shift > 0 )
259 i_bits <<= 6 - i_shift;
268 while( i_shift >= 6 )
271 *dst++ = b64[(i_bits >> i_shift)&0x3f];
280 VLC_EXPORT( int, net_ConvertIPv4, ( uint32_t *p_addr, const char * psz_address ) );
282 /* Portable networking layer communication */
283 #define net_OpenTCP(a, b, c) __net_OpenTCP(VLC_OBJECT(a), b, c)
284 VLC_EXPORT( int, __net_OpenTCP, ( vlc_object_t *p_this, const char *psz_host, int i_port ) );
286 #define net_ListenTCP(a, b, c) __net_ListenTCP(VLC_OBJECT(a), b, c)
287 VLC_EXPORT( int, __net_ListenTCP, ( vlc_object_t *p_this, char *psz_localaddr, int i_port ) );
289 #define net_Accept(a, b, c) __net_Accept(VLC_OBJECT(a), b, c)
290 VLC_EXPORT( int, __net_Accept, ( vlc_object_t *p_this, int fd_listen, mtime_t i_wait ) );
292 #define net_OpenUDP(a, b, c, d, e ) __net_OpenUDP(VLC_OBJECT(a), b, c, d, e)
293 VLC_EXPORT( int, __net_OpenUDP, ( vlc_object_t *p_this, char *psz_bind, int i_bind, char *psz_server, int i_server ) );
295 VLC_EXPORT( void, net_Close, ( int fd ) );
298 /* Functions to read from or write to the networking layer */
299 struct virtual_socket_t
302 int (*pf_recv) ( void *, void *, int );
303 int (*pf_send) ( void *, const void *, int );
306 #define net_Read(a,b,c,d,e,f) __net_Read(VLC_OBJECT(a),b,c,d,e,f)
307 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 ) );
309 #define net_ReadNonBlock(a,b,c,d,e,f) __net_ReadNonBlock(VLC_OBJECT(a),b,c,d,e,f)
310 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 ) );
312 #define net_Select(a,b,c,d,e,f,g) __net_Select(VLC_OBJECT(a),b,c,d,e,f,g)
313 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 ) );
315 #define net_Write(a,b,c,d,e) __net_Write(VLC_OBJECT(a),b,c,d,e)
316 VLC_EXPORT( int, __net_Write, ( vlc_object_t *p_this, int fd, v_socket_t *, uint8_t *p_data, int i_data ) );
318 #define net_Gets(a,b,c) __net_Gets(VLC_OBJECT(a),b,c)
319 VLC_EXPORT( char *, __net_Gets, ( vlc_object_t *p_this, int fd, v_socket_t * ) );
321 VLC_EXPORT( int, net_Printf, ( vlc_object_t *p_this, int fd, v_socket_t *, const char *psz_fmt, ... ) );
323 #define net_vaPrintf(a,b,c,d,e) __net_vaPrintf(VLC_OBJECT(a),b,c,d,e)
324 VLC_EXPORT( int, __net_vaPrintf, ( vlc_object_t *p_this, int fd, v_socket_t *, const char *psz_fmt, va_list args ) );