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 * Only unicast addresses are considered.
92 * If found, the address is referenced
94 BOOLEAN
AddrLocateADEv4(
95 IPv4_RAW_ADDRESS MatchAddress
, PIP_ADDRESS Address
)
98 BOOLEAN Matched
= FALSE
;
99 IF_LIST_ITER(CurrentIF
);
101 TcpipAcquireSpinLock(&InterfaceListLock
, &OldIrql
);
103 ForEachInterface(CurrentIF
) {
104 if( AddrIsEqualIPv4( &CurrentIF
->Unicast
, MatchAddress
) ) {
105 Address
->Address
.IPv4Address
= MatchAddress
;
106 Matched
= TRUE
; break;
110 TcpipReleaseSpinLock(&InterfaceListLock
, OldIrql
);
115 BOOLEAN
IPGetDefaultAddress( PIP_ADDRESS Address
) {
117 BOOLEAN Matched
= FALSE
;
118 IF_LIST_ITER(CurrentIF
);
120 TcpipAcquireSpinLock(&InterfaceListLock
, &OldIrql
);
122 /* Find the first 'real' interface */
123 ForEachInterface(CurrentIF
) {
124 if( CurrentIF
->Context
) {
125 *Address
= CurrentIF
->Unicast
;
126 Matched
= TRUE
; break;
130 /* Not matched, use the first one */
132 ForEachInterface(CurrentIF
) {
133 *Address
= CurrentIF
->Unicast
;
134 Matched
= TRUE
; break;
138 TcpipReleaseSpinLock(&InterfaceListLock
, OldIrql
);
148 * FUNCTION: Determines wether an address has an given prefix
150 * Address = Pointer to address to use
151 * Prefix = Pointer to prefix to check for
152 * Length = Length of prefix
154 * TRUE if the address has the prefix, FALSE if not
156 * The two addresses must be of the same type
159 PUCHAR pAddress
= (PUCHAR
)&Address
->Address
;
160 PUCHAR pPrefix
= (PUCHAR
)&Prefix
->Address
;
162 TI_DbgPrint(DEBUG_ROUTER
, ("Called. Address (0x%X) Prefix (0x%X) Length (%d).\n", Address
, Prefix
, Length
));
165 TI_DbgPrint(DEBUG_ROUTER
, ("Address (%s) Prefix (%s).\n",
166 A2S(Address
), A2S(Prefix
)));
169 /* Check that initial integral bytes match */
171 if (*pAddress
++ != *pPrefix
++)
176 /* Check any remaining bits */
177 if ((Length
> 0) && ((*pAddress
>> (8 - Length
)) != (*pPrefix
>> (8 - Length
))))
183 PIP_INTERFACE
FindOnLinkInterface(PIP_ADDRESS Address
)
185 * FUNCTION: Checks all on-link prefixes to find out if an address is on-link
187 * Address = Pointer to address to check
189 * Pointer to interface if address is on-link, NULL if not
193 IF_LIST_ITER(CurrentIF
);
195 TI_DbgPrint(DEBUG_ROUTER
, ("Called. Address (0x%X)\n", Address
));
196 TI_DbgPrint(DEBUG_ROUTER
, ("Address (%s)\n", A2S(Address
)));
198 TcpipAcquireSpinLock(&InterfaceListLock
, &OldIrql
);
200 ForEachInterface(CurrentIF
) {
201 if (HasPrefix(Address
, &CurrentIF
->Unicast
,
202 AddrCountPrefixBits(&CurrentIF
->Netmask
))) {
203 TcpipReleaseSpinLock(&InterfaceListLock
, OldIrql
);
208 TcpipReleaseSpinLock(&InterfaceListLock
, OldIrql
);
213 NTSTATUS GetInterfaceConnectionStatus
214 ( PIP_INTERFACE Interface
, PDWORD Result
) {
215 NTSTATUS Status
= TcpipLanGetDwordOid
216 ( Interface
, OID_GEN_HARDWARE_STATUS
, Result
);
217 if( NT_SUCCESS(Status
) ) switch( *Result
) {
218 case NdisHardwareStatusReady
:
219 *Result
= MIB_IF_OPER_STATUS_OPERATIONAL
;
221 case NdisHardwareStatusInitializing
:
222 *Result
= MIB_IF_OPER_STATUS_CONNECTING
;
224 case NdisHardwareStatusReset
:
225 *Result
= MIB_IF_OPER_STATUS_DISCONNECTED
;
227 case NdisHardwareStatusNotReady
:
228 *Result
= MIB_IF_OPER_STATUS_DISCONNECTED
;
230 case NdisHardwareStatusClosing
:
232 *Result
= MIB_IF_OPER_STATUS_NON_OPERATIONAL
;