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 |
24 |
#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/in.h> /* sockaddr_in */ |
26 |
|
|
#include <netinet/tcp.h> /* TCP_NODELAY */ |
27 |
|
|
#include <arpa/inet.h> /* inet_addr */ |
28 |
|
|
#include <errno.h> /* errno */ |
29 |
matty |
10 |
#include "rdesktop.h" |
30 |
matty |
3 |
|
31 |
matty |
10 |
static int sock; |
32 |
|
|
static struct stream in; |
33 |
|
|
static struct stream out; |
34 |
|
|
|
35 |
|
|
/* Initialise TCP transport data packet */ |
36 |
matty |
25 |
STREAM |
37 |
|
|
tcp_init(int maxlen) |
38 |
matty |
3 |
{ |
39 |
matty |
10 |
if (maxlen > out.size) |
40 |
matty |
3 |
{ |
41 |
matty |
10 |
out.data = xrealloc(out.data, maxlen); |
42 |
|
|
out.size = maxlen; |
43 |
matty |
3 |
} |
44 |
|
|
|
45 |
matty |
10 |
out.p = out.data; |
46 |
|
|
out.end = out.data + out.size; |
47 |
|
|
return &out; |
48 |
matty |
3 |
} |
49 |
|
|
|
50 |
|
|
/* Send TCP transport data packet */ |
51 |
matty |
25 |
void |
52 |
|
|
tcp_send(STREAM s) |
53 |
matty |
3 |
{ |
54 |
matty |
24 |
int length = s->end - s->data; |
55 |
matty |
3 |
int sent, total = 0; |
56 |
|
|
|
57 |
|
|
while (total < length) |
58 |
|
|
{ |
59 |
matty |
10 |
sent = write(sock, s->data + total, length - total); |
60 |
matty |
3 |
|
61 |
|
|
if (sent <= 0) |
62 |
|
|
{ |
63 |
matty |
10 |
STATUS("write: %s\n", strerror(errno)); |
64 |
|
|
return; |
65 |
matty |
3 |
} |
66 |
|
|
|
67 |
|
|
total += sent; |
68 |
|
|
} |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
/* Receive a message on the TCP layer */ |
72 |
matty |
25 |
STREAM |
73 |
|
|
tcp_recv(int length) |
74 |
matty |
3 |
{ |
75 |
matty |
9 |
int ret, rcvd = 0; |
76 |
|
|
struct timeval tv; |
77 |
|
|
fd_set rfds; |
78 |
matty |
3 |
|
79 |
matty |
10 |
if (length > in.size) |
80 |
|
|
{ |
81 |
|
|
in.data = xrealloc(in.data, length); |
82 |
|
|
in.size = length; |
83 |
|
|
} |
84 |
matty |
3 |
|
85 |
matty |
10 |
in.end = in.p = in.data; |
86 |
|
|
|
87 |
matty |
3 |
while (length > 0) |
88 |
|
|
{ |
89 |
matty |
10 |
ui_process_events(); |
90 |
matty |
3 |
|
91 |
matty |
9 |
FD_ZERO(&rfds); |
92 |
matty |
10 |
FD_SET(sock, &rfds); |
93 |
matty |
9 |
tv.tv_sec = 0; |
94 |
|
|
tv.tv_usec = 100; |
95 |
|
|
|
96 |
matty |
24 |
ret = select(sock + 1, &rfds, NULL, NULL, &tv); |
97 |
matty |
9 |
|
98 |
|
|
if (ret) |
99 |
matty |
3 |
{ |
100 |
matty |
10 |
rcvd = read(sock, in.end, length); |
101 |
matty |
9 |
|
102 |
|
|
if (rcvd <= 0) |
103 |
|
|
{ |
104 |
matty |
10 |
STATUS("read: %s\n", strerror(errno)); |
105 |
|
|
return NULL; |
106 |
matty |
9 |
} |
107 |
|
|
|
108 |
matty |
10 |
in.end += rcvd; |
109 |
matty |
9 |
length -= rcvd; |
110 |
matty |
3 |
} |
111 |
|
|
} |
112 |
|
|
|
113 |
matty |
10 |
return ∈ |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
/* Establish a connection on the TCP layer */ |
117 |
matty |
25 |
BOOL |
118 |
|
|
tcp_connect(char *server) |
119 |
matty |
10 |
{ |
120 |
|
|
struct hostent *nslookup; |
121 |
|
|
struct sockaddr_in servaddr; |
122 |
|
|
int true = 1; |
123 |
|
|
|
124 |
|
|
if ((nslookup = gethostbyname(server)) != NULL) |
125 |
|
|
{ |
126 |
matty |
24 |
memcpy(&servaddr.sin_addr, nslookup->h_addr, |
127 |
|
|
sizeof(servaddr.sin_addr)); |
128 |
matty |
10 |
} |
129 |
matty |
12 |
else if (!(servaddr.sin_addr.s_addr = inet_addr(server))) |
130 |
matty |
10 |
{ |
131 |
|
|
STATUS("%s: unable to resolve host\n", server); |
132 |
|
|
return False; |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) |
136 |
|
|
{ |
137 |
|
|
STATUS("socket: %s\n", strerror(errno)); |
138 |
|
|
return False; |
139 |
|
|
} |
140 |
|
|
|
141 |
|
|
servaddr.sin_family = AF_INET; |
142 |
|
|
servaddr.sin_port = htons(TCP_PORT_RDP); |
143 |
|
|
|
144 |
matty |
24 |
if (connect |
145 |
|
|
(sock, (struct sockaddr *) &servaddr, |
146 |
|
|
sizeof(struct sockaddr)) < 0) |
147 |
matty |
10 |
{ |
148 |
|
|
STATUS("connect: %s\n", strerror(errno)); |
149 |
|
|
close(sock); |
150 |
|
|
return False; |
151 |
|
|
} |
152 |
|
|
|
153 |
matty |
24 |
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void *) &true, |
154 |
|
|
sizeof(true)); |
155 |
matty |
10 |
|
156 |
|
|
in.size = 4096; |
157 |
|
|
in.data = xmalloc(in.size); |
158 |
|
|
|
159 |
|
|
out.size = 4096; |
160 |
|
|
out.data = xmalloc(out.size); |
161 |
|
|
|
162 |
matty |
3 |
return True; |
163 |
|
|
} |
164 |
matty |
10 |
|
165 |
|
|
/* Disconnect on the TCP layer */ |
166 |
matty |
25 |
void |
167 |
|
|
tcp_disconnect() |
168 |
matty |
10 |
{ |
169 |
|
|
close(sock); |
170 |
|
|
} |