ReactOS  0.4.15-dev-2993-g14fbe80
interface.c File Reference
#include "precomp.h"
#include <ntifs.h>
#include <ipifcons.h>
Include dependency graph for interface.c:

Go to the source code of this file.

Functions

NTSTATUS GetInterfaceIPv4Address (PIP_INTERFACE Interface, ULONG TargetType, PULONG Address)
 
UINT CountInterfaces ()
 
NTSTATUS GetInterfaceSpeed (PIP_INTERFACE Interface, PUINT Speed)
 
NTSTATUS GetInterfaceName (PIP_INTERFACE Interface, PCHAR NameBuffer, UINT Len)
 
PIP_INTERFACE AddrLocateInterface (PIP_ADDRESS MatchAddress)
 
BOOLEAN HasPrefix (PIP_ADDRESS Address, PIP_ADDRESS Prefix, UINT Length)
 
PIP_INTERFACE GetDefaultInterface (VOID)
 
PIP_INTERFACE FindOnLinkInterface (PIP_ADDRESS Address)
 
VOID GetInterfaceConnectionStatus (PIP_INTERFACE Interface, PULONG Result)
 

Variables

ULONG NextDefaultAdapter = 0
 

Function Documentation

◆ AddrLocateInterface()

PIP_INTERFACE AddrLocateInterface ( PIP_ADDRESS  MatchAddress)

Definition at line 89 of file interface.c.

91 {
92  KIRQL OldIrql;
93  PIP_INTERFACE RetIF = NULL;
94  IF_LIST_ITER(CurrentIF);
95 
97 
98  ForEachInterface(CurrentIF) {
99  if( AddrIsEqual( &CurrentIF->Unicast, MatchAddress ) ||
100  AddrIsEqual( &CurrentIF->Broadcast, MatchAddress ) ) {
101  RetIF = CurrentIF;
102  break;
103  }
104  } EndFor(CurrentIF);
105 
107 
108  return RetIF;
109 }
KSPIN_LOCK InterfaceListLock
Definition: ip.c:18
#define IF_LIST_ITER(n)
Definition: tilists.h:5
VOID TcpipReleaseSpinLock(PKSPIN_LOCK SpinLock, KIRQL Irql)
Definition: lock.c:26
VOID TcpipAcquireSpinLock(PKSPIN_LOCK SpinLock, PKIRQL Irql)
Definition: lock.c:18
UCHAR KIRQL
Definition: env_spec_w32.h:591
#define ForEachInterface(n)
Definition: tilists.h:9
KIRQL OldIrql
Definition: mm.h:1502
BOOLEAN AddrIsEqual(PIP_ADDRESS Address1, PIP_ADDRESS Address2)
Definition: address.c:221
#define EndFor(n)
Definition: tilists.h:20
#define NULL
Definition: types.h:112

Referenced by DispTdiQueryIpHwAddress(), and FileOpenAddress().

◆ CountInterfaces()

UINT CountInterfaces ( )

Definition at line 46 of file interface.c.

46  {
47  ULONG Count = 0;
48  KIRQL OldIrql;
49  IF_LIST_ITER(CurrentIF);
50 
52 
53  ForEachInterface(CurrentIF) {
54  Count++;
55  } EndFor(CurrentIF);
56 
58 
59  return Count;
60 }
KSPIN_LOCK InterfaceListLock
Definition: ip.c:18
#define IF_LIST_ITER(n)
Definition: tilists.h:5
VOID TcpipReleaseSpinLock(PKSPIN_LOCK SpinLock, KIRQL Irql)
Definition: lock.c:26
VOID TcpipAcquireSpinLock(PKSPIN_LOCK SpinLock, PKIRQL Irql)
Definition: lock.c:18
UCHAR KIRQL
Definition: env_spec_w32.h:591
#define ForEachInterface(n)
Definition: tilists.h:9
KIRQL OldIrql
Definition: mm.h:1502
int Count
Definition: noreturn.cpp:7
#define EndFor(n)
Definition: tilists.h:20
unsigned int ULONG
Definition: retypes.h:1

◆ FindOnLinkInterface()

PIP_INTERFACE FindOnLinkInterface ( PIP_ADDRESS  Address)

Definition at line 221 of file interface.c.

229 {
230  KIRQL OldIrql;
231  IF_LIST_ITER(CurrentIF);
232 
233  TI_DbgPrint(DEBUG_ROUTER, ("Called. Address (0x%X)\n", Address));
234  TI_DbgPrint(DEBUG_ROUTER, ("Address (%s)\n", A2S(Address)));
235 
237  return GetDefaultInterface();
238 
240 
241  ForEachInterface(CurrentIF) {
242  if (HasPrefix(Address, &CurrentIF->Unicast,
243  AddrCountPrefixBits(&CurrentIF->Netmask))) {
245  return CurrentIF;
246  }
247  } EndFor(CurrentIF);
248 
250 
251  return NULL;
252 }
BOOLEAN AddrIsUnspecified(PIP_ADDRESS Address)
Definition: address.c:113
#define TI_DbgPrint(_t_, _x_)
Definition: debug.h:45
KSPIN_LOCK InterfaceListLock
Definition: ip.c:18
BOOLEAN HasPrefix(PIP_ADDRESS Address, PIP_ADDRESS Prefix, UINT Length)
Definition: interface.c:111
#define IF_LIST_ITER(n)
Definition: tilists.h:5
VOID TcpipReleaseSpinLock(PKSPIN_LOCK SpinLock, KIRQL Irql)
Definition: lock.c:26
VOID TcpipAcquireSpinLock(PKSPIN_LOCK SpinLock, PKIRQL Irql)
Definition: lock.c:18
UCHAR KIRQL
Definition: env_spec_w32.h:591
#define ForEachInterface(n)
Definition: tilists.h:9
static WCHAR Address[46]
Definition: ping.c:68
PIP_INTERFACE GetDefaultInterface(VOID)
Definition: interface.c:156
KIRQL OldIrql
Definition: mm.h:1502
UINT AddrCountPrefixBits(PIP_ADDRESS Netmask)
Definition: address.c:64
#define EndFor(n)
Definition: tilists.h:20
#define NULL
Definition: types.h:112
#define DEBUG_ROUTER
Definition: debug.h:30
PCHAR A2S(PIP_ADDRESS Address)
Definition: address.c:17

Referenced by RouteGetRouteToDestination().

◆ GetDefaultInterface()

PIP_INTERFACE GetDefaultInterface ( VOID  )

Definition at line 156 of file interface.c.

157 {
158  KIRQL OldIrql;
159  ULONG Index = 0;
160  ULONG IfStatus;
161 
162  IF_LIST_ITER(CurrentIF);
163 
165  /* DHCP hack: Always return the adapter without an IP address */
166  ForEachInterface(CurrentIF) {
167  if (CurrentIF->Context && AddrIsUnspecified(&CurrentIF->Unicast)) {
169 
170  GetInterfaceConnectionStatus(CurrentIF, &IfStatus);
171  if (IfStatus == MIB_IF_OPER_STATUS_OPERATIONAL) {
172  return CurrentIF;
173  }
174 
176  }
177  } EndFor(CurrentIF);
178 
179  /* Try to continue from the next adapter */
180  ForEachInterface(CurrentIF) {
181  if (CurrentIF->Context && (Index++ == NextDefaultAdapter)) {
183 
184  GetInterfaceConnectionStatus(CurrentIF, &IfStatus);
185  if (IfStatus == MIB_IF_OPER_STATUS_OPERATIONAL) {
187  return CurrentIF;
188  }
189 
191  }
192  } EndFor(CurrentIF);
193 
194  /* No luck, so we'll choose the first adapter this time */
195  Index = 0;
196  ForEachInterface(CurrentIF) {
197  if (CurrentIF->Context) {
198  Index++;
200 
201  GetInterfaceConnectionStatus(CurrentIF, &IfStatus);
202  if (IfStatus == MIB_IF_OPER_STATUS_OPERATIONAL) {
204  return CurrentIF;
205  }
206 
208  }
209  } EndFor(CurrentIF);
210 
211  /* Even that didn't work, so we'll just go with loopback */
212  NextDefaultAdapter = 0;
214 
215  /* There are no physical interfaces on the system
216  * so we must pick the loopback interface */
217 
218  return Loopback;
219 }
BOOLEAN AddrIsUnspecified(PIP_ADDRESS Address)
Definition: address.c:113
KSPIN_LOCK InterfaceListLock
Definition: ip.c:18
#define IF_LIST_ITER(n)
Definition: tilists.h:5
VOID TcpipReleaseSpinLock(PKSPIN_LOCK SpinLock, KIRQL Irql)
Definition: lock.c:26
VOID TcpipAcquireSpinLock(PKSPIN_LOCK SpinLock, PKIRQL Irql)
Definition: lock.c:18
PIP_INTERFACE Loopback
Definition: loopback.c:13
VOID GetInterfaceConnectionStatus(PIP_INTERFACE Interface, PULONG Result)
Definition: interface.c:254
UCHAR KIRQL
Definition: env_spec_w32.h:591
#define ForEachInterface(n)
Definition: tilists.h:9
KIRQL OldIrql
Definition: mm.h:1502
_In_ WDFCOLLECTION _In_ ULONG Index
#define EndFor(n)
Definition: tilists.h:20
unsigned int ULONG
Definition: retypes.h:1
#define MIB_IF_OPER_STATUS_OPERATIONAL
Definition: ipifcons.h:251
ULONG NextDefaultAdapter
Definition: interface.c:17

Referenced by DispTdiQueryIpHwAddress(), FindOnLinkInterface(), and NBLocateNeighbor().

◆ GetInterfaceConnectionStatus()

VOID GetInterfaceConnectionStatus ( PIP_INTERFACE  Interface,
PULONG  Result 
)

Definition at line 254 of file interface.c.

255 {
256  PLAN_ADAPTER Adapter = Interface->Context;
257 
258  /* Loopback has no adapter context */
259  if (Adapter == NULL || Adapter->State == LAN_STATE_STARTED) {
261  }
262  else {
264  }
265 }
PVOID Context
Definition: lan.h:64
#define MIB_IF_OPER_STATUS_DISCONNECTED
Definition: ipifcons.h:248
_At_(*)(_In_ PWSK_CLIENT Client, _In_opt_ PUNICODE_STRING NodeName, _In_opt_ PUNICODE_STRING ServiceName, _In_opt_ ULONG NameSpace, _In_opt_ GUID *Provider, _In_opt_ PADDRINFOEXW Hints, _Outptr_ PADDRINFOEXW *Result, _In_opt_ PEPROCESS OwningProcess, _In_opt_ PETHREAD OwningThread, _Inout_ PIRP Irp Result)(Mem)) NTSTATUS(WSKAPI *PFN_WSK_GET_ADDRESS_INFO
Definition: wsk.h:426
IN PVOID IN PVOID IN USHORT IN USHORT IN PINTERFACE Interface
Definition: pci.h:361
UCHAR State
Definition: lan.h:62
#define NULL
Definition: types.h:112
#define LAN_STATE_STARTED
Definition: lan.h:113
#define MIB_IF_OPER_STATUS_OPERATIONAL
Definition: ipifcons.h:251

Referenced by GetDefaultInterface(), InfoTdiQueryGetInterfaceMIB(), and TCPUpdateInterfaceLinkStatus().

◆ GetInterfaceIPv4Address()

NTSTATUS GetInterfaceIPv4Address ( PIP_INTERFACE  Interface,
ULONG  TargetType,
PULONG  Address 
)

Definition at line 19 of file interface.c.

21  {
22  switch( TargetType ) {
23  case ADE_UNICAST:
24  *Address = Interface->Unicast.Address.IPv4Address;
25  break;
26 
27  case ADE_ADDRMASK:
28  *Address = Interface->Netmask.Address.IPv4Address;
29  break;
30 
31  case ADE_BROADCAST:
32  *Address = Interface->Broadcast.Address.IPv4Address;
33  break;
34 
35  case ADE_POINTOPOINT:
36  *Address = Interface->PointToPoint.Address.IPv4Address;
37  break;
38 
39  default:
40  return STATUS_UNSUCCESSFUL;
41  }
42 
43  return STATUS_SUCCESS;
44 }
#define ADE_BROADCAST
Definition: ip.h:111
#define ADE_POINTOPOINT
Definition: ip.h:113
static WCHAR Address[46]
Definition: ping.c:68
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
#define ADE_ADDRMASK
Definition: ip.h:112
IN PVOID IN PVOID IN USHORT IN USHORT IN PINTERFACE Interface
Definition: pci.h:361
#define STATUS_SUCCESS
Definition: shellext.h:65
#define ADE_UNICAST
Definition: ip.h:110

Referenced by InfoTdiQueryGetAddrTable(), and TCPUpdateInterfaceIPInformation().

◆ GetInterfaceName()

NTSTATUS GetInterfaceName ( PIP_INTERFACE  Interface,
PCHAR  NameBuffer,
UINT  Len 
)

Definition at line 70 of file interface.c.

72  {
73  ULONG ResultSize = 0;
75  RtlUnicodeToMultiByteN( NameBuffer,
76  Len,
77  &ResultSize,
78  Interface->Name.Buffer,
79  Interface->Name.Length );
80 
81  if( NT_SUCCESS(Status) )
82  NameBuffer[ResultSize] = 0;
83  else
84  NameBuffer[0] = 0;
85 
86  return Status;
87 }
LONG NTSTATUS
Definition: precomp.h:26
Status
Definition: gdiplustypes.h:24
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define Len
Definition: deflate.h:82
NTSYSAPI NTSTATUS NTAPI RtlUnicodeToMultiByteN(PCHAR MbString, ULONG MbSize, PULONG ResultSize, PCWCH UnicodeString, ULONG UnicodeSize)
IN PVOID IN PVOID IN USHORT IN USHORT IN PINTERFACE Interface
Definition: pci.h:361
unsigned int ULONG
Definition: retypes.h:1

◆ GetInterfaceSpeed()

NTSTATUS GetInterfaceSpeed ( PIP_INTERFACE  Interface,
PUINT  Speed 
)

Definition at line 62 of file interface.c.

62  {
64 
65  *Speed = IF->Speed;
66 
67  return STATUS_SUCCESS;
68 }
PVOID Context
Definition: lan.h:64
UINT Speed
Definition: lan.h:79
struct LAN_ADAPTER * PLAN_ADAPTER
IN PVOID IN PVOID IN USHORT IN USHORT IN PINTERFACE Interface
Definition: pci.h:361
#define STATUS_SUCCESS
Definition: shellext.h:65

Referenced by InfoTdiQueryGetInterfaceMIB().

◆ HasPrefix()

BOOLEAN HasPrefix ( PIP_ADDRESS  Address,
PIP_ADDRESS  Prefix,
UINT  Length 
)

Definition at line 111 of file interface.c.

126 {
127  PUCHAR pAddress = (PUCHAR)&Address->Address;
128  PUCHAR pPrefix = (PUCHAR)&Prefix->Address;
129 
130  TI_DbgPrint(DEBUG_ROUTER, ("Called. Address (0x%X) Prefix (0x%X) Length (%d).\n", Address, Prefix, Length));
131 
132 #if 0
133  TI_DbgPrint(DEBUG_ROUTER, ("Address (%s) Prefix (%s).\n",
134  A2S(Address), A2S(Prefix)));
135 #endif
136 
137  /* Don't report matches for empty prefixes */
138  if (Length == 0) {
139  return FALSE;
140  }
141 
142  /* Check that initial integral bytes match */
143  while (Length > 8) {
144  if (*pAddress++ != *pPrefix++)
145  return FALSE;
146  Length -= 8;
147  }
148 
149  /* Check any remaining bits */
150  if ((Length > 0) && ((*pAddress >> (8 - Length)) != (*pPrefix >> (8 - Length))))
151  return FALSE;
152 
153  return TRUE;
154 }
#define TI_DbgPrint(_t_, _x_)
Definition: debug.h:45
_In_ __drv_aliasesMem PSTRING Prefix
Definition: rtlfuncs.h:1630
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
#define TRUE
Definition: types.h:120
unsigned char * PUCHAR
Definition: retypes.h:3
#define FALSE
Definition: types.h:117
static WCHAR Address[46]
Definition: ping.c:68
#define DEBUG_ROUTER
Definition: debug.h:30
PCHAR A2S(PIP_ADDRESS Address)
Definition: address.c:17

Referenced by AcpiExGetNameString(), and FindOnLinkInterface().

Variable Documentation

◆ NextDefaultAdapter

ULONG NextDefaultAdapter = 0

Definition at line 17 of file interface.c.

Referenced by GetDefaultInterface().