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