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.

562 lines
14 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  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. // Note: calls to NetpGetDomainId should eventually be replaced by calls
  299. // to NetpGetLocalDomainId. --JR
  300. NET_API_STATUS
  301. NetpGetDomainId (
  302. OUT PSID *RetDomainId // alloc and set ptr (free with LocalFree)
  303. );
  304. NET_API_STATUS
  305. NetpGetDomainName (
  306. OUT LPWSTR *DomainNamePtr // alloc and set ptr (free with NetApiBufferFree)
  307. );
  308. NET_API_STATUS
  309. NetpGetDomainNameEx (
  310. OUT LPWSTR *DomainNamePtr, // alloc and set ptr (free with NetApiBufferFree)
  311. OUT PBOOLEAN IsWorkgroupName
  312. );
  313. NET_API_STATUS
  314. NetpGetDomainNameExEx (
  315. OUT LPWSTR *DomainNamePtr,
  316. OUT LPWSTR *DnsDomainNamePtr OPTIONAL,
  317. OUT PBOOLEAN IsWorkgroupName
  318. );
  319. #ifndef GUID_DEFINED
  320. #define GUID_DEFINED
  321. typedef struct _GUID
  322. {
  323. DWORD Data1;
  324. WORD Data2;
  325. WORD Data3;
  326. BYTE Data4[ 8 ];
  327. } GUID;
  328. #endif // !GUID_DEFINED
  329. NET_API_STATUS
  330. NetpGetDomainNameExExEx (
  331. OUT LPTSTR *DomainNamePtr,
  332. OUT LPTSTR *DnsDomainNamePtr OPTIONAL,
  333. OUT LPTSTR *DnsForestNamePtr OPTIONAL,
  334. OUT GUID **DomainGuidPtr OPTIONAL,
  335. OUT PBOOLEAN IsWorkgroupName
  336. );
  337. typedef enum _LOCAL_DOMAIN_TYPE {
  338. LOCAL_DOMAIN_TYPE_ACCOUNTS,
  339. LOCAL_DOMAIN_TYPE_BUILTIN,
  340. LOCAL_DOMAIN_TYPE_PRIMARY
  341. } LOCAL_DOMAIN_TYPE, *PLOCAL_DOMAIN_TYPE, *LPLOCAL_DOMAIN_TYPE;
  342. NET_API_STATUS
  343. NetpGetLocalDomainId (
  344. IN LOCAL_DOMAIN_TYPE TypeWanted,
  345. OUT PSID *RetDomainId // alloc and set ptr (free with LocalFree)
  346. );
  347. //
  348. // NetService API helpers
  349. //
  350. // BOOL
  351. // NetpIsServiceLevelValid(
  352. // IN DWORD Level
  353. // );
  354. //
  355. #define NetpIsServiceLevelValid( Level ) \
  356. ( ((Level)==0) || ((Level)==1) || ((Level)==2) )
  357. NET_API_STATUS
  358. NetpTranslateNamesInServiceArray(
  359. IN DWORD Level,
  360. IN LPVOID ArrayBase,
  361. IN DWORD EntryCount,
  362. IN BOOL PreferNewStyle,
  363. OUT LPVOID * NewArrayBase
  364. );
  365. NET_API_STATUS
  366. NetpTranslateServiceName(
  367. IN LPWSTR GivenServiceName,
  368. IN BOOL PreferNewStyle,
  369. OUT LPWSTR * TranslatedName
  370. );
  371. //
  372. // Mapping routines to map DosxxxMessage API's to FormatMessage
  373. //
  374. WORD
  375. DosGetMessage(
  376. IN LPSTR * InsertionStrings,
  377. IN WORD NumberofStrings,
  378. OUT LPBYTE Buffer,
  379. IN WORD BufferLength,
  380. IN WORD MessageId,
  381. IN LPWSTR FileName,
  382. OUT PWORD pMessageLength
  383. );
  384. DWORD
  385. NetpGetPrivilege(
  386. IN DWORD numPrivileges,
  387. IN PULONG pulPrivileges
  388. );
  389. DWORD
  390. NetpReleasePrivilege(
  391. VOID
  392. );
  393. DWORD
  394. NetpWriteEventlog(
  395. LPWSTR Source,
  396. DWORD EventID,
  397. DWORD EventType,
  398. DWORD NumStrings,
  399. LPWSTR *Strings,
  400. DWORD DataLength,
  401. LPVOID Data
  402. );
  403. DWORD
  404. NetpRaiseAlert(
  405. IN LPWSTR ServiceName,
  406. IN DWORD alert_no,
  407. IN LPWSTR *string_array
  408. );
  409. //
  410. // Special flags to NetpEventlogWrite
  411. //
  412. #define NETP_LAST_MESSAGE_IS_NTSTATUS 0x80000000
  413. #define NETP_LAST_MESSAGE_IS_NETSTATUS 0x40000000
  414. #define NETP_ALLOW_DUPLICATE_EVENTS 0x20000000
  415. #define NETP_RAISE_ALERT_TOO 0x10000000
  416. #define NETP_STRING_COUNT_MASK 0x000FFFFF
  417. HANDLE
  418. NetpEventlogOpen (
  419. IN LPWSTR Source,
  420. IN ULONG DuplicateEventlogTimeout
  421. );
  422. DWORD
  423. NetpEventlogWrite (
  424. IN HANDLE NetpEventHandle,
  425. IN DWORD EventId,
  426. IN DWORD EventType,
  427. IN LPBYTE RawDataBuffer OPTIONAL,
  428. IN DWORD RawDataSize,
  429. IN LPWSTR *StringArray,
  430. IN DWORD StringCount
  431. );
  432. //
  433. // extended version with re-arranged parameters + category, to be more
  434. // compatible with ReportEvent().
  435. //
  436. DWORD
  437. NetpEventlogWriteEx (
  438. IN HANDLE NetpEventHandle,
  439. IN DWORD EventType,
  440. IN DWORD EventCategory,
  441. IN DWORD EventId,
  442. IN DWORD StringCount,
  443. IN DWORD RawDataSize,
  444. IN LPWSTR *StringArray,
  445. IN LPVOID RawDataBuffer OPTIONAL
  446. );
  447. VOID
  448. NetpEventlogClearList (
  449. IN HANDLE NetpEventHandle
  450. );
  451. VOID
  452. NetpEventlogSetTimeout (
  453. IN HANDLE NetpEventHandle,
  454. IN ULONG DuplicateEventlogTimeout
  455. );
  456. VOID
  457. NetpEventlogClose (
  458. IN HANDLE NetpEventHandle
  459. );
  460. #ifdef __cplusplus
  461. }
  462. #endif
  463. #endif // ndef _NETLIB_