Subversion Repositories userdetect2

Rev

Rev 73 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 73 Rev 92
Line 14... Line 14...
14
function GetDomainName(var outDomainName: WideString): boolean;
14
function GetDomainName(var outDomainName: WideString): boolean;
15
 
15
 
16
implementation
16
implementation
17
 
17
 
18
uses
18
uses
19
  iphlp, WinSock, Registry;
19
  iphlpapi, IpTypes, Iprtrmib, WinSock, Registry;
20
 
20
 
21
// TODO: Replace GetAdaptersInfo()? A comment at MSDN states that there might be problems with IPv6
21
// TODO: Replace GetAdaptersInfo()? A comment at MSDN states that there might be problems with IPv6
22
//           "GetAdaptersInfo returns ERROR_NO_DATA if there are only IPv6 interfaces
22
//           "GetAdaptersInfo returns ERROR_NO_DATA if there are only IPv6 interfaces
23
//            configured on system. In that case GetAdapterAddresses has to be used!"
23
//            configured on system. In that case GetAdapterAddresses has to be used!"
24
 
24
 
25
function GetLocalIPAddressList(outsl: TStrings): DWORD;
25
function GetLocalIPAddressList(outsl: TStrings): DWORD;
26
var
26
var
27
  pAdapterInfo: PIP_ADAPTER_INFO;
27
  pAdapterInfo: PIP_ADAPTER_INFO;
28
  addr: string;
28
  addr: AnsiString;
29
  addrStr: IP_ADDR_STRING;
29
  addrStr: IP_ADDR_STRING;
30
  BufLen: Cardinal;
30
  BufLen: Cardinal;
31
begin
31
begin
32
  BufLen := SizeOf(IP_ADAPTER_INFO);
32
  BufLen := SizeOf(IP_ADAPTER_INFO);
33
  Result := GetAdaptersInfo(nil, @BufLen);
33
  Result := GetAdaptersInfo(nil, BufLen);
34
  if Result <> ERROR_BUFFER_OVERFLOW then Exit;
34
  if Result <> ERROR_BUFFER_OVERFLOW then Exit;
35
  pAdapterInfo := AllocMem(BufLen);
35
  pAdapterInfo := AllocMem(BufLen);
36
  try
36
  try
-
 
37
    ZeroMemory(pAdapterInfo, BufLen);
37
    Result := GetAdaptersInfo(pAdapterInfo, @BufLen);
38
    Result := GetAdaptersInfo(pAdapterInfo, BufLen);
38
    if Result <> ERROR_SUCCESS then Exit;
39
    if Result <> ERROR_SUCCESS then Exit;
39
    while pAdapterInfo <> nil do
40
    while pAdapterInfo <> nil do
40
    begin
41
    begin
41
      addrStr := pAdapterInfo^.IpAddressList;
42
      addrStr := pAdapterInfo^.IpAddressList;
42
      repeat
43
      repeat
Line 54... Line 55...
54
end;
55
end;
55
 
56
 
56
function GetDHCPIPAddressList(outsl: TStrings): DWORD;
57
function GetDHCPIPAddressList(outsl: TStrings): DWORD;
57
var
58
var
58
  pAdapterInfo: PIP_ADAPTER_INFO;
59
  pAdapterInfo: PIP_ADAPTER_INFO;
59
  addr: string;
60
  addr: AnsiString;
60
  addrStr: IP_ADDR_STRING;
61
  addrStr: IP_ADDR_STRING;
61
  BufLen: Cardinal;
62
  BufLen: Cardinal;
62
begin
63
begin
63
  BufLen := SizeOf(IP_ADAPTER_INFO);
64
  BufLen := SizeOf(IP_ADAPTER_INFO);
64
  Result := GetAdaptersInfo(nil, @BufLen);
65
  Result := GetAdaptersInfo(nil, BufLen);
65
  if Result <> ERROR_BUFFER_OVERFLOW then Exit;
66
  if Result <> ERROR_BUFFER_OVERFLOW then Exit;
66
  pAdapterInfo := AllocMem(BufLen);
67
  pAdapterInfo := AllocMem(BufLen);
67
  try
68
  try
-
 
69
    ZeroMemory(pAdapterInfo, BufLen);
68
    Result := GetAdaptersInfo(pAdapterInfo, @BufLen);
70
    Result := GetAdaptersInfo(pAdapterInfo, BufLen);
69
    if Result <> ERROR_SUCCESS then Exit;
71
    if Result <> ERROR_SUCCESS then Exit;
70
    while pAdapterInfo <> nil do
72
    while pAdapterInfo <> nil do
71
    begin
73
    begin
72
      addrStr := pAdapterInfo^.DhcpServer;
74
      addrStr := pAdapterInfo^.DhcpServer;
73
      repeat
75
      repeat
Line 85... Line 87...
85
end;
87
end;
86
 
88
 
87
function GetGatewayIPAddressList(outsl: TStrings): DWORD;
89
function GetGatewayIPAddressList(outsl: TStrings): DWORD;
88
var
90
var
89
  pAdapterInfo: PIP_ADAPTER_INFO;
91
  pAdapterInfo: PIP_ADAPTER_INFO;
90
  addr: string;
92
  addr: AnsiString;
91
  addrStr: IP_ADDR_STRING;
93
  addrStr: IP_ADDR_STRING;
92
  BufLen: Cardinal;
94
  BufLen: Cardinal;
93
begin
95
begin
94
  BufLen := SizeOf(IP_ADAPTER_INFO);
96
  BufLen := SizeOf(IP_ADAPTER_INFO);
95
  Result := GetAdaptersInfo(nil, @BufLen);
97
  Result := GetAdaptersInfo(nil, BufLen);
96
  if Result <> ERROR_BUFFER_OVERFLOW then Exit;
98
  if Result <> ERROR_BUFFER_OVERFLOW then Exit;
97
  pAdapterInfo := AllocMem(BufLen);
99
  pAdapterInfo := AllocMem(BufLen);
98
  try
100
  try
-
 
101
    ZeroMemory(pAdapterInfo, BufLen);
99
    Result := GetAdaptersInfo(pAdapterInfo, @BufLen);
102
    Result := GetAdaptersInfo(pAdapterInfo, BufLen);
100
    if Result <> ERROR_SUCCESS then Exit;
103
    if Result <> ERROR_SUCCESS then Exit;
101
    while pAdapterInfo <> nil do
104
    while pAdapterInfo <> nil do
102
    begin
105
    begin
103
      addrStr := pAdapterInfo^.GatewayList;
106
      addrStr := pAdapterInfo^.GatewayList;
104
      repeat
107
      repeat
Line 128... Line 131...
128
  WSAStartup($0101, WSAData);
131
  WSAStartup($0101, WSAData);
129
  try
132
  try
130
    ZeroMemory(@MacAddr, SizeOf(MacAddr));
133
    ZeroMemory(@MacAddr, SizeOf(MacAddr));
131
    DestIP     := inet_addr(PAnsiChar(IPAddress));
134
    DestIP     := inet_addr(PAnsiChar(IPAddress));
132
    PhyAddrLen := SizeOf(MacAddr); // TODO: more ?
135
    PhyAddrLen := SizeOf(MacAddr); // TODO: more ?
133
    Result     := SendArp(DestIP, 0, @MacAddr, @PhyAddrLen);
136
    Result     := SendArp(DestIP, 0, @MacAddr, PhyAddrLen);
134
    if Result = S_OK then
137
    if Result = S_OK then
135
    begin
138
    begin
136
      outAddress := '';
139
      outAddress := '';
137
      for j := 0 to PhyAddrLen-1 do
140
      for j := 0 to PhyAddrLen-1 do
138
      begin
141
      begin
Line 144... Line 147...
144
    WSACleanup;
147
    WSACleanup;
145
  end;
148
  end;
146
end;
149
end;
147
 
150
 
148
function GetLocalMACAddressList(outSL: TStrings): DWORD;
151
function GetLocalMACAddressList(outSL: TStrings): DWORD;
149
const
-
 
150
  _MAX_ROWS_ = 100;
-
 
151
type
-
 
152
  _IfTable = Record
-
 
153
    nRows: LongInt;
-
 
154
    ifRow: Array[1.._MAX_ROWS_] of MIB_IFROW;
-
 
155
  end;
-
 
156
var
152
var
157
  pIfTable: ^_IfTable;
153
  pIfTable: PMIB_IFTABLE;
158
  TableSize: LongInt;
154
  TableSize: Cardinal;
159
  tmp: String;
155
  tmp: String;
160
  i, j: Integer;
156
  i, j: Integer;
161
begin
157
begin
162
  pIfTable := nil;
158
  pIfTable := nil;
163
  try
159
  try
164
    // First: just get the buffer size.
160
    // First: just get the buffer size.
165
    // TableSize returns the size needed.
161
    // TableSize returns the size needed.
166
    TableSize := 0; // Set to zero so the GetIfTabel function
162
    TableSize := 0; // Set to zero so the GetIfTable function won't try to fill the buffer yet,
167
    // won't try to fill the buffer yet,
-
 
168
    // but only return the actual size it needs.
163
                    // but only return the actual size it needs.
169
    GetIfTable(pIfTable, TableSize, 1);
164
    GetIfTable(pIfTable, TableSize, true);
170
    if (TableSize < SizeOf(MIB_IFROW)+SizeOf(LongInt)) then
165
    if (TableSize < SizeOf(MIB_IFROW)+SizeOf(LongInt)) then
171
    begin
166
    begin
172
      Result := ERROR_NO_DATA;
167
      Result := ERROR_NO_DATA;
173
      Exit; // less than 1 table entry?!
168
      Exit; // less than 1 table entry?!
174
    end;
169
    end;
175
 
170
 
176
    // Second:
171
    // Second:
177
    // allocate memory for the buffer and retrieve the
172
    // allocate memory for the buffer and retrieve the
178
    // entire table.
173
    // entire table.
179
    GetMem(pIfTable, TableSize);
174
    GetMem(pIfTable, TableSize);
180
    Result := GetIfTable(pIfTable, TableSize, 1);
175
    Result := GetIfTable(pIfTable, TableSize, true);
181
    if Result <> NO_ERROR then Exit;
176
    if Result <> NO_ERROR then Exit;
182
 
177
 
183
    // Read the ETHERNET addresses.
178
    // Read the ETHERNET addresses.
184
    for i := 1 to pIfTable^.nRows do
179
    for i := 1 to pIfTable^.dwNumEntries do
185
    begin
180
    begin
186
      //if pIfTable^.ifRow[i].dwType=MIB_IF_TYPE_ETHERNET then
181
      //if pIfTable^.table[i].dwType = MIB_IF_TYPE_ETHERNET then
187
      begin
182
      begin
188
        tmp := '';
183
        tmp := '';
189
        for j := 0 to pIfTable^.ifRow[i].dwPhysAddrLen-1 do
184
        for j := 0 to pIfTable^.table[i].dwPhysAddrLen-1 do
190
        begin
185
        begin
191
          tmp := tmp + format('%.2x', [pIfTable^.ifRow[i].bPhysAddr[j]]);
186
          tmp := tmp + format('%.2x', [pIfTable^.table[i].bPhysAddr[j]]);
192
        end;
187
        end;
193
        tmp := FormatMAC(tmp);
188
        tmp := FormatMAC(tmp);
194
        if (tmp <> '') and (outSL.IndexOf(tmp) = -1) then
189
        if (tmp <> '') and (outSL.IndexOf(tmp) = -1) then
195
          outSL.Add(tmp);
190
          outSL.Add(tmp);
196
      end;
191
      end;