1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004-2005 VLC authors and VideoLAN
5 * Copyright (C) 2005-2006 Rémi Denis-Courmont
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 /*****************************************************************************
28 *****************************************************************************/
33 #include <vlc_common.h>
37 #include <vlc_network.h>
38 #include <vlc_interrupt.h>
40 /*****************************************************************************
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];
54 if( i_socks_version
!= 5 )
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 */
69 buffer
[1] = 1; /* Number of methods */
70 buffer
[2] = 0x00; /* - No auth required */
74 if( net_Write( p_obj
, fd
, buffer
, i_len
) != i_len
)
76 if( net_Read( p_obj
, fd
, buffer
, 2) != 2 )
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" );
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)" );
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
)
117 if( net_Read( p_obj
, fd
, buffer
, 2 ) != 2 )
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" );
130 msg_Err( p_obj
, "socks: unsupported authentication method %x",
133 msg_Err( p_obj
, "socks: authentication needed" );
140 /*****************************************************************************
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
,
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
);
159 if( i_socks_version
== 5 &&
160 SocksNegotiate( p_obj
, fd
, i_socks_version
,
161 psz_user
, psz_passwd
) )
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
,
173 struct addrinfo
*res
;
175 if (vlc_getaddrinfo_i11e(psz_host
, 0, &hints
, &res
))
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);
185 buffer
[8] = 0; /* Empty user id */
187 if( net_Write( p_obj
, fd
, buffer
, 9 ) != 9 )
189 if( net_Read( p_obj
, fd
, buffer
, 8 ) != 8 )
192 msg_Dbg( p_obj
, "socks: v=%d cd=%d",
193 buffer
[0], buffer
[1] );
195 if( buffer
[1] != 90 )
198 else if( i_socks_version
== 5 )
200 int i_hlen
= __MIN(strlen( psz_host
), 255);
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 */
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
)
218 /* Read the header */
219 if( net_Read( p_obj
, fd
, buffer
, 5 ) != 5 )
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" );
231 /* Read the remaining bytes */
232 if( buffer
[3] == 0x01 )
234 else if( buffer
[3] == 0x03 )
235 i_len
= buffer
[4] + 2;
236 else if( buffer
[3] == 0x04 )
241 if( net_Read( p_obj
, fd
, buffer
, i_len
) != i_len
)
248 int (net_ConnectTCP
)(vlc_object_t
*obj
, const char *host
, int serv
)
250 const char *realhost
;
253 char *socks
= var_InheritString(obj
, "socks");
258 char *p
= strchr(socks
, ':');
267 msg_Dbg(obj
, "net: connecting to %s port %d (SOCKS) "
268 "for %s port %d", realhost
, realserv
, host
, serv
);
272 msg_Dbg(obj
, "net: connecting to %s port %d", host
, 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");