]> git.sesse.net Git - vlc/blob - include/network.h
preferences_widgets -- Delete spinctrl hack for WIN32 (not needed with updated wxwidg...
[vlc] / include / network.h
1 /*****************************************************************************
2  * network.h: interface to communicate with network plug-ins
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Laurent Aimar <fenrir@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 #ifndef __VLC_NETWORK_H
26 # define __VLC_NETWORK_H
27 /*****************************************************************************
28  * network_socket_t: structure passed to a network plug-in to define the
29  *                   kind of socket we want
30  *****************************************************************************/
31 struct network_socket_t
32 {
33     unsigned int i_type;
34
35     char * psz_bind_addr;
36     int i_bind_port;
37
38     char * psz_server_addr;
39     int i_server_port;
40
41     int i_ttl;
42
43     /* Return values */
44     int i_handle;
45     size_t i_mtu;
46 };
47
48 /* Socket types */
49 #define NETWORK_UDP 1
50 #define NETWORK_TCP 2
51 #define NETWORK_TCP_PASSIVE 3
52
53
54 typedef struct
55 {
56     char *psz_protocol;
57     char *psz_host;
58     int  i_port;
59
60     char *psz_path;
61
62     char *psz_option;
63 } vlc_url_t;
64
65 /*****************************************************************************
66  * vlc_UrlParse:
67  *****************************************************************************
68  * option : if != 0 then path is split at this char
69  *
70  * format [protocol://][host[:port]]/path[OPTIONoption]
71  *****************************************************************************/
72 static inline void vlc_UrlParse( vlc_url_t *url, char *psz_url, char option )
73 {
74     char *psz_dup = psz_url ? strdup( psz_url ) : 0;
75     char *psz_parse = psz_dup;
76     char *p;
77
78     url->psz_protocol = NULL;
79     url->psz_host     = NULL;
80     url->i_port       = 0;
81     url->psz_path     = NULL;
82     url->psz_option   = NULL;
83
84     if( !psz_url ) return;
85
86     if( ( p  = strstr( psz_parse, ":/" ) ) )
87     {
88         /* we have a protocol */
89
90         /* skip :// */
91         *p++ = '\0';
92         if( p[0] == '/' && p[1] == '/' )
93         {
94             p += 2;
95         }
96         url->psz_protocol = strdup( psz_dup );
97
98         psz_parse = p;
99     }
100
101     p = strchr( psz_parse, '/' );
102     if( !p || psz_parse < p )
103     {
104         char *p2;
105
106         /* We have a host[:port] */
107         url->psz_host = strdup( psz_parse );
108         if( p )
109         {
110             url->psz_host[p - psz_parse] = '\0';
111         }
112
113         if( *url->psz_host == '[' )
114         {
115             /* Ipv6 address */
116             p2 = strchr( url->psz_host, ']' );
117             if( p2 )
118             {
119                 p2 = strchr( p2, ':' );
120             }
121         }
122         else
123         {
124             p2 = strchr( url->psz_host, ':' );
125         }
126         if( p2 )
127         {
128             *p2++ = '\0';
129             url->i_port = atoi( p2 );
130         }
131     }
132     psz_parse = p;
133
134     /* Now parse psz_path and psz_option */
135     if( psz_parse )
136     {
137         url->psz_path = strdup( psz_parse );
138         if( option != '\0' )
139         {
140             p = strchr( url->psz_path, option );
141             if( p )
142             {
143                 *p++ = '\0';
144                 url->psz_option = strdup( p );
145             }
146         }
147     }
148     free( psz_dup );
149 }
150
151 /*****************************************************************************
152  * vlc_UrlClean:
153  *****************************************************************************
154  *
155  *****************************************************************************/
156 static inline void vlc_UrlClean( vlc_url_t *url )
157 {
158     if( url->psz_protocol ) free( url->psz_protocol );
159     if( url->psz_host )     free( url->psz_host );
160     if( url->psz_path )     free( url->psz_path );
161     if( url->psz_option )   free( url->psz_option );
162
163     url->psz_protocol = NULL;
164     url->psz_host     = NULL;
165     url->i_port       = 0;
166     url->psz_path     = NULL;
167     url->psz_option   = NULL;
168 }
169                     
170 /*****************************************************************************
171  * vlc_b64_encode:
172  *****************************************************************************
173  *
174  *****************************************************************************/
175 static inline char *vlc_b64_encode( unsigned char *src )
176 {
177     static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
178                                                                                 
179     char *dst = malloc( strlen( src ) * 4 / 3 + 12 );
180     char *ret = dst;
181     unsigned i_bits = 0;
182     unsigned i_shift = 0;
183                                                                                 
184     for( ;; )
185     {
186         if( *src )
187         {
188             i_bits = ( i_bits << 8 )|( *src++ );
189             i_shift += 8;
190         }
191         else if( i_shift > 0 )
192         {
193            i_bits <<= 6 - i_shift;
194            i_shift = 6;
195         }
196         else
197         {
198             *dst++ = '=';
199             break;
200         }
201                                                                                 
202         while( i_shift >= 6 )
203         {
204             i_shift -= 6;
205             *dst++ = b64[(i_bits >> i_shift)&0x3f];
206         }
207     }
208                                                                                 
209     *dst++ = '\0';
210                                                                                 
211     return ret;
212 }
213
214 VLC_EXPORT( int, net_ConvertIPv4, ( uint32_t *p_addr, const char * psz_address ) );
215
216 /* Portable networking layer communication */
217 #define net_OpenTCP(a, b, c) __net_OpenTCP(VLC_OBJECT(a), b, c)
218 VLC_EXPORT( int, __net_OpenTCP, ( vlc_object_t *p_this, const char *psz_host, int i_port ) );
219
220 #define net_ListenTCP(a, b, c) __net_ListenTCP(VLC_OBJECT(a), b, c)
221 VLC_EXPORT( int, __net_ListenTCP, ( vlc_object_t *p_this, char *psz_localaddr, int i_port ) );
222
223 #define net_Accept(a, b, c) __net_Accept(VLC_OBJECT(a), b, c)
224 VLC_EXPORT( int, __net_Accept, ( vlc_object_t *p_this, int fd_listen, mtime_t i_wait ) );
225
226 #define net_OpenUDP(a, b, c, d, e ) __net_OpenUDP(VLC_OBJECT(a), b, c, d, e)
227 VLC_EXPORT( int, __net_OpenUDP, ( vlc_object_t *p_this, char *psz_bind, int i_bind, char *psz_server, int i_server ) );
228
229 VLC_EXPORT( void, net_Close, ( int fd ) );
230
231
232 /* Functions to read from or write to the networking layer */
233 struct virtual_socket_t
234 {
235     void *p_sys;
236     int (*pf_recv) ( void *, void *, int );
237     int (*pf_send) ( void *, const void *, int );
238 };
239
240 #define net_Read(a,b,c,d,e,f) __net_Read(VLC_OBJECT(a),b,c,d,e,f)
241 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 ) );
242
243 #define net_ReadNonBlock(a,b,c,d,e,f) __net_ReadNonBlock(VLC_OBJECT(a),b,c,d,e,f)
244 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 ) );
245
246 #define net_Select(a,b,c,d,e,f,g) __net_Select(VLC_OBJECT(a),b,c,d,e,f,g)
247 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 ) );
248
249 #define net_Write(a,b,c,d,e) __net_Write(VLC_OBJECT(a),b,c,d,e)
250 VLC_EXPORT( int, __net_Write, ( vlc_object_t *p_this, int fd, v_socket_t *, uint8_t *p_data, int i_data ) );
251
252 #define net_Gets(a,b,c) __net_Gets(VLC_OBJECT(a),b,c)
253 VLC_EXPORT( char *, __net_Gets, ( vlc_object_t *p_this, int fd, v_socket_t * ) );
254
255 VLC_EXPORT( int, net_Printf, ( vlc_object_t *p_this, int fd, v_socket_t *, const char *psz_fmt, ... ) );
256
257 #define net_vaPrintf(a,b,c,d,e) __net_vaPrintf(VLC_OBJECT(a),b,c,d,e)
258 VLC_EXPORT( int, __net_vaPrintf, ( vlc_object_t *p_this, int fd, v_socket_t *, const char *psz_fmt, va_list args ) );
259
260 #endif