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.

335 lines
7.8 KiB

  1. //#include "ICSHelp.h"
  2. #include <winsock2.h>
  3. #include <wsipx.h>
  4. #include <iphlpapi.h>
  5. #include "ICSutils.h"
  6. #include <io.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <fcntl.h>
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #define BUF_SIZE 25
  13. #define ADDR_SZE 4096
  14. int gDbgFlag=3;
  15. extern char g_szPublicAddr[];
  16. extern int iDbgFileHandle;
  17. /*************************************************************
  18. *
  19. * DbgSpew(DbgClass, char *, ...)
  20. * Sends debug information.
  21. *
  22. *************************************************************/
  23. void DbgSpew(int DbgClass, WCHAR *lpFormat, va_list ap)
  24. {
  25. WCHAR szMessage[2500+3]; // extra space for '\r', '\n', and NULL
  26. DWORD bufSize = sizeof(szMessage)/sizeof(szMessage[0]);
  27. if ((DbgClass & 0x0F) >= (gDbgFlag & 0x0F))
  28. {
  29. _vsnwprintf( szMessage, bufSize - 3, lpFormat, ap);
  30. szMessage[ bufSize - 3 ] = 0;
  31. wcscat(szMessage, L"\r\n");
  32. // should this be sent to the debugger?
  33. if (DbgClass & DBG_MSG_DEST_DBG)
  34. OutputDebugStringW(szMessage);
  35. // should this go to our log file?
  36. if (iDbgFileHandle)
  37. _write(iDbgFileHandle, szMessage, (2*lstrlen(szMessage)));
  38. }
  39. }
  40. void TrivialSpew(WCHAR *lpFormat, ...)
  41. {
  42. va_list vd;
  43. va_start(vd, lpFormat);
  44. DbgSpew(DBG_MSG_TRIVIAL+DBG_MSG_DEST_DBG, lpFormat, vd);
  45. va_end(vd);
  46. }
  47. void InterestingSpew(WCHAR *lpFormat, ...)
  48. {
  49. va_list ap;
  50. va_start(ap, lpFormat);
  51. DbgSpew(DBG_MSG_INTERESTING+DBG_MSG_DEST_DBG, lpFormat, ap);
  52. va_end(ap);
  53. }
  54. void ImportantSpew(WCHAR *lpFormat, ...)
  55. {
  56. va_list ap;
  57. va_start(ap, lpFormat);
  58. DbgSpew(DBG_MSG_IMPORTANT+DBG_MSG_DEST_DBG+DBG_MSG_DEST_FILE, lpFormat, ap);
  59. va_end(ap);
  60. }
  61. void HeinousESpew(WCHAR *lpFormat, ...)
  62. {
  63. va_list ap;
  64. va_start(ap, lpFormat);
  65. DbgSpew(DBG_MSG_HEINOUS+DBG_MSG_DEST_DBG+DBG_MSG_DEST_FILE+DBG_MSG_DEST_EVENT+DBG_MSG_CLASS_ERROR, lpFormat, ap);
  66. va_end(ap);
  67. }
  68. void HeinousISpew(WCHAR *lpFormat, ...)
  69. {
  70. va_list ap;
  71. va_start(ap, lpFormat);
  72. DbgSpew(DBG_MSG_HEINOUS+DBG_MSG_DEST_DBG+DBG_MSG_DEST_FILE+DBG_MSG_DEST_EVENT, lpFormat, ap);
  73. va_end(ap);
  74. }
  75. // ------------------------------
  76. // DumpSocketAddress - dump a socket address
  77. //
  78. // Entry: Debug level
  79. // Pointer to socket address
  80. // Socket family
  81. //
  82. // Exit: Nothing
  83. // ------------------------------
  84. void DumpSocketAddress( const DWORD dwDebugLevel, const SOCKADDR *const pSocketAddress, const DWORD dwFamily )
  85. {
  86. switch ( dwFamily )
  87. {
  88. case AF_INET:
  89. {
  90. SOCKADDR_IN *pInetAddress = (SOCKADDR_IN*)( pSocketAddress );
  91. TrivialSpew(L"IP socket:\tAddress: %d.%d.%d.%d\tPort: %d",
  92. pInetAddress->sin_addr.S_un.S_un_b.s_b1,
  93. pInetAddress->sin_addr.S_un.S_un_b.s_b2,
  94. pInetAddress->sin_addr.S_un.S_un_b.s_b3,
  95. pInetAddress->sin_addr.S_un.S_un_b.s_b4,
  96. ntohs( pInetAddress->sin_port ));
  97. break;
  98. }
  99. case AF_IPX:
  100. {
  101. SOCKADDR_IPX *pIPXAddress = (SOCKADDR_IPX*)( pSocketAddress );
  102. TrivialSpew(L"IPX socket:\tNet (hex) %x-%x-%x-%x\tNode (hex): %x-%x-%x-%x-%x-%x\tSocket: %d",
  103. (BYTE)pIPXAddress->sa_netnum[ 0 ],
  104. (BYTE)pIPXAddress->sa_netnum[ 1 ],
  105. (BYTE)pIPXAddress->sa_netnum[ 2 ],
  106. (BYTE)pIPXAddress->sa_netnum[ 3 ],
  107. (BYTE)pIPXAddress->sa_nodenum[ 0 ],
  108. (BYTE)pIPXAddress->sa_nodenum[ 1 ],
  109. (BYTE)pIPXAddress->sa_nodenum[ 2 ],
  110. (BYTE)pIPXAddress->sa_nodenum[ 3 ],
  111. (BYTE)pIPXAddress->sa_nodenum[ 4 ],
  112. (BYTE)pIPXAddress->sa_nodenum[ 5 ],
  113. ntohs( pIPXAddress->sa_socket )
  114. );
  115. break;
  116. }
  117. default:
  118. {
  119. TrivialSpew(L"Unknown socket type!" );
  120. //INT3;
  121. break;
  122. }
  123. }
  124. }
  125. DWORD GetIPAddress(WCHAR *pVal, int iSize, int iPort)
  126. {
  127. DWORD hr = S_FALSE; // In case no adapter
  128. PMIB_IPADDRTABLE pmib=NULL;
  129. ULONG ulSize = 0;
  130. DWORD dw;
  131. PIP_ADAPTER_INFO pAdpInfo = NULL;
  132. WCHAR szPortBfr[ BUF_SIZE ];
  133. WCHAR *lStr = NULL;
  134. if( pVal == NULL || iSize == 0 )
  135. {
  136. return E_INVALIDARG;
  137. }
  138. TRIVIAL_MSG((L"GetIPAddress(0x%x, %d, %d)", pVal, iSize, iPort));
  139. szPortBfr[0]= L';';
  140. szPortBfr[1]= 0;
  141. // buffer is pretty big no need to limit it.
  142. if (iPort)
  143. wsprintf(szPortBfr, L":%d;", iPort);
  144. dw = GetAdaptersInfo(
  145. pAdpInfo,
  146. &ulSize );
  147. if (dw == ERROR_BUFFER_OVERFLOW && pVal)
  148. {
  149. /* let's make certain the buffer is as big as we'll
  150. * ever need
  151. */
  152. ulSize*=2;
  153. pAdpInfo = (IP_ADAPTER_INFO*)malloc(ulSize);
  154. if (!pAdpInfo)
  155. goto done;
  156. lStr = (WCHAR *)malloc(ADDR_SZE * sizeof(WCHAR));
  157. if (!lStr)
  158. goto done;
  159. *lStr = L'\0';
  160. dw = GetAdaptersInfo(
  161. pAdpInfo,
  162. &ulSize);
  163. if (dw == ERROR_SUCCESS)
  164. {
  165. int iAddrSize;
  166. PIP_ADAPTER_INFO p;
  167. PIP_ADDR_STRING ps;
  168. int nPortBfr = wcslen( szPortBfr );
  169. int bufSpaceLeft = ADDR_SZE;
  170. for(p=pAdpInfo; p!=NULL; p=p->Next)
  171. {
  172. INTERESTING_MSG((L"looking at %S, type=0x%x", p->Description, p->Type));
  173. for(ps = &(p->IpAddressList); ps; ps=ps->Next)
  174. {
  175. if (strcmp(ps->IpAddress.String, "0.0.0.0") != 0 &&
  176. strcmp(ps->IpAddress.String, g_szPublicAddr ) != 0)
  177. {
  178. WCHAR wcsBfr[BUF_SIZE];
  179. _snwprintf( wcsBfr , BUF_SIZE , L"%S", ps->IpAddress.String);
  180. wcsBfr[ BUF_SIZE - 1 ] = 0;
  181. bufSpaceLeft -= ( wcslen( wcsBfr ) + nPortBfr );
  182. if( bufSpaceLeft > 0 )
  183. {
  184. wcscat( lStr , wcsBfr );
  185. wcscat(lStr, szPortBfr);
  186. lStr[ ADDR_SZE - bufSpaceLeft ] = 0;
  187. }
  188. else
  189. {
  190. hr = E_FAIL;
  191. goto done;
  192. }
  193. }
  194. }
  195. }
  196. iAddrSize = wcslen(lStr);
  197. if (iAddrSize)
  198. {
  199. iAddrSize = min(iAddrSize, iSize-1);
  200. memcpy(pVal, lStr, (iAddrSize+1)*sizeof(WCHAR));
  201. pVal[ iAddrSize ] = 0;
  202. TRIVIAL_MSG((L"Copying %d chars for %s", iAddrSize+1, lStr));
  203. }
  204. else
  205. goto done;
  206. hr = S_OK;
  207. }
  208. }
  209. done:
  210. if (pAdpInfo)
  211. free(pAdpInfo);
  212. if (lStr)
  213. free(lStr);
  214. return hr;
  215. }
  216. #if 0 // bug547442 - this method is not used anywhere
  217. /******************************************************************
  218. **
  219. ** GetGatewayAddr -- returns a flag to
  220. ** indicate if a gateway is present
  221. **
  222. ******************************************************************/
  223. int GetGatewayAddr(char *retStr)
  224. {
  225. int retval = 0;
  226. PMIB_IPADDRTABLE pmib=NULL;
  227. ULONG ulSize = 0;
  228. DWORD dw;
  229. PIP_ADAPTER_INFO pAdpInfo = NULL;
  230. if (!retStr) return 0;
  231. dw = GetAdaptersInfo(
  232. pAdpInfo,
  233. &ulSize );
  234. if (dw == ERROR_BUFFER_OVERFLOW)
  235. {
  236. pAdpInfo = (IP_ADAPTER_INFO*)malloc(ulSize);
  237. dw = GetAdaptersInfo(
  238. pAdpInfo,
  239. &ulSize);
  240. if (dw == ERROR_SUCCESS)
  241. {
  242. strcpy(retStr, pAdpInfo->GatewayList.IpAddress.String);
  243. retval = 1;
  244. }
  245. free(pAdpInfo);
  246. }
  247. return retval;
  248. }
  249. int LocalFDIsSet(SOCKET fd, fd_set *set)
  250. /*++
  251. Routine Description:
  252. Determines if a specific socket is a contained in an FD_SET.
  253. Arguments:
  254. s - A descriptor identifying the socket.
  255. set - A pointer to an FD_SET.
  256. Returns:
  257. Returns TRUE if socket s is a member of set, otherwise FALSE.
  258. --*/
  259. {
  260. int i = set->fd_count; // index into FD_SET
  261. int rc=FALSE; // user return code
  262. while (i--){
  263. if (set->fd_array[i] == fd) {
  264. rc = TRUE;
  265. } //if
  266. } //while
  267. return (rc);
  268. } // LocalFDIsSet
  269. #endif