1 /*****************************************************************************
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>
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 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc_common.h>
36 #include <vlc_network.h>
37 #include <vlc_interrupt.h>
39 /*****************************************************************************
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];
53 if( i_socks_version
!= 5 )
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 */
68 buffer
[1] = 1; /* Number of methods */
69 buffer
[2] = 0x00; /* - No auth required */
73 if( net_Write( p_obj
, fd
, buffer
, i_len
) != i_len
)
75 if( net_Read( p_obj
, fd
, buffer
, 2) != 2 )
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" );
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)" );
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
)
116 if( net_Read( p_obj
, fd
, buffer
, 2 ) != 2 )
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" );
129 msg_Err( p_obj
, "socks: unsupported authentication method %x",
132 msg_Err( p_obj
, "socks: authentication needed" );
139 /*****************************************************************************
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
,
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
);
158 if( i_socks_version
== 5 &&
159 SocksNegotiate( p_obj
, fd
, i_socks_version
,
160 psz_user
, psz_passwd
) )
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
,
172 struct addrinfo
*res
;
174 if (vlc_getaddrinfo_i11e(psz_host
, 0, &hints
, &res
))
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);
184 buffer
[8] = 0; /* Empty user id */
186 if( net_Write( p_obj
, fd
, buffer
, 9 ) != 9 )
188 if( net_Read( p_obj
, fd
, buffer
, 8 ) != 8 )
191 msg_Dbg( p_obj
, "socks: v=%d cd=%d",
192 buffer
[0], buffer
[1] );
194 if( buffer
[1] != 90 )
197 else if( i_socks_version
== 5 )
199 int i_hlen
= __MIN(strlen( psz_host
), 255);
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 */
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
)
217 /* Read the header */
218 if( net_Read( p_obj
, fd
, buffer
, 5 ) != 5 )
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" );
230 /* Read the remaining bytes */
231 if( buffer
[3] == 0x01 )
233 else if( buffer
[3] == 0x03 )
234 i_len
= buffer
[4] + 2;
235 else if( buffer
[3] == 0x04 )
240 if( net_Read( p_obj
, fd
, buffer
, i_len
) != i_len
)
247 int (net_ConnectTCP
)(vlc_object_t
*obj
, const char *host
, int serv
)
249 const char *realhost
;
252 char *socks
= var_InheritString(obj
, "socks");
257 char *p
= strchr(socks
, ':');
266 msg_Dbg(obj
, "net: connecting to %s port %d (SOCKS) "
267 "for %s port %d", realhost
, realserv
, host
, serv
);
271 msg_Dbg(obj
, "net: connecting to %s port %d", host
, 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");