qt: playlist: use item title if available
[vlc.git] / src / network / tcp.c
blob3caadab380799f1de4fc077203342d78b6561810
1 /*****************************************************************************
2 * tcp.c:
3 *****************************************************************************
4 * Copyright (C) 2004-2005 VLC authors and VideoLAN
5 * Copyright (C) 2005-2006 Rémi Denis-Courmont
7 * Authors: Laurent Aimar <fenrir@videolan.org>
8 * Rémi Denis-Courmont
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <vlc_common.h>
34 #include <unistd.h>
36 #include <vlc_network.h>
37 #include <vlc_interrupt.h>
39 /*****************************************************************************
40 * SocksNegotiate:
41 *****************************************************************************
42 * Negotiate authentication with a SOCKS server.
43 *****************************************************************************/
44 static int SocksNegotiate( vlc_object_t *p_obj,
45 int fd, int i_socks_version,
46 const char *psz_socks_user,
47 const char *psz_socks_passwd )
49 uint8_t buffer[128+2*256];
50 int i_len;
51 bool b_auth = false;
53 if( i_socks_version != 5 )
54 return VLC_SUCCESS;
56 /* We negotiate authentication */
57 buffer[0] = i_socks_version; /* SOCKS version */
58 if( psz_socks_user != NULL && psz_socks_passwd != NULL )
60 buffer[1] = 2; /* Number of methods */
61 buffer[2] = 0x00; /* - No auth required */
62 buffer[3] = 0x02; /* - USer/Password */
63 i_len = 4;
64 b_auth = true;
66 else
68 buffer[1] = 1; /* Number of methods */
69 buffer[2] = 0x00; /* - No auth required */
70 i_len = 3;
73 if( net_Write( p_obj, fd, buffer, i_len ) != i_len )
74 return VLC_EGENERIC;
75 if( net_Read( p_obj, fd, buffer, 2) != 2 )
76 return VLC_EGENERIC;
78 msg_Dbg( p_obj, "socks: v=%d method=%x", buffer[0], buffer[1] );
80 if( buffer[1] == 0x00 )
82 msg_Dbg( p_obj, "socks: no authentication required" );
84 else if( buffer[1] == 0x02 )
86 if( psz_socks_user == NULL || psz_socks_passwd == NULL )
88 msg_Err( p_obj, "socks: server mandates authentication but "
89 "a username and/or password was not supplied" );
90 return VLC_EGENERIC;
93 int const i_user = strlen( psz_socks_user );
94 int const i_pasw = strlen( psz_socks_passwd );
96 if( i_user > 255 || i_pasw > 255 )
98 msg_Err( p_obj, "socks: rejecting username and/or password due to "
99 "violation of RFC1929 (longer than 255 bytes)" );
100 return VLC_EGENERIC;
103 msg_Dbg( p_obj, "socks: username/password authentication" );
105 buffer[0] = i_socks_version; /* Version */
106 buffer[1] = i_user; /* User length */
107 memcpy( &buffer[2], psz_socks_user, i_user );
108 buffer[2+i_user] = i_pasw; /* Password length */
109 memcpy( &buffer[2+i_user+1], psz_socks_passwd, i_pasw );
111 i_len = 3 + i_user + i_pasw;
113 if( net_Write( p_obj, fd, buffer, i_len ) != i_len )
114 return VLC_EGENERIC;
116 if( net_Read( p_obj, fd, buffer, 2 ) != 2 )
117 return VLC_EGENERIC;
119 msg_Dbg( p_obj, "socks: v=%d status=%x", buffer[0], buffer[1] );
120 if( buffer[1] != 0x00 )
122 msg_Err( p_obj, "socks: authentication rejected" );
123 return VLC_EGENERIC;
126 else
128 if( b_auth )
129 msg_Err( p_obj, "socks: unsupported authentication method %x",
130 buffer[0] );
131 else
132 msg_Err( p_obj, "socks: authentication needed" );
133 return VLC_EGENERIC;
136 return VLC_SUCCESS;
139 /*****************************************************************************
140 * SocksHandshakeTCP:
141 *****************************************************************************
142 * Open a TCP connection using a SOCKS server and return a handle (RFC 1928)
143 *****************************************************************************/
144 static int SocksHandshakeTCP( vlc_object_t *p_obj,
145 int fd,
146 int i_socks_version,
147 const char *psz_user, const char *psz_passwd,
148 const char *psz_host, int i_port )
150 uint8_t buffer[128+2*256];
152 if( i_socks_version != 4 && i_socks_version != 5 )
154 msg_Warn( p_obj, "invalid socks protocol version %d", i_socks_version );
155 i_socks_version = 5;
158 if( i_socks_version == 5 &&
159 SocksNegotiate( p_obj, fd, i_socks_version,
160 psz_user, psz_passwd ) )
161 return VLC_EGENERIC;
163 if( i_socks_version == 4 )
165 /* v4 only support ipv4 */
166 static const struct addrinfo hints = {
167 .ai_family = AF_INET,
168 .ai_socktype = SOCK_STREAM,
169 .ai_protocol = IPPROTO_TCP,
170 .ai_flags = AI_IDN,
172 struct addrinfo *res;
174 if (vlc_getaddrinfo_i11e(psz_host, 0, &hints, &res))
175 return VLC_EGENERIC;
177 buffer[0] = i_socks_version;
178 buffer[1] = 0x01; /* CONNECT */
179 SetWBE( &buffer[2], i_port ); /* Port */
180 memcpy (&buffer[4], /* Address */
181 &((struct sockaddr_in *)(res->ai_addr))->sin_addr, 4);
182 freeaddrinfo (res);
184 buffer[8] = 0; /* Empty user id */
186 if( net_Write( p_obj, fd, buffer, 9 ) != 9 )
187 return VLC_EGENERIC;
188 if( net_Read( p_obj, fd, buffer, 8 ) != 8 )
189 return VLC_EGENERIC;
191 msg_Dbg( p_obj, "socks: v=%d cd=%d",
192 buffer[0], buffer[1] );
194 if( buffer[1] != 90 )
195 return VLC_EGENERIC;
197 else if( i_socks_version == 5 )
199 int i_hlen = __MIN(strlen( psz_host ), 255);
200 int i_len;
202 buffer[0] = i_socks_version; /* Version */
203 buffer[1] = 0x01; /* Cmd: connect */
204 buffer[2] = 0x00; /* Reserved */
205 buffer[3] = 3; /* ATYP: for now domainname */
207 buffer[4] = i_hlen;
208 memcpy( &buffer[5], psz_host, i_hlen );
209 SetWBE( &buffer[5+i_hlen], i_port );
211 i_len = 5 + i_hlen + 2;
214 if( net_Write( p_obj, fd, buffer, i_len ) != i_len )
215 return VLC_EGENERIC;
217 /* Read the header */
218 if( net_Read( p_obj, fd, buffer, 5 ) != 5 )
219 return VLC_EGENERIC;
221 msg_Dbg( p_obj, "socks: v=%d rep=%d atyp=%d",
222 buffer[0], buffer[1], buffer[3] );
224 if( buffer[1] != 0x00 )
226 msg_Err( p_obj, "socks: CONNECT request failed" );
227 return VLC_EGENERIC;
230 /* Read the remaining bytes */
231 if( buffer[3] == 0x01 )
232 i_len = 4-1 + 2;
233 else if( buffer[3] == 0x03 )
234 i_len = buffer[4] + 2;
235 else if( buffer[3] == 0x04 )
236 i_len = 16-1+2;
237 else
238 return VLC_EGENERIC;
240 if( net_Read( p_obj, fd, buffer, i_len ) != i_len )
241 return VLC_EGENERIC;
244 return VLC_SUCCESS;
247 int (net_ConnectTCP)(vlc_object_t *obj, const char *host, int serv)
249 const char *realhost;
250 int realserv;
252 char *socks = var_InheritString(obj, "socks");
253 if (socks != NULL)
255 realhost = socks;
257 char *p = strchr(socks, ':');
258 if (p != NULL)
260 *(p++) = '\0';
261 realserv = atoi(p);
263 else
264 realserv = 1080;
266 msg_Dbg(obj, "net: connecting to %s port %d (SOCKS) "
267 "for %s port %d", realhost, realserv, host, serv);
269 else
271 msg_Dbg(obj, "net: connecting to %s port %d", host, serv);
272 realhost = host;
273 realserv = serv;
276 int fd = net_Connect(obj, realhost, realserv, SOCK_STREAM, IPPROTO_TCP);
278 if (socks != NULL && fd != -1)
280 /* NOTE: psz_socks already free'd! */
281 char *user = var_InheritString(obj, "socks-user");
282 char *pwd = var_InheritString(obj, "socks-pwd");
284 if (SocksHandshakeTCP(obj, fd, 5, user, pwd, host, serv))
286 msg_Err(obj, "SOCKS handshake failed");
287 net_Close(fd);
288 fd = -1;
291 free(pwd);
292 free(user);
295 return fd;