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
);
62 /*****************************************************************************
64 *****************************************************************************
65 * Open a network connection.
66 * @return socket handler or -1 on error.
67 *****************************************************************************/
68 int net_Connect( vlc_object_t
*p_this
, const char *psz_host
, int i_port
,
71 const char *psz_realhost
;
73 int i_realport
, i_handle
= -1;
75 psz_socks
= var_InheritString( p_this
, "socks" );
76 if( psz_socks
!= NULL
)
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 (SOCKS) "
87 "for %s port %d", psz_realhost
, i_realport
,
90 /* We only implement TCP with SOCKS */
98 msg_Err( p_this
, "Socket type not supported through SOCKS" );
109 msg_Err( p_this
, "Transport not supported through SOCKS" );
116 psz_realhost
= psz_host
;
119 msg_Dbg( p_this
, "net: connecting to %s port %d", psz_realhost
,
123 struct addrinfo hints
= {
125 .ai_protocol
= proto
,
126 .ai_flags
= AI_NUMERICSERV
| AI_IDN
,
129 int val
= vlc_getaddrinfo_i11e(psz_realhost
, i_realport
, &hints
, &res
);
132 msg_Err (p_this
, "cannot resolve %s port %d : %s", psz_realhost
,
133 i_realport
, gai_strerror (val
));
139 vlc_tick_t timeout
= VLC_TICK_FROM_MS( var_InheritInteger(p_this
, "ipv4-timeout") );
141 for (struct addrinfo
*ptr
= res
; ptr
!= NULL
; ptr
= ptr
->ai_next
)
143 int fd
= net_Socket( p_this
, ptr
->ai_family
,
144 ptr
->ai_socktype
, ptr
->ai_protocol
);
147 msg_Dbg( p_this
, "socket error: %s", vlc_strerror_c(net_errno
) );
151 if( connect( fd
, ptr
->ai_addr
, ptr
->ai_addrlen
) )
153 if( net_errno
!= EINPROGRESS
&& errno
!= EINTR
)
155 msg_Err( p_this
, "connection failed: %s",
156 vlc_strerror_c(net_errno
) );
161 vlc_tick_t deadline
= VLC_TICK_INVALID
;
164 ufd
.events
= POLLOUT
;
165 deadline
= vlc_tick_now() + timeout
;
169 vlc_tick_t now
= vlc_tick_now();
177 val
= vlc_poll_i11e(&ufd
, 1, MS_FROM_VLC_TICK(deadline
- now
));
179 while (val
== -1 && errno
== EINTR
);
184 msg_Err (p_this
, "polling error: %s",
185 vlc_strerror_c(net_errno
));
188 case 0: /* timeout */
189 msg_Warn (p_this
, "connection timed out");
193 /* There is NO WAY around checking SO_ERROR.
194 * Don't ifdef it out!!! */
195 if (getsockopt (fd
, SOL_SOCKET
, SO_ERROR
, &val
,
196 &(socklen_t
){ sizeof (val
) }) || val
)
198 msg_Err (p_this
, "connection failed: %s",
199 vlc_strerror_c(val
));
204 msg_Dbg( p_this
, "connection succeeded (socket = %d)", fd
);
205 i_handle
= fd
; /* success! */
208 next_ai
: /* failure */
217 if( psz_socks
!= NULL
)
219 /* NOTE: psz_socks already free'd! */
220 char *psz_user
= var_InheritString( p_this
, "socks-user" );
221 char *psz_pwd
= var_InheritString( p_this
, "socks-pwd" );
223 if( SocksHandshakeTCP( p_this
, i_handle
, 5, psz_user
, psz_pwd
,
226 msg_Err( p_this
, "SOCKS handshake failed" );
227 net_Close( i_handle
);
239 int net_AcceptSingle (vlc_object_t
*obj
, int lfd
)
241 int fd
= vlc_accept (lfd
, NULL
, NULL
, true);
244 if (net_errno
!= EAGAIN
)
245 #if (EAGAIN != EWOULDBLOCK)
246 if (net_errno
!= EWOULDBLOCK
)
248 msg_Err (obj
, "accept failed (from socket %d): %s", lfd
,
249 vlc_strerror_c(net_errno
));
253 msg_Dbg (obj
, "accepted socket %d (from socket %d)", fd
, lfd
);
254 setsockopt (fd
, SOL_SOCKET
, SO_REUSEADDR
, &(int){ 1 }, sizeof(int));
261 * Accepts an new connection on a set of listening sockets.
262 * If there are no pending connections, this function will wait.
263 * @note If the thread needs to handle events other than incoming connections,
264 * you need to use poll() and net_AcceptSingle() instead.
266 * @param p_this VLC object for logging and object kill signal
267 * @param pi_fd listening socket set
268 * @return -1 on error (may be transient error due to network issues),
269 * a new socket descriptor on success.
271 int net_Accept (vlc_object_t
*p_this
, int *pi_fd
)
273 assert (pi_fd
!= NULL
);
276 while (pi_fd
[n
] != -1)
279 struct pollfd ufd
[n
];
280 /* Initialize file descriptor set */
281 for (unsigned i
= 0; i
< n
; i
++)
283 ufd
[i
].fd
= pi_fd
[i
];
284 ufd
[i
].events
= POLLIN
;
289 while (poll (ufd
, n
, -1) == -1)
291 if (net_errno
!= EINTR
)
293 msg_Err (p_this
, "poll error: %s", vlc_strerror_c(net_errno
));
298 for (unsigned i
= 0; i
< n
; i
++)
300 if (ufd
[i
].revents
== 0)
304 int fd
= net_AcceptSingle (p_this
, sfd
);
309 * Move listening socket to the end to let the others in the
310 * set a chance next time.
312 memmove (pi_fd
+ i
, pi_fd
+ i
+ 1, n
- (i
+ 1));
321 /*****************************************************************************
323 *****************************************************************************
324 * Negotiate authentication with a SOCKS server.
325 *****************************************************************************/
326 static int SocksNegotiate( vlc_object_t
*p_obj
,
327 int fd
, int i_socks_version
,
328 const char *psz_socks_user
,
329 const char *psz_socks_passwd
)
331 uint8_t buffer
[128+2*256];
335 if( i_socks_version
!= 5 )
338 /* We negotiate authentication */
339 buffer
[0] = i_socks_version
; /* SOCKS version */
340 if( psz_socks_user
!= NULL
&& psz_socks_passwd
!= NULL
)
342 buffer
[1] = 2; /* Number of methods */
343 buffer
[2] = 0x00; /* - No auth required */
344 buffer
[3] = 0x02; /* - USer/Password */
350 buffer
[1] = 1; /* Number of methods */
351 buffer
[2] = 0x00; /* - No auth required */
355 if( net_Write( p_obj
, fd
, buffer
, i_len
) != i_len
)
357 if( net_Read( p_obj
, fd
, buffer
, 2) != 2 )
360 msg_Dbg( p_obj
, "socks: v=%d method=%x", buffer
[0], buffer
[1] );
362 if( buffer
[1] == 0x00 )
364 msg_Dbg( p_obj
, "socks: no authentication required" );
366 else if( buffer
[1] == 0x02 )
368 if( psz_socks_user
== NULL
|| psz_socks_passwd
== NULL
)
370 msg_Err( p_obj
, "socks: server mandates authentication but "
371 "a username and/or password was not supplied" );
375 int const i_user
= strlen( psz_socks_user
);
376 int const i_pasw
= strlen( psz_socks_passwd
);
378 if( i_user
> 255 || i_pasw
> 255 )
380 msg_Err( p_obj
, "socks: rejecting username and/or password due to "
381 "violation of RFC1929 (longer than 255 bytes)" );
385 msg_Dbg( p_obj
, "socks: username/password authentication" );
387 buffer
[0] = i_socks_version
; /* Version */
388 buffer
[1] = i_user
; /* User length */
389 memcpy( &buffer
[2], psz_socks_user
, i_user
);
390 buffer
[2+i_user
] = i_pasw
; /* Password length */
391 memcpy( &buffer
[2+i_user
+1], psz_socks_passwd
, i_pasw
);
393 i_len
= 3 + i_user
+ i_pasw
;
395 if( net_Write( p_obj
, fd
, buffer
, i_len
) != i_len
)
398 if( net_Read( p_obj
, fd
, buffer
, 2 ) != 2 )
401 msg_Dbg( p_obj
, "socks: v=%d status=%x", buffer
[0], buffer
[1] );
402 if( buffer
[1] != 0x00 )
404 msg_Err( p_obj
, "socks: authentication rejected" );
411 msg_Err( p_obj
, "socks: unsupported authentication method %x",
414 msg_Err( p_obj
, "socks: authentication needed" );
421 /*****************************************************************************
423 *****************************************************************************
424 * Open a TCP connection using a SOCKS server and return a handle (RFC 1928)
425 *****************************************************************************/
426 static int SocksHandshakeTCP( vlc_object_t
*p_obj
,
429 const char *psz_user
, const char *psz_passwd
,
430 const char *psz_host
, int i_port
)
432 uint8_t buffer
[128+2*256];
434 if( i_socks_version
!= 4 && i_socks_version
!= 5 )
436 msg_Warn( p_obj
, "invalid socks protocol version %d", i_socks_version
);
440 if( i_socks_version
== 5 &&
441 SocksNegotiate( p_obj
, fd
, i_socks_version
,
442 psz_user
, psz_passwd
) )
445 if( i_socks_version
== 4 )
447 /* v4 only support ipv4 */
448 static const struct addrinfo hints
= {
449 .ai_family
= AF_INET
,
450 .ai_socktype
= SOCK_STREAM
,
451 .ai_protocol
= IPPROTO_TCP
,
454 struct addrinfo
*res
;
456 if (vlc_getaddrinfo_i11e(psz_host
, 0, &hints
, &res
))
459 buffer
[0] = i_socks_version
;
460 buffer
[1] = 0x01; /* CONNECT */
461 SetWBE( &buffer
[2], i_port
); /* Port */
462 memcpy (&buffer
[4], /* Address */
463 &((struct sockaddr_in
*)(res
->ai_addr
))->sin_addr
, 4);
466 buffer
[8] = 0; /* Empty user id */
468 if( net_Write( p_obj
, fd
, buffer
, 9 ) != 9 )
470 if( net_Read( p_obj
, fd
, buffer
, 8 ) != 8 )
473 msg_Dbg( p_obj
, "socks: v=%d cd=%d",
474 buffer
[0], buffer
[1] );
476 if( buffer
[1] != 90 )
479 else if( i_socks_version
== 5 )
481 int i_hlen
= __MIN(strlen( psz_host
), 255);
484 buffer
[0] = i_socks_version
; /* Version */
485 buffer
[1] = 0x01; /* Cmd: connect */
486 buffer
[2] = 0x00; /* Reserved */
487 buffer
[3] = 3; /* ATYP: for now domainname */
490 memcpy( &buffer
[5], psz_host
, i_hlen
);
491 SetWBE( &buffer
[5+i_hlen
], i_port
);
493 i_len
= 5 + i_hlen
+ 2;
496 if( net_Write( p_obj
, fd
, buffer
, i_len
) != i_len
)
499 /* Read the header */
500 if( net_Read( p_obj
, fd
, buffer
, 5 ) != 5 )
503 msg_Dbg( p_obj
, "socks: v=%d rep=%d atyp=%d",
504 buffer
[0], buffer
[1], buffer
[3] );
506 if( buffer
[1] != 0x00 )
508 msg_Err( p_obj
, "socks: CONNECT request failed" );
512 /* Read the remaining bytes */
513 if( buffer
[3] == 0x01 )
515 else if( buffer
[3] == 0x03 )
516 i_len
= buffer
[4] + 2;
517 else if( buffer
[3] == 0x04 )
522 if( net_Read( p_obj
, fd
, buffer
, i_len
) != i_len
)
529 void net_ListenClose( int *pi_fd
)
535 for( pi
= pi_fd
; *pi
!= -1; pi
++ )