--- sourceforge.net/trunk/rdesktop/tcp.c 2000/05/10 07:36:34 4 +++ sourceforge.net/trunk/rdesktop/tcp.c 2003/08/11 11:09:17 440 @@ -1,7 +1,7 @@ /* rdesktop: A Remote Desktop Protocol client. Protocol services - TCP layer - Copyright (C) Matthew Chapman 1999-2000 + Copyright (C) Matthew Chapman 1999-2002 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -12,113 +12,208 @@ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - + You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -#include "includes.h" - -/* Establish a connection on the TCP layer */ -HCONN tcp_connect(char *server) +#include /* select read write close */ +#include /* socket connect setsockopt */ +#include /* timeval */ +#include /* gethostbyname */ +#include /* sockaddr_in */ +#include /* TCP_NODELAY */ +#include /* inet_addr */ +#include /* errno */ +#include "rdesktop.h" + +#ifndef INADDR_NONE +#define INADDR_NONE ((unsigned long) -1) +#endif + +static int sock; +static struct stream in; +static struct stream out; +extern int tcp_port_rdp; + +/* Initialise TCP transport data packet */ +STREAM +tcp_init(uint32 maxlen) { - struct hostent *nslookup; - struct sockaddr_in servaddr; - struct connection *conn; - int sock; - int true = 1; - - if ((nslookup = gethostbyname(server)) != NULL) - { - memcpy(&servaddr.sin_addr, nslookup->h_addr, sizeof(servaddr.sin_addr)); - } - else if (!inet_aton(server, &servaddr.sin_addr)) + if (maxlen > out.size) { - fprintf(stderr, "%s: unable to resolve host\n", server); - return NULL; + out.data = (uint8 *) xrealloc(out.data, maxlen); + out.size = maxlen; } - if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) - { - fprintf(stderr, "socket: %s\n", strerror(errno)); - return NULL; - } - - servaddr.sin_family = AF_INET; - servaddr.sin_port = htons(TCP_PORT_RDP); - - if (connect(sock, (struct sockaddr *)&servaddr, sizeof(struct sockaddr)) < 0) - { - fprintf(stderr, "connect: %s\n", strerror(errno)); - close(sock); - return NULL; - } - - setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &true, sizeof(true)); - - conn = xmalloc(sizeof(struct connection)); - STREAM_INIT(conn->in, False); - STREAM_INIT(conn->out, True); - - conn->tcp_socket = sock; - return conn; -} - -/* Disconnect on the TCP layer */ -void tcp_disconnect(HCONN conn) -{ - close(conn->tcp_socket); - free(conn); + out.p = out.data; + out.end = out.data + out.size; + return &out; } /* Send TCP transport data packet */ -BOOL tcp_send(HCONN conn) +void +tcp_send(STREAM s) { - int length = conn->out.end; + int length = s->end - s->data; int sent, total = 0; while (total < length) { - sent = write(conn->tcp_socket, conn->out.data + total, - length - total); - + sent = send(sock, s->data + total, length - total, 0); if (sent <= 0) { - fprintf(stderr, "write: %s\n", strerror(errno)); - return False; + error("send: %s\n", strerror(errno)); + return; } total += sent; } - - conn->out.offset = 0; - conn->out.end = conn->out.size; - return True; } /* Receive a message on the TCP layer */ -BOOL tcp_recv(HCONN conn, int length) +STREAM +tcp_recv(STREAM s, uint32 length) { - int rcvd; + unsigned int new_length, end_offset, p_offset; + int rcvd = 0; - STREAM_SIZE(conn->in, length); - conn->in.end = conn->in.offset = 0; + if (s == NULL) + { + /* read into "new" stream */ + if (length > in.size) + { + in.data = (uint8 *) xrealloc(in.data, length); + in.size = length; + } + in.end = in.p = in.data; + s = ∈ + } + else + { + /* append to existing stream */ + new_length = (s->end - s->data) + length; + if (new_length > s->size) + { + p_offset = s->p - s->data; + end_offset = s->end - s->data; + s->data = (uint8 *) xrealloc(s->data, new_length); + s->size = new_length; + s->p = s->data + p_offset; + s->end = s->data + end_offset; + } + } while (length > 0) { - rcvd = read(conn->tcp_socket, conn->in.data + conn->in.end, - length); + if (!ui_select(sock)) + /* User quit */ + return NULL; + rcvd = recv(sock, s->end, length, 0); if (rcvd <= 0) { - fprintf(stderr, "read: %s\n", strerror(errno)); - return False; + error("recv: %s\n", strerror(errno)); + return NULL; } - conn->in.end += rcvd; + s->end += rcvd; length -= rcvd; } + return s; +} + +/* Establish a connection on the TCP layer */ +BOOL +tcp_connect(char *server) +{ + int true_value = 1; + +#ifdef IPv6 + + int n; + struct addrinfo hints, *res, *ressave; + char tcp_port_rdp_s[10]; + + snprintf(tcp_port_rdp_s, 10, "%d", tcp_port_rdp); + + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + + n = getaddrinfo(server, tcp_port_rdp_s, &hints, &res); + + if (n < 0) + { + error("getaddrinfo error:: [%s]\n", gai_strerror(n)); + return False; + } + + ressave = res; + sock = -1; + while (res) + { + sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol); + if (!(sock < 0)) + { + if (connect(sock, res->ai_addr, res->ai_addrlen) == 0) + break; + close(sock); + sock = -1; + } + res = res->ai_next; + } + freeaddrinfo(ressave); + +#else /* no IPv6 support */ + + struct hostent *nslookup; + struct sockaddr_in servaddr; + + if ((nslookup = gethostbyname(server)) != NULL) + { + memcpy(&servaddr.sin_addr, nslookup->h_addr, sizeof(servaddr.sin_addr)); + } + else if ((servaddr.sin_addr.s_addr = inet_addr(server)) == INADDR_NONE) + { + error("%s: unable to resolve host\n", server); + return False; + } + + if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) + { + error("socket: %s\n", strerror(errno)); + return False; + } + + servaddr.sin_family = AF_INET; + servaddr.sin_port = htons(tcp_port_rdp); + + if (connect(sock, (struct sockaddr *) &servaddr, sizeof(struct sockaddr)) < 0) + { + error("connect: %s\n", strerror(errno)); + close(sock); + return False; + } + +#endif /* IPv6 */ + + setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void *) &true_value, sizeof(true_value)); + + in.size = 4096; + in.data = (uint8 *) xmalloc(in.size); + + out.size = 4096; + out.data = (uint8 *) xmalloc(out.size); + return True; } + +/* Disconnect on the TCP layer */ +void +tcp_disconnect(void) +{ + close(sock); +}