codec: dvbsub: remove usage of bs_show
[vlc.git] / src / network / tcp.c
blobf289e5c01aa64fdd1cc0809c1ae2eac37b2f7d36
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 );
61 #undef net_Connect
62 /*****************************************************************************
63 * net_Connect:
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,
69 int type, int proto )
71 const char *psz_realhost;
72 char *psz_socks;
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, ':' );
80 if( psz )
81 *psz++ = '\0';
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,
88 psz_host, i_port );
90 /* We only implement TCP with SOCKS */
91 switch( type )
93 case 0:
94 type = SOCK_STREAM;
95 case SOCK_STREAM:
96 break;
97 default:
98 msg_Err( p_this, "Socket type not supported through SOCKS" );
99 free( psz_socks );
100 return -1;
102 switch( proto )
104 case 0:
105 proto = IPPROTO_TCP;
106 case IPPROTO_TCP:
107 break;
108 default:
109 msg_Err( p_this, "Transport not supported through SOCKS" );
110 free( psz_socks );
111 return -1;
114 else
116 psz_realhost = psz_host;
117 i_realport = i_port;
119 msg_Dbg( p_this, "net: connecting to %s port %d", psz_realhost,
120 i_realport );
123 struct addrinfo hints = {
124 .ai_socktype = type,
125 .ai_protocol = proto,
126 .ai_flags = AI_NUMERICSERV | AI_IDN,
127 }, *res;
129 int val = vlc_getaddrinfo_i11e(psz_realhost, i_realport, &hints, &res);
130 if (val)
132 msg_Err (p_this, "cannot resolve %s port %d : %s", psz_realhost,
133 i_realport, gai_strerror (val));
134 free( psz_socks );
135 return -1;
137 free( psz_socks );
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 );
145 if( fd == -1 )
147 msg_Dbg( p_this, "socket error: %s", vlc_strerror_c(net_errno) );
148 continue;
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) );
157 goto next_ai;
160 struct pollfd ufd;
161 vlc_tick_t deadline = VLC_TICK_INVALID;
163 ufd.fd = fd;
164 ufd.events = POLLOUT;
165 deadline = vlc_tick_now() + timeout;
169 vlc_tick_t now = vlc_tick_now();
171 if (vlc_killed())
172 goto next_ai;
174 if (now > deadline)
175 now = deadline;
177 val = vlc_poll_i11e(&ufd, 1, MS_FROM_VLC_TICK(deadline - now));
179 while (val == -1 && errno == EINTR);
181 switch (val)
183 case -1: /* error */
184 msg_Err (p_this, "polling error: %s",
185 vlc_strerror_c(net_errno));
186 goto next_ai;
188 case 0: /* timeout */
189 msg_Warn (p_this, "connection timed out");
190 goto next_ai;
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));
200 goto next_ai;
204 msg_Dbg( p_this, "connection succeeded (socket = %d)", fd );
205 i_handle = fd; /* success! */
206 break;
208 next_ai: /* failure */
209 net_Close( fd );
212 freeaddrinfo( res );
214 if( i_handle == -1 )
215 return -1;
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,
224 psz_host, i_port ) )
226 msg_Err( p_this, "SOCKS handshake failed" );
227 net_Close( i_handle );
228 i_handle = -1;
231 free( psz_user );
232 free( psz_pwd );
235 return i_handle;
239 int net_AcceptSingle (vlc_object_t *obj, int lfd)
241 int fd = vlc_accept (lfd, NULL, NULL, true);
242 if (fd == -1)
244 if (net_errno != EAGAIN)
245 #if (EAGAIN != EWOULDBLOCK)
246 if (net_errno != EWOULDBLOCK)
247 #endif
248 msg_Err (obj, "accept failed (from socket %d): %s", lfd,
249 vlc_strerror_c(net_errno));
250 return -1;
253 msg_Dbg (obj, "accepted socket %d (from socket %d)", fd, lfd);
254 setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &(int){ 1 }, sizeof(int));
255 return fd;
259 #undef net_Accept
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);
275 unsigned n = 0;
276 while (pi_fd[n] != -1)
277 n++;
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;
287 for (;;)
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));
294 return -1;
298 for (unsigned i = 0; i < n; i++)
300 if (ufd[i].revents == 0)
301 continue;
303 int sfd = ufd[i].fd;
304 int fd = net_AcceptSingle (p_this, sfd);
305 if (fd == -1)
306 continue;
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));
313 pi_fd[n - 1] = sfd;
314 return fd;
317 return -1;
321 /*****************************************************************************
322 * SocksNegotiate:
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];
332 int i_len;
333 bool b_auth = false;
335 if( i_socks_version != 5 )
336 return VLC_SUCCESS;
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 */
345 i_len = 4;
346 b_auth = true;
348 else
350 buffer[1] = 1; /* Number of methods */
351 buffer[2] = 0x00; /* - No auth required */
352 i_len = 3;
355 if( net_Write( p_obj, fd, buffer, i_len ) != i_len )
356 return VLC_EGENERIC;
357 if( net_Read( p_obj, fd, buffer, 2) != 2 )
358 return VLC_EGENERIC;
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" );
372 return VLC_EGENERIC;
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)" );
382 return VLC_EGENERIC;
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 )
396 return VLC_EGENERIC;
398 if( net_Read( p_obj, fd, buffer, 2 ) != 2 )
399 return VLC_EGENERIC;
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" );
405 return VLC_EGENERIC;
408 else
410 if( b_auth )
411 msg_Err( p_obj, "socks: unsupported authentication method %x",
412 buffer[0] );
413 else
414 msg_Err( p_obj, "socks: authentication needed" );
415 return VLC_EGENERIC;
418 return VLC_SUCCESS;
421 /*****************************************************************************
422 * SocksHandshakeTCP:
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,
427 int fd,
428 int i_socks_version,
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 );
437 i_socks_version = 5;
440 if( i_socks_version == 5 &&
441 SocksNegotiate( p_obj, fd, i_socks_version,
442 psz_user, psz_passwd ) )
443 return VLC_EGENERIC;
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,
452 .ai_flags = AI_IDN,
454 struct addrinfo *res;
456 if (vlc_getaddrinfo_i11e(psz_host, 0, &hints, &res))
457 return VLC_EGENERIC;
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);
464 freeaddrinfo (res);
466 buffer[8] = 0; /* Empty user id */
468 if( net_Write( p_obj, fd, buffer, 9 ) != 9 )
469 return VLC_EGENERIC;
470 if( net_Read( p_obj, fd, buffer, 8 ) != 8 )
471 return VLC_EGENERIC;
473 msg_Dbg( p_obj, "socks: v=%d cd=%d",
474 buffer[0], buffer[1] );
476 if( buffer[1] != 90 )
477 return VLC_EGENERIC;
479 else if( i_socks_version == 5 )
481 int i_hlen = __MIN(strlen( psz_host ), 255);
482 int i_len;
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 */
489 buffer[4] = i_hlen;
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 )
497 return VLC_EGENERIC;
499 /* Read the header */
500 if( net_Read( p_obj, fd, buffer, 5 ) != 5 )
501 return VLC_EGENERIC;
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" );
509 return VLC_EGENERIC;
512 /* Read the remaining bytes */
513 if( buffer[3] == 0x01 )
514 i_len = 4-1 + 2;
515 else if( buffer[3] == 0x03 )
516 i_len = buffer[4] + 2;
517 else if( buffer[3] == 0x04 )
518 i_len = 16-1+2;
519 else
520 return VLC_EGENERIC;
522 if( net_Read( p_obj, fd, buffer, i_len ) != i_len )
523 return VLC_EGENERIC;
526 return VLC_SUCCESS;
529 void net_ListenClose( int *pi_fd )
531 if( pi_fd != NULL )
533 int *pi;
535 for( pi = pi_fd; *pi != -1; pi++ )
536 net_Close( *pi );
537 free( pi_fd );