]> git.sesse.net Git - vlc/blob - include/network.h
2aeb2a3787321d90bc4ad69e862627ccc253b151
[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 #   include <ws2tcpip.h>
34 #elif HAVE_SYS_SOCKET_H
35 #   include <sys/socket.h>
36 #endif
37
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
43 {
44     char * psz_bind_addr;
45     int i_bind_port;
46
47     char * psz_server_addr;
48     int i_server_port;
49
50     int i_ttl;
51
52     /* Return values */
53     int i_handle;
54     size_t i_mtu;
55 };
56
57 typedef struct
58 {
59     char *psz_protocol;
60     char *psz_username;
61     char *psz_password;
62     char *psz_host;
63     int  i_port;
64
65     char *psz_path;
66
67     char *psz_option;
68     
69     char *psz_buffer; /* to be freed */
70 } vlc_url_t;
71
72 /*****************************************************************************
73  * vlc_UrlParse:
74  *****************************************************************************
75  * option : if != 0 then path is split at this char
76  *
77  * format [protocol://[login[:password]@]][host[:port]]/path[OPTIONoption]
78  *****************************************************************************/
79 static inline void vlc_UrlParse( vlc_url_t *url, const char *psz_url,
80                                  char option )
81 {
82     char *psz_dup;
83     char *psz_parse;
84     char *p;
85
86     url->psz_protocol = NULL;
87     url->psz_username = NULL;
88     url->psz_password = NULL;
89     url->psz_host     = NULL;
90     url->i_port       = 0;
91     url->psz_path     = NULL;
92     url->psz_option   = NULL;
93     
94     if( psz_url == NULL )
95     {
96         url->psz_buffer = NULL;
97         return;
98     }
99     url->psz_buffer = psz_parse = psz_dup = strdup( psz_url );
100
101     p  = strstr( psz_parse, ":/" );
102     if( p != NULL )
103     {
104         /* we have a protocol */
105
106         /* skip :// */
107         *p++ = '\0';
108         if( p[1] == '/' )
109             p += 2;
110         url->psz_protocol = psz_parse;
111
112         psz_parse = p;
113         p = strchr( psz_parse, '@' );
114         if( p != NULL )
115         {
116             /* We have a login */
117             url->psz_username = psz_parse;
118             *p++ = '\0';
119
120             psz_parse = strchr( psz_parse, ':' );
121             if( psz_parse != NULL )
122             {
123                 /* We have a password */
124                 *psz_parse++ = '\0';
125                 url->psz_password = psz_parse;
126             }
127
128             psz_parse = p;
129         }
130     }
131
132     p = strchr( psz_parse, '/' );
133     if( !p || psz_parse < p )
134     {
135         char *p2;
136
137         /* We have a host[:port] */
138         url->psz_host = strdup( psz_parse );
139         if( p )
140         {
141             url->psz_host[p - psz_parse] = '\0';
142         }
143
144         if( *url->psz_host == '[' )
145         {
146             /* Ipv6 address */
147             p2 = strchr( url->psz_host, ']' );
148             if( p2 )
149             {
150                 p2 = strchr( p2, ':' );
151             }
152         }
153         else
154         {
155             p2 = strchr( url->psz_host, ':' );
156         }
157         if( p2 )
158         {
159             *p2++ = '\0';
160             url->i_port = atoi( p2 );
161         }
162     }
163     psz_parse = p;
164
165     /* Now parse psz_path and psz_option */
166     if( psz_parse )
167     {
168         url->psz_path = psz_parse;
169         if( option != '\0' )
170         {
171             p = strchr( url->psz_path, option );
172             if( p )
173             {
174                 *p++ = '\0';
175                 url->psz_option = p;
176             }
177         }
178     }
179 }
180
181 /*****************************************************************************
182  * vlc_UrlClean:
183  *****************************************************************************
184  *
185  *****************************************************************************/
186 static inline void vlc_UrlClean( vlc_url_t *url )
187 {
188     if( url->psz_buffer ) free( url->psz_buffer );
189     if( url->psz_host )   free( url->psz_host );
190
191     url->psz_protocol = NULL;
192     url->psz_username = NULL;
193     url->psz_password = NULL;
194     url->psz_host     = NULL;
195     url->i_port       = 0;
196     url->psz_path     = NULL;
197     url->psz_option   = NULL;
198
199     url->psz_buffer   = NULL;
200 }
201
202 /*****************************************************************************
203  * vlc_UrlEncode: 
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 )
209 {
210     char *psz_enc, *out;
211     const char *in;
212
213     psz_enc = (char *)malloc( 3 * strlen( psz_url ) + 1 );
214     if( psz_enc == NULL )
215         return NULL;
216
217     out = psz_enc;
218     for( in = psz_url; *in; in++ )
219     {
220         char c = *in;
221
222         if( ( c <= 32 ) || ( c == '%' ) || ( c == '?' ) || ( c == '&' )
223          || ( c == '+' ) )
224         {
225             *out++ = '%';   
226             *out++ = ( ( c >> 4 ) >= 0xA ) ? 'A' + ( c >> 4 ) - 0xA
227                                            : '0' + ( c >> 4 );
228             *out++ = ( ( c & 0xf ) >= 0xA ) ? 'A' + ( c & 0xf ) - 0xA
229                                            : '0' + ( c & 0xf );
230         }
231         else
232             *out++ = c;
233     }
234     *out++ = '\0';
235
236     return (char *)realloc( psz_enc, out - psz_enc );
237 }
238
239 /*****************************************************************************
240  * vlc_UrlIsNotEncoded:
241  *****************************************************************************
242  * check if given string is not a valid URL and must hence be encoded
243  *****************************************************************************/
244 #include <ctype.h>
245
246 static inline int vlc_UrlIsNotEncoded( const char *psz_url )
247 {
248     const char *ptr;
249
250     for( ptr = psz_url; *ptr; ptr++ )
251     {
252         char c = *ptr;
253
254         if( c == '%' )
255         {
256             if( !isxdigit( ptr[1] ) || !isxdigit( ptr[2] ) )
257                 return 1; /* not encoded */
258             ptr += 2;
259         }
260         else
261         if( c == ' ' )
262             return 1;
263     }
264     return 0; /* looks fine - but maybe it is not encoded */
265 }
266                     
267 /*****************************************************************************
268  * vlc_b64_encode:
269  *****************************************************************************
270  *
271  *****************************************************************************/
272 static inline char *vlc_b64_encode( char *src )
273 {
274     static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
275                                                                                 
276     char *dst = (char *)malloc( strlen( src ) * 4 / 3 + 12 );
277     char *ret = dst;
278     unsigned i_bits = 0;
279     unsigned i_shift = 0;
280                                                                                 
281     for( ;; )
282     {
283         if( *src )
284         {
285             i_bits = ( i_bits << 8 )|( *src++ );
286             i_shift += 8;
287         }
288         else if( i_shift > 0 )
289         {
290            i_bits <<= 6 - i_shift;
291            i_shift = 6;
292         }
293         else
294         {
295             *dst++ = '=';
296             break;
297         }
298                                                                                 
299         while( i_shift >= 6 )
300         {
301             i_shift -= 6;
302             *dst++ = b64[(i_bits >> i_shift)&0x3f];
303         }
304     }
305                                                                                 
306     *dst++ = '\0';
307                                                                                 
308     return ret;
309 }
310
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 ) );
314
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 ) );
317
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 ) );
320
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 ) );
323
324 VLC_EXPORT( void, net_Close, ( int fd ) );
325 VLC_EXPORT( void, net_ListenClose, ( int *fd ) );
326
327
328 /* Functions to read from or write to the networking layer */
329 struct virtual_socket_t
330 {
331     void *p_sys;
332     int (*pf_recv) ( void *, void *, int );
333     int (*pf_send) ( void *, const void *, int );
334 };
335
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 ) );
338
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 ) );
341
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 ) );
344
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 ) );
347
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 * ) );
350
351 VLC_EXPORT( int, net_Printf, ( vlc_object_t *p_this, int fd, v_socket_t *, const char *psz_fmt, ... ) );
352
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 ) );
355
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 ) );
359
360 /*****************************************************************************
361  * net_StopRecv/Send
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 )
373 #else
374 # warning FIXME: implement shutdown on your platform!
375 # define net_StopSend( fd ) (void)0
376 # define net_StopRecv( fd ) (void)0
377 #endif
378
379 /* Portable network names/addresses resolution layer */
380
381 /* GAI error codes */
382 # ifndef EAI_BADFLAGS
383 #  define EAI_BADFLAGS -1
384 # endif
385 # ifndef EAI_NONAME
386 #  define EAI_NONAME -2
387 # endif
388 # ifndef EAI_AGAIN
389 #  define EAI_AGAIN -3
390 # endif
391 # ifndef EAI_FAIL
392 #  define EAI_FAIL -4
393 # endif
394 # ifndef EAI_NODATA
395 #  define EAI_NODATA -5
396 # endif
397 # ifndef EAI_FAMILY
398 #  define EAI_FAMILY -6
399 # endif
400 # ifndef EAI_SOCKTYPE
401 #  define EAI_SOCKTYPE -7
402 # endif
403 # ifndef EAI_SERVICE
404 #  define EAI_SERVICE -8
405 # endif
406 # ifndef EAI_ADDRFAMILY
407 #  define EAI_ADDRFAMILY -9
408 # endif
409 # ifndef EAI_MEMORY
410 #  define EAI_MEMORY -10
411 # endif
412 # ifndef EAI_SYSTEM
413 #  define EAI_SYSTEM -11
414 # endif
415
416
417 # ifndef NI_MAXHOST
418 #  define NI_MAXHOST 1025
419 #  define NI_MAXSERV 32
420 # endif
421 # define NI_MAXNUMERICHOST 48
422
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
429 # endif
430
431 # ifndef HAVE_STRUCT_ADDRINFO
432 struct addrinfo
433 {
434     int ai_flags;
435     int ai_family;
436     int ai_socktype;
437     int ai_protocol;
438     size_t ai_addrlen;
439     struct sockaddr *ai_addr;
440     char *ai_canonname;
441     struct addrinfo *ai_next;
442 };
443 #  define AI_PASSIVE     1
444 #  define AI_CANONNAME   2
445 #  define AI_NUMERICHOST 4
446 # endif /* if !HAVE_STRUCT_ADDRINFO */
447
448 /*** libidn support ***/
449 # ifndef AI_IDN
450 #  define AI_IDN      0
451 #  define AI_CANONIDN 0
452 # endif
453
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 * ) );
458
459 #endif