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.

238 lines
5.2 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. caddrlst.hxx
  5. Abstract:
  6. Contains CAddressList class definition
  7. Contents:
  8. Author:
  9. Richard L Firth (rfirth) 21-Apr-1997
  10. Revision History:
  11. 21-Apr-1997 rfirth
  12. Created
  13. --*/
  14. //
  15. // manifests
  16. //
  17. #define CSADDR_BUFFER_LENGTH (sizeof(CSADDR_INFO) + 128)
  18. //
  19. // types
  20. //
  21. //
  22. // RESOLVED_ADDRESS - a CSADDR_INFO with additional IsBad field which is set
  23. // when we fail to connect to the address
  24. //
  25. typedef struct {
  26. CSADDR_INFO AddrInfo;
  27. BOOL IsValid;
  28. } RESOLVED_ADDRESS, * LPRESOLVED_ADDRESS;
  29. //
  30. // forward references
  31. //
  32. class CFsm_ResolveHost;
  33. //
  34. // classes
  35. //
  36. //
  37. // CAddressList - maintains list of resolved addresses for a host name/port
  38. // combination
  39. //
  40. class CAddressList {
  41. private:
  42. CRITICAL_SECTION m_CritSec; // grab this before updating
  43. DWORD m_ResolutionId; // determines when OK to resolve
  44. INT m_CurrentAddress; // index of current (good) address
  45. INT m_AddressCount; // number of addresses in list
  46. INT m_BadAddressCount; // number addresses already tried & failed
  47. LPRESOLVED_ADDRESS m_Addresses; // list of resolved addresses
  48. DWORD
  49. IPAddressToAddressList(
  50. IN DWORD ipAddr
  51. );
  52. DWORD
  53. HostentToAddressList(
  54. IN LPHOSTENT lpHostent
  55. );
  56. DWORD
  57. AddrInfoToAddressList(
  58. IN struct addrinfo FAR *lpAddrInfo
  59. );
  60. public:
  61. CAddressList() {
  62. InitializeCriticalSection(&m_CritSec);
  63. m_ResolutionId = 0;
  64. m_CurrentAddress = 0;
  65. m_AddressCount = 0;
  66. m_BadAddressCount = 0;
  67. m_Addresses = NULL;
  68. }
  69. ~CAddressList() {
  70. FreeList();
  71. DeleteCriticalSection(&m_CritSec);
  72. }
  73. VOID Acquire(VOID) {
  74. EnterCriticalSection(&m_CritSec);
  75. }
  76. VOID Release(VOID) {
  77. LeaveCriticalSection(&m_CritSec);
  78. }
  79. VOID
  80. FreeList(
  81. VOID
  82. );
  83. DWORD
  84. SetList(
  85. IN DWORD dwIpAddress
  86. );
  87. DWORD
  88. SetList(
  89. IN LPHOSTENT lpHostent
  90. );
  91. DWORD
  92. SetList(
  93. IN struct addrinfo *lpAddrInfo
  94. );
  95. BOOL
  96. GetNextAddress(
  97. IN OUT LPDWORD lpdwResolutionId,
  98. IN OUT LPDWORD lpdwIndex,
  99. IN INTERNET_PORT nPort,
  100. OUT LPCSADDR_INFO lpResolvedAddress
  101. );
  102. VOID
  103. InvalidateAddress(
  104. IN DWORD dwResolutionId,
  105. IN DWORD dwAddressIndex
  106. );
  107. DWORD
  108. ResolveHost(
  109. IN LPSTR lpszHostName,
  110. IN OUT LPDWORD lpdwResolutionId,
  111. IN DWORD dwFlags
  112. );
  113. DWORD
  114. ResolveHost_Fsm(
  115. IN CFsm_ResolveHost * Fsm
  116. );
  117. VOID NextAddress(VOID) {
  118. ++m_CurrentAddress;
  119. if (m_CurrentAddress == m_AddressCount) {
  120. m_CurrentAddress = 0;
  121. }
  122. }
  123. LPCSADDR_INFO CurrentAddress(VOID) {
  124. return &m_Addresses[m_CurrentAddress].AddrInfo;
  125. }
  126. LPSOCKADDR LocalSockaddr() {
  127. return CurrentAddress()->LocalAddr.lpSockaddr;
  128. }
  129. INT LocalSockaddrLength() {
  130. return CurrentAddress()->LocalAddr.iSockaddrLength;
  131. }
  132. INT LocalFamily(VOID) {
  133. return (INT)LocalSockaddr()->sa_family;
  134. }
  135. INT LocalPort() {
  136. //
  137. // The port number field is in the same location in both a
  138. // sockaddr_in and a sockaddr_in6, so it is safe to cast the
  139. // sockaddr to sockaddr_in here - this works for IPv4 or IPv6.
  140. //
  141. INET_ASSERT(offsetof(SOCKADDR_IN, sin_port) ==
  142. offsetof(SOCKADDR_IN6, sin6_port));
  143. return ((LPSOCKADDR_IN)LocalSockaddr())->sin_port;
  144. }
  145. VOID SetLocalPort(INTERNET_PORT Port) {
  146. //
  147. // The port number field is in the same location in both a
  148. // sockaddr_in and a sockaddr_in6, so it is safe to cast the
  149. // sockaddr to sockaddr_in here - this works for IPv4 or IPv6.
  150. //
  151. INET_ASSERT(offsetof(SOCKADDR_IN, sin_port) ==
  152. offsetof(SOCKADDR_IN6, sin6_port));
  153. ((LPSOCKADDR_IN)LocalSockaddr())->sin_port = Port;
  154. }
  155. LPSOCKADDR RemoteSockaddr() {
  156. return CurrentAddress()->RemoteAddr.lpSockaddr;
  157. }
  158. INT RemoteSockaddrLength() {
  159. return CurrentAddress()->RemoteAddr.iSockaddrLength;
  160. }
  161. INT RemoteFamily(VOID) {
  162. return (INT)RemoteSockaddr()->sa_family;
  163. }
  164. INT RemotePort() {
  165. //
  166. // The port number field is in the same location in both a
  167. // sockaddr_in and a sockaddr_in6, so it is safe to cast the
  168. // sockaddr to sockaddr_in here - this works for IPv4 or IPv6.
  169. //
  170. INET_ASSERT(offsetof(SOCKADDR_IN, sin_port) ==
  171. offsetof(SOCKADDR_IN6, sin6_port));
  172. return ((LPSOCKADDR_IN)RemoteSockaddr())->sin_port;
  173. }
  174. INT SocketType(VOID) {
  175. return CurrentAddress()->iSocketType;
  176. }
  177. INT Protocol(VOID) {
  178. return CurrentAddress()->iProtocol;
  179. }
  180. BOOL IsCurrentAddressValid(VOID) {
  181. return m_Addresses[m_CurrentAddress].IsValid;
  182. }
  183. };