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.

323 lines
6.5 KiB

  1. #include <windows.h>
  2. #include <winsock.h>
  3. #include <wininet.h>
  4. #undef INTERNET_MAX_URL_LENGTH
  5. #include "autoprox.hxx"
  6. #define INET_ASSERT
  7. #define DEBUG_PRINT(a,b,c)
  8. #define PERF_LOG(a,b)
  9. DWORD
  10. AUTO_PROXY_HELPER_APIS::ResolveHostName(
  11. IN LPSTR lpszHostName,
  12. IN OUT LPSTR lpszIPAddress,
  13. IN OUT LPDWORD lpdwIPAddressSize
  14. )
  15. /*++
  16. Routine Description:
  17. Resolves a HostName to an IP address by using Winsock DNS.
  18. Arguments:
  19. lpszHostName - the host name that should be used.
  20. lpszIPAddress - the output IP address as a string.
  21. lpdwIPAddressSize - the size of the outputed IP address string.
  22. Return Value:
  23. DWORD
  24. Win32 error code.
  25. --*/
  26. {
  27. //
  28. // figure out if we're being asked to resolve a name or an address. If
  29. // inet_addr() succeeds then we were given a string respresentation of an
  30. // address
  31. //
  32. DWORD ipAddr;
  33. LPBYTE address;
  34. LPHOSTENT lpHostent;
  35. DWORD ttl;
  36. DWORD dwIPAddressSize;
  37. BOOL bFromCache = FALSE;
  38. DWORD error = ERROR_SUCCESS;
  39. ipAddr = inet_addr(lpszHostName);
  40. if (ipAddr != INADDR_NONE)
  41. {
  42. dwIPAddressSize = lstrlen(lpszHostName);
  43. if ( *lpdwIPAddressSize < dwIPAddressSize ||
  44. lpszIPAddress == NULL )
  45. {
  46. *lpdwIPAddressSize = dwIPAddressSize+1;
  47. error = ERROR_INSUFFICIENT_BUFFER;
  48. goto quit;
  49. }
  50. lstrcpy(lpszIPAddress, lpszHostName);
  51. goto quit;
  52. }
  53. ipAddr = 0;
  54. address = (LPBYTE) &ipAddr;
  55. //
  56. // now try to find the name or address in the cache. If it's not in the
  57. // cache then resolve it
  58. //
  59. // if (QueryHostentCache(lpszHostName, address, &lpHostent, &ttl)) {
  60. // bFromCache = TRUE;
  61. // } else {
  62. {
  63. DEBUG_PRINT(SOCKETS,
  64. INFO,
  65. ("resolving %q\n",
  66. lpszHostName
  67. ));
  68. PERF_LOG(PE_NAMERES_START, 0);
  69. lpHostent = gethostbyname(lpszHostName);
  70. PERF_LOG(PE_NAMERES_END, 0);
  71. DEBUG_PRINT(SOCKETS,
  72. INFO,
  73. ("%q %sresolved\n",
  74. lpszHostName,
  75. lpHostent ? "" : "NOT "
  76. ));
  77. //
  78. // if we successfully resolved the name or address then add the
  79. // information to the cache
  80. //
  81. if (lpHostent != NULL)
  82. {
  83. // CacheHostent(lpszHostName, lpHostent, LIVE_DEFAULT);
  84. }
  85. }
  86. if ( lpHostent )
  87. {
  88. char *pszAddressStr;
  89. LPBYTE * addressList;
  90. struct in_addr sin_addr;
  91. // *(LPDWORD)&lpSin->sin_addr = *(LPDWORD)addressList[i];
  92. // ((struct sockaddr_in*)lpSockAddr)->sin_addr
  93. // struct in_addr sin_addr
  94. addressList = (LPBYTE *)lpHostent->h_addr_list;
  95. *(LPDWORD)&sin_addr = *(LPDWORD)addressList[0] ;
  96. pszAddressStr = inet_ntoa (sin_addr);
  97. INET_ASSERT(pszAddressStr);
  98. dwIPAddressSize = lstrlen(pszAddressStr);
  99. if ( *lpdwIPAddressSize < dwIPAddressSize ||
  100. lpszIPAddress == NULL )
  101. {
  102. *lpdwIPAddressSize = dwIPAddressSize+1;
  103. error = ERROR_INSUFFICIENT_BUFFER;
  104. goto quit;
  105. }
  106. lstrcpy(lpszIPAddress, pszAddressStr);
  107. goto quit;
  108. }
  109. //
  110. // otherwise, if we get here its an error
  111. //
  112. error = ERROR_INTERNET_NAME_NOT_RESOLVED;
  113. quit:
  114. if (bFromCache) {
  115. INET_ASSERT(lpHostent != NULL);
  116. // ReleaseHostentCacheEntry(lpHostent);
  117. }
  118. return error;
  119. }
  120. BOOL
  121. AUTO_PROXY_HELPER_APIS::IsResolvable(
  122. IN LPSTR lpszHost
  123. )
  124. /*++
  125. Routine Description:
  126. Determines wheter a HostName can be resolved. Performs a Winsock DNS query,
  127. and if it succeeds returns TRUE.
  128. Arguments:
  129. lpszHost - the host name that should be used.
  130. Return Value:
  131. BOOL
  132. TRUE - the host is resolved.
  133. FALSE - could not resolve.
  134. --*/
  135. {
  136. DWORD dwDummySize;
  137. DWORD error;
  138. error = ResolveHostName(
  139. lpszHost,
  140. NULL,
  141. &dwDummySize
  142. );
  143. if ( error == ERROR_INSUFFICIENT_BUFFER )
  144. {
  145. return TRUE;
  146. }
  147. else
  148. {
  149. INET_ASSERT(error != ERROR_SUCCESS );
  150. return FALSE;
  151. }
  152. }
  153. DWORD
  154. AUTO_PROXY_HELPER_APIS::GetIPAddress(
  155. IN OUT LPSTR lpszIPAddress,
  156. IN OUT LPDWORD lpdwIPAddressSize
  157. )
  158. /*++
  159. Routine Description:
  160. Acquires the IP address string of this client machine WININET is running on.
  161. Arguments:
  162. lpszIPAddress - the IP address of the machine, returned.
  163. lpdwIPAddressSize - size of the IP address string.
  164. Return Value:
  165. DWORD
  166. Win32 Error.
  167. --*/
  168. {
  169. CHAR szHostBuffer[255];
  170. int serr;
  171. serr = gethostname(
  172. szHostBuffer,
  173. 255-1
  174. );
  175. if ( serr != 0)
  176. {
  177. return ERROR_INTERNET_INTERNAL_ERROR;
  178. }
  179. return ResolveHostName(
  180. szHostBuffer,
  181. lpszIPAddress,
  182. lpdwIPAddressSize
  183. );
  184. }
  185. BOOL
  186. AUTO_PROXY_HELPER_APIS::IsInNet(
  187. IN LPSTR lpszIPAddress,
  188. IN LPSTR lpszDest,
  189. IN LPSTR lpszMask
  190. )
  191. /*++
  192. Routine Description:
  193. Determines whether a given IP address is in a given dest/mask IP address.
  194. Arguments:
  195. lpszIPAddress - the host name that should be used.
  196. lpszDest - the IP address dest to check against.
  197. lpszMask - the IP mask string
  198. Return Value:
  199. BOOL
  200. TRUE - the IP address is in the given dest/mask
  201. FALSE - the IP address is NOT in the given dest/mask
  202. --*/
  203. {
  204. DWORD dwDest, dwIpAddr, dwMask;
  205. INET_ASSERT(lpszIPAddress);
  206. INET_ASSERT(lpszDest);
  207. INET_ASSERT(lpszMask);
  208. dwIpAddr = inet_addr(lpszIPAddress);
  209. dwDest = inet_addr(lpszDest);
  210. dwMask = inet_addr(lpszMask);
  211. if ( dwDest == INADDR_NONE ||
  212. dwIpAddr == INADDR_NONE )
  213. {
  214. INET_ASSERT(FALSE);
  215. return FALSE;
  216. }
  217. if ( (dwIpAddr & dwMask) != dwDest)
  218. {
  219. return FALSE;
  220. }
  221. //
  222. // Pass, its Matches.
  223. //
  224. return TRUE;
  225. }