Repository navigation
Expand file tree
/
Copy pathendpoint.cpp
More file actions
673 lines (547 loc) · 16 KB
/
Copy pathendpoint.cpp
File metadata and controls
673 lines (547 loc) · 16 KB
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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
/*
This source file is part of KBEngine
For the latest info, see http://www.kbengine.org/
Copyright (c) 2008-2016 KBEngine.
KBEngine is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
KBEngine 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with KBEngine. If not, see <http://www.gnu.org/licenses/>.
*/
#include "endpoint.h"
#ifndef CODE_INLINE
#include "endpoint.inl"
#endif
#include "network/bundle.h"
#include "network/tcp_packet_receiver.h"
#include "network/tcp_packet_sender.h"
#include "network/udp_packet_receiver.h"
#if KBE_PLATFORM == PLATFORM_WIN32
#include <Iphlpapi.h>
#pragma comment (lib,"iphlpapi.lib")
#else
#include <net/if.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#endif
namespace KBEngine {
namespace Network
{
#ifdef unix
#else // not unix
// Need to implement if_nameindex functions on Windows
/** @internal */
struct if_nameindex
{
unsigned int if_index; /* 1, 2, ... */
char *if_name; /* null terminated name: "eth0", ... */
};
/** @internal */
struct if_nameindex *if_nameindex(void)
{
static struct if_nameindex staticIfList[3] =
{ { 1, "eth0" }, { 2, "lo" }, { 0, 0 } };
return staticIfList;
}
/** @internal */
inline void if_freenameindex(struct if_nameindex *)
{}
#endif // not unix
static bool g_networkInitted = false;
//-------------------------------------------------------------------------------------
static ObjectPool<EndPoint> _g_objPool("EndPoint");
ObjectPool<EndPoint>& EndPoint::ObjPool()
{
return _g_objPool;
}
//-------------------------------------------------------------------------------------
EndPoint* EndPoint::createPoolObject()
{
return _g_objPool.createObject();
}
//-------------------------------------------------------------------------------------
void EndPoint::reclaimPoolObject(EndPoint* obj)
{
_g_objPool.reclaimObject(obj);
}
//-------------------------------------------------------------------------------------
void EndPoint::destroyObjPool()
{
DEBUG_MSG(fmt::format("EndPoint::destroyObjPool(): size {}.\n",
_g_objPool.size()));
_g_objPool.destroy();
}
//-------------------------------------------------------------------------------------
EndPoint::SmartPoolObjectPtr EndPoint::createSmartPoolObj()
{
return SmartPoolObjectPtr(new SmartPoolObject<EndPoint>(ObjPool().createObject(), _g_objPool));
}
//-------------------------------------------------------------------------------------
void EndPoint::onReclaimObject()
{
#if KBE_PLATFORM == PLATFORM_WIN32
socket_ = INVALID_SOCKET;
#else
socket_ = -1;
#endif
address_ = Address::NONE;
}
//-------------------------------------------------------------------------------------
bool EndPoint::getClosedPort(Network::Address & closedPort)
{
bool isResultSet = false;
#ifdef unix
// KBE_ASSERT(errno == ECONNREFUSED);
struct sockaddr_in offender;
offender.sin_family = 0;
offender.sin_port = 0;
offender.sin_addr.s_addr = 0;
struct msghdr errHeader;
struct iovec errPacket;
char data[ 256 ];
char control[ 256 ];
errHeader.msg_name = &offender;
errHeader.msg_namelen = sizeof(offender);
errHeader.msg_iov = &errPacket;
errHeader.msg_iovlen = 1;
errHeader.msg_control = control;
errHeader.msg_controllen = sizeof(control);
errHeader.msg_flags = 0; // result only
errPacket.iov_base = data;
errPacket.iov_len = sizeof(data);
int errMsgErr = recvmsg(*this, &errHeader, MSG_ERRQUEUE);
if (errMsgErr < 0)
{
return false;
}
struct cmsghdr * ctlHeader;
for (ctlHeader = CMSG_FIRSTHDR(&errHeader);
ctlHeader != NULL;
ctlHeader = CMSG_NXTHDR(&errHeader,ctlHeader))
{
if (ctlHeader->cmsg_level == SOL_IP &&
ctlHeader->cmsg_type == IP_RECVERR) break;
}
// Was there an IP_RECVERR error.
if (ctlHeader != NULL)
{
struct sock_extended_err * extError =
(struct sock_extended_err*)CMSG_DATA(ctlHeader);
// Only use this address if the kernel has the bug where it does not
// report the packet details.
if (errHeader.msg_namelen == 0)
{
// Finally we figure out whose fault it is except that this is the
// generator of the error (possibly a machine on the path to the
// destination), and we are interested in the actual destination.
offender = *(sockaddr_in*)SO_EE_OFFENDER(extError);
offender.sin_port = 0;
ERROR_MSG("EndPoint::getClosedPort: "
"Kernel has a bug: recv_msg did not set msg_name.\n");
}
closedPort.ip = offender.sin_addr.s_addr;
closedPort.port = offender.sin_port;
isResultSet = true;
}
#endif // unix
return isResultSet;
}
//-------------------------------------------------------------------------------------
int EndPoint::getBufferSize(int optname) const
{
KBE_ASSERT(optname == SO_SNDBUF || optname == SO_RCVBUF);
int recvbuf = -1;
socklen_t rbargsize = sizeof(int);
int rberr = getsockopt(socket_, SOL_SOCKET, optname,
(char*)&recvbuf, &rbargsize);
if (rberr == 0 && rbargsize == sizeof(int))
return recvbuf;
ERROR_MSG(fmt::format("EndPoint::getBufferSize: "
"Failed to read option {}: {}\n",
(optname == SO_SNDBUF ? "SO_SNDBUF" : "SO_RCVBUF"),
kbe_strerror()));
return -1;
}
//-------------------------------------------------------------------------------------
bool EndPoint::getInterfaces(std::map< u_int32_t, std::string > &interfaces)
{
#if KBE_PLATFORM == PLATFORM_WIN32
int count = 0;
char hostname[1024];
struct hostent* inaddrs;
if(gethostname(hostname, 1024) == 0)
{
inaddrs = gethostbyname(hostname);
if(inaddrs)
{
while(inaddrs->h_addr_list[count])
{
unsigned long addrs = *(unsigned long*)inaddrs->h_addr_list[count];
interfaces[addrs] = "eth0";
char *ip = inet_ntoa (*(struct in_addr *)inaddrs->h_addr_list[count]);
DEBUG_MSG(fmt::format("EndPoint::getInterfaces: found eth0 {}\n", ip));
++count;
}
}
}
return count > 0;
#else
struct ifconf ifc;
char buf[1024];
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = buf;
if(ioctl(socket_, SIOCGIFCONF, &ifc) < 0)
{
ERROR_MSG("EndPoint::getInterfaces: ioctl(SIOCGIFCONF) failed.\n");
return false;
}
struct ifreq * ifr = ifc.ifc_req;
int nInterfaces = ifc.ifc_len / sizeof(struct ifreq);
for (int i = 0; i < nInterfaces; ++i)
{
struct ifreq *item = &ifr[i];
interfaces[ ((struct sockaddr_in *)&item->ifr_addr)->sin_addr.s_addr ] =
item->ifr_name;
}
return true;
#endif
}
//-------------------------------------------------------------------------------------
int EndPoint::findIndicatedInterface(const char * spec, u_int32_t & address)
{
address = 0;
if(spec == NULL || spec[0] == 0)
{
return -1;
}
// ÊÇ·ñÖ¸¶¨µØÖ·
if(0 != Address::string2ip(spec, address))
{
return -1;
}
else if(0 != this->getInterfaceAddressByMAC(spec, address))
{
return -1;
}
else if(0 != this->getInterfaceAddressByName(spec, address))
{
return -1;
}
return 0;
}
//-------------------------------------------------------------------------------------
int EndPoint::getInterfaceAddressByName(const char * name, u_int32_t & address)
{
int ret = -1;
#if KBE_PLATFORM == PLATFORM_WIN32
PIP_ADAPTER_INFO pIpAdapterInfo = new IP_ADAPTER_INFO();
unsigned long size = sizeof(IP_ADAPTER_INFO);
int ret_info = ::GetAdaptersInfo(pIpAdapterInfo, &size);
if (ERROR_BUFFER_OVERFLOW == ret_info)
{
delete pIpAdapterInfo;
pIpAdapterInfo = (PIP_ADAPTER_INFO)new unsigned char[size];
ret_info = ::GetAdaptersInfo(pIpAdapterInfo, &size);
}
if (ERROR_SUCCESS == ret_info)
{
PIP_ADAPTER_INFO _pIpAdapterInfo = pIpAdapterInfo;
while (_pIpAdapterInfo)
{
if(!strcmp(_pIpAdapterInfo->AdapterName, name))
{
IP_ADDR_STRING* pIpAddrString = &(_pIpAdapterInfo->IpAddressList);
ret = Address::string2ip(pIpAddrString->IpAddress.String, address);
break;
}
_pIpAdapterInfo = _pIpAdapterInfo->Next;
}
}
if (pIpAdapterInfo)
{
delete pIpAdapterInfo;
}
#else
int fd;
int interfaceNum = 0;
struct ifreq buf[16];
struct ifconf ifc;
if((fd = ::socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
::close(fd);
return -1;
}
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = (caddr_t)buf;
if(!ioctl(fd, SIOCGIFCONF, (char *)&ifc))
{
interfaceNum = ifc.ifc_len / sizeof(struct ifreq);
while(interfaceNum-- > 0)
{
if(!strcmp((char*)buf[interfaceNum].ifr_name, (char*)name))
{
if(!ioctl(fd, SIOCGIFADDR, (char *)&buf[interfaceNum]))
{
ret = Address::string2ip((const char *)inet_ntoa(((struct sockaddr_in *)&(buf[interfaceNum].ifr_addr))->sin_addr), address);
}
break;
}
}
}
::close(fd);
#endif
return ret;
}
//-------------------------------------------------------------------------------------
int EndPoint::getInterfaceAddressByMAC(const char * mac, u_int32_t & address)
{
int ret = -1;
if(!mac)
{
return ret;
}
// macµØÖ·×ª»»
unsigned char macAddress[16] = {0};
unsigned char macAddressIdx = 0;
char szTemp[2] = {0};
char szTempIdx = 0;
char* pMac = (char*)mac;
while(*pMac && macAddressIdx < sizeof(macAddress))
{
if(('a' <= *pMac && *pMac <= 'f') || ('A' <= *pMac && *pMac <= 'F') || ('0' <= *pMac && *pMac <= '9'))
{
szTemp[szTempIdx++] = *pMac;
if(szTempIdx > 1)
{
macAddress[macAddressIdx++] = (unsigned char)::strtol(szTemp, NULL, 16);
szTempIdx = 0;
}
}
++pMac;
}
#if KBE_PLATFORM == PLATFORM_WIN32
PIP_ADAPTER_INFO pIpAdapterInfo = new IP_ADAPTER_INFO();
unsigned long size = sizeof(IP_ADAPTER_INFO);
int ret_info = ::GetAdaptersInfo(pIpAdapterInfo, &size);
if (ERROR_BUFFER_OVERFLOW == ret_info)
{
delete pIpAdapterInfo;
pIpAdapterInfo = (PIP_ADAPTER_INFO)new unsigned char[size];
ret_info = ::GetAdaptersInfo(pIpAdapterInfo, &size);
}
if (ERROR_SUCCESS == ret_info)
{
PIP_ADAPTER_INFO _pIpAdapterInfo = pIpAdapterInfo;
while (_pIpAdapterInfo)
{
if(!strcmp((char*)_pIpAdapterInfo->Address, (char*)macAddress))
{
IP_ADDR_STRING* pIpAddrString = &(_pIpAdapterInfo->IpAddressList);
ret = Address::string2ip(pIpAddrString->IpAddress.String, address);
break;
}
_pIpAdapterInfo = _pIpAdapterInfo->Next;
}
}
if (pIpAdapterInfo)
{
delete pIpAdapterInfo;
}
#else
int fd;
int interfaceNum = 0;
struct ifreq buf[16];
struct ifconf ifc;
if((fd = ::socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
::close(fd);
return -1;
}
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = (caddr_t)buf;
if(!ioctl(fd, SIOCGIFCONF, (char *)&ifc))
{
interfaceNum = ifc.ifc_len / sizeof(struct ifreq);
while(interfaceNum-- > 0)
{
if(!ioctl(fd, SIOCGIFHWADDR, (char *)(&buf[interfaceNum])))
{
if(!strcmp((char*)buf[interfaceNum].ifr_hwaddr.sa_data, (char*)macAddress))
{
if(!ioctl(fd, SIOCGIFADDR, (char *)&buf[interfaceNum]))
{
ret = Address::string2ip((const char *)inet_ntoa(((struct sockaddr_in *)&(buf[interfaceNum].ifr_addr))->sin_addr), address);
}
break;
}
}
else
{
break;
}
}
}
::close(fd);
#endif
return ret;
}
//-------------------------------------------------------------------------------------
int EndPoint::findDefaultInterface(char * name, int buffsize)
{
#ifndef unix
strcpy(name, "eth0");
return 0;
#else
int ret = -1;
struct if_nameindex* pIfInfo = if_nameindex();
if (pIfInfo)
{
int flags = 0;
struct if_nameindex* pIfInfoCur = pIfInfo;
while (pIfInfoCur->if_name)
{
flags = 0;
this->getInterfaceFlags(pIfInfoCur->if_name, flags);
if ((flags & IFF_UP) && (flags & IFF_RUNNING))
{
u_int32_t addr;
if (this->getInterfaceAddress(pIfInfoCur->if_name, addr) == 0)
{
strncpy(name, pIfInfoCur->if_name, MAX_BUF);
ret = 0;
// we only stop if it's not a loopback address,
// otherwise we continue, hoping to find a better one
if (!(flags & IFF_LOOPBACK)) break;
}
}
++pIfInfoCur;
}
if_freenameindex(pIfInfo);
}
else
{
ERROR_MSG(fmt::format("EndPoint::findDefaultInterface: "
"if_nameindex returned NULL ({})\n",
kbe_strerror()));
}
return ret;
#endif // unix
}
//-------------------------------------------------------------------------------------
int EndPoint::getDefaultInterfaceAddress(u_int32_t & address)
{
int ret = -1;
char interfaceName[MAX_BUF] = {0};
ret = findDefaultInterface(interfaceName, MAX_BUF);
if(0 == ret)
{
ret = getInterfaceAddressByName(interfaceName, address);
}
if(0 != ret)
{
char hostname[256] = {0};
::gethostname(hostname, sizeof(hostname));
struct hostent * host = gethostbyname(hostname);
if(host)
{
if(host->h_addr_list[0] < host->h_name)
{
address = ((struct in_addr*)(host->h_addr_list[0]))->s_addr;
ret = 0;
}
}
}
return ret;
}
//-------------------------------------------------------------------------------------
bool EndPoint::setBufferSize(int optname, int size)
{
setsockopt(socket_, SOL_SOCKET, optname, (const char*)&size, sizeof(size));
return this->getBufferSize(optname) >= size;
}
//-------------------------------------------------------------------------------------
bool EndPoint::recvAll(void * gramData, int gramSize)
{
while (gramSize > 0)
{
int len = this->recv(gramData, gramSize);
if (len <= 0)
{
if (len == 0)
{
WARNING_MSG("EndPoint::recvAll: Connection lost\n");
}
else
{
WARNING_MSG(fmt::format("EndPoint::recvAll: Got error '{}'\n",
kbe_strerror()));
}
return false;
}
gramSize -= len;
gramData = ((char *)gramData) + len;
}
return true;
}
//-------------------------------------------------------------------------------------
Network::Address EndPoint::getLocalAddress() const
{
Network::Address addr(0, 0);
if (this->getlocaladdress((u_int16_t*)&addr.port,
(u_int32_t*)&addr.ip) == -1)
{
ERROR_MSG("EndPoint::getLocalAddress: Failed\n");
}
return addr;
}
//-------------------------------------------------------------------------------------
Network::Address EndPoint::getRemoteAddress() const
{
Network::Address addr(0, 0);
if (this->getremoteaddress((u_int16_t*)&addr.port,
(u_int32_t*)&addr.ip) == -1)
{
ERROR_MSG("EndPoint::getRemoteAddress: Failed\n");
}
return addr;
}
//-------------------------------------------------------------------------------------
void EndPoint::initNetwork()
{
if (g_networkInitted)
return;
g_networkInitted = true;
#if KBE_PLATFORM == PLATFORM_WIN32
WSAData wsdata;
WSAStartup(0x202, &wsdata);
#endif
}
//-------------------------------------------------------------------------------------
bool EndPoint::waitSend()
{
fd_set fds;
struct timeval tv = { 0, 10000 };
FD_ZERO( &fds );
FD_SET(socket_, &fds);
return select(socket_+1, NULL, &fds, NULL, &tv) > 0;
}
//-------------------------------------------------------------------------------------
void EndPoint::send(Bundle * pBundle)
{
//AUTO_SCOPED_PROFILE("sendBundle");
SEND_BUNDLE((*this), (*pBundle));
}
//-------------------------------------------------------------------------------------
void EndPoint::sendto(Bundle * pBundle, u_int16_t networkPort, u_int32_t networkAddr)
{
//AUTO_SCOPED_PROFILE("sendBundle");
SENDTO_BUNDLE((*this), networkAddr, networkPort, (*pBundle));
}
//-------------------------------------------------------------------------------------
}
}