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.

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