forked from qualisys/qualisys_cpp_sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Network.cpp
593 lines (514 loc) · 15.6 KB
/
Network.cpp
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
#define _CRT_SECURE_NO_WARNINGS
#define NOMINMAX
#include "Network.h"
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#ifdef _WIN32
#include <iphlpapi.h>
#include <Ws2tcpip.h>
#else
#include <arpa/inet.h> // for inet_addr
#include <netinet/in.h> // for sockaddr_in, ntohl, in_addr, etc
#include <sys/socket.h> // for getsockname, send, AF_INET, etc
#include <unistd.h> // for close, read, fork, etc
#include <sys/types.h> /* Solaris 2.5.1 fix: u_short, required by sys/socket.h */
#include <sys/socket.h> /* sockets */
#include <sys/time.h> /* timeval */
#include <sys/ioctl.h> /* ioctl */
#include <string.h> /* bzero, for FD_SET */
#include <strings.h> /* bzero, for FD_ZERO (AIX) */
#include <netinet/in.h> /* INADDR_*, in_addr, sockaddr_in, htonl etc. */
#include <netinet/tcp.h> // for TCP_NODELAY
#include <netdb.h> /* getservbyname */
#include <arpa/inet.h> /* inet_addr */
#include <errno.h> /* socket error codes */
#include <ifaddrs.h>
#define SOCKET_ERROR (-1)
#define TIMEVAL timeval
#define closesocket close
#define ioctlsocket ioctl
#define SOCKADDR sockaddr
//#define SD_RECEIVE SHUT_RD
#define SD_SEND SHUT_WR
//#define SD_BOTH SHUT_RDWR
#endif
CNetwork::CNetwork()
{
mSocket = INVALID_SOCKET;
mUDPSocket = INVALID_SOCKET;
mUDPBroadcastSocket = INVALID_SOCKET;
mLastError = 0;
mErrorStr[0] = 0;
InitWinsock();
}
CNetwork::~CNetwork()
{
if (Connected())
{
Disconnect();
}
#ifdef _WIN32
WSACleanup();
#endif
}
bool CNetwork::InitWinsock()
{
#ifdef _WIN32
WORD wVersionRequested = MAKEWORD(2,2);
WSADATA wsaData;
// Initialize WinSock and check version
if (WSAStartup(wVersionRequested, &wsaData) != 0)
{
SetErrorString();
return false;
}
if (wsaData.wVersion != wVersionRequested)
{
SetErrorString();
return false;
}
#endif
return true;
} // InitWinsock
bool CNetwork::Connect(const char* serverAddr, unsigned short nPort)
{
mLastError = 0;
mErrorStr[0] = 0;
// Connect to QTM RT server.
mSocket = socket(AF_INET, SOCK_STREAM, 0);
if (mSocket == -1)
{
strcpy(mErrorStr, "Socket could not be created.");
}
sockaddr_in sAddr;
// First check if the address is a dotted number "A.B.C.D"
if (inet_pton(AF_INET, serverAddr, &(sAddr.sin_addr)) == 0)
{
// If it wasn't a dotted number lookup the server name
struct addrinfo hints, *servinfo;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
if (getaddrinfo(serverAddr, nullptr, &hints, &servinfo) != 0)
{
strcpy(mErrorStr, "Error looking up host name.");
closesocket(mSocket);
return false;
}
if (servinfo == nullptr)
{
strcpy(mErrorStr, "Error looking up host name.");
closesocket(mSocket);
return false;
}
sAddr.sin_addr = ((sockaddr_in *)servinfo[0].ai_addr)->sin_addr;
}
sAddr.sin_port = htons(nPort);
sAddr.sin_family = AF_INET;
if (connect(mSocket, (sockaddr*)(&sAddr), sizeof(sAddr)) == SOCKET_ERROR)
{
strcpy(mErrorStr, "Connect failed.");
//SetErrorString();
closesocket(mSocket);
return false;
}
// Disable Nagle's algorithm
#ifdef _WIN32
char bNoDelay = 1;
#else
int bNoDelay = 1;
#endif
if (setsockopt(mSocket, IPPROTO_TCP, TCP_NODELAY, &bNoDelay, sizeof(bNoDelay)) != 0)
{
SetErrorString();
closesocket(mSocket);
return false;
}
return true;
} // Connect
void CNetwork::Disconnect()
{
// Try to shutdown gracefully
shutdown(mSocket, SD_SEND);
closesocket(mSocket);
closesocket(mUDPSocket);
closesocket(mUDPBroadcastSocket);
mSocket = INVALID_SOCKET;
mUDPSocket = INVALID_SOCKET;
mUDPBroadcastSocket = INVALID_SOCKET;
} // Disconnect
bool CNetwork::Connected() const
{
return mSocket != INVALID_SOCKET;
}
bool CNetwork::CreateUDPSocket(unsigned short &nUDPPort, bool bBroadcast)
{
if (nUDPPort == 0 || nUDPPort > 1023)
{
SOCKET tempSocket = INVALID_SOCKET;
// Create UDP socket for data streaming
sockaddr_in recvAddr;
recvAddr.sin_family = AF_INET;
recvAddr.sin_port = htons(nUDPPort);
recvAddr.sin_addr.s_addr = INADDR_ANY;
tempSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (tempSocket != INVALID_SOCKET)
{
u_long argp = 1;
// Make socket unblocking.
if (ioctlsocket(tempSocket, FIONBIO, &argp) == 0)
{
if (bind(tempSocket, (SOCKADDR *) &recvAddr, sizeof(recvAddr)) != -1)
{
nUDPPort = GetUdpServerPort(tempSocket);
if (bBroadcast)
{
#ifdef _WIN32
char broadcast = 1;
#else
int broadcast = 1;
#endif
if (setsockopt(tempSocket, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast)) == 0)
{
mUDPBroadcastSocket = tempSocket;
return true;
}
else
{
strcpy(mErrorStr, "Failed to set socket options for UDP server socket.");
}
}
else
{
mUDPSocket = tempSocket;
return true;
}
}
else
{
strcpy(mErrorStr, "Failed to bind UDP server socket.");
}
}
else
{
strcpy(mErrorStr, "Failed to make UDP server socket unblocking.");
}
}
else
{
strcpy(mErrorStr, "Failed to create UDP server socket.");
}
closesocket(tempSocket);
}
return false;
}
unsigned short CNetwork::GetUdpServerPort(SOCKET socket)
{
sockaddr_in recvAddr;
socklen_t addrLen = sizeof(recvAddr);
if (getsockname(socket, (struct sockaddr *)&recvAddr, &addrLen) == 0 &&
recvAddr.sin_family == AF_INET &&
addrLen == sizeof(recvAddr))
{
return ntohs(recvAddr.sin_port);
}
return 0;
}
unsigned short CNetwork::GetUdpServerPort()
{
return GetUdpServerPort(mUDPSocket);
}
unsigned short CNetwork::GetUdpBroadcastServerPort()
{
return GetUdpServerPort(mUDPBroadcastSocket);
}
// Receive a data packet. Data is stored in a local static buffer
CNetwork::Response CNetwork::Receive(SOCKET socket, SOCKET udpSocket, char* rtDataBuff, int dataBufSize, bool header, int timeoutMicroseconds, unsigned int *ipAddr)
{
int received = 0;
sockaddr_in source_addr;
socklen_t fromlen = sizeof(source_addr);
fd_set readFDs, exceptFDs;
FD_ZERO(&readFDs);
FD_ZERO(&exceptFDs);
if (socket != INVALID_SOCKET)
{
FD_SET(socket, &readFDs);
FD_SET(socket, &exceptFDs);
}
if (udpSocket != INVALID_SOCKET)
{
FD_SET(udpSocket, &readFDs);
FD_SET(udpSocket, &exceptFDs);
}
TIMEVAL* pTimeval;
TIMEVAL sTimeval;
if (timeoutMicroseconds < 0)
{
pTimeval = nullptr;
}
else
{
sTimeval.tv_sec = timeoutMicroseconds / 1000000;
sTimeval.tv_usec = timeoutMicroseconds % 1000000;
pTimeval = &sTimeval;
}
#ifdef _WIN32
const int nfds = 0;
#else
const int nfds = std::max(socket, udpSocket) + 1;
#endif
// Wait for activity on the TCP and UDP sockets.
int selectRes = select(nfds, &readFDs, nullptr, &exceptFDs, pTimeval);
if (selectRes == SOCKET_ERROR)
{
SetErrorString();
return Response(CNetwork::ResponseType::error, 0);
}
if (selectRes == 0)
{
return Response(CNetwork::ResponseType::timeout, 0);
}
if (FD_ISSET(socket, &exceptFDs))
{
// General socket error
FD_CLR(socket, &exceptFDs);
SetErrorString();
return Response(CNetwork::ResponseType::error, 0);
}
else if (FD_ISSET(socket, &readFDs))
{
received = recv(socket, rtDataBuff, header ? 8 : dataBufSize, 0);
FD_CLR(socket, &readFDs);
if (selectRes == SOCKET_ERROR)
{
SetErrorString();
return Response(CNetwork::ResponseType::error, 0);
}
if (received == 0)
{
return Response(CNetwork::ResponseType::disconnect, 0);
}
return Response(CNetwork::ResponseType::success, received);
}
else if (FD_ISSET(udpSocket, &exceptFDs))
{
// General socket error
FD_CLR(udpSocket, &exceptFDs);
SetErrorString();
return Response(CNetwork::ResponseType::error, 0);
}
else if (FD_ISSET(udpSocket, &readFDs))
{
received = recvfrom(udpSocket, rtDataBuff, dataBufSize, 0, (sockaddr*)&source_addr, &fromlen);
FD_CLR(udpSocket, &readFDs);
if (ipAddr)
{
*ipAddr = source_addr.sin_addr.s_addr;
}
if (selectRes == SOCKET_ERROR)
{
SetErrorString();
return Response(CNetwork::ResponseType::error, 0);
}
if (received == 0)
{
return Response(CNetwork::ResponseType::disconnect, 0);
}
return Response(CNetwork::ResponseType::success, received);
}
return Response(CNetwork::ResponseType::error, 0);
}
CNetwork::Response CNetwork::Receive(char* rtDataBuff, int dataBufSize, bool header, int timeoutMicroseconds, unsigned int *ipAddr)
{
return Receive(mSocket, mUDPSocket, rtDataBuff, dataBufSize, header, timeoutMicroseconds, ipAddr);
}
CNetwork::Response CNetwork::ReceiveUdpBroadcast(char* rtDataBuff, int dataBufSize, int timeoutMicroseconds, unsigned int *ipAddr)
{
return Receive(static_cast<SOCKET>(SOCKET_ERROR), mUDPBroadcastSocket, rtDataBuff, dataBufSize, false, timeoutMicroseconds, ipAddr);
}
bool CNetwork::Send(const char* sendBuf, int size)
{
int sent = 0;
int totalSent = 0;
while (totalSent < size)
{
sent = send(mSocket, sendBuf + totalSent, size - totalSent, 0);
if (sent == SOCKET_ERROR)
{
SetErrorString();
return false;
}
totalSent += sent;
}
return true;
}
bool CNetwork::SendUDPBroadcast(const char* sendBuf, int size, short port, unsigned int filterAddr /* = 0 */)
{
bool broadCastSent = false;
if (mUDPBroadcastSocket != INVALID_SOCKET)
{
#ifdef _WIN32
IP_ADAPTER_INFO* ifap = nullptr;
IP_ADAPTER_INFO* ifa = nullptr;
ULONG ulLen = 0;
DWORD erradapt;
// Find all network interfaces.
erradapt = ::GetAdaptersInfo(ifap, &ulLen);
if (erradapt == ERROR_BUFFER_OVERFLOW)
{
ifap = (IP_ADAPTER_INFO*)malloc(ulLen);
erradapt = ::GetAdaptersInfo(ifap, &ulLen);
}
if (erradapt == ERROR_SUCCESS)
{
sockaddr_in recvAddr;
recvAddr.sin_family = AF_INET;
recvAddr.sin_port = htons(port);
recvAddr.sin_addr.s_addr = 0xffffffff;
// Send broadcast on all Ethernet interfaces.
ifa = ifap;
while (ifa)
{
if (ifa->Type == MIB_IF_TYPE_ETHERNET)
{
unsigned int nIPaddr;
unsigned int nIPmask;
if (inet_pton(AF_INET, ifa->IpAddressList.IpAddress.String, &nIPaddr) == 0 ||
inet_pton(AF_INET, ifa->IpAddressList.IpMask.String, &nIPmask) == 0)
{
return false;
}
recvAddr.sin_addr.s_addr = nIPaddr | (~nIPmask);
if (recvAddr.sin_addr.s_addr != (filterAddr | (~nIPmask)))
{
if (sendto(mUDPBroadcastSocket, sendBuf, size, 0, (sockaddr*)&recvAddr, sizeof(recvAddr)) == size)
{
broadCastSent = true;
}
}
}
ifa = ifa->Next;
}
}
free(ifap);
#else
// Find all network interfaces.
struct ifaddrs* ifap = nullptr;
if (getifaddrs(&ifap) == 0)
{
sockaddr_in recvAddr;
recvAddr.sin_family = AF_INET;
recvAddr.sin_port = htons(port);
recvAddr.sin_addr.s_addr = 0xffffffff;
// Send broadcast on all Ethernet interfaces.
auto* ifa = ifap;
while (ifa)
{
if (ifa->ifa_addr->sa_family == AF_INET)
{
auto* sa = (struct sockaddr_in *) ifa->ifa_addr;
auto ipAddr = sa->sin_addr.s_addr;
auto* saMask = (struct sockaddr_in *) ifa->ifa_netmask;
auto ipMask = saMask->sin_addr.s_addr;
recvAddr.sin_addr.s_addr = ipAddr | (~ipMask);
if (recvAddr.sin_addr.s_addr != (filterAddr | (~ipMask)))
{
if (sendto(mUDPBroadcastSocket, sendBuf, size, 0, (sockaddr*)&recvAddr, sizeof(recvAddr)) == size)
{
broadCastSent = true;
}
}
}
ifa = ifa->ifa_next;
}
}
freeifaddrs(ifap);
#endif
}
return broadCastSent;
} // SendUDPBroadcast
void CNetwork::SetErrorString()
{
#ifdef _WIN32
char* error = nullptr;
mLastError = GetLastError();
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, nullptr, mLastError, 0, reinterpret_cast<LPTSTR>(&error), 0, nullptr);
sprintf(mErrorStr, "%s", error);
LocalFree(error);
#else
mLastError = errno;
char* error = strerror(mLastError);
if (error != nullptr)
{
sprintf(mErrorStr, "%s", error);
}
#endif
}
char* CNetwork::GetErrorString()
{
return mErrorStr;
}
int CNetwork::GetError() const
{
return mLastError;
}
bool CNetwork::IsLocalAddress(unsigned int nAddr) const
{
#ifdef _WIN32
IP_ADAPTER_INFO* pAdptInfo = nullptr;
IP_ADAPTER_INFO* pNextAd = nullptr;
DWORD erradapt;
ULONG ulLen = 0;
// Find all network interfaces.
erradapt = ::GetAdaptersInfo(pAdptInfo, &ulLen);
if (erradapt == ERROR_BUFFER_OVERFLOW)
{
pAdptInfo = (IP_ADAPTER_INFO*)malloc(ulLen);
erradapt = ::GetAdaptersInfo(pAdptInfo, &ulLen);
}
if (erradapt == ERROR_SUCCESS)
{
pNextAd = pAdptInfo;
while(pNextAd)
{
if (pNextAd->Type == MIB_IF_TYPE_ETHERNET)
{
// Check if it's a response from a local interface.
unsigned int addr;
if (inet_pton(AF_INET, pNextAd->IpAddressList.IpAddress.String, &addr) != 0)
{
return addr == nAddr;
}
}
pNextAd = pNextAd->Next;
}
}
free(pAdptInfo);
#else
struct ifaddrs* pAdptInfo = nullptr;
struct ifaddrs* pNextAd = nullptr;
if (getifaddrs(&pAdptInfo) == 0)
{
pNextAd = pAdptInfo;
while (pNextAd)
{
if (pNextAd->ifa_addr->sa_family == AF_INET)
{
struct sockaddr_in* pNextAd_in = (struct sockaddr_in *)pNextAd->ifa_addr;
if (pNextAd_in->sin_addr.s_addr == nAddr)
{
return true;
}
}
pNextAd = pNextAd->ifa_next;
}
}
freeifaddrs(pAdptInfo);
#endif
return false;
}