CVS: rdesktop tcp.c,1.34,1.35

Jay Sorg <[email protected]> Fri, 22 Dec 2006 19:24:02 -0800
Newsgroups gmane.network.rdesktop.cvs
Message-ID <[email protected]>
Update of /cvsroot/rdesktop/rdesktop
In directory sc8-pr-cvs9.sourceforge.net:/tmp/cvs-serv21320

Modified Files:
	tcp.c 
Log Message:
win32 bits

Index: tcp.c
===================================================================
RCS file: /cvsroot/rdesktop/rdesktop/tcp.c,v
retrieving revision 1.34
retrieving revision 1.35
diff -C2 -d -r1.34 -r1.35
*** tcp.c	3 Nov 2006 23:51:35 -0000	1.34
--- tcp.c	23 Dec 2006 03:23:59 -0000	1.35
***************
*** 3,12 ****
     Protocol services - TCP layer
     Copyright (C) Matthew Chapman 1999-2005
!    
     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
     the Free Software Foundation; either version 2 of the License, or
     (at your option) any later version.
!    
     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
--- 3,12 ----
     Protocol services - TCP layer
     Copyright (C) Matthew Chapman 1999-2005
! 
     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
     the Free Software Foundation; either version 2 of the License, or
     (at your option) any later version.
! 
     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
***************
*** 19,22 ****
--- 19,23 ----
  */
  
+ #ifndef _WIN32
  #include <unistd.h>		/* select read write close */
  #include <sys/socket.h>		/* socket connect setsockopt */
***************
*** 27,43 ****
  #include <arpa/inet.h>		/* inet_addr */
  #include <errno.h>		/* errno */
  #include "rdesktop.h"
  
  #ifndef INADDR_NONE
  #define INADDR_NONE ((unsigned long) -1)
  #endif
  
! static int sock;
! static struct stream in;
  #ifndef WITH_SCARD
! static struct stream out;
  #endif
  int g_tcp_port_rdp = TCP_PORT_RDP;
  
  /* Initialise TCP transport data packet */
  STREAM
--- 28,77 ----
  #include <arpa/inet.h>		/* inet_addr */
  #include <errno.h>		/* errno */
+ #endif
+ 
  #include "rdesktop.h"
  
+ #ifdef _WIN32
+ #define socklen_t int
+ #define TCP_CLOSE(_sck) closesocket(_sck)
+ #define TCP_STRERROR "tcp error"
+ #define TCP_BLOCKS (WSAGetLastError() == WSAEWOULDBLOCK)
+ #else
+ #define TCP_CLOSE(_sck) close(_sck)
+ #define TCP_STRERROR strerror(errno)
+ #define TCP_BLOCKS (errno == EWOULDBLOCK)
+ #endif
+ 
  #ifndef INADDR_NONE
  #define INADDR_NONE ((unsigned long) -1)
  #endif
  
! static int g_sock;
! static struct stream g_in;
  #ifndef WITH_SCARD
! static struct stream g_out;
  #endif
  int g_tcp_port_rdp = TCP_PORT_RDP;
  
+ /* wait till socket is ready to write or timeout */
+ static BOOL
+ tcp_can_send(int sck, int millis)
+ {
+ 	fd_set wfds;
+ 	struct timeval time;
+ 	int sel_count;
+ 
+ 	time.tv_sec = millis / 1000;
+ 	time.tv_usec = (millis * 1000) % 1000000;
+ 	FD_ZERO(&wfds);
+ 	FD_SET(sck, &wfds);
+ 	sel_count = select(sck + 1, 0, &wfds, 0, &time);
+ 	if (sel_count > 0)
+ 	{
+ 		return True;
+ 	}
+ 	return False;
+ }
+ 
  /* Initialise TCP transport data packet */
  STREAM
***************
*** 50,54 ****
  	result = scard_tcp_init();
  #else
! 	result = &out;
  #endif
  
--- 84,88 ----
  	result = scard_tcp_init();
  #else
! 	result = &g_out;
  #endif
  
***************
*** 79,89 ****
  	while (total < length)
  	{
! 		sent = send(sock, s->data + total, length - total, 0);
  		if (sent <= 0)
  		{
! 			error("send: %s\n", strerror(errno));
! 			return;
  		}
- 
  		total += sent;
  	}
--- 113,130 ----
  	while (total < length)
  	{
! 		sent = send(g_sock, s->data + total, length - total, 0);
  		if (sent <= 0)
  		{
! 			if (sent == -1 && TCP_BLOCKS)
! 			{
! 				tcp_can_send(g_sock, 100);
! 				sent = 0;
! 			}
! 			else
! 			{
! 				error("send: %s\n", TCP_STRERROR);
! 				return;
! 			}
  		}
  		total += sent;
  	}
***************
*** 97,101 ****
  tcp_recv(STREAM s, uint32 length)
  {
! 	unsigned int new_length, end_offset, p_offset;
  	int rcvd = 0;
  
--- 138,142 ----
  tcp_recv(STREAM s, uint32 length)
  {
! 	uint32 new_length, end_offset, p_offset;
  	int rcvd = 0;
  
***************
*** 103,113 ****
  	{
  		/* 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 = &in;
  	}
  	else
--- 144,154 ----
  	{
  		/* read into "new" stream */
! 		if (length > g_in.size)
  		{
! 			g_in.data = (uint8 *) xrealloc(g_in.data, length);
! 			g_in.size = length;
  		}
! 		g_in.end = g_in.p = g_in.data;
! 		s = &g_in;
  	}
  	else
***************
*** 128,140 ****
  	while (length > 0)
  	{
! 		if (!ui_select(sock))
  			/* User quit */
  			return NULL;
  
! 		rcvd = recv(sock, s->end, length, 0);
  		if (rcvd < 0)
  		{
! 			error("recv: %s\n", strerror(errno));
! 			return NULL;
  		}
  		else if (rcvd == 0)
--- 169,188 ----
  	while (length > 0)
  	{
! 		if (!ui_select(g_sock))
  			/* User quit */
  			return NULL;
  
! 		rcvd = recv(g_sock, s->end, length, 0);
  		if (rcvd < 0)
  		{
! 			if (rcvd == -1 && TCP_BLOCKS)
! 			{
! 				rcvd = 0;
! 			}
! 			else
! 			{
! 				error("recv: %s\n", TCP_STRERROR);
! 				return NULL;
! 			}
  		}
  		else if (rcvd == 0)
***************
*** 155,159 ****
  tcp_connect(char *server)
  {
! 	int true_value = 1;
  
  #ifdef IPv6
--- 203,208 ----
  tcp_connect(char *server)
  {
! 	socklen_t option_len;
! 	uint32 option_value;
  
  #ifdef IPv6
***************
*** 176,189 ****
  
  	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;
--- 225,238 ----
  
  	ressave = res;
! 	g_sock = -1;
  	while (res)
  	{
! 		g_sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
! 		if (!(g_sock < 0))
  		{
! 			if (connect(g_sock, res->ai_addr, res->ai_addrlen) == 0)
  				break;
! 			TCP_CLOSE(g_sock);
! 			g_sock = -1;
  		}
  		res = res->ai_next;
***************
*** 191,195 ****
  	freeaddrinfo(ressave);
  
! 	if (sock == -1)
  	{
  		error("%s: unable to connect\n", server);
--- 240,244 ----
  	freeaddrinfo(ressave);
  
! 	if (g_sock == -1)
  	{
  		error("%s: unable to connect\n", server);
***************
*** 212,228 ****
  	}
  
! 	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(g_tcp_port_rdp);
  
! 	if (connect(sock, (struct sockaddr *) &servaddr, sizeof(struct sockaddr)) < 0)
  	{
! 		error("connect: %s\n", strerror(errno));
! 		close(sock);
  		return False;
  	}
--- 261,277 ----
  	}
  
! 	if ((g_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  	{
! 		error("socket: %s\n", TCP_STRERROR);
  		return False;
  	}
  
  	servaddr.sin_family = AF_INET;
! 	servaddr.sin_port = htons((uint16) g_tcp_port_rdp);
  
! 	if (connect(g_sock, (struct sockaddr *) &servaddr, sizeof(struct sockaddr)) < 0)
  	{
! 		error("connect: %s\n", TCP_STRERROR);
! 		TCP_CLOSE(g_sock);
  		return False;
  	}
***************
*** 230,243 ****
  #endif /* IPv6 */
  
! 	setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void *) &true_value, sizeof(true_value));
  
! 	in.size = 4096;
! 	in.data = (uint8 *) xmalloc(in.size);
  
  #ifdef WITH_SCARD
  	scard_tcp_connect();
  #else
! 	out.size = 4096;
! 	out.data = (uint8 *) xmalloc(out.size);
  #endif
  
--- 279,304 ----
  #endif /* IPv6 */
  
! 	option_value = 1;
! 	option_len = sizeof(option_value);
! 	setsockopt(g_sock, IPPROTO_TCP, TCP_NODELAY, (void *) &option_value, option_len);
! 	/* receive buffer must be a least 16 K */
! 	if (getsockopt(g_sock, SOL_SOCKET, SO_RCVBUF, (void *) &option_value, &option_len) == 0)
! 	{
! 		if (option_value < (1024 * 16))
! 		{
! 			option_value = 1024 * 16;
! 			option_len = sizeof(option_value);
! 			setsockopt(g_sock, SOL_SOCKET, SO_RCVBUF, (void *) &option_value, option_len);
! 		}
! 	}
  
! 	g_in.size = 4096;
! 	g_in.data = (uint8 *) xmalloc(g_in.size);
  
  #ifdef WITH_SCARD
  	scard_tcp_connect();
  #else
! 	g_out.size = 4096;
! 	g_out.data = (uint8 *) xmalloc(g_out.size);
  #endif
  
***************
*** 249,253 ****
  tcp_disconnect(void)
  {
! 	close(sock);
  }
  
--- 310,314 ----
  tcp_disconnect(void)
  {
! 	TCP_CLOSE(g_sock);
  }
  
***************
*** 258,264 ****
  	struct sockaddr_in sockaddr;
  	socklen_t len = sizeof(sockaddr);
! 	if (getsockname(sock, (struct sockaddr *) &sockaddr, &len) == 0)
  	{
! 		unsigned char *ip = (unsigned char *) &sockaddr.sin_addr;
  		sprintf(ipaddr, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
  	}
--- 319,325 ----
  	struct sockaddr_in sockaddr;
  	socklen_t len = sizeof(sockaddr);
! 	if (getsockname(g_sock, (struct sockaddr *) &sockaddr, &len) == 0)
  	{
! 		uint8 *ip = (uint8 *) &sockaddr.sin_addr;
  		sprintf(ipaddr, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
  	}
***************
*** 273,290 ****
  tcp_reset_state(void)
  {
! 	sock = -1;		/* reset socket */
  
  	/* Clear the incoming stream */
! 	if (in.data != NULL)
! 		xfree(in.data);
! 	in.p = NULL;
! 	in.end = NULL;
! 	in.data = NULL;
! 	in.size = 0;
! 	in.iso_hdr = NULL;
! 	in.mcs_hdr = NULL;
! 	in.sec_hdr = NULL;
! 	in.rdp_hdr = NULL;
! 	in.channel_hdr = NULL;
  
  	/* Clear the outgoing stream(s) */
--- 334,351 ----
  tcp_reset_state(void)
  {
! 	g_sock = -1;		/* reset socket */
  
  	/* Clear the incoming stream */
! 	if (g_in.data != NULL)
! 		xfree(g_in.data);
! 	g_in.p = NULL;
! 	g_in.end = NULL;
! 	g_in.data = NULL;
! 	g_in.size = 0;
! 	g_in.iso_hdr = NULL;
! 	g_in.mcs_hdr = NULL;
! 	g_in.sec_hdr = NULL;
! 	g_in.rdp_hdr = NULL;
! 	g_in.channel_hdr = NULL;
  
  	/* Clear the outgoing stream(s) */
***************
*** 292,306 ****
  	scard_tcp_reset_state();
  #else
! 	if (out.data != NULL)
! 		xfree(out.data);
! 	out.p = NULL;
! 	out.end = NULL;
! 	out.data = NULL;
! 	out.size = 0;
! 	out.iso_hdr = NULL;
! 	out.mcs_hdr = NULL;
! 	out.sec_hdr = NULL;
! 	out.rdp_hdr = NULL;
! 	out.channel_hdr = NULL;
  #endif
  }
--- 353,367 ----
  	scard_tcp_reset_state();
  #else
! 	if (g_out.data != NULL)
! 		xfree(g_out.data);
! 	g_out.p = NULL;
! 	g_out.end = NULL;
! 	g_out.data = NULL;
! 	g_out.size = 0;
! 	g_out.iso_hdr = NULL;
! 	g_out.mcs_hdr = NULL;
! 	g_out.sec_hdr = NULL;
! 	g_out.rdp_hdr = NULL;
! 	g_out.channel_hdr = NULL;
  #endif
  }


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV