1 |
matty |
3 |
/* |
2 |
|
|
rdesktop: A Remote Desktop Protocol client. |
3 |
|
|
Protocol services - TCP layer |
4 |
|
|
Copyright (C) Matthew Chapman 1999-2000 |
5 |
|
|
|
6 |
|
|
This program is free software; you can redistribute it and/or modify |
7 |
|
|
it under the terms of the GNU General Public License as published by |
8 |
|
|
the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
(at your option) any later version. |
10 |
|
|
|
11 |
|
|
This program is distributed in the hope that it will be useful, |
12 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
GNU General Public License for more details. |
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License |
17 |
|
|
along with this program; if not, write to the Free Software |
18 |
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 |
|
|
*/ |
20 |
|
|
|
21 |
matty |
10 |
#include <unistd.h> /* select read write close */ |
22 |
|
|
#include <sys/socket.h> /* socket connect setsockopt */ |
23 |
|
|
#include <sys/time.h> /* timeval */ |
24 |
|
|
#include <netdb.h> /* gethostbyname */ |
25 |
|
|
#include <netinet/tcp.h> /* TCP_NODELAY */ |
26 |
|
|
#include <arpa/inet.h> /* inet_aton */ |
27 |
|
|
#include <errno.h> /* errno */ |
28 |
|
|
#include "rdesktop.h" |
29 |
matty |
3 |
|
30 |
matty |
10 |
static int sock; |
31 |
|
|
static struct stream in; |
32 |
|
|
static struct stream out; |
33 |
|
|
|
34 |
|
|
/* Initialise TCP transport data packet */ |
35 |
|
|
STREAM tcp_init(int maxlen) |
36 |
matty |
3 |
{ |
37 |
matty |
10 |
if (maxlen > out.size) |
38 |
matty |
3 |
{ |
39 |
matty |
10 |
out.data = xrealloc(out.data, maxlen); |
40 |
|
|
out.size = maxlen; |
41 |
matty |
3 |
} |
42 |
|
|
|
43 |
matty |
10 |
out.p = out.data; |
44 |
|
|
out.end = out.data + out.size; |
45 |
|
|
return &out; |
46 |
matty |
3 |
} |
47 |
|
|
|
48 |
|
|
/* Send TCP transport data packet */ |
49 |
matty |
10 |
void tcp_send(STREAM s) |
50 |
matty |
3 |
{ |
51 |
matty |
10 |
int length = s->end - s->data; |
52 |
matty |
3 |
int sent, total = 0; |
53 |
|
|
|
54 |
|
|
while (total < length) |
55 |
|
|
{ |
56 |
matty |
10 |
sent = write(sock, s->data + total, length - total); |
57 |
matty |
3 |
|
58 |
|
|
if (sent <= 0) |
59 |
|
|
{ |
60 |
matty |
10 |
STATUS("write: %s\n", strerror(errno)); |
61 |
|
|
return; |
62 |
matty |
3 |
} |
63 |
|
|
|
64 |
|
|
total += sent; |
65 |
|
|
} |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
/* Receive a message on the TCP layer */ |
69 |
matty |
10 |
STREAM tcp_recv(int length) |
70 |
matty |
3 |
{ |
71 |
matty |
9 |
int ret, rcvd = 0; |
72 |
|
|
struct timeval tv; |
73 |
|
|
fd_set rfds; |
74 |
matty |
3 |
|
75 |
matty |
10 |
if (length > in.size) |
76 |
|
|
{ |
77 |
|
|
in.data = xrealloc(in.data, length); |
78 |
|
|
in.size = length; |
79 |
|
|
} |
80 |
matty |
3 |
|
81 |
matty |
10 |
in.end = in.p = in.data; |
82 |
|
|
|
83 |
matty |
3 |
while (length > 0) |
84 |
|
|
{ |
85 |
matty |
10 |
ui_process_events(); |
86 |
matty |
3 |
|
87 |
matty |
9 |
FD_ZERO(&rfds); |
88 |
matty |
10 |
FD_SET(sock, &rfds); |
89 |
matty |
9 |
tv.tv_sec = 0; |
90 |
|
|
tv.tv_usec = 100; |
91 |
|
|
|
92 |
matty |
10 |
ret = select(sock+1, &rfds, NULL, NULL, &tv); |
93 |
matty |
9 |
|
94 |
|
|
if (ret) |
95 |
matty |
3 |
{ |
96 |
matty |
10 |
rcvd = read(sock, in.end, length); |
97 |
matty |
9 |
|
98 |
|
|
if (rcvd <= 0) |
99 |
|
|
{ |
100 |
matty |
10 |
STATUS("read: %s\n", strerror(errno)); |
101 |
|
|
return NULL; |
102 |
matty |
9 |
} |
103 |
|
|
|
104 |
matty |
10 |
in.end += rcvd; |
105 |
matty |
9 |
length -= rcvd; |
106 |
matty |
3 |
} |
107 |
|
|
} |
108 |
|
|
|
109 |
matty |
10 |
return ∈ |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
/* Establish a connection on the TCP layer */ |
113 |
|
|
BOOL tcp_connect(char *server) |
114 |
|
|
{ |
115 |
|
|
struct hostent *nslookup; |
116 |
|
|
struct sockaddr_in servaddr; |
117 |
|
|
int true = 1; |
118 |
|
|
|
119 |
|
|
if ((nslookup = gethostbyname(server)) != NULL) |
120 |
|
|
{ |
121 |
|
|
memcpy(&servaddr.sin_addr, nslookup->h_addr, sizeof(servaddr.sin_addr)); |
122 |
|
|
} |
123 |
|
|
else if (!inet_aton(server, &servaddr.sin_addr)) |
124 |
|
|
{ |
125 |
|
|
STATUS("%s: unable to resolve host\n", server); |
126 |
|
|
return False; |
127 |
|
|
} |
128 |
|
|
|
129 |
|
|
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) |
130 |
|
|
{ |
131 |
|
|
STATUS("socket: %s\n", strerror(errno)); |
132 |
|
|
return False; |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
servaddr.sin_family = AF_INET; |
136 |
|
|
servaddr.sin_port = htons(TCP_PORT_RDP); |
137 |
|
|
|
138 |
|
|
if (connect(sock, (struct sockaddr *)&servaddr, sizeof(struct sockaddr)) < 0) |
139 |
|
|
{ |
140 |
|
|
STATUS("connect: %s\n", strerror(errno)); |
141 |
|
|
close(sock); |
142 |
|
|
return False; |
143 |
|
|
} |
144 |
|
|
|
145 |
|
|
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &true, sizeof(true)); |
146 |
|
|
|
147 |
|
|
in.size = 4096; |
148 |
|
|
in.data = xmalloc(in.size); |
149 |
|
|
|
150 |
|
|
out.size = 4096; |
151 |
|
|
out.data = xmalloc(out.size); |
152 |
|
|
|
153 |
matty |
3 |
return True; |
154 |
|
|
} |
155 |
matty |
10 |
|
156 |
|
|
/* Disconnect on the TCP layer */ |
157 |
|
|
void tcp_disconnect() |
158 |
|
|
{ |
159 |
|
|
close(sock); |
160 |
|
|
} |