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.

532 lines
15 KiB

  1. //--------------------------------------------------------------------
  2. // Copyright (c)1998 Microsoft Corporation, All Rights Reserved.
  3. //
  4. // io.h
  5. //
  6. // Author:
  7. //
  8. // Edward Reus (edwardr) 02-24-98 Initial coding.
  9. //
  10. //--------------------------------------------------------------------
  11. #ifndef _IO_H_
  12. #define _IO_H_
  13. // Different debug flags for various aspects of the code:
  14. #ifdef DBG
  15. // #define DBG_ERROR
  16. // #define DBG_IO
  17. // #define DBG_TARGET_DIR
  18. // #define DBG_MEM
  19. // #define DBG_MEM_VALIDATE
  20. // #define DBG_ASSEMBLE
  21. // #define DBG_DATE
  22. // #define DBG_REGISTRY
  23. // #define DBG_RETURN_STATUS
  24. // #define DBG_IMPERSONATE
  25. // #define DBG_PROPERTIES
  26. #endif
  27. // For NT we want to use IO Completion ports, for Windows98 we can't:
  28. // #define USE_IOCOMPLETION
  29. #define SYNCHRONOUS_FILES
  30. #ifndef NTENV
  31. #define DbgPrint printf
  32. #undef ASSERT
  33. #define ASSERT(x)
  34. #ifndef NTSTATUS
  35. #define NTSTATUS DWORD
  36. #endif
  37. #ifndef NT_SUCCESS
  38. #define NT_SUCCESS(x) ((x)==0)
  39. #endif
  40. #define RTL_CRITICAL_SECTION CRITICAL_SECTION
  41. NTSTATUS
  42. RtlInitializeCriticalSection( IN OUT RTL_CRITICAL_SECTION *pcs );
  43. NTSTATUS
  44. RtlDeleteCriticalSection( IN OUT RTL_CRITICAL_SECTION *pcs );
  45. NTSTATUS
  46. RtlEnterCriticalSection( IN OUT RTL_CRITICAL_SECTION *pcs );
  47. NTSTATUS
  48. RtlLeaveCriticalSection( IN OUT RTL_CRITICAL_SECTION *pcs );
  49. // Doubly-linked list manipulation routines. Implemented as macros
  50. // but logically these are procedures.
  51. //
  52. //
  53. // VOID
  54. // InitializeListHead(
  55. // PLIST_ENTRY ListHead
  56. // );
  57. //
  58. #define InitializeListHead(ListHead) (\
  59. (ListHead)->Flink = (ListHead)->Blink = (ListHead))
  60. //
  61. // BOOLEAN
  62. // IsListEmpty(
  63. // PLIST_ENTRY ListHead
  64. // );
  65. //
  66. #define IsListEmpty(ListHead) \
  67. ((ListHead)->Flink == (ListHead))
  68. //
  69. // PLIST_ENTRY
  70. // RemoveHeadList(
  71. // PLIST_ENTRY ListHead
  72. // );
  73. //
  74. #define RemoveHeadList(ListHead) \
  75. (ListHead)->Flink;\
  76. {RemoveEntryList((ListHead)->Flink)}
  77. //
  78. // PLIST_ENTRY
  79. // RemoveTailList(
  80. // PLIST_ENTRY ListHead
  81. // );
  82. //
  83. #define RemoveTailList(ListHead) \
  84. (ListHead)->Blink;\
  85. {RemoveEntryList((ListHead)->Blink)}
  86. //
  87. // VOID
  88. // RemoveEntryList(
  89. // PLIST_ENTRY Entry
  90. // );
  91. //
  92. #define RemoveEntryList(Entry) {\
  93. PLIST_ENTRY _EX_Blink;\
  94. PLIST_ENTRY _EX_Flink;\
  95. _EX_Flink = (Entry)->Flink;\
  96. _EX_Blink = (Entry)->Blink;\
  97. _EX_Blink->Flink = _EX_Flink;\
  98. _EX_Flink->Blink = _EX_Blink;\
  99. }
  100. //
  101. // VOID
  102. // InsertTailList(
  103. // PLIST_ENTRY ListHead,
  104. // PLIST_ENTRY Entry
  105. // );
  106. //
  107. #define InsertTailList(ListHead,Entry) {\
  108. PLIST_ENTRY _EX_Blink;\
  109. PLIST_ENTRY _EX_ListHead;\
  110. _EX_ListHead = (ListHead);\
  111. _EX_Blink = _EX_ListHead->Blink;\
  112. (Entry)->Flink = _EX_ListHead;\
  113. (Entry)->Blink = _EX_Blink;\
  114. _EX_Blink->Flink = (Entry);\
  115. _EX_ListHead->Blink = (Entry);\
  116. }
  117. //
  118. // VOID
  119. // InsertHeadList(
  120. // PLIST_ENTRY ListHead,
  121. // PLIST_ENTRY Entry
  122. // );
  123. //
  124. #define InsertHeadList(ListHead,Entry) {\
  125. PLIST_ENTRY _EX_Flink;\
  126. PLIST_ENTRY _EX_ListHead;\
  127. _EX_ListHead = (ListHead);\
  128. _EX_Flink = _EX_ListHead->Flink;\
  129. (Entry)->Flink = _EX_Flink;\
  130. (Entry)->Blink = _EX_ListHead;\
  131. _EX_Flink->Blink = (Entry);\
  132. _EX_ListHead->Flink = (Entry);\
  133. }
  134. //
  135. //
  136. // PSINGLE_LIST_ENTRY
  137. // PopEntryList(
  138. // PSINGLE_LIST_ENTRY ListHead
  139. // );
  140. //
  141. #define PopEntryList(ListHead) \
  142. (ListHead)->Next;\
  143. {\
  144. PSINGLE_LIST_ENTRY FirstEntry;\
  145. FirstEntry = (ListHead)->Next;\
  146. if (FirstEntry != NULL) { \
  147. (ListHead)->Next = FirstEntry->Next;\
  148. } \
  149. }
  150. //
  151. // VOID
  152. // PushEntryList(
  153. // PSINGLE_LIST_ENTRY ListHead,
  154. // PSINGLE_LIST_ENTRY Entry
  155. // );
  156. //
  157. #define PushEntryList(ListHead,Entry) \
  158. (Entry)->Next = (ListHead)->Next; \
  159. (ListHead)->Next = (Entry)
  160. #endif
  161. // Maximum number of "Copy XX of" files to create:
  162. #define MAX_COPYOF_TRIES 100
  163. // CIOPACKET objects can have the following kinds:
  164. #define PACKET_KIND_LISTEN 0
  165. #define PACKET_KIND_READ 1
  166. #define PACKET_KIND_WRITE_SOCKET 2
  167. #define PACKET_KIND_WRITE_FILE 3
  168. // The number of pending IOs depends on what you are doing:
  169. #define MAX_PENDING_LISTEN 1
  170. #define MAX_PENDING_READ 2
  171. #define MAX_PENDING_WRITE 3
  172. // This dwKey value for the key in IO completion is a special
  173. // value used to shutdown the IrTran-P thread:
  174. #define IOKEY_SHUTDOWN 0xFFFFFFFF
  175. // This is the default size for the read buffer in IO reads
  176. // posted to the IO completion port:
  177. #define DEFAULT_READ_BUFFER_SIZE 4096
  178. // Used in setting up the IrCOMM listen socket:
  179. #define IAS_SET_ATTRIB_MAX_LEN 32
  180. #define IAS_QUERY_ATTRIB_MAX_LEN IAS_SET_ATTRIB_MAX_LEN
  181. #define IRDA_PARAMETERS "Parameters"
  182. #define OCTET_SEQ_SIZE 6
  183. #define OCTET_SEQ "\000\001\006\001\001\001"
  184. // The names of the services we will provide listen sockets for:
  185. #define IRTRANP_SERVICE "IrTranPv1"
  186. #define IRCOMM_9WIRE "IrDA:IrCOMM"
  187. // The status of the listen socket for each service:
  188. #define STATUS_STOPPED 0
  189. #define STATUS_RUNNING 1
  190. // Registry paths and value names:
  191. #define REG_PATH_HKCU "Control Panel\\Infrared\\IrTranP"
  192. #define REG_DWORD_SAVE_AS_UPF "SaveAsUPF"
  193. #define REG_DWORD_DISABLE_IRTRANP "DisableIrTranPv1"
  194. #define REG_DWORD_DISABLE_IRCOMM "DisableIrCOMM"
  195. #define REG_DWORD_EXPLORE "ExploreOnCompletion"
  196. #define REG_SZ_DESTINATION "RecvdFileLocation"
  197. // Last chance location to put image files.
  198. #define SZ_UNDERSCORE "_"
  199. #define SZ_SLASH "\\"
  200. #define SZ_SUBDIRECTORY "IrTranP"
  201. #define SZ_BACKUP_MY_PICTURES "\\TEMP"
  202. #define SZ_BACKUP_DRIVE "C:"
  203. // File Suffix:
  204. #define SLASH '\\'
  205. #define PERIOD '.'
  206. #define SZ_JPEG ".JPG"
  207. #define SZ_UPF ".UPF"
  208. // Forward reference:
  209. class CIOSTATUS;
  210. extern "C" DWORD ProcessIoPackets( CIOSTATUS *pIoStatus );
  211. //--------------------------------------------------------------------
  212. // Global functions (in irtranp.cpp)
  213. //--------------------------------------------------------------------
  214. extern HANDLE GetUserToken();
  215. extern BOOL CheckSaveAsUPF();
  216. extern BOOL CheckExploreOnCompletion();
  217. extern BOOL ReceivesAllowed();
  218. extern char *GetImageDirectory();
  219. //--------------------------------------------------------------------
  220. // class CIOPACKET
  221. //--------------------------------------------------------------------
  222. class CIOPACKET
  223. {
  224. public:
  225. CIOPACKET();
  226. ~CIOPACKET();
  227. void *operator new( IN size_t Size );
  228. void operator delete( IN void *pObj,
  229. IN size_t Size );
  230. DWORD Initialize( IN DWORD dwKind = PACKET_KIND_LISTEN,
  231. IN SOCKET ListenSocket = INVALID_SOCKET,
  232. IN SOCKET Socket = INVALID_SOCKET,
  233. IN HANDLE hIoCP = INVALID_HANDLE_VALUE );
  234. // void * operator new( size_t ObjectSize );
  235. // void operator delete( void * pObject );
  236. DWORD PostIo();
  237. DWORD PostIoRead(); // Called by PostIo().
  238. DWORD PostIoWrite( IN void *pvBuffer,
  239. IN DWORD dwBufferSize,
  240. IN DWORD dwOffset );
  241. #ifdef NTENV
  242. void GetSockAddrs( OUT SOCKADDR_IRDA **ppAddrLocal,
  243. OUT SOCKADDR_IRDA **ppAddrFrom );
  244. #endif
  245. DWORD GetIoPacketKind();
  246. void SetIoPacketKind( IN DWORD dwKind );
  247. HANDLE GetIoCompletionPort();
  248. char *GetReadBuffer();
  249. SOCKET GetSocket();
  250. void SetSocket( SOCKET Socket );
  251. SOCKET GetListenSocket();
  252. void SetListenSocket( SOCKET Socket );
  253. HANDLE GetFileHandle();
  254. void SetFileHandle( HANDLE hFile );
  255. void *GetWritePdu();
  256. void SetWritePdu( void *pvPdu );
  257. static CIOPACKET *CIoPacketFromOverlapped( OVERLAPPED *pOverlapped );
  258. private:
  259. DWORD m_dwKind;
  260. SOCKET m_ListenSocket;
  261. SOCKET m_Socket;
  262. HANDLE m_hFile;
  263. SOCKADDR_IRDA *m_pLocalAddr;
  264. SOCKADDR_IRDA *m_pFromAddr;
  265. void *m_pAcceptBuffer;
  266. void *m_pReadBuffer;
  267. void *m_pvWritePdu; // SCEP_HEADER PDU holder.
  268. DWORD m_dwReadBufferSize;
  269. OVERLAPPED m_Overlapped;
  270. };
  271. //--------------------------------------------------------------------
  272. // class CIOSTATUS
  273. //
  274. //--------------------------------------------------------------------
  275. class CIOSTATUS
  276. {
  277. public:
  278. CIOSTATUS();
  279. ~CIOSTATUS();
  280. void *operator new( IN size_t Size );
  281. void operator delete( IN void *pObj,
  282. IN size_t Size );
  283. DWORD Initialize();
  284. BOOL IsMainThreadId( DWORD dwTid );
  285. HANDLE GetIoCompletionPort();
  286. LONG IncrementNumThreads();
  287. LONG DecrementNumThreads();
  288. LONG IncrementNumPendingThreads();
  289. LONG DecrementNumPendingThreads();
  290. private:
  291. DWORD m_dwMainThreadId;
  292. LONG m_lNumThreads;
  293. LONG m_lNumPendingThreads;
  294. };
  295. //--------------------------------------------------------------------
  296. // CIOPACKET::GetIoCompletionPort()
  297. //--------------------------------------------------------------------
  298. inline HANDLE CIOPACKET::GetIoCompletionPort()
  299. {
  300. return INVALID_HANDLE_VALUE;
  301. }
  302. //--------------------------------------------------------------------
  303. // CIOPACKET::GetSocket()
  304. //--------------------------------------------------------------------
  305. inline SOCKET CIOPACKET::GetSocket()
  306. {
  307. return m_Socket;
  308. }
  309. //--------------------------------------------------------------------
  310. // CIOPACKET::SetSocket()
  311. //--------------------------------------------------------------------
  312. inline void CIOPACKET::SetSocket( SOCKET Socket )
  313. {
  314. m_Socket = Socket;
  315. }
  316. //--------------------------------------------------------------------
  317. // CIOPACKET::GetListenSocket()
  318. //--------------------------------------------------------------------
  319. inline SOCKET CIOPACKET::GetListenSocket()
  320. {
  321. return m_ListenSocket;
  322. }
  323. //--------------------------------------------------------------------
  324. // CIOPACKET::SetListenSocket()
  325. //--------------------------------------------------------------------
  326. inline void CIOPACKET::SetListenSocket( SOCKET ListenSocket )
  327. {
  328. m_ListenSocket = ListenSocket;
  329. }
  330. //--------------------------------------------------------------------
  331. // CIOPACKET::CIoPacketFromOverlapped()
  332. //--------------------------------------------------------------------
  333. inline CIOPACKET *CIOPACKET::CIoPacketFromOverlapped( OVERLAPPED *pOverlapped )
  334. {
  335. return CONTAINING_RECORD(pOverlapped,CIOPACKET,m_Overlapped);
  336. }
  337. //--------------------------------------------------------------------
  338. // CIOPACKET::GetIoPacketKind()
  339. //--------------------------------------------------------------------
  340. inline DWORD CIOPACKET::GetIoPacketKind()
  341. {
  342. return m_dwKind;
  343. }
  344. //--------------------------------------------------------------------
  345. // CIOPACKET::SetIoPacketKind()
  346. //--------------------------------------------------------------------
  347. inline void CIOPACKET::SetIoPacketKind( DWORD dwKind )
  348. {
  349. m_dwKind = dwKind;
  350. }
  351. //--------------------------------------------------------------------
  352. // CIOPACKET::GetReadBuffer()
  353. //--------------------------------------------------------------------
  354. inline char *CIOPACKET::GetReadBuffer()
  355. {
  356. return (char*)m_pReadBuffer;
  357. }
  358. //--------------------------------------------------------------------
  359. // CIOPACKET::GetFileHandle()
  360. //--------------------------------------------------------------------
  361. inline HANDLE CIOPACKET::GetFileHandle()
  362. {
  363. return m_hFile;
  364. }
  365. //--------------------------------------------------------------------
  366. // CIOPACKET::SetFileHandle()
  367. //--------------------------------------------------------------------
  368. inline void CIOPACKET::SetFileHandle( HANDLE hFile )
  369. {
  370. m_hFile = hFile;
  371. }
  372. //--------------------------------------------------------------------
  373. // CIOPACKET::GetWritePdu()
  374. //--------------------------------------------------------------------
  375. inline void *CIOPACKET::GetWritePdu()
  376. {
  377. return m_pvWritePdu;
  378. }
  379. //--------------------------------------------------------------------
  380. // CIOPACKET::SetWritePdu()
  381. //--------------------------------------------------------------------
  382. inline void CIOPACKET::SetWritePdu( void *pvWritePdu )
  383. {
  384. m_pvWritePdu = pvWritePdu;
  385. }
  386. //********************************************************************
  387. //--------------------------------------------------------------------
  388. // CIOSTATUS::IsMainTheadId()
  389. //--------------------------------------------------------------------
  390. inline BOOL CIOSTATUS::IsMainThreadId( DWORD dwTid )
  391. {
  392. return (dwTid == m_dwMainThreadId);
  393. }
  394. //--------------------------------------------------------------------
  395. // CIOSTATUS::GetIoCompletionPort()
  396. //--------------------------------------------------------------------
  397. inline HANDLE CIOSTATUS::GetIoCompletionPort()
  398. {
  399. return INVALID_HANDLE_VALUE;
  400. }
  401. //--------------------------------------------------------------------
  402. // CIOSTATUS::IncrementNumThreads()
  403. //--------------------------------------------------------------------
  404. inline LONG CIOSTATUS::IncrementNumThreads()
  405. {
  406. return InterlockedIncrement(&m_lNumThreads);
  407. }
  408. //--------------------------------------------------------------------
  409. // CIOSTATUS::DecrementNumThreads()
  410. //--------------------------------------------------------------------
  411. inline LONG CIOSTATUS::DecrementNumThreads()
  412. {
  413. return InterlockedDecrement(&m_lNumThreads);
  414. }
  415. //--------------------------------------------------------------------
  416. // CIOSTATUS::IncrementNumPendingThreads()
  417. //--------------------------------------------------------------------
  418. inline LONG CIOSTATUS::IncrementNumPendingThreads()
  419. {
  420. return InterlockedIncrement(&m_lNumPendingThreads);
  421. }
  422. //--------------------------------------------------------------------
  423. // CIOSTATUS::DecrementNumPendingThreads()
  424. //--------------------------------------------------------------------
  425. inline LONG CIOSTATUS::DecrementNumPendingThreads()
  426. {
  427. return InterlockedDecrement(&m_lNumPendingThreads);
  428. }
  429. #endif //_IO_H_