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>
43 #include <vlc_network.h>
46 # define EINPROGRESS WSAEWOULDBLOCK
48 # define EWOULDBLOCK WSAEWOULDBLOCK
50 # define EAGAIN WSAEWOULDBLOCK
52 #include <vlc_interrupt.h>
54 static int SocksNegotiate( vlc_object_t
*, int fd
, int i_socks_version
,
55 const char *psz_user
, const char *psz_passwd
);
56 static int SocksHandshakeTCP( vlc_object_t
*,
57 int fd
, int i_socks_version
,
58 const char *psz_user
, const char *psz_passwd
,
59 const char *psz_host
, int i_port
);
60 extern int net_Socket( vlc_object_t
*p_this
, int i_family
, int i_socktype
,
64 /*****************************************************************************
66 *****************************************************************************
67 * Open a network connection.
68 * @return socket handler or -1 on error.
69 *****************************************************************************/
70 int net_Connect( vlc_object_t
*p_this
, const char *psz_host
, int i_port
,
73 const char *psz_realhost
;
75 int i_realport
, i_handle
= -1;
77 psz_socks
= var_InheritString( p_this
, "socks" );
78 if( psz_socks
!= NULL
)
80 char *psz
= strchr( psz_socks
, ':' );
85 psz_realhost
= psz_socks
;
86 i_realport
= ( psz
!= NULL
) ? atoi( psz
) : 1080;
88 msg_Dbg( p_this
, "net: connecting to %s port %d (SOCKS) "
89 "for %s port %d", psz_realhost
, i_realport
,
92 /* We only implement TCP with SOCKS */
100 msg_Err( p_this
, "Socket type not supported through SOCKS" );
111 msg_Err( p_this
, "Transport not supported through SOCKS" );
118 psz_realhost
= psz_host
;
121 msg_Dbg( p_this
, "net: connecting to %s port %d", psz_realhost
,
125 struct addrinfo hints
= {
127 .ai_protocol
= proto
,
128 .ai_flags
= AI_NUMERICSERV
| AI_IDN
,
131 int val
= vlc_getaddrinfo_i11e(psz_realhost
, i_realport
, &hints
, &res
);
134 msg_Err (p_this
, "cannot resolve %s port %d : %s", psz_realhost
,
135 i_realport
, gai_strerror (val
));
141 mtime_t timeout
= var_InheritInteger(p_this
, "ipv4-timeout")
142 * (CLOCK_FREQ
/ 1000);
144 for (struct addrinfo
*ptr
= res
; ptr
!= NULL
; ptr
= ptr
->ai_next
)
146 int fd
= net_Socket( p_this
, ptr
->ai_family
,
147 ptr
->ai_socktype
, ptr
->ai_protocol
);
150 msg_Dbg( p_this
, "socket error: %s", vlc_strerror_c(net_errno
) );
154 if( connect( fd
, ptr
->ai_addr
, ptr
->ai_addrlen
) )
156 if( net_errno
!= EINPROGRESS
&& errno
!= EINTR
)
158 msg_Err( p_this
, "connection failed: %s",
159 vlc_strerror_c(net_errno
) );
164 mtime_t deadline
= VLC_TS_INVALID
;
167 ufd
.events
= POLLOUT
;
168 deadline
= mdate() + timeout
;
172 mtime_t now
= mdate();
180 val
= vlc_poll_i11e(&ufd
, 1, (deadline
- now
) / 1000);
182 while (val
== -1 && errno
== EINTR
);
187 msg_Err (p_this
, "polling error: %s",
188 vlc_strerror_c(net_errno
));
191 case 0: /* timeout */
192 msg_Warn (p_this
, "connection timed out");
196 /* There is NO WAY around checking SO_ERROR.
197 * Don't ifdef it out!!! */
198 if (getsockopt (fd
, SOL_SOCKET
, SO_ERROR
, &val
,
199 &(socklen_t
){ sizeof (val
) }) || val
)
201 msg_Err (p_this
, "connection failed: %s",
202 vlc_strerror_c(val
));
207 msg_Dbg( p_this
, "connection succeeded (socket = %d)", fd
);
208 i_handle
= fd
; /* success! */
211 next_ai
: /* failure */
220 if( psz_socks
!= NULL
)
222 /* NOTE: psz_socks already free'd! */
223 char *psz_user
= var_InheritString( p_this
, "socks-user" );
224 char *psz_pwd
= var_InheritString( p_this
, "socks-pwd" );
226 if( SocksHandshakeTCP( p_this
, i_handle
, 5, psz_user
, psz_pwd
,
229 msg_Err( p_this
, "SOCKS handshake failed" );
230 net_Close( i_handle
);
242 int net_AcceptSingle (vlc_object_t
*obj
, int lfd
)
244 int fd
= vlc_accept (lfd
, NULL
, NULL
, true);
247 if (net_errno
!= EAGAIN
)
248 #if (EAGAIN != EWOULDBLOCK)
249 if (net_errno
!= EWOULDBLOCK
)
251 msg_Err (obj
, "accept failed (from socket %d): %s", lfd
,
252 vlc_strerror_c(net_errno
));
256 msg_Dbg (obj
, "accepted socket %d (from socket %d)", fd
, lfd
);
257 setsockopt (fd
, SOL_SOCKET
, SO_REUSEADDR
, &(int){ 1 }, sizeof(int));
264 * Accepts an new connection on a set of listening sockets.
265 * If there are no pending connections, this function will wait.
266 * @note If the thread needs to handle events other than incoming connections,
267 * you need to use poll() and net_AcceptSingle() instead.
269 * @param p_this VLC object for logging and object kill signal
270 * @param pi_fd listening socket set
271 * @return -1 on error (may be transient error due to network issues),
272 * a new socket descriptor on success.
274 int net_Accept (vlc_object_t
*p_this
, int *pi_fd
)
276 assert (pi_fd
!= NULL
);
279 while (pi_fd
[n
] != -1)
282 struct pollfd ufd
[n
];
283 /* Initialize file descriptor set */
284 for (unsigned i
= 0; i
< n
; i
++)
286 ufd
[i
].fd
= pi_fd
[i
];
287 ufd
[i
].events
= POLLIN
;
292 while (poll (ufd
, n
, -1) == -1)
294 if (net_errno
!= EINTR
)
296 msg_Err (p_this
, "poll error: %s", vlc_strerror_c(net_errno
));
301 for (unsigned i
= 0; i
< n
; i
++)
303 if (ufd
[i
].revents
== 0)
307 int fd
= net_AcceptSingle (p_this
, sfd
);
312 * Move listening socket to the end to let the others in the
313 * set a chance next time.
315 memmove (pi_fd
+ i
, pi_fd
+ i
+ 1, n
- (i
+ 1));
324 /*****************************************************************************
326 *****************************************************************************
327 * Negotiate authentication with a SOCKS server.
328 *****************************************************************************/
329 static int SocksNegotiate( vlc_object_t
*p_obj
,
330 int fd
, int i_socks_version
,
331 const char *psz_socks_user
,
332 const char *psz_socks_passwd
)
334 uint8_t buffer
[128+2*256];
338 if( i_socks_version
!= 5 )
341 /* We negotiate authentication */
342 buffer
[0] = i_socks_version
; /* SOCKS version */
343 if( psz_socks_user
!= NULL
&& psz_socks_passwd
!= NULL
)
345 buffer
[1] = 2; /* Number of methods */
346 buffer
[2] = 0x00; /* - No auth required */
347 buffer
[3] = 0x02; /* - USer/Password */
353 buffer
[1] = 1; /* Number of methods */
354 buffer
[2] = 0x00; /* - No auth required */
358 if( net_Write( p_obj
, fd
, buffer
, i_len
) != i_len
)
360 if( net_Read( p_obj
, fd
, buffer
, 2) != 2 )
363 msg_Dbg( p_obj
, "socks: v=%d method=%x", buffer
[0], buffer
[1] );
365 if( buffer
[1] == 0x00 )
367 msg_Dbg( p_obj
, "socks: no authentication required" );
369 else if( buffer
[1] == 0x02 )
371 if( psz_socks_user
== NULL
|| psz_socks_passwd
== NULL
)
373 msg_Err( p_obj
, "socks: server mandates authentication but "
374 "a username and/or password was not supplied" );
378 int const i_user
= strlen( psz_socks_user
);
379 int const i_pasw
= strlen( psz_socks_passwd
);
381 if( i_user
> 255 || i_pasw
> 255 )
383 msg_Err( p_obj
, "socks: rejecting username and/or password due to "
384 "violation of RFC1929 (longer than 255 bytes)" );
388 msg_Dbg( p_obj
, "socks: username/password authentication" );
390 buffer
[0] = i_socks_version
; /* Version */
391 buffer
[1] = i_user
; /* User length */
392 memcpy( &buffer
[2], psz_socks_user
, i_user
);
393 buffer
[2+i_user
] = i_pasw
; /* Password length */
394 memcpy( &buffer
[2+i_user
+1], psz_socks_passwd
, i_pasw
);
396 i_len
= 3 + i_user
+ i_pasw
;
398 if( net_Write( p_obj
, fd
, buffer
, i_len
) != i_len
)
401 if( net_Read( p_obj
, fd
, buffer
, 2 ) != 2 )
404 msg_Dbg( p_obj
, "socks: v=%d status=%x", buffer
[0], buffer
[1] );
405 if( buffer
[1] != 0x00 )
407 msg_Err( p_obj
, "socks: authentication rejected" );
414 msg_Err( p_obj
, "socks: unsupported authentication method %x",
417 msg_Err( p_obj
, "socks: authentication needed" );
424 /*****************************************************************************
426 *****************************************************************************
427 * Open a TCP connection using a SOCKS server and return a handle (RFC 1928)
428 *****************************************************************************/
429 static int SocksHandshakeTCP( vlc_object_t
*p_obj
,
432 const char *psz_user
, const char *psz_passwd
,
433 const char *psz_host
, int i_port
)
435 uint8_t buffer
[128+2*256];
437 if( i_socks_version
!= 4 && i_socks_version
!= 5 )
439 msg_Warn( p_obj
, "invalid socks protocol version %d", i_socks_version
);
443 if( i_socks_version
== 5 &&
444 SocksNegotiate( p_obj
, fd
, i_socks_version
,
445 psz_user
, psz_passwd
) )
448 if( i_socks_version
== 4 )
450 /* v4 only support ipv4 */
451 static const struct addrinfo hints
= {
452 .ai_family
= AF_INET
,
453 .ai_socktype
= SOCK_STREAM
,
454 .ai_protocol
= IPPROTO_TCP
,
457 struct addrinfo
*res
;
459 if (vlc_getaddrinfo_i11e(psz_host
, 0, &hints
, &res
))
462 buffer
[0] = i_socks_version
;
463 buffer
[1] = 0x01; /* CONNECT */
464 SetWBE( &buffer
[2], i_port
); /* Port */
465 memcpy (&buffer
[4], /* Address */
466 &((struct sockaddr_in
*)(res
->ai_addr
))->sin_addr
, 4);
469 buffer
[8] = 0; /* Empty user id */
471 if( net_Write( p_obj
, fd
, buffer
, 9 ) != 9 )
473 if( net_Read( p_obj
, fd
, buffer
, 8 ) != 8 )
476 msg_Dbg( p_obj
, "socks: v=%d cd=%d",
477 buffer
[0], buffer
[1] );
479 if( buffer
[1] != 90 )
482 else if( i_socks_version
== 5 )
484 int i_hlen
= __MIN(strlen( psz_host
), 255);
487 buffer
[0] = i_socks_version
; /* Version */
488 buffer
[1] = 0x01; /* Cmd: connect */
489 buffer
[2] = 0x00; /* Reserved */
490 buffer
[3] = 3; /* ATYP: for now domainname */
493 memcpy( &buffer
[5], psz_host
, i_hlen
);
494 SetWBE( &buffer
[5+i_hlen
], i_port
);
496 i_len
= 5 + i_hlen
+ 2;
499 if( net_Write( p_obj
, fd
, buffer
, i_len
) != i_len
)
502 /* Read the header */
503 if( net_Read( p_obj
, fd
, buffer
, 5 ) != 5 )
506 msg_Dbg( p_obj
, "socks: v=%d rep=%d atyp=%d",
507 buffer
[0], buffer
[1], buffer
[3] );
509 if( buffer
[1] != 0x00 )
511 msg_Err( p_obj
, "socks: CONNECT request failed" );
515 /* Read the remaining bytes */
516 if( buffer
[3] == 0x01 )
518 else if( buffer
[3] == 0x03 )
519 i_len
= buffer
[4] + 2;
520 else if( buffer
[3] == 0x04 )
525 if( net_Read( p_obj
, fd
, buffer
, i_len
) != i_len
)
532 void net_ListenClose( int *pi_fd
)
538 for( pi
= pi_fd
; *pi
!= -1; pi
++ )