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