-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsockets.cxx
352 lines (285 loc) · 7.48 KB
/
sockets.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
* Copyright (C) 2002 RealVNC Ltd.
* Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
*
* This 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 software 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 software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
/*
* sockets.cxx - functions to deal with sockets.
*/
#ifdef WIN32
#include <winsock2.h>
#define close(x) closesocket(x)
typedef int socklen_t;
#else
#include <unistd.h>
#include <sys/socket.h>
#include <errno.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fcntl.h>
#endif
extern "C" {
#include "vncsnapshot.h"
}
#include "rdr/FdInStream.h"
#include "rdr/FdOutStream.h"
#include "rdr/Exception.h"
int rfbsock;
rdr::FdInStream* fis;
rdr::FdOutStream* fos;
bool sameMachine = false;
/*static bool rfbsockReady = false;*/
/*
* InitializeSockets is called on startup. It will do any required one-time setup
* for the network.
*
* On *nix systems, it does nothing.
*
* On Windows, it initializes Windows Sockets.
*/
extern "C" bool
InitializeSockets(void)
{
#ifdef WIN32
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
fprintf(stderr, "Cannot initialize Windows Sockets\n");
return false;
}
/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
fprintf(stderr, "Cannot get proper version of Windows Sockets\n");
WSACleanup( );
return false;
}
/* The WinSock DLL is acceptable. Proceed. */
#endif
return true;
}
/*
* ConnectToRFBServer.
*/
bool ConnectToRFBServer(const char *hostname, uint16_t port)
{
int sock = ConnectToTcpAddr(hostname, port);
if (sock < 0) {
fprintf(stderr,"Unable to connect to VNC server\n");
return false;
}
return SetRFBSock(sock);
}
bool SetRFBSock(int sock)
{
try {
rfbsock = sock;
fis = new rdr::FdInStream(rfbsock);
fos = new rdr::FdOutStream(rfbsock);
struct sockaddr_in peeraddr, myaddr;
socklen_t addrlen = sizeof(struct sockaddr_in);
getpeername(sock, (struct sockaddr *)&peeraddr, &addrlen);
getsockname(sock, (struct sockaddr *)&myaddr, &addrlen);
sameMachine = (peeraddr.sin_addr.s_addr == myaddr.sin_addr.s_addr);
return true;
} catch (rdr::Exception& e) {
fprintf(stderr,"initialiseInStream: %s\n",e.str());
}
return false;
}
bool ReadFromRFBServer(uint8_t *out, size_t n)
{
try {
fis->readBytes(out, n);
return true;
} catch (rdr::Exception& e) {
fprintf(stderr,"ReadFromRFBServer: %s\n",e.str());
}
return false;
}
/*
* Write an exact number of bytes, and don't return until you've sent them.
*/
bool WriteToRFBServer(uint8_t *buf, size_t n)
{
try {
fos->writeBytes(buf, n);
fos->flush();
return true;
} catch (rdr::Exception& e) {
fprintf(stderr,"WriteExact: %s\n",e.str());
}
return false;
}
/*
* ConnectToTcpAddr connects to the given host and port.
*/
int ConnectToTcpAddr(const char* hostname, uint16_t port)
{
int sock;
struct sockaddr_in addr;
int one = 1;
unsigned int host;
if (!StringToIPAddr(hostname, &host)) {
fprintf(stderr,"Couldn't convert '%s' to host address\n", hostname);
return -1;
}
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = host;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
fprintf(stderr,programName);
perror(": ConnectToTcpAddr: socket");
return -1;
}
if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
fprintf(stderr,programName);
perror(": ConnectToTcpAddr: connect");
close(sock);
return -1;
}
if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
(char *)&one, sizeof(one)) < 0) {
fprintf(stderr,programName);
perror(": ConnectToTcpAddr: setsockopt");
close(sock);
return -1;
}
return sock;
}
/*
* FindFreeTcpPort tries to find unused TCP port in the range
* (TUNNEL_PORT_OFFSET, TUNNEL_PORT_OFFSET + 99]. Returns 0 on failure.
*/
uint16_t
FindFreeTcpPort(void)
{
int sock;
uint16_t port;
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
fprintf(stderr,programName);
perror(": FindFreeTcpPort: socket");
return 0;
}
for (port = TUNNEL_PORT_OFFSET + 99; port > TUNNEL_PORT_OFFSET; port--) {
addr.sin_port = htons((unsigned short)port);
if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0) {
close(sock);
return port;
}
}
close(sock);
return 0;
}
/*
* ListenAtTcpPort starts listening at the given TCP port.
*/
int ListenAtTcpPort(uint16_t port)
{
int sock;
struct sockaddr_in addr;
int one = 1;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = INADDR_ANY;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
fprintf(stderr,programName);
perror(": ListenAtTcpPort: socket");
return -1;
}
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
(const char *)&one, sizeof(one)) < 0) {
fprintf(stderr,programName);
perror(": ListenAtTcpPort: setsockopt");
close(sock);
return -1;
}
if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
fprintf(stderr,programName);
perror(": ListenAtTcpPort: bind");
close(sock);
return -1;
}
if (listen(sock, 5) < 0) {
fprintf(stderr,programName);
perror(": ListenAtTcpPort: listen");
close(sock);
return -1;
}
return sock;
}
/*
* AcceptTcpConnection accepts a TCP connection.
*/
int AcceptTcpConnection(int listenSock)
{
int sock;
struct sockaddr_in addr;
socklen_t addrlen = sizeof(addr);
int one = 1;
sock = accept(listenSock, (struct sockaddr *) &addr, &addrlen);
if (sock < 0) {
fprintf(stderr,programName);
perror(": AcceptTcpConnection: accept");
return -1;
}
if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
(char *)&one, sizeof(one)) < 0) {
fprintf(stderr,programName);
perror(": AcceptTcpConnection: setsockopt");
close(sock);
return -1;
}
return sock;
}
/*
* StringToIPAddr - convert a host string to an IP address.
*/
bool StringToIPAddr(const char *str, unsigned int *addr)
{
struct hostent *hp;
if (strcmp(str,"") == 0) {
*addr = 0; /* local */
return true;
}
*addr = inet_addr(str);
if (*addr != (unsigned int)-1)
return true;
hp = gethostbyname(str);
if (hp) {
*addr = *(unsigned int *)hp->h_addr;
return true;
}
return false;
}