Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

241 lines
5.6 KiB

  1. /*++
  2. Copyright (c) 1989-1993 Microsoft Corporation
  3. Module Name:
  4. spxdev.c
  5. Abstract:
  6. This module contains code which implements the DEVICE_CONTEXT object.
  7. Routines are provided to reference, and dereference transport device
  8. context objects.
  9. The transport device context object is a structure which contains a
  10. system-defined DEVICE_OBJECT followed by information which is maintained
  11. by the transport provider, called the context.
  12. Author:
  13. Nikhil Kamkolkar (nikhilk) 11-November-1993
  14. Environment:
  15. Kernel mode
  16. Revision History:
  17. --*/
  18. #include "precomp.h"
  19. #pragma hdrstop
  20. // Define module number for event logging entries
  21. #define FILENUM SPXDEV
  22. #ifdef ALLOC_PRAGMA
  23. #pragma alloc_text(INIT, SpxInitCreateDevice)
  24. #pragma alloc_text(PAGE, SpxDestroyDevice)
  25. #endif
  26. #ifndef __PREFAST__
  27. #pragma warning(disable:4068)
  28. #endif
  29. #pragma prefast(disable:276, "The assignments are harmless")
  30. VOID
  31. SpxDerefDevice(
  32. IN PDEVICE Device
  33. )
  34. /*++
  35. Routine Description:
  36. This routine dereferences a device context by decrementing the
  37. reference count contained in the structure. Currently, we don't
  38. do anything special when the reference count drops to zero, but
  39. we could dynamically unload stuff then.
  40. Arguments:
  41. Device - Pointer to a transport device context object.
  42. Return Value:
  43. none.
  44. --*/
  45. {
  46. LONG result;
  47. result = InterlockedDecrement (&Device->dev_RefCount);
  48. CTEAssert (result >= 0);
  49. if (result == 0)
  50. {
  51. // Close binding to IPX
  52. SpxUnbindFromIpx();
  53. // Set unload event.
  54. KeSetEvent(&SpxUnloadEvent, IO_NETWORK_INCREMENT, FALSE);
  55. }
  56. } // SpxDerefDevice
  57. NTSTATUS
  58. SpxInitCreateDevice(
  59. IN PDRIVER_OBJECT DriverObject,
  60. IN PUNICODE_STRING DeviceName
  61. )
  62. /*++
  63. Routine Description:
  64. This routine creates and initializes a device context structure.
  65. Arguments:
  66. DriverObject - pointer to the IO subsystem supplied driver object.
  67. Device - Pointer to a pointer to a transport device context object.
  68. DeviceName - pointer to the name of the device this device object points to.
  69. Return Value:
  70. STATUS_SUCCESS if all is well; STATUS_INSUFFICIENT_RESOURCES otherwise.
  71. --*/
  72. {
  73. NTSTATUS status;
  74. PDEVICE Device;
  75. ULONG DeviceNameOffset;
  76. DBGPRINT(DEVICE, INFO,
  77. ("SpxInitCreateDevice - Create device %ws\n", DeviceName->Buffer));
  78. // Create the device object for the sample transport, allowing
  79. // room at the end for the device name to be stored (for use
  80. // in logging errors).
  81. SpxDevice = SpxAllocateMemory(sizeof (DEVICE) + DeviceName->Length + sizeof(UNICODE_NULL));
  82. if (!SpxDevice) {
  83. DbgPrint("SPX: FATAL Error: cant allocate Device Structure\n");
  84. return STATUS_INSUFFICIENT_RESOURCES;
  85. }
  86. Device = (PDEVICE)SpxDevice;
  87. RtlZeroMemory(SpxDevice, sizeof (DEVICE) + DeviceName->Length + sizeof(UNICODE_NULL) );
  88. //
  89. // This is the closest we can set the provider info [ShreeM]
  90. //
  91. SpxQueryInitProviderInfo(&Device->dev_ProviderInfo);
  92. DBGPRINT(DEVICE, INFO, ("IoCreateDevice succeeded %lx\n", Device));
  93. // Initialize our part of the device context.
  94. RtlZeroMemory(
  95. ((PUCHAR)Device) + sizeof(DEVICE_OBJECT),
  96. sizeof(DEVICE) - sizeof(DEVICE_OBJECT));
  97. DeviceNameOffset = sizeof(DEVICE);
  98. // Copy over the device name.
  99. Device->dev_DeviceNameLen = DeviceName->Length + sizeof(WCHAR);
  100. Device->dev_DeviceName = (PWCHAR)(((PUCHAR)Device) + DeviceNameOffset);
  101. RtlCopyMemory(
  102. Device->dev_DeviceName,
  103. DeviceName->Buffer,
  104. DeviceName->Length);
  105. Device->dev_DeviceName[DeviceName->Length/sizeof(WCHAR)] = UNICODE_NULL;
  106. // Initialize the reference count.
  107. Device->dev_RefCount = 1;
  108. #if DBG
  109. Device->dev_RefTypes[DREF_CREATE] = 1;
  110. #endif
  111. #if DBG
  112. RtlCopyMemory(Device->dev_Signature1, "IDC1", 4);
  113. RtlCopyMemory(Device->dev_Signature2, "IDC2", 4);
  114. #endif
  115. // Set next conn id to be used.
  116. Device->dev_NextConnId = (USHORT)SpxRandomNumber();
  117. if (Device->dev_NextConnId == 0xFFFF)
  118. {
  119. Device->dev_NextConnId = 1;
  120. }
  121. DBGPRINT(DEVICE, ERR,
  122. ("SpxInitCreateDevice: Start Conn Id %lx\n", Device->dev_NextConnId));
  123. // Initialize the resource that guards address ACLs.
  124. ExInitializeResourceLite (&Device->dev_AddrResource);
  125. // initialize the various fields in the device context
  126. CTEInitLock (&Device->dev_Interlock);
  127. CTEInitLock (&Device->dev_Lock);
  128. KeInitializeSpinLock (&Device->dev_StatInterlock);
  129. KeInitializeSpinLock (&Device->dev_StatSpinLock);
  130. Device->dev_State = DEVICE_STATE_CLOSED;
  131. Device->dev_Type = SPX_DEVICE_SIGNATURE;
  132. Device->dev_Size = sizeof (DEVICE);
  133. Device->dev_Stat.Version = 0x100;
  134. return STATUS_SUCCESS;
  135. } // SpxCreateDevice
  136. VOID
  137. SpxDestroyDevice(
  138. IN PDEVICE Device
  139. )
  140. /*++
  141. Routine Description:
  142. This routine destroys a device context structure.
  143. Arguments:
  144. Device - Pointer to a pointer to a transport device context object.
  145. Return Value:
  146. None.
  147. --*/
  148. {
  149. ExDeleteResourceLite (&Device->dev_AddrResource);
  150. IoDeleteDevice ((PDEVICE_OBJECT)SpxDevice->dev_DevObj);
  151. SpxFreeMemory(SpxDevice);
  152. } // SpxDestroyDevice