ReactOS  0.4.15-dev-3177-gd6a0299
fileobjs.c File Reference
#include "precomp.h"
#include <ntifs.h>
#include <ndk/pstypes.h>
Include dependency graph for fileobjs.c:

Go to the source code of this file.

Functions

PADDRESS_FILE AddrSearchFirst (PIP_ADDRESS Address, USHORT Port, USHORT Protocol, PAF_SEARCH SearchContext)
 
BOOLEAN AddrIsBroadcastMatch (PIP_ADDRESS UnicastAddress, PIP_ADDRESS BroadcastAddress)
 
BOOLEAN AddrReceiveMatch (PIP_ADDRESS LocalAddress, PIP_ADDRESS RemoteAddress)
 
VOID LogActiveObjects (VOID)
 
PADDRESS_FILE AddrFindShared (PIP_ADDRESS BindAddress, USHORT Port, USHORT Protocol)
 
PADDRESS_FILE AddrSearchNext (PAF_SEARCH SearchContext)
 
VOID AddrFileFree (PVOID Object)
 
VOID ControlChannelFree (PVOID Object)
 
NTSTATUS FileOpenAddress (PTDI_REQUEST Request, PTA_IP_ADDRESS Address, USHORT Protocol, BOOLEAN Shared, PVOID Options)
 
NTSTATUS FileCloseAddress (PTDI_REQUEST Request)
 
NTSTATUS FileOpenConnection (PTDI_REQUEST Request, PVOID ClientContext)
 
NTSTATUS FileCloseConnection (PTDI_REQUEST Request)
 
NTSTATUS FileOpenControlChannel (PTDI_REQUEST Request)
 
NTSTATUS FileCloseControlChannel (PTDI_REQUEST Request)
 

Variables

LIST_ENTRY AddressFileListHead
 
KSPIN_LOCK AddressFileListLock
 
LIST_ENTRY ConnectionEndpointListHead
 
KSPIN_LOCK ConnectionEndpointListLock
 

Function Documentation

◆ AddrFileFree()

VOID AddrFileFree ( PVOID  Object)

Definition at line 302 of file fileobjs.c.

309 {
310  PADDRESS_FILE AddrFile = Object;
311  KIRQL OldIrql;
312  PDATAGRAM_RECEIVE_REQUEST ReceiveRequest;
313  // PDATAGRAM_SEND_REQUEST SendRequest; See WTF below
314  PLIST_ENTRY CurrentEntry;
315 
316  TI_DbgPrint(MID_TRACE, ("Called.\n"));
317 
318  /* We should not be associated with a connection here */
319  ASSERT(!AddrFile->Connection);
320 
321  /* Remove address file from the global list */
323  RemoveEntryList(&AddrFile->ListEntry);
325 
326  /* FIXME: Kill TCP connections on this address file object */
327 
328  /* Return pending requests with error */
329 
330  TI_DbgPrint(DEBUG_ADDRFILE, ("Aborting receive requests on AddrFile at (0x%X).\n", AddrFile));
331 
332  /* Go through pending receive request list and cancel them all */
333  while (!IsListEmpty(&AddrFile->ReceiveQueue))
334  {
335  CurrentEntry = RemoveHeadList(&AddrFile->ReceiveQueue);
336  ReceiveRequest = CONTAINING_RECORD(CurrentEntry, DATAGRAM_RECEIVE_REQUEST, ListEntry);
337  (*ReceiveRequest->Complete)(ReceiveRequest->Context, STATUS_CANCELLED, 0);
338  ExFreePoolWithTag(ReceiveRequest, DATAGRAM_RECV_TAG);
339  }
340 
341  TI_DbgPrint(DEBUG_ADDRFILE, ("Aborting send requests on address file at (0x%X).\n", AddrFile));
342 
343 #if 0 /* Biggest WTF. All of this was taken care of above as DATAGRAM_RECEIVE_REQUEST. */
344  /* Go through pending send request list and cancel them all */
345  while (!IsListEmpty(&AddrFile->ReceiveQueue))
346  {
347  CurrentEntry = RemoveHeadList(&AddrFile->ReceiveQueue);
348  SendRequest = CONTAINING_RECORD(CurrentEntry, DATAGRAM_SEND_REQUEST, ListEntry);
349  (*SendRequest->Complete)(SendRequest->Context, STATUS_CANCELLED, 0);
351  }
352 #endif
353 
354  /* Protocol specific handling */
355  switch (AddrFile->Protocol) {
356  case IPPROTO_TCP:
357  if (AddrFile->Port)
358  {
359  TCPFreePort(AddrFile->Port);
360  }
361  break;
362 
363  case IPPROTO_UDP:
364  UDPFreePort( AddrFile->Port );
365  break;
366  }
367 
368  RemoveEntityByContext(AddrFile);
369 
370  ExDeleteResourceLite(&AddrFile->Resource);
371 
373 }
#define TI_DbgPrint(_t_, _x_)
Definition: debug.h:45
#define MID_TRACE
Definition: debug.h:15
USHORT Protocol
Definition: titypes.h:119
NTSTATUS NTAPI ExDeleteResourceLite(IN PERESOURCE Resource)
Definition: resource.c:1456
VOID TCPFreePort(const UINT Port)
Definition: tcp.c:639
DATAGRAM_COMPLETION_ROUTINE Complete
Definition: titypes.h:84
VOID TcpipReleaseSpinLock(PKSPIN_LOCK SpinLock, KIRQL Irql)
Definition: lock.c:26
_Must_inspect_result_ FORCEINLINE BOOLEAN IsListEmpty(_In_ const LIST_ENTRY *ListHead)
Definition: rtlfuncs.h:57
VOID TcpipAcquireSpinLock(PKSPIN_LOCK SpinLock, PKIRQL Irql)
Definition: lock.c:18
#define ADDR_FILE_TAG
Definition: tags.h:11
FORCEINLINE BOOLEAN RemoveEntryList(_In_ PLIST_ENTRY Entry)
Definition: rtlfuncs.h:105
UCHAR KIRQL
Definition: env_spec_w32.h:591
ERESOURCE Resource
Definition: titypes.h:116
FORCEINLINE PLIST_ENTRY RemoveHeadList(_Inout_ PLIST_ENTRY ListHead)
Definition: rtlfuncs.h:128
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
KIRQL OldIrql
Definition: mm.h:1502
#define DATAGRAM_RECV_TAG
Definition: tags.h:16
#define ASSERT(a)
Definition: mode.c:44
#define STATUS_CANCELLED
Definition: udferr_usr.h:170
VOID UDPFreePort(UINT Port)
Definition: udp.c:393
Definition: typedefs.h:119
#define DATAGRAM_SEND_TAG
Definition: tags.h:15
_Must_inspect_result_ _In_ WDFCOLLECTION _In_ WDFOBJECT Object
KSPIN_LOCK AddressFileListLock
Definition: fileobjs.c:22
LIST_ENTRY ListEntry
Definition: titypes.h:113
#define DEBUG_ADDRFILE
Definition: debug.h:23
LIST_ENTRY ReceiveQueue
Definition: titypes.h:129
struct _CONNECTION_ENDPOINT * Connection
Definition: titypes.h:131
USHORT Port
Definition: titypes.h:120
VOID RemoveEntityByContext(PVOID Context)
Definition: info.c:46
BOOL SendRequest(PCHAR pInBuffer, ULONG InBufferLength, PCHAR pOutBuffer, PULONG pOutBufferLength)
Definition: utility.c:11
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099

Referenced by FileOpenAddress().

◆ AddrFindShared()

PADDRESS_FILE AddrFindShared ( PIP_ADDRESS  BindAddress,
USHORT  Port,
USHORT  Protocol 
)

Definition at line 190 of file fileobjs.c.

194 {
195  PLIST_ENTRY CurrentEntry;
196  KIRQL OldIrql;
197  PADDRESS_FILE Current = NULL;
198 
200 
201  CurrentEntry = AddressFileListHead.Flink;
202  while (CurrentEntry != &AddressFileListHead) {
203  Current = CONTAINING_RECORD(CurrentEntry, ADDRESS_FILE, ListEntry);
204 
205  /* See if this address matches the search criteria */
206  if ((Current->Port == Port) &&
207  (Current->Protocol == Protocol))
208  {
209  /* Increase the sharer count */
210  ASSERT(Current->Sharers != 0);
211  InterlockedIncrement(&Current->Sharers);
212  break;
213  }
214 
215  CurrentEntry = CurrentEntry->Flink;
216  Current = NULL;
217  }
218 
220 
221  return Current;
222 }
CPPORT Port[4]
Definition: headless.c:35
LONG Sharers
Definition: titypes.h:121
USHORT Protocol
Definition: titypes.h:119
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
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
KIRQL OldIrql
Definition: mm.h:1502
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
#define ASSERT(a)
Definition: mode.c:44
Definition: typedefs.h:119
LIST_ENTRY AddressFileListHead
Definition: fileobjs.c:21
KSPIN_LOCK AddressFileListLock
Definition: fileobjs.c:22
#define InterlockedIncrement
Definition: armddk.h:53
#define NULL
Definition: types.h:112
USHORT Port
Definition: titypes.h:120

Referenced by FileOpenAddress().

◆ AddrIsBroadcastMatch()

BOOLEAN AddrIsBroadcastMatch ( PIP_ADDRESS  UnicastAddress,
PIP_ADDRESS  BroadcastAddress 
)

Definition at line 62 of file fileobjs.c.

64  {
65  IF_LIST_ITER(IF);
66 
67  ForEachInterface(IF) {
68  if ((AddrIsUnspecified(UnicastAddress) ||
69  AddrIsEqual(&IF->Unicast, UnicastAddress)) &&
70  (AddrIsEqual(&IF->Broadcast, BroadcastAddress)))
71  return TRUE;
72  } EndFor(IF);
73 
74  return FALSE;
75 }
BOOLEAN AddrIsUnspecified(PIP_ADDRESS Address)
Definition: address.c:113
#define TRUE
Definition: types.h:120
#define IF_LIST_ITER(n)
Definition: tilists.h:5
#define FALSE
Definition: types.h:117
#define ForEachInterface(n)
Definition: tilists.h:9
BOOLEAN AddrIsEqual(PIP_ADDRESS Address1, PIP_ADDRESS Address2)
Definition: address.c:221
#define EndFor(n)
Definition: tilists.h:20

Referenced by AddrReceiveMatch().

◆ AddrReceiveMatch()

BOOLEAN AddrReceiveMatch ( PIP_ADDRESS  LocalAddress,
PIP_ADDRESS  RemoteAddress 
)

Definition at line 77 of file fileobjs.c.

80 {
82  {
83  /* Unicast address match */
84  return TRUE;
85  }
86 
88  {
89  /* Broadcast address match */
90  return TRUE;
91  }
92 
94  {
95  /* Local address unspecified */
96  return TRUE;
97  }
98 
100  {
101  /* Remote address unspecified */
102  return TRUE;
103  }
104 
105  return FALSE;
106 }
BOOLEAN AddrIsUnspecified(PIP_ADDRESS Address)
Definition: address.c:113
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
BOOLEAN AddrIsBroadcastMatch(PIP_ADDRESS UnicastAddress, PIP_ADDRESS BroadcastAddress)
Definition: fileobjs.c:62
BOOLEAN AddrIsEqual(PIP_ADDRESS Address1, PIP_ADDRESS Address2)
Definition: address.c:221
_Must_inspect_result_ _In_ ULONG _In_ PSOCKADDR _In_ PSOCKADDR RemoteAddress
Definition: wsk.h:170
_Must_inspect_result_ _In_ ULONG _In_ PSOCKADDR LocalAddress
Definition: wsk.h:170

Referenced by AddrSearchNext().

◆ AddrSearchFirst()

PADDRESS_FILE AddrSearchFirst ( PIP_ADDRESS  Address,
USHORT  Port,
USHORT  Protocol,
PAF_SEARCH  SearchContext 
)

Definition at line 38 of file fileobjs.c.

43 {
44  KIRQL OldIrql;
45 
46  SearchContext->Address = Address;
47  SearchContext->Port = Port;
48  SearchContext->Protocol = Protocol;
49 
51 
52  SearchContext->Next = AddressFileListHead.Flink;
53 
55  ReferenceObject(CONTAINING_RECORD(SearchContext->Next, ADDRESS_FILE, ListEntry));
56 
58 
59  return AddrSearchNext(SearchContext);
60 }
CPPORT Port[4]
Definition: headless.c:35
PLIST_ENTRY Next
Definition: titypes.h:184
_In_ USHORT _In_ ULONG Protocol
Definition: wsk.h:182
PIP_ADDRESS Address
Definition: titypes.h:185
VOID TcpipReleaseSpinLock(PKSPIN_LOCK SpinLock, KIRQL Irql)
Definition: lock.c:26
_Must_inspect_result_ FORCEINLINE BOOLEAN IsListEmpty(_In_ const LIST_ENTRY *ListHead)
Definition: rtlfuncs.h:57
VOID TcpipAcquireSpinLock(PKSPIN_LOCK SpinLock, PKIRQL Irql)
Definition: lock.c:18
UCHAR KIRQL
Definition: env_spec_w32.h:591
static WCHAR Address[46]
Definition: ping.c:68
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
KIRQL OldIrql
Definition: mm.h:1502
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
USHORT Protocol
Definition: titypes.h:187
#define ReferenceObject(Object)
Definition: titypes.h:14
LIST_ENTRY AddressFileListHead
Definition: fileobjs.c:21
KSPIN_LOCK AddressFileListLock
Definition: fileobjs.c:22
PADDRESS_FILE AddrSearchNext(PAF_SEARCH SearchContext)
Definition: fileobjs.c:231
USHORT Port
Definition: titypes.h:186

Referenced by RawIpReceive(), and UDPReceive().

◆ AddrSearchNext()

PADDRESS_FILE AddrSearchNext ( PAF_SEARCH  SearchContext)

Definition at line 231 of file fileobjs.c.

233 {
234  PLIST_ENTRY CurrentEntry;
235  PIP_ADDRESS IPAddress;
236  KIRQL OldIrql;
237  PADDRESS_FILE Current = NULL;
238  BOOLEAN Found = FALSE;
239  PADDRESS_FILE StartingAddrFile;
240 
242 
243  if (SearchContext->Next == &AddressFileListHead)
244  {
246  return NULL;
247  }
248 
249  /* Save this pointer so we can dereference it later */
250  StartingAddrFile = CONTAINING_RECORD(SearchContext->Next, ADDRESS_FILE, ListEntry);
251 
252  CurrentEntry = SearchContext->Next;
253 
254  while (CurrentEntry != &AddressFileListHead) {
255  Current = CONTAINING_RECORD(CurrentEntry, ADDRESS_FILE, ListEntry);
256 
257  IPAddress = &Current->Address;
258 
259  TI_DbgPrint(DEBUG_ADDRFILE, ("Comparing: ((%d, %d, %s), (%d, %d, %s)).\n",
260  WN2H(Current->Port),
261  Current->Protocol,
262  A2S(IPAddress),
263  WN2H(SearchContext->Port),
264  SearchContext->Protocol,
265  A2S(SearchContext->Address)));
266 
267  /* See if this address matches the search criteria */
268  if ((Current->Port == SearchContext->Port) &&
269  (Current->Protocol == SearchContext->Protocol) &&
270  (AddrReceiveMatch(IPAddress, SearchContext->Address))) {
271  /* We've found a match */
272  Found = TRUE;
273  break;
274  }
275  CurrentEntry = CurrentEntry->Flink;
276  }
277 
278  if (Found)
279  {
280  SearchContext->Next = CurrentEntry->Flink;
281 
282  if (SearchContext->Next != &AddressFileListHead)
283  {
284  /* Reference the next address file to prevent the link from disappearing behind our back */
285  ReferenceObject(CONTAINING_RECORD(SearchContext->Next, ADDRESS_FILE, ListEntry));
286  }
287 
288  /* Reference the returned address file before dereferencing the starting
289  * address file because it may be that Current == StartingAddrFile */
290  ReferenceObject(Current);
291  }
292  else
293  Current = NULL;
294 
295  DereferenceObject(StartingAddrFile);
296 
298 
299  return Current;
300 }
#define TI_DbgPrint(_t_, _x_)
Definition: debug.h:45
PLIST_ENTRY Next
Definition: titypes.h:184
#define TRUE
Definition: types.h:120
USHORT Protocol
Definition: titypes.h:119
PIP_ADDRESS Address
Definition: titypes.h:185
VOID TcpipReleaseSpinLock(PKSPIN_LOCK SpinLock, KIRQL Irql)
Definition: lock.c:26
VOID TcpipAcquireSpinLock(PKSPIN_LOCK SpinLock, PKIRQL Irql)
Definition: lock.c:18
BOOLEAN AddrReceiveMatch(PIP_ADDRESS LocalAddress, PIP_ADDRESS RemoteAddress)
Definition: fileobjs.c:77
#define DereferenceObject(Object)
Definition: titypes.h:24
Definition: ip.h:23
UCHAR KIRQL
Definition: env_spec_w32.h:591
#define FALSE
Definition: types.h:117
unsigned char BOOLEAN
return Found
Definition: dirsup.c:1270
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
KIRQL OldIrql
Definition: mm.h:1502
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
#define WN2H(w)
Definition: addrconv.c:35
USHORT Protocol
Definition: titypes.h:187
#define ReferenceObject(Object)
Definition: titypes.h:14
Definition: typedefs.h:119
LIST_ENTRY AddressFileListHead
Definition: fileobjs.c:21
KSPIN_LOCK AddressFileListLock
Definition: fileobjs.c:22
#define DEBUG_ADDRFILE
Definition: debug.h:23
USHORT Port
Definition: titypes.h:186
IP_ADDRESS Address
Definition: titypes.h:117
#define NULL
Definition: types.h:112
USHORT Port
Definition: titypes.h:120
PCHAR A2S(PIP_ADDRESS Address)
Definition: address.c:17

Referenced by AddrSearchFirst(), RawIpReceive(), and UDPReceive().

◆ ControlChannelFree()

VOID ControlChannelFree ( PVOID  Object)

Definition at line 376 of file fileobjs.c.

383 {
385 }
_Must_inspect_result_ _In_ WDFCOLLECTION _In_ WDFOBJECT Object
#define CONTROL_CHANNEL_TAG
Definition: tags.h:12
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099

Referenced by FileOpenControlChannel().

◆ FileCloseAddress()

NTSTATUS FileCloseAddress ( PTDI_REQUEST  Request)

Definition at line 588 of file fileobjs.c.

590 {
591  PADDRESS_FILE AddrFile = Request->Handle.AddressHandle;
592  PCONNECTION_ENDPOINT Listener;
593 
594  if (!Request->Handle.AddressHandle) return STATUS_INVALID_PARAMETER;
595 
596  LockObject(AddrFile);
597 
598  if (InterlockedDecrement(&AddrFile->Sharers) != 0)
599  {
600  /* Still other guys have open handles to this, so keep it around */
601  UnlockObject(AddrFile);
602  return STATUS_SUCCESS;
603  }
604 
605  /* We have to close this listener because we started it */
606  Listener = AddrFile->Listener;
607  UnlockObject(AddrFile);
608  if( Listener )
609  {
610  TCPClose( Listener );
611  }
612 
613  DereferenceObject(AddrFile);
614 
615  TI_DbgPrint(MAX_TRACE, ("Leaving.\n"));
616 
617  return STATUS_SUCCESS;
618 }
#define TI_DbgPrint(_t_, _x_)
Definition: debug.h:45
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
LONG Sharers
Definition: titypes.h:121
_In_ WDFREQUEST Request
Definition: wdfdevice.h:547
#define DereferenceObject(Object)
Definition: titypes.h:24
NTSTATUS TCPClose(PCONNECTION_ENDPOINT Connection)
Definition: tcp.c:177
#define InterlockedDecrement
Definition: armddk.h:52
struct _CONNECTION_ENDPOINT * Listener
Definition: titypes.h:133
#define UnlockObject(Object)
Definition: titypes.h:44
#define MAX_TRACE
Definition: debug.h:16
#define STATUS_SUCCESS
Definition: shellext.h:65
#define LockObject(Object)
Definition: titypes.h:34

Referenced by DispEchoRequest(), EndRequestHandler(), and TiCloseFileObject().

◆ FileCloseConnection()

NTSTATUS FileCloseConnection ( PTDI_REQUEST  Request)

Definition at line 664 of file fileobjs.c.

666 {
667  PCONNECTION_ENDPOINT Connection;
668 
669  TI_DbgPrint(MID_TRACE, ("Called.\n"));
670 
671  Connection = Request->Handle.ConnectionContext;
672 
673  if (!Connection) return STATUS_INVALID_PARAMETER;
674 
675  TCPClose( Connection );
676 
677  Request->Handle.ConnectionContext = NULL;
678 
679  TI_DbgPrint(MAX_TRACE, ("Leaving.\n"));
680 
681  return STATUS_SUCCESS;
682 }
#define TI_DbgPrint(_t_, _x_)
Definition: debug.h:45
#define MID_TRACE
Definition: debug.h:15
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
_In_ WDFREQUEST Request
Definition: wdfdevice.h:547
NTSTATUS TCPClose(PCONNECTION_ENDPOINT Connection)
Definition: tcp.c:177
#define MAX_TRACE
Definition: debug.h:16
#define NULL
Definition: types.h:112
#define STATUS_SUCCESS
Definition: shellext.h:65

Referenced by TiCloseFileObject().

◆ FileCloseControlChannel()

NTSTATUS FileCloseControlChannel ( PTDI_REQUEST  Request)

Definition at line 735 of file fileobjs.c.

737 {
738  if (!Request->Handle.ControlChannel) return STATUS_INVALID_PARAMETER;
739 
740  DereferenceObject((PCONTROL_CHANNEL)Request->Handle.ControlChannel);
741 
742  Request->Handle.ControlChannel = NULL;
743 
744  return STATUS_SUCCESS;
745 }
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
_In_ WDFREQUEST Request
Definition: wdfdevice.h:547
#define DereferenceObject(Object)
Definition: titypes.h:24
#define NULL
Definition: types.h:112
#define STATUS_SUCCESS
Definition: shellext.h:65

Referenced by TiCloseFileObject().

◆ FileOpenAddress()

NTSTATUS FileOpenAddress ( PTDI_REQUEST  Request,
PTA_IP_ADDRESS  Address,
USHORT  Protocol,
BOOLEAN  Shared,
PVOID  Options 
)

Definition at line 399 of file fileobjs.c.

405 {
406  PADDRESS_FILE AddrFile;
407 
408  TI_DbgPrint(MID_TRACE, ("Called (Proto %d).\n", Protocol));
409 
410  /* If it's shared and has a port specified, look for a match */
411  if ((Shared != FALSE) && (Address->Address[0].Address[0].sin_port != 0))
412  {
413  AddrFile = AddrFindShared(NULL, Address->Address[0].Address[0].sin_port, Protocol);
414  if (AddrFile != NULL)
415  {
416  Request->Handle.AddressHandle = AddrFile;
417  return STATUS_SUCCESS;
418  }
419  }
420 
421  AddrFile = ExAllocatePoolWithTag(NonPagedPool, sizeof(ADDRESS_FILE),
422  ADDR_FILE_TAG);
423  if (!AddrFile) {
424  TI_DbgPrint(MIN_TRACE, ("Insufficient resources.\n"));
426  }
427 
428  RtlZeroMemory(AddrFile, sizeof(ADDRESS_FILE));
429 
430  AddrFile->RefCount = 1;
431  AddrFile->Free = AddrFileFree;
432  AddrFile->Sharers = 1;
433 
434  /* Set our default options */
435  AddrFile->TTL = 128;
436  AddrFile->DF = 0;
437  AddrFile->BCast = 1;
438  AddrFile->HeaderIncl = 1;
439  AddrFile->ProcessId = PsGetCurrentProcessId();
440 
441  _SEH2_TRY {
442  PTEB Teb;
443 
444  Teb = PsGetCurrentThreadTeb();
445  if (Teb != NULL)
446  AddrFile->SubProcessTag = Teb->SubProcessTag;
448  AddrFile->SubProcessTag = 0;
449  } _SEH2_END;
450 
451  KeQuerySystemTime(&AddrFile->CreationTime);
452 
453  /* Make sure address is a local unicast address or 0 */
454  /* FIXME: IPv4 only */
455  AddrFile->Family = Address->Address[0].AddressType;
456  AddrFile->Address.Address.IPv4Address = Address->Address[0].Address[0].in_addr;
457  AddrFile->Address.Type = IP_ADDRESS_V4;
458 
459  if (!AddrIsUnspecified(&AddrFile->Address) &&
460  !AddrLocateInterface(&AddrFile->Address)) {
461  TI_DbgPrint(MIN_TRACE, ("Non-local address given (0x%X).\n", A2S(&AddrFile->Address)));
462  ExFreePoolWithTag(AddrFile, ADDR_FILE_TAG);
463  return STATUS_INVALID_ADDRESS;
464  }
465 
466  TI_DbgPrint(MID_TRACE, ("Opening address %s for communication (P=%d U=%d).\n",
467  A2S(&AddrFile->Address), Protocol, IPPROTO_UDP));
468 
469  /* Protocol specific handling */
470  switch (Protocol) {
471  case IPPROTO_TCP:
472  if (Address->Address[0].Address[0].sin_port)
473  {
474  /* The client specified an explicit port so we force a bind to this */
475  AddrFile->Port = TCPAllocatePort(Address->Address[0].Address[0].sin_port);
476 
477  /* Check for bind success */
478  if (AddrFile->Port == 0xffff)
479  {
480  ExFreePoolWithTag(AddrFile, ADDR_FILE_TAG);
482  }
483 
484  /* Sanity check */
485  ASSERT(Address->Address[0].Address[0].sin_port == AddrFile->Port);
486  }
487  else if (!AddrIsUnspecified(&AddrFile->Address))
488  {
489  /* The client is trying to bind to a local address so allocate a port now too */
490  AddrFile->Port = TCPAllocatePort(0);
491 
492  /* Check for bind success */
493  if (AddrFile->Port == 0xffff)
494  {
495  ExFreePoolWithTag(AddrFile, ADDR_FILE_TAG);
497  }
498  }
499  else
500  {
501  /* The client wants an unspecified port with an unspecified address so we wait to see what the TCP library gives us */
502  AddrFile->Port = 0;
503  }
504 
505  AddEntity(CO_TL_ENTITY, AddrFile, CO_TL_TCP);
506 
507  AddrFile->Send = NULL; /* TCPSendData */
508  break;
509 
510  case IPPROTO_UDP:
511  TI_DbgPrint(MID_TRACE,("Allocating udp port\n"));
512  AddrFile->Port =
513  UDPAllocatePort(Address->Address[0].Address[0].sin_port);
514 
515  if ((Address->Address[0].Address[0].sin_port &&
516  AddrFile->Port != Address->Address[0].Address[0].sin_port) ||
517  AddrFile->Port == 0xffff)
518  {
519  ExFreePoolWithTag(AddrFile, ADDR_FILE_TAG);
521  }
522 
523  TI_DbgPrint(MID_TRACE,("Setting port %d (wanted %d)\n",
524  AddrFile->Port,
525  Address->Address[0].Address[0].sin_port));
526 
527  AddEntity(CL_TL_ENTITY, AddrFile, CL_TL_UDP);
528 
529  AddrFile->Send = UDPSendDatagram;
530  break;
531 
532  case IPPROTO_ICMP:
533  AddrFile->Port = 0;
534  AddrFile->Send = ICMPSendDatagram;
535 
536  /* FIXME: Verify this */
537  AddEntity(ER_ENTITY, AddrFile, ER_ICMP);
538  break;
539 
540  default:
541  /* Use raw IP for all other protocols */
542  AddrFile->Port = 0;
543  AddrFile->Send = RawIPSendDatagram;
544 
545  /* FIXME: Verify this */
546  AddEntity(CL_TL_ENTITY, AddrFile, 0);
547  break;
548  }
549 
550  TI_DbgPrint(MID_TRACE, ("IP protocol number for address file object is %d.\n",
551  Protocol));
552 
553  TI_DbgPrint(MID_TRACE, ("Port number for address file object is %d.\n",
554  WN2H(AddrFile->Port)));
555 
556  /* Set protocol */
557  AddrFile->Protocol = Protocol;
558 
559  /* Initialize receive and transmit queues */
560  InitializeListHead(&AddrFile->ReceiveQueue);
561  InitializeListHead(&AddrFile->TransmitQueue);
562 
563  /* Initialize spin lock that protects the address file object */
565 
566  /* Return address file object */
567  Request->Handle.AddressHandle = AddrFile;
568 
569  /* Add address file to global list */
572  &AddrFile->ListEntry,
574 
575  TI_DbgPrint(MAX_TRACE, ("Leaving.\n"));
576 
577  return STATUS_SUCCESS;
578 }
#define KeQuerySystemTime(t)
Definition: env_spec_w32.h:570
BOOLEAN AddrIsUnspecified(PIP_ADDRESS Address)
Definition: address.c:113
PADDRESS_FILE AddrFindShared(PIP_ADDRESS BindAddress, USHORT Port, USHORT Protocol)
Definition: fileobjs.c:190
#define TI_DbgPrint(_t_, _x_)
Definition: debug.h:45
IPv4_RAW_ADDRESS IPv4Address
Definition: ip.h:26
NTSTATUS ICMPSendDatagram(PADDRESS_FILE AddrFile, PTDI_CONNECTION_INFORMATION ConnInfo, PCHAR BufferData, ULONG DataSize, PULONG DataUsed)
Definition: icmp.c:29
UINT UDPAllocatePort(UINT HintPort)
Definition: udp.c:384
#define MID_TRACE
Definition: debug.h:15
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
UCHAR TTL
Definition: titypes.h:122
union IP_ADDRESS::@974 Address
DATAGRAM_SEND_ROUTINE Send
Definition: titypes.h:128
LONG Sharers
Definition: titypes.h:121
LONG RefCount
Definition: titypes.h:114
USHORT Protocol
Definition: titypes.h:119
_In_ USHORT _In_ ULONG Protocol
Definition: wsk.h:182
NTSTATUS ExInitializeResourceLite(PULONG res)
Definition: env_spec_w32.h:641
_In_ WDFREQUEST Request
Definition: wdfdevice.h:547
HANDLE ProcessId
Definition: titypes.h:137
#define STATUS_ADDRESS_ALREADY_EXISTS
Definition: ntstatus.h:654
#define ADDR_FILE_TAG
Definition: tags.h:11
_SEH2_TRY
Definition: create.c:4226
#define FALSE
Definition: types.h:117
#define IP_ADDRESS_V4
Definition: ip.h:32
ERESOURCE Resource
Definition: titypes.h:116
static WCHAR Address[46]
Definition: ping.c:68
#define CO_TL_ENTITY
Definition: tdiinfo.h:45
NTSTATUS UDPSendDatagram(PADDRESS_FILE AddrFile, PTDI_CONNECTION_INFORMATION ConnInfo, PCHAR BufferData, ULONG DataSize, PULONG DataUsed)
Definition: udp.c:150
UINT TCPAllocatePort(const UINT HintPort)
Definition: tcp.c:623
#define ASSERT(a)
Definition: mode.c:44
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
PLIST_ENTRY NTAPI ExInterlockedInsertTailList(IN OUT PLIST_ENTRY ListHead, IN OUT PLIST_ENTRY ListEntry, IN OUT PKSPIN_LOCK Lock)
Definition: interlocked.c:140
#define ER_ICMP
Definition: tdiinfo.h:59
NTSTATUS RawIPSendDatagram(PADDRESS_FILE AddrFile, PTDI_CONNECTION_INFORMATION ConnInfo, PCHAR Buffer, ULONG DataSize, PULONG DataUsed)
Definition: rawip.c:171
PVOID NTAPI PsGetCurrentThreadTeb(VOID)
Definition: thread.c:785
#define WN2H(w)
Definition: addrconv.c:35
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
VOID AddrFileFree(PVOID Object)
Definition: fileobjs.c:302
UINT HeaderIncl
Definition: titypes.h:125
LIST_ENTRY AddressFileListHead
Definition: fileobjs.c:21
KSPIN_LOCK AddressFileListLock
Definition: fileobjs.c:22
LIST_ENTRY ListEntry
Definition: titypes.h:113
PVOID SubProcessTag
Definition: titypes.h:138
#define CL_TL_ENTITY
Definition: tdiinfo.h:43
Definition: compat.h:694
UCHAR Type
Definition: ip.h:24
_SEH2_END
Definition: create.c:4400
PIP_INTERFACE AddrLocateInterface(PIP_ADDRESS MatchAddress)
Definition: interface.c:89
#define MAX_TRACE
Definition: debug.h:16
#define STATUS_INVALID_ADDRESS
Definition: ntstatus.h:557
IP_ADDRESS Address
Definition: titypes.h:117
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
UINT BCast
Definition: titypes.h:124
#define ER_ENTITY
Definition: tdiinfo.h:46
HANDLE NTAPI PsGetCurrentProcessId(VOID)
Definition: process.c:1123
#define NULL
Definition: types.h:112
OBJECT_FREE_ROUTINE Free
Definition: titypes.h:115
VOID AddEntity(ULONG EntityType, PVOID Context, ULONG Flags)
Definition: info.c:15
LIST_ENTRY TransmitQueue
Definition: titypes.h:130
USHORT Family
Definition: titypes.h:118
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
#define MIN_TRACE
Definition: debug.h:14
LIST_ENTRY ReceiveQueue
Definition: titypes.h:129
USHORT Port
Definition: titypes.h:120
#define CL_TL_UDP
Definition: tdiinfo.h:52
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:40
#define STATUS_SUCCESS
Definition: shellext.h:65
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
#define CO_TL_TCP
Definition: tdiinfo.h:57
PCHAR A2S(PIP_ADDRESS Address)
Definition: address.c:17
LARGE_INTEGER CreationTime
Definition: titypes.h:139

Referenced by DispEchoRequest(), and TiCreateFileObject().

◆ FileOpenConnection()

NTSTATUS FileOpenConnection ( PTDI_REQUEST  Request,
PVOID  ClientContext 
)

Definition at line 629 of file fileobjs.c.

632 {
634  PCONNECTION_ENDPOINT Connection;
635 
636  TI_DbgPrint(MID_TRACE, ("Called.\n"));
637 
639 
640  if( !Connection ) return STATUS_NO_MEMORY;
641 
642  Status = TCPSocket( Connection, AF_INET, SOCK_STREAM, IPPROTO_TCP );
643 
644  if( !NT_SUCCESS(Status) ) {
645  DereferenceObject( Connection );
646  return Status;
647  }
648 
649  /* Return connection endpoint file object */
650  Request->Handle.ConnectionContext = Connection;
651 
652  TI_DbgPrint(MAX_TRACE, ("Leaving.\n"));
653 
654  return STATUS_SUCCESS;
655 }
#define TI_DbgPrint(_t_, _x_)
Definition: debug.h:45
#define MID_TRACE
Definition: debug.h:15
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS TCPSocket(PCONNECTION_ENDPOINT Connection, UINT Family, UINT Type, UINT Proto)
Definition: tcp.c:153
_In_ WDFREQUEST Request
Definition: wdfdevice.h:547
#define DereferenceObject(Object)
Definition: titypes.h:24
Status
Definition: gdiplustypes.h:24
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
_In_ PVOID ClientContext
Definition: netioddk.h:55
#define MAX_TRACE
Definition: debug.h:16
#define STATUS_NO_MEMORY
Definition: ntstatus.h:260
#define AF_INET
Definition: tcpip.h:117
#define STATUS_SUCCESS
Definition: shellext.h:65
#define SOCK_STREAM
Definition: tcpip.h:118
PCONNECTION_ENDPOINT TCPAllocateConnectionEndpoint(PVOID ClientContext)
Definition: tcp.c:107

Referenced by TiCreateFileObject().

◆ FileOpenControlChannel()

NTSTATUS FileOpenControlChannel ( PTDI_REQUEST  Request)

Definition at line 691 of file fileobjs.c.

693 {
694  PCONTROL_CHANNEL ControlChannel;
695  TI_DbgPrint(MID_TRACE, ("Called.\n"));
696 
697  ControlChannel = ExAllocatePoolWithTag(NonPagedPool, sizeof(*ControlChannel),
699 
700  if (!ControlChannel) {
701  TI_DbgPrint(MIN_TRACE, ("Insufficient resources.\n"));
703  }
704 
705  RtlZeroMemory(ControlChannel, sizeof(CONTROL_CHANNEL));
706 
707  /* Make sure address is a local unicast address or 0 */
708 
709  /* Locate address entry. If specified address is 0, a random address is chosen */
710 
711  /* Initialize receive and transmit queues */
712  InitializeListHead(&ControlChannel->ListEntry);
713 
714  /* Initialize spin lock that protects the address file object */
715  KeInitializeSpinLock(&ControlChannel->Lock);
716 
717  ControlChannel->RefCount = 1;
718  ControlChannel->Free = ControlChannelFree;
719 
720  /* Return address file object */
721  Request->Handle.ControlChannel = ControlChannel;
722 
723  TI_DbgPrint(MAX_TRACE, ("Leaving.\n"));
724 
725  return STATUS_SUCCESS;
726 }
#define TI_DbgPrint(_t_, _x_)
Definition: debug.h:45
#define MID_TRACE
Definition: debug.h:15
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
VOID ControlChannelFree(PVOID Object)
Definition: fileobjs.c:376
OBJECT_FREE_ROUTINE Free
Definition: titypes.h:283
_In_ WDFREQUEST Request
Definition: wdfdevice.h:547
FORCEINLINE VOID KeInitializeSpinLock(_Out_ PKSPIN_LOCK SpinLock)
Definition: kefuncs.h:238
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
LIST_ENTRY ListEntry
Definition: titypes.h:281
#define MAX_TRACE
Definition: debug.h:16
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
#define CONTROL_CHANNEL_TAG
Definition: tags.h:12
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
#define MIN_TRACE
Definition: debug.h:14
#define STATUS_SUCCESS
Definition: shellext.h:65
KSPIN_LOCK Lock
Definition: titypes.h:284

Referenced by TiCreateFileObject().

◆ LogActiveObjects()

VOID LogActiveObjects ( VOID  )

Definition at line 109 of file fileobjs.c.

110 {
111 #ifdef LOG_OBJECTS
112  PLIST_ENTRY CurrentEntry;
113  KIRQL OldIrql;
114  PADDRESS_FILE AddrFile;
116 
117  DbgPrint("----------- TCP/IP Active Object Dump -------------\n");
118 
120 
121  CurrentEntry = AddressFileListHead.Flink;
122  while (CurrentEntry != &AddressFileListHead)
123  {
124  AddrFile = CONTAINING_RECORD(CurrentEntry, ADDRESS_FILE, ListEntry);
125 
126  DbgPrint("Address File (%s, %d, %d) @ 0x%p | Ref count: %d | Sharers: %d\n",
127  A2S(&AddrFile->Address), WN2H(AddrFile->Port), AddrFile->Protocol,
128  AddrFile, AddrFile->RefCount, AddrFile->Sharers);
129  DbgPrint("\tListener: ");
130  if (AddrFile->Listener == NULL)
131  DbgPrint("<None>\n");
132  else
133  DbgPrint("0x%p\n", AddrFile->Listener);
134  DbgPrint("\tAssociated endpoints: ");
135  if (AddrFile->Connection == NULL)
136  DbgPrint("<None>\n");
137  else
138  {
139  Conn = AddrFile->Connection;
140  while (Conn)
141  {
142  DbgPrint("0x%p ", Conn);
143  Conn = Conn->Next;
144  }
145  DbgPrint("\n");
146  }
147 
148  CurrentEntry = CurrentEntry->Flink;
149  }
150 
152 
154 
155  CurrentEntry = ConnectionEndpointListHead.Flink;
156  while (CurrentEntry != &ConnectionEndpointListHead)
157  {
158  Conn = CONTAINING_RECORD(CurrentEntry, CONNECTION_ENDPOINT, ListEntry);
159 
160  DbgPrint("Connection @ 0x%p | Ref count: %d\n", Conn, Conn->RefCount);
161  DbgPrint("\tPCB: ");
162  if (Conn->SocketContext == NULL)
163  DbgPrint("<None>\n");
164  else
165  {
166  DbgPrint("0x%p\n", Conn->SocketContext);
168  }
169  DbgPrint("\tPacket queue status: %s\n", IsListEmpty(&Conn->PacketQueue) ? "Empty" : "Not Empty");
170  DbgPrint("\tRequest lists: Connect: %s | Recv: %s | Send: %s | Shutdown: %s | Listen: %s\n",
171  IsListEmpty(&Conn->ConnectRequest) ? "Empty" : "Not Empty",
172  IsListEmpty(&Conn->ReceiveRequest) ? "Empty" : "Not Empty",
173  IsListEmpty(&Conn->SendRequest) ? "Empty" : "Not Empty",
174  IsListEmpty(&Conn->ShutdownRequest) ? "Empty" : "Not Empty",
175  IsListEmpty(&Conn->ListenRequest) ? "Empty" : "Not Empty");
176  DbgPrint("\tSend shutdown: %s\n", Conn->SendShutdown ? "Yes" : "No");
177  DbgPrint("\tReceive shutdown: %s\n", Conn->ReceiveShutdown ? "Yes" : "No");
178  if (Conn->ReceiveShutdown) DbgPrint("\tReceive shutdown status: 0x%x\n", Conn->ReceiveShutdownStatus);
179  DbgPrint("\tClosing: %s\n", Conn->Closing ? "Yes" : "No");
180 
181  CurrentEntry = CurrentEntry->Flink;
182  }
183 
185 
186  DbgPrint("---------------------------------------------------\n");
187 #endif
188 }
#define DbgPrint
Definition: hal.h:12
LONG Sharers
Definition: titypes.h:121
LONG RefCount
Definition: titypes.h:114
USHORT Protocol
Definition: titypes.h:119
NTSTATUS ReceiveShutdownStatus
Definition: titypes.h:266
VOID TcpipReleaseSpinLock(PKSPIN_LOCK SpinLock, KIRQL Irql)
Definition: lock.c:26
_Must_inspect_result_ FORCEINLINE BOOLEAN IsListEmpty(_In_ const LIST_ENTRY *ListHead)
Definition: rtlfuncs.h:57
VOID TcpipAcquireSpinLock(PKSPIN_LOCK SpinLock, PKIRQL Irql)
Definition: lock.c:18
LIST_ENTRY PacketQueue
Definition: titypes.h:256
struct _CONNECTION_ENDPOINT * Next
Definition: titypes.h:269
UCHAR KIRQL
Definition: env_spec_w32.h:591
BOOLEAN SendShutdown
Definition: titypes.h:264
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
KIRQL OldIrql
Definition: mm.h:1502
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
#define WN2H(w)
Definition: addrconv.c:35
LIST_ENTRY ShutdownRequest
Definition: titypes.h:254
LIST_ENTRY ReceiveRequest
Definition: titypes.h:252
Definition: typedefs.h:119
struct _CONNECTION_ENDPOINT * Listener
Definition: titypes.h:133
LIST_ENTRY AddressFileListHead
Definition: fileobjs.c:21
KSPIN_LOCK AddressFileListLock
Definition: fileobjs.c:22
void LibTCPDumpPcb(PVOID SocketContext)
Definition: rostcp.c:39
BOOLEAN ReceiveShutdown
Definition: titypes.h:265
IP_ADDRESS Address
Definition: titypes.h:117
LIST_ENTRY ConnectRequest
Definition: titypes.h:250
LIST_ENTRY ListenRequest
Definition: titypes.h:251
#define NULL
Definition: types.h:112
LIST_ENTRY SendRequest
Definition: titypes.h:253
KSPIN_LOCK ConnectionEndpointListLock
Definition: fileobjs.c:26
struct _CONNECTION_ENDPOINT * Connection
Definition: titypes.h:131
USHORT Port
Definition: titypes.h:120
PCHAR A2S(PIP_ADDRESS Address)
Definition: address.c:17
LIST_ENTRY ConnectionEndpointListHead
Definition: fileobjs.c:25

Referenced by IPTimeoutDpcFn().

Variable Documentation

◆ AddressFileListHead

◆ AddressFileListLock

◆ ConnectionEndpointListHead

LIST_ENTRY ConnectionEndpointListHead

Definition at line 25 of file fileobjs.c.

Referenced by DriverEntry(), LogActiveObjects(), and TCPAllocateConnectionEndpoint().

◆ ConnectionEndpointListLock

KSPIN_LOCK ConnectionEndpointListLock