]> git.sesse.net Git - vlc/blob - include/network.h
* backport of [11413] and [11422] Also closes #198
[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 <courmisch # via.ecp.fr>
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     unsigned int i_type;
35
36     char * psz_bind_addr;
37     int i_bind_port;
38
39     char * psz_server_addr;
40     int i_server_port;
41
42     int i_ttl;
43
44     /* Return values */
45     int i_handle;
46     size_t i_mtu;
47 };
48
49 /* Socket types */
50 #define NETWORK_UDP 1
51 #define NETWORK_TCP 2
52 #define NETWORK_TCP_PASSIVE 3
53
54
55 typedef struct
56 {
57     char *psz_protocol;
58     char *psz_host;
59     int  i_port;
60
61     char *psz_path;
62
63     char *psz_option;
64 } vlc_url_t;
65
66 /*****************************************************************************
67  * vlc_UrlParse:
68  *****************************************************************************
69  * option : if != 0 then path is split at this char
70  *
71  * format [protocol://][host[:port]]/path[OPTIONoption]
72  *****************************************************************************/
73 static inline void vlc_UrlParse( vlc_url_t *url, char *psz_url, char option )
74 {
75     char *psz_dup = psz_url ? strdup( psz_url ) : 0;
76     char *psz_parse = psz_dup;
77     char *p;
78
79     url->psz_protocol = NULL;
80     url->psz_host     = NULL;
81     url->i_port       = 0;
82     url->psz_path     = NULL;
83     url->psz_option   = NULL;
84
85     if( !psz_url ) return;
86
87     if( ( p  = strstr( psz_parse, ":/" ) ) )
88     {
89         /* we have a protocol */
90
91         /* skip :// */
92         *p++ = '\0';
93         if( p[0] == '/' && p[1] == '/' )
94         {
95             p += 2;
96         }
97         url->psz_protocol = strdup( psz_dup );
98
99         psz_parse = p;
100     }
101
102     p = strchr( psz_parse, '/' );
103     if( !p || psz_parse < p )
104     {
105         char *p2;
106
107         /* We have a host[:port] */
108         url->psz_host = strdup( psz_parse );
109         if( p )
110         {
111             url->psz_host[p - psz_parse] = '\0';
112         }
113
114         if( *url->psz_host == '[' )
115         {
116             /* Ipv6 address */
117             p2 = strchr( url->psz_host, ']' );
118             if( p2 )
119             {
120                 p2 = strchr( p2, ':' );
121             }
122         }
123         else
124         {
125             p2 = strchr( url->psz_host, ':' );
126         }
127         if( p2 )
128         {
129             *p2++ = '\0';
130             url->i_port = atoi( p2 );
131         }
132     }
133     psz_parse = p;
134
135     /* Now parse psz_path and psz_option */
136     if( psz_parse )
137     {
138         url->psz_path = strdup( psz_parse );
139         if( option != '\0' )
140         {
141             p = strchr( url->psz_path, option );
142             if( p )
143             {
144                 *p++ = '\0';
145                 url->psz_option = strdup( p );
146             }
147         }
148     }
149     free( psz_dup );
150 }
151
152 /*****************************************************************************
153  * vlc_UrlClean:
154  *****************************************************************************
155  *
156  *****************************************************************************/
157 static inline void vlc_UrlClean( vlc_url_t *url )
158 {
159     if( url->psz_protocol ) free( url->psz_protocol );
160     if( url->psz_host )     free( url->psz_host );
161     if( url->psz_path )     free( url->psz_path );
162     if( url->psz_option )   free( url->psz_option );
163
164     url->psz_protocol = NULL;
165     url->psz_host     = NULL;
166     url->i_port       = 0;
167     url->psz_path     = NULL;
168     url->psz_option   = NULL;
169 }
170
171 /*****************************************************************************
172  * vlc_UrlEncode: 
173  *****************************************************************************
174  * perform URL encoding
175  * (you do NOT want to do URL decoding - it is not reversible - do NOT do it)
176  *****************************************************************************/
177 static inline char *vlc_UrlEncode( const char *psz_url )
178 {
179     char *psz_enc, *out;
180     const char *in;
181
182     psz_enc = (char *)malloc( 3 * strlen( psz_url ) + 1 );
183     if( psz_enc == NULL )
184         return NULL;
185
186     out = psz_enc;
187     for( in = psz_url; *in; in++ )
188     {
189         char c = *in;
190
191         if( ( c <= 32 ) || ( c == '%' ) || ( c == '?' ) || ( c == '&' )
192          || ( c == '+' ) )
193         {
194             *out++ = '%';   
195             *out++ = ( ( c >> 4 ) >= 0xA ) ? 'A' + ( c >> 4 ) - 0xA
196                                            : '0' + ( c >> 4 );
197             *out++ = ( ( c & 0xf ) >= 0xA ) ? 'A' + ( c & 0xf ) - 0xA
198                                            : '0' + ( c & 0xf );
199         }
200         else
201             *out++ = c;
202     }
203     *out++ = '\0';
204
205     return (char *)realloc( psz_enc, out - psz_enc );
206 }
207
208 /*****************************************************************************
209  * vlc_UrlIsNotEncoded:
210  *****************************************************************************
211  * check if given string is not a valid URL and must hence be encoded
212  *****************************************************************************/
213 #include <ctype.h>
214
215 static inline int vlc_UrlIsNotEncoded( const char *psz_url )
216 {
217     const char *ptr;
218
219     for( ptr = psz_url; *ptr; ptr++ )
220     {
221         char c = *ptr;
222
223         if( c == '%' )
224         {
225             if( !isxdigit( ptr[1] ) || !isxdigit( ptr[2] ) )
226                 return 1; /* not encoded */
227             ptr += 2;
228         }
229         else
230         if( c == ' ' )
231             return 1;
232     }
233     return 0; /* looks fine - but maybe it is not encoded */
234 }
235                     
236 /*****************************************************************************
237  * vlc_b64_encode:
238  *****************************************************************************
239  *
240  *****************************************************************************/
241 static inline char *vlc_b64_encode( char *src )
242 {
243     static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
244                                                                                 
245     char *dst = (char *)malloc( strlen( src ) * 4 / 3 + 12 );
246     char *ret = dst;
247     unsigned i_bits = 0;
248     unsigned i_shift = 0;
249                                                                                 
250     for( ;; )
251     {
252         if( *src )
253         {
254             i_bits = ( i_bits << 8 )|( *src++ );
255             i_shift += 8;
256         }
257         else if( i_shift > 0 )
258         {
259            i_bits <<= 6 - i_shift;
260            i_shift = 6;
261         }
262         else
263         {
264             *dst++ = '=';
265             break;
266         }
267                                                                                 
268         while( i_shift >= 6 )
269         {
270             i_shift -= 6;
271             *dst++ = b64[(i_bits >> i_shift)&0x3f];
272         }
273     }
274                                                                                 
275     *dst++ = '\0';
276                                                                                 
277     return ret;
278 }
279
280 VLC_EXPORT( int, net_ConvertIPv4, ( uint32_t *p_addr, const char * psz_address ) );
281
282 /* Portable networking layer communication */
283 #define net_OpenTCP(a, b, c) __net_OpenTCP(VLC_OBJECT(a), b, c)
284 VLC_EXPORT( int, __net_OpenTCP, ( vlc_object_t *p_this, const char *psz_host, int i_port ) );
285
286 #define net_ListenTCP(a, b, c) __net_ListenTCP(VLC_OBJECT(a), b, c)
287 VLC_EXPORT( int, __net_ListenTCP, ( vlc_object_t *p_this, char *psz_localaddr, int i_port ) );
288
289 #define net_Accept(a, b, c) __net_Accept(VLC_OBJECT(a), b, c)
290 VLC_EXPORT( int, __net_Accept, ( vlc_object_t *p_this, int fd_listen, mtime_t i_wait ) );
291
292 #define net_OpenUDP(a, b, c, d, e ) __net_OpenUDP(VLC_OBJECT(a), b, c, d, e)
293 VLC_EXPORT( int, __net_OpenUDP, ( vlc_object_t *p_this, char *psz_bind, int i_bind, char *psz_server, int i_server ) );
294
295 VLC_EXPORT( void, net_Close, ( int fd ) );
296
297
298 /* Functions to read from or write to the networking layer */
299 struct virtual_socket_t
300 {
301     void *p_sys;
302     int (*pf_recv) ( void *, void *, int );
303     int (*pf_send) ( void *, const void *, int );
304 };
305
306 #define net_Read(a,b,c,d,e,f) __net_Read(VLC_OBJECT(a),b,c,d,e,f)
307 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 ) );
308
309 #define net_ReadNonBlock(a,b,c,d,e,f) __net_ReadNonBlock(VLC_OBJECT(a),b,c,d,e,f)
310 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 ) );
311
312 #define net_Select(a,b,c,d,e,f,g) __net_Select(VLC_OBJECT(a),b,c,d,e,f,g)
313 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 ) );
314
315 #define net_Write(a,b,c,d,e) __net_Write(VLC_OBJECT(a),b,c,d,e)
316 VLC_EXPORT( int, __net_Write, ( vlc_object_t *p_this, int fd, v_socket_t *, uint8_t *p_data, int i_data ) );
317
318 #define net_Gets(a,b,c) __net_Gets(VLC_OBJECT(a),b,c)
319 VLC_EXPORT( char *, __net_Gets, ( vlc_object_t *p_this, int fd, v_socket_t * ) );
320
321 VLC_EXPORT( int, net_Printf, ( vlc_object_t *p_this, int fd, v_socket_t *, const char *psz_fmt, ... ) );
322
323 #define net_vaPrintf(a,b,c,d,e) __net_vaPrintf(VLC_OBJECT(a),b,c,d,e)
324 VLC_EXPORT( int, __net_vaPrintf, ( vlc_object_t *p_this, int fd, v_socket_t *, const char *psz_fmt, va_list args ) );
325
326 #endif