--- sourceforge.net/trunk/rdesktop/tcp.c 2001/01/06 03:12:10 24 +++ sourceforge.net/trunk/rdesktop/tcp.c 2003/06/06 10:44:20 408 @@ -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,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,16 +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) +STREAM +tcp_init(uint32 maxlen) { if (maxlen > out.size) { - out.data = xrealloc(out.data, maxlen); + out.data = (uint8 *) xrealloc(out.data, maxlen); out.size = maxlen; } @@ -47,18 +53,18 @@ } /* Send TCP transport data packet */ -void tcp_send(STREAM s) +void +tcp_send(STREAM s) { int length = s->end - s->data; int sent, total = 0; while (total < length) { - sent = write(sock, s->data + total, length - total); - + sent = send(sock, s->data + total, length - total, 0); if (sent <= 0) { - STATUS("write: %s\n", strerror(errno)); + error("send: %s\n", strerror(errno)); return; } @@ -67,15 +73,14 @@ } /* Receive a message on the TCP layer */ -STREAM tcp_recv(int length) +STREAM +tcp_recv(uint32 length) { - int ret, rcvd = 0; - struct timeval tv; - fd_set rfds; + int rcvd = 0; if (length > in.size) { - in.data = xrealloc(in.data, length); + in.data = (uint8 *) xrealloc(in.data, length); in.size = length; } @@ -83,83 +88,72 @@ while (length > 0) { - ui_process_events(); - - FD_ZERO(&rfds); - FD_SET(sock, &rfds); - tv.tv_sec = 0; - tv.tv_usec = 100; + if (!ui_select(sock)) + /* User quit */ + return NULL; - ret = select(sock + 1, &rfds, NULL, NULL, &tv); - - if (ret) + rcvd = recv(sock, in.end, length, 0); + if (rcvd == -1) { - rcvd = read(sock, in.end, length); - - if (rcvd <= 0) - { - STATUS("read: %s\n", strerror(errno)); - return NULL; - } - - in.end += rcvd; - length -= rcvd; + error("recv: %s\n", strerror(errno)); + return NULL; } + + in.end += rcvd; + length -= rcvd; } return ∈ } /* Establish a connection on the TCP layer */ -BOOL tcp_connect(char *server) +BOOL +tcp_connect(char *server) { struct hostent *nslookup; struct sockaddr_in servaddr; - int true = 1; + int true_value = 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) { - STATUS("%s: unable to resolve host\n", server); + error("%s: unable to resolve host\n", server); return False; } if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { - STATUS("socket: %s\n", strerror(errno)); + error("socket: %s\n", strerror(errno)); return False; } 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) { - STATUS("connect: %s\n", strerror(errno)); + error("connect: %s\n", strerror(errno)); close(sock); return False; } - setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void *) &true, - sizeof(true)); + 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() +void +tcp_disconnect(void) { close(sock); }