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.

590 lines
13 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. precomp.h
  5. Abstract:
  6. This header contains all the RFC constants, structure
  7. definitions used by all modules, and function declarations.
  8. Author:
  9. Jeffrey C. Venable, Sr. (jeffv) 01-Jun-2001
  10. Revision History:
  11. --*/
  12. #pragma once
  13. #include <nt.h>
  14. #include <ntrtl.h>
  15. #include <nturtl.h>
  16. #include <winsock2.h>
  17. #include <mswsock.h>
  18. #include <ws2tcpip.h>
  19. #include <svcs.h>
  20. #include <stdlib.h>
  21. //
  22. // TFTPD protocol-specific values, per RFC.
  23. //
  24. typedef enum _TFTPD_DATA_SIZE {
  25. // RFC 2348.
  26. TFTPD_MIN_DATA = 8,
  27. // RFC 1350.
  28. TFTPD_DEF_DATA = 512,
  29. // RFC 1783 (obsoleted by RFC 2348 which says 1428).
  30. // We implement RFC 2348, but honor RFC 1783.
  31. TFTPD_MTU_DATA = 1432,
  32. // RFC 2348.
  33. TFTPD_MAX_DATA = 65464,
  34. } TFTPD_DATA_SIZE;
  35. typedef enum _TFTPD_BUFFER_SIZE TFTPD_BUFFER_SIZE;
  36. typedef struct _TFTPD_CONTEXT TFTPD_CONTEXT, *PTFTPD_CONTEXT;
  37. typedef struct _TFTPD_SOCKET {
  38. LIST_ENTRY linkage;
  39. SOCKET s;
  40. SOCKADDR_IN addr;
  41. PTFTPD_CONTEXT context;
  42. HANDLE wSelectWait;
  43. HANDLE hSelect;
  44. TFTPD_BUFFER_SIZE buffersize;
  45. TFTPD_DATA_SIZE datasize;
  46. LONG numBuffers;
  47. DWORD postedBuffers;
  48. DWORD lowWaterMark;
  49. DWORD highWaterMark;
  50. } TFTPD_SOCKET, *PTFTPD_SOCKET;
  51. typedef struct _TFTPD_BUFFER {
  52. struct _internal {
  53. PTFTPD_SOCKET socket;
  54. DWORD datasize;
  55. PTFTPD_CONTEXT context;
  56. struct {
  57. SOCKADDR_IN peer;
  58. INT peerLen;
  59. DWORD bytes;
  60. DWORD flags;
  61. union {
  62. WSAOVERLAPPED overlapped;
  63. IO_STATUS_BLOCK ioStatus;
  64. };
  65. // WSARecvMsg values:
  66. WSAMSG msg;
  67. struct {
  68. WSACMSGHDR header;
  69. IN_PKTINFO info;
  70. } control;
  71. } io;
  72. } internal;
  73. #pragma pack( push, 1 )
  74. struct _message {
  75. USHORT opcode;
  76. union {
  77. struct _rrq {
  78. char data[1];
  79. } rrq;
  80. struct _wrq {
  81. char data[1];
  82. } wrq;
  83. struct _data {
  84. USHORT block;
  85. char data[1];
  86. } data;
  87. struct _ack {
  88. USHORT block;
  89. } ack;
  90. struct _error {
  91. USHORT code;
  92. char error[1];
  93. } error;
  94. struct _oack {
  95. char data[1];
  96. } oack;
  97. };
  98. } message;
  99. #pragma pack( pop )
  100. } TFTPD_BUFFER, *PTFTPD_BUFFER;
  101. typedef enum _TFTPD_BUFFER_SIZE {
  102. TFTPD_DEF_BUFFER = (FIELD_OFFSET(TFTPD_BUFFER, message.data.data) + TFTPD_DEF_DATA),
  103. TFTPD_MTU_BUFFER = (FIELD_OFFSET(TFTPD_BUFFER, message.data.data) + TFTPD_MTU_DATA),
  104. TFTPD_MAX_BUFFER = (FIELD_OFFSET(TFTPD_BUFFER, message.data.data) + TFTPD_MAX_DATA),
  105. } TFTPD_BUFFER_SIZE;
  106. typedef enum _TFTPD_PACKET_TYPE {
  107. TFTPD_RRQ = 1,
  108. TFTPD_WRQ = 2,
  109. TFTPD_DATA = 3,
  110. TFTPD_ACK = 4,
  111. TFTPD_ERROR = 5,
  112. TFTPD_OACK = 6,
  113. } TFTPD_PACKET_TYPE;
  114. typedef enum _TFTPD_ERROR_CODE {
  115. TFTPD_ERROR_UNDEFINED = 0,
  116. TFTPD_ERROR_FILE_NOT_FOUND = 1,
  117. TFTPD_ERROR_ACCESS_VIOLATION = 2,
  118. TFTPD_ERROR_DISK_FULL = 3,
  119. TFTPD_ERROR_ILLEGAL_OPERATION = 4,
  120. TFTPD_ERROR_UNKNOWN_TRANSFER_ID = 5,
  121. TFTPD_ERROR_FILE_EXISTS = 6,
  122. TFTPD_ERROR_NO_SUCH_USER = 7,
  123. TFTPD_ERROR_OPTION_NEGOT_FAILED = 8,
  124. } TFTPD_ERROR_CODE;
  125. typedef enum _TFTPD_MODE {
  126. TFTPD_MODE_TEXT = 1,
  127. TFTPD_MODE_BINARY = 2,
  128. TFTPD_MODE_MAIL = 3,
  129. } TFTPD_MODE;
  130. typedef enum _TFTPD_OPTION_VALUES {
  131. TFTPD_OPTION_BLKSIZE = 0x00000001,
  132. TFTPD_OPTION_TIMEOUT = 0x00000002,
  133. TFTPD_OPTION_TSIZE = 0x00000004,
  134. } TFTPD_OPTION_VALUES;
  135. typedef struct _TFTPD_CONTEXT {
  136. LIST_ENTRY linkage;
  137. volatile LONG reference;
  138. TFTPD_PACKET_TYPE type;
  139. SOCKADDR_IN peer;
  140. PTFTPD_SOCKET socket;
  141. DWORD options;
  142. HANDLE hFile;
  143. PCHAR filename;
  144. LARGE_INTEGER filesize;
  145. LARGE_INTEGER fileOffset;
  146. LARGE_INTEGER translationOffset;
  147. TFTPD_MODE mode;
  148. DWORD blksize;
  149. DWORD timeout;
  150. USHORT block;
  151. USHORT sorcerer;
  152. BOOL danglingCR;
  153. HANDLE hWait;
  154. HANDLE wWait;
  155. HANDLE hTimer;
  156. ULONG retransmissions;
  157. volatile LONG state;
  158. } TFTPD_CONTEXT, *PTFTPD_CONTEXT;
  159. typedef enum _TFTPD_STATE {
  160. TFTPD_STATE_BUSY = 0x00000001,
  161. TFTPD_STATE_DEAD = 0x00000002,
  162. } TFTPD_STATE;
  163. typedef struct _TFTPD_HASH_BUCKET {
  164. CRITICAL_SECTION cs;
  165. // #if defined(DBG)
  166. DWORD numEntries;
  167. // #endif // defined(DBG)
  168. LIST_ENTRY bucket;
  169. } TFTPD_HASH_BUCKET, *PTFTPD_HASH_BUCKET;
  170. //
  171. // Global variables :
  172. //
  173. typedef struct _TFTPD_GLOBALS {
  174. // Initialization flags :
  175. struct _initialized {
  176. BOOL ioCS;
  177. BOOL reaperContextCS;
  178. BOOL reaperSocketCS;
  179. BOOL winsock;
  180. BOOL contextHashTable;
  181. } initialized;
  182. // Service control :
  183. struct _service {
  184. SERVICE_STATUS_HANDLE hStatus;
  185. SERVICE_STATUS status;
  186. HANDLE hEventLogSource;
  187. volatile DWORD shutdown;
  188. } service;
  189. // Service private heap :
  190. HANDLE hServiceHeap;
  191. // Registry parameters :
  192. struct _parameters {
  193. DWORD hashEntries;
  194. ULONG lowWaterMark;
  195. ULONG highWaterMark;
  196. DWORD maxRetries;
  197. CHAR rootDirectory[MAX_PATH];
  198. CHAR validClients[16]; // IPv4 "xxx.xxx.xxx.xxx"
  199. CHAR validReadFiles[MAX_PATH];
  200. CHAR validMasters[16]; // IPv4 "xxx.xxx.xxx.xxx"
  201. CHAR validWriteFiles[MAX_PATH];
  202. #if defined(DBG)
  203. DWORD debugFlags;
  204. #endif // defined(DBG)
  205. } parameters;
  206. // I/O mechanisms (sockets) :
  207. struct _io {
  208. CRITICAL_SECTION cs;
  209. TFTPD_SOCKET master;
  210. TFTPD_SOCKET def;
  211. TFTPD_SOCKET mtu;
  212. TFTPD_SOCKET max;
  213. HANDLE hTimerQueue;
  214. LONG numContexts;
  215. LONG numBuffers;
  216. } io;
  217. struct _hash {
  218. PTFTPD_HASH_BUCKET table;
  219. #if defined(DBG)
  220. DWORD numEntries;
  221. #endif // defined(DBG)
  222. } hash;
  223. struct _fp {
  224. LPFN_WSARECVMSG WSARecvMsg;
  225. } fp;
  226. struct _reaper {
  227. CRITICAL_SECTION contextCS;
  228. LIST_ENTRY leakedContexts;
  229. DWORD numLeakedContexts;
  230. CRITICAL_SECTION socketCS;
  231. LIST_ENTRY leakedSockets;
  232. DWORD numLeakedSockets;
  233. } reaper;
  234. #if defined(DBG)
  235. struct _performance {
  236. DWORD maxClients;
  237. DWORD timeouts;
  238. DWORD drops;
  239. DWORD privateSockets;
  240. DWORD sorcerersApprentice;
  241. } performance;
  242. #endif // defined(DBG)
  243. } TFTPD_GLOBALS, *PTFTPD_GLOBALS;
  244. extern TFTPD_GLOBALS globals;
  245. //
  246. // Useful macros
  247. //
  248. #define TFTPD_MIN_RECEIVED_DATA \
  249. (FIELD_OFFSET(TFTPD_BUFFER, message.data.data) - \
  250. FIELD_OFFSET(TFTPD_BUFFER, message.opcode))
  251. #define TFTPD_DATA_AMOUNT_RECEIVED(buffer) \
  252. (buffer->internal.io.bytes - TFTPD_MIN_RECEIVED_DATA) \
  253. #define TFTPD_ACK_SIZE \
  254. (FIELD_OFFSET(TFTPD_BUFFER, message.ack.block) - \
  255. FIELD_OFFSET(TFTPD_BUFFER, message.opcode) + \
  256. sizeof(buffer->message.ack))
  257. //
  258. // Function prototypes : context.c
  259. //
  260. PTFTPD_CONTEXT
  261. TftpdContextAllocate(
  262. );
  263. BOOL
  264. TftpdContextAdd(
  265. PTFTPD_CONTEXT context
  266. );
  267. DWORD
  268. TftpdContextAddReference(
  269. PTFTPD_CONTEXT context
  270. );
  271. PTFTPD_CONTEXT
  272. TftpdContextAquire(
  273. PSOCKADDR_IN addr
  274. );
  275. DWORD
  276. TftpdContextRelease(
  277. PTFTPD_CONTEXT context
  278. );
  279. BOOL
  280. TftpdContextUpdateTimer(
  281. PTFTPD_CONTEXT context
  282. );
  283. BOOL
  284. TftpdContextFree(
  285. PTFTPD_CONTEXT context
  286. );
  287. void
  288. TftpdContextKill(
  289. PTFTPD_CONTEXT context
  290. );
  291. void
  292. TftpdContextLeak(
  293. PTFTPD_CONTEXT context
  294. );
  295. //
  296. // Function prototypes : io.c
  297. //
  298. PTFTPD_BUFFER
  299. TftpdIoAllocateBuffer(
  300. PTFTPD_SOCKET socket
  301. );
  302. PTFTPD_BUFFER
  303. TftpdIoSwapBuffer(
  304. PTFTPD_BUFFER buffer,
  305. PTFTPD_SOCKET socket
  306. );
  307. void
  308. TftpdIoFreeBuffer(
  309. PTFTPD_BUFFER buffer
  310. );
  311. DWORD
  312. TftpdIoPostReceiveBuffer(
  313. PTFTPD_SOCKET socket,
  314. PTFTPD_BUFFER buffer
  315. );
  316. void
  317. TftpdIoInitializeSocketContext(
  318. PTFTPD_SOCKET socket,
  319. PSOCKADDR_IN addr,
  320. PTFTPD_CONTEXT context
  321. );
  322. BOOL
  323. TftpdIoAssignSocket(
  324. PTFTPD_CONTEXT context,
  325. PTFTPD_BUFFER buffer
  326. );
  327. BOOL
  328. TftpdIoDestroySocketContext(
  329. PTFTPD_SOCKET context
  330. );
  331. void
  332. TftpdIoSendErrorPacket(
  333. PTFTPD_BUFFER buffer,
  334. TFTPD_ERROR_CODE error,
  335. char *reason
  336. );
  337. PTFTPD_BUFFER
  338. TftpdIoSendPacket(
  339. PTFTPD_BUFFER buffer
  340. );
  341. PTFTPD_BUFFER
  342. TftpdIoSendOackPacket(
  343. PTFTPD_BUFFER buffer
  344. );
  345. //
  346. // Function prototypes : log.c
  347. //
  348. void
  349. TftpdLogEvent(
  350. WORD type,
  351. DWORD request,
  352. WORD numStrings
  353. );
  354. //
  355. // Function prototypes : process.c
  356. //
  357. BOOL
  358. TftpdProcessComplete(
  359. PTFTPD_BUFFER buffer
  360. );
  361. void CALLBACK
  362. TftpdProcessTimeout(
  363. PTFTPD_CONTEXT context,
  364. BOOLEAN
  365. );
  366. void
  367. TftpdProcessError(
  368. PTFTPD_BUFFER buffer
  369. );
  370. PTFTPD_BUFFER
  371. TftpdProcessReceivedBuffer(
  372. PTFTPD_BUFFER buffer
  373. );
  374. //
  375. // Function prototypes: read.c
  376. //
  377. PTFTPD_BUFFER
  378. TftpdReadResume(
  379. PTFTPD_BUFFER buffer
  380. );
  381. DWORD WINAPI
  382. TftpdReadQueuedResume(
  383. PTFTPD_BUFFER buffer
  384. );
  385. PTFTPD_BUFFER
  386. TftpdReadAck(
  387. PTFTPD_BUFFER buffer
  388. );
  389. PTFTPD_BUFFER
  390. TftpdReadRequest(
  391. PTFTPD_BUFFER buffer
  392. );
  393. //
  394. // Function prototypes : service.c
  395. //
  396. void
  397. TftpdServiceAttemptCleanup(
  398. );
  399. //
  400. // Function prototypes : util.c
  401. //
  402. BOOL
  403. TftpdUtilGetFileModeAndOptions(
  404. PTFTPD_CONTEXT context,
  405. PTFTPD_BUFFER buffer
  406. );
  407. PTFTPD_BUFFER
  408. TftpdUtilSendOackPacket(
  409. PTFTPD_BUFFER buffer
  410. );
  411. BOOL
  412. TftpdUtilMatch(
  413. const char *const p,
  414. const char *const q
  415. );
  416. //
  417. // Function prototypes: write.c
  418. //
  419. PTFTPD_BUFFER
  420. TftpdWriteSendAck(
  421. PTFTPD_BUFFER buffer
  422. );
  423. DWORD WINAPI
  424. TftpdWriteQueuedResume(
  425. PTFTPD_BUFFER buffer
  426. );
  427. PTFTPD_BUFFER
  428. TftpdWriteData(
  429. PTFTPD_BUFFER buffer
  430. );
  431. PTFTPD_BUFFER
  432. TftpdWriteRequest(
  433. PTFTPD_BUFFER buffer
  434. );
  435. //
  436. // Debug
  437. //
  438. #if defined(DBG)
  439. void __cdecl
  440. TftpdOutputDebug(ULONG flag, char *format, ...);
  441. #define TFTPD_DEBUG(x) TftpdOutputDebug x
  442. #define TFTPD_DBG_SERVICE 0x00000001
  443. #define TFTPD_DBG_IO 0x00000002
  444. #define TFTPD_DBG_PROCESS 0x00000004
  445. #define TFTPD_DBG_CONTEXT 0x00000008
  446. #define TFTPD_TRACE_SERVICE 0x00010000
  447. #define TFTPD_TRACE_IO 0x00020000
  448. #define TFTPD_TRACE_PROCESS 0x00040000
  449. #define TFTPD_TRACE_CONTEXT 0x00080000
  450. #else
  451. #define TFTPD_DEBUG(x)
  452. #endif // defined(DBG)