Source code of Windows XP (NT5)
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.

798 lines
18 KiB

  1. /********************************************************************/
  2. /** Microsoft LAN Manager **/
  3. /** Copyright(c) Microsoft Corp., 1987-1990 **/
  4. /********************************************************************/
  5. #ifndef _MSRV_INCLUDED
  6. #define _MSRV_INCLUDED
  7. #include <nt.h> // for ntrtl.h
  8. #include <ntrtl.h> // DbgPrint prototypes
  9. #include <nturtl.h> // needed for windows.h when I have nt.h
  10. #define WINMM_H
  11. #include <windows.h> // ExitThread prototype
  12. #include <lmcons.h>
  13. #include <lmerr.h>
  14. #include <nb30.h> // NetBios Prototypes and constants
  15. #include <winsta.h> // Winstation functions (for HYDRA)
  16. #include "heap.h"
  17. #ifdef LINT
  18. #define near
  19. #define far
  20. #define void int
  21. #endif // LINT
  22. #define clearncb(x) memset((char *)x,'\0',sizeof(NCB))
  23. #define clearncbf(x) memsetf((char far *)x,'\0',sizeof(NCB))
  24. #define so_to_far(seg, off) \
  25. ((((long)(unsigned)(seg)) << 16) + (unsigned)(off))
  26. //
  27. // Constant definitions
  28. //
  29. #define BUFLEN 200 // Length of NCB_BUF
  30. #define LOGNAMLEN PATHLEN // Log file name length (max)
  31. #define TXTMAX 128 // Maximum bytes of text per block
  32. #define MAXHEAD 80 // Maximum message header length
  33. #define MAXEND 60 // Maximum message end length
  34. #define MAXGRPMSGLEN 128 // Max domain message length
  35. #define MAX_SIZMESSBUF 62000 // The max size for the message buffer
  36. #define MIN_SIZMESSBUF 512 // The min size for the message buffer
  37. #define MSNGR_MAX_NETS MAX_LANA // The maximum number of nets the messenger
  38. // can handle. (Currently 12)
  39. #define TIME_BUF_SIZE 128 // Size of the buffer to hold the message time
  40. #define MSGFILENAMLEN PATHLEN*sizeof(TCHAR)
  41. //
  42. // Messaging name end bytes
  43. //
  44. #define NAME_LOCAL_END '\003' // 16th byte in local NCB name
  45. #define NAME_REMOTE_END '\005' // 16th byte in remote NCB name
  46. //
  47. // Messenger Thread Manager States (used as return codes)
  48. //
  49. #define UPDATE_ONLY 0 // no change in state - just send current status.
  50. #define STARTING 1 // the messenger is initializing.
  51. #define RUNNING 2 // initialization completed normally - now running
  52. #define STOPPING 3 // uninstall pending
  53. #define STOPPED 4 // uninstalled
  54. //
  55. // Forced Shutdown PendingCodes
  56. //
  57. #define PENDING TRUE
  58. #define IMMEDIATE FALSE
  59. //
  60. // Message transfer states
  61. //
  62. #define MESSTART 0 // Message start state
  63. #define MESSTOP 1 // Message stop state
  64. #define MESCONT 2 // Message continued state
  65. #define MESERR 3 // Message error state
  66. //
  67. // Alert Size
  68. //
  69. #define ALERT_MAX_DISPLAYED_MSG_SIZE 4096
  70. //
  71. // Special Session Id = -1 (used to indicate that the message has to be broadcasted to every session)
  72. //
  73. #define EVERYBODY_SESSION_ID -1
  74. // Structure definitions
  75. //
  76. // ncb worker function type
  77. //
  78. typedef VOID (*PNCBIFCN) (
  79. DWORD NetIndex, // Network Index
  80. DWORD NcbIndex, // Network Control Block Index
  81. CHAR RetVal // value returned by net bios
  82. );
  83. typedef PNCBIFCN LPNCBIFCN;
  84. typedef struct _NCB_STATUS {
  85. int this_immediate;
  86. int last_immediate;
  87. unsigned char this_final;
  88. unsigned char last_final;
  89. unsigned char rep_count;
  90. unsigned char align; // *ALIGNMENT*
  91. }NCB_STATUS, *PNCB_STATUS, *LPNCB_STATUS;
  92. // structure used for keeping the session id list for each alias
  93. typedef struct _MSG_SESSION_ID_ITEM
  94. {
  95. LIST_ENTRY List;
  96. ULONG SessionId;
  97. }
  98. MSG_SESSION_ID_ITEM, *PMSG_SESSION_ID_ITEM;
  99. // Per NCB Information
  100. typedef struct _NCB_DATA {
  101. DWORD MsgPtr;
  102. LPNCBIFCN IFunc;
  103. NCB_STATUS Status;
  104. NCB Ncb; // Structure passed to Netbios
  105. CHAR Buffer[BUFLEN];
  106. CHAR Name[NCBNAMSZ + 4];
  107. CHAR Fname[NCBNAMSZ + 4];
  108. SHORT mgid;
  109. CHAR State;
  110. UCHAR NameFlags;
  111. UCHAR NameNum;
  112. LIST_ENTRY SessionList;
  113. UCHAR Pad[3];
  114. } NCB_DATA, *PNCB_DATA;
  115. // Per Network Information
  116. typedef struct _NET_DATA {
  117. ULONG NumNcbs;
  118. PNCB_DATA *NcbList;
  119. UCHAR net_lana_num;
  120. UCHAR Pad[3];
  121. } NET_DATA, *PNET_DATA;
  122. // Global Information
  123. typedef struct _GLOBAL_DATA
  124. {
  125. ULONG NumNets;
  126. DWORD StartFlags;
  127. DWORD LogStatus;
  128. DWORD BufSize;
  129. DWORD MsgQueueF;
  130. DWORD MsgQueueB;
  131. PNET_DATA NetData;
  132. CHAR LogFileName[LOGNAMLEN];
  133. PCHAR Buffer;
  134. } GLOBAL_DATA, *PGLOBAL_DATA;
  135. extern GLOBAL_DATA GlobalData;
  136. #define NCB_INIT_ENTRIES 16 // Initial number of NCB allocated per network
  137. // For Multi-user systems, we allow up to 256 NCBs per network
  138. #define NCB_MAX_ENTRIES 256 // Maximum number of NCBs per network
  139. #define SESSION_MAX 256
  140. //
  141. // Name Flag definitions
  142. //
  143. #define NFNEW 0x01 // New name
  144. #define NFDEL 0x02 // Name deleted
  145. #define NFFOR 0x04 // Messages forwarded
  146. #define NFFWDNAME 0x10 // Forward-name
  147. #define NFMACHNAME 0x20 // Machine name (undeletable)
  148. #define NFLOCK 0x40 // Name entry locked
  149. #define NFDEL_PENDING 0x80 // Delete name issued but not complete*/
  150. //
  151. // Memory area names
  152. //
  153. #define DATAMEM "\\SHAREMEM\\MSRV.DAT"
  154. #define INITMEM "\\SHAREMEM\\MSRVINIT.DAT"
  155. //
  156. // System semaphore definitions
  157. //
  158. #define WAKEUP_SEM "\\SEM\\MSRVWU"
  159. #define WAKEUPSEM_LEN 13 // The number of characters in WAKEUP_SEM +2
  160. //
  161. // The messenger mailslot for domain messaging
  162. //
  163. #ifdef remove
  164. #define MESSNGR_MS_NAME "\\mailslot\\messngr"
  165. #define MESSNGR_MS_NAME_LEN 17
  166. #endif
  167. #define MESSNGR_MS_NAME "\\\\.\\mailslot\\messngr"
  168. #define MESSNGR_MS_NAME_LEN 20
  169. //
  170. // The character used to separate the components of a domain message
  171. //
  172. #define SEPCHAR "\6"
  173. //
  174. // Memory allocator flags
  175. //
  176. // #define MEMMOVE 0x0002 // Movable memory flag
  177. // #define MEMWRIT 0x0080 // Writable memory flag
  178. //
  179. // Structure and macro definitions
  180. //
  181. #ifdef INULL // If heap structures defined
  182. //
  183. // Single-block message header
  184. //
  185. typedef struct {
  186. HEAPHDR sbm_hp; // Heap block header
  187. unsigned short sbm_next; // Link to next message
  188. unsigned short align; // *ALIGNMENT*
  189. SYSTEMTIME sbm_bigtime; // Date and time of message
  190. }SBM;
  191. #define SBM_SIZE(x) HP_SIZE((x).sbm_hp)
  192. #define SBM_CODE(x) HP_FLAG((x).sbm_hp)
  193. #define SBM_NEXT(x) (x).sbm_next
  194. #define SBM_BIGTIME(x) (x).sbm_bigtime
  195. #define SBMPTR(x) ((SBM far *) &heap[(x)])
  196. //
  197. // Multi-block message header
  198. //
  199. typedef struct {
  200. HEAPHDR mbb_hp; // Heap block header
  201. DWORD mbb_next; // Link to next message
  202. SYSTEMTIME mbb_bigtime; // Date of message
  203. DWORD mbb_btext; // Link to last text block
  204. DWORD mbb_ftext; // Link to first text block
  205. DWORD mbb_state; // State flag
  206. }MBB;
  207. #define MBB_SIZE(x) HP_SIZE((x).mbb_hp)
  208. #define MBB_CODE(x) HP_FLAG((x).mbb_hp)
  209. #define MBB_NEXT(x) (x).mbb_next
  210. #define MBB_BIGTIME(x) (x).mbb_bigtime
  211. #define MBB_BTEXT(x) (x).mbb_btext
  212. #define MBB_FTEXT(x) (x).mbb_ftext
  213. #define MBB_STATE(x) (x).mbb_state
  214. #define MBBPTR(x) ((MBB far *) &heap[(x)])
  215. //
  216. // Multi-block message text
  217. //
  218. typedef struct {
  219. HEAPHDR mbt_hp; // Heap block header
  220. DWORD mbt_next; // Link to next block (offset)
  221. DWORD mbt_bytecount; // *ALIGNMENT2*
  222. }MBT, *PMBT, *LPMBT;
  223. #define MBT_SIZE(x) HP_SIZE((x).mbt_hp)
  224. #define MBT_CODE(x) HP_FLAG((x).mbt_hp)
  225. #define MBT_NEXT(x) (x).mbt_next
  226. #define MBT_COUNT(x) (x).mbt_bytecount // *ALIGNMENT2*
  227. #define MBTPTR(x) ((LPMBT) &heap[(x)])
  228. #endif // INULL - End heap access macros
  229. //
  230. // A one session/name status structure
  231. //
  232. typedef struct _MSG_SESSION_STATUS{
  233. SESSION_HEADER SessHead;
  234. SESSION_BUFFER SessBuffer[SESSION_MAX];
  235. }MSG_SESSION_STATUS, *PMSG_SESSION_STATUS, *LPMSG_SESSION_STATUS;
  236. //
  237. // Shared data access macros
  238. //
  239. #define GETNCBDATA(n, x) GlobalData.NetData[(n)].NcbList[(x)]
  240. #define GETNCB(n, x) &GlobalData.NetData[(n)].NcbList[(x)]->Ncb
  241. #define GETNETLANANUM(n) GlobalData.NetData[(n)].net_lana_num
  242. #define NETLANANUM GETNETLANANUM
  243. #define GETNETDATA(n) &GlobalData.NetData[(n)]
  244. #define SD_NUMNETS() GlobalData.NumNets
  245. #define SD_MSRV() GlobalData.StartFlags
  246. #define SD_NAMEFLAGS(n, x) GlobalData.NetData[(n)].NcbList[(x)]->NameFlags
  247. #define SD_NAMENUMS(n, x) GlobalData.NetData[(n)].NcbList[(x)]->NameNum
  248. #define SD_NAMES(n, x) GlobalData.NetData[(n)].NcbList[(x)]->Name
  249. #define SD_FWDNAMES(n, x) GlobalData.NetData[(n)].NcbList[(x)]->Fname
  250. #define SD_LOGNAM() GlobalData.LogFileName
  251. #define SD_BUFLEN() GlobalData.BufSize
  252. #define SD_MESLOG() GlobalData.LogStatus
  253. #define SD_MESQF() GlobalData.MsgQueueF
  254. #define SD_MESQB() GlobalData.MsgQueueB
  255. #define SD_MESPTR(n, x) GlobalData.NetData[(n)].NcbList[(x)]->MsgPtr
  256. #define SD_BUFFER() GlobalData.Buffer
  257. #define SD_SIDLIST(n,x) GlobalData.NetData[(n)].NcbList[(x)]->SessionList
  258. #define NCBMAX(n) GlobalData.NetData[(n)].NumNcbs
  259. NCB_STATUS ncb_status;
  260. #define SIG_IGNORE 1
  261. #define SIG_ACCEPT 2
  262. #define SIG_ERROR 3
  263. #define SIG_RESET 4
  264. //
  265. // g_install_state bit definitions
  266. //
  267. //#define IS_EXECED_MAIN 0x0001
  268. //#define IS_ALLOC_SEG 0x0002
  269. //
  270. // Timeout for waiting for the shared segment before giving up
  271. // and reporting an internal error.
  272. //
  273. //#define MSG_SEM_TO 60000L // 60 second timeout
  274. //
  275. // No. of repeated consectutive NCB errors required to abort the
  276. // message server.
  277. //
  278. #define SHUTDOWN_THRESHOLD 10
  279. // net send timeout and retry constatnts
  280. #define MAX_CALL_RETRY 5 // Retry a failed send up to 5 times
  281. #define CALL_RETRY_TIMEOUT 1 // second delay between retries
  282. //
  283. // Database Lock requests for the MsgDatabaseLock function.
  284. //
  285. typedef enum _MSG_LOCK_REQUEST
  286. {
  287. MSG_INITIALIZE,
  288. MSG_GET_SHARED,
  289. MSG_GET_EXCLUSIVE,
  290. MSG_RELEASE
  291. }
  292. MSG_LOCK_REQUEST, *PMSG_LOCK_REQUEST, *LPMSG_LOCK_REQUEST;
  293. //
  294. // Macros to deregister a thread pool item and close
  295. // a handle once and only once
  296. //
  297. #define DEREGISTER_WORK_ITEM(g_hWorkItem) \
  298. { \
  299. HANDLE hTemp = InterlockedExchangePointer(&g_hWorkItem, NULL); \
  300. \
  301. if (hTemp != NULL) \
  302. { \
  303. NTSTATUS Status = RtlDeregisterWait(hTemp); \
  304. \
  305. if (!NT_SUCCESS(Status)) \
  306. { \
  307. MSG_LOG2(ERROR, \
  308. "RtlDeregisterWait on %p failed %x\n", \
  309. hTemp, \
  310. Status); \
  311. } \
  312. } \
  313. }
  314. #define CLOSE_HANDLE(HandleToClose, InvalidHandleValue) \
  315. { \
  316. HANDLE hTemp = InterlockedExchangePointer(&HandleToClose, InvalidHandleValue); \
  317. \
  318. if (hTemp != InvalidHandleValue) \
  319. { \
  320. CloseHandle(hTemp); \
  321. } \
  322. }
  323. //
  324. // global variables
  325. //
  326. extern BOOL g_IsTerminalServer;
  327. // WinStationQueryInformationW
  328. typedef BOOLEAN (*PWINSTATION_QUERY_INFORMATION) (
  329. HANDLE hServer,
  330. ULONG SessionId,
  331. WINSTATIONINFOCLASS WinStationInformationClass,
  332. PVOID pWinStationInformation,
  333. ULONG WinStationInformationLength,
  334. PULONG pReturnLength
  335. );
  336. extern PWINSTATION_QUERY_INFORMATION gpfnWinStationQueryInformation;
  337. // WinStationSendMessageW
  338. typedef BOOLEAN (*PWINSTATION_SEND_MESSAGE) (
  339. HANDLE hServer,
  340. ULONG SessionId,
  341. LPWSTR pTitle,
  342. ULONG TitleLength,
  343. LPWSTR pMessage,
  344. ULONG MessageLength,
  345. ULONG Style,
  346. ULONG Timeout,
  347. PULONG pResponse,
  348. BOOLEAN DoNotWait
  349. );
  350. extern PWINSTATION_SEND_MESSAGE gpfnWinStationSendMessage;
  351. // WinStationFreeMemory
  352. typedef BOOLEAN (*PWINSTATION_FREE_MEMORY) (
  353. PVOID pBuffer
  354. );
  355. extern PWINSTATION_FREE_MEMORY gpfnWinStationFreeMemory;
  356. // WinStationEnumerateW
  357. typedef BOOLEAN (*PWINSTATION_ENUMERATE) (
  358. HANDLE hServer,
  359. PLOGONIDW *ppLogonId,
  360. PULONG pEntries
  361. );
  362. extern PWINSTATION_ENUMERATE gpfnWinStationEnumerate;
  363. //
  364. // Function Prototypes
  365. //
  366. DWORD
  367. GetMsgrState(
  368. VOID
  369. );
  370. VOID
  371. MsgrBlockStateChange(
  372. VOID
  373. );
  374. VOID
  375. MsgrUnblockStateChange(
  376. VOID
  377. );
  378. NET_API_STATUS
  379. MsgAddName(
  380. LPTSTR Name,
  381. ULONG SessionId
  382. );
  383. VOID
  384. MsgAddUserNames(
  385. VOID
  386. );
  387. VOID
  388. MsgAddAlreadyLoggedOnUserNames(
  389. VOID
  390. );
  391. DWORD
  392. MsgBeginForcedShutdown(
  393. IN BOOL PendingCode,
  394. IN DWORD ExitCode
  395. );
  396. BOOL
  397. MsgDatabaseLock(
  398. IN MSG_LOCK_REQUEST request,
  399. IN LPSTR idString
  400. );
  401. BOOL
  402. MsgConfigurationLock(
  403. IN MSG_LOCK_REQUEST request,
  404. IN LPSTR idString
  405. );
  406. NTSTATUS
  407. MsgInitCriticalSection(
  408. PRTL_CRITICAL_SECTION pCritsec
  409. );
  410. NTSTATUS
  411. MsgInitResource(
  412. PRTL_RESOURCE pResource
  413. );
  414. DWORD
  415. MsgDisplayInit(
  416. VOID
  417. );
  418. BOOL
  419. MsgDisplayQueueAdd(
  420. IN LPSTR pMsgBuffer,
  421. IN DWORD MsgSize,
  422. IN ULONG SessionId,
  423. IN SYSTEMTIME BigTime
  424. );
  425. VOID
  426. MsgDisplayThreadWakeup(
  427. VOID
  428. );
  429. VOID
  430. MsgDisplayEnd(
  431. VOID
  432. );
  433. NET_API_STATUS
  434. MsgErrorLogWrite(
  435. IN DWORD Code,
  436. IN LPTSTR Component,
  437. IN LPBYTE Buffer,
  438. IN DWORD BufferSize,
  439. IN LPSTR Strings,
  440. IN DWORD NumStrings
  441. );
  442. NET_API_STATUS
  443. MsgInitializeMsgr(
  444. IN DWORD argc,
  445. IN LPTSTR *argv
  446. );
  447. NET_API_STATUS
  448. MsgrInitializeMsgrInternal1(
  449. void
  450. );
  451. NET_API_STATUS
  452. MsgrInitializeMsgrInternal2(
  453. void
  454. );
  455. NET_API_STATUS
  456. MsgNewName(
  457. IN DWORD neti,
  458. IN DWORD ncbi
  459. );
  460. VOID
  461. MsgrShutdown(
  462. );
  463. VOID
  464. MsgThreadWakeup(
  465. VOID
  466. );
  467. VOID
  468. MsgStatusInit(
  469. VOID
  470. );
  471. DWORD
  472. MsgStatusUpdate(
  473. IN DWORD NewState
  474. );
  475. VOID
  476. MsgThreadCloseAll(
  477. VOID
  478. );
  479. DWORD
  480. MsgThreadManagerInit(
  481. VOID
  482. );
  483. NET_API_STATUS
  484. MsgInit_NetBios(
  485. VOID
  486. );
  487. BOOL
  488. MsgServeNCBs(
  489. DWORD net // Which network am I serving?
  490. );
  491. VOID
  492. MsgServeNameReqs(
  493. IN DWORD net
  494. );
  495. VOID
  496. MsgReadGroupMailslot(
  497. VOID
  498. );
  499. NET_API_STATUS
  500. MsgServeGroupMailslot(
  501. VOID
  502. );
  503. NET_API_STATUS
  504. MsgFmtNcbName(
  505. OUT PCHAR DestBuf,
  506. IN LPTSTR Name,
  507. IN DWORD Type);
  508. DWORD
  509. Msghdrprint(
  510. int action, // Where to log the header to.
  511. LPSTR from, // Name of sender
  512. LPSTR to, // Name of recipient
  513. SYSTEMTIME bigtime, // Bigtime of message
  514. HANDLE file_handle // Output file handle*/
  515. );
  516. DWORD
  517. Msglogmbb(
  518. LPSTR from, // Name of sender
  519. LPSTR to, // Name of recipient
  520. DWORD net, // Which network ?
  521. DWORD ncbi // Network Control Block index
  522. );
  523. UCHAR
  524. Msglogmbe(
  525. DWORD state, // Final state of message
  526. DWORD net, // Which network?
  527. DWORD ncbi // Network Control Block index
  528. );
  529. DWORD
  530. Msglogmbt(
  531. LPSTR text, // Text of message
  532. DWORD net, // Which network?
  533. DWORD ncbi // Network Control Block index
  534. );
  535. DWORD
  536. Msglogsbm(
  537. LPSTR from, // Name of sender
  538. LPSTR to, // Name of recipient
  539. LPSTR text, // Text of message
  540. ULONG SessionId // Session Id
  541. );
  542. VOID
  543. Msgmbmfree(
  544. DWORD mesi
  545. );
  546. DWORD
  547. Msgmbmprint(
  548. int action,
  549. DWORD mesi,
  550. HANDLE file_handle,
  551. LPDWORD pdwAlertFlag
  552. );
  553. DWORD
  554. MsgrCtrlHandler(
  555. IN DWORD dwControl,
  556. IN DWORD dwEventType,
  557. IN LPVOID lpEventData,
  558. IN LPVOID lpContext
  559. );
  560. DWORD
  561. Msgopen_append(
  562. LPSTR file_name, // Name of file to open
  563. PHANDLE file_handle_ptr // pointer to storage for file handle
  564. );
  565. UCHAR
  566. Msgsendncb(
  567. PNCB NcbPtr,
  568. DWORD neti
  569. );
  570. int
  571. Msgsmbcheck(
  572. LPBYTE buffer,
  573. USHORT size,
  574. UCHAR func,
  575. int parms,
  576. LPSTR fields
  577. );
  578. NET_API_STATUS
  579. MsgStartListen(
  580. DWORD net,
  581. DWORD ncbi
  582. );
  583. DWORD
  584. Msgtxtprint(
  585. int action, // Alert, File, or Alert and file
  586. LPSTR text, // Pointer to text
  587. DWORD length, // Length of text
  588. HANDLE file_handle // Log file handle
  589. );
  590. NET_API_STATUS
  591. MsgInitSupportSeg(
  592. VOID
  593. );
  594. VOID
  595. MsgFreeSupportSeg(
  596. VOID
  597. );
  598. VOID
  599. MsgFreeSharedData(
  600. VOID
  601. );
  602. BOOL
  603. MsgCreateWakeupSems(
  604. DWORD NumNets
  605. );
  606. BOOL
  607. MsgCreateWakeupEvent(
  608. void
  609. );
  610. VOID
  611. MsgCloseWakeupSems(
  612. VOID
  613. );
  614. VOID
  615. MsgCloseWakeupEvent(
  616. VOID
  617. );
  618. NET_API_STATUS
  619. MsgInitGroupSupport(
  620. DWORD iGrpMailslotWakeupSem
  621. );
  622. VOID
  623. MsgGrpThreadShutdown(
  624. VOID
  625. );
  626. DWORD
  627. MsgGetNumNets(
  628. VOID
  629. );
  630. NET_API_STATUS
  631. MultiUserInitMessage(
  632. VOID
  633. );
  634. VOID
  635. MsgArrivalBeep(
  636. ULONG SessionId
  637. );
  638. INT
  639. DisplayMessage(
  640. LPWSTR pMessage,
  641. LPWSTR pTitle,
  642. ULONG SessionId
  643. );
  644. NET_API_STATUS
  645. MsgGetClientSessionId(
  646. OUT PULONG pSessionId
  647. );
  648. VOID
  649. MsgNetEventCompletion(
  650. PVOID pvContext, // This passed in as context.
  651. BOOLEAN fWaitStatus
  652. );
  653. #endif // MSRV_INCLUDED