-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuSandkastenNet-impl.pas
75 lines (69 loc) · 2.47 KB
/
uSandkastenNet-impl.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
function GetHostByHelpers(const HostEnt : PHostEnt;
const AIPAddresses : TStrings = nil;
const AAlternateNames : TStrings = nil;
AReturnIP : boolean = true): string;
type PPInAddr= ^PInAddr;
var addr : PPInAddr;
pTmp : Pointer;
begin
Result := '';
if hostent <> nil then
begin
// take care for the IP addresses. Return the first one as function result
// and optionally other IP addresses, if a TStrings object was handed over
Addr := Pointer(hostent^.h_addr_list);
if AReturnIP and (addr <> nil) and (addr^ <> nil)
then Result := StrPas(inet_ntoa(addr^^));
if Assigned(AIPAddresses) then
while (addr <> nil) and (addr^ <> nil) do
begin
AIPAddresses.Add(StrPas(inet_ntoa(addr^^))) ;
inc(Addr);
end;
// Return the host name as result and optionally fill the TStrings object
// with alternate names
if not AReturnIP and (hostent^.h_name <> nil)
then Result := StrPas(PChar(hostent^.h_name));
if Assigned(AAlternateNames) and (hostent^.h_aliases <> nil) then
begin
pTmp := hostent^.h_aliases;
while (PChar(pTmp^) <> nil) do
begin
AAlternateNames.Add(StrPas(PChar(pTmp^)));
Inc(PChar(pTmp), SizeOf(PChar));
end;
end
end
end;
function GetHostByAddr(AAddr : string;
const AIPAddresses : TStrings = nil;
const AAlternateNames : TStrings = nil): string;
var WSAData : TWSAData;
hostent : PHostEnt;
pAddr : PChar;
lIP : longint;
begin
Result := '';
if WSAStartUp(MAKEWORD(2,0), WSAData) = 0 then
try
lIP := inet_addr( @AAddr[1] );
pAddr := PChar( @lIP );
hostent := Winsock.getHostByAddr(PChar(pAddr), 4, AF_INET);
Result := GetHostByHelpers(hostent, AIPAddresses, AAlternateNames, false);
finally
WSACleanup;
end;
end;
function GetHostByName(AHostname: string; const AIPAddresses: TStrings = nil; const AAlternateNames : TStrings = nil): string;
var WSAData : TWSAData;
hostent : PHostEnt;
begin
Result := '';
if WSAStartUp(MAKEWORD(2,0), WSAData) = 0 then
try
hostent := Winsock.getHostByName(PChar(AHostname));
Result := GetHostByHelpers(hostent, AIPAddresses, AAlternateNames);
finally
WSACleanup;
end;
end;