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.

319 lines
14 KiB

  1. /*++
  2. Copyright (c) 1999, Microsoft Corporation
  3. Module Name:
  4. sample\defs.h
  5. Abstract:
  6. The file contains various...
  7. . constants
  8. . definitions
  9. . macros
  10. - memory-allocation
  11. - logging
  12. - tracing
  13. --*/
  14. #ifndef _DEFS_H_
  15. #define _DEFS_H_
  16. // constants
  17. #define INTERFACE_TABLE_BUCKETS 29 // # buckets in the interface hash table
  18. #define NOW 0
  19. #define INFINITE_INTERVAL 0x7fffffff
  20. #define PERIODIC_INTERVAL 5 // # seconds
  21. #define MAX_PACKET_LENGTH 512
  22. // definitions
  23. #define is ==
  24. #define and &&
  25. #define or ||
  26. #define GLOBAL_HEAP g_ce.hGlobalHeap
  27. #define TRACEID g_ce.dwTraceID
  28. #define LOGLEVEL g_ce.dwLogLevel
  29. #define LOGHANDLE g_ce.hLogHandle
  30. #define LOCKSTORE g_ce.dlsDynamicLocksStore
  31. // invoked when entering API or worker functions
  32. #define ENTER_SAMPLE_API() EnterSampleAPI()
  33. #define ENTER_SAMPLE_WORKER() EnterSampleWorker()
  34. // invoked when leaving API or worker functions
  35. #define LEAVE_SAMPLE_API() LeaveSampleWorker()
  36. #define LEAVE_SAMPLE_WORKER() LeaveSampleWorker()
  37. // dynamic locks
  38. #define ACQUIRE_READ_DLOCK(pLock) \
  39. AcquireDynamicReadwriteLock(&pLock, READ_MODE, LOCKSTORE)
  40. #define RELEASE_READ_DLOCK(pLock) \
  41. ReleaseDynamicReadwriteLock(pLock, READ_MODE, LOCKSTORE)
  42. #define ACQUIRE_WRITE_DLOCK(pLock) \
  43. AcquireDynamicReadwriteLock(&pLock, WRITE_MODE, LOCKSTORE)
  44. #define RELEASE_WRITE_DLOCK(pLock) \
  45. ReleaseDynamicReadwriteLock(pLock, WRITE_MODE, LOCKSTORE)
  46. // macros
  47. #define SECTOMILLISEC(x) ((x) * 1000)
  48. #define MAX(a, b) (((a) >= (b)) ? (a) : (b))
  49. #define MIN(a, b) (((a) <= (b)) ? (a) : (b))
  50. // IP ADDRESS
  51. // type
  52. typedef DWORD IPADDRESS, *PIPADDRESS;
  53. // definitions
  54. #define IP_LOWEST 0x00000000
  55. #define IP_HIGHEST 0xffffffff
  56. #define STAR 0x00000000
  57. // macros
  58. // compare a and b
  59. #define IP_COMPARE(a, b) (((a) < (b)) ? -1 \
  60. : (((a) is (b)) ? 0 : 1))
  61. // assign b to a
  62. #define IP_ASSIGN(pip, ip) *(pip) = (ip)
  63. // verify whether an ip address is valid
  64. #define IP_VALID(ip) (IP_COMPARE(ip, IP_HIGHEST) is -1)
  65. // returns the string representation of an ip address in a static buffer
  66. #define INET_NTOA(x) (inet_ntoa(*(struct in_addr*)&(x)))
  67. // TIMER
  68. // macros
  69. #define CREATE_TIMER(phHandle, pfnCallback, pvContext, ulWhen, pdwErr) \
  70. { \
  71. if (CreateTimerQueueTimer((phHandle), \
  72. g_ce.hTimerQueue, \
  73. (pfnCallback), \
  74. (pvContext), \
  75. SECTOMILLISEC(ulWhen), \
  76. INFINITE_INTERVAL, \
  77. 0)) \
  78. { \
  79. *(pdwErr) = NO_ERROR; \
  80. } \
  81. else \
  82. { \
  83. *(pdwErr) = GetLastError(); \
  84. TRACE1(ANY, "Error %u creating timer", *(pdwErr)); \
  85. LOGERR0(CREATE_TIMER_FAILED, *(pdwErr)); \
  86. } \
  87. }
  88. // it is safe to hold locks while making this call since
  89. // it is non blocking (NULL for hCompletionEvent).
  90. #define DELETE_TIMER(hHandle, pdwErr) \
  91. { \
  92. if (DeleteTimerQueueTimer(g_ce.hTimerQueue, \
  93. (hHandle), \
  94. NULL)) \
  95. { \
  96. *(pdwErr) = NO_ERROR; \
  97. } \
  98. else \
  99. { \
  100. *(pdwErr) = GetLastError(); \
  101. TRACE1(ANY, "Error %u deleting timer, continuing...", *(pdwErr)); \
  102. } \
  103. }
  104. #define RESTART_TIMER(hHandle, ulWhen, pdwErr) \
  105. { \
  106. if (ChangeTimerQueueTimer(g_ce.hTimerQueue, \
  107. (hHandle), \
  108. SECTOMILLISEC(ulWhen), \
  109. INFINITE_INTERVAL)) \
  110. { \
  111. *(pdwErr) = NO_ERROR; \
  112. } \
  113. else \
  114. { \
  115. *(pdwErr) = GetLastError(); \
  116. TRACE1(ANY, "Error %u restarting timer, continuing...", *(pdwErr)); \
  117. } \
  118. }
  119. // MEMORY ALLOCATION
  120. // macros
  121. #define MALLOC(ppPointer, ulSize, pdwErr) \
  122. { \
  123. if (*(ppPointer) = HeapAlloc(GLOBAL_HEAP, HEAP_ZERO_MEMORY, (ulSize))) \
  124. { \
  125. *(pdwErr) = NO_ERROR; \
  126. } \
  127. else \
  128. { \
  129. *(pdwErr) = ERROR_NOT_ENOUGH_MEMORY; \
  130. TRACE1(ANY, "Error allocating %u bytes", (ulSize)); \
  131. LOGERR0(HEAP_ALLOC_FAILED, *(pdwErr)); \
  132. } \
  133. }
  134. #define REALLOC(ptr, size) HeapReAlloc(GLOBAL_HEAP, 0, ptr, size)
  135. #define FREE(ptr) \
  136. { \
  137. HeapFree(GLOBAL_HEAP, 0, (ptr)); \
  138. (ptr) = NULL; \
  139. }
  140. #define FREE_NOT_NULL(ptr) \
  141. { \
  142. if (!(ptr)) HeapFree(GLOBAL_HEAP, 0, (ptr)); \
  143. (ptr) = NULL; \
  144. }
  145. // TRACING
  146. // definitions...
  147. #define IPSAMPLE_TRACE_ANY ((DWORD)0xFFFF0000 | TRACE_USE_MASK)
  148. #define IPSAMPLE_TRACE_ENTER ((DWORD)0x00010000 | TRACE_USE_MASK)
  149. #define IPSAMPLE_TRACE_LEAVE ((DWORD)0x00020000 | TRACE_USE_MASK)
  150. #define IPSAMPLE_TRACE_DEBUG ((DWORD)0x00040000 | TRACE_USE_MASK)
  151. #define IPSAMPLE_TRACE_CONFIGURATION ((DWORD)0x00100000 | TRACE_USE_MASK)
  152. #define IPSAMPLE_TRACE_NETWORK ((DWORD)0x00200000 | TRACE_USE_MASK)
  153. #define IPSAMPLE_TRACE_PACKET ((DWORD)0x00400000 | TRACE_USE_MASK)
  154. #define IPSAMPLE_TRACE_TIMER ((DWORD)0x00800000 | TRACE_USE_MASK)
  155. #define IPSAMPLE_TRACE_MIB ((DWORD)0x01000000 | TRACE_USE_MASK)
  156. // macros...
  157. #define TRACE0(l,a) \
  158. if (TRACEID != INVALID_TRACEID) \
  159. TracePrintfEx(TRACEID, IPSAMPLE_TRACE_ ## l, a)
  160. #define TRACE1(l,a,b) \
  161. if (TRACEID != INVALID_TRACEID) \
  162. TracePrintfEx(TRACEID, IPSAMPLE_TRACE_ ## l, a, b)
  163. #define TRACE2(l,a,b,c) \
  164. if (TRACEID != INVALID_TRACEID) \
  165. TracePrintfEx(TRACEID, IPSAMPLE_TRACE_ ## l, a, b, c)
  166. #define TRACE3(l,a,b,c,d) \
  167. if (TRACEID != INVALID_TRACEID) \
  168. TracePrintfEx(TRACEID, IPSAMPLE_TRACE_ ## l, a, b, c, d)
  169. #define TRACE4(l,a,b,c,d,e) \
  170. if (TRACEID != INVALID_TRACEID) \
  171. TracePrintfEx(TRACEID, IPSAMPLE_TRACE_ ## l, a, b, c, d, e)
  172. #define TRACE5(l,a,b,c,d,e,f) \
  173. if (TRACEID != INVALID_TRACEID) \
  174. TracePrintfEx(TRACEID, IPSAMPLE_TRACE_ ## l, a, b, c, d, e, f)
  175. // EVENT LOGGING
  176. // definitions...
  177. #define LOGERR RouterLogError
  178. #define LOGWARN RouterLogWarning
  179. #define LOGINFO RouterLogInformation
  180. #define LOGWARNDATA RouterLogWarningData
  181. // macros...
  182. // ERRORS
  183. #define LOGERR0(msg,err) \
  184. if (LOGLEVEL >= IPSAMPLE_LOGGING_ERROR) \
  185. LOGERR(LOGHANDLE,IPSAMPLELOG_ ## msg,0,NULL,(err))
  186. #define LOGERR1(msg,a,err) \
  187. if (LOGLEVEL >= IPSAMPLE_LOGGING_ERROR) \
  188. LOGERR(LOGHANDLE,IPSAMPLELOG_ ## msg,1,&(a),(err))
  189. #define LOGERR2(msg,a,b,err) \
  190. if (LOGLEVEL >= IPSAMPLE_LOGGING_ERROR) { \
  191. LPSTR _asz[2] = { (a), (b) }; \
  192. LOGERR(LOGHANDLE,IPSAMPLELOG_ ## msg,2,_asz,(err)); \
  193. }
  194. #define LOGERR3(msg,a,b,c,err) \
  195. if (LOGLEVEL >= IPSAMPLE_LOGGING_ERROR) { \
  196. LPSTR _asz[3] = { (a), (b), (c) }; \
  197. LOGERR(LOGHANDLE,IPSAMPLELOG_ ## msg,3,_asz,(err)); \
  198. }
  199. #define LOGERR4(msg,a,b,c,d,err) \
  200. if (LOGLEVEL >= IPSAMPLE_LOGGING_ERROR) { \
  201. LPSTR _asz[4] = { (a), (b), (c), (d) }; \
  202. LOGERR(LOGHANDLE,IPSAMPLELOG_ ## msg,4,_asz,(err)); \
  203. }
  204. // WARNINGS
  205. #define LOGWARN0(msg,err) \
  206. if (LOGLEVEL >= IPSAMPLE_LOGGING_WARN) \
  207. LOGWARN(LOGHANDLE,IPSAMPLELOG_ ## msg,0,NULL,(err))
  208. #define LOGWARN1(msg,a,err) \
  209. if (LOGLEVEL >= IPSAMPLE_LOGGING_WARN) \
  210. LOGWARN(LOGHANDLE,IPSAMPLELOG_ ## msg,1,&(a),(err))
  211. #define LOGWARN2(msg,a,b,err) \
  212. if (LOGLEVEL >= IPSAMPLE_LOGGING_WARN) { \
  213. LPSTR _asz[2] = { (a), (b) }; \
  214. LOGWARN(LOGHANDLE,IPSAMPLELOG_ ## msg,2,_asz,(err)); \
  215. }
  216. #define LOGWARN3(msg,a,b,c,err) \
  217. if (LOGLEVEL >= IPSAMPLE_LOGGING_WARN) { \
  218. LPSTR _asz[3] = { (a), (b), (c) }; \
  219. LOGWARN(LOGHANDLE,IPSAMPLELOG_ ## msg,3,_asz,(err)); \
  220. }
  221. #define LOGWARN4(msg,a,b,c,d,err) \
  222. if (LOGLEVEL >= IPSAMPLE_LOGGING_WARN) { \
  223. LPSTR _asz[4] = { (a), (b), (c), (d) }; \
  224. LOGWARN(LOGHANDLE,IPSAMPLELOG_ ## msg,4,_asz,(err)); \
  225. }
  226. #define LOGWARNDATA2(msg,a,b,dw,buf) \
  227. if (LOGLEVEL >= IPSAMPLE_LOGGING_WARN) { \
  228. LPSTR _asz[2] = { (a), (b) }; \
  229. LOGWARNDATA(LOGHANDLE,IPSAMPLELOG_ ## msg,2,_asz,(dw),(buf)); \
  230. }
  231. // INFORMATION
  232. #define LOGINFO0(msg,err) \
  233. if (LOGLEVEL >= IPSAMPLE_LOGGING_INFO) \
  234. LOGINFO(LOGHANDLE,IPSAMPLELOG_ ## msg,0,NULL,(err))
  235. #define LOGINFO1(msg,a,err) \
  236. if (LOGLEVEL >= IPSAMPLE_LOGGING_INFO) \
  237. LOGINFO(LOGHANDLE,IPSAMPLELOG_ ## msg,1,&(a),(err))
  238. #define LOGINFO2(msg,a,b,err) \
  239. if (LOGLEVEL >= IPSAMPLE_LOGGING_INFO) { \
  240. LPSTR _asz[2] = { (a), (b) }; \
  241. LOGINFO(LOGHANDLE,IPSAMPLELOG_ ## msg,2,_asz,(err)); \
  242. }
  243. #define LOGINFO3(msg,a,b,c,err) \
  244. if (LOGLEVEL >= IPSAMPLE_LOGGING_INFO) { \
  245. LPSTR _asz[3] = { (a), (b), (c) }; \
  246. LOGINFO(LOGHANDLE,IPSAMPLELOG_ ## msg,3,_asz,(err)); \
  247. }
  248. #define LOGINFO4(msg,a,b,c,d,err) \
  249. if (LOGLEVEL >= IPSAMPLE_LOGGING_INFO) { \
  250. LPSTR _asz[4] = { (a), (b), (c), (d) }; \
  251. LOGINFO(LOGHANDLE,IPSAMPLELOG_ ## msg,4,_asz,(err)); \
  252. }
  253. #endif // _DEFS_H_