1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004-2005 VideoLAN
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 *****************************************************************************/
37 #ifdef HAVE_SYS_TIME_H
38 # include <sys/time.h>
41 #if defined( WIN32 ) || defined( UNDER_CE )
42 # if defined(UNDER_CE) && defined(sockaddr_storage)
43 # undef sockaddr_storage
45 # include <winsock2.h>
46 # include <ws2tcpip.h>
48 # include <sys/socket.h>
49 # include <netinet/in.h>
50 # ifdef HAVE_ARPA_INET_H
51 # include <arpa/inet.h>
63 # define INADDR_ANY 0x00000000
66 # define INADDR_NONE 0xFFFFFFFF
69 # define PF_INET AF_INET
72 static int SocksNegociate( vlc_object_t *, int fd, int i_socks_version,
73 char *psz_socks_user, char *psz_socks_passwd );
74 static int SocksHandshakeTCP( vlc_object_t *,
75 int fd, int i_socks_version,
76 char *psz_socks_user, char *psz_socks_passwd,
77 const char *psz_host, int i_port );
79 /*****************************************************************************
81 *****************************************************************************
82 * Open a TCP connection and return a handle
83 *****************************************************************************/
84 int __net_OpenTCP( vlc_object_t *p_this, const char *psz_host, int i_port )
86 struct addrinfo hints, *res, *ptr;
87 const char *psz_realhost;
89 int i_realport, i_val, i_handle = -1;
92 i_port = 80; /* historical VLC thing */
94 memset( &hints, 0, sizeof( hints ) );
95 hints.ai_socktype = SOCK_STREAM;
97 psz_socks = var_CreateGetString( p_this, "socks" );
98 if( *psz_socks && *psz_socks != ':' )
100 char *psz = strchr( psz_socks, ':' );
105 psz_realhost = psz_socks;
106 i_realport = ( psz != NULL ) ? atoi( psz ) : 1080;
108 msg_Dbg( p_this, "net: connecting to '%s:%d' for '%s:%d'",
109 psz_realhost, i_realport, psz_host, i_port );
113 psz_realhost = psz_host;
116 msg_Dbg( p_this, "net: connecting to '%s:%d'", psz_realhost,
120 i_val = vlc_getaddrinfo( p_this, psz_realhost, i_realport, &hints, &res );
123 msg_Err( p_this, "cannot resolve '%s:%d' : %s", psz_realhost,
124 i_realport, vlc_gai_strerror( i_val ) );
129 for( ptr = res; (ptr != NULL) && (i_handle == -1); ptr = ptr->ai_next )
133 fd = socket( ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol );
136 #if defined(WIN32) || defined(UNDER_CE)
137 msg_Warn( p_this, "cannot create socket (%i)",
140 msg_Warn( p_this, "cannot create socket (%s)",
147 /* Set to non-blocking */
148 #if defined( WIN32 ) || defined( UNDER_CE )
150 unsigned long i_dummy = 1;
151 if( ioctlsocket( fd, FIONBIO, &i_dummy ) != 0 )
152 msg_Err( p_this, "cannot set socket to non-blocking mode" );
155 if( ( ( i_val = fcntl( fd, F_GETFL, 0 ) ) < 0 ) ||
156 ( fcntl( fd, F_SETFL, i_val | O_NONBLOCK ) < 0 ) )
157 msg_Err( p_this, "cannot set socket to non-blocking mode (%s)",
162 setsockopt( fd, SOL_SOCKET, SO_REUSEADDR, (void *)&i_val,
165 if( connect( fd, ptr->ai_addr, ptr->ai_addrlen ) )
167 int i_val_size = sizeof( i_val );
171 #if defined( WIN32 ) || defined( UNDER_CE )
172 if( WSAGetLastError() != WSAEWOULDBLOCK )
174 msg_Warn( p_this, "connection to %s:%d failed (%d)", psz_host,
175 i_port, WSAGetLastError( ) );
180 if( errno != EINPROGRESS )
182 msg_Warn( p_this, "connection to %s:%d : %s", psz_host,
183 i_port, strerror( errno ) );
189 var_Create( p_this, "ipv4-timeout",
190 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
191 var_Get( p_this, "ipv4-timeout", &timeout );
192 if( timeout.i_int < 0 )
194 msg_Err( p_this, "invalid negative value for ipv4-timeout" );
197 d = div( timeout.i_int, 100 );
199 msg_Dbg( p_this, "connection in progress" );
206 msg_Dbg( p_this, "connection aborted" );
208 vlc_freeaddrinfo( res );
213 /* Initialize file descriptor set */
217 /* We'll wait 0.1 second if nothing happens */
219 tv.tv_usec = (d.quot > 0) ? 100000 : (1000 * d.rem);
221 i_val = select( fd + 1, NULL, &fds, NULL, &tv );
225 msg_Dbg( p_this, "connection timed out" );
233 while( ( i_val == 0 ) || ( ( i_val < 0 ) &&
234 #if defined( WIN32 ) || defined( UNDER_CE )
235 ( WSAGetLastError() == WSAEWOULDBLOCK )
242 continue; /* timeout */
246 msg_Warn( p_this, "connection aborted (select failed)" );
251 #if !defined( SYS_BEOS ) && !defined( UNDER_CE )
252 if( getsockopt( fd, SOL_SOCKET, SO_ERROR, (void*)&i_val,
253 &i_val_size ) == -1 || i_val != 0 )
256 msg_Warn( p_this, "connection to %s:%d failed (%d)", psz_host,
257 i_port, WSAGetLastError( ) );
259 msg_Warn( p_this, "connection to %s:%d : %s", psz_host,
260 i_port, strerror( i_val ) );
267 i_handle = fd; /* success! */
270 vlc_freeaddrinfo( res );
272 if( *psz_socks && *psz_socks != ':' )
274 char *psz_user = var_CreateGetString( p_this, "socks-user" );
275 char *psz_pwd = var_CreateGetString( p_this, "socks-pwd" );
277 if( SocksHandshakeTCP( p_this, i_handle, 5, psz_user, psz_pwd,
280 msg_Err( p_this, "failed to use the SOCKS server" );
281 net_Close( i_handle );
294 /*****************************************************************************
296 *****************************************************************************
297 * Open TCP passive "listening" socket(s)
298 * This function returns NULL in case of error.
299 *****************************************************************************/
300 int *__net_ListenTCP( vlc_object_t *p_this, const char *psz_host, int i_port )
302 struct addrinfo hints, *res, *ptr;
303 int i_val, *pi_handles, i_size;
305 memset( &hints, 0, sizeof( hints ) );
306 hints.ai_socktype = SOCK_STREAM;
307 hints.ai_flags = AI_PASSIVE;
309 msg_Dbg( p_this, "net: listening to '%s:%d'", psz_host, i_port );
311 i_val = vlc_getaddrinfo( p_this, psz_host, i_port, &hints, &res );
314 msg_Err( p_this, "cannot resolve '%s:%d' : %s", psz_host, i_port,
315 vlc_gai_strerror( i_val ) );
322 for( ptr = res; ptr != NULL; ptr = ptr->ai_next )
326 fd = socket( ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol );
329 #if defined(WIN32) || defined(UNDER_CE)
330 msg_Warn( p_this, "cannot create socket (%i)",
333 msg_Warn( p_this, "cannot create socket (%s)",
339 /* Set to non-blocking */
340 #if defined( WIN32 ) || defined( UNDER_CE )
342 unsigned long i_dummy = 1;
343 if( ioctlsocket( fd, FIONBIO, &i_dummy ) != 0 )
344 msg_Err( p_this, "cannot set socket to non-blocking mode" );
347 if( ( ( i_val = fcntl( fd, F_GETFL, 0 ) ) < 0 ) ||
348 ( fcntl( fd, F_SETFL, i_val | O_NONBLOCK ) < 0 ) )
349 msg_Err( p_this, "cannot set socket to non-blocking mode (%s)",
354 setsockopt( fd, SOL_SOCKET, SO_REUSEADDR, (void *)&i_val,
359 * Accepts only IPv6 connections on IPv6 sockets
360 * (and open an IPv4 socket later as well if needed).
361 * Only Linux and FreeBSD can map IPv4 connections on IPv6 sockets,
362 * so this allows for more uniform handling across platforms. Besides,
363 * it makes sure that IPv4 addresses will be printed as w.x.y.z rather
364 * than ::ffff:w.x.y.z
366 if( ptr->ai_family == PF_INET6 )
367 setsockopt( fd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&i_val,
371 #if defined( WIN32 ) || defined( UNDER_CE )
372 # ifdef IPV6_PROTECTION_LEVEL
373 if( ptr->ai_family == PF_INET6 )
375 i_val = PROTECTION_LEVEL_UNRESTRICTED;
376 setsockopt( fd, IPPROTO_IPV6, IPV6_PROTECTION_LEVEL, &i_val,
380 # warning You are using outdated headers for Winsock !
384 /* Bind the socket */
385 if( bind( fd, ptr->ai_addr, ptr->ai_addrlen ) )
387 #if defined(WIN32) || defined(UNDER_CE)
388 msg_Warn( p_this, "cannot bind socket (%i)", WSAGetLastError( ) );
390 msg_Warn( p_this, "cannot bind socket (%s)", strerror( errno ) );
397 if( listen( fd, 100 ) == -1 )
399 #if defined(WIN32) || defined(UNDER_CE)
400 msg_Err( p_this, "cannot bring socket in listening mode (%i)",
403 msg_Err( p_this, "cannot bring the socket in listening mode (%s)",
410 newpi = (int *)realloc( pi_handles, (++i_size) * sizeof( int ) );
418 newpi[i_size - 2] = fd;
423 vlc_freeaddrinfo( res );
425 if( pi_handles != NULL )
426 pi_handles[i_size - 1] = -1;
430 /*****************************************************************************
432 *****************************************************************************
433 * Accept a connection on a set of listening sockets and return it
434 *****************************************************************************/
435 int __net_Accept( vlc_object_t *p_this, int *pi_fd, mtime_t i_wait )
437 vlc_bool_t b_die = p_this->b_die, b_block = (i_wait < 0);
439 while( p_this->b_die == b_die )
441 int i_val = -1, *pi, *pi_end;
442 struct timeval timeout;
447 /* Initialize file descriptor set */
451 for( pi = pi_fd; *pi != -1; pi++ )
458 FD_SET( i_fd, &fds_r );
459 FD_SET( i_fd, &fds_e );
464 timeout.tv_usec = b_block ? 500000 : i_wait;
466 i_val = select( i_val + 1, &fds_r, NULL, &fds_e, &timeout );
467 if( ( ( i_val < 0 ) && ( errno == EINTR ) ) || i_val == 0 )
476 #if defined(WIN32) || defined(UNDER_CE)
477 msg_Err( p_this, "network select error (%i)", WSAGetLastError() );
479 msg_Err( p_this, "network select error (%s)", strerror( errno ) );
484 for( pi = pi_fd; *pi != -1; pi++ )
488 if( !FD_ISSET( i_fd, &fds_r ) && !FD_ISSET( i_fd, &fds_e ) )
491 i_val = accept( i_fd, NULL, 0 );
494 #if defined(WIN32) || defined(UNDER_CE)
495 msg_Err( p_this, "accept failed (%i)", WSAGetLastError() );
497 msg_Err( p_this, "accept failed (%s)", strerror( errno ) );
503 * This round-robin trick ensures that the first sockets in
504 * pi_fd won't prevent the last ones from getting accept'ed.
507 memmove( pi, pi + 1, pi_end - pi );
517 /*****************************************************************************
519 *****************************************************************************
520 * Open a UDP connection and return a handle
521 *****************************************************************************/
522 int __net_OpenUDP( vlc_object_t *p_this, char *psz_bind, int i_bind,
523 char *psz_server, int i_server )
528 char *psz_network = "";
529 network_socket_t sock;
533 /* Check if we have force ipv4 or ipv6 */
534 var_Create( p_this, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
535 var_Get( p_this, "ipv4", &val );
538 psz_network = "ipv4";
541 var_Create( p_this, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
542 var_Get( p_this, "ipv6", &val );
545 psz_network = "ipv6";
547 if( psz_server == NULL ) psz_server = "";
548 if( psz_bind == NULL ) psz_bind = "";
550 /* Prepare the network_socket_t structure */
551 sock.psz_bind_addr = psz_bind;
552 sock.i_bind_port = i_bind;
553 sock.psz_server_addr = psz_server;
554 sock.i_server_port = i_server;
557 msg_Dbg( p_this, "net: connecting to '%s:%d@%s:%d'",
558 psz_server, i_server, psz_bind, i_bind );
559 private = p_this->p_private;
560 p_this->p_private = (void*)&sock;
561 if( !( p_network = module_Need( p_this, "network", psz_network, 0 ) ) )
563 msg_Dbg( p_this, "net: connection to '%s:%d@%s:%d' failed",
564 psz_server, i_server, psz_bind, i_bind );
567 module_Unneed( p_this, p_network );
568 p_this->p_private = private;
570 return sock.i_handle;
573 /*****************************************************************************
575 *****************************************************************************
576 * Close a network handle
577 *****************************************************************************/
578 void net_Close( int fd )
581 CloseHandle( (HANDLE)fd );
582 #elif defined( WIN32 )
589 void net_ListenClose( int *pi_fd )
595 for( pi = pi_fd; *pi != -1; pi++ )
601 /*****************************************************************************
603 *****************************************************************************
604 * Read from a network socket
605 * If b_rety is true, then we repeat until we have read the right amount of
607 *****************************************************************************/
608 int __net_Read( vlc_object_t *p_this, int fd, v_socket_t *p_vs,
609 uint8_t *p_data, int i_data, vlc_bool_t b_retry )
611 struct timeval timeout;
616 vlc_bool_t b_die = p_this->b_die;
622 if( p_this->b_die != b_die )
627 /* Initialize file descriptor set */
629 FD_SET( fd, &fds_r );
631 FD_SET( fd, &fds_e );
633 /* We'll wait 0.5 second if nothing happens */
635 timeout.tv_usec = 500000;
637 } while( (i_ret = select(fd + 1, &fds_r, NULL, &fds_e, &timeout)) == 0
638 || ( i_ret < 0 && errno == EINTR ) );
642 #if defined(WIN32) || defined(UNDER_CE)
643 msg_Err( p_this, "network select error" );
645 msg_Err( p_this, "network select error (%s)", strerror(errno) );
647 return i_total > 0 ? i_total : -1;
650 if( ( i_recv = (p_vs != NULL)
651 ? p_vs->pf_recv( p_vs->p_sys, p_data, i_data )
652 : recv( fd, p_data, i_data, 0 ) ) < 0 )
654 #if defined(WIN32) || defined(UNDER_CE)
655 if( WSAGetLastError() == WSAEWOULDBLOCK )
657 /* only happens with p_vs (SSL) - not really an error */
661 /* On win32 recv() will fail if the datagram doesn't fit inside
662 * the passed buffer, even though the buffer will be filled with
663 * the first part of the datagram. */
664 if( WSAGetLastError() == WSAEMSGSIZE )
666 msg_Err( p_this, "recv() failed. "
667 "Increase the mtu size (--mtu option)" );
670 else if( WSAGetLastError() == WSAEINTR ) continue;
671 else msg_Err( p_this, "recv failed (%i)", WSAGetLastError() );
673 /* EAGAIN only happens with p_vs (SSL) and it's not an error */
674 if( errno != EAGAIN )
675 msg_Err( p_this, "recv failed (%s)", strerror(errno) );
677 return i_total > 0 ? i_total : -1;
679 else if( i_recv == 0 )
681 /* Connection closed */
696 /*****************************************************************************
697 * __net_ReadNonBlock:
698 *****************************************************************************
699 * Read from a network socket, non blocking mode (with timeout)
700 *****************************************************************************/
701 int __net_ReadNonBlock( vlc_object_t *p_this, int fd, v_socket_t *p_vs,
702 uint8_t *p_data, int i_data, mtime_t i_wait)
704 struct timeval timeout;
709 /* Initialize file descriptor set */
711 FD_SET( fd, &fds_r );
713 FD_SET( fd, &fds_e );
716 timeout.tv_usec = i_wait;
718 i_ret = select(fd + 1, &fds_r, NULL, &fds_e, &timeout);
720 if( i_ret < 0 && errno == EINTR )
726 #if defined(WIN32) || defined(UNDER_CE)
727 msg_Err( p_this, "network select error" );
729 msg_Err( p_this, "network select error (%s)", strerror(errno) );
739 #if !defined(UNDER_CE)
740 if( fd == 0/*STDIN_FILENO*/ ) i_recv = read( fd, p_data, i_data ); else
742 if( ( i_recv = (p_vs != NULL)
743 ? p_vs->pf_recv( p_vs->p_sys, p_data, i_data )
744 : recv( fd, p_data, i_data, 0 ) ) <= 0 )
746 #if defined(WIN32) || defined(UNDER_CE)
748 /* On win32 recv() will fail if the datagram doesn't fit inside
749 * the passed buffer, even though the buffer will be filled with
750 * the first part of the datagram. */
751 if( WSAGetLastError() == WSAEMSGSIZE )
753 msg_Err( p_this, "recv() failed. "
754 "Increase the mtu size (--mtu option)" );
756 else msg_Err( p_this, "recv failed (%i)", WSAGetLastError() );
758 msg_Err( p_this, "recv failed (%s)", strerror(errno) );
763 return i_recv ? i_recv : -1; /* !i_recv -> connection closed if tcp */
766 /* We will never be here */
770 /*****************************************************************************
772 *****************************************************************************
773 * Read from several sockets (with timeout). Takes data from the first socket
775 *****************************************************************************/
776 int __net_Select( vlc_object_t *p_this, int *pi_fd, v_socket_t **pp_vs,
777 int i_fd, uint8_t *p_data, int i_data, mtime_t i_wait )
779 struct timeval timeout;
786 /* Initialize file descriptor set */
790 for( i = 0 ; i < i_fd ; i++)
792 if( pi_fd[i] > i_max_fd ) i_max_fd = pi_fd[i];
793 FD_SET( pi_fd[i], &fds_r );
794 FD_SET( pi_fd[i], &fds_e );
798 timeout.tv_usec = i_wait;
800 i_ret = select( i_max_fd + 1, &fds_r, NULL, &fds_e, &timeout );
802 if( i_ret < 0 && errno == EINTR )
808 msg_Err( p_this, "network select error (%s)", strerror(errno) );
811 else if( i_ret == 0 )
817 for( i = 0 ; i < i_fd ; i++)
819 if( FD_ISSET( pi_fd[i], &fds_r ) )
821 i_recv = ((pp_vs != NULL) && (pp_vs[i] != NULL))
822 ? pp_vs[i]->pf_recv( pp_vs[i]->p_sys, p_data, i_data )
823 : recv( pi_fd[i], p_data, i_data, 0 );
828 /* On win32 recv() will fail if the datagram doesn't
829 * fit inside the passed buffer, even though the buffer
830 * will be filled with the first part of the datagram. */
831 if( WSAGetLastError() == WSAEMSGSIZE )
833 msg_Err( p_this, "recv() failed. "
834 "Increase the mtu size (--mtu option)" );
836 else msg_Err( p_this, "recv failed (%i)",
839 msg_Err( p_this, "recv failed (%s)", strerror(errno) );
849 /* We will never be here */
854 /* Write exact amount requested */
855 int __net_Write( vlc_object_t *p_this, int fd, v_socket_t *p_vs,
856 uint8_t *p_data, int i_data )
858 struct timeval timeout;
864 vlc_bool_t b_die = p_this->b_die;
870 if( p_this->b_die != b_die )
875 /* Initialize file descriptor set */
877 FD_SET( fd, &fds_w );
879 FD_SET( fd, &fds_e );
881 /* We'll wait 0.5 second if nothing happens */
883 timeout.tv_usec = 500000;
885 } while( (i_ret = select(fd + 1, NULL, &fds_w, &fds_e, &timeout)) == 0
886 || ( i_ret < 0 && errno == EINTR ) );
890 #if defined(WIN32) || defined(UNDER_CE)
891 msg_Err( p_this, "network select error" );
893 msg_Err( p_this, "network select error (%s)", strerror(errno) );
895 return i_total > 0 ? i_total : -1;
898 if( ( i_send = (p_vs != NULL)
899 ? p_vs->pf_send( p_vs->p_sys, p_data, i_data )
900 : send( fd, p_data, i_data, 0 ) ) < 0 )
902 /* XXX With udp for example, it will issue a message if the host
904 /* msg_Err( p_this, "send failed (%s)", strerror(errno) ); */
905 return i_total > 0 ? i_total : -1;
915 char *__net_Gets( vlc_object_t *p_this, int fd, v_socket_t *p_vs )
917 char *psz_line = malloc( 1024 );
924 if( net_Read( p_this, fd, p_vs, &psz_line[i_line], 1, VLC_TRUE ) != 1 )
926 psz_line[i_line] = '\0';
931 if( psz_line[i_line-1] == '\n' )
933 psz_line[i_line] = '\0';
937 if( i_line >= i_max - 1 )
940 psz_line = realloc( psz_line, i_max );
950 while( i_line >= 1 &&
951 ( psz_line[i_line-1] == '\n' || psz_line[i_line-1] == '\r' ) )
954 psz_line[i_line] = '\0';
959 int net_Printf( vlc_object_t *p_this, int fd, v_socket_t *p_vs,
960 const char *psz_fmt, ... )
964 va_start( args, psz_fmt );
965 i_ret = net_vaPrintf( p_this, fd, p_vs, psz_fmt, args );
971 int __net_vaPrintf( vlc_object_t *p_this, int fd, v_socket_t *p_vs,
972 const char *psz_fmt, va_list args )
977 vasprintf( &psz, psz_fmt, args );
978 i_size = strlen( psz );
979 i_ret = __net_Write( p_this, fd, p_vs, psz, i_size ) < i_size ? -1 : i_size;
987 /*****************************************************************************
989 *****************************************************************************
990 * Negociate authentication with a SOCKS server.
991 *****************************************************************************/
992 static int SocksNegociate( vlc_object_t *p_obj,
993 int fd, int i_socks_version,
994 char *psz_socks_user,
995 char *psz_socks_passwd )
997 uint8_t buffer[128+2*256];
999 vlc_bool_t b_auth = VLC_FALSE;
1001 if( i_socks_version != 5 )
1004 /* We negociate authentication */
1006 if( psz_socks_user && psz_socks_passwd &&
1007 *psz_socks_user && *psz_socks_passwd )
1010 buffer[0] = i_socks_version; /* SOCKS version */
1013 buffer[1] = 2; /* Number of methods */
1014 buffer[2] = 0x00; /* - No auth required */
1015 buffer[3] = 0x02; /* - USer/Password */
1020 buffer[1] = 1; /* Number of methods */
1021 buffer[2] = 0x00; /* - No auth required */
1025 if( net_Write( p_obj, fd, NULL, buffer, i_len ) != i_len )
1026 return VLC_EGENERIC;
1027 if( net_Read( p_obj, fd, NULL, buffer, 2, VLC_TRUE ) != 2 )
1028 return VLC_EGENERIC;
1030 msg_Dbg( p_obj, "socks: v=%d method=%x", buffer[0], buffer[1] );
1032 if( buffer[1] == 0x00 )
1034 msg_Dbg( p_obj, "socks: no authentication required" );
1036 else if( buffer[1] == 0x02 )
1038 int i_len1 = __MIN( strlen(psz_socks_user), 255 );
1039 int i_len2 = __MIN( strlen(psz_socks_passwd), 255 );
1040 msg_Dbg( p_obj, "socks: username/password authentication" );
1042 /* XXX: we don't support user/pwd > 255 (truncated)*/
1043 buffer[0] = i_socks_version; /* Version */
1044 buffer[1] = i_len1; /* User length */
1045 memcpy( &buffer[2], psz_socks_user, i_len1 );
1046 buffer[2+i_len1] = i_len2; /* Password length */
1047 memcpy( &buffer[2+i_len1+1], psz_socks_passwd, i_len2 );
1049 i_len = 3 + i_len1 + i_len2;
1051 if( net_Write( p_obj, fd, NULL, buffer, i_len ) != i_len )
1052 return VLC_EGENERIC;
1054 if( net_Read( p_obj, fd, NULL, buffer, 2, VLC_TRUE ) != 2 )
1055 return VLC_EGENERIC;
1057 msg_Dbg( p_obj, "socks: v=%d status=%x", buffer[0], buffer[1] );
1058 if( buffer[1] != 0x00 )
1060 msg_Err( p_obj, "socks: authentication rejected" );
1061 return VLC_EGENERIC;
1067 msg_Err( p_obj, "socks: unsupported authentication method %x",
1070 msg_Err( p_obj, "socks: authentification needed" );
1071 return VLC_EGENERIC;
1077 /*****************************************************************************
1078 * SocksHandshakeTCP:
1079 *****************************************************************************
1080 * Open a TCP connection using a SOCKS server and return a handle (RFC 1928)
1081 *****************************************************************************/
1082 static int SocksHandshakeTCP( vlc_object_t *p_obj,
1084 int i_socks_version,
1085 char *psz_socks_user, char *psz_socks_passwd,
1086 const char *psz_host, int i_port )
1088 uint8_t buffer[128+2*256];
1090 if( i_socks_version != 4 && i_socks_version != 5 )
1092 msg_Warn( p_obj, "invalid socks protocol version %d", i_socks_version );
1093 i_socks_version = 5;
1096 if( i_socks_version == 5 &&
1097 SocksNegociate( p_obj, fd, i_socks_version,
1098 psz_socks_user, psz_socks_passwd ) )
1099 return VLC_EGENERIC;
1101 if( i_socks_version == 4 )
1103 struct addrinfo hints = { 0 }, *p_res;
1105 /* v4 only support ipv4 */
1106 hints.ai_family = PF_INET;
1107 if( vlc_getaddrinfo( p_obj, psz_host, 0, &hints, &p_res ) )
1108 return VLC_EGENERIC;
1110 buffer[0] = i_socks_version;
1111 buffer[1] = 0x01; /* CONNECT */
1112 SetWBE( &buffer[2], i_port ); /* Port */
1113 memcpy( &buffer[4], /* Address */
1114 &((struct sockaddr_in *)(p_res->ai_addr))->sin_addr, 4 );
1115 vlc_freeaddrinfo( p_res );
1117 buffer[8] = 0; /* Empty user id */
1119 if( net_Write( p_obj, fd, NULL, buffer, 9 ) != 9 )
1120 return VLC_EGENERIC;
1121 if( net_Read( p_obj, fd, NULL, buffer, 8, VLC_TRUE ) != 8 )
1122 return VLC_EGENERIC;
1124 msg_Dbg( p_obj, "socks: v=%d cd=%d",
1125 buffer[0], buffer[1] );
1127 if( buffer[1] != 90 )
1128 return VLC_EGENERIC;
1130 else if( i_socks_version == 5 )
1132 int i_hlen = __MIN(strlen( psz_host ), 255);
1135 buffer[0] = i_socks_version; /* Version */
1136 buffer[1] = 0x01; /* Cmd: connect */
1137 buffer[2] = 0x00; /* Reserved */
1138 buffer[3] = 3; /* ATYP: for now domainname */
1141 memcpy( &buffer[5], psz_host, i_hlen );
1142 SetWBE( &buffer[5+i_hlen], i_port );
1144 i_len = 5 + i_hlen + 2;
1147 if( net_Write( p_obj, fd, NULL, buffer, i_len ) != i_len )
1148 return VLC_EGENERIC;
1150 /* Read the header */
1151 if( net_Read( p_obj, fd, NULL, buffer, 5, VLC_TRUE ) != 5 )
1152 return VLC_EGENERIC;
1154 msg_Dbg( p_obj, "socks: v=%d rep=%d atyp=%d",
1155 buffer[0], buffer[1], buffer[3] );
1157 if( buffer[1] != 0x00 )
1159 msg_Err( p_obj, "socks: CONNECT request failed\n" );
1160 return VLC_EGENERIC;
1163 /* Read the remaining bytes */
1164 if( buffer[3] == 0x01 )
1166 else if( buffer[3] == 0x03 )
1167 i_len = buffer[4] + 2;
1168 else if( buffer[3] == 0x04 )
1171 return VLC_EGENERIC;
1173 if( net_Read( p_obj, fd, NULL, buffer, i_len, VLC_TRUE ) != i_len )
1174 return VLC_EGENERIC;
1180 /*****************************************************************************
1182 *****************************************************************************
1183 * Wrappers for shutdown()
1184 *****************************************************************************/
1185 int net_StopRecv( int fd )
1187 #if defined (SHUT_RD)
1188 /* the standard way */
1189 return shutdown( fd, SHUT_RD );
1190 #elif defined (SD_RECEIVE)
1191 /* the Microsoft seemingly-purposedly-different-for-the-sake-of-it way */
1192 return shutdown( fd, SD_RECEIVE );
1194 # warning FIXME: implement shutdown on your platform!
1199 int net_StopSend( int fd )
1201 #if defined (SHUT_WR)
1202 /* the standard way */
1203 return shutdown( fd, SHUT_WR );
1204 #elif defined (SD_SEND)
1205 /* the Microsoft seemingly-purposedly-different-for-the-sake-of-it way */
1206 return shutdown( fd, SD_SEND );
1208 # warning FIXME: implement shutdown on your platform!
1213 /*****************************************************************************
1215 *****************************************************************************
1216 * Check that a given IP is within a set of IP/netmask.
1217 *****************************************************************************/
1218 int __net_CheckIP( vlc_object_t *p_this, char *psz_ip, char **ppsz_hosts,
1224 if( (ip.s_addr = inet_addr( psz_ip )) == INADDR_NONE )
1226 return VLC_EGENERIC;
1229 for( i = 0; i < i_hosts; i++ )
1231 struct in_addr base, mask;
1232 char *psz_host = strdup( ppsz_hosts[i] );
1233 char *p = strchr( psz_host, '/' );
1240 if( i_mask < 0 || i_mask > 32 )
1242 msg_Err( p_this, "invalid netmask %s", p );
1243 mask.s_addr = INADDR_NONE;
1245 else if( i_mask == 0 )
1246 mask.s_addr = INADDR_ANY;
1248 mask.s_addr = htonl( ntohl(INADDR_NONE) << (32 - i_mask) );
1251 mask.s_addr = INADDR_NONE;
1253 if( (base.s_addr = inet_addr( psz_host )) == INADDR_NONE )
1255 msg_Err( p_this, "invalid base address %s", psz_host );
1261 if( !((ip.s_addr ^ base.s_addr) & mask.s_addr) )