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., 59 Temple Place - Suite 330, Boston, MA 02111, 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 ) );
330 msg_Dbg( p_this, "using socket %d from rootwrap", fd );
332 if( listen( fd, 100 ) == -1 )
334 #if defined(WIN32) || defined(UNDER_CE)
335 msg_Err( p_this, "cannot bring socket in listening mode (%i)",
338 msg_Err( p_this, "cannot bring the socket in listening mode (%s)",
345 newpi = (int *)realloc( pi_handles, (++i_size) * sizeof( int ) );
353 newpi[i_size - 2] = fd;
358 vlc_freeaddrinfo( res );
360 if( pi_handles != NULL )
361 pi_handles[i_size - 1] = -1;
365 /*****************************************************************************
367 *****************************************************************************
368 * Accept a connection on a set of listening sockets and return it
369 *****************************************************************************/
370 int __net_Accept( vlc_object_t *p_this, int *pi_fd, mtime_t i_wait )
372 vlc_bool_t b_die = p_this->b_die, b_block = (i_wait < 0);
374 while( p_this->b_die == b_die )
376 int i_val = -1, *pi, *pi_end;
377 struct timeval timeout;
382 /* Initialize file descriptor set */
386 for( pi = pi_fd; *pi != -1; pi++ )
393 FD_SET( i_fd, &fds_r );
394 FD_SET( i_fd, &fds_e );
399 timeout.tv_usec = b_block ? 500000 : i_wait;
401 i_val = select( i_val + 1, &fds_r, NULL, &fds_e, &timeout );
402 if( ( ( i_val < 0 ) && ( errno == EINTR ) ) || i_val == 0 )
411 #if defined(WIN32) || defined(UNDER_CE)
412 msg_Err( p_this, "network select error (%i)", WSAGetLastError() );
414 msg_Err( p_this, "network select error (%s)", strerror( errno ) );
419 for( pi = pi_fd; *pi != -1; pi++ )
423 if( !FD_ISSET( i_fd, &fds_r ) && !FD_ISSET( i_fd, &fds_e ) )
426 i_val = accept( i_fd, NULL, 0 );
429 #if defined(WIN32) || defined(UNDER_CE)
430 msg_Err( p_this, "accept failed (%i)", WSAGetLastError() );
432 msg_Err( p_this, "accept failed (%s)", strerror( errno ) );
438 * This round-robin trick ensures that the first sockets in
439 * pi_fd won't prevent the last ones from getting accept'ed.
442 memmove( pi, pi + 1, pi_end - pi );
453 /*****************************************************************************
455 *****************************************************************************
456 * Negociate authentication with a SOCKS server.
457 *****************************************************************************/
458 static int SocksNegociate( vlc_object_t *p_obj,
459 int fd, int i_socks_version,
460 char *psz_socks_user,
461 char *psz_socks_passwd )
463 uint8_t buffer[128+2*256];
465 vlc_bool_t b_auth = VLC_FALSE;
467 if( i_socks_version != 5 )
470 /* We negociate authentication */
472 if( psz_socks_user && psz_socks_passwd &&
473 *psz_socks_user && *psz_socks_passwd )
476 buffer[0] = i_socks_version; /* SOCKS version */
479 buffer[1] = 2; /* Number of methods */
480 buffer[2] = 0x00; /* - No auth required */
481 buffer[3] = 0x02; /* - USer/Password */
486 buffer[1] = 1; /* Number of methods */
487 buffer[2] = 0x00; /* - No auth required */
491 if( net_Write( p_obj, fd, NULL, buffer, i_len ) != i_len )
493 if( net_Read( p_obj, fd, NULL, buffer, 2, VLC_TRUE ) != 2 )
496 msg_Dbg( p_obj, "socks: v=%d method=%x", buffer[0], buffer[1] );
498 if( buffer[1] == 0x00 )
500 msg_Dbg( p_obj, "socks: no authentication required" );
502 else if( buffer[1] == 0x02 )
504 int i_len1 = __MIN( strlen(psz_socks_user), 255 );
505 int i_len2 = __MIN( strlen(psz_socks_passwd), 255 );
506 msg_Dbg( p_obj, "socks: username/password authentication" );
508 /* XXX: we don't support user/pwd > 255 (truncated)*/
509 buffer[0] = i_socks_version; /* Version */
510 buffer[1] = i_len1; /* User length */
511 memcpy( &buffer[2], psz_socks_user, i_len1 );
512 buffer[2+i_len1] = i_len2; /* Password length */
513 memcpy( &buffer[2+i_len1+1], psz_socks_passwd, i_len2 );
515 i_len = 3 + i_len1 + i_len2;
517 if( net_Write( p_obj, fd, NULL, buffer, i_len ) != i_len )
520 if( net_Read( p_obj, fd, NULL, buffer, 2, VLC_TRUE ) != 2 )
523 msg_Dbg( p_obj, "socks: v=%d status=%x", buffer[0], buffer[1] );
524 if( buffer[1] != 0x00 )
526 msg_Err( p_obj, "socks: authentication rejected" );
533 msg_Err( p_obj, "socks: unsupported authentication method %x",
536 msg_Err( p_obj, "socks: authentification needed" );
543 /*****************************************************************************
545 *****************************************************************************
546 * Open a TCP connection using a SOCKS server and return a handle (RFC 1928)
547 *****************************************************************************/
548 static int SocksHandshakeTCP( vlc_object_t *p_obj,
551 char *psz_socks_user, char *psz_socks_passwd,
552 const char *psz_host, int i_port )
554 uint8_t buffer[128+2*256];
556 if( i_socks_version != 4 && i_socks_version != 5 )
558 msg_Warn( p_obj, "invalid socks protocol version %d", i_socks_version );
562 if( i_socks_version == 5 &&
563 SocksNegociate( p_obj, fd, i_socks_version,
564 psz_socks_user, psz_socks_passwd ) )
567 if( i_socks_version == 4 )
569 struct addrinfo hints = { 0 }, *p_res;
571 /* v4 only support ipv4 */
572 hints.ai_family = AF_INET;
573 if( vlc_getaddrinfo( p_obj, psz_host, 0, &hints, &p_res ) )
576 buffer[0] = i_socks_version;
577 buffer[1] = 0x01; /* CONNECT */
578 SetWBE( &buffer[2], i_port ); /* Port */
579 memcpy( &buffer[4], /* Address */
580 &((struct sockaddr_in *)(p_res->ai_addr))->sin_addr, 4 );
581 vlc_freeaddrinfo( p_res );
583 buffer[8] = 0; /* Empty user id */
585 if( net_Write( p_obj, fd, NULL, buffer, 9 ) != 9 )
587 if( net_Read( p_obj, fd, NULL, buffer, 8, VLC_TRUE ) != 8 )
590 msg_Dbg( p_obj, "socks: v=%d cd=%d",
591 buffer[0], buffer[1] );
593 if( buffer[1] != 90 )
596 else if( i_socks_version == 5 )
598 int i_hlen = __MIN(strlen( psz_host ), 255);
601 buffer[0] = i_socks_version; /* Version */
602 buffer[1] = 0x01; /* Cmd: connect */
603 buffer[2] = 0x00; /* Reserved */
604 buffer[3] = 3; /* ATYP: for now domainname */
607 memcpy( &buffer[5], psz_host, i_hlen );
608 SetWBE( &buffer[5+i_hlen], i_port );
610 i_len = 5 + i_hlen + 2;
613 if( net_Write( p_obj, fd, NULL, buffer, i_len ) != i_len )
616 /* Read the header */
617 if( net_Read( p_obj, fd, NULL, buffer, 5, VLC_TRUE ) != 5 )
620 msg_Dbg( p_obj, "socks: v=%d rep=%d atyp=%d",
621 buffer[0], buffer[1], buffer[3] );
623 if( buffer[1] != 0x00 )
625 msg_Err( p_obj, "socks: CONNECT request failed\n" );
629 /* Read the remaining bytes */
630 if( buffer[3] == 0x01 )
632 else if( buffer[3] == 0x03 )
633 i_len = buffer[4] + 2;
634 else if( buffer[3] == 0x04 )
639 if( net_Read( p_obj, fd, NULL, buffer, i_len, VLC_TRUE ) != i_len )
646 void net_ListenClose( int *pi_fd )
652 for( pi = pi_fd; *pi != -1; pi++ )