1. Moved windows styles pertaining to the edit control to main.h
[reactos.git] / reactos / lib / iphlpapi / ifenum_reactos.c
1 /* Copyright (C) 2003 Art Yerkes
2 * A reimplementation of ifenum.c by Juan Lang
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Implementation notes
19 * - Our bretheren use IOCTL_TCP_QUERY_INFORMATION_EX to get information
20 * from tcpip.sys and IOCTL_TCP_SET_INFORMATION_EX to set info (such as
21 * the route table). These ioctls mirror WsControl exactly in usage.
22 * - This iphlpapi does not rely on any reactos-only features.
23 * - This implementation is meant to be largely correct. I am not, however,
24 * paying any attention to performance. It can be done faster, and
25 * someone should definately optimize this code when speed is more of a
26 * priority than it is now.
27 *
28 * Edited implementation notes from the original -- Basically edited to add
29 * information and prune things which are not accurate about this file.
30 * Interface index fun:
31 * - Windows may rely on an index being cleared in the topmost 8 bits in some
32 * APIs; see GetFriendlyIfIndex and the mention of "backward compatible"
33 * indexes. It isn't clear which APIs might fail with non-backward-compatible
34 * indexes, but I'll keep them bits clear just in case.
35 * FIXME:
36 * - We don't support IPv6 addresses here yet -- I moved the upper edge
37 * functions into iphlpv6.c (arty)
38 */
39 #include "iphlpapi_private.h"
40 #include "ifenum.h"
41 #include <assert.h>
42
43 #define NDEBUG
44 #include "debug.h"
45
46 /* Globals */
47 const PWCHAR TcpFileName = L"\\Device\\Tcp";
48
49 /* Functions */
50
51 /* I'm a bit skittish about maintaining this info in memory, as I'd rather
52 * not add any mutex or critical section blockers to these functions. I've
53 * encountered far too many windows functions that contribute to deadlock
54 * by not announcing themselves. */
55 void interfaceMapInit(void)
56 {
57 /* For now, nothing */
58 }
59
60 void interfaceMapFree(void)
61 {
62 /* Ditto. */
63 }
64
65 NTSTATUS openTcpFile(PHANDLE tcpFile) {
66 UNICODE_STRING fileName;
67 OBJECT_ATTRIBUTES objectAttributes;
68 IO_STATUS_BLOCK ioStatusBlock;
69 NTSTATUS status;
70
71 TRACE("called.\n");
72
73 /* Shamelessly ripped from CreateFileW */
74 RtlInitUnicodeString( &fileName, TcpFileName );
75
76 InitializeObjectAttributes( &objectAttributes,
77 &fileName,
78 OBJ_CASE_INSENSITIVE,
79 NULL,
80 NULL );
81
82 status = ZwCreateFile( tcpFile,
83 SYNCHRONIZE | GENERIC_EXECUTE |
84 GENERIC_READ | GENERIC_WRITE,
85 &objectAttributes,
86 &ioStatusBlock,
87 NULL,
88 FILE_ATTRIBUTE_NORMAL,
89 FILE_SHARE_READ | FILE_SHARE_WRITE,
90 FILE_OPEN_IF,
91 FILE_SYNCHRONOUS_IO_NONALERT,
92 0,
93 0 );
94
95 /* String does not need to be freed: it points to the constant
96 * string we provided */
97
98 TRACE("returning %08x\n", (int)status);
99
100 return status;
101 }
102
103 void closeTcpFile( HANDLE h ) {
104 TRACE("called.\n");
105 NtClose( h );
106 }
107
108 /* A generic thing-getting function which interacts in the right way with
109 * TDI. This may seem oblique, but I'm using it to reduce code and hopefully
110 * make this thing easier to debug.
111 *
112 * The things returned can be any of:
113 * TDIEntityID
114 * TDIObjectID
115 * IFEntry
116 * IPSNMPInfo
117 * IPAddrEntry
118 * IPInterfaceInfo
119 */
120 NTSTATUS tdiGetSetOfThings( HANDLE tcpFile,
121 DWORD toiClass,
122 DWORD toiType,
123 DWORD toiId,
124 DWORD teiEntity,
125 DWORD fixedPart,
126 DWORD entrySize,
127 PVOID *tdiEntitySet,
128 PDWORD numEntries ) {
129 TCP_REQUEST_QUERY_INFORMATION_EX req = TCP_REQUEST_QUERY_INFORMATION_INIT;
130 PVOID entitySet = 0;
131 NTSTATUS status = STATUS_SUCCESS;
132 DWORD allocationSizeForEntityArray = entrySize * MAX_TDI_ENTITIES,
133 arraySize = entrySize * MAX_TDI_ENTITIES;
134
135 TRACE("TdiGetSetOfThings(tcpFile %x,toiClass %x,toiType %x,toiId %x,"
136 "teiEntity %x,fixedPart %d,entrySize %d)\n",
137 (int)tcpFile,
138 (int)toiClass,
139 (int)toiType,
140 (int)toiId,
141 (int)teiEntity,
142 (int)fixedPart,
143 (int)entrySize );
144
145 req.ID.toi_class = toiClass;
146 req.ID.toi_type = toiType;
147 req.ID.toi_id = toiId;
148 req.ID.toi_entity.tei_entity = teiEntity;
149
150 /* There's a subtle problem here...
151 * If an interface is added at this exact instant, (as if by a PCMCIA
152 * card insertion), the array will still not have enough entries after
153 * have allocated it after the first DeviceIoControl call.
154 *
155 * We'll get around this by repeating until the number of interfaces
156 * stabilizes.
157 */
158 do {
159 assert( !entitySet ); /* We must not have an entity set allocated */
160 status = DeviceIoControl( tcpFile,
161 IOCTL_TCP_QUERY_INFORMATION_EX,
162 &req,
163 sizeof(req),
164 0,
165 0,
166 &allocationSizeForEntityArray,
167 NULL );
168
169 if(!status)
170 {
171 DPRINT("IOCTL Failed\n");
172 return STATUS_UNSUCCESSFUL;
173 }
174
175 arraySize = allocationSizeForEntityArray;
176 entitySet = HeapAlloc( GetProcessHeap(), 0, arraySize );
177
178 if( !entitySet ) {
179 status = STATUS_INSUFFICIENT_RESOURCES;
180 DPRINT("TdiGetSetOfThings() => %08x\n", (int)status);
181 return status;
182 }
183
184 status = DeviceIoControl( tcpFile,
185 IOCTL_TCP_QUERY_INFORMATION_EX,
186 &req,
187 sizeof(req),
188 entitySet,
189 arraySize,
190 &allocationSizeForEntityArray,
191 NULL );
192
193 /* This is why we have the loop -- we might have added an adapter */
194 if( arraySize == allocationSizeForEntityArray )
195 break;
196
197 HeapFree( GetProcessHeap(), 0, entitySet );
198 entitySet = 0;
199
200 if(!status)
201 {
202 DPRINT("IOCTL Failed\n");
203 return STATUS_UNSUCCESSFUL;
204 }
205
206 DPRINT("TdiGetSetOfThings(): Array changed size: %d -> %d.\n",
207 arraySize, allocationSizeForEntityArray );
208 } while( TRUE ); /* We break if the array we received was the size we
209 * expected. Therefore, we got here because it wasn't */
210
211 *numEntries = (arraySize - fixedPart) / entrySize;
212 *tdiEntitySet = entitySet;
213
214 DPRINT("TdiGetSetOfThings() => Success: %d things @ %08x\n",
215 (int)*numEntries, (int)entitySet);
216
217 return STATUS_SUCCESS;
218 }
219
220 VOID tdiFreeThingSet( PVOID things ) {
221 HeapFree( GetProcessHeap(), 0, things );
222 }
223
224 NTSTATUS tdiGetMibForIfEntity
225 ( HANDLE tcpFile, TDIEntityID *ent, IFEntrySafelySized *entry ) {
226 TCP_REQUEST_QUERY_INFORMATION_EX req = TCP_REQUEST_QUERY_INFORMATION_INIT;
227 NTSTATUS status = STATUS_SUCCESS;
228 DWORD returnSize;
229
230 DPRINT("TdiGetMibForIfEntity(tcpFile %x,entityId %x)\n",
231 (int)tcpFile, (int)ent->tei_instance);
232
233 req.ID.toi_class = INFO_CLASS_PROTOCOL;
234 req.ID.toi_type = INFO_TYPE_PROVIDER;
235 req.ID.toi_id = IF_MIB_STATS_ID;
236 req.ID.toi_entity = *ent;
237
238 status = DeviceIoControl( tcpFile,
239 IOCTL_TCP_QUERY_INFORMATION_EX,
240 &req,
241 sizeof(req),
242 entry,
243 sizeof(*entry),
244 &returnSize,
245 NULL );
246
247 if(!status)
248 {
249 DPRINT("IOCTL Failed\n");
250 return STATUS_UNSUCCESSFUL;
251 }
252
253 DPRINT("TdiGetMibForIfEntity() => {\n"
254 " if_index ....................... %x\n"
255 " if_type ........................ %x\n"
256 " if_mtu ......................... %d\n"
257 " if_speed ....................... %x\n"
258 " if_physaddrlen ................. %d\n",
259 entry->ent.if_index,
260 entry->ent.if_type,
261 entry->ent.if_mtu,
262 entry->ent.if_speed,
263 entry->ent.if_physaddrlen);
264 DPRINT(" if_physaddr .................... %02x:%02x:%02x:%02x:%02x:%02x\n",
265 " if_descr ....................... %s\n",
266 entry->ent.if_physaddr[0] & 0xff,
267 entry->ent.if_physaddr[1] & 0xff,
268 entry->ent.if_physaddr[2] & 0xff,
269 entry->ent.if_physaddr[3] & 0xff,
270 entry->ent.if_physaddr[4] & 0xff,
271 entry->ent.if_physaddr[5] & 0xff,
272 entry->ent.if_descr);
273 DPRINT("} status %08x\n",status);
274
275 return status;
276 }
277
278 NTSTATUS tdiGetEntityIDSet( HANDLE tcpFile,
279 TDIEntityID **entitySet,
280 PDWORD numEntities ) {
281 NTSTATUS status = tdiGetSetOfThings( tcpFile,
282 INFO_CLASS_GENERIC,
283 INFO_TYPE_PROVIDER,
284 ENTITY_LIST_ID,
285 GENERIC_ENTITY,
286 0,
287 sizeof(TDIEntityID),
288 (PVOID *)entitySet,
289 numEntities );
290 if( NT_SUCCESS(status) ) {
291 int i;
292
293 for( i = 0; i < *numEntities; i++ ) {
294 DPRINT("%-4d: %04x:%08x\n",
295 i,
296 (*entitySet)[i].tei_entity,
297 (*entitySet)[i].tei_instance );
298 }
299 }
300
301 return status;
302 }
303
304 static BOOL isInterface( TDIEntityID *if_maybe ) {
305 return
306 if_maybe->tei_entity == IF_ENTITY;
307 }
308
309 static BOOL isLoopback( HANDLE tcpFile, TDIEntityID *loop_maybe ) {
310 IFEntrySafelySized entryInfo;
311 NTSTATUS status;
312
313 status = tdiGetMibForIfEntity( tcpFile,
314 loop_maybe,
315 &entryInfo );
316
317 return NT_SUCCESS(status) && (!entryInfo.ent.if_type ||
318 entryInfo.ent.if_type == IFENT_SOFTWARE_LOOPBACK);
319 }
320
321 NTSTATUS tdiGetEntityType( HANDLE tcpFile, TDIEntityID *ent, PULONG type ) {
322 TCP_REQUEST_QUERY_INFORMATION_EX req = TCP_REQUEST_QUERY_INFORMATION_INIT;
323 NTSTATUS status = STATUS_SUCCESS;
324 DWORD returnSize;
325
326 DPRINT("TdiGetEntityType(tcpFile %x,entityId %x)\n",
327 (DWORD)tcpFile, ent->tei_instance);
328
329 req.ID.toi_class = INFO_CLASS_GENERIC;
330 req.ID.toi_type = INFO_TYPE_PROVIDER;
331 req.ID.toi_id = ENTITY_TYPE_ID;
332 req.ID.toi_entity.tei_entity = ent->tei_entity;
333 req.ID.toi_entity.tei_instance = ent->tei_instance;
334
335 status = DeviceIoControl( tcpFile,
336 IOCTL_TCP_QUERY_INFORMATION_EX,
337 &req,
338 sizeof(req),
339 type,
340 sizeof(*type),
341 &returnSize,
342 NULL );
343
344 DPRINT("TdiGetEntityType() => %08x %08x\n", *type, status);
345
346 return (status ? STATUS_SUCCESS : STATUS_UNSUCCESSFUL);
347 }
348
349 static NTSTATUS getInterfaceInfoSet( HANDLE tcpFile,
350 IFInfo **infoSet,
351 PDWORD numInterfaces ) {
352 DWORD numEntities;
353 TDIEntityID *entIDSet = 0;
354 NTSTATUS status = tdiGetEntityIDSet( tcpFile, &entIDSet, &numEntities );
355 IFInfo *infoSetInt = 0;
356 BOOL interfaceInfoComplete;
357 int curInterf = 0, i;
358
359 if( NT_SUCCESS(status) )
360 infoSetInt = HeapAlloc( GetProcessHeap(), 0,
361 sizeof(IFInfo) * numEntities );
362
363 if( infoSetInt ) {
364 for( i = 0; i < numEntities; i++ ) {
365 if( isInterface( &entIDSet[i] ) ) {
366 status = tdiGetMibForIfEntity
367 ( tcpFile,
368 &entIDSet[i],
369 &infoSetInt[curInterf].if_info );
370 DPRINT("tdiGetMibForIfEntity: %08x\n", status);
371 if( NT_SUCCESS(status) ) {
372 DWORD numAddrs;
373 IPAddrEntry *addrs;
374 TDIEntityID ip_ent;
375 int j;
376
377 interfaceInfoComplete = FALSE;
378 status = getNthIpEntity( tcpFile, 0, &ip_ent );
379 if( NT_SUCCESS(status) )
380 status = tdiGetIpAddrsForIpEntity
381 ( tcpFile, &ip_ent, &addrs, &numAddrs );
382 for( j = 0; j < numAddrs && NT_SUCCESS(status); j++ ) {
383 DPRINT("ADDR %d: index %d (target %d)\n", j, addrs[j].iae_index, infoSetInt[curInterf].if_info.ent.if_index);
384 if( addrs[j].iae_index ==
385 infoSetInt[curInterf].if_info.ent.if_index ) {
386 memcpy( &infoSetInt[curInterf].ip_addr,
387 &addrs[j],
388 sizeof( addrs[j] ) );
389 curInterf++;
390 break;
391 }
392 }
393 }
394 }
395 }
396
397 if (NT_SUCCESS(status)) {
398 *infoSet = infoSetInt;
399 *numInterfaces = curInterf;
400 } else {
401 HeapFree(GetProcessHeap(), 0, infoSetInt);
402 }
403
404 return status;
405 } else {
406 return STATUS_INSUFFICIENT_RESOURCES;
407 }
408 }
409
410 static DWORD getNumInterfacesInt(BOOL onlyNonLoopback)
411 {
412 DWORD numEntities, numInterfaces = 0;
413 TDIEntityID *entitySet;
414 HANDLE tcpFile;
415 NTSTATUS status;
416 int i;
417
418 status = openTcpFile( &tcpFile );
419
420 if( !NT_SUCCESS(status) ) {
421 DPRINT("getNumInterfaces: failed %08x\n", status );
422 return 0;
423 }
424
425 status = tdiGetEntityIDSet( tcpFile, &entitySet, &numEntities );
426
427 if( !NT_SUCCESS(status) ) {
428 DPRINT("getNumInterfaces: failed %08x\n", status );
429 return 0;
430 }
431
432 for( i = 0; i < numEntities; i++ ) {
433 if( isInterface( &entitySet[i] ) &&
434 (!onlyNonLoopback ||
435 (onlyNonLoopback && !isLoopback( tcpFile, &entitySet[i] ))) )
436 numInterfaces++;
437 }
438
439 DPRINT("getNumInterfaces: success: %d %d %08x\n",
440 onlyNonLoopback, numInterfaces, status );
441
442 closeTcpFile( tcpFile );
443
444 tdiFreeThingSet( entitySet );
445
446 return numInterfaces;
447 }
448
449 DWORD getNumInterfaces(void)
450 {
451 return getNumInterfacesInt( FALSE );
452 }
453
454 DWORD getNumNonLoopbackInterfaces(void)
455 {
456 return getNumInterfacesInt( TRUE );
457 }
458
459 DWORD getNthInterfaceEntity( HANDLE tcpFile, DWORD index, TDIEntityID *ent ) {
460 DWORD numEntities = 0;
461 DWORD numInterfaces = 0;
462 TDIEntityID *entitySet = 0;
463 NTSTATUS status = tdiGetEntityIDSet( tcpFile, &entitySet, &numEntities );
464 int i;
465
466 if( !NT_SUCCESS(status) )
467 return status;
468
469 for( i = 0; i < numEntities; i++ ) {
470 if( isInterface( &entitySet[i] ) ) {
471 if( numInterfaces == index ) break;
472 else numInterfaces++;
473 }
474 }
475
476 DPRINT("Index %d is entity #%d - %04x:%08x\n", index, i,
477 entitySet[i].tei_entity, entitySet[i].tei_instance );
478
479 if( numInterfaces == index && i < numEntities ) {
480 memcpy( ent, &entitySet[i], sizeof(*ent) );
481 tdiFreeThingSet( entitySet );
482 return STATUS_SUCCESS;
483 } else {
484 tdiFreeThingSet( entitySet );
485 return STATUS_UNSUCCESSFUL;
486 }
487 }
488
489 NTSTATUS getInterfaceInfoByIndex( HANDLE tcpFile, DWORD index, IFInfo *info ) {
490 IFInfo *ifInfo;
491 DWORD numInterfaces;
492 NTSTATUS status = getInterfaceInfoSet( tcpFile, &ifInfo, &numInterfaces );
493 int i;
494
495 if( NT_SUCCESS(status) )
496 for( i = 0; i < numInterfaces; i++ ) {
497 if( ifInfo[i].if_info.ent.if_index == index ) {
498 memcpy( info, &ifInfo[i], sizeof(*info) );
499 break;
500 }
501 }
502
503 if( NT_SUCCESS(status) )
504 return i < numInterfaces ? STATUS_SUCCESS : STATUS_UNSUCCESSFUL;
505 else
506 return status;
507 }
508
509 NTSTATUS getInterfaceInfoByName( HANDLE tcpFile, char *name, IFInfo *info ) {
510 IFInfo *ifInfo;
511 DWORD numInterfaces;
512 int i;
513 NTSTATUS status = getInterfaceInfoSet( tcpFile, &ifInfo, &numInterfaces );
514
515 if( NT_SUCCESS(status) )
516 for( i = 0; i < numInterfaces; i++ ) {
517 if( !strcmp(ifInfo[i].if_info.ent.if_descr, name) ) {
518 memcpy( info, &ifInfo[i], sizeof(*info) );
519 break;
520 }
521 }
522
523 if( NT_SUCCESS(status) )
524 return i < numInterfaces ? STATUS_SUCCESS : STATUS_UNSUCCESSFUL;
525 else
526 return status;
527 }
528
529 /* Note that the result of this operation must be freed later */
530
531 const char *getInterfaceNameByIndex(DWORD index)
532 {
533 IFInfo ifInfo;
534 HANDLE tcpFile;
535 char *interfaceName = 0, *adapter_name = 0;
536 NTSTATUS status = openTcpFile( &tcpFile );
537
538 if( NT_SUCCESS(status) ) {
539 status = getInterfaceInfoByIndex( tcpFile, index, &ifInfo );
540
541 if( NT_SUCCESS(status) ) {
542 adapter_name = ifInfo.if_info.ent.if_descr;
543
544 interfaceName = HeapAlloc( GetProcessHeap(), 0,
545 strlen(adapter_name) + 1 );
546 strcpy( interfaceName, adapter_name );
547
548 closeTcpFile( tcpFile );
549 }
550 }
551
552 return interfaceName;
553 }
554
555 void consumeInterfaceName(const char *name) {
556 HeapFree( GetProcessHeap(), 0, (char *)name );
557 }
558
559 DWORD getInterfaceIndexByName(const char *name, PDWORD index)
560 {
561 IFInfo ifInfo;
562 HANDLE tcpFile;
563 NTSTATUS status = openTcpFile( &tcpFile );
564
565 if( NT_SUCCESS(status) ) {
566 status = getInterfaceInfoByName( tcpFile, (char *)name, &ifInfo );
567
568 if( NT_SUCCESS(status) ) {
569 *index = ifInfo.if_info.ent.if_index;
570 closeTcpFile( tcpFile );
571 }
572 }
573
574 return status;
575 }
576
577 InterfaceIndexTable *getInterfaceIndexTableInt( BOOL nonLoopbackOnly ) {
578 DWORD numInterfaces, curInterface = 0;
579 int i;
580 IFInfo *ifInfo;
581 InterfaceIndexTable *ret = 0;
582 HANDLE tcpFile;
583 NTSTATUS status = openTcpFile( &tcpFile );
584
585 if( NT_SUCCESS(status) ) {
586 status = getInterfaceInfoSet( tcpFile, &ifInfo, &numInterfaces );
587
588 DPRINT("InterfaceInfoSet: %08x, %04x:%08x\n",
589 status,
590 ifInfo->entity_id.tei_entity,
591 ifInfo->entity_id.tei_instance);
592
593 if( NT_SUCCESS(status) ) {
594 ret = (InterfaceIndexTable *)
595 calloc(1,
596 sizeof(InterfaceIndexTable) +
597 (numInterfaces - 1) * sizeof(DWORD));
598
599 if (ret) {
600 ret->numAllocated = numInterfaces;
601 DPRINT("NumInterfaces = %d\n", numInterfaces);
602
603 for( i = 0; i < numInterfaces; i++ ) {
604 DPRINT("Examining interface %d\n", i);
605 if( !nonLoopbackOnly ||
606 !isLoopback( tcpFile, &ifInfo[i].entity_id ) ) {
607 DPRINT("Interface %d matches (%d)\n", i, curInterface);
608 ret->indexes[curInterface++] =
609 ifInfo[i].if_info.ent.if_index;
610 }
611 }
612
613 ret->numIndexes = curInterface;
614 }
615
616 tdiFreeThingSet( ifInfo );
617 }
618 closeTcpFile( tcpFile );
619 }
620
621 return ret;
622 }
623
624 InterfaceIndexTable *getInterfaceIndexTable(void) {
625 return getInterfaceIndexTableInt( FALSE );
626 }
627
628 InterfaceIndexTable *getNonLoopbackInterfaceIndexTable(void) {
629 return getInterfaceIndexTableInt( TRUE );
630 }
631
632 DWORD getInterfaceIPAddrByName(const char *name)
633 {
634 return INADDR_ANY;
635 }
636
637 NTSTATUS getIPAddrEntryForIf(HANDLE tcpFile,
638 char *name,
639 DWORD index,
640 IFInfo *ifInfo) {
641 NTSTATUS status =
642 name ?
643 getInterfaceInfoByName( tcpFile, name, ifInfo ) :
644 getInterfaceInfoByIndex( tcpFile, index, ifInfo );
645 return status;
646 }
647
648 DWORD getAddrByIndexOrName( char *name, DWORD index, IPHLPAddrType addrType ) {
649 IFInfo ifInfo;
650 HANDLE tcpFile = INVALID_HANDLE_VALUE;
651 NTSTATUS status = STATUS_SUCCESS;
652 DWORD addrOut = INADDR_ANY;
653
654 status = openTcpFile( &tcpFile );
655
656 if( NT_SUCCESS(status) ) {
657 status = getIPAddrEntryForIf( tcpFile, name, index, &ifInfo );
658 if( NT_SUCCESS(status) ) {
659 switch( addrType ) {
660 case IPAAddr: addrOut = ifInfo.ip_addr.iae_addr; break;
661 case IPABcast: addrOut = ifInfo.ip_addr.iae_bcastaddr; break;
662 case IPAMask: addrOut = ifInfo.ip_addr.iae_mask; break;
663 case IFMtu: addrOut = ifInfo.if_info.ent.if_mtu; break;
664 case IFStatus: addrOut = ifInfo.if_info.ent.if_operstatus; break;
665 }
666 }
667 closeTcpFile( &tcpFile );
668 }
669
670 return addrOut;
671 }
672
673 DWORD getInterfaceIPAddrByIndex(DWORD index) {
674 return getAddrByIndexOrName( 0, index, IPAAddr );
675 }
676
677 DWORD getInterfaceBCastAddrByName(const char *name) {
678 return getAddrByIndexOrName( (char *)name, 0, IPABcast );
679 }
680
681 DWORD getInterfaceBCastAddrByIndex(DWORD index) {
682 return getAddrByIndexOrName( 0, index, IPABcast );
683 }
684
685 DWORD getInterfaceMaskByName(const char *name) {
686 return getAddrByIndexOrName( (char *)name, 0, IPAMask );
687 }
688
689 DWORD getInterfaceMaskByIndex(DWORD index) {
690 return getAddrByIndexOrName( 0, index, IPAMask );
691 }
692
693 void getInterfacePhysicalFromInfo( IFInfo *info,
694 PDWORD len, PBYTE addr, PDWORD type ) {
695 *len = info->if_info.ent.if_physaddrlen;
696 memcpy( addr, info->if_info.ent.if_physaddr, *len );
697 *type = info->if_info.ent.if_type;
698 }
699
700 DWORD getInterfacePhysicalByName(const char *name, PDWORD len, PBYTE addr,
701 PDWORD type)
702 {
703 HANDLE tcpFile;
704 IFInfo info;
705 NTSTATUS status = openTcpFile( &tcpFile );
706
707 if( NT_SUCCESS(status) ) {
708 status = getInterfaceInfoByName( tcpFile, (char *)name, &info );
709 if( NT_SUCCESS(status) )
710 getInterfacePhysicalFromInfo( &info, len, addr, type );
711 closeTcpFile( tcpFile );
712 }
713
714 return status;
715 }
716
717 DWORD getInterfacePhysicalByIndex(DWORD index, PDWORD len, PBYTE addr,
718 PDWORD type)
719 {
720 HANDLE tcpFile;
721 IFInfo info;
722 NTSTATUS status = openTcpFile( &tcpFile );
723
724 if( NT_SUCCESS(status) ) {
725 status = getInterfaceInfoByIndex( tcpFile, index, &info );
726 if( NT_SUCCESS(status) )
727 getInterfacePhysicalFromInfo( &info, len, addr, type );
728 closeTcpFile( tcpFile );
729 }
730
731 return status;
732 }
733
734 DWORD getInterfaceMtuByName(const char *name, PDWORD mtu) {
735 *mtu = getAddrByIndexOrName( (char *)name, 0, IFMtu );
736 return STATUS_SUCCESS;
737 }
738
739 DWORD getInterfaceMtuByIndex(DWORD index, PDWORD mtu) {
740 *mtu = getAddrByIndexOrName( 0, index, IFMtu );
741 return STATUS_SUCCESS;
742 }
743
744 DWORD getInterfaceStatusByName(const char *name, PDWORD status) {
745 *status = getAddrByIndexOrName( (char *)name, 0, IFStatus );
746 return STATUS_SUCCESS;
747 }
748
749 DWORD getInterfaceStatusByIndex(DWORD index, PDWORD status)
750 {
751 *status = getAddrByIndexOrName( 0, index, IFStatus );
752 return STATUS_SUCCESS;
753 }
754
755 DWORD getInterfaceEntryByName(const char *name, PMIB_IFROW entry)
756 {
757 HANDLE tcpFile;
758 IFInfo info;
759 NTSTATUS status = openTcpFile( &tcpFile );
760
761 DPRINT("Called.\n");
762
763 if( NT_SUCCESS(status) ) {
764 status = getInterfaceInfoByName( tcpFile, (char *)name, &info );
765
766 if( NT_SUCCESS(status) ) {
767 memcpy( &entry->wszName[MAX_INTERFACE_NAME_LEN],
768 &info.if_info,
769 sizeof(info.if_info) );
770 }
771
772 DPRINT1("entry->bDescr = %s\n", entry->bDescr);
773
774 closeTcpFile( tcpFile );
775 }
776
777 return status;
778 }
779
780 DWORD getInterfaceEntryByIndex(DWORD index, PMIB_IFROW entry)
781 {
782 HANDLE tcpFile;
783 IFInfo info;
784 NTSTATUS status = openTcpFile( &tcpFile );
785
786 DPRINT("Called.\n");
787
788 if( NT_SUCCESS(status) ) {
789 status = getInterfaceInfoByIndex( tcpFile, index, &info );
790
791 if( NT_SUCCESS(status) ) {
792 memcpy( &entry->wszName[MAX_INTERFACE_NAME_LEN],
793 &info.if_info,
794 sizeof(info.if_info) );
795 }
796
797 closeTcpFile( tcpFile );
798 }
799
800 return status;
801 }
802
803 char *toIPAddressString(unsigned int addr, char string[16])
804 {
805 if (string) {
806 struct in_addr iAddr;
807
808 iAddr.s_addr = addr;
809 /* extra-anal, just to make auditors happy */
810 strncpy(string, inet_ntoa(iAddr), 16);
811 string[16] = '\0';
812 }
813 return string;
814 }
815
816 NTSTATUS addIPAddress( IPAddr Address, IPMask Mask, DWORD IfIndex,
817 PULONG NteContext, PULONG NteInstance )
818 {
819 HANDLE tcpFile;
820 NTSTATUS status = openTcpFile( &tcpFile );
821 IP_SET_DATA Data;
822 IO_STATUS_BLOCK Iosb;
823
824 DPRINT("Called.\n");
825
826 if( !NT_SUCCESS(status) ) return status;
827
828 Data.NteContext = IfIndex;
829 Data.NewAddress = Address;
830 Data.NewNetmask = Mask;
831
832 status = NtDeviceIoControlFile( tcpFile,
833 NULL,
834 NULL,
835 NULL,
836 &Iosb,
837 IOCTL_SET_IP_ADDRESS,
838 &Data,
839 sizeof(Data),
840 &Data,
841 sizeof(Data) );
842
843 closeTcpFile( tcpFile );
844
845 if( NT_SUCCESS(status) ) {
846 *NteContext = Iosb.Information;
847 *NteInstance = Data.NewAddress;
848 }
849
850 switch( status ) {
851 case STATUS_SUCCESS: return ERROR_SUCCESS;
852 case STATUS_DEVICE_DOES_NOT_EXIST: return ERROR_DEV_NOT_EXIST;
853 default: return status;
854 }
855 }
856
857 NTSTATUS deleteIpAddress( ULONG NteContext )
858 {
859 HANDLE tcpFile;
860 NTSTATUS status = openTcpFile( &tcpFile );
861 IO_STATUS_BLOCK Iosb;
862
863 DPRINT("Called.\n");
864
865 if( !NT_SUCCESS(status) ) return status;
866
867 status = NtDeviceIoControlFile( tcpFile,
868 NULL,
869 NULL,
870 NULL,
871 &Iosb,
872 IOCTL_DELETE_IP_ADDRESS,
873 &NteContext,
874 sizeof(USHORT),
875 NULL,
876 0 );
877
878 closeTcpFile( tcpFile );
879
880 if( NT_SUCCESS(status) ) return ERROR_SUCCESS;
881 else return ERROR_GEN_FAILURE;
882 }