1 | /* $Id: iphlpapi.cpp,v 1.15 2003-05-05 15:26:03 sandervl Exp $ */
|
---|
2 | /*
|
---|
3 | * IPHLPAPI library
|
---|
4 | *
|
---|
5 | */
|
---|
6 |
|
---|
7 |
|
---|
8 | /****************************************************************************
|
---|
9 | * includes
|
---|
10 | ****************************************************************************/
|
---|
11 |
|
---|
12 |
|
---|
13 | #include <stdio.h>
|
---|
14 | #include <odin.h>
|
---|
15 | #include <odinwrap.h>
|
---|
16 | #include <os2sel.h>
|
---|
17 |
|
---|
18 | #include <os2win.h>
|
---|
19 | #include <winversion.h>
|
---|
20 |
|
---|
21 | #include <string.h>
|
---|
22 | #include <iprtrmib.h>
|
---|
23 | #include <winnls.h>
|
---|
24 |
|
---|
25 | #define BSD_SELECT 1
|
---|
26 | #define OS2 1
|
---|
27 |
|
---|
28 | #include <types.h>
|
---|
29 | #include <sys\socket.h>
|
---|
30 | #include <sys\ioctl.h>
|
---|
31 | #include <net\route.h>
|
---|
32 | #include <net\if.h>
|
---|
33 | #include <net\if_arp.h>
|
---|
34 | #ifdef TCPV40HDRS
|
---|
35 | #include <netinet\in.h>
|
---|
36 | #include <arpa\NAMESER.H>
|
---|
37 | #endif
|
---|
38 | #include <resolv.h>
|
---|
39 | #include <unistd.h>
|
---|
40 |
|
---|
41 | #include "iphlwrap.h"
|
---|
42 |
|
---|
43 | /* from ipexport.h */
|
---|
44 | typedef ULONG IPAddr;
|
---|
45 | typedef ULONG IPMask;
|
---|
46 | typedef ULONG IP_STATUS;
|
---|
47 |
|
---|
48 | #pragma pack(1)
|
---|
49 |
|
---|
50 | typedef struct
|
---|
51 | {
|
---|
52 | unsigned long IPAddress;
|
---|
53 | unsigned short interfaceIndex;
|
---|
54 | unsigned long netmask;
|
---|
55 | unsigned long broadcastAddress;
|
---|
56 | }
|
---|
57 | AddrInfo;
|
---|
58 |
|
---|
59 | #pragma pack()
|
---|
60 |
|
---|
61 | //We don't want to use the OS2 version directly, but the one in wsock32
|
---|
62 | int WIN32API ODIN_gethostname (char * name, int namelen);
|
---|
63 |
|
---|
64 | ODINDEBUGCHANNEL(IPHLPAPI-IPHLPAPI)
|
---|
65 |
|
---|
66 |
|
---|
67 | /****************************************************************************
|
---|
68 | * module global variables
|
---|
69 | ****************************************************************************/
|
---|
70 |
|
---|
71 | static PIP_ADAPTER_INFO pipAdapter = NULL;
|
---|
72 | static PMIB_IFTABLE pmibTable = NULL;
|
---|
73 | static PMIB_IPADDRTABLE pmipaddrTable = NULL;
|
---|
74 |
|
---|
75 | //******************************************************************************
|
---|
76 | //******************************************************************************
|
---|
77 |
|
---|
78 | void stringIPAddress(char* dst,u_long data)
|
---|
79 | {
|
---|
80 | sprintf(dst, "%u.%u.%u.%u",
|
---|
81 | (char)data,
|
---|
82 | (char)(*(((char*)&data) + 1)),
|
---|
83 | (char)(*(((char*)&data) + 2)),
|
---|
84 | (char)(*(((char*)&data) + 3)));
|
---|
85 | }
|
---|
86 |
|
---|
87 | static void i_initializeAdapterInformation(void)
|
---|
88 | {
|
---|
89 | char iShortName[8];
|
---|
90 | PIP_ADAPTER_INFO oldAdapter = NULL, topAdapter = NULL;
|
---|
91 | int rc;
|
---|
92 | char *buffer = NULL, *buffer2 = NULL;
|
---|
93 | struct ifmib ifmibget;
|
---|
94 | int cAddresses;
|
---|
95 | AddrInfo *addrInfo;
|
---|
96 | struct rtentries *rtentries;
|
---|
97 |
|
---|
98 | // Init Subsystem and open a socket for ioctl() calls
|
---|
99 | sock_init();
|
---|
100 |
|
---|
101 | int clientSocket = socket(PF_INET, SOCK_STREAM, 0);
|
---|
102 | dprintf(("IPHLPAPI: Init: Opened socket %d\n", clientSocket));
|
---|
103 |
|
---|
104 | // Whole buf minimum size is 65536 and memsets are really needed in other case
|
---|
105 | // we will get garbage in adapter names.
|
---|
106 |
|
---|
107 | memset(&ifmibget,0, sizeof(struct ifmib));
|
---|
108 | buffer = (char*) malloc(65536);
|
---|
109 | memset(buffer, 0, 65536);
|
---|
110 | buffer2 = (char*) malloc(65536);
|
---|
111 | memset(buffer2, 0, 65536);
|
---|
112 |
|
---|
113 | rc = ioctl(clientSocket, SIOSTATIF, (char*)&ifmibget, sizeof(struct ifmib));
|
---|
114 | dprintf(("IPHLPAPI: ioctl(SIOSTATIF) returned: %d\n", rc));
|
---|
115 | if (rc == -1)
|
---|
116 | {
|
---|
117 | free(buffer2);
|
---|
118 | free(buffer);
|
---|
119 | soclose(clientSocket);
|
---|
120 | return;
|
---|
121 | }
|
---|
122 | dprintf(("IPHLPAPI: ioctl(SIOSTATIF) returned %d interfaces\n", ifmibget.ifNumber));
|
---|
123 |
|
---|
124 | rc = ioctl(clientSocket, SIOSTATAT, buffer, 65536);
|
---|
125 | dprintf(("IPHLPAPI: ioctl(SIOSTATAT) returned: %d\n", rc));
|
---|
126 | if (rc == -1)
|
---|
127 | {
|
---|
128 | free(buffer2);
|
---|
129 | free(buffer);
|
---|
130 | soclose(clientSocket);
|
---|
131 | return;
|
---|
132 | }
|
---|
133 | cAddresses = *(short int *) buffer;
|
---|
134 | addrInfo = (AddrInfo *) (buffer + sizeof(short int));
|
---|
135 | dprintf(("IPHLPAPI: ioctl(SIOSTATAT) returned %d addresses\n", cAddresses));
|
---|
136 |
|
---|
137 | rc = ioctl(clientSocket, SIOSTATRT, buffer2, 65536);
|
---|
138 | dprintf(("IPHLPAPI: ioctl(SIOSTATRT) returned: %d\n", rc));
|
---|
139 | if (rc == -1)
|
---|
140 | {
|
---|
141 | free(buffer2);
|
---|
142 | free(buffer);
|
---|
143 | soclose(clientSocket);
|
---|
144 | return;
|
---|
145 | }
|
---|
146 | rtentries = (struct rtentries *) buffer2;
|
---|
147 | dprintf(("IPHLPAPI: ioctl(SIOSTATRT) returned %d host and %d net routes\n",
|
---|
148 | rtentries->hostcount, rtentries->netcount));
|
---|
149 |
|
---|
150 | pmibTable = (PMIB_IFTABLE) malloc(ifmibget.ifNumber * sizeof(MIB_IFTABLE));
|
---|
151 | memset(pmibTable, 0, ifmibget.ifNumber * sizeof(MIB_IFTABLE));
|
---|
152 | pmibTable->dwNumEntries = ifmibget.ifNumber;
|
---|
153 |
|
---|
154 | pmipaddrTable = (PMIB_IPADDRTABLE) malloc(cAddresses * sizeof(MIB_IPADDRTABLE));
|
---|
155 | memset(pmipaddrTable, 0, cAddresses * sizeof(MIB_IPADDRTABLE));
|
---|
156 | pmipaddrTable->dwNumEntries = cAddresses;
|
---|
157 |
|
---|
158 | // loop over interfaces
|
---|
159 | int idx, i;
|
---|
160 | for (i = idx = 0; i < IFMIB_ENTRIES && idx < ifmibget.ifNumber; ++i)
|
---|
161 | {
|
---|
162 | // skip empty interface entries
|
---|
163 | if (ifmibget.iftable[i].ifType == 0)
|
---|
164 | continue;
|
---|
165 |
|
---|
166 | short ifIndex = ifmibget.iftable[i].ifIndex;
|
---|
167 | dprintf(("IPHLPAPI: interface index: %u\n", ifIndex));
|
---|
168 |
|
---|
169 | // Guess the symbolic interface name. I do not like this very much, but
|
---|
170 | // seems there is no other documented way
|
---|
171 |
|
---|
172 | if (ifIndex >= 0 && ifIndex < 9) // lanX
|
---|
173 | {
|
---|
174 | strcpy(iShortName,"lan");
|
---|
175 | iShortName[3] = ifIndex + 48;
|
---|
176 | iShortName[4] = 0;
|
---|
177 | }
|
---|
178 | else
|
---|
179 | if (strstr(ifmibget.iftable[i].ifDescr, "back")) // loopback
|
---|
180 | {
|
---|
181 | strcpy(iShortName,"lo");
|
---|
182 | }
|
---|
183 | else
|
---|
184 | if (strstr(ifmibget.iftable[i].ifDescr, "ace ppp")) // pppX
|
---|
185 | {
|
---|
186 | strcpy(iShortName, strstr(ifmibget.iftable[i].ifDescr, "ppp"));
|
---|
187 | }
|
---|
188 | else
|
---|
189 | if (strstr(ifmibget.iftable[i].ifDescr,"ace sl")) // slX
|
---|
190 | {
|
---|
191 | strcpy(iShortName,strstr(ifmibget.iftable[i].ifDescr, "sl"));
|
---|
192 | }
|
---|
193 | else
|
---|
194 | if (strstr(ifmibget.iftable[i].ifDescr,"ace dod")) // dodX
|
---|
195 | {
|
---|
196 | strcpy(iShortName,strstr(ifmibget.iftable[i].ifDescr, "dod"));
|
---|
197 | }
|
---|
198 | else // something else...
|
---|
199 | {
|
---|
200 | strcpy(iShortName,strstr(ifmibget.iftable[i].ifDescr,"unk"));
|
---|
201 | iShortName[3] = ifIndex + 48;
|
---|
202 | iShortName[4] = 0;
|
---|
203 | }
|
---|
204 |
|
---|
205 | dprintf(("IPHLPAPI: interface name: %s [%s]\n", iShortName,
|
---|
206 | ifmibget.iftable[i].ifDescr));
|
---|
207 |
|
---|
208 | // Allocate the adapter info entry
|
---|
209 | pipAdapter = (PIP_ADAPTER_INFO)malloc (sizeof (IP_ADAPTER_INFO));
|
---|
210 | memset(pipAdapter, 0, sizeof(IP_ADAPTER_INFO));
|
---|
211 | if (oldAdapter)
|
---|
212 | oldAdapter->Next = pipAdapter;
|
---|
213 |
|
---|
214 | // Fill the adapter info entry
|
---|
215 | pipAdapter->Next = NULL;
|
---|
216 | pipAdapter->ComboIndex = 1; // unused according to MSDN
|
---|
217 | strcpy(pipAdapter->AdapterName, ifmibget.iftable[i].ifDescr);
|
---|
218 | strcpy(pipAdapter->Description, ifmibget.iftable[i].ifDescr);
|
---|
219 |
|
---|
220 | pipAdapter->AddressLength = sizeof(ifmibget.iftable[i].ifPhysAddr);
|
---|
221 | memcpy(pipAdapter->Address,ifmibget.iftable[i].ifPhysAddr, sizeof(ifmibget.iftable[i].ifPhysAddr));
|
---|
222 | pipAdapter->Index = ifIndex;
|
---|
223 | pipAdapter->Type = ifmibget.iftable[i].ifType; // Careful with this (?)
|
---|
224 |
|
---|
225 | // what about OS/2 DHCP?
|
---|
226 | pipAdapter->DhcpEnabled = 0; // Also a question
|
---|
227 | memset((char*)&pipAdapter->DhcpServer, 0, sizeof(IP_ADDR_STRING));
|
---|
228 |
|
---|
229 | pipAdapter->HaveWins = 0;
|
---|
230 | memset((char*)&pipAdapter->PrimaryWinsServer, 0, sizeof(IP_ADDR_STRING));
|
---|
231 | memset((char*)&pipAdapter->SecondaryWinsServer, 0, sizeof(IP_ADDR_STRING));
|
---|
232 | pipAdapter->LeaseObtained = 0;
|
---|
233 | pipAdapter->LeaseExpires = 0;
|
---|
234 |
|
---|
235 | // Fill the interface table entry
|
---|
236 | MultiByteToWideChar(CP_ACP, 0, iShortName, strlen(iShortName),
|
---|
237 | pmibTable->table[idx].wszName,
|
---|
238 | MAX_INTERFACE_NAME_LEN);
|
---|
239 |
|
---|
240 | pmibTable->table[idx].dwIndex = ifIndex;
|
---|
241 | pmibTable->table[idx].dwType = ifmibget.iftable[i].ifType;
|
---|
242 | pmibTable->table[idx].dwMtu = ifmibget.iftable[i].ifMtu;
|
---|
243 | pmibTable->table[idx].dwSpeed = ifmibget.iftable[i].ifSpeed;
|
---|
244 |
|
---|
245 | pmibTable->table[idx].dwPhysAddrLen = sizeof(ifmibget.iftable[i].ifPhysAddr);
|
---|
246 | memcpy(pmibTable->table[idx].bPhysAddr, ifmibget.iftable[i].ifPhysAddr,
|
---|
247 | sizeof(ifmibget.iftable[i].ifPhysAddr));
|
---|
248 |
|
---|
249 | pmibTable->table[idx].dwAdminStatus =
|
---|
250 | (ifmibget.iftable[i].ifOperStatus == IFF_UP) ?
|
---|
251 | MIB_IF_ADMIN_STATUS_UP : MIB_IF_ADMIN_STATUS_DOWN;
|
---|
252 | pmibTable->table[idx].dwOperStatus =
|
---|
253 | (ifmibget.iftable[i].ifOperStatus == IFF_UP) ?
|
---|
254 | MIB_IF_OPER_STATUS_OPERATIONAL : MIB_IF_OPER_STATUS_NON_OPERATIONAL;
|
---|
255 |
|
---|
256 | pmibTable->table[idx].dwLastChange = ifmibget.iftable[i].ifLastChange;
|
---|
257 | pmibTable->table[idx].dwInOctets = ifmibget.iftable[i].ifInOctets;
|
---|
258 | pmibTable->table[idx].dwInUcastPkts = ifmibget.iftable[i].ifInUcastPkts;
|
---|
259 | pmibTable->table[idx].dwInNUcastPkts = ifmibget.iftable[i].ifInNUcastPkts;
|
---|
260 | pmibTable->table[idx].dwInDiscards = ifmibget.iftable[i].ifInDiscards;
|
---|
261 | pmibTable->table[idx].dwInErrors = ifmibget.iftable[i].ifInErrors;
|
---|
262 | pmibTable->table[idx].dwInUnknownProtos = ifmibget.iftable[i].ifInUnknownProtos;
|
---|
263 | pmibTable->table[idx].dwOutOctets = ifmibget.iftable[i].ifOutOctets;
|
---|
264 | pmibTable->table[idx].dwOutUcastPkts = ifmibget.iftable[i].ifOutUcastPkts;
|
---|
265 | pmibTable->table[idx].dwOutNUcastPkts = ifmibget.iftable[i].ifOutNUcastPkts;
|
---|
266 | pmibTable->table[idx].dwOutDiscards = ifmibget.iftable[i].ifOutDiscards;
|
---|
267 | pmibTable->table[idx].dwOutErrors = ifmibget.iftable[i].ifOutErrors;
|
---|
268 | pmibTable->table[idx].dwOutQLen = 0; // unused according to MSDN
|
---|
269 |
|
---|
270 | pmibTable->table[idx].dwDescrLen = strlen(ifmibget.iftable[i].ifDescr);
|
---|
271 | strncpy((char *)pmibTable->table[idx].bDescr, ifmibget.iftable[i].ifDescr,
|
---|
272 | sizeof(pmibTable->table[idx].bDescr));
|
---|
273 |
|
---|
274 | // fill pipAdapter->IpAddressList
|
---|
275 | int cIfAddresses = 0;
|
---|
276 | for (int j = 0; j < cAddresses; ++j)
|
---|
277 | {
|
---|
278 | if (addrInfo[j].interfaceIndex == ifIndex)
|
---|
279 | {
|
---|
280 | ++cIfAddresses;
|
---|
281 |
|
---|
282 | IP_ADDR_STRING *addr;
|
---|
283 | if (cIfAddresses == 1)
|
---|
284 | {
|
---|
285 | addr = &pipAdapter->IpAddressList;
|
---|
286 | }
|
---|
287 | else
|
---|
288 | {
|
---|
289 | addr->Next = (IP_ADDR_STRING*) malloc(sizeof(IP_ADDR_STRING));
|
---|
290 | addr = addr->Next;
|
---|
291 | }
|
---|
292 |
|
---|
293 | memset((char *) addr, 0, sizeof(IP_ADDR_STRING));
|
---|
294 | addr->Next = NULL;
|
---|
295 | stringIPAddress(addr->IpAddress.String, addrInfo[j].IPAddress);
|
---|
296 | // mask is in network byte order for some reason
|
---|
297 | stringIPAddress(addr->IpMask.String, ntohl(addrInfo[j].netmask));
|
---|
298 | addr->Context = 0;
|
---|
299 | }
|
---|
300 | }
|
---|
301 |
|
---|
302 | // fill pipAdapter->GatewayList
|
---|
303 | int cIfGateways = 0;
|
---|
304 | struct rtentry *rtentry = rtentries->rttable;
|
---|
305 | for (j = 0; j < rtentries->hostcount + rtentries->netcount; ++j)
|
---|
306 | {
|
---|
307 | // only take default gateways for this interface
|
---|
308 | if (strcmp(rtentry->rt_ifp->if_name, iShortName) == 0 &&
|
---|
309 | ((struct sockaddr_in *)(&rtentry->rt_dst))->sin_addr.s_addr == 0)
|
---|
310 | {
|
---|
311 | ++cIfGateways;
|
---|
312 |
|
---|
313 | IP_ADDR_STRING *addr;
|
---|
314 | if (cIfGateways == 1)
|
---|
315 | {
|
---|
316 | addr = &pipAdapter->GatewayList;
|
---|
317 | }
|
---|
318 | else
|
---|
319 | {
|
---|
320 | addr->Next = (IP_ADDR_STRING *) malloc(sizeof(IP_ADDR_STRING));
|
---|
321 | addr = addr->Next;
|
---|
322 | }
|
---|
323 |
|
---|
324 | memset((char *) addr, 0, sizeof(IP_ADDR_STRING));
|
---|
325 | addr->Next = NULL;
|
---|
326 | struct sockaddr_in * sin =
|
---|
327 | (struct sockaddr_in *)(&rtentry->rt_gateway);
|
---|
328 | strcpy((char *) &addr->IpAddress.String, inet_ntoa(sin->sin_addr));
|
---|
329 | strcpy((char *) &addr->IpMask.String, "255.255.255.255");
|
---|
330 | addr->Context = 0;
|
---|
331 | }
|
---|
332 |
|
---|
333 | // For some strange reason, the definition of struct rtentries
|
---|
334 | // is wrong. Each entry in the rtentries::rttable array is followed
|
---|
335 | // by the interface name. Compensate for that.
|
---|
336 | ++rtentry;
|
---|
337 | rtentry = (struct rtentry *)
|
---|
338 | (((char *) rtentry) + strlen(((char *) rtentry)) + 1);
|
---|
339 | }
|
---|
340 |
|
---|
341 |
|
---|
342 | // loop over
|
---|
343 | if (!topAdapter)
|
---|
344 | topAdapter = pipAdapter;
|
---|
345 | oldAdapter =
|
---|
346 | pipAdapter;
|
---|
347 |
|
---|
348 | ++idx;
|
---|
349 | }
|
---|
350 | pipAdapter = topAdapter;
|
---|
351 |
|
---|
352 | // loop over addressees to fill pmipaddrTable
|
---|
353 | for (i = 0; i < cAddresses; ++i)
|
---|
354 | {
|
---|
355 | // skip addresses referring to empty interface entries
|
---|
356 | if (addrInfo[i].interfaceIndex >= IFMIB_ENTRIES ||
|
---|
357 | ifmibget.iftable[addrInfo[i].interfaceIndex].ifType == 0)
|
---|
358 | continue;
|
---|
359 |
|
---|
360 | pmipaddrTable->table[i].dwAddr = addrInfo[i].IPAddress;
|
---|
361 | pmipaddrTable->table[i].dwIndex = addrInfo[i].interfaceIndex;
|
---|
362 | // mask is in network byte order for some reason
|
---|
363 | pmipaddrTable->table[i].dwMask = ntohl(addrInfo[i].netmask);
|
---|
364 | pmipaddrTable->table[i].dwBCastAddr = addrInfo[i].broadcastAddress;
|
---|
365 | pmipaddrTable->table[i].dwReasmSize = 0; // ?
|
---|
366 | }
|
---|
367 |
|
---|
368 | // current address is the first address so far
|
---|
369 | pipAdapter->CurrentIpAddress = &pipAdapter->IpAddressList;
|
---|
370 |
|
---|
371 | // Cleanup
|
---|
372 | soclose(clientSocket);
|
---|
373 | free(buffer2);
|
---|
374 | free(buffer);
|
---|
375 | }
|
---|
376 |
|
---|
377 | // copy over the whole list and advance the target pointer and correct new list
|
---|
378 | static void i_copyIP_ADDRESS_STRING(PBYTE *ppTarget, PIP_ADDR_STRING pstruct,PIP_ADDR_STRING pias)
|
---|
379 | {
|
---|
380 | PIP_ADDR_STRING dummy = NULL;
|
---|
381 | // We already have this copied
|
---|
382 | pias = pias -> Next;
|
---|
383 | while (pias)
|
---|
384 | {
|
---|
385 | memcpy(*ppTarget, pias, sizeof( IP_ADDR_STRING ) );
|
---|
386 | pstruct->Next = (PIP_ADDR_STRING) *ppTarget;
|
---|
387 | *ppTarget += sizeof ( IP_ADDR_STRING );
|
---|
388 | pias = pias->Next;
|
---|
389 | pstruct = pstruct->Next;
|
---|
390 | }
|
---|
391 | }
|
---|
392 |
|
---|
393 | static DWORD i_sizeOfIP_ADAPTER_INFO(PIP_ADAPTER_INFO piai)
|
---|
394 | {
|
---|
395 | PIP_ADDR_STRING pias;
|
---|
396 |
|
---|
397 | // check for sufficient space
|
---|
398 | DWORD dwRequired = sizeof( IP_ADAPTER_INFO );
|
---|
399 |
|
---|
400 | // follow the IP_ADDR_STRING lists
|
---|
401 | pias = &piai->IpAddressList;
|
---|
402 | while( pias )
|
---|
403 | {
|
---|
404 | dwRequired += sizeof( IP_ADDR_STRING );
|
---|
405 | pias = pias->Next;
|
---|
406 | }
|
---|
407 |
|
---|
408 | pias = &piai->GatewayList;
|
---|
409 | while( pias )
|
---|
410 | {
|
---|
411 | dwRequired += sizeof( IP_ADDR_STRING );
|
---|
412 | pias = pias->Next;
|
---|
413 | }
|
---|
414 |
|
---|
415 | pias = &piai->DhcpServer;
|
---|
416 | while( pias )
|
---|
417 | {
|
---|
418 | dwRequired += sizeof( IP_ADDR_STRING );
|
---|
419 | pias = pias->Next;
|
---|
420 | }
|
---|
421 |
|
---|
422 | pias = &piai->PrimaryWinsServer;
|
---|
423 | while( pias )
|
---|
424 | {
|
---|
425 | dwRequired += sizeof( IP_ADDR_STRING );
|
---|
426 | pias = pias->Next;
|
---|
427 | }
|
---|
428 |
|
---|
429 | pias = &piai->SecondaryWinsServer;
|
---|
430 | while( pias )
|
---|
431 | {
|
---|
432 | dwRequired += sizeof( IP_ADDR_STRING );
|
---|
433 | pias = pias->Next;
|
---|
434 | }
|
---|
435 |
|
---|
436 | return dwRequired;
|
---|
437 | }
|
---|
438 |
|
---|
439 |
|
---|
440 | //******************************************************************************
|
---|
441 | //******************************************************************************
|
---|
442 |
|
---|
443 | // Note: returns error 50 under NT4 (NOT_SUPPORTED)
|
---|
444 | // so we better check out alternative ways, too.
|
---|
445 |
|
---|
446 | ODINFUNCTION2(DWORD, GetAdaptersInfo,
|
---|
447 | PIP_ADAPTER_INFO, pAdapterInfo,
|
---|
448 | PULONG, pOutBufLen)
|
---|
449 | {
|
---|
450 | dprintf(("GetAdaptersInfo API called"));
|
---|
451 | dprintf(("Address passed is %p",pAdapterInfo));
|
---|
452 | if (NULL == pOutBufLen)
|
---|
453 | return ERROR_INVALID_PARAMETER;
|
---|
454 |
|
---|
455 | // verify first block of memory to write to
|
---|
456 | if (IsBadWritePtr(pAdapterInfo, 4))
|
---|
457 | return ERROR_INVALID_PARAMETER;
|
---|
458 |
|
---|
459 | if (NULL == pipAdapter)
|
---|
460 | {
|
---|
461 | // gather the information and save it
|
---|
462 | i_initializeAdapterInformation();
|
---|
463 | }
|
---|
464 |
|
---|
465 | if (NULL == pipAdapter)
|
---|
466 | return ERROR_NO_DATA;
|
---|
467 |
|
---|
468 | // OK, just copy over the information as far as possible
|
---|
469 | LONG lSpaceLeft = *pOutBufLen;
|
---|
470 | PBYTE pTarget = (PBYTE)pAdapterInfo;
|
---|
471 | PIP_ADAPTER_INFO pip;
|
---|
472 |
|
---|
473 | // calculate overall required buffer size
|
---|
474 | DWORD dwRequiredBuffer = 0;
|
---|
475 |
|
---|
476 | for( pip = pipAdapter ; pip ; pip = pip->Next )
|
---|
477 | {
|
---|
478 | // check for sufficient space
|
---|
479 | dwRequiredBuffer += i_sizeOfIP_ADAPTER_INFO(pip);
|
---|
480 | }
|
---|
481 |
|
---|
482 | for( pip = pipAdapter ; pip ; pip = pip->Next )
|
---|
483 | {
|
---|
484 | // check for sufficient space
|
---|
485 | DWORD dwRequired = i_sizeOfIP_ADAPTER_INFO(pip);
|
---|
486 |
|
---|
487 | lSpaceLeft -= dwRequired;
|
---|
488 | if (lSpaceLeft >= 0)
|
---|
489 | {
|
---|
490 | // @PF revised - this thing works because we currently do not support
|
---|
491 | // multi-ip, multi-gateway or multi-DHCP servers lists
|
---|
492 | // TODO - add lists support
|
---|
493 | memcpy(pTarget, pip, sizeof( IP_ADAPTER_INFO ));
|
---|
494 | // point to currentIPAddress
|
---|
495 | ((PIP_ADAPTER_INFO)(pTarget))->CurrentIpAddress = &((PIP_ADAPTER_INFO)(pTarget))->IpAddressList;
|
---|
496 | pTarget += sizeof( IP_ADAPTER_INFO );
|
---|
497 |
|
---|
498 | // i_copyIP_ADDRESS_STRING(&pTarget, &pip->IpAddressList);
|
---|
499 | // i_copyIP_ADDRESS_STRING(&pTarget, &pip->GatewayList);
|
---|
500 | // i_copyIP_ADDRESS_STRING(&pTarget, &pip->DhcpServer);
|
---|
501 | // i_copyIP_ADDRESS_STRING(&pTarget, &pip->PrimaryWinsServer);
|
---|
502 | // i_copyIP_ADDRESS_STRING(&pTarget, &pip->SecondaryWinsServer);
|
---|
503 | }
|
---|
504 | else
|
---|
505 | {
|
---|
506 | // return overall size of required buffer
|
---|
507 | *pOutBufLen = dwRequiredBuffer;
|
---|
508 | return ERROR_BUFFER_OVERFLOW;
|
---|
509 | }
|
---|
510 | }
|
---|
511 | return ERROR_SUCCESS;
|
---|
512 | }
|
---|
513 |
|
---|
514 |
|
---|
515 | //******************************************************************************
|
---|
516 | //******************************************************************************
|
---|
517 | ODINFUNCTION2(DWORD, GetNetworkParams,
|
---|
518 | PFIXED_INFO, pFixedInfo,
|
---|
519 | PULONG, pOutBufLen)
|
---|
520 | {
|
---|
521 | struct sockaddr_in * sin;
|
---|
522 | PFIXED_INFO fi = pFixedInfo;
|
---|
523 | DWORD memNeeded;
|
---|
524 | PIP_ADDR_STRING dnslist = NULL, pdnslist = NULL;
|
---|
525 |
|
---|
526 | dprintf(("GetNetworkParams pFixedInfo:%x pOutBufLen:%d",pFixedInfo,*pOutBufLen));
|
---|
527 | res_init();
|
---|
528 |
|
---|
529 | // Check how much mem we will need
|
---|
530 | memNeeded = sizeof(FIXED_INFO)+_res.nscount*sizeof(IP_ADDR_STRING);
|
---|
531 |
|
---|
532 | if (((LONG)(*pOutBufLen - memNeeded)) < 0)
|
---|
533 | {
|
---|
534 | // return overall size of required buffer
|
---|
535 | *pOutBufLen = memNeeded;
|
---|
536 | return ERROR_BUFFER_OVERFLOW;
|
---|
537 | }
|
---|
538 |
|
---|
539 | // This is dynamically updated info
|
---|
540 | memset(pFixedInfo,0,memNeeded);
|
---|
541 |
|
---|
542 | ODIN_gethostname(fi->HostName,128);
|
---|
543 | strcpy(fi->DomainName,_res.defdname);
|
---|
544 |
|
---|
545 | // Fill in DNS Servers
|
---|
546 | fi->CurrentDnsServer = &fi->DnsServerList;
|
---|
547 | dnslist = &fi->DnsServerList;
|
---|
548 |
|
---|
549 | for (int i = 0; i<_res.nscount; i++)
|
---|
550 | {
|
---|
551 | if (pdnslist) pdnslist->Next = dnslist;
|
---|
552 | sin = (struct sockaddr_in *)&_res.nsaddr_list[i];
|
---|
553 | strcpy(dnslist->IpAddress.String,inet_ntoa(sin->sin_addr));
|
---|
554 | dprintf(("IPHLPAPI: GetNetworkParams Adding DNS Server %s",inet_ntoa(sin->sin_addr)));
|
---|
555 | pdnslist = dnslist;
|
---|
556 | if ( pdnslist == &fi->DnsServerList) dnslist = (PIP_ADDR_STRING)(fi + 1);
|
---|
557 | else dnslist += 1;
|
---|
558 | }
|
---|
559 | fi->EnableDns = 1;
|
---|
560 | return ERROR_SUCCESS;
|
---|
561 | }
|
---|
562 | //******************************************************************************
|
---|
563 | //******************************************************************************
|
---|
564 |
|
---|
565 | DWORD AddIPAddress(IPAddr Address, // IP address to add
|
---|
566 | IPMask IpMask, // subnet mask for IP address
|
---|
567 | DWORD IfIndex, // index of adapter
|
---|
568 | PULONG NTEContext, // Net Table Entry context
|
---|
569 | PULONG NTEInstance // Net Table Entry Instance
|
---|
570 | );
|
---|
571 | // SIOCAIFADDR
|
---|
572 |
|
---|
573 | DWORD DeleteIPAddress(
|
---|
574 | ULONG NTEContext // net table entry context
|
---|
575 | );
|
---|
576 | // SIOCDIFADDR
|
---|
577 |
|
---|
578 | //******************************************************************************
|
---|
579 | //******************************************************************************
|
---|
580 | DWORD WIN32API GetIpAddrTable(PMIB_IPADDRTABLE pIpAddrTable, PULONG pdwSize,
|
---|
581 | BOOL bOrder)
|
---|
582 | {
|
---|
583 | DWORD buflen;
|
---|
584 | DWORD rc;
|
---|
585 |
|
---|
586 | dprintf(("GetIpAddrTable %x %x %d", pIpAddrTable, pdwSize, bOrder));
|
---|
587 |
|
---|
588 | if(pdwSize == NULL) {
|
---|
589 | rc = ERROR_INVALID_PARAMETER;
|
---|
590 | goto end;
|
---|
591 | }
|
---|
592 |
|
---|
593 | if(pmipaddrTable == NULL)
|
---|
594 | {
|
---|
595 | // gather the information and save it
|
---|
596 | i_initializeAdapterInformation();
|
---|
597 | }
|
---|
598 | if(pmipaddrTable == NULL)
|
---|
599 | return ERROR_NO_DATA;
|
---|
600 |
|
---|
601 |
|
---|
602 | buflen = sizeof(MIB_IPADDRTABLE) - sizeof(MIB_IPADDRROW);
|
---|
603 | buflen+= pmipaddrTable->dwNumEntries*sizeof(MIB_IPADDRROW);
|
---|
604 |
|
---|
605 | if(buflen > *pdwSize) {
|
---|
606 | *pdwSize = buflen;
|
---|
607 | rc = ERROR_BUFFER_OVERFLOW;
|
---|
608 | goto end;
|
---|
609 | }
|
---|
610 | rc = ERROR_SUCCESS;
|
---|
611 |
|
---|
612 | memcpy(pIpAddrTable, pmipaddrTable, buflen);
|
---|
613 |
|
---|
614 | end:
|
---|
615 | dprintf(("GetIpAddrTable returned %d", rc));
|
---|
616 | return rc;
|
---|
617 | }
|
---|
618 | //******************************************************************************
|
---|
619 | //******************************************************************************
|
---|
620 | DWORD WIN32API GetIfTable(PMIB_IFTABLE pIfTable, PULONG pdwSize, BOOL bOrder)
|
---|
621 | {
|
---|
622 | DWORD buflen;
|
---|
623 | DWORD rc;
|
---|
624 |
|
---|
625 | dprintf(("GetIfTable %x %x %d", pIfTable, pdwSize, bOrder));
|
---|
626 |
|
---|
627 | if(pdwSize == NULL) {
|
---|
628 | rc = ERROR_INVALID_PARAMETER;
|
---|
629 | goto end;
|
---|
630 | }
|
---|
631 |
|
---|
632 | if(pmibTable == NULL)
|
---|
633 | {
|
---|
634 | // gather the information and save it
|
---|
635 | i_initializeAdapterInformation();
|
---|
636 | }
|
---|
637 | if(pmibTable == NULL)
|
---|
638 | return ERROR_NO_DATA;
|
---|
639 |
|
---|
640 |
|
---|
641 | buflen = sizeof(MIB_IFTABLE) - sizeof(MIB_IFROW);
|
---|
642 | buflen+= pmibTable->dwNumEntries*sizeof(MIB_IFROW);
|
---|
643 |
|
---|
644 | if(buflen > *pdwSize) {
|
---|
645 | *pdwSize = buflen;
|
---|
646 | rc = ERROR_BUFFER_OVERFLOW;
|
---|
647 | goto end;
|
---|
648 | }
|
---|
649 |
|
---|
650 | memcpy(pIfTable, pmibTable, buflen);
|
---|
651 |
|
---|
652 | rc = ERROR_SUCCESS;
|
---|
653 | end:
|
---|
654 | dprintf(("GetIfTable returned %d", rc));
|
---|
655 | return rc;
|
---|
656 | }
|
---|
657 | //******************************************************************************
|
---|
658 | //******************************************************************************
|
---|
659 | DWORD WIN32API GetFriendlyIfIndex(DWORD IfIndex)
|
---|
660 | {
|
---|
661 | dprintf(("GetFriendlyIfIndex %d; returns the same index", IfIndex));
|
---|
662 | return IfIndex;
|
---|
663 | }
|
---|
664 | //******************************************************************************
|
---|
665 | //******************************************************************************
|
---|