]> git.sesse.net Git - vlc/blob - include/network.h
* modules/control/http.c: Added support for .hosts files detailing hosts
[vlc] / include / network.h
1 /*****************************************************************************
2  * network.h: interface to communicate with network plug-ins
3  *****************************************************************************
4  * Copyright (C) 2002-2005 VideoLAN
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Laurent Aimar <fenrir@via.ecp.fr>
9  *          RĂ©mi Denis-Courmont <rem # videolan.org>
10  *
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.
15  *
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.
20  *
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  *****************************************************************************/
25
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
33 {
34     char * psz_bind_addr;
35     int i_bind_port;
36
37     char * psz_server_addr;
38     int i_server_port;
39
40     int i_ttl;
41
42     /* Return values */
43     int i_handle;
44     size_t i_mtu;
45 };
46
47 typedef struct
48 {
49     char *psz_protocol;
50     char *psz_host;
51     int  i_port;
52
53     char *psz_path;
54
55     char *psz_option;
56 } vlc_url_t;
57
58 /*****************************************************************************
59  * vlc_UrlParse:
60  *****************************************************************************
61  * option : if != 0 then path is split at this char
62  *
63  * format [protocol://][host[:port]]/path[OPTIONoption]
64  *****************************************************************************/
65 static inline void vlc_UrlParse( vlc_url_t *url, char *psz_url, char option )
66 {
67     char *psz_dup = psz_url ? strdup( psz_url ) : 0;
68     char *psz_parse = psz_dup;
69     char *p;
70
71     url->psz_protocol = NULL;
72     url->psz_host     = NULL;
73     url->i_port       = 0;
74     url->psz_path     = NULL;
75     url->psz_option   = NULL;
76
77     if( !psz_url ) return;
78
79     if( ( p  = strstr( psz_parse, ":/" ) ) )
80     {
81         /* we have a protocol */
82
83         /* skip :// */
84         *p++ = '\0';
85         if( p[0] == '/' && p[1] == '/' )
86         {
87             p += 2;
88         }
89         url->psz_protocol = strdup( psz_dup );
90
91         psz_parse = p;
92     }
93
94     p = strchr( psz_parse, '/' );
95     if( !p || psz_parse < p )
96     {
97         char *p2;
98
99         /* We have a host[:port] */
100         url->psz_host = strdup( psz_parse );
101         if( p )
102         {
103             url->psz_host[p - psz_parse] = '\0';
104         }
105
106         if( *url->psz_host == '[' )
107         {
108             /* Ipv6 address */
109             p2 = strchr( url->psz_host, ']' );
110             if( p2 )
111             {
112                 p2 = strchr( p2, ':' );
113             }
114         }
115         else
116         {
117             p2 = strchr( url->psz_host, ':' );
118         }
119         if( p2 )
120         {
121             *p2++ = '\0';
122             url->i_port = atoi( p2 );
123         }
124     }
125     psz_parse = p;
126
127     /* Now parse psz_path and psz_option */
128     if( psz_parse )
129     {
130         url->psz_path = strdup( psz_parse );
131         if( option != '\0' )
132         {
133             p = strchr( url->psz_path, option );
134             if( p )
135             {
136                 *p++ = '\0';
137                 url->psz_option = strdup( p );
138             }
139         }
140     }
141     free( psz_dup );
142 }
143
144 /*****************************************************************************
145  * vlc_UrlClean:
146  *****************************************************************************
147  *
148  *****************************************************************************/
149 static inline void vlc_UrlClean( vlc_url_t *url )
150 {
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 );
155
156     url->psz_protocol = NULL;
157     url->psz_host     = NULL;
158     url->i_port       = 0;
159     url->psz_path     = NULL;
160     url->psz_option   = NULL;
161 }
162
163 /*****************************************************************************
164  * vlc_UrlEncode: 
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 )
170 {
171     char *psz_enc, *out;
172     const char *in;
173
174     psz_enc = (char *)malloc( 3 * strlen( psz_url ) + 1 );
175     if( psz_enc == NULL )
176         return NULL;
177
178     out = psz_enc;
179     for( in = psz_url; *in; in++ )
180     {
181         char c = *in;
182
183         if( ( c <= 32 ) || ( c == '%' ) || ( c == '?' ) || ( c == '&' )
184          || ( c == '+' ) )
185         {
186             *out++ = '%';   
187             *out++ = ( ( c >> 4 ) >= 0xA ) ? 'A' + ( c >> 4 ) - 0xA
188                                            : '0' + ( c >> 4 );
189             *out++ = ( ( c & 0xf ) >= 0xA ) ? 'A' + ( c & 0xf ) - 0xA
190                                            : '0' + ( c & 0xf );
191         }
192         else
193             *out++ = c;
194     }
195     *out++ = '\0';
196
197     return (char *)realloc( psz_enc, out - psz_enc );
198 }
199
200 /*****************************************************************************
201  * vlc_UrlIsNotEncoded:
202  *****************************************************************************
203  * check if given string is not a valid URL and must hence be encoded
204  *****************************************************************************/
205 #include <ctype.h>
206
207 static inline int vlc_UrlIsNotEncoded( const char *psz_url )
208 {
209     const char *ptr;
210
211     for( ptr = psz_url; *ptr; ptr++ )
212     {
213         char c = *ptr;
214
215         if( c == '%' )
216         {
217             if( !isxdigit( ptr[1] ) || !isxdigit( ptr[2] ) )
218                 return 1; /* not encoded */
219             ptr += 2;
220         }
221         else
222         if( c == ' ' )
223             return 1;
224     }
225     return 0; /* looks fine - but maybe it is not encoded */
226 }
227                     
228 /*****************************************************************************
229  * vlc_b64_encode:
230  *****************************************************************************
231  *
232  *****************************************************************************/
233 static inline char *vlc_b64_encode( char *src )
234 {
235     static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
236                                                                                 
237     char *dst = (char *)malloc( strlen( src ) * 4 / 3 + 12 );
238     char *ret = dst;
239     unsigned i_bits = 0;
240     unsigned i_shift = 0;
241                                                                                 
242     for( ;; )
243     {
244         if( *src )
245         {
246             i_bits = ( i_bits << 8 )|( *src++ );
247             i_shift += 8;
248         }
249         else if( i_shift > 0 )
250         {
251            i_bits <<= 6 - i_shift;
252            i_shift = 6;
253         }
254         else
255         {
256             *dst++ = '=';
257             break;
258         }
259                                                                                 
260         while( i_shift >= 6 )
261         {
262             i_shift -= 6;
263             *dst++ = b64[(i_bits >> i_shift)&0x3f];
264         }
265     }
266                                                                                 
267     *dst++ = '\0';
268                                                                                 
269     return ret;
270 }
271
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 ) );
275
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 ) );
278
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 ) );
281
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 ) );
284
285 VLC_EXPORT( void, net_Close, ( int fd ) );
286 VLC_EXPORT( void, net_ListenClose, ( int *fd ) );
287
288
289 /* Functions to read from or write to the networking layer */
290 struct virtual_socket_t
291 {
292     void *p_sys;
293     int (*pf_recv) ( void *, void *, int );
294     int (*pf_send) ( void *, const void *, int );
295 };
296
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 ) );
299
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 ) );
302
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 ) );
305
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 ) );
308
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 * ) );
311
312 VLC_EXPORT( int, net_Printf, ( vlc_object_t *p_this, int fd, v_socket_t *, const char *psz_fmt, ... ) );
313
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 ) );
316
317 #define net_CheckIP(a,b,c,d) __net_CheckIP(VLC_OBJECT(a),b,c,d)
318 VLC_EXPORT( int, __net_CheckIP, ( vlc_object_t *p_this, char *psz_ip, char **ppsz_hosts, int i_hosts ) );
319
320 /* Portable network names/addresses resolution layer */
321
322 /* GAI error codes */
323 # ifndef EAI_BADFLAGS
324 #  define EAI_BADFLAGS -1
325 # endif
326 # ifndef EAI_NONAME
327 #  define EAI_NONAME -2
328 # endif
329 # ifndef EAI_AGAIN
330 #  define EAI_AGAIN -3
331 # endif
332 # ifndef EAI_FAIL
333 #  define EAI_FAIL -4
334 # endif
335 # ifndef EAI_NODATA
336 #  define EAI_NODATA -5
337 # endif
338 # ifndef EAI_FAMILY
339 #  define EAI_FAMILY -6
340 # endif
341 # ifndef EAI_SOCKTYPE
342 #  define EAI_SOCKTYPE -7
343 # endif
344 # ifndef EAI_SERVICE
345 #  define EAI_SERVICE -8
346 # endif
347 # ifndef EAI_ADDRFAMILY
348 #  define EAI_ADDRFAMILY -9
349 # endif
350 # ifndef EAI_MEMORY
351 #  define EAI_MEMORY -10
352 # endif
353 # ifndef EAI_SYSTEM
354 #  define EAI_SYSTEM -11
355 # endif
356
357
358 # ifndef NI_MAXHOST
359 #  define NI_MAXHOST 1025
360 #  define NI_MAXSERV 32
361 # endif
362
363 # ifndef NI_NUMERICHOST
364 #  define NI_NUMERICHOST 0x01
365 #  define NI_NUMERICSERV 0x02
366 #  define NI_NOFQDN      0x04
367 #  define NI_NAMEREQD    0x08
368 #  define NI_DGRAM       0x10
369 # endif
370
371 # ifndef HAVE_STRUCT_ADDRINFO
372 struct addrinfo
373 {
374     int ai_flags;
375     int ai_family;
376     int ai_socktype;
377     int ai_protocol;
378     size_t ai_addrlen;
379     struct sockaddr *ai_addr;
380     char *ai_canonname;
381     struct addrinfo *ai_next;
382 };
383 #  define AI_PASSIVE     1
384 #  define AI_CANONNAME   2
385 #  define AI_NUMERICHOST 4
386 # endif /* if !HAVE_STRUCT_ADDRINFO */
387
388 /*** libidn support ***/
389 # ifndef AI_IDN
390 #  define AI_IDN      0
391 #  define AI_CANONIDN 0
392 # endif
393
394 VLC_EXPORT( const char *, vlc_gai_strerror, ( int ) );
395 VLC_EXPORT( int, vlc_getnameinfo, ( vlc_object_t *, const struct sockaddr *, int, char *, int, char *, int, int ) );
396 VLC_EXPORT( int, vlc_getaddrinfo, ( vlc_object_t *, const char *, const char *, const struct addrinfo *, struct addrinfo ** ) );
397 VLC_EXPORT( void, vlc_freeaddrinfo, ( struct addrinfo * ) );
398
399 #endif