]> git.sesse.net Git - vlc/blob - include/network.h
* change hotkeys OSD message from 0-100% to 0-400%
[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     char * psz_bind_addr;
49     int i_bind_port;
50
51     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, char *psz_bind, int i_bind, 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 #define net_GetSockAddress(a,b,c,d) __net_GetAddress(VLC_OBJECT(a),VLC_FALSE,b,c,d)
360 #define net_GetPeerAddress(a,b,c,d) __net_GetAddress(VLC_OBJECT(a),VLC_TRUE,b,c,d)
361 VLC_EXPORT( int, __net_GetAddress, ( vlc_object_t *p_this, vlc_bool_t peer, int fd, char *address, int *port ) );
362
363 /*****************************************************************************
364  * net_StopRecv/Send
365  *****************************************************************************
366  * Wrappers for shutdown()
367  *****************************************************************************/
368 #if defined (SHUT_WR)
369 /* the standard way */
370 # define net_StopSend( fd ) (void)shutdown( fd, SHUT_WR )
371 # define net_StopRecv( fd ) (void)shutdown( fd, SHUT_RD )
372 #elif defined (SD_SEND)
373 /* the Microsoft seemingly-purposedly-different-for-the-sake-of-it way */
374 # define net_StopSend( fd ) (void)shutdown( fd, SD_SEND )
375 # define net_StopRecv( fd ) (void)shutdown( fd, SD_RECEIVE )
376 #else
377 # warning FIXME: implement shutdown on your platform!
378 # define net_StopSend( fd ) (void)0
379 # define net_StopRecv( fd ) (void)0
380 #endif
381
382 /* Portable network names/addresses resolution layer */
383
384 /* GAI error codes */
385 # ifndef EAI_BADFLAGS
386 #  define EAI_BADFLAGS -1
387 # endif
388 # ifndef EAI_NONAME
389 #  define EAI_NONAME -2
390 # endif
391 # ifndef EAI_AGAIN
392 #  define EAI_AGAIN -3
393 # endif
394 # ifndef EAI_FAIL
395 #  define EAI_FAIL -4
396 # endif
397 # ifndef EAI_NODATA
398 #  define EAI_NODATA -5
399 # endif
400 # ifndef EAI_FAMILY
401 #  define EAI_FAMILY -6
402 # endif
403 # ifndef EAI_SOCKTYPE
404 #  define EAI_SOCKTYPE -7
405 # endif
406 # ifndef EAI_SERVICE
407 #  define EAI_SERVICE -8
408 # endif
409 # ifndef EAI_ADDRFAMILY
410 #  define EAI_ADDRFAMILY -9
411 # endif
412 # ifndef EAI_MEMORY
413 #  define EAI_MEMORY -10
414 # endif
415 # ifndef EAI_SYSTEM
416 #  define EAI_SYSTEM -11
417 # endif
418
419
420 # ifndef NI_MAXHOST
421 #  define NI_MAXHOST 1025
422 #  define NI_MAXSERV 32
423 # endif
424 # define NI_MAXNUMERICHOST 48
425
426 # ifndef NI_NUMERICHOST
427 #  define NI_NUMERICHOST 0x01
428 #  define NI_NUMERICSERV 0x02
429 #  define NI_NOFQDN      0x04
430 #  define NI_NAMEREQD    0x08
431 #  define NI_DGRAM       0x10
432 # endif
433
434 # ifndef HAVE_STRUCT_ADDRINFO
435 struct addrinfo
436 {
437     int ai_flags;
438     int ai_family;
439     int ai_socktype;
440     int ai_protocol;
441     size_t ai_addrlen;
442     struct sockaddr *ai_addr;
443     char *ai_canonname;
444     struct addrinfo *ai_next;
445 };
446 #  define AI_PASSIVE     1
447 #  define AI_CANONNAME   2
448 #  define AI_NUMERICHOST 4
449 # endif /* if !HAVE_STRUCT_ADDRINFO */
450
451 /*** libidn support ***/
452 # ifndef AI_IDN
453 #  define AI_IDN      0
454 #  define AI_CANONIDN 0
455 # endif
456
457 VLC_EXPORT( const char *, vlc_gai_strerror, ( int ) );
458 VLC_EXPORT( int, vlc_getnameinfo, ( const struct sockaddr *, int, char *, int, int *, int ) );
459 VLC_EXPORT( int, vlc_getaddrinfo, ( vlc_object_t *, const char *, int, const struct addrinfo *, struct addrinfo ** ) );
460 VLC_EXPORT( void, vlc_freeaddrinfo, ( struct addrinfo * ) );
461
462 /*****************************************************************************
463  * net_AddressIsMulticast: This function returns VLC_FALSE if the psz_addr does
464  * not specify a multicast address or if the address is not a valid address.
465  * FIXME: Only the first returned address is checked. This might be problematic.
466  *****************************************************************************/
467 static inline vlc_bool_t net_AddressIsMulticast( vlc_object_t *p_object, char *psz_addr )
468 {
469     struct addrinfo hints, *res;
470     vlc_bool_t b_multicast = VLC_FALSE;
471     int i;
472
473     if( psz_addr == NULL )
474     {
475         msg_Err( p_object, "*FIXME* Unexpected NULL URI for net_AddressIsMulticast" );
476         msg_Err( p_object, "This should not happen. VLC needs fixing." );
477         return VLC_FALSE;
478     }
479     
480     memset( &hints, 0, sizeof( hints ) );
481     hints.ai_socktype = SOCK_DGRAM; /* UDP */
482     hints.ai_flags = AI_NUMERICHOST;
483
484     i = vlc_getaddrinfo( p_object, psz_addr, 0,
485                          &hints, &res );
486     /*if( i == 0 )
487         i = vlc_getnameinfo( res->ai_addr, res->ai_addrlen, psz_buf,
488                              sizeof( psz_buf ), NULL, NI_NUMERICHOST );*/
489     if( i )
490     {
491         msg_Err( p_object, "Invalid node for net_AddressIsMulticast: %s : %s",
492                  psz_addr, vlc_gai_strerror( i ) );
493         return b_multicast;
494     }
495     
496     if( res->ai_family == AF_INET )
497     {
498 #if !defined( SYS_BEOS )
499         struct sockaddr_in *v4 = (struct sockaddr_in *) res->ai_addr;
500         b_multicast = ( ntohl( v4->sin_addr.s_addr ) > 0xe1000000 && ntohl( v4->sin_addr.s_addr ) <= 0xEFFFFFFF );
501 #endif
502     }
503     else if( res->ai_family == AF_INET6 )
504     {
505 #if defined( WIN32 ) || defined( HAVE_IF_NAMETOINDEX )
506         struct sockaddr_in6 *v6 = (struct sockaddr_in6 *) res->ai_addr;
507         b_multicast = IN6_IS_ADDR_MULTICAST( &v6->sin6_addr);
508 #endif
509     }
510     
511     vlc_freeaddrinfo( res );
512     return b_multicast;
513 }
514
515 #endif