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.

352 lines
8.5 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. Module Name :
  4. cdns.h
  5. Abstract:
  6. This module defines the DNS connection class.
  7. Author:
  8. Rohan Phillips ( Rohanp ) 07-May-1998
  9. Project:
  10. Revision History:
  11. --*/
  12. # ifndef _ADNS_CLIENT_HXX_
  13. # define _ADNS_CLIENT_HXX_
  14. /************************************************************
  15. * Include Headers
  16. ************************************************************/
  17. #include <dnsreci.h>
  18. //
  19. // Redefine the type to indicate that this is a call-back function
  20. //
  21. typedef ATQ_COMPLETION PFN_ATQ_COMPLETION;
  22. /************************************************************
  23. * Symbolic Constants
  24. ************************************************************/
  25. //
  26. // Valid & Invalid Signatures for Client Connection object
  27. // (Ims Connection USed/FRee)
  28. //
  29. # define DNS_CONNECTION_SIGNATURE_VALID 'DNSU'
  30. # define DNS_CONNECTION_SIGNATURE_FREE 'DNSF'
  31. //
  32. // POP3 requires a minimum of 10 minutes before a timeout
  33. // (SMTP doesn't specify, but might as well follow POP3)
  34. //
  35. # define MINIMUM_CONNECTION_IO_TIMEOUT (10 * 60) // 10 minutes
  36. //
  37. //
  38. #define DNS_TCP_DEFAULT_PACKET_LENGTH (0x4000)
  39. enum DNSLASTIOSTATE
  40. {
  41. DNS_READIO, DNS_WRITEIO
  42. };
  43. typedef struct _DNS_OVERLAPPED
  44. {
  45. OVERLAPPED Overlapped;
  46. DNSLASTIOSTATE LastIoState;
  47. } DNS_OVERLAPPED;
  48. /************************************************************
  49. * Type Definitions
  50. ************************************************************/
  51. /*++
  52. class CLIENT_CONNECTION
  53. This class is used for keeping track of individual client
  54. connections established with the server.
  55. It maintains the state of the connection being processed.
  56. In addition it also encapsulates data related to Asynchronous
  57. thread context used for processing the request.
  58. --*/
  59. class CAsyncDns
  60. {
  61. private:
  62. ULONG m_signature; // signature on object for sanity check
  63. LONG m_cPendingIoCount;
  64. LONG m_cThreadCount;
  65. DWORD m_cbReceived;
  66. DWORD m_BytesToRead;
  67. DWORD m_dwIpServer;
  68. BOOL m_fUdp;
  69. BOOL m_FirstRead;
  70. PDNS_MESSAGE_BUFFER m_pMsgRecv;
  71. BYTE *m_pMsgRecvBuf;
  72. PDNS_MESSAGE_BUFFER m_pMsgSend;
  73. BYTE *m_pMsgSendBuf;
  74. WORD m_cbSendBufSize;
  75. SOCKADDR_IN m_RemoteAddress;
  76. PATQ_CONTEXT m_pAtqContext;
  77. SOCKET m_DnsSocket; // socket for this connection
  78. BOOL m_fIsGlobalDnsList;
  79. protected:
  80. DWORD m_dwFlags;
  81. char m_HostName [MAX_PATH];
  82. CDnsServerList *m_pTcpRegIpList;
  83. //
  84. // The overlapped structure for reads (one outstanding read at a time)
  85. // -- writes will dynamically allocate them
  86. //
  87. DNS_OVERLAPPED m_ReadOverlapped;
  88. DNS_OVERLAPPED m_WriteOverlapped;
  89. SOCKET QuerySocket( VOID) const
  90. { return ( m_DnsSocket); }
  91. PATQ_CONTEXT QueryAtqContext( VOID) const
  92. { return ( m_pAtqContext); }
  93. LPOVERLAPPED QueryAtqOverlapped( void ) const
  94. { return ( m_pAtqContext == NULL ? NULL : &m_pAtqContext->Overlapped ); }
  95. DWORD QueryDnsServer() { return m_dwIpServer; }
  96. BOOL IsUdp() { return m_fUdp; }
  97. //
  98. // DNS server failover is disabled if this query was a TCP failover query
  99. // kicked off because of UDP truncation.
  100. //
  101. BOOL FailoverDisabled() { return ((m_dwFlags == DNS_FLAGS_NONE) && !m_fUdp); }
  102. public:
  103. CAsyncDns();
  104. virtual ~CAsyncDns(VOID);
  105. DNS_STATUS DnsSendRecord();
  106. DNS_STATUS Dns_OpenTcpConnectionAndSend();
  107. DNS_STATUS Dns_Send( );
  108. DNS_STATUS SendPacket( void);
  109. DNS_STATUS Dns_QueryLib(
  110. DNS_NAME pszQuestionName,
  111. WORD wQuestionType,
  112. DWORD dwFlags,
  113. BOOL fUdp,
  114. CDnsServerList *pTcpRegIpList,
  115. BOOL fIsGlobalDnsList);
  116. SOCKET Dns_CreateSocket( IN INT SockType );
  117. //BOOL MakeDnsConnection(void);
  118. //
  119. // IsValid()
  120. // o Checks the signature of the object to determine
  121. //
  122. // Returns: TRUE on success and FALSE if invalid.
  123. //
  124. BOOL IsValid( VOID) const
  125. {
  126. return ( m_signature == DNS_CONNECTION_SIGNATURE_VALID);
  127. }
  128. //-------------------------------------------------------------------------
  129. // Virtual method that MUST be defined by derived classes.
  130. //
  131. // Processes a completed IO on the connection for the client.
  132. //
  133. // -- Calls and maybe called from the Atq functions.
  134. //
  135. virtual BOOL ProcessClient(
  136. IN DWORD cbWritten,
  137. IN DWORD dwCompletionStatus,
  138. IN OUT OVERLAPPED * lpo
  139. ) ;
  140. CDnsServerList *GetDnsList()
  141. {
  142. return m_pTcpRegIpList;
  143. }
  144. //
  145. // Returns a copy the IP_ARRAY in the DNS-serverlist for this CAsyncDns
  146. // Returns NULL if the DNS-serverlist is the default global-list of DNS
  147. // servers on this box. Otherwise, the pipArray returned should be deleted
  148. // using ReleaseDnsIpArray() by the caller. On failure to allocate memory,
  149. // FALSE is returned.
  150. //
  151. BOOL GetDnsIpArrayCopy(PIP_ARRAY *ppipArray)
  152. {
  153. if(m_fIsGlobalDnsList) {
  154. *ppipArray = NULL;
  155. return TRUE;
  156. }
  157. return m_pTcpRegIpList->CopyList(ppipArray);
  158. }
  159. void ReleaseDnsIpArray(PIP_ARRAY pipArray)
  160. {
  161. if(pipArray)
  162. delete pipArray;
  163. }
  164. LONG IncPendingIoCount(void)
  165. {
  166. LONG RetVal;
  167. RetVal = InterlockedIncrement( &m_cPendingIoCount );
  168. return RetVal;
  169. }
  170. LONG DecPendingIoCount(void) { return InterlockedDecrement( &m_cPendingIoCount );}
  171. LONG IncThreadCount(void)
  172. {
  173. LONG RetVal;
  174. RetVal = InterlockedIncrement( &m_cThreadCount );
  175. return RetVal;
  176. }
  177. LONG DecThreadCount(void) { return InterlockedDecrement( &m_cThreadCount );}
  178. BOOL ReadFile(
  179. IN LPVOID pBuffer,
  180. IN DWORD cbSize /* = MAX_READ_BUFF_SIZE */
  181. );
  182. BOOL WriteFile(
  183. IN LPVOID pBuffer,
  184. IN DWORD cbSize /* = MAX_READ_BUFF_SIZE */
  185. );
  186. BOOL ProcessReadIO(IN DWORD InputBufferLen,
  187. IN DWORD dwCompletionStatus,
  188. IN OUT OVERLAPPED * lpo);
  189. void DisconnectClient(void);
  190. // Virtual functions to implement app-specific processing
  191. virtual void DnsProcessReply(
  192. IN DWORD dwStatus,
  193. IN PDNS_RECORD pRecordList) = 0;
  194. virtual BOOL RetryAsyncDnsQuery(BOOL fUdp) = 0;
  195. public:
  196. //
  197. // LIST_ENTRY object for storing client connections in a list.
  198. //
  199. LIST_ENTRY m_listEntry;
  200. LIST_ENTRY & QueryListEntry( VOID)
  201. { return ( m_listEntry); }
  202. };
  203. typedef CAsyncDns * PCAsyncDns;
  204. class CAsyncMxDns : public CAsyncDns
  205. {
  206. protected:
  207. //
  208. // SMTP DNS specific members
  209. //
  210. DWORD m_LocalPref;
  211. BOOL m_SeenLocal;
  212. DWORD m_Index;
  213. DWORD m_Weight [100];
  214. DWORD m_Prefer [100];
  215. BOOL m_fUsingMx;
  216. char m_FQDNToDrop [MAX_PATH];
  217. PSMTPDNS_RECS m_AuxList;
  218. BOOL m_fMxLoopBack;
  219. public:
  220. CAsyncMxDns(char *MyFQDN);
  221. BOOL GetMissingIpAddresses(PSMTPDNS_RECS pDnsList);
  222. BOOL GetIpFromDns(PSMTPDNS_RECS pDnsRec, DWORD Count);
  223. BOOL CheckMxLoopback();
  224. void ProcessMxRecord(PDNS_RECORD pnewRR);
  225. void ProcessARecord(PDNS_RECORD pnewRR);
  226. BOOL SortMxList(void);
  227. BOOL CheckList(void);
  228. void DnsProcessReply(
  229. IN DWORD dwStatus,
  230. IN PDNS_RECORD pRecordList);
  231. //
  232. // The following functions allow SMTP to do SMTP connection specific
  233. // processing after the MX resolution is over
  234. //
  235. virtual void HandleCompletedData(DNS_STATUS) = 0;
  236. virtual BOOL IsShuttingDown() = 0;
  237. virtual BOOL IsAddressMine(DWORD dwIp) = 0;
  238. };
  239. //
  240. // Auxiliary functions
  241. //
  242. INT ShutAndCloseSocket( IN SOCKET sock);
  243. DWORD ResolveHost(
  244. LPSTR pszHost,
  245. PIP_ARRAY pipDnsServers,
  246. DWORD fOptions,
  247. DWORD *rgdwIpAddresses,
  248. DWORD *pcbIpAddresses);
  249. # endif
  250. /************************ End of File ***********************/