]> git.sesse.net Git - vlc/blob - include/network.h
Better ACL API (full rewrite)
[vlc] / include / network.h
1 /*****************************************************************************
2  * network.h: interface to communicate with network plug-ins
3  *****************************************************************************
4  * Copyright (C) 2002-2005 the VideoLAN team
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 #if defined( UNDER_CE )
30 #   include <winsock.h>
31 #elif defined( WIN32 )
32 #   include <winsock2.h>
33 #elif HAVE_SYS_SOCKET_H
34 #   include <sys/socket.h>
35 #endif
36
37 /*****************************************************************************
38  * network_socket_t: structure passed to a network plug-in to define the
39  *                   kind of socket we want
40  *****************************************************************************/
41 struct network_socket_t
42 {
43     char * psz_bind_addr;
44     int i_bind_port;
45
46     char * psz_server_addr;
47     int i_server_port;
48
49     int i_ttl;
50
51     /* Return values */
52     int i_handle;
53     size_t i_mtu;
54 };
55
56 typedef struct
57 {
58     char *psz_protocol;
59     char *psz_username;
60     char *psz_password;
61     char *psz_host;
62     int  i_port;
63
64     char *psz_path;
65
66     char *psz_option;
67     
68     char *psz_buffer; /* to be freed */
69 } vlc_url_t;
70
71 /*****************************************************************************
72  * vlc_UrlParse:
73  *****************************************************************************
74  * option : if != 0 then path is split at this char
75  *
76  * format [protocol://[login[:password]@]][host[:port]]/path[OPTIONoption]
77  *****************************************************************************/
78 static inline void vlc_UrlParse( vlc_url_t *url, const char *psz_url,
79                                  char option )
80 {
81     char *psz_dup;
82     char *psz_parse;
83     char *p;
84
85     url->psz_protocol = NULL;
86     url->psz_username = NULL;
87     url->psz_password = NULL;
88     url->psz_host     = NULL;
89     url->i_port       = 0;
90     url->psz_path     = NULL;
91     url->psz_option   = NULL;
92     
93     if( psz_url == NULL )
94     {
95         url->psz_buffer = NULL;
96         return;
97     }
98     url->psz_buffer = psz_parse = psz_dup = strdup( psz_url );
99
100     p  = strstr( psz_parse, ":/" );
101     if( p != NULL )
102     {
103         /* we have a protocol */
104
105         /* skip :// */
106         *p++ = '\0';
107         if( p[1] == '/' )
108             p += 2;
109         url->psz_protocol = psz_parse;
110
111         psz_parse = p;
112         p = strchr( psz_parse, '@' );
113         if( p != NULL )
114         {
115             /* We have a login */
116             url->psz_username = psz_parse;
117             *p++ = '\0';
118
119             psz_parse = strchr( psz_parse, ':' );
120             if( psz_parse != NULL )
121             {
122                 /* We have a password */
123                 *psz_parse++ = '\0';
124                 url->psz_password = psz_parse;
125             }
126
127             psz_parse = p;
128         }
129     }
130
131     p = strchr( psz_parse, '/' );
132     if( !p || psz_parse < p )
133     {
134         char *p2;
135
136         /* We have a host[:port] */
137         url->psz_host = strdup( psz_parse );
138         if( p )
139         {
140             url->psz_host[p - psz_parse] = '\0';
141         }
142
143         if( *url->psz_host == '[' )
144         {
145             /* Ipv6 address */
146             p2 = strchr( url->psz_host, ']' );
147             if( p2 )
148             {
149                 p2 = strchr( p2, ':' );
150             }
151         }
152         else
153         {
154             p2 = strchr( url->psz_host, ':' );
155         }
156         if( p2 )
157         {
158             *p2++ = '\0';
159             url->i_port = atoi( p2 );
160         }
161     }
162     psz_parse = p;
163
164     /* Now parse psz_path and psz_option */
165     if( psz_parse )
166     {
167         url->psz_path = psz_parse;
168         if( option != '\0' )
169         {
170             p = strchr( url->psz_path, option );
171             if( p )
172             {
173                 *p++ = '\0';
174                 url->psz_option = p;
175             }
176         }
177     }
178 }
179
180 /*****************************************************************************
181  * vlc_UrlClean:
182  *****************************************************************************
183  *
184  *****************************************************************************/
185 static inline void vlc_UrlClean( vlc_url_t *url )
186 {
187     if( url->psz_buffer ) free( url->psz_buffer );
188     if( url->psz_host )   free( url->psz_host );
189
190     url->psz_protocol = NULL;
191     url->psz_username = NULL;
192     url->psz_password = NULL;
193     url->psz_host     = NULL;
194     url->i_port       = 0;
195     url->psz_path     = NULL;
196     url->psz_option   = NULL;
197
198     url->psz_buffer   = NULL;
199 }
200
201 /*****************************************************************************
202  * vlc_UrlEncode: 
203  *****************************************************************************
204  * perform URL encoding
205  * (you do NOT want to do URL decoding - it is not reversible - do NOT do it)
206  *****************************************************************************/
207 static inline char *vlc_UrlEncode( const char *psz_url )
208 {
209     char *psz_enc, *out;
210     const char *in;
211
212     psz_enc = (char *)malloc( 3 * strlen( psz_url ) + 1 );
213     if( psz_enc == NULL )
214         return NULL;
215
216     out = psz_enc;
217     for( in = psz_url; *in; in++ )
218     {
219         char c = *in;
220
221         if( ( c <= 32 ) || ( c == '%' ) || ( c == '?' ) || ( c == '&' )
222          || ( c == '+' ) )
223         {
224             *out++ = '%';   
225             *out++ = ( ( c >> 4 ) >= 0xA ) ? 'A' + ( c >> 4 ) - 0xA
226                                            : '0' + ( c >> 4 );
227             *out++ = ( ( c & 0xf ) >= 0xA ) ? 'A' + ( c & 0xf ) - 0xA
228                                            : '0' + ( c & 0xf );
229         }
230         else
231             *out++ = c;
232     }
233     *out++ = '\0';
234
235     return (char *)realloc( psz_enc, out - psz_enc );
236 }
237
238 /*****************************************************************************
239  * vlc_UrlIsNotEncoded:
240  *****************************************************************************
241  * check if given string is not a valid URL and must hence be encoded
242  *****************************************************************************/
243 #include <ctype.h>
244
245 static inline int vlc_UrlIsNotEncoded( const char *psz_url )
246 {
247     const char *ptr;
248
249     for( ptr = psz_url; *ptr; ptr++ )
250     {
251         char c = *ptr;
252
253         if( c == '%' )
254         {
255             if( !isxdigit( ptr[1] ) || !isxdigit( ptr[2] ) )
256                 return 1; /* not encoded */
257             ptr += 2;
258         }
259         else
260         if( c == ' ' )
261             return 1;
262     }
263     return 0; /* looks fine - but maybe it is not encoded */
264 }
265                     
266 /*****************************************************************************
267  * vlc_b64_encode:
268  *****************************************************************************
269  *
270  *****************************************************************************/
271 static inline char *vlc_b64_encode( char *src )
272 {
273     static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
274                                                                                 
275     char *dst = (char *)malloc( strlen( src ) * 4 / 3 + 12 );
276     char *ret = dst;
277     unsigned i_bits = 0;
278     unsigned i_shift = 0;
279                                                                                 
280     for( ;; )
281     {
282         if( *src )
283         {
284             i_bits = ( i_bits << 8 )|( *src++ );
285             i_shift += 8;
286         }
287         else if( i_shift > 0 )
288         {
289            i_bits <<= 6 - i_shift;
290            i_shift = 6;
291         }
292         else
293         {
294             *dst++ = '=';
295             break;
296         }
297                                                                                 
298         while( i_shift >= 6 )
299         {
300             i_shift -= 6;
301             *dst++ = b64[(i_bits >> i_shift)&0x3f];
302         }
303     }
304                                                                                 
305     *dst++ = '\0';
306                                                                                 
307     return ret;
308 }
309
310 /* Portable networking layer communication */
311 #define net_OpenTCP(a, b, c) __net_OpenTCP(VLC_OBJECT(a), b, c)
312 VLC_EXPORT( int, __net_OpenTCP, ( vlc_object_t *p_this, const char *psz_host, int i_port ) );
313
314 #define net_ListenTCP(a, b, c) __net_ListenTCP(VLC_OBJECT(a), b, c)
315 VLC_EXPORT( int *, __net_ListenTCP, ( vlc_object_t *, const char *, int ) );
316
317 #define net_Accept(a, b, c) __net_Accept(VLC_OBJECT(a), b, c)
318 VLC_EXPORT( int, __net_Accept, ( vlc_object_t *, int *, mtime_t ) );
319
320 #define net_OpenUDP(a, b, c, d, e ) __net_OpenUDP(VLC_OBJECT(a), b, c, d, e)
321 VLC_EXPORT( int, __net_OpenUDP, ( vlc_object_t *p_this, char *psz_bind, int i_bind, char *psz_server, int i_server ) );
322
323 VLC_EXPORT( void, net_Close, ( int fd ) );
324 VLC_EXPORT( void, net_ListenClose, ( int *fd ) );
325
326
327 /* Functions to read from or write to the networking layer */
328 struct virtual_socket_t
329 {
330     void *p_sys;
331     int (*pf_recv) ( void *, void *, int );
332     int (*pf_send) ( void *, const void *, int );
333 };
334
335 #define net_Read(a,b,c,d,e,f) __net_Read(VLC_OBJECT(a),b,c,d,e,f)
336 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 ) );
337
338 #define net_ReadNonBlock(a,b,c,d,e,f) __net_ReadNonBlock(VLC_OBJECT(a),b,c,d,e,f)
339 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 ) );
340
341 #define net_Select(a,b,c,d,e,f,g) __net_Select(VLC_OBJECT(a),b,c,d,e,f,g)
342 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 ) );
343
344 #define net_Write(a,b,c,d,e) __net_Write(VLC_OBJECT(a),b,c,d,e)
345 VLC_EXPORT( int, __net_Write, ( vlc_object_t *p_this, int fd, v_socket_t *, uint8_t *p_data, int i_data ) );
346
347 #define net_Gets(a,b,c) __net_Gets(VLC_OBJECT(a),b,c)
348 VLC_EXPORT( char *, __net_Gets, ( vlc_object_t *p_this, int fd, v_socket_t * ) );
349
350 VLC_EXPORT( int, net_Printf, ( vlc_object_t *p_this, int fd, v_socket_t *, const char *psz_fmt, ... ) );
351
352 #define net_vaPrintf(a,b,c,d,e) __net_vaPrintf(VLC_OBJECT(a),b,c,d,e)
353 VLC_EXPORT( int, __net_vaPrintf, ( vlc_object_t *p_this, int fd, v_socket_t *, const char *psz_fmt, va_list args ) );
354
355 /*****************************************************************************
356  * net_StopRecv/Send
357  *****************************************************************************
358  * Wrappers for shutdown()
359  *****************************************************************************/
360 #if defined (SHUT_WR)
361 /* the standard way */
362 # define net_StopSend( fd ) (void)shutdown( fd, SHUT_WR )
363 # define net_StopRecv( fd ) (void)shutdown( fd, SHUT_RD )
364 #elif defined (SD_SEND)
365 /* the Microsoft seemingly-purposedly-different-for-the-sake-of-it way */
366 # define net_StopSend( fd ) (void)shutdown( fd, SD_SEND )
367 # define net_StopRecv( fd ) (void)shutdown( fd, SD_RECEIVE )
368 #else
369 # warning FIXME: implement shutdown on your platform!
370 # define net_StopSend( fd ) (void)0
371 # define net_StopRecv( fd ) (void)0
372 #endif
373
374 /* Portable network names/addresses resolution layer */
375
376 /* GAI error codes */
377 # ifndef EAI_BADFLAGS
378 #  define EAI_BADFLAGS -1
379 # endif
380 # ifndef EAI_NONAME
381 #  define EAI_NONAME -2
382 # endif
383 # ifndef EAI_AGAIN
384 #  define EAI_AGAIN -3
385 # endif
386 # ifndef EAI_FAIL
387 #  define EAI_FAIL -4
388 # endif
389 # ifndef EAI_NODATA
390 #  define EAI_NODATA -5
391 # endif
392 # ifndef EAI_FAMILY
393 #  define EAI_FAMILY -6
394 # endif
395 # ifndef EAI_SOCKTYPE
396 #  define EAI_SOCKTYPE -7
397 # endif
398 # ifndef EAI_SERVICE
399 #  define EAI_SERVICE -8
400 # endif
401 # ifndef EAI_ADDRFAMILY
402 #  define EAI_ADDRFAMILY -9
403 # endif
404 # ifndef EAI_MEMORY
405 #  define EAI_MEMORY -10
406 # endif
407 # ifndef EAI_SYSTEM
408 #  define EAI_SYSTEM -11
409 # endif
410
411
412 # ifndef NI_MAXHOST
413 #  define NI_MAXHOST 1025
414 #  define NI_MAXSERV 32
415 # endif
416 # define NI_MAXNUMERICHOST 48
417
418 # ifndef NI_NUMERICHOST
419 #  define NI_NUMERICHOST 0x01
420 #  define NI_NUMERICSERV 0x02
421 #  define NI_NOFQDN      0x04
422 #  define NI_NAMEREQD    0x08
423 #  define NI_DGRAM       0x10
424 # endif
425
426 # ifndef HAVE_STRUCT_ADDRINFO
427 struct addrinfo
428 {
429     int ai_flags;
430     int ai_family;
431     int ai_socktype;
432     int ai_protocol;
433     size_t ai_addrlen;
434     struct sockaddr *ai_addr;
435     char *ai_canonname;
436     struct addrinfo *ai_next;
437 };
438 #  define AI_PASSIVE     1
439 #  define AI_CANONNAME   2
440 #  define AI_NUMERICHOST 4
441 # endif /* if !HAVE_STRUCT_ADDRINFO */
442
443 /*** libidn support ***/
444 # ifndef AI_IDN
445 #  define AI_IDN      0
446 #  define AI_CANONIDN 0
447 # endif
448
449 VLC_EXPORT( const char *, vlc_gai_strerror, ( int ) );
450 VLC_EXPORT( int, vlc_getnameinfo, ( const struct sockaddr *, int, char *, int, int *, int ) );
451 VLC_EXPORT( int, vlc_getaddrinfo, ( vlc_object_t *, const char *, int, const struct addrinfo *, struct addrinfo ** ) );
452 VLC_EXPORT( void, vlc_freeaddrinfo, ( struct addrinfo * ) );
453
454 #endif