Fortunes about QT and "hearing" video codecs
[vlc.git] / src / network / tcp.c
blobeb398c9336706db5813154e107213b717fbbb4f3
1 /*****************************************************************************
2 * tcp.c:
3 *****************************************************************************
4 * Copyright (C) 2004-2005 VLC authors and VideoLAN
5 * Copyright (C) 2005-2006 Rémi Denis-Courmont
6 * $Id$
8 * Authors: Laurent Aimar <fenrir@videolan.org>
9 * Rémi Denis-Courmont <rem # videolan.org>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
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 Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
27 * Preamble
28 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #include <vlc_common.h>
35 #include <unistd.h>
37 #include <vlc_network.h>
38 #include <vlc_interrupt.h>
40 /*****************************************************************************
41 * SocksNegotiate:
42 *****************************************************************************
43 * Negotiate authentication with a SOCKS server.
44 *****************************************************************************/
45 static int SocksNegotiate( vlc_object_t *p_obj,
46 int fd, int i_socks_version,
47 const char *psz_socks_user,
48 const char *psz_socks_passwd )
50 uint8_t buffer[128+2*256];
51 int i_len;
52 bool b_auth = false;
54 if( i_socks_version != 5 )
55 return VLC_SUCCESS;
57 /* We negotiate authentication */
58 buffer[0] = i_socks_version; /* SOCKS version */
59 if( psz_socks_user != NULL && psz_socks_passwd != NULL )
61 buffer[1] = 2; /* Number of methods */
62 buffer[2] = 0x00; /* - No auth required */
63 buffer[3] = 0x02; /* - USer/Password */
64 i_len = 4;
65 b_auth = true;
67 else
69 buffer[1] = 1; /* Number of methods */
70 buffer[2] = 0x00; /* - No auth required */
71 i_len = 3;
74 if( net_Write( p_obj, fd, buffer, i_len ) != i_len )
75 return VLC_EGENERIC;
76 if( net_Read( p_obj, fd, buffer, 2) != 2 )
77 return VLC_EGENERIC;
79 msg_Dbg( p_obj, "socks: v=%d method=%x", buffer[0], buffer[1] );
81 if( buffer[1] == 0x00 )
83 msg_Dbg( p_obj, "socks: no authentication required" );
85 else if( buffer[1] == 0x02 )
87 if( psz_socks_user == NULL || psz_socks_passwd == NULL )
89 msg_Err( p_obj, "socks: server mandates authentication but "
90 "a username and/or password was not supplied" );
91 return VLC_EGENERIC;
94 int const i_user = strlen( psz_socks_user );
95 int const i_pasw = strlen( psz_socks_passwd );
97 if( i_user > 255 || i_pasw > 255 )
99 msg_Err( p_obj, "socks: rejecting username and/or password due to "
100 "violation of RFC1929 (longer than 255 bytes)" );
101 return VLC_EGENERIC;
104 msg_Dbg( p_obj, "socks: username/password authentication" );
106 buffer[0] = i_socks_version; /* Version */
107 buffer[1] = i_user; /* User length */
108 memcpy( &buffer[2], psz_socks_user, i_user );
109 buffer[2+i_user] = i_pasw; /* Password length */
110 memcpy( &buffer[2+i_user+1], psz_socks_passwd, i_pasw );
112 i_len = 3 + i_user + i_pasw;
114 if( net_Write( p_obj, fd, buffer, i_len ) != i_len )
115 return VLC_EGENERIC;
117 if( net_Read( p_obj, fd, buffer, 2 ) != 2 )
118 return VLC_EGENERIC;
120 msg_Dbg( p_obj, "socks: v=%d status=%x", buffer[0], buffer[1] );
121 if( buffer[1] != 0x00 )
123 msg_Err( p_obj, "socks: authentication rejected" );
124 return VLC_EGENERIC;
127 else
129 if( b_auth )
130 msg_Err( p_obj, "socks: unsupported authentication method %x",
131 buffer[0] );
132 else
133 msg_Err( p_obj, "socks: authentication needed" );
134 return VLC_EGENERIC;
137 return VLC_SUCCESS;
140 /*****************************************************************************
141 * SocksHandshakeTCP:
142 *****************************************************************************
143 * Open a TCP connection using a SOCKS server and return a handle (RFC 1928)
144 *****************************************************************************/
145 static int SocksHandshakeTCP( vlc_object_t *p_obj,
146 int fd,
147 int i_socks_version,
148 const char *psz_user, const char *psz_passwd,
149 const char *psz_host, int i_port )
151 uint8_t buffer[128+2*256];
153 if( i_socks_version != 4 && i_socks_version != 5 )
155 msg_Warn( p_obj, "invalid socks protocol version %d", i_socks_version );
156 i_socks_version = 5;
159 if( i_socks_version == 5 &&
160 SocksNegotiate( p_obj, fd, i_socks_version,
161 psz_user, psz_passwd ) )
162 return VLC_EGENERIC;
164 if( i_socks_version == 4 )
166 /* v4 only support ipv4 */
167 static const struct addrinfo hints = {
168 .ai_family = AF_INET,
169 .ai_socktype = SOCK_STREAM,
170 .ai_protocol = IPPROTO_TCP,
171 .ai_flags = AI_IDN,
173 struct addrinfo *res;
175 if (vlc_getaddrinfo_i11e(psz_host, 0, &hints, &res))
176 return VLC_EGENERIC;
178 buffer[0] = i_socks_version;
179 buffer[1] = 0x01; /* CONNECT */
180 SetWBE( &buffer[2], i_port ); /* Port */
181 memcpy (&buffer[4], /* Address */
182 &((struct sockaddr_in *)(res->ai_addr))->sin_addr, 4);
183 freeaddrinfo (res);
185 buffer[8] = 0; /* Empty user id */
187 if( net_Write( p_obj, fd, buffer, 9 ) != 9 )
188 return VLC_EGENERIC;
189 if( net_Read( p_obj, fd, buffer, 8 ) != 8 )
190 return VLC_EGENERIC;
192 msg_Dbg( p_obj, "socks: v=%d cd=%d",
193 buffer[0], buffer[1] );
195 if( buffer[1] != 90 )
196 return VLC_EGENERIC;
198 else if( i_socks_version == 5 )
200 int i_hlen = __MIN(strlen( psz_host ), 255);
201 int i_len;
203 buffer[0] = i_socks_version; /* Version */
204 buffer[1] = 0x01; /* Cmd: connect */
205 buffer[2] = 0x00; /* Reserved */
206 buffer[3] = 3; /* ATYP: for now domainname */
208 buffer[4] = i_hlen;
209 memcpy( &buffer[5], psz_host, i_hlen );
210 SetWBE( &buffer[5+i_hlen], i_port );
212 i_len = 5 + i_hlen + 2;
215 if( net_Write( p_obj, fd, buffer, i_len ) != i_len )
216 return VLC_EGENERIC;
218 /* Read the header */
219 if( net_Read( p_obj, fd, buffer, 5 ) != 5 )
220 return VLC_EGENERIC;
222 msg_Dbg( p_obj, "socks: v=%d rep=%d atyp=%d",
223 buffer[0], buffer[1], buffer[3] );
225 if( buffer[1] != 0x00 )
227 msg_Err( p_obj, "socks: CONNECT request failed" );
228 return VLC_EGENERIC;
231 /* Read the remaining bytes */
232 if( buffer[3] == 0x01 )
233 i_len = 4-1 + 2;
234 else if( buffer[3] == 0x03 )
235 i_len = buffer[4] + 2;
236 else if( buffer[3] == 0x04 )
237 i_len = 16-1+2;
238 else
239 return VLC_EGENERIC;
241 if( net_Read( p_obj, fd, buffer, i_len ) != i_len )
242 return VLC_EGENERIC;
245 return VLC_SUCCESS;
248 int (net_ConnectTCP)(vlc_object_t *obj, const char *host, int serv)
250 const char *realhost;
251 int realserv;
253 char *socks = var_InheritString(obj, "socks");
254 if (socks != NULL)
256 realhost = socks;
258 char *p = strchr(socks, ':');
259 if (p != NULL)
261 *(p++) = '\0';
262 realserv = atoi(p);
264 else
265 realserv = 1080;
267 msg_Dbg(obj, "net: connecting to %s port %d (SOCKS) "
268 "for %s port %d", realhost, realserv, host, serv);
270 else
272 msg_Dbg(obj, "net: connecting to %s port %d", host, serv);
273 realhost = host;
274 realserv = serv;
277 int fd = net_Connect(obj, realhost, realserv, SOCK_STREAM, IPPROTO_TCP);
279 if (socks != NULL && fd != -1)
281 /* NOTE: psz_socks already free'd! */
282 char *user = var_InheritString(obj, "socks-user");
283 char *pwd = var_InheritString(obj, "socks-pwd");
285 if (SocksHandshakeTCP(obj, fd, 5, user, pwd, host, serv))
287 msg_Err(obj, "SOCKS handshake failed");
288 net_Close(fd);
289 fd = -1;
292 free(pwd);
293 free(user);
296 return fd;