contrib: protobuf: Require version 3.1.0+
[vlc.git] / src / network / tcp.c
blobf03a7168994b9960f1b1a3ae5032a23d2dfb76bc
1 /*****************************************************************************
2 * tcp.c:
3 *****************************************************************************
4 * Copyright (C) 2004-2005 VLC authors and VideoLAN
5 * Copyright (C) 2005-2006 Rémi Denis-Courmont
6 * $Id$
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 /*****************************************************************************
27 * Preamble
28 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #include <vlc_common.h>
35 #include <errno.h>
36 #include <assert.h>
37 #include <limits.h>
38 #include <unistd.h>
39 #ifdef HAVE_POLL
40 # include <poll.h>
41 #endif
43 #include <vlc_network.h>
44 #if defined (_WIN32)
45 # undef EINPROGRESS
46 # define EINPROGRESS WSAEWOULDBLOCK
47 # undef EWOULDBLOCK
48 # define EWOULDBLOCK WSAEWOULDBLOCK
49 # undef EAGAIN
50 # define EAGAIN WSAEWOULDBLOCK
51 #endif
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,
61 int i_protocol );
63 #undef net_Connect
64 /*****************************************************************************
65 * net_Connect:
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,
71 int type, int proto )
73 const char *psz_realhost;
74 char *psz_socks;
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, ':' );
82 if( psz )
83 *psz++ = '\0';
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,
90 psz_host, i_port );
92 /* We only implement TCP with SOCKS */
93 switch( type )
95 case 0:
96 type = SOCK_STREAM;
97 case SOCK_STREAM:
98 break;
99 default:
100 msg_Err( p_this, "Socket type not supported through SOCKS" );
101 free( psz_socks );
102 return -1;
104 switch( proto )
106 case 0:
107 proto = IPPROTO_TCP;
108 case IPPROTO_TCP:
109 break;
110 default:
111 msg_Err( p_this, "Transport not supported through SOCKS" );
112 free( psz_socks );
113 return -1;
116 else
118 psz_realhost = psz_host;
119 i_realport = i_port;
121 msg_Dbg( p_this, "net: connecting to %s port %d", psz_realhost,
122 i_realport );
125 struct addrinfo hints = {
126 .ai_socktype = type,
127 .ai_protocol = proto,
128 .ai_flags = AI_NUMERICSERV | AI_IDN,
129 }, *res;
131 int val = vlc_getaddrinfo_i11e(psz_realhost, i_realport, &hints, &res);
132 if (val)
134 msg_Err (p_this, "cannot resolve %s port %d : %s", psz_realhost,
135 i_realport, gai_strerror (val));
136 free( psz_socks );
137 return -1;
139 free( psz_socks );
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 );
148 if( fd == -1 )
150 msg_Dbg( p_this, "socket error: %s", vlc_strerror_c(net_errno) );
151 continue;
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) );
160 goto next_ai;
163 struct pollfd ufd;
164 mtime_t deadline = VLC_TS_INVALID;
166 ufd.fd = fd;
167 ufd.events = POLLOUT;
168 deadline = mdate() + timeout;
172 mtime_t now = mdate();
174 if (vlc_killed())
175 goto next_ai;
177 if (now > deadline)
178 now = deadline;
180 val = vlc_poll_i11e(&ufd, 1, (deadline - now) / 1000);
182 while (val == -1 && errno == EINTR);
184 switch (val)
186 case -1: /* error */
187 msg_Err (p_this, "polling error: %s",
188 vlc_strerror_c(net_errno));
189 goto next_ai;
191 case 0: /* timeout */
192 msg_Warn (p_this, "connection timed out");
193 goto next_ai;
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));
203 goto next_ai;
207 msg_Dbg( p_this, "connection succeeded (socket = %d)", fd );
208 i_handle = fd; /* success! */
209 break;
211 next_ai: /* failure */
212 net_Close( fd );
215 freeaddrinfo( res );
217 if( i_handle == -1 )
218 return -1;
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,
227 psz_host, i_port ) )
229 msg_Err( p_this, "SOCKS handshake failed" );
230 net_Close( i_handle );
231 i_handle = -1;
234 free( psz_user );
235 free( psz_pwd );
238 return i_handle;
242 int net_AcceptSingle (vlc_object_t *obj, int lfd)
244 int fd = vlc_accept (lfd, NULL, NULL, true);
245 if (fd == -1)
247 if (net_errno != EAGAIN)
248 #if (EAGAIN != EWOULDBLOCK)
249 if (net_errno != EWOULDBLOCK)
250 #endif
251 msg_Err (obj, "accept failed (from socket %d): %s", lfd,
252 vlc_strerror_c(net_errno));
253 return -1;
256 msg_Dbg (obj, "accepted socket %d (from socket %d)", fd, lfd);
257 setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &(int){ 1 }, sizeof(int));
258 return fd;
262 #undef net_Accept
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);
278 unsigned n = 0;
279 while (pi_fd[n] != -1)
280 n++;
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;
290 for (;;)
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));
297 return -1;
301 for (unsigned i = 0; i < n; i++)
303 if (ufd[i].revents == 0)
304 continue;
306 int sfd = ufd[i].fd;
307 int fd = net_AcceptSingle (p_this, sfd);
308 if (fd == -1)
309 continue;
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));
316 pi_fd[n - 1] = sfd;
317 return fd;
320 return -1;
324 /*****************************************************************************
325 * SocksNegotiate:
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];
335 int i_len;
336 bool b_auth = false;
338 if( i_socks_version != 5 )
339 return VLC_SUCCESS;
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 */
348 i_len = 4;
349 b_auth = true;
351 else
353 buffer[1] = 1; /* Number of methods */
354 buffer[2] = 0x00; /* - No auth required */
355 i_len = 3;
358 if( net_Write( p_obj, fd, buffer, i_len ) != i_len )
359 return VLC_EGENERIC;
360 if( net_Read( p_obj, fd, buffer, 2) != 2 )
361 return VLC_EGENERIC;
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" );
375 return VLC_EGENERIC;
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)" );
385 return VLC_EGENERIC;
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 )
399 return VLC_EGENERIC;
401 if( net_Read( p_obj, fd, buffer, 2 ) != 2 )
402 return VLC_EGENERIC;
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" );
408 return VLC_EGENERIC;
411 else
413 if( b_auth )
414 msg_Err( p_obj, "socks: unsupported authentication method %x",
415 buffer[0] );
416 else
417 msg_Err( p_obj, "socks: authentication needed" );
418 return VLC_EGENERIC;
421 return VLC_SUCCESS;
424 /*****************************************************************************
425 * SocksHandshakeTCP:
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,
430 int fd,
431 int i_socks_version,
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 );
440 i_socks_version = 5;
443 if( i_socks_version == 5 &&
444 SocksNegotiate( p_obj, fd, i_socks_version,
445 psz_user, psz_passwd ) )
446 return VLC_EGENERIC;
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,
455 .ai_flags = AI_IDN,
457 struct addrinfo *res;
459 if (vlc_getaddrinfo_i11e(psz_host, 0, &hints, &res))
460 return VLC_EGENERIC;
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);
467 freeaddrinfo (res);
469 buffer[8] = 0; /* Empty user id */
471 if( net_Write( p_obj, fd, buffer, 9 ) != 9 )
472 return VLC_EGENERIC;
473 if( net_Read( p_obj, fd, buffer, 8 ) != 8 )
474 return VLC_EGENERIC;
476 msg_Dbg( p_obj, "socks: v=%d cd=%d",
477 buffer[0], buffer[1] );
479 if( buffer[1] != 90 )
480 return VLC_EGENERIC;
482 else if( i_socks_version == 5 )
484 int i_hlen = __MIN(strlen( psz_host ), 255);
485 int i_len;
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 */
492 buffer[4] = i_hlen;
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 )
500 return VLC_EGENERIC;
502 /* Read the header */
503 if( net_Read( p_obj, fd, buffer, 5 ) != 5 )
504 return VLC_EGENERIC;
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" );
512 return VLC_EGENERIC;
515 /* Read the remaining bytes */
516 if( buffer[3] == 0x01 )
517 i_len = 4-1 + 2;
518 else if( buffer[3] == 0x03 )
519 i_len = buffer[4] + 2;
520 else if( buffer[3] == 0x04 )
521 i_len = 16-1+2;
522 else
523 return VLC_EGENERIC;
525 if( net_Read( p_obj, fd, buffer, i_len ) != i_len )
526 return VLC_EGENERIC;
529 return VLC_SUCCESS;
532 void net_ListenClose( int *pi_fd )
534 if( pi_fd != NULL )
536 int *pi;
538 for( pi = pi_fd; *pi != -1; pi++ )
539 net_Close( *pi );
540 free( pi_fd );