-
Notifications
You must be signed in to change notification settings - Fork 2
/
ipfuncs.pas
225 lines (172 loc) · 4.11 KB
/
ipfuncs.pas
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
{
IP functions
}
unit ipfuncs;
interface
uses
SysUtils, Messages, Windows, WinSock;
const
IPHeaderSize = 20;
TCPHeaderSize = 24;
ICMPHeaderSize = 8;
TotalTCPOverhead = TCPHeaderSize + IPHeaderSize;
ICMPOverhead = IPHeaderSize + ICMPHeaderSize;
RequiredWinSock = $101;
ReqDataSize = 32;
ICMP_ECHOREPLY = 0;
ICMP_ECHOREQ = 8;
CM_TCP = WM_USER + 1500;
CM_ICMP = WM_USER + 1501;
CM_HTTP = WM_USER + 1502;
CM_TRAY = WM_USER + 1600;
icmpSeq : word = 1;
IcmpRequestData : PChar = 'abcdefghijklmnopqrstuvwabcdefghi';
IPHelperAvailable : boolean = false;
MAXLEN_PHYSADDR = 8;
type
TIPHeader = packed record
VIHL : byte;
TOS : byte;
TotLen : word;
ID : word;
FlagOff : word;
TTL : byte;
Protocol : byte;
Checksum : word;
iaSrc : TInAddr;
iaDst : TInAddr;
end;
TICMPHeader = packed record
Type_ : byte;
Code : byte;
Checksum : word;
ID : word;
Seq : word;
// Data : char;
end;
TEchoRequest = packed record
icmpHdr : TICMPHeader;
dwTime : longword;
cData : array[0..ReqDataSize-1] of char;
end;
TEchoReply = packed record
ipHdr : TIPHeader;
echoRequest : TEchoRequest;
cFiller : array[0..255] of char;
end;
PIPOptionInformation = ^TIPOptionInformation;
TIPOptionInformation = packed record
TTL : byte;
TOS: byte;
Flags : byte;
OptionsSize : byte;
OptionsData : PByteArray;
end;
MIB_IPNETROW = packed record
dwIndex : DWORD;
dwPhysAddrLen : DWORD;
bPhysAddr : array[0..MAXLEN_PHYSADDR-1] of byte;
dwAddr : DWORD;
dwType : DWORD;
end;
PMIB_IPNETTABLE = ^MIB_IPNETTABLE;
MIB_IPNETTABLE = packed record
dwNumEntries : DWORD;
table : array[0..0] of MIB_IPNETROW;
end;
var
JunkBuffer : array[0..4096] of byte;
procedure InitWinsock;
function in_cksum(addr:PWordArray; len:integer):word;
procedure KillSocket(s:TSocket);
function InternetGetConnectedState(out dwFlags:DWORD; dwReserved:DWORD):boolean;stdcall;
function LookupName(name:string):Cardinal;
function IpToStr(ip:integer):string;
function GetIPMacAddress(ip:DWORD):string;
function GetIpNetTable(pIpNetTable:PMIB_IPNETTABLE;pdwSize:PULONG;bOrder:boolean):DWORD;stdcall;
implementation
function GetIPMacAddress;
var
n:integer;
buf:PMIB_IPNETTABLE;
P:^MIB_IPNETROW;
res,bufsize:DWORD;
begin
Result := '';
bufsize := 65536;
GetMem(buf,bufsize);
res := GetIpNetTable(buf,@bufsize,false);
if res = ERROR_INSUFFICIENT_BUFFER then begin
ReallocMem(buf,bufsize);
res := GetIpNetTable(buf,@bufsize,false);
end;
if res <> NO_ERROR then begin
Result := '';
FreeMem(buf);
exit;
end;
P := @buf.table;
for n:=1 to buf.dwNumEntries do begin
if P.dwAddr = ip then begin
for res := 1 to P.dwPhysAddrLen do begin
Result := Result + IntToHex(P.bPhysAddr[res],2);
if res < P.dwPhysAddrLen then Result := Result + '-';
end;
break;
end;
end;
FreeMem(buf);
end;
const
iphlpapi = 'iphlpapi.dll';
function GetIpNetTable(pIpNetTable:PMIB_IPNETTABLE;pdwSize:PULONG;bOrder:boolean):DWORD;external iphlpapi;
function IpToStr;
begin
Result := string(inet_ntoa(in_addr(ip)));
end;
function LookupName;
var
P:PHostEnt;
Pc:PAnsiChar;
begin
Pc := PAnsiChar(AnsiString(name));
Result := inet_addr(Pc);
if Result <> INADDR_NONE then exit;
P := gethostbyname(Pc);
if P <> NIL then Result := PInteger(P^.h_addr^)^;
end;
procedure KillSocket;
begin
closesocket(s);
end;
const
wininet = 'wininet.dll';
function InternetGetConnectedState(out dwFlags:DWORD; dwReserved:DWORD):boolean;
external wininet;
function in_cksum(addr:PWordArray; len:integer):word;
var
nleft:integer;
w:PWordArray;
sum:integer;
begin
w := addr;
sum := 0;
nleft := len;
while nleft > 1 do begin
inc(sum,PWord(w)^);
inc(integer(w),2);
dec(nleft,2);
end;
if nleft = 1 then inc(sum,PByte(w)^);
sum := (sum shr 16) + (sum and $ffff);
inc(sum,(sum shr 16));
Result := not sum;
end;
procedure InitWinsock;
var
wsa:TWSAData;
begin
if WSAStartup(RequiredWinSock,wsa) = SOCKET_ERROR then
RaiseLastOSError;
end;
end.