ReactOS  0.4.13-dev-657-g10798fa
ninfo.c
Go to the documentation of this file.
1 /*
2  * COPYRIGHT: See COPYING in the top level directory
3  * PROJECT: ReactOS TCP/IP protocol driver
4  * FILE: tcpip/ninfo.c
5  * PURPOSE: Network information
6  * PROGRAMMERS: Art Yerkes
7  * REVISIONS:
8  * CSH 01/08-2000 Created
9  */
10 
11 #include "precomp.h"
12 
13 #define IP_ROUTE_TYPE_ADD 3
14 #define IP_ROUTE_TYPE_DEL 2
15 
16 /* Get IPRouteEntry s for each of the routes in the system */
19  KIRQL OldIrql;
20  UINT RtCount = CountFIBs(IF);
21  UINT Size = sizeof( IPROUTE_ENTRY ) * RtCount;
22  PFIB_ENTRY RCache, RCacheCur;
23  PIPROUTE_ENTRY RouteEntries, RtCurrent;
24  UINT i;
25 
26  TI_DbgPrint(DEBUG_INFO, ("Called, routes = %d\n",
27  RtCount));
28 
29  if (RtCount == 0)
30  return InfoCopyOut(NULL, 0, NULL, BufferSize);
31 
33  RtCurrent = RouteEntries;
34 
35  RCache = ExAllocatePoolWithTag( NonPagedPool, sizeof( FIB_ENTRY ) * RtCount, FIB_TAG );
36  RCacheCur = RCache;
37 
38  if( !RCache || !RouteEntries ) {
39  if( RCache ) ExFreePoolWithTag( RCache, FIB_TAG );
40  if( RouteEntries ) ExFreePoolWithTag( RouteEntries, ROUTE_ENTRY_TAG );
41  return TDI_NO_RESOURCES;
42  }
43 
44  RtlZeroMemory( RouteEntries, Size );
45 
46  RtCount = CopyFIBs( IF, RCache );
47 
48  while( RtCurrent < RouteEntries + RtCount ) {
49  ASSERT(RCacheCur->Router);
50 
51  RtlCopyMemory( &RtCurrent->Dest,
52  &RCacheCur->NetworkAddress.Address,
53  sizeof(RtCurrent->Dest) );
54  RtlCopyMemory( &RtCurrent->Mask,
55  &RCacheCur->Netmask.Address,
56  sizeof(RtCurrent->Mask) );
57  RtlCopyMemory( &RtCurrent->Gw,
58  &RCacheCur->Router->Address.Address,
59  sizeof(RtCurrent->Gw) );
60 
61  RtCurrent->Metric1 = RCacheCur->Metric;
62  RtCurrent->Type = TDI_ADDRESS_TYPE_IP;
63 
65  (DEBUG_INFO,
66  ("%d: NA %08x NM %08x GW %08x MT %x\n",
67  RtCurrent - RouteEntries,
68  RtCurrent->Dest,
69  RtCurrent->Mask,
70  RtCurrent->Gw,
71  RtCurrent->Metric1 ));
72 
74  for (i = 0; i < EntityCount; i++)
75  if (EntityList[i].context == IF)
76  break;
77 
78  if (i < EntityCount)
79  RtCurrent->Index = EntityList[i].tei_instance;
80  else
81  RtCurrent->Index = 0;
82 
84 
85  RtCurrent++; RCacheCur++;
86  }
87 
88  Status = InfoCopyOut( (PCHAR)RouteEntries, Size, Buffer, BufferSize );
89 
90  ExFreePoolWithTag( RouteEntries, ROUTE_ENTRY_TAG );
91  ExFreePoolWithTag( RCache, FIB_TAG );
92 
93  TI_DbgPrint(DEBUG_INFO, ("Returning %08x\n", Status));
94 
95  return Status;
96 }
97 
101 {
102  KIRQL OldIrql;
103  PIPADDR_ENTRY IPEntry;
104  PIP_INTERFACE CurrentIF;
105  UINT i;
106 
107  TI_DbgPrint(DEBUG_INFO, ("Called.\n"));
108 
109 
111 
112  for (i = 0; i < EntityCount; i++)
113  {
114  if (EntityList[i].tei_entity == ID.tei_entity &&
115  EntityList[i].tei_instance == ID.tei_instance)
116  break;
117  }
118 
119  if (i == EntityCount)
120  {
122  return TDI_INVALID_PARAMETER;
123  }
124 
126  if (!IPEntry)
127  {
129  return TDI_NO_RESOURCES;
130  }
131 
132  CurrentIF = EntityList[i].context;
133 
134  IPEntry->Index = CurrentIF->Index;
135  GetInterfaceIPv4Address(CurrentIF,
136  ADE_UNICAST,
137  &IPEntry->Addr);
138  GetInterfaceIPv4Address(CurrentIF,
139  ADE_ADDRMASK,
140  &IPEntry->Mask);
141  GetInterfaceIPv4Address(CurrentIF,
143  &IPEntry->BcastAddr);
144 
146 
147  InfoCopyOut((PCHAR)IPEntry, sizeof(IPADDR_ENTRY),
148  Buffer, BufferSize);
149 
151 
152  return TDI_SUCCESS;
153 }
154 
156  PIP_INTERFACE IF,
158  PUINT BufferSize ) {
159  IPSNMPInfo SnmpInfo;
160  UINT IfCount = CountInterfaces();
161  UINT RouteCount = CountFIBs(IF);
163 
164  TI_DbgPrint(DEBUG_INFO, ("Called.\n"));
165 
166  RtlZeroMemory(&SnmpInfo, sizeof(SnmpInfo));
167 
168  SnmpInfo.ipsi_numif = IfCount;
169  SnmpInfo.ipsi_numaddr = 1;
170  SnmpInfo.ipsi_numroutes = RouteCount;
171 
172  Status = InfoCopyOut( (PCHAR)&SnmpInfo, sizeof(SnmpInfo),
173  Buffer, BufferSize );
174 
175  TI_DbgPrint(DEBUG_INFO, ("Returning %08x\n", Status));
176 
177  return Status;
178 }
179 
180 #define ntohs(n) ((((n) & 0xff) << 8) | (((n) & 0xff00) >> 8))
181 
186 {
187  SIZE_T Size;
190 
191  TI_DbgPrint(DEBUG_INFO, ("Called.\n"));
192 
193  if (Class == TcpUdpClassOwnerPid)
194  {
195  Size = sizeof(MIB_TCPROW_OWNER_PID);
196  }
197  else if (Class == TcpUdpClassOwner)
198  {
199  Size = sizeof(MIB_TCPROW_OWNER_MODULE);
200  }
201  else
202  {
203  Size = sizeof(MIB_TCPROW);
204  }
205 
206  TcpRow.dwOwningPid = HandleToUlong(AddrFile->ProcessId);
207  TcpRow.liCreateTimestamp = AddrFile->CreationTime;
208 
209  if (AddrFile->Listener != NULL)
210  {
211  PADDRESS_FILE EndPoint;
212 
213  EndPoint = AddrFile->Listener->AddressFile;
214 
215  TcpRow.dwState = MIB_TCP_STATE_LISTEN;
216  TcpRow.dwLocalAddr = AddrFile->Address.Address.IPv4Address;
217  TcpRow.dwLocalPort = AddrFile->Port;
218  TcpRow.dwRemoteAddr = EndPoint->Address.Address.IPv4Address;
219  TcpRow.dwRemotePort = EndPoint->Port;
220 
222  }
223  else if (AddrFile->Connection != NULL &&
224  AddrFile->Connection->SocketContext != NULL)
225  {
226  TA_IP_ADDRESS EndPoint;
227 
228  Status = TCPGetSockAddress(AddrFile->Connection, (PTRANSPORT_ADDRESS)&EndPoint, FALSE);
229  if (NT_SUCCESS(Status))
230  {
231  ASSERT(EndPoint.TAAddressCount >= 1);
233  TcpRow.dwLocalAddr = EndPoint.Address[0].Address[0].in_addr;
234  TcpRow.dwLocalPort = ntohs(EndPoint.Address[0].Address[0].sin_port);
235 
236  Status = TCPGetSockAddress(AddrFile->Connection, (PTRANSPORT_ADDRESS)&EndPoint, TRUE);
237  if (NT_SUCCESS(Status))
238  {
239  ASSERT(EndPoint.TAAddressCount >= 1);
241  TcpRow.dwRemoteAddr = EndPoint.Address[0].Address[0].in_addr;
242  TcpRow.dwRemotePort = ntohs(EndPoint.Address[0].Address[0].sin_port);
243 
244  Status = TCPGetSocketStatus(AddrFile->Connection, &TcpRow.dwState);
246  }
247  }
248  }
249 
250  if (NT_SUCCESS(Status))
251  {
252  if (Class == TcpUdpClassOwner)
253  {
254  RtlZeroMemory(&TcpRow.OwningModuleInfo[0], sizeof(TcpRow.OwningModuleInfo));
255  TcpRow.OwningModuleInfo[0] = (ULONG_PTR)AddrFile->SubProcessTag;
256  }
257 
258  Status = InfoCopyOut( (PCHAR)&TcpRow, Size,
259  Buffer, BufferSize );
260  }
261 
262  TI_DbgPrint(DEBUG_INFO, ("Returning %08x\n", Status));
263 
264  return Status;
265 }
266 
271 {
272  SIZE_T Size;
275 
276  TI_DbgPrint(DEBUG_INFO, ("Called.\n"));
277 
278  if (Class == TcpUdpClassOwnerPid)
279  {
280  Size = sizeof(MIB_UDPROW_OWNER_PID);
281  }
282  else if (Class == TcpUdpClassOwner)
283  {
284  Size = sizeof(MIB_UDPROW_OWNER_MODULE);
285  }
286  else
287  {
288  Size = sizeof(MIB_UDPROW);
289  }
290 
291  UdpRow.dwLocalAddr = AddrFile->Address.Address.IPv4Address;
292  UdpRow.dwLocalPort = AddrFile->Port;
293  UdpRow.dwOwningPid = HandleToUlong(AddrFile->ProcessId);
294  UdpRow.liCreateTimestamp = AddrFile->CreationTime;
295  UdpRow.dwFlags = 0; /* FIXME */
296  if (Class == TcpUdpClassOwner)
297  {
298  RtlZeroMemory(&UdpRow.OwningModuleInfo[0], sizeof(UdpRow.OwningModuleInfo));
299  UdpRow.OwningModuleInfo[0] = (ULONG_PTR)AddrFile->SubProcessTag;
300  }
301 
302  Status = InfoCopyOut( (PCHAR)&UdpRow,
303  Size, Buffer, BufferSize );
304 
305  TI_DbgPrint(DEBUG_INFO, ("Returning %08x\n", Status));
306 
307  return Status;
308 }
309 
311 {
312  IP_ADDRESS Address, Netmask, Router;
313  PIPROUTE_ENTRY Route = Buffer;
314 
315  if (!Buffer || BufferSize < sizeof(IPROUTE_ENTRY))
316  return TDI_INVALID_PARAMETER;
317 
318  AddrInitIPv4( &Address, Route->Dest );
319  AddrInitIPv4( &Netmask, Route->Mask );
320  AddrInitIPv4( &Router, Route->Gw );
321 
322  if (IF == Loopback)
323  {
324  DbgPrint("Failing attempt to add route to loopback adapter\n");
325  return TDI_INVALID_PARAMETER;
326  }
327 
328  if( Route->Type == IP_ROUTE_TYPE_ADD ) { /* Add the route */
329  TI_DbgPrint(DEBUG_INFO,("Adding route (%s)\n", A2S(&Address)));
330  if (!RouterCreateRoute( &Address, &Netmask, &Router,
331  IF, Route->Metric1))
332  return TDI_NO_RESOURCES;
333 
334  return TDI_SUCCESS;
335  } else if( Route->Type == IP_ROUTE_TYPE_DEL ) {
336  TI_DbgPrint(DEBUG_INFO,("Removing route (%s)\n", A2S(&Address)));
337  if (NT_SUCCESS(RouterRemoveRoute( &Address, &Router )))
338  return TDI_SUCCESS;
339  else
340  return TDI_INVALID_PARAMETER;
341  }
342 
343  return TDI_INVALID_REQUEST;
344 }
TDI_STATUS InfoTdiQueryGetConnectionUdpTable(PADDRESS_FILE AddrFile, PNDIS_BUFFER Buffer, PUINT BufferSize, TDI_TCPUDP_CLASS_INFO Class)
Definition: ninfo.c:267
signed char * PCHAR
Definition: retypes.h:7
UINT CountInterfaces(VOID)
#define TI_DbgPrint(_t_, _x_)
Definition: debug.h:45
ULONG Metric1
Definition: info.h:46
IPv4_RAW_ADDRESS IPv4Address
Definition: ip.h:26
ULONG ipsi_numaddr
Definition: tcpioctl.h:153
ULONG EntityCount
Definition: main.c:24
#define TRUE
Definition: types.h:120
NTSYSAPI VOID NTAPI RtlCopyMemory(VOID UNALIGNED *Destination, CONST VOID UNALIGNED *Source, ULONG Length)
struct _MIB_UDPROW MIB_UDPROW
#define TDI_SUCCESS
Definition: tdistat.h:30
KSPIN_LOCK EntityListLock
Definition: main.c:22
UCHAR Address[1]
Definition: tdi.h:340
#define DbgPrint
Definition: loader.c:25
NTSTATUS TCPGetSocketStatus(PCONNECTION_ENDPOINT Connection, PULONG State)
Definition: tcp.c:695
Definition: http.c:6587
struct _MIB_TCPROW_OWNER_MODULE MIB_TCPROW_OWNER_MODULE
MDL * PNDIS_BUFFER
Definition: ndis.h:343
Definition: afd.h:56
#define FIB_TAG
Definition: tags.h:29
TDIEntityInfo * EntityList
Definition: main.c:23
ULONGLONG OwningModuleInfo[TCPIP_OWNING_MODULE_SIZE]
Definition: udpmib.h:65
#define HandleToUlong(h)
Definition: basetsd.h:79
struct _MIB_UDPROW_OWNER_PID MIB_UDPROW_OWNER_PID
TDI_STATUS InfoTdiQueryGetIPSnmpInfo(TDIEntityID ID, PIP_INTERFACE IF, PNDIS_BUFFER Buffer, PUINT BufferSize)
Definition: ninfo.c:155
IP_ADDRESS NetworkAddress
Definition: router.h:17
TDI_TCPUDP_CLASS_INFO
Definition: info.h:83
LONG TDI_STATUS
Definition: tdi.h:36
#define ROUTE_ENTRY_TAG
Definition: tags.h:38
HANDLE ProcessId
Definition: titypes.h:157
UINT CountFIBs(PIP_INTERFACE IF)
Definition: router.c:101
VOID TcpipReleaseSpinLock(PKSPIN_LOCK SpinLock, KIRQL Irql)
Definition: lock.c:26
VOID TcpipAcquireSpinLock(PKSPIN_LOCK SpinLock, PKIRQL Irql)
Definition: lock.c:18
ULONG ipsi_numroutes
Definition: tcpioctl.h:154
PIP_INTERFACE Loopback
Definition: loopback.c:13
Definition: ip.h:23
#define ADE_BROADCAST
Definition: ip.h:111
UCHAR KIRQL
Definition: env_spec_w32.h:591
NTSTATUS TCPGetSockAddress(PCONNECTION_ENDPOINT Connection, PTRANSPORT_ADDRESS TransportAddress, BOOLEAN RemoteAddress)
Definition: tcp.c:602
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
ULONG Mask
Definition: afd.h:59
TDI_STATUS InfoCopyOut(PCHAR DataOut, UINT SizeOut, PNDIS_BUFFER ClientBuf, PUINT ClientBufSize)
Definition: info.c:97
IP_ADDRESS Netmask
Definition: router.h:18
smooth NULL
Definition: ftsmooth.c:416
static WCHAR Address[46]
Definition: ping.c:68
Definition: bufpool.h:45
ULONG Dest
Definition: info.h:44
ULONG BcastAddr
Definition: afd.h:60
ULONG Index
Definition: info.h:45
UINT Metric
Definition: router.h:20
UINT tei_instance
Definition: tcpip.h:147
#define IP_ADDRESS_TAG
Definition: tags.h:18
#define ntohs(n)
Definition: ninfo.c:180
ULONGLONG OwningModuleInfo[TCPIP_OWNING_MODULE_SIZE]
Definition: tcpmib.h:127
LARGE_INTEGER liCreateTimestamp
Definition: udpmib.h:56
ULONG Index
Definition: afd.h:58
UINT Index
Definition: ip.h:167
ULONG ipsi_numif
Definition: tcpioctl.h:152
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
LARGE_INTEGER liCreateTimestamp
Definition: tcpmib.h:126
#define AddrInitIPv4(IPAddress, RawAddress)
Definition: address.h:16
#define IP_ROUTE_TYPE_DEL
Definition: ninfo.c:14
PVOID context
Definition: tcpip.h:148
ULONG Mask
Definition: info.h:54
#define BufferSize
Definition: classpnp.h:419
LONG TAAddressCount
Definition: tdi.h:376
Definition: info.h:43
ULONG Gw
Definition: info.h:50
TDI_STATUS InfoTdiQueryGetRouteTable(PIP_INTERFACE IF, PNDIS_BUFFER Buffer, PUINT BufferSize)
Definition: ninfo.c:17
struct IPROUTE_ENTRY IPROUTE_ENTRY
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
struct _MIB_TCPROW_OWNER_PID MIB_TCPROW_OWNER_PID
PNEIGHBOR_CACHE_ENTRY Router
Definition: router.h:19
#define TDI_INVALID_PARAMETER
Definition: tdistat.h:52
TDI_STATUS InfoTdiQueryGetConnectionTcpTable(PADDRESS_FILE AddrFile, PNDIS_BUFFER Buffer, PUINT BufferSize, TDI_TCPUDP_CLASS_INFO Class)
Definition: ninfo.c:182
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:803
#define ADE_ADDRMASK
Definition: ip.h:112
#define TDI_ADDRESS_TYPE_IP
Definition: tdi.h:345
struct _MIB_UDPROW_OWNER_MODULE MIB_UDPROW_OWNER_MODULE
#define ID
Definition: ruserpass.c:36
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
struct _CONNECTION_ENDPOINT * Listener
Definition: titypes.h:153
NTSTATUS RouterRemoveRoute(PIP_ADDRESS Target, PIP_ADDRESS Router)
Definition: router.c:364
#define IP_ROUTE_TYPE_ADD
Definition: ninfo.c:13
PFIB_ENTRY RouterCreateRoute(PIP_ADDRESS NetworkAddress, PIP_ADDRESS Netmask, PIP_ADDRESS RouterAddress, PIP_INTERFACE Interface, UINT Metric)
Definition: router.c:421
Status
Definition: gdiplustypes.h:24
TA_ADDRESS Address[1]
Definition: tdi.h:377
ULONG Type
Definition: info.h:51
PVOID SubProcessTag
Definition: titypes.h:158
ULONG_PTR SIZE_T
Definition: typedefs.h:78
NTSTATUS GetInterfaceIPv4Address(PIP_INTERFACE Interface, ULONG Type, PULONG Address)
Definition: interface.c:19
IP_ADDRESS Address
Definition: titypes.h:136
unsigned int UINT
Definition: ndis.h:50
#define TDI_NO_RESOURCES
Definition: tdistat.h:31
IP_ADDRESS Address
Definition: neighbor.h:36
USHORT AddressLength
Definition: tdi.h:338
TDI_STATUS InfoTdiSetRoute(PIP_INTERFACE IF, PVOID Buffer, UINT BufferSize)
Definition: ninfo.c:310
TDI_STATUS InfoTdiQueryGetAddrTable(TDIEntityID ID, PNDIS_BUFFER Buffer, PUINT BufferSize)
Definition: ninfo.c:98
ULONG Addr
Definition: afd.h:57
Definition: router.h:14
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
#define ULONG_PTR
Definition: config.h:101
struct _CONNECTION_ENDPOINT * Connection
Definition: titypes.h:151
USHORT Port
Definition: titypes.h:139
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
#define TDI_ADDRESS_LENGTH_IP
Definition: tdi.h:413
#define ADE_UNICAST
Definition: ip.h:110
IN BOOLEAN OUT PSTR Buffer
Definition: progress.h:34
UINT CopyFIBs(PIP_INTERFACE IF, PFIB_ENTRY Target)
Definition: router.c:120
#define DEBUG_INFO
Definition: debug.h:33
#define TDI_INVALID_REQUEST
Definition: tdistat.h:63
unsigned int * PUINT
Definition: ndis.h:50
union IP_ADDRESS::@973 Address
struct _MIB_TCPROW MIB_TCPROW
PCHAR A2S(PIP_ADDRESS Address)
Definition: address.c:17
LARGE_INTEGER CreationTime
Definition: titypes.h:159