2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS TCP/IP protocol driver
4 * FILE: tcpip/interface.c
5 * PURPOSE: Convenient abstraction for getting and setting information
7 * PROGRAMMERS: Art Yerkes
9 * CSH 01/08-2000 Created
14 NTSTATUS
GetInterfaceIPv4Address( PIP_INTERFACE Interface
,
17 switch( TargetType
) {
19 *Address
= Interface
->Unicast
.Address
.IPv4Address
;
23 *Address
= Interface
->Netmask
.Address
.IPv4Address
;
27 *Address
= Interface
->Broadcast
.Address
.IPv4Address
;
31 *Address
= Interface
->PointToPoint
.Address
.IPv4Address
;
35 return STATUS_UNSUCCESSFUL
;
38 return STATUS_SUCCESS
;
41 UINT
CountInterfaces() {
44 IF_LIST_ITER(CurrentIF
);
46 TcpipAcquireSpinLock(&InterfaceListLock
, &OldIrql
);
48 ForEachInterface(CurrentIF
) {
52 TcpipReleaseSpinLock(&InterfaceListLock
, OldIrql
);
57 NTSTATUS
GetInterfaceSpeed( PIP_INTERFACE Interface
, PUINT Speed
) {
58 PLAN_ADAPTER IF
= (PLAN_ADAPTER
)Interface
->Context
;
62 return STATUS_SUCCESS
;
65 NTSTATUS
GetInterfaceName( PIP_INTERFACE Interface
,
70 RtlUnicodeToMultiByteN( NameBuffer
,
73 Interface
->Name
.Buffer
,
74 Interface
->Name
.Length
);
76 if( NT_SUCCESS(Status
) )
77 NameBuffer
[ResultSize
] = 0;
85 * FUNCTION: Locates and returns an address entry using IPv4 adress as argument
87 * Address = Raw IPv4 address
89 * Pointer to address entry if found, NULL if not found
91 * If found, the address is referenced
93 BOOLEAN
AddrLocateADEv4(
94 IPv4_RAW_ADDRESS MatchAddress
, PIP_ADDRESS Address
)
97 BOOLEAN Matched
= FALSE
;
98 IF_LIST_ITER(CurrentIF
);
100 TcpipAcquireSpinLock(&InterfaceListLock
, &OldIrql
);
102 ForEachInterface(CurrentIF
) {
103 if( AddrIsEqualIPv4( &CurrentIF
->Unicast
, MatchAddress
) ||
104 AddrIsEqualIPv4( &CurrentIF
->Broadcast
, MatchAddress
) ) {
105 Address
->Type
= IP_ADDRESS_V4
;
106 Address
->Address
.IPv4Address
= MatchAddress
;
107 Matched
= TRUE
; break;
111 TcpipReleaseSpinLock(&InterfaceListLock
, OldIrql
);
121 * FUNCTION: Determines wether an address has an given prefix
123 * Address = Pointer to address to use
124 * Prefix = Pointer to prefix to check for
125 * Length = Length of prefix
127 * TRUE if the address has the prefix, FALSE if not
129 * The two addresses must be of the same type
132 PUCHAR pAddress
= (PUCHAR
)&Address
->Address
;
133 PUCHAR pPrefix
= (PUCHAR
)&Prefix
->Address
;
135 TI_DbgPrint(DEBUG_ROUTER
, ("Called. Address (0x%X) Prefix (0x%X) Length (%d).\n", Address
, Prefix
, Length
));
138 TI_DbgPrint(DEBUG_ROUTER
, ("Address (%s) Prefix (%s).\n",
139 A2S(Address
), A2S(Prefix
)));
142 /* Check that initial integral bytes match */
144 if (*pAddress
++ != *pPrefix
++)
149 /* Check any remaining bits */
150 if ((Length
> 0) && ((*pAddress
>> (8 - Length
)) != (*pPrefix
>> (8 - Length
))))
156 static PIP_INTERFACE
GetDefaultInterface(VOID
)
159 IF_LIST_ITER(CurrentIF
);
161 TcpipAcquireSpinLock(&InterfaceListLock
, &OldIrql
);
162 ForEachInterface(CurrentIF
) {
163 if (CurrentIF
->Context
) {
164 TcpipReleaseSpinLock(&InterfaceListLock
, OldIrql
);
168 TcpipReleaseSpinLock(&InterfaceListLock
, OldIrql
);
170 /* There are no physical interfaces on the system
171 * so we must pick the loopback interface */
176 PIP_INTERFACE
FindOnLinkInterface(PIP_ADDRESS Address
)
178 * FUNCTION: Checks all on-link prefixes to find out if an address is on-link
180 * Address = Pointer to address to check
182 * Pointer to interface if address is on-link, NULL if not
186 IF_LIST_ITER(CurrentIF
);
188 TI_DbgPrint(DEBUG_ROUTER
, ("Called. Address (0x%X)\n", Address
));
189 TI_DbgPrint(DEBUG_ROUTER
, ("Address (%s)\n", A2S(Address
)));
191 if (AddrIsUnspecified(Address
))
192 return GetDefaultInterface();
194 TcpipAcquireSpinLock(&InterfaceListLock
, &OldIrql
);
196 ForEachInterface(CurrentIF
) {
197 if (HasPrefix(Address
, &CurrentIF
->Unicast
,
198 AddrCountPrefixBits(&CurrentIF
->Netmask
))) {
199 TcpipReleaseSpinLock(&InterfaceListLock
, OldIrql
);
204 TcpipReleaseSpinLock(&InterfaceListLock
, OldIrql
);
209 NTSTATUS GetInterfaceConnectionStatus
210 ( PIP_INTERFACE Interface
, PULONG Result
) {
211 NTSTATUS Status
= TcpipLanGetDwordOid
212 ( Interface
, OID_GEN_HARDWARE_STATUS
, Result
);
213 if( NT_SUCCESS(Status
) ) switch( *Result
) {
214 case NdisHardwareStatusReady
:
215 *Result
= MIB_IF_OPER_STATUS_OPERATIONAL
;
217 case NdisHardwareStatusInitializing
:
218 *Result
= MIB_IF_OPER_STATUS_CONNECTING
;
220 case NdisHardwareStatusReset
:
221 *Result
= MIB_IF_OPER_STATUS_DISCONNECTED
;
223 case NdisHardwareStatusNotReady
:
224 *Result
= MIB_IF_OPER_STATUS_DISCONNECTED
;
226 case NdisHardwareStatusClosing
:
228 *Result
= MIB_IF_OPER_STATUS_NON_OPERATIONAL
;