2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS TCP/IP protocol driver
5 * PURPOSE: Network information
6 * PROGRAMMERS: Art Yerkes
8 * CSH 01/08-2000 Created
13 #define IP_ROUTE_TYPE_ADD 3
14 #define IP_ROUTE_TYPE_DEL 2
16 /* Get IPRouteEntry s for each of the routes in the system */
17 TDI_STATUS
InfoTdiQueryGetRouteTable( PIP_INTERFACE IF
, PNDIS_BUFFER Buffer
, PUINT BufferSize
) {
20 UINT RtCount
= CountFIBs(IF
);
21 UINT Size
= sizeof( IPROUTE_ENTRY
) * RtCount
;
22 PFIB_ENTRY RCache
, RCacheCur
;
23 PIPROUTE_ENTRY RouteEntries
, RtCurrent
;
26 TI_DbgPrint(DEBUG_INFO
, ("Called, routes = %d\n",
30 return InfoCopyOut(NULL
, 0, NULL
, BufferSize
);
32 RouteEntries
= ExAllocatePoolWithTag( NonPagedPool
, Size
, ROUTE_ENTRY_TAG
);
33 RtCurrent
= RouteEntries
;
35 RCache
= ExAllocatePoolWithTag( NonPagedPool
, sizeof( FIB_ENTRY
) * RtCount
, FIB_TAG
);
38 if( !RCache
|| !RouteEntries
) {
39 if( RCache
) ExFreePoolWithTag( RCache
, FIB_TAG
);
40 if( RouteEntries
) ExFreePoolWithTag( RouteEntries
, ROUTE_ENTRY_TAG
);
41 return TDI_NO_RESOURCES
;
44 RtlZeroMemory( RouteEntries
, Size
);
46 RtCount
= CopyFIBs( IF
, RCache
);
48 while( RtCurrent
< RouteEntries
+ RtCount
) {
49 ASSERT(RCacheCur
->Router
);
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
) );
61 RtCurrent
->Metric1
= RCacheCur
->Metric
;
62 RtCurrent
->Type
= TDI_ADDRESS_TYPE_IP
;
66 ("%d: NA %08x NM %08x GW %08x MT %x\n",
67 RtCurrent
- RouteEntries
,
71 RtCurrent
->Metric1
));
73 TcpipAcquireSpinLock(&EntityListLock
, &OldIrql
);
74 for (i
= 0; i
< EntityCount
; i
++)
75 if (EntityList
[i
].context
== IF
)
79 RtCurrent
->Index
= EntityList
[i
].tei_instance
;
83 TcpipReleaseSpinLock(&EntityListLock
, OldIrql
);
85 RtCurrent
++; RCacheCur
++;
88 Status
= InfoCopyOut( (PCHAR
)RouteEntries
, Size
, Buffer
, BufferSize
);
90 ExFreePoolWithTag( RouteEntries
, ROUTE_ENTRY_TAG
);
91 ExFreePoolWithTag( RCache
, FIB_TAG
);
93 TI_DbgPrint(DEBUG_INFO
, ("Returning %08x\n", Status
));
98 TDI_STATUS
InfoTdiQueryGetAddrTable(TDIEntityID ID
,
103 PIPADDR_ENTRY IPEntry
;
104 PIP_INTERFACE CurrentIF
;
107 TI_DbgPrint(DEBUG_INFO
, ("Called.\n"));
110 TcpipAcquireSpinLock(&EntityListLock
, &OldIrql
);
112 for (i
= 0; i
< EntityCount
; i
++)
114 if (EntityList
[i
].tei_entity
== ID
.tei_entity
&&
115 EntityList
[i
].tei_instance
== ID
.tei_instance
)
119 if (i
== EntityCount
)
121 TcpipReleaseSpinLock(&EntityListLock
, OldIrql
);
122 return TDI_INVALID_PARAMETER
;
125 IPEntry
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(IPADDR_ENTRY
), IP_ADDRESS_TAG
);
128 TcpipReleaseSpinLock(&EntityListLock
, OldIrql
);
129 return TDI_NO_RESOURCES
;
132 CurrentIF
= EntityList
[i
].context
;
134 IPEntry
->Index
= CurrentIF
->Index
;
135 GetInterfaceIPv4Address(CurrentIF
,
138 GetInterfaceIPv4Address(CurrentIF
,
141 GetInterfaceIPv4Address(CurrentIF
,
143 &IPEntry
->BcastAddr
);
145 TcpipReleaseSpinLock(&EntityListLock
, OldIrql
);
147 InfoCopyOut((PCHAR
)IPEntry
, sizeof(IPADDR_ENTRY
),
150 ExFreePoolWithTag(IPEntry
, IP_ADDRESS_TAG
);
155 TDI_STATUS
InfoTdiQueryGetIPSnmpInfo( TDIEntityID ID
,
160 UINT IfCount
= CountInterfaces();
161 UINT RouteCount
= CountFIBs(IF
);
162 TDI_STATUS Status
= TDI_INVALID_REQUEST
;
164 TI_DbgPrint(DEBUG_INFO
, ("Called.\n"));
166 RtlZeroMemory(&SnmpInfo
, sizeof(SnmpInfo
));
168 SnmpInfo
.ipsi_numif
= IfCount
;
169 SnmpInfo
.ipsi_numaddr
= 1;
170 SnmpInfo
.ipsi_numroutes
= RouteCount
;
172 Status
= InfoCopyOut( (PCHAR
)&SnmpInfo
, sizeof(SnmpInfo
),
173 Buffer
, BufferSize
);
175 TI_DbgPrint(DEBUG_INFO
, ("Returning %08x\n", Status
));
180 #define ntohs(n) ((((n) & 0xff) << 8) | (((n) & 0xff00) >> 8))
182 TDI_STATUS
InfoTdiQueryGetConnectionTcpTable(PADDRESS_FILE AddrFile
,
185 TDI_TCPUDP_CLASS_INFO Class
)
188 MIB_TCPROW_OWNER_MODULE TcpRow
;
189 TDI_STATUS Status
= TDI_INVALID_REQUEST
;
191 TI_DbgPrint(DEBUG_INFO
, ("Called.\n"));
193 if (Class
== TcpUdpClassOwnerPid
)
195 Size
= sizeof(MIB_TCPROW_OWNER_PID
);
197 else if (Class
== TcpUdpClassOwner
)
199 Size
= sizeof(MIB_TCPROW_OWNER_MODULE
);
203 Size
= sizeof(MIB_TCPROW
);
206 TcpRow
.dwOwningPid
= (DWORD
)AddrFile
->ProcessId
;
207 TcpRow
.liCreateTimestamp
= AddrFile
->CreationTime
;
209 if (AddrFile
->Listener
!= NULL
)
211 PADDRESS_FILE EndPoint
;
213 EndPoint
= AddrFile
->Listener
->AddressFile
;
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
;
221 Status
= TDI_SUCCESS
;
223 else if (AddrFile
->Connection
!= NULL
&&
224 AddrFile
->Connection
->SocketContext
!= NULL
)
226 TA_IP_ADDRESS EndPoint
;
228 Status
= TCPGetSockAddress(AddrFile
->Connection
, (PTRANSPORT_ADDRESS
)&EndPoint
, FALSE
);
229 if (NT_SUCCESS(Status
))
231 ASSERT(EndPoint
.TAAddressCount
>= 1);
232 ASSERT(EndPoint
.Address
[0].AddressLength
== TDI_ADDRESS_LENGTH_IP
);
233 TcpRow
.dwLocalAddr
= EndPoint
.Address
[0].Address
[0].in_addr
;
234 TcpRow
.dwLocalPort
= ntohs(EndPoint
.Address
[0].Address
[0].sin_port
);
236 Status
= TCPGetSockAddress(AddrFile
->Connection
, (PTRANSPORT_ADDRESS
)&EndPoint
, TRUE
);
237 if (NT_SUCCESS(Status
))
239 ASSERT(EndPoint
.TAAddressCount
>= 1);
240 ASSERT(EndPoint
.Address
[0].AddressLength
== TDI_ADDRESS_LENGTH_IP
);
241 TcpRow
.dwRemoteAddr
= EndPoint
.Address
[0].Address
[0].in_addr
;
242 TcpRow
.dwRemotePort
= ntohs(EndPoint
.Address
[0].Address
[0].sin_port
);
244 Status
= TCPGetSocketStatus(AddrFile
->Connection
, &TcpRow
.dwState
);
245 ASSERT(NT_SUCCESS(Status
));
250 if (NT_SUCCESS(Status
))
252 if (Class
== TcpUdpClassOwner
)
254 RtlZeroMemory(&TcpRow
.OwningModuleInfo
[0], sizeof(TcpRow
.OwningModuleInfo
));
255 TcpRow
.OwningModuleInfo
[0] = (ULONG_PTR
)AddrFile
->SubProcessTag
;
258 Status
= InfoCopyOut( (PCHAR
)&TcpRow
, Size
,
259 Buffer
, BufferSize
);
262 TI_DbgPrint(DEBUG_INFO
, ("Returning %08x\n", Status
));
267 TDI_STATUS
InfoTdiQueryGetConnectionUdpTable(PADDRESS_FILE AddrFile
,
270 TDI_TCPUDP_CLASS_INFO Class
)
273 MIB_UDPROW_OWNER_MODULE UdpRow
;
274 TDI_STATUS Status
= TDI_INVALID_REQUEST
;
276 TI_DbgPrint(DEBUG_INFO
, ("Called.\n"));
278 if (Class
== TcpUdpClassOwnerPid
)
280 Size
= sizeof(MIB_UDPROW_OWNER_PID
);
282 else if (Class
== TcpUdpClassOwner
)
284 Size
= sizeof(MIB_UDPROW_OWNER_MODULE
);
288 Size
= sizeof(MIB_UDPROW
);
291 UdpRow
.dwLocalAddr
= AddrFile
->Address
.Address
.IPv4Address
;
292 UdpRow
.dwLocalPort
= AddrFile
->Port
;
293 UdpRow
.dwOwningPid
= (DWORD
)AddrFile
->ProcessId
;
294 UdpRow
.liCreateTimestamp
= AddrFile
->CreationTime
;
295 UdpRow
.dwFlags
= 0; /* FIXME */
296 if (Class
== TcpUdpClassOwner
)
298 RtlZeroMemory(&UdpRow
.OwningModuleInfo
[0], sizeof(UdpRow
.OwningModuleInfo
));
299 UdpRow
.OwningModuleInfo
[0] = (ULONG_PTR
)AddrFile
->SubProcessTag
;
302 Status
= InfoCopyOut( (PCHAR
)&UdpRow
,
303 Size
, Buffer
, BufferSize
);
305 TI_DbgPrint(DEBUG_INFO
, ("Returning %08x\n", Status
));
310 TDI_STATUS
InfoTdiSetRoute(PIP_INTERFACE IF
, PVOID Buffer
, UINT BufferSize
)
312 IP_ADDRESS Address
, Netmask
, Router
;
313 PIPROUTE_ENTRY Route
= Buffer
;
315 AddrInitIPv4( &Address
, Route
->Dest
);
316 AddrInitIPv4( &Netmask
, Route
->Mask
);
317 AddrInitIPv4( &Router
, Route
->Gw
);
319 if (!Buffer
|| BufferSize
< sizeof(IPROUTE_ENTRY
))
320 return TDI_INVALID_PARAMETER
;
324 DbgPrint("Failing attempt to add route to loopback adapter\n");
325 return TDI_INVALID_PARAMETER
;
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
,
332 return TDI_NO_RESOURCES
;
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
)))
340 return TDI_INVALID_PARAMETER
;
343 return TDI_INVALID_REQUEST
;