1 /*****************************************************************************
2 * network.h: interface to communicate with network plug-ins
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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 *****************************************************************************/
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
38 char * psz_server_addr;
51 #define NETWORK_TCP_PASSIVE 3
65 /*****************************************************************************
67 *****************************************************************************
68 * option : if != 0 then path is split at this char
70 * format [protocol://][host[:port]]/path[OPTIONoption]
71 *****************************************************************************/
72 static inline void vlc_UrlParse( vlc_url_t *url, char *psz_url, char option )
74 char *psz_dup = psz_url ? strdup( psz_url ) : 0;
75 char *psz_parse = psz_dup;
78 url->psz_protocol = NULL;
82 url->psz_option = NULL;
84 if( !psz_url ) return;
86 if( ( p = strstr( psz_parse, ":/" ) ) )
88 /* we have a protocol */
92 if( p[0] == '/' && p[1] == '/' )
96 url->psz_protocol = strdup( psz_dup );
101 p = strchr( psz_parse, '/' );
102 if( !p || psz_parse < p )
106 /* We have a host[:port] */
107 url->psz_host = strdup( psz_parse );
110 url->psz_host[p - psz_parse] = '\0';
113 if( *url->psz_host == '[' )
116 p2 = strchr( url->psz_host, ']' );
119 p2 = strchr( p2, ':' );
124 p2 = strchr( url->psz_host, ':' );
129 url->i_port = atoi( p2 );
134 /* Now parse psz_path and psz_option */
137 url->psz_path = strdup( psz_parse );
140 p = strchr( url->psz_path, option );
144 url->psz_option = strdup( p );
151 /*****************************************************************************
153 *****************************************************************************
155 *****************************************************************************/
156 static inline void vlc_UrlClean( vlc_url_t *url )
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 );
163 url->psz_protocol = NULL;
164 url->psz_host = NULL;
166 url->psz_path = NULL;
167 url->psz_option = NULL;
170 /*****************************************************************************
172 *****************************************************************************
174 *****************************************************************************/
175 static inline char *vlc_b64_encode( unsigned char *src )
177 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
179 char *dst = malloc( strlen( src ) * 4 / 3 + 12 );
182 unsigned i_shift = 0;
188 i_bits = ( i_bits << 8 )|( *src++ );
191 else if( i_shift > 0 )
193 i_bits <<= 6 - i_shift;
202 while( i_shift >= 6 )
205 *dst++ = b64[(i_bits >> i_shift)&0x3f];
214 VLC_EXPORT( int, net_ConvertIPv4, ( uint32_t *p_addr, const char * psz_address ) );
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 ) );
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 ) );
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 ) );
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 ) );
229 VLC_EXPORT( void, net_Close, ( int fd ) );
232 /* Functions to read from or write to the networking layer */
233 struct virtual_socket_t
236 int (*pf_recv) ( void *, void *, int );
237 int (*pf_send) ( void *, const void *, int );
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 ) );
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 ) );
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 ) );
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 ) );
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 * ) );
255 VLC_EXPORT( int, net_Printf, ( vlc_object_t *p_this, int fd, v_socket_t *, const char *psz_fmt, ... ) );
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 ) );