--- sourceforge.net/trunk/rdesktop/tcp.c 2001/09/14 13:51:38 30 +++ sourceforge.net/trunk/rdesktop/tcp.c 2003/08/17 07:28:53 443 @@ -1,7 +1,7 @@ /* rdesktop: A Remote Desktop Protocol client. Protocol services - TCP layer - Copyright (C) Matthew Chapman 1999-2001 + 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,7 +12,7 @@ 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. @@ -28,17 +28,22 @@ #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(int maxlen) +tcp_init(uint32 maxlen) { if (maxlen > out.size) { - out.data = xrealloc(out.data, maxlen); + out.data = (uint8 *) xrealloc(out.data, maxlen); out.size = maxlen; } @@ -56,11 +61,10 @@ while (total < length) { - sent = write(sock, s->data + total, length - total); - + sent = send(sock, s->data + total, length - total, 0); if (sent <= 0) { - error("write: %s\n", strerror(errno)); + error("send: %s\n", strerror(errno)); return; } @@ -70,63 +74,115 @@ /* Receive a message on the TCP layer */ STREAM -tcp_recv(int length) +tcp_recv(STREAM s, uint32 length) { - int ret, rcvd = 0; - struct timeval tv; - fd_set rfds; + unsigned int new_length, end_offset, p_offset; + int rcvd = 0; - if (length > in.size) + if (s == NULL) { - in.data = xrealloc(in.data, length); - in.size = length; + /* 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; + } } - - in.end = in.p = in.data; while (length > 0) { - ui_process_events(); - - FD_ZERO(&rfds); - FD_SET(sock, &rfds); - tv.tv_sec = 0; - tv.tv_usec = 100; - - ret = select(sock + 1, &rfds, NULL, NULL, &tv); + if (!ui_select(sock)) + /* User quit */ + return NULL; - if (ret) + rcvd = recv(sock, s->end, length, 0); + if (rcvd <= 0) { - rcvd = read(sock, in.end, length); - - if (rcvd <= 0) - { - error("read: %s\n", strerror(errno)); - return NULL; - } - - in.end += rcvd; - length -= rcvd; + error("recv: %s\n", strerror(errno)); + return NULL; } + + s->end += rcvd; + length -= rcvd; } - return ∈ + 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: %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); + + if (sock == -1) + { + error("%s: unable to connect\n", server); + return False; + } + +#else /* no IPv6 support */ + struct hostent *nslookup; struct sockaddr_in servaddr; - int true = 1; if ((nslookup = gethostbyname(server)) != NULL) { - memcpy(&servaddr.sin_addr, nslookup->h_addr, - sizeof(servaddr.sin_addr)); + memcpy(&servaddr.sin_addr, nslookup->h_addr, sizeof(servaddr.sin_addr)); } - else if (!(servaddr.sin_addr.s_addr = inet_addr(server))) + else if ((servaddr.sin_addr.s_addr = inet_addr(server)) == INADDR_NONE) { error("%s: unable to resolve host\n", server); return False; @@ -139,32 +195,31 @@ } servaddr.sin_family = AF_INET; - servaddr.sin_port = htons(TCP_PORT_RDP); + servaddr.sin_port = htons(tcp_port_rdp); - if (connect - (sock, (struct sockaddr *) &servaddr, - sizeof(struct sockaddr)) < 0) + if (connect(sock, (struct sockaddr *) &servaddr, sizeof(struct sockaddr)) < 0) { error("connect: %s\n", strerror(errno)); close(sock); return False; } - setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void *) &true, - sizeof(true)); +#endif /* IPv6 */ + + setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void *) &true_value, sizeof(true_value)); in.size = 4096; - in.data = xmalloc(in.size); + in.data = (uint8 *) xmalloc(in.size); out.size = 4096; - out.data = xmalloc(out.size); + out.data = (uint8 *) xmalloc(out.size); return True; } /* Disconnect on the TCP layer */ void -tcp_disconnect() +tcp_disconnect(void) { close(sock); }