a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* -*- c-basic-offset: 8 -*-
a180a41bba1d50822df23fff0099e90b86638b89vboxsync rdesktop: A Remote Desktop Protocol client.
a180a41bba1d50822df23fff0099e90b86638b89vboxsync Protocol services - TCP layer
a180a41bba1d50822df23fff0099e90b86638b89vboxsync Copyright (C) Matthew Chapman <matthewc.unsw.edu.au> 1999-2008
a180a41bba1d50822df23fff0099e90b86638b89vboxsync Copyright 2005-2011 Peter Astrand <astrand@cendio.se> for Cendio AB
a180a41bba1d50822df23fff0099e90b86638b89vboxsync Copyright 2012-2013 Henrik Andersson <hean01@cendio.se> for Cendio AB
a180a41bba1d50822df23fff0099e90b86638b89vboxsync This program is free software: you can redistribute it and/or modify
a180a41bba1d50822df23fff0099e90b86638b89vboxsync it under the terms of the GNU General Public License as published by
a180a41bba1d50822df23fff0099e90b86638b89vboxsync the Free Software Foundation, either version 3 of the License, or
a180a41bba1d50822df23fff0099e90b86638b89vboxsync (at your option) any later version.
a180a41bba1d50822df23fff0099e90b86638b89vboxsync This program is distributed in the hope that it will be useful,
a180a41bba1d50822df23fff0099e90b86638b89vboxsync but WITHOUT ANY WARRANTY; without even the implied warranty of
a180a41bba1d50822df23fff0099e90b86638b89vboxsync MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a180a41bba1d50822df23fff0099e90b86638b89vboxsync GNU General Public License for more details.
a180a41bba1d50822df23fff0099e90b86638b89vboxsync You should have received a copy of the GNU General Public License
a180a41bba1d50822df23fff0099e90b86638b89vboxsync along with this program. If not, see <http://www.gnu.org/licenses/>.
6e9aa255e3376b2da5824c09c4c62bc233463bfevboxsync * Oracle GPL Disclaimer: For the avoidance of doubt, except that if any license choice
6e9aa255e3376b2da5824c09c4c62bc233463bfevboxsync * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
6e9aa255e3376b2da5824c09c4c62bc233463bfevboxsync * the General Public License version 2 (GPLv2) at this time for any software where
6e9aa255e3376b2da5824c09c4c62bc233463bfevboxsync * a choice of GPL license versions is made available with the language indicating
6e9aa255e3376b2da5824c09c4c62bc233463bfevboxsync * that GPLv2 or any later version may be used, or where a choice of which version
6e9aa255e3376b2da5824c09c4c62bc233463bfevboxsync * of the GPL is applied is otherwise unspecified.
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#include <sys/socket.h> /* socket connect setsockopt */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#define TCP_BLOCKS (WSAGetLastError() == WSAEWOULDBLOCK)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* wait till socket is ready to write or timeout */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* Initialise TCP transport data packet */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync cur_stream_id = (cur_stream_id + 1) % STREAM_COUNT;
a180a41bba1d50822df23fff0099e90b86638b89vboxsync result->data = (uint8 *) xrealloc(result->data, maxlen);
a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* Send TCP transport data packet */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync sent = SSL_write(g_ssl, s->data + total, length - total);
a180a41bba1d50822df23fff0099e90b86638b89vboxsync error("SSL_write: %d (%s)\n", ssl_err, TCP_STRERROR);
a180a41bba1d50822df23fff0099e90b86638b89vboxsync sent = send(g_sock, s->data + total, length - total, 0);
a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* Receive a message on the TCP layer */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync /* read into "new" stream */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync /* append to existing stream */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync if ((!g_ssl || SSL_pending(g_ssl) <= 0) && g_run_ui)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync /* User quit */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync if (SSL_get_shutdown(g_ssl) & SSL_RECEIVED_SHUTDOWN)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync if (ssl_err == SSL_ERROR_WANT_READ || ssl_err == SSL_ERROR_WANT_WRITE)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync error("SSL_read: %d (%s)\n", ssl_err, TCP_STRERROR);
a180a41bba1d50822df23fff0099e90b86638b89vboxsync else if (rcvd == 0)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* Establish a SSL/TLS 1.0 connection */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync /* create process context */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync error("tcp_tls_connect: SSL_CTX_new() failed to create TLS v1.0 context\n");
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#endif // __SSL_OP_NO_COMPRESSION
a180a41bba1d50822df23fff0099e90b86638b89vboxsync /* free old connection */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync /* create new ssl connection */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync while (SSL_get_error(g_ssl, err) == SSL_ERROR_WANT_READ);
a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* Get public key from server of TLS 1.0 connection */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync error("tcp_tls_get_server_pubkey: SSL_get_peer_certificate() failed\n");
a180a41bba1d50822df23fff0099e90b86638b89vboxsync error("tcp_tls_get_server_pubkey: X509_get_pubkey() failed\n");
a180a41bba1d50822df23fff0099e90b86638b89vboxsync error("tcp_tls_get_server_pubkey: i2d_PublicKey() failed\n");
a180a41bba1d50822df23fff0099e90b86638b89vboxsync return (s->size != 0);
a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* Establish a connection on the TCP layer */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync snprintf(tcp_port_rdp_s, 10, "%d", g_tcp_port_rdp);
a180a41bba1d50822df23fff0099e90b86638b89vboxsync if ((n = getaddrinfo(server, tcp_port_rdp_s, &hints, &res)))
a180a41bba1d50822df23fff0099e90b86638b89vboxsync g_sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
a180a41bba1d50822df23fff0099e90b86638b89vboxsync if (connect(g_sock, res->ai_addr, res->ai_addrlen) == 0)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#else /* no IPv6 support */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync memcpy(&servaddr.sin_addr, nslookup->h_addr, sizeof(servaddr.sin_addr));
a180a41bba1d50822df23fff0099e90b86638b89vboxsync else if ((servaddr.sin_addr.s_addr = inet_addr(server)) == INADDR_NONE)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync if ((g_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync servaddr.sin_port = htons((uint16) g_tcp_port_rdp);
a180a41bba1d50822df23fff0099e90b86638b89vboxsync if (connect(g_sock, (struct sockaddr *) &servaddr, sizeof(struct sockaddr)) < 0)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync#endif /* IPv6 */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync setsockopt(g_sock, IPPROTO_TCP, TCP_NODELAY, (void *) &option_value, option_len);
a180a41bba1d50822df23fff0099e90b86638b89vboxsync /* receive buffer must be a least 16 K */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync if (getsockopt(g_sock, SOL_SOCKET, SO_RCVBUF, (void *) &option_value, &option_len) == 0)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync setsockopt(g_sock, SOL_SOCKET, SO_RCVBUF, (void *) &option_value,
a180a41bba1d50822df23fff0099e90b86638b89vboxsync for (i = 0; i < STREAM_COUNT; i++)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* Disconnect on the TCP layer */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync if (getsockname(g_sock, (struct sockaddr *) &sockaddr, &len) == 0)
a180a41bba1d50822df23fff0099e90b86638b89vboxsync sprintf(ipaddr, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
a180a41bba1d50822df23fff0099e90b86638b89vboxsync if (getpeername(g_sock, (struct sockaddr *) &sockaddr, &len))
a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* reset the state of the tcp layer */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync/* Support for Session Directory */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync /* Clear the incoming stream */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync /* Clear the outgoing stream(s) */
a180a41bba1d50822df23fff0099e90b86638b89vboxsync for (i = 0; i < STREAM_COUNT; i++)