1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004-2005 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@videolan.org>
8 * RĂ©mi Denis-Courmont <rem # videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 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 General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
36 #ifdef HAVE_SYS_TIME_H
37 # include <sys/time.h>
45 static int SocksNegociate( vlc_object_t *, int fd, int i_socks_version,
46 char *psz_socks_user, char *psz_socks_passwd );
47 static int SocksHandshakeTCP( vlc_object_t *,
48 int fd, int i_socks_version,
49 char *psz_socks_user, char *psz_socks_passwd,
50 const char *psz_host, int i_port );
51 extern int net_Socket( vlc_object_t *p_this, int i_family, int i_socktype,
53 extern int rootwrap_bind (int family, int socktype, int protocol,
54 const struct sockaddr *addr, size_t alen);
56 /*****************************************************************************
58 *****************************************************************************
59 * Open a TCP connection and return a handle
60 *****************************************************************************/
61 int __net_ConnectTCP( vlc_object_t *p_this, const char *psz_host, int i_port )
63 struct addrinfo hints, *res, *ptr;
64 const char *psz_realhost;
66 int i_realport, i_val, i_handle = -1;
67 vlc_bool_t b_unreach = VLC_FALSE;
70 i_port = 80; /* historical VLC thing */
72 memset( &hints, 0, sizeof( hints ) );
73 hints.ai_socktype = SOCK_STREAM;
75 psz_socks = var_CreateGetString( p_this, "socks" );
76 if( *psz_socks && *psz_socks != ':' )
78 char *psz = strchr( psz_socks, ':' );
83 psz_realhost = psz_socks;
84 i_realport = ( psz != NULL ) ? atoi( psz ) : 1080;
86 msg_Dbg( p_this, "net: connecting to %s port %d for %s port %d",
87 psz_realhost, i_realport, psz_host, i_port );
91 psz_realhost = psz_host;
94 msg_Dbg( p_this, "net: connecting to %s port %d", psz_realhost,
98 i_val = vlc_getaddrinfo( p_this, psz_realhost, i_realport, &hints, &res );
101 msg_Err( p_this, "cannot resolve %s port %d : %s", psz_realhost,
102 i_realport, vlc_gai_strerror( i_val ) );
107 for( ptr = res; (ptr != NULL) && (i_handle == -1); ptr = ptr->ai_next )
111 fd = net_Socket( p_this, ptr->ai_family, ptr->ai_socktype,
116 if( connect( fd, ptr->ai_addr, ptr->ai_addrlen ) )
118 socklen_t i_val_size = sizeof( i_val );
123 #if defined( WIN32 ) || defined( UNDER_CE )
124 if( WSAGetLastError() != WSAEWOULDBLOCK )
126 if( WSAGetLastError () == WSAENETUNREACH )
127 b_unreach = VLC_TRUE;
129 msg_Warn( p_this, "connection to %s port %d failed (%d)",
130 psz_host, i_port, WSAGetLastError( ) );
135 if( errno != EINPROGRESS )
137 if( errno == ENETUNREACH )
138 b_unreach = VLC_TRUE;
140 msg_Warn( p_this, "connection to %s port %d : %s", psz_host,
141 i_port, strerror( errno ) );
147 var_Create( p_this, "ipv4-timeout",
148 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
149 var_Get( p_this, "ipv4-timeout", &timeout );
150 if( timeout.i_int < 0 )
152 msg_Err( p_this, "invalid negative value for ipv4-timeout" );
155 d = div( timeout.i_int, 100 );
157 msg_Dbg( p_this, "connection in progress" );
164 msg_Dbg( p_this, "connection aborted" );
166 vlc_freeaddrinfo( res );
171 /* Initialize file descriptor set */
175 /* We'll wait 0.1 second if nothing happens */
177 tv.tv_usec = (d.quot > 0) ? 100000 : (1000 * d.rem);
179 i_val = select( fd + 1, NULL, &fds, NULL, &tv );
183 msg_Dbg( p_this, "connection timed out" );
191 while( ( i_val == 0 ) || ( ( i_val < 0 ) &&
192 #if defined( WIN32 ) || defined( UNDER_CE )
193 ( WSAGetLastError() == WSAEWOULDBLOCK )
200 continue; /* timeout */
204 msg_Warn( p_this, "connection aborted (select failed)" );
209 #if !defined( SYS_BEOS ) && !defined( UNDER_CE )
210 if( getsockopt( fd, SOL_SOCKET, SO_ERROR, (void*)&i_val,
211 &i_val_size ) == -1 || i_val != 0 )
213 if( i_val == ENETUNREACH )
214 b_unreach = VLC_TRUE;
218 msg_Warn( p_this, "connection to %s port %d failed (%d)",
219 psz_host, i_port, WSAGetLastError( ) );
221 msg_Warn( p_this, "connection to %s port %d : %s", psz_host,
222 i_port, strerror( i_val ) );
230 i_handle = fd; /* success! */
233 vlc_freeaddrinfo( res );
238 msg_Err( p_this, "Host %s port %d is unreachable", psz_host,
243 if( *psz_socks && *psz_socks != ':' )
245 char *psz_user = var_CreateGetString( p_this, "socks-user" );
246 char *psz_pwd = var_CreateGetString( p_this, "socks-pwd" );
248 if( SocksHandshakeTCP( p_this, i_handle, 5, psz_user, psz_pwd,
251 msg_Err( p_this, "failed to use the SOCKS server" );
252 net_Close( i_handle );
265 /*****************************************************************************
267 *****************************************************************************
268 * Open TCP passive "listening" socket(s)
269 * This function returns NULL in case of error.
270 *****************************************************************************/
271 int *__net_ListenTCP( vlc_object_t *p_this, const char *psz_host, int i_port )
273 struct addrinfo hints, *res, *ptr;
274 int i_val, *pi_handles, i_size;
276 memset( &hints, 0, sizeof( hints ) );
277 hints.ai_socktype = SOCK_STREAM;
278 hints.ai_flags = AI_PASSIVE;
280 msg_Dbg( p_this, "net: listening to %s port %d", psz_host, i_port );
282 i_val = vlc_getaddrinfo( p_this, psz_host, i_port, &hints, &res );
285 msg_Err( p_this, "cannot resolve %s port %d : %s", psz_host, i_port,
286 vlc_gai_strerror( i_val ) );
293 for( ptr = res; ptr != NULL; ptr = ptr->ai_next )
297 fd = net_Socket( p_this, ptr->ai_family, ptr->ai_socktype,
302 /* Bind the socket */
303 if( bind( fd, ptr->ai_addr, ptr->ai_addrlen ) )
305 #if defined(WIN32) || defined(UNDER_CE)
306 msg_Warn( p_this, "cannot bind socket (%i)", WSAGetLastError( ) );
314 fd = rootwrap_bind( ptr->ai_family, ptr->ai_socktype,
315 ptr->ai_protocol, ptr->ai_addr,
319 msg_Dbg( p_this, "got socket %d from rootwrap", fd );
323 msg_Warn( p_this, "cannot bind socket (%s)",
324 strerror( saved_errno ) );
331 if( listen( fd, 100 ) == -1 )
333 #if defined(WIN32) || defined(UNDER_CE)
334 msg_Err( p_this, "cannot bring socket in listening mode (%i)",
337 msg_Err( p_this, "cannot bring the socket in listening mode (%s)",
344 newpi = (int *)realloc( pi_handles, (++i_size) * sizeof( int ) );
352 newpi[i_size - 2] = fd;
357 vlc_freeaddrinfo( res );
359 if( pi_handles != NULL )
360 pi_handles[i_size - 1] = -1;
364 /*****************************************************************************
366 *****************************************************************************
367 * Accept a connection on a set of listening sockets and return it
368 *****************************************************************************/
369 int __net_Accept( vlc_object_t *p_this, int *pi_fd, mtime_t i_wait )
371 vlc_bool_t b_die = p_this->b_die, b_block = (i_wait < 0);
373 while( p_this->b_die == b_die )
375 int i_val = -1, *pi, *pi_end;
376 struct timeval timeout;
381 /* Initialize file descriptor set */
385 for( pi = pi_fd; *pi != -1; pi++ )
392 FD_SET( i_fd, &fds_r );
393 FD_SET( i_fd, &fds_e );
398 timeout.tv_usec = b_block ? 500000 : i_wait;
400 i_val = select( i_val + 1, &fds_r, NULL, &fds_e, &timeout );
401 if( ( ( i_val < 0 ) && ( errno == EINTR ) ) || i_val == 0 )
410 #if defined(WIN32) || defined(UNDER_CE)
411 msg_Err( p_this, "network select error (%i)", WSAGetLastError() );
413 msg_Err( p_this, "network select error (%s)", strerror( errno ) );
418 for( pi = pi_fd; *pi != -1; pi++ )
422 if( !FD_ISSET( i_fd, &fds_r ) && !FD_ISSET( i_fd, &fds_e ) )
425 i_val = accept( i_fd, NULL, 0 );
428 #if defined(WIN32) || defined(UNDER_CE)
429 msg_Err( p_this, "accept failed (%i)", WSAGetLastError() );
431 msg_Err( p_this, "accept failed (%s)", strerror( errno ) );
437 * This round-robin trick ensures that the first sockets in
438 * pi_fd won't prevent the last ones from getting accept'ed.
441 memmove( pi, pi + 1, pi_end - pi );
452 /*****************************************************************************
454 *****************************************************************************
455 * Negociate authentication with a SOCKS server.
456 *****************************************************************************/
457 static int SocksNegociate( vlc_object_t *p_obj,
458 int fd, int i_socks_version,
459 char *psz_socks_user,
460 char *psz_socks_passwd )
462 uint8_t buffer[128+2*256];
464 vlc_bool_t b_auth = VLC_FALSE;
466 if( i_socks_version != 5 )
469 /* We negociate authentication */
471 if( psz_socks_user && psz_socks_passwd &&
472 *psz_socks_user && *psz_socks_passwd )
475 buffer[0] = i_socks_version; /* SOCKS version */
478 buffer[1] = 2; /* Number of methods */
479 buffer[2] = 0x00; /* - No auth required */
480 buffer[3] = 0x02; /* - USer/Password */
485 buffer[1] = 1; /* Number of methods */
486 buffer[2] = 0x00; /* - No auth required */
490 if( net_Write( p_obj, fd, NULL, buffer, i_len ) != i_len )
492 if( net_Read( p_obj, fd, NULL, buffer, 2, VLC_TRUE ) != 2 )
495 msg_Dbg( p_obj, "socks: v=%d method=%x", buffer[0], buffer[1] );
497 if( buffer[1] == 0x00 )
499 msg_Dbg( p_obj, "socks: no authentication required" );
501 else if( buffer[1] == 0x02 )
503 int i_len1 = __MIN( strlen(psz_socks_user), 255 );
504 int i_len2 = __MIN( strlen(psz_socks_passwd), 255 );
505 msg_Dbg( p_obj, "socks: username/password authentication" );
507 /* XXX: we don't support user/pwd > 255 (truncated)*/
508 buffer[0] = i_socks_version; /* Version */
509 buffer[1] = i_len1; /* User length */
510 memcpy( &buffer[2], psz_socks_user, i_len1 );
511 buffer[2+i_len1] = i_len2; /* Password length */
512 memcpy( &buffer[2+i_len1+1], psz_socks_passwd, i_len2 );
514 i_len = 3 + i_len1 + i_len2;
516 if( net_Write( p_obj, fd, NULL, buffer, i_len ) != i_len )
519 if( net_Read( p_obj, fd, NULL, buffer, 2, VLC_TRUE ) != 2 )
522 msg_Dbg( p_obj, "socks: v=%d status=%x", buffer[0], buffer[1] );
523 if( buffer[1] != 0x00 )
525 msg_Err( p_obj, "socks: authentication rejected" );
532 msg_Err( p_obj, "socks: unsupported authentication method %x",
535 msg_Err( p_obj, "socks: authentification needed" );
542 /*****************************************************************************
544 *****************************************************************************
545 * Open a TCP connection using a SOCKS server and return a handle (RFC 1928)
546 *****************************************************************************/
547 static int SocksHandshakeTCP( vlc_object_t *p_obj,
550 char *psz_socks_user, char *psz_socks_passwd,
551 const char *psz_host, int i_port )
553 uint8_t buffer[128+2*256];
555 if( i_socks_version != 4 && i_socks_version != 5 )
557 msg_Warn( p_obj, "invalid socks protocol version %d", i_socks_version );
561 if( i_socks_version == 5 &&
562 SocksNegociate( p_obj, fd, i_socks_version,
563 psz_socks_user, psz_socks_passwd ) )
566 if( i_socks_version == 4 )
568 struct addrinfo hints = { 0 }, *p_res;
570 /* v4 only support ipv4 */
571 hints.ai_family = AF_INET;
572 if( vlc_getaddrinfo( p_obj, psz_host, 0, &hints, &p_res ) )
575 buffer[0] = i_socks_version;
576 buffer[1] = 0x01; /* CONNECT */
577 SetWBE( &buffer[2], i_port ); /* Port */
578 memcpy( &buffer[4], /* Address */
579 &((struct sockaddr_in *)(p_res->ai_addr))->sin_addr, 4 );
580 vlc_freeaddrinfo( p_res );
582 buffer[8] = 0; /* Empty user id */
584 if( net_Write( p_obj, fd, NULL, buffer, 9 ) != 9 )
586 if( net_Read( p_obj, fd, NULL, buffer, 8, VLC_TRUE ) != 8 )
589 msg_Dbg( p_obj, "socks: v=%d cd=%d",
590 buffer[0], buffer[1] );
592 if( buffer[1] != 90 )
595 else if( i_socks_version == 5 )
597 int i_hlen = __MIN(strlen( psz_host ), 255);
600 buffer[0] = i_socks_version; /* Version */
601 buffer[1] = 0x01; /* Cmd: connect */
602 buffer[2] = 0x00; /* Reserved */
603 buffer[3] = 3; /* ATYP: for now domainname */
606 memcpy( &buffer[5], psz_host, i_hlen );
607 SetWBE( &buffer[5+i_hlen], i_port );
609 i_len = 5 + i_hlen + 2;
612 if( net_Write( p_obj, fd, NULL, buffer, i_len ) != i_len )
615 /* Read the header */
616 if( net_Read( p_obj, fd, NULL, buffer, 5, VLC_TRUE ) != 5 )
619 msg_Dbg( p_obj, "socks: v=%d rep=%d atyp=%d",
620 buffer[0], buffer[1], buffer[3] );
622 if( buffer[1] != 0x00 )
624 msg_Err( p_obj, "socks: CONNECT request failed\n" );
628 /* Read the remaining bytes */
629 if( buffer[3] == 0x01 )
631 else if( buffer[3] == 0x03 )
632 i_len = buffer[4] + 2;
633 else if( buffer[3] == 0x04 )
638 if( net_Read( p_obj, fd, NULL, buffer, i_len, VLC_TRUE ) != i_len )
645 void net_ListenClose( int *pi_fd )
651 for( pi = pi_fd; *pi != -1; pi++ )