2000-05-10 09:36:34 +02:00
|
|
|
/*
|
|
|
|
rdesktop: A Remote Desktop Protocol client.
|
|
|
|
Protocol services - TCP layer
|
2002-09-26 16:26:46 +02:00
|
|
|
Copyright (C) Matthew Chapman 1999-2002
|
2000-05-10 09:36:34 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2001-01-06 04:12:10 +01:00
|
|
|
#include <unistd.h> /* select read write close */
|
|
|
|
#include <sys/socket.h> /* socket connect setsockopt */
|
|
|
|
#include <sys/time.h> /* timeval */
|
|
|
|
#include <netdb.h> /* gethostbyname */
|
|
|
|
#include <netinet/in.h> /* sockaddr_in */
|
|
|
|
#include <netinet/tcp.h> /* TCP_NODELAY */
|
|
|
|
#include <arpa/inet.h> /* inet_addr */
|
|
|
|
#include <errno.h> /* errno */
|
2000-08-15 12:23:24 +02:00
|
|
|
#include "rdesktop.h"
|
|
|
|
|
|
|
|
static int sock;
|
|
|
|
static struct stream in;
|
|
|
|
static struct stream out;
|
2002-07-14 02:34:21 +02:00
|
|
|
extern int tcp_port_rdp;
|
2000-08-15 12:23:24 +02:00
|
|
|
|
|
|
|
/* Initialise TCP transport data packet */
|
2001-01-06 04:47:04 +01:00
|
|
|
STREAM
|
|
|
|
tcp_init(int maxlen)
|
2000-05-10 09:36:34 +02:00
|
|
|
{
|
2000-08-15 12:23:24 +02:00
|
|
|
if (maxlen > out.size)
|
2000-05-10 09:36:34 +02:00
|
|
|
{
|
2000-08-15 12:23:24 +02:00
|
|
|
out.data = xrealloc(out.data, maxlen);
|
|
|
|
out.size = maxlen;
|
2000-05-10 09:36:34 +02:00
|
|
|
}
|
|
|
|
|
2000-08-15 12:23:24 +02:00
|
|
|
out.p = out.data;
|
|
|
|
out.end = out.data + out.size;
|
|
|
|
return &out;
|
2000-05-10 09:36:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Send TCP transport data packet */
|
2001-01-06 04:47:04 +01:00
|
|
|
void
|
|
|
|
tcp_send(STREAM s)
|
2000-05-10 09:36:34 +02:00
|
|
|
{
|
2001-01-06 04:12:10 +01:00
|
|
|
int length = s->end - s->data;
|
2000-05-10 09:36:34 +02:00
|
|
|
int sent, total = 0;
|
|
|
|
|
|
|
|
while (total < length)
|
|
|
|
{
|
2001-09-15 14:34:34 +02:00
|
|
|
sent = send(sock, s->data + total, length - total, 0);
|
2000-05-10 09:36:34 +02:00
|
|
|
if (sent <= 0)
|
|
|
|
{
|
2001-09-15 14:34:34 +02:00
|
|
|
error("send: %s\n", strerror(errno));
|
2000-08-15 12:23:24 +02:00
|
|
|
return;
|
2000-05-10 09:36:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
total += sent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Receive a message on the TCP layer */
|
2001-01-06 04:47:04 +01:00
|
|
|
STREAM
|
|
|
|
tcp_recv(int length)
|
2000-05-10 09:36:34 +02:00
|
|
|
{
|
2001-09-15 14:34:34 +02:00
|
|
|
int rcvd = 0;
|
2000-05-10 09:36:34 +02:00
|
|
|
|
2000-08-15 12:23:24 +02:00
|
|
|
if (length > in.size)
|
|
|
|
{
|
|
|
|
in.data = xrealloc(in.data, length);
|
|
|
|
in.size = length;
|
|
|
|
}
|
|
|
|
|
|
|
|
in.end = in.p = in.data;
|
2000-05-10 09:36:34 +02:00
|
|
|
|
|
|
|
while (length > 0)
|
|
|
|
{
|
2002-11-19 15:48:02 +01:00
|
|
|
if (!ui_select(sock))
|
|
|
|
/* User quit */
|
|
|
|
return NULL;
|
2000-07-25 14:34:29 +02:00
|
|
|
|
2001-09-15 14:34:34 +02:00
|
|
|
rcvd = recv(sock, in.end, length, 0);
|
|
|
|
if (rcvd == -1)
|
2000-05-10 09:36:34 +02:00
|
|
|
{
|
2001-09-15 14:34:34 +02:00
|
|
|
error("recv: %s\n", strerror(errno));
|
|
|
|
return NULL;
|
2000-05-10 09:36:34 +02:00
|
|
|
}
|
2001-09-15 14:34:34 +02:00
|
|
|
|
|
|
|
in.end += rcvd;
|
|
|
|
length -= rcvd;
|
2000-05-10 09:36:34 +02:00
|
|
|
}
|
|
|
|
|
2000-08-15 12:23:24 +02:00
|
|
|
return ∈
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Establish a connection on the TCP layer */
|
2001-01-06 04:47:04 +01:00
|
|
|
BOOL
|
|
|
|
tcp_connect(char *server)
|
2000-08-15 12:23:24 +02:00
|
|
|
{
|
|
|
|
struct hostent *nslookup;
|
|
|
|
struct sockaddr_in servaddr;
|
|
|
|
int true = 1;
|
|
|
|
|
|
|
|
if ((nslookup = gethostbyname(server)) != NULL)
|
|
|
|
{
|
2002-07-30 09:18:48 +02:00
|
|
|
memcpy(&servaddr.sin_addr, nslookup->h_addr, sizeof(servaddr.sin_addr));
|
2000-08-15 12:23:24 +02:00
|
|
|
}
|
2000-08-15 14:01:01 +02:00
|
|
|
else if (!(servaddr.sin_addr.s_addr = inet_addr(server)))
|
2000-08-15 12:23:24 +02:00
|
|
|
{
|
2001-09-14 15:51:38 +02:00
|
|
|
error("%s: unable to resolve host\n", server);
|
2000-08-15 12:23:24 +02:00
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
|
|
|
|
{
|
2001-09-14 15:51:38 +02:00
|
|
|
error("socket: %s\n", strerror(errno));
|
2000-08-15 12:23:24 +02:00
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
servaddr.sin_family = AF_INET;
|
2002-07-14 02:34:21 +02:00
|
|
|
servaddr.sin_port = htons(tcp_port_rdp);
|
2000-08-15 12:23:24 +02:00
|
|
|
|
2002-07-30 09:18:48 +02:00
|
|
|
if (connect(sock, (struct sockaddr *) &servaddr, sizeof(struct sockaddr)) < 0)
|
2000-08-15 12:23:24 +02:00
|
|
|
{
|
2001-09-14 15:51:38 +02:00
|
|
|
error("connect: %s\n", strerror(errno));
|
2000-08-15 12:23:24 +02:00
|
|
|
close(sock);
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
2002-07-30 09:18:48 +02:00
|
|
|
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void *) &true, sizeof(true));
|
2000-08-15 12:23:24 +02:00
|
|
|
|
|
|
|
in.size = 4096;
|
|
|
|
in.data = xmalloc(in.size);
|
|
|
|
|
|
|
|
out.size = 4096;
|
|
|
|
out.data = xmalloc(out.size);
|
|
|
|
|
2000-05-10 09:36:34 +02:00
|
|
|
return True;
|
|
|
|
}
|
2000-08-15 12:23:24 +02:00
|
|
|
|
|
|
|
/* Disconnect on the TCP layer */
|
2001-01-06 04:47:04 +01:00
|
|
|
void
|
2002-09-24 09:59:14 +02:00
|
|
|
tcp_disconnect(void)
|
2000-08-15 12:23:24 +02:00
|
|
|
{
|
|
|
|
close(sock);
|
|
|
|
}
|