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