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.

573 lines
15 KiB

  1. /*++
  2. Copyright (c) 1991-1993 Microsoft Corporation
  3. Module Name:
  4. NetLib.h
  5. Abstract:
  6. This header file declares various common routines for use in the
  7. networking code.
  8. Author:
  9. John Rogers (JohnRo) 14-Mar-1991
  10. Environment:
  11. Portable to any flat, 32-bit environment. (Uses Win32 typedefs.)
  12. Requires ANSI C extensions: slash-slash comments, long external names.
  13. Notes:
  14. You must include <windows.h> and <lmcons.h> before this file.
  15. Revision History:
  16. 14-Mar-1991 JohnRo
  17. Created.
  18. 20-Mar-1991 JohnRo
  19. Moved NetpPackString here (was NetapipPackString). Removed tabs.
  20. 21-Mar-1991 RitaW
  21. Added NetpCopyStringToBuffer.
  22. 02-Apr-1991 JohnRo
  23. Moved NetpRdrFsControlTree to <netlibnt.h>.
  24. 03-Apr-1991 JohnRo
  25. Fixed types for NetpCopyStringToBuffer.
  26. 08-Apr-1991 CliffV
  27. Added NetpCopyDataToBuffer
  28. 10-Apr-1991 JohnRo
  29. Added NetpSetParmError (descended from CliffV's SetParmError).
  30. 10-Apr-1991 Danl
  31. Added NetpGetComputerName
  32. 24-Apr-1991 JohnRo
  33. Avoid conflicts with MIDL-generated files.
  34. Added NetpAdjustPreferedMaximum().
  35. NetpCopyStringToBuffer's input string ptr is optional.
  36. 26-Apr-1991 CliffV
  37. Added NetpAllocateEnumBuffer.
  38. Added typedefs PTRDIFF_T and BUFFER_DESCRIPTOR.
  39. 16-Apr-1991 JohnRo
  40. Clarify UNICODE handling of pack and copy routines.
  41. 24-Jul-1991 JohnRo
  42. Provide NetpIsServiceStarted() for use by <netrpc.h> macros.
  43. 29-Oct-1991 JohnRo
  44. Added NetpChangeNullCharToNullPtr() macro.
  45. 29-Oct-1991 Danhi
  46. Add function prototypes for DosxxxMessage Api's
  47. 20-Nov-1991 JohnRo
  48. Removed NT dependencies to reduce recompiles.
  49. 09-Jan-1992 JohnRo
  50. Added NetpGetDomainName().
  51. 23-Jan-1992 JohnRo
  52. Added IN_RANGE() macro based on MadanA's RANGECHECK().
  53. 25-Mar-1992 RitaW
  54. Added SET_SERVICE_EXITCODE() macro for setting Win32 vs
  55. service specific exitcode.
  56. 06-May-1992 JohnRo
  57. Added NetpGetLocalDomainId() for PortUAS.
  58. Added NetpTranslateServiceName() for service controller APIs.
  59. 27-Jul-1992 Madana
  60. Added NetpWriteEventlog function proto type.
  61. 05-Aug-1992 JohnRo
  62. RAID 3021: NetService APIs don't always translate svc names.
  63. 09-Sep-1992 JohnRo
  64. RAID 1090: net start/stop "" causes assertion.
  65. 14-Oct-1992 JohnRo
  66. RAID 9020: setup: PortUas fails ("prompt on conflicts" version).
  67. 02-Nov-1992 JohnRo
  68. Added NetpIsRemoteServiceStarted().
  69. 15-Feb-1993 JohnRo
  70. RAID 10685: user name not in repl event log.
  71. 24-Mar-1993 JohnRo
  72. Repl svc shuold use DBFlag in registry.
  73. 05-Aug-1993 JohnRo
  74. RAID 17010: Implement per-first-level-directory change notify.
  75. 19-Aug-1993 JohnRo
  76. RAID 2822: PortUAS maps chars funny. (Workaround FormatMessageA bug.)
  77. RAID 3094: PortUAS displays chars incorrectly.
  78. --*/
  79. #ifndef _NETLIB_
  80. #define _NETLIB_
  81. // These may be included in any order:
  82. #include <string.h> // memcpy().
  83. // Don't complain about "unneeded" includes of this file:
  84. /*lint -efile(764,wchar.h) */
  85. /*lint -efile(766,wchar.h) */
  86. #include <wchar.h> // iswdigit().
  87. #ifdef CDEBUG // Debug in ANSI C environment?
  88. #include <netdebug.h> // NetpAssert().
  89. #endif // ndef CDEBUG
  90. #ifdef __cplusplus
  91. extern "C" {
  92. #endif
  93. //
  94. // IN_RANGE(): Make sure SomeValue is between SomeMin and SomeMax.
  95. // Beware side-effects (SomeValue is evaluated twice).
  96. // Created by JohnRo from MadanA's RANGECHECK().
  97. //
  98. // BOOL
  99. // IN_RANGE(
  100. // IN DWORD SomeValue,
  101. // IN DWORD SomeMin,
  102. // IN DWORD SomeMax
  103. // );
  104. //
  105. #define IN_RANGE(SomeValue, SomeMin, SomeMax) \
  106. ( ((SomeValue) >= (SomeMin)) && ((SomeValue) <= (SomeMax)) )
  107. //
  108. // SET_SERVICE_EXITCODE() sets the SomeApiStatus to NetCodeVariable
  109. // if it is within the NERR_BASE and NERR_MAX range. Otherwise,
  110. // Win32CodeVariable is set. This original code came from JohnRo.
  111. //
  112. #define SET_SERVICE_EXITCODE(SomeApiStatus, Win32CodeVariable, NetCodeVariable) \
  113. { \
  114. if ((SomeApiStatus) == NERR_Success) { \
  115. (Win32CodeVariable) = NO_ERROR; \
  116. (NetCodeVariable) = NERR_Success; \
  117. } else if (! IN_RANGE((SomeApiStatus), MIN_LANMAN_MESSAGE_ID, MAX_LANMAN_MESSAGE_ID)) { \
  118. (Win32CodeVariable) = (DWORD) (SomeApiStatus); \
  119. (NetCodeVariable) = (DWORD) (SomeApiStatus); \
  120. } else { \
  121. (Win32CodeVariable) = ERROR_SERVICE_SPECIFIC_ERROR; \
  122. (NetCodeVariable) = (DWORD) (SomeApiStatus); \
  123. } \
  124. }
  125. VOID
  126. NetpAdjustPreferedMaximum (
  127. IN DWORD PreferedMaximum,
  128. IN DWORD EntrySize,
  129. IN DWORD Overhead,
  130. OUT LPDWORD BytesToAllocate OPTIONAL,
  131. OUT LPDWORD EntriesToAllocate OPTIONAL
  132. );
  133. // Portable memory move/copy routine: This is intended to have exactly
  134. // the semantics of ANSI C's memcpy() routine, except that the byte count
  135. // is 32 bits long.
  136. //
  137. // VOID
  138. // NetpMoveMemory(
  139. // OUT LPBYTE Dest, // Destination (must not be NULL).
  140. // IN LPBYTE Src, // Source
  141. // IN DWORD Size // Byte count
  142. // );
  143. #ifdef CDEBUG
  144. // Note that C6 version doesn't allow 32-bit Size, hence the
  145. // assertion. Replace this macro with another if this is a problem.
  146. #define NetpMoveMemory(Dest,Src,Size) \
  147. { \
  148. NetpAssert( (Size) == (DWORD) (size_t) (Size)); \
  149. (void) memcpy( (Dest), (Src), (size_t) (Size) ); \
  150. }
  151. #else // ndef CDEBUG
  152. #define NetpMoveMemory(Dest,Src,Size) \
  153. (void) memcpy( (Dest), (Src), (size_t) (Size) )
  154. #endif // ndef CDEBUG
  155. DWORD
  156. NetpPackString(
  157. IN OUT LPWSTR * string, // pointer by reference: string to be copied.
  158. IN LPBYTE dataend, // pointer to end of fixed size data.
  159. IN OUT LPWSTR * laststring // pointer by reference: top of string data.
  160. );
  161. //
  162. // This routine is like NetpPackString, except that it does not expect the
  163. // caller to assign the pointer of the source string to the variable in the
  164. // fixed size structure before the call. It also takes a string character
  165. // count parameter instead of calling strlen on String.
  166. //
  167. BOOL
  168. NetpCopyStringToBuffer (
  169. IN LPWSTR String OPTIONAL,
  170. IN DWORD CharacterCount,
  171. IN LPBYTE FixedDataEnd,
  172. IN OUT LPWSTR *EndOfVariableData,
  173. OUT LPWSTR *VariableDataPointer
  174. );
  175. //
  176. // This routine is like NetpCopyStringToBuffer except it copies any data
  177. // (not just strings), it does not put a zero byte at the end of the
  178. // data, and it allows the alignment of the resultant copied data to be
  179. // specified.
  180. //
  181. BOOL
  182. NetpCopyDataToBuffer (
  183. IN LPBYTE Data,
  184. IN DWORD ByteCount,
  185. IN LPBYTE FixedDataEnd,
  186. IN OUT LPBYTE *EndOfVariableData,
  187. OUT LPBYTE *VariableDataPointer,
  188. IN DWORD Alignment
  189. );
  190. //
  191. // Declare a type for the difference between two pointers.
  192. //
  193. // This must be at least as long as a ptrdiff_t but we don't want to
  194. // add a dependency on <stddef.h> here.
  195. //
  196. typedef DWORD_PTR PTRDIFF_T;
  197. //
  198. // Declare a description of an enumeration buffer.
  199. //
  200. typedef struct _BUFFER_DESCRIPTOR {
  201. LPBYTE Buffer; // Pointer to the allocated buffer.
  202. DWORD AllocSize; // Current size of the allocated buffer.
  203. DWORD AllocIncrement; // Amount to increment size by on each reallocate.
  204. LPBYTE EndOfVariableData;// Pointer past last avaliable byte of string space
  205. LPBYTE FixedDataEnd; // Pointer past last used byte of fixed data space
  206. } BUFFER_DESCRIPTOR, *PBUFFER_DESCRIPTOR;
  207. //
  208. // This routine handles all the details of allocating and growing a
  209. // buffer returned from an enumeration function. It takes the users
  210. // prefered maximum size into consideration.
  211. //
  212. #define NETP_ENUM_GUESS 16384 // Initial guess for enumeration buffer size
  213. NET_API_STATUS
  214. NetpAllocateEnumBuffer(
  215. IN OUT PBUFFER_DESCRIPTOR BufferDescriptor,
  216. // Caller must deallocate BD->Buffer using MIDL_user_free.
  217. IN BOOL IsGet,
  218. IN DWORD PrefMaxSize,
  219. IN DWORD NeededSize,
  220. IN VOID (*RelocationRoutine)( IN DWORD RelocationParameter,
  221. IN OUT PBUFFER_DESCRIPTOR BufferDescriptor,
  222. IN PTRDIFF_T Offset ),
  223. IN DWORD RelocationParameter
  224. );
  225. NET_API_STATUS
  226. NetpAllocateEnumBufferEx(
  227. IN OUT PBUFFER_DESCRIPTOR BufferDescriptor,
  228. IN BOOL IsGet,
  229. IN DWORD PrefMaxSize,
  230. IN DWORD NeededSize,
  231. IN VOID (*RelocationRoutine)( IN DWORD RelocationParameter,
  232. IN OUT PBUFFER_DESCRIPTOR BufferDescriptor,
  233. IN PTRDIFF_T Offset ),
  234. IN DWORD RelocationParameter,
  235. IN DWORD IncrementalSize
  236. );
  237. BOOL
  238. NetpIsServiceStarted(
  239. IN LPWSTR ServiceName
  240. );
  241. //
  242. // Portable memory allocation routines. Memory is per-process only.
  243. //
  244. // Allocate memory, or return NULL if not available.
  245. LPVOID
  246. NetpMemoryAllocate(
  247. IN DWORD Size
  248. );
  249. // Free memory at Address (must have been gotten from NetpMemoryAllocate or
  250. // NetpMemoryReallocate). (Address may be NULL.)
  251. VOID
  252. NetpMemoryFree(
  253. IN LPVOID Address OPTIONAL
  254. );
  255. // Reallocate block (now at OldAddress) to NewSize. OldAddress may be NULL.
  256. // Contents of block are copied if necessary. Returns NULL if unable to
  257. // allocate additional storage.
  258. LPVOID
  259. NetpMemoryReallocate(
  260. IN LPVOID OldAddress OPTIONAL,
  261. IN DWORD NewSize
  262. );
  263. //
  264. // Random handy macros:
  265. //
  266. #define NetpPointerPlusSomeBytes(p,n) \
  267. (LPBYTE) ( ( (LPBYTE) (p)) + (n) )
  268. #define NetpSetOptionalArg(arg, value) \
  269. { \
  270. if ((arg) != NULL) { \
  271. *(arg) = (value); \
  272. } \
  273. }
  274. //
  275. // Set the optional ParmError parameter
  276. //
  277. #define NetpSetParmError( _ParmNumValue ) \
  278. if ( ParmError != NULL ) { \
  279. *ParmError = (_ParmNumValue); \
  280. }
  281. #if defined(lint) || defined(_lint)
  282. #define UNUSED(x) { (x) = (x); }
  283. #else
  284. #define UNUSED(x) { (void) (x); }
  285. #endif
  286. //
  287. // NetpGetComputerName retrieves the local computername from the local
  288. // configuration database.
  289. //
  290. NET_API_STATUS
  291. NetpGetComputerName (
  292. IN LPWSTR *ComputerNamePtr);
  293. NET_API_STATUS
  294. NetpGetComputerNameEx (
  295. IN LPWSTR *ComputerNamePtr,
  296. IN BOOL PhysicalNetbiosName
  297. );
  298. NET_API_STATUS
  299. NetpGetDomainName (
  300. OUT LPWSTR *DomainNamePtr // alloc and set ptr (free with NetApiBufferFree)
  301. );
  302. NET_API_STATUS
  303. NetpGetDomainNameEx (
  304. OUT LPWSTR *DomainNamePtr, // alloc and set ptr (free with NetApiBufferFree)
  305. OUT PBOOLEAN IsWorkgroupName
  306. );
  307. NET_API_STATUS
  308. NetpGetDomainNameExEx (
  309. OUT LPWSTR *DomainNamePtr,
  310. OUT LPWSTR *DnsDomainNamePtr OPTIONAL,
  311. OUT PBOOLEAN IsWorkgroupName
  312. );
  313. #ifndef GUID_DEFINED
  314. #define GUID_DEFINED
  315. typedef struct _GUID
  316. {
  317. DWORD Data1;
  318. WORD Data2;
  319. WORD Data3;
  320. BYTE Data4[ 8 ];
  321. } GUID;
  322. #endif // !GUID_DEFINED
  323. NET_API_STATUS
  324. NetpGetDomainNameExExEx (
  325. OUT LPTSTR *DomainNamePtr,
  326. OUT LPTSTR *DnsDomainNamePtr OPTIONAL,
  327. OUT LPTSTR *DnsForestNamePtr OPTIONAL,
  328. OUT GUID **DomainGuidPtr OPTIONAL,
  329. OUT PBOOLEAN IsWorkgroupName
  330. );
  331. typedef enum _LOCAL_DOMAIN_TYPE {
  332. LOCAL_DOMAIN_TYPE_ACCOUNTS,
  333. LOCAL_DOMAIN_TYPE_BUILTIN,
  334. LOCAL_DOMAIN_TYPE_PRIMARY
  335. } LOCAL_DOMAIN_TYPE, *PLOCAL_DOMAIN_TYPE, *LPLOCAL_DOMAIN_TYPE;
  336. NET_API_STATUS
  337. NetpGetLocalDomainId (
  338. IN LOCAL_DOMAIN_TYPE TypeWanted,
  339. OUT PSID *RetDomainId // alloc and set ptr (free with LocalFree)
  340. );
  341. //
  342. // NetService API helpers
  343. //
  344. // BOOL
  345. // NetpIsServiceLevelValid(
  346. // IN DWORD Level
  347. // );
  348. //
  349. #define NetpIsServiceLevelValid( Level ) \
  350. ( ((Level)==0) || ((Level)==1) || ((Level)==2) )
  351. NET_API_STATUS
  352. NetpTranslateNamesInServiceArray(
  353. IN DWORD Level,
  354. IN LPVOID ArrayBase,
  355. IN DWORD EntryCount,
  356. IN BOOL PreferNewStyle,
  357. OUT LPVOID * NewArrayBase
  358. );
  359. NET_API_STATUS
  360. NetpTranslateServiceName(
  361. IN LPWSTR GivenServiceName,
  362. IN BOOL PreferNewStyle,
  363. OUT LPWSTR * TranslatedName
  364. );
  365. //
  366. // Mapping routines to map DosxxxMessage API's to FormatMessage
  367. //
  368. WORD
  369. DosGetMessage(
  370. IN LPSTR * InsertionStrings,
  371. IN WORD NumberofStrings,
  372. OUT LPBYTE Buffer,
  373. IN WORD BufferLength,
  374. IN WORD MessageId,
  375. IN LPWSTR FileName,
  376. OUT PWORD pMessageLength
  377. );
  378. DWORD
  379. NetpGetPrivilege(
  380. IN DWORD numPrivileges,
  381. IN PULONG pulPrivileges
  382. );
  383. DWORD
  384. NetpReleasePrivilege(
  385. VOID
  386. );
  387. DWORD
  388. NetpWriteEventlog(
  389. LPWSTR Source,
  390. DWORD EventID,
  391. DWORD EventType,
  392. DWORD NumStrings,
  393. LPWSTR *Strings,
  394. DWORD DataLength,
  395. LPVOID Data
  396. );
  397. DWORD
  398. NetpRaiseAlert(
  399. IN LPWSTR ServiceName,
  400. IN DWORD alert_no,
  401. IN LPWSTR *string_array
  402. );
  403. //
  404. // Special flags to NetpEventlogWrite
  405. //
  406. #define NETP_LAST_MESSAGE_IS_NTSTATUS 0x80000000
  407. #define NETP_LAST_MESSAGE_IS_NETSTATUS 0x40000000
  408. #define NETP_ALLOW_DUPLICATE_EVENTS 0x20000000
  409. #define NETP_RAISE_ALERT_TOO 0x10000000
  410. #define NETP_STRING_COUNT_MASK 0x000FFFFF
  411. HANDLE
  412. NetpEventlogOpen (
  413. IN LPWSTR Source,
  414. IN ULONG DuplicateEventlogTimeout
  415. );
  416. DWORD
  417. NetpEventlogWrite (
  418. IN HANDLE NetpEventHandle,
  419. IN DWORD EventId,
  420. IN DWORD EventType,
  421. IN LPBYTE RawDataBuffer OPTIONAL,
  422. IN DWORD RawDataSize,
  423. IN LPWSTR *StringArray,
  424. IN DWORD StringCount
  425. );
  426. //
  427. // extended version with re-arranged parameters + category, to be more
  428. // compatible with ReportEvent().
  429. //
  430. DWORD
  431. NetpEventlogWriteEx (
  432. IN HANDLE NetpEventHandle,
  433. IN DWORD EventType,
  434. IN DWORD EventCategory,
  435. IN DWORD EventId,
  436. IN DWORD StringCount,
  437. IN DWORD RawDataSize,
  438. IN LPWSTR *StringArray,
  439. IN LPVOID RawDataBuffer OPTIONAL
  440. );
  441. //
  442. // even more extended version to allow specifying
  443. // arbitrary message index for the status code
  444. //
  445. DWORD
  446. NetpEventlogWriteEx2 (
  447. IN HANDLE NetpEventHandle,
  448. IN DWORD EventType,
  449. IN DWORD EventCategory,
  450. IN DWORD EventId,
  451. IN DWORD StringCount,
  452. IN DWORD StatusMessageIndex,
  453. IN DWORD RawDataSize,
  454. IN LPWSTR *StringArray,
  455. IN LPVOID pvRawDataBuffer OPTIONAL
  456. );
  457. VOID
  458. NetpEventlogClearList (
  459. IN HANDLE NetpEventHandle
  460. );
  461. VOID
  462. NetpEventlogSetTimeout (
  463. IN HANDLE NetpEventHandle,
  464. IN ULONG DuplicateEventlogTimeout
  465. );
  466. VOID
  467. NetpEventlogClose (
  468. IN HANDLE NetpEventHandle
  469. );
  470. #ifdef __cplusplus
  471. }
  472. #endif
  473. #endif // ndef _NETLIB_