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.

280 lines
6.5 KiB

  1. /********************************************************************/
  2. /** Copyright(c) 1995 Microsoft Corporation. **/
  3. /********************************************************************/
  4. //***
  5. //
  6. // Filename: util.c
  7. //
  8. // Description: Various miscillaneous routines
  9. //
  10. // History: May 11,1995 NarenG Created original version.
  11. //
  12. #include "dimsvcp.h"
  13. //**
  14. //
  15. // Call: GetTransportIndex
  16. //
  17. // Returns: Index of the tansport entry in the interface object
  18. //
  19. // Description: Given the id of a protocol return an index.
  20. //
  21. DWORD
  22. GetTransportIndex(
  23. IN DWORD dwProtocolId
  24. )
  25. {
  26. DWORD dwTransportIndex;
  27. for ( dwTransportIndex = 0;
  28. dwTransportIndex < gblDIMConfigInfo.dwNumRouterManagers;
  29. dwTransportIndex++ )
  30. {
  31. if ( gblRouterManagers[dwTransportIndex].DdmRouterIf.dwProtocolId
  32. == dwProtocolId )
  33. {
  34. return( dwTransportIndex );
  35. }
  36. }
  37. return( (DWORD)-1 );
  38. }
  39. //**
  40. //
  41. // Call: GetDDMEntryPoint
  42. //
  43. // Returns: Pointer to entry point into DDM - success
  44. // NULL - failure
  45. //
  46. // Description: Will return the entry point into the DDM if there is one.
  47. //
  48. FARPROC
  49. GetDDMEntryPoint(
  50. IN LPSTR lpEntryPoint
  51. )
  52. {
  53. DWORD dwIndex;
  54. for ( dwIndex = 0;
  55. gblDDMFunctionTable[dwIndex].lpEntryPointName != NULL;
  56. dwIndex ++ )
  57. {
  58. if ( _stricmp( gblDDMFunctionTable[dwIndex].lpEntryPointName,
  59. lpEntryPoint ) == 0 )
  60. {
  61. return( gblDDMFunctionTable[dwIndex].pEntryPoint );
  62. }
  63. }
  64. return( NULL );
  65. }
  66. //**
  67. //
  68. // Call: GetSizeOfDialoutHoursRestriction
  69. //
  70. // Returns: size in bytes of lpwsDialoutHoursRestriction
  71. //
  72. // Description: Utility to calculate the size in bytes of the MULTI_SZ pointed
  73. // to by lpwsDialoutHoursRestriction.
  74. //
  75. DWORD
  76. GetSizeOfDialoutHoursRestriction(
  77. IN LPWSTR lpwsMultSz
  78. )
  79. {
  80. LPWSTR lpwsPtr = lpwsMultSz;
  81. DWORD dwcbBytes = 0;
  82. DWORD dwCurCount;
  83. if ( lpwsMultSz == NULL )
  84. {
  85. return( 0 );
  86. }
  87. while( *lpwsPtr != L'\0' )
  88. {
  89. dwCurCount = ( wcslen( lpwsPtr ) + 1 );
  90. dwcbBytes += dwCurCount;
  91. lpwsPtr += dwCurCount;
  92. }
  93. //
  94. // One more for the last NULL terminator
  95. //
  96. dwcbBytes++;
  97. dwcbBytes *= sizeof( WCHAR );
  98. return( dwcbBytes );
  99. }
  100. //**
  101. //
  102. // Call: IsInterfaceRoleAcceptable
  103. //
  104. // Returns: TRUE if the interface plays a role that is compatible with the
  105. // given transport and router configuration.
  106. //
  107. // FALSE otherwise.
  108. //
  109. // Description: Some interfaces are only acceptable to some transports when
  110. // the router is running in a certain mode. The classic example
  111. // is the internal ip interface which will be rejected by the IP
  112. // router manager when in lan-only mode.
  113. //
  114. // The acceptable roles are hardcoded in this function.
  115. // At the time this function was written, there was only one
  116. // interface (internal ip) whose role was important to any
  117. // transport. In the future, instead of harcoding more roles
  118. // into this function, we should consider adding "role" as a
  119. // per-interface property both to the runtime structures
  120. // and to the permanant store.
  121. //
  122. BOOL
  123. IsInterfaceRoleAcceptable(
  124. IN ROUTER_INTERFACE_OBJECT* pIfObject,
  125. IN DWORD dwTransportId)
  126. {
  127. if (pIfObject == NULL)
  128. {
  129. return FALSE;
  130. }
  131. if ((gblDIMConfigInfo.dwRouterRole == ROUTER_ROLE_LAN) &&
  132. (dwTransportId == PID_IP) &&
  133. (pIfObject->IfType == ROUTER_IF_TYPE_INTERNAL))
  134. {
  135. return FALSE;
  136. }
  137. return TRUE;
  138. }
  139. #ifdef MEM_LEAK_CHECK
  140. //**
  141. //
  142. // Call: DebugAlloc
  143. //
  144. // Returns: return from HeapAlloc
  145. //
  146. // Description: Will use the memory table to store the pointer returned by
  147. // LocalAlloc
  148. //
  149. LPVOID
  150. DebugAlloc( DWORD Flags, DWORD dwSize )
  151. {
  152. DWORD Index;
  153. LPVOID pMem = HeapAlloc(gblDIMConfigInfo.hHeap, HEAP_ZERO_MEMORY,dwSize+4);
  154. if ( pMem == NULL )
  155. {
  156. return( pMem );
  157. }
  158. for( Index=0; Index < DIM_MEM_TABLE_SIZE; Index++ )
  159. {
  160. if ( DimMemTable[Index] == NULL )
  161. {
  162. DimMemTable[Index] = pMem;
  163. break;
  164. }
  165. }
  166. //
  167. // Our signature
  168. //
  169. *(((LPBYTE)pMem)+dwSize) = 0x0F;
  170. *(((LPBYTE)pMem)+dwSize+1) = 0x0E;
  171. *(((LPBYTE)pMem)+dwSize+2) = 0x0A;
  172. *(((LPBYTE)pMem)+dwSize+3) = 0x0B;
  173. RTASSERT( Index != DIM_MEM_TABLE_SIZE );
  174. return( pMem );
  175. }
  176. //**
  177. //
  178. // Call: DebugFree
  179. //
  180. // Returns: return from HeapFree
  181. //
  182. // Description: Will remove the pointer from the memeory table before freeing
  183. // the memory block
  184. //
  185. BOOL
  186. DebugFree( PVOID pMem )
  187. {
  188. DWORD Index;
  189. for( Index=0; Index < DIM_MEM_TABLE_SIZE; Index++ )
  190. {
  191. if ( DimMemTable[Index] == pMem )
  192. {
  193. DimMemTable[Index] = NULL;
  194. break;
  195. }
  196. }
  197. RTASSERT( Index != DIM_MEM_TABLE_SIZE );
  198. return( HeapFree( gblDIMConfigInfo.hHeap, 0, pMem ) );
  199. }
  200. //**
  201. //
  202. // Call: DebugReAlloc
  203. //
  204. // Returns: return from HeapReAlloc
  205. //
  206. // Description: Will change the value of the realloced pointer.
  207. //
  208. LPVOID
  209. DebugReAlloc( PVOID pMem, DWORD dwSize )
  210. {
  211. DWORD Index;
  212. if ( pMem == NULL )
  213. {
  214. RTASSERT(FALSE);
  215. }
  216. for( Index=0; Index < DDM_MEM_TABLE_SIZE; Index++ )
  217. {
  218. if ( DdmMemTable[Index] == pMem )
  219. {
  220. DdmMemTable[Index] = HeapReAlloc( gblDDMConfigInfo.hHeap,
  221. HEAP_ZERO_MEMORY,
  222. pMem, dwSize+8 );
  223. pMem = DdmMemTable[Index];
  224. *((LPDWORD)pMem) = dwSize;
  225. ((LPBYTE)pMem) += 4;
  226. //
  227. // Our signature
  228. //
  229. *(((LPBYTE)pMem)+dwSize) = 0x0F;
  230. *(((LPBYTE)pMem)+dwSize+1) = 0x0E;
  231. *(((LPBYTE)pMem)+dwSize+2) = 0x0A;
  232. *(((LPBYTE)pMem)+dwSize+3) = 0x0B;
  233. break;
  234. }
  235. }
  236. RTASSERT( Index != DDM_MEM_TABLE_SIZE );
  237. return( (LPVOID)pMem );
  238. }
  239. #endif