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.

342 lines
8.0 KiB

  1. /*
  2. ** wcsutil.h
  3. ** Sean P. Nolan
  4. **
  5. ** server-side utils
  6. */
  7. #ifndef _WCSUTIL_H_
  8. #define _WCSUTIL_H_
  9. #include <windows.h>
  10. #include <ole2.h>
  11. #include <winsock.h>
  12. #include <httpext.h>
  13. #include "wcguids.h"
  14. /*--------------------------------------------------------------------------+
  15. | Utilities Provided by the Framework |
  16. +--------------------------------------------------------------------------*/
  17. extern LPSTR g_szSSOProgID;
  18. // Logging to application event log -- see ReportEvent() in win32 sdk
  19. // documentation for information on wType and dwEventID.
  20. void LogEvent(WORD wType, DWORD dwEventID, char *sz);
  21. // Asserts
  22. #ifdef DEBUG
  23. extern void AssertProc(PCSTR szFile, DWORD dwLine, PCSTR szMsg, DWORD dwFlags);
  24. #define AssertSzFlg(f, sz, dwFlag) ( (f) ? 0 : AssertProc(__FILE__, __LINE__, sz, dwFlag))
  25. #else // !DEBUG
  26. #define AssertSzFlg(f, sz, dwFlg)
  27. #endif // !DEBUG
  28. #define AP_GETLASTERROR 0x00000001
  29. // under non-debug, AssertSzFlg is defined away, so these #defines don't
  30. // have to be #ifdef DEBUG
  31. #define AssertSz(f, sz) AssertSzFlg(f, sz, 0)
  32. #define AssertEx(f) AssertSz((f), "!(" #f ")")
  33. #define Assert(f) AssertEx(f)
  34. #define AssertSzGLE(f, sz) AssertSzFlg(f, sz, AP_GETLASTERROR)
  35. #define AssertExGLE(f) AssertSzGLE((f), "!(" #f ")")
  36. // Debug Printing
  37. #ifdef DEBUG
  38. int dbgprintf(PCSTR pFormat, ...);
  39. #else
  40. #define dbgprintf()
  41. #endif
  42. // Memory Mgmt
  43. LPVOID _MsnAlloc(DWORD cb);
  44. LPVOID _MsnRealloc(LPVOID pv, DWORD cb);
  45. void _MsnFree(LPVOID pv);
  46. // Data Munging
  47. LPSTR _SzFromVariant(VARIANT *pvar);
  48. BSTR _BstrFromVariant(VARIANT *pvar, BOOL *pfFree);
  49. BOOL _FIntFromVariant(VARIANT *pvar, int *pi);
  50. // String Manipulation / Parsing
  51. void _AnsiStringFromGuid(REFGUID rguid, LPSTR sz);
  52. LPSTR _SkipWhiteSpace(LPSTR sz);
  53. LPSTR _FindEndOfLine(LPSTR sz);
  54. DWORD _atoi(LPSTR sz);
  55. // URL Fiddling
  56. int UrlType(char *szUrl);
  57. #define URL_TYPE_ABSOLUTE 0 // e.g. http://foo/bar or just //foo.com/bar.htm
  58. #define URL_TYPE_LOCAL_ABSOLUTE 1 // e.g. /foo/bar/baz.htm
  59. #define URL_TYPE_RELATIVE 2 // e.g. xyzzy/plugh.html
  60. // IIS Hacks
  61. #ifdef _HTTPEXT_H_
  62. BOOL _FTranslateVirtualRoot(EXTENSION_CONTROL_BLOCK *pecb, LPSTR szPathIn,
  63. LPSTR szPathTranslated, DWORD cbPathTranslated);
  64. #endif // _HTTPEXT_H_
  65. // Critical Sections
  66. class CCritSec
  67. {
  68. private:
  69. CRITICAL_SECTION m_cs;
  70. public:
  71. CCritSec(void) { ::InitializeCriticalSection(&m_cs); }
  72. ~CCritSec(void) { ::DeleteCriticalSection(&m_cs); }
  73. void Lock(void) { ::EnterCriticalSection(&m_cs); }
  74. void Unlock(void) { ::LeaveCriticalSection(&m_cs); }
  75. };
  76. /*--------------------------------------------------------------------------+
  77. | CThingWatcher/CFileWatcher/CRegKeyWatcher |
  78. +--------------------------------------------------------------------------*/
  79. typedef void (*PFNFILECHANGED)(LPSTR szFile, LONG lUser);
  80. typedef void (*PFNREGKEYCHANGED)(HKEY hkey, LONG lUser);
  81. typedef int (__stdcall *PFNCLOSEHEVTNOTIFY)(HANDLE hevtNotify);
  82. class CThingWatcher
  83. {
  84. friend DWORD ThingWatcherThread(CThingWatcher *pfw);
  85. public:
  86. CThingWatcher(PFNCLOSEHEVTNOTIFY pfnCloseHevtNotify);
  87. ~CThingWatcher();
  88. BOOL FWatchHandle(HANDLE hevtNotify);
  89. private:
  90. virtual BOOL FireChange(DWORD dwWait) = 0;
  91. HANDLE m_rghWait[2];
  92. HINSTANCE m_hModule;
  93. PFNCLOSEHEVTNOTIFY m_pfnCloseHevtNotify;
  94. };
  95. class CFileWatcher : public CThingWatcher
  96. {
  97. private:
  98. char m_szPath[MAX_PATH];
  99. FILETIME m_ftLastWrite;
  100. LONG m_lUser;
  101. PFNFILECHANGED m_pfnChanged;
  102. BOOL m_fDirectory;
  103. HANDLE m_hevtNotify;
  104. public:
  105. CFileWatcher(void);
  106. ~CFileWatcher(void);
  107. BOOL FStartWatching(LPSTR szPath, LONG lUser, PFNFILECHANGED pfnChanged);
  108. private:
  109. virtual BOOL FireChange(DWORD dwWait);
  110. };
  111. class CRegKeyWatcher : public CThingWatcher
  112. {
  113. public:
  114. CRegKeyWatcher();
  115. ~CRegKeyWatcher();
  116. BOOL FStartWatching(HKEY hkey, BOOL fSubTree, DWORD dwNotifyFilter, LONG lUser, PFNREGKEYCHANGED pfnChanged);
  117. private:
  118. virtual BOOL FireChange(DWORD dwWait);
  119. HKEY m_hkey;
  120. LONG m_lUser;
  121. PFNREGKEYCHANGED m_pfnChanged;
  122. HANDLE m_hevtNotify;
  123. };
  124. /*--------------------------------------------------------------------------+
  125. | CDataFile |
  126. +--------------------------------------------------------------------------*/
  127. class CDataFile
  128. {
  129. friend class CDataFileGroup;
  130. friend void DataFileChanged(LPSTR szFile, LONG lUser);
  131. private:
  132. ULONG m_cRef;
  133. CDataFile *m_pdfNext;
  134. CDataFile *m_pdfPrev;
  135. CDataFileGroup *m_pfg;
  136. protected:
  137. CCritSec m_cs;
  138. char m_szDataPath[MAX_PATH];
  139. CFileWatcher m_fw;
  140. public:
  141. CDataFile(LPSTR szDataPath, CDataFileGroup *pfg);
  142. ~CDataFile(void);
  143. ULONG AddRef(void);
  144. ULONG Release(void);
  145. BOOL FWatchFile(PFNFILECHANGED pfnChanged = NULL);
  146. virtual void FreeDataFile(void) { delete this; }
  147. private:
  148. ULONG GetRefCount(void);
  149. BOOL FMatch(LPSTR szDataPath);
  150. CDataFile *GetNext(void) { return(m_pdfNext); }
  151. CDataFile *GetPrev(void) { return(m_pdfPrev); }
  152. void SetNext(CDataFile *pdf) { m_pdfNext = pdf; }
  153. void SetPrev(CDataFile *pdf) { m_pdfPrev = pdf; }
  154. };
  155. /*--------------------------------------------------------------------------+
  156. | CDataFileGroup |
  157. +--------------------------------------------------------------------------*/
  158. #define NUM_GROUP_BUCKETS (1024)
  159. typedef struct _HashBucket
  160. {
  161. CDataFile *pdfHead;
  162. CDataFile *pdfTail;
  163. }
  164. HB;
  165. class CDataFileGroup
  166. {
  167. private:
  168. CCritSec m_cs;
  169. HB m_rghb[NUM_GROUP_BUCKETS];
  170. public:
  171. CDataFileGroup(void);
  172. ~CDataFileGroup(void);
  173. CDataFile *GetDataFile(LPSTR szDataPath);
  174. void ForgetDataFile(CDataFile *pdf);
  175. virtual CDataFile *CreateDataFile(LPSTR szDataPath) = 0;
  176. private:
  177. void RememberDataFile(CDataFile *pdf, HB *phb = NULL);
  178. HB *GetHashBucket(LPSTR szDataPath);
  179. };
  180. /*--------------------------------------------------------------------------+
  181. | CGenericHash |
  182. +--------------------------------------------------------------------------*/
  183. typedef struct _HashItem
  184. {
  185. BSTR bstrName;
  186. LPVOID pvData;
  187. struct _HashItem *phiNext;
  188. struct _HashItem *phiPrev;
  189. }
  190. HITEM;
  191. class CGenericHash
  192. {
  193. protected:
  194. DWORD m_chi;
  195. HITEM **m_rgphi;
  196. CCritSec m_cs;
  197. public:
  198. CGenericHash(DWORD cBuckets);
  199. ~CGenericHash(void);
  200. LPVOID PvFind(OLECHAR *wszName);
  201. BOOL FAdd(OLECHAR *wszName, LPVOID pv);
  202. void Remove(OLECHAR *wszName);
  203. void RemoveAll(void);
  204. void Lock(void) { m_cs.Lock(); }
  205. void Unlock(void) { m_cs.Unlock(); }
  206. virtual DWORD GetHashValue(OLECHAR *wsz);
  207. virtual void FreeHashData(LPVOID pv);
  208. private:
  209. HITEM *FindItem(OLECHAR *wszName, HITEM ***ppphiHead);
  210. void RemoveItem(HITEM **pphiHead, HITEM *phi);
  211. BOOL FEnsureBuckets(void);
  212. };
  213. /*--------------------------------------------------------------------------+
  214. | CResourceCollection |
  215. +--------------------------------------------------------------------------*/
  216. typedef struct _resource
  217. {
  218. BOOL fInUse;
  219. BOOL fValid;
  220. PVOID pv;
  221. } RS, *PRS;
  222. typedef PRS HRS;
  223. #define hrsNil NULL
  224. class CResourceCollection
  225. {
  226. public:
  227. CResourceCollection();
  228. ~CResourceCollection();
  229. BOOL FInit(int cRsrc);
  230. BOOL FTerm();
  231. HRS HrsGetResource();
  232. void ReleaseResource(BOOL fReset, HRS hrs);
  233. void CleanupAll(PVOID pvNil);
  234. BOOL FValid(HRS hrs);
  235. private:
  236. PRS PrsFree();
  237. void WaitForRs();
  238. CCritSec m_cs;
  239. HANDLE m_hsem;
  240. int m_crs;
  241. PRS m_rgrs;
  242. virtual BOOL FInitResource(PRS prs) = 0;
  243. virtual void CleanupResource(PRS prs) = 0;
  244. };
  245. /*--------------------------------------------------------------------------+
  246. | CSocketCollection |
  247. +--------------------------------------------------------------------------*/
  248. class CSocketCollection : public CResourceCollection
  249. {
  250. public:
  251. CSocketCollection() : CResourceCollection() {};
  252. ~CSocketCollection() {};
  253. BOOL FInit(int cRsrc, char *szServer, USHORT usPort); // usPort should be in host byte order
  254. BOOL FReinit(char *szServer, USHORT usPort);
  255. SOCKET ScFromHrs(HRS hrs);
  256. private:
  257. char m_szServer[MAX_PATH];
  258. int m_usPort;
  259. SOCKADDR_IN m_sin;
  260. virtual BOOL FInitResource(PRS prs);
  261. virtual void CleanupResource(PRS prs);
  262. };
  263. #endif // _WCSUTIL_H_