-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathip_common.cpp
More file actions
65 lines (58 loc) · 1.27 KB
/
Copy pathip_common.cpp
File metadata and controls
65 lines (58 loc) · 1.27 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
#include "ip_common.h"
#if defined( __WIN32__ ) || defined( _WIN32 )
static const struct ap_init
{
bool is_init;
ap_init()
{
is_init = true;
static WSAData wsadata;
if (::WSAStartup(MAKEWORD(2, 2), &wsadata) != 0)
{
if (::WSAStartup(MAKEWORD(2,0), &wsadata) != 0)
is_init = false;
}
}
~ap_init()
{
::WSACleanup();
}
}os_ap;
#endif
quint16 IP::CheckSum(quint16* buffer, int size)
{
quint32 cksum = 0;
while(size > 1)
{
cksum += *buffer++;
size -= sizeof(quint16);
}
if(size)
cksum += *(quint8*)buffer;
cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >> 16);
return (quint16)(~cksum);
}
IP::Header IP::MakeHeaderIP()
{
Header head;
head.version = DefaultVersion;
head.headlen = DefaultIHL;
head.tos = 0;
head.length = 0;
head.id = 0;
head.foff = 0;
head.ttl = DefaultTTL;
head.protocol = DefaultProtocol;
head.checksum = 0;
head.addrs = 0;
head.addrd = 0;
return head;
}
quint16 IP::HashID (char* v, int len)
{
quint32 hash = 5381;
for (int i = 0; i < len; ++i)
hash = (hash + (hash << 5)) + v[i];
return quint16((hash >>16) | ((hash << 16) >> 16));
}