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.

237 lines
6.6 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name :
  4. asynccon.hxx
  5. Abstract:
  6. This file contains type definitions for async connections
  7. Author:
  8. Rohan Phillips (Rohanp) Feb-26-1998
  9. Revision History:
  10. --*/
  11. #ifndef _ASYNC_CONN_HXX_
  12. #define _ASYNC_CONN_HXX_
  13. #define ASYNCCON_SIGNATURE 'uASY'
  14. #define ASYNCCON_SIGNATURE_FREE 'fASY'
  15. //Based on the NT WAITORTIMERCALLBACKFUNC - only adds a return value
  16. typedef BOOL (NTAPI * USERCALLBACKFUNC) (PVOID, BOOLEAN );
  17. extern BOOL
  18. WINAPI
  19. SmtpUnregisterWait(
  20. HANDLE WaitHandle
  21. );
  22. extern HANDLE
  23. WINAPI
  24. SmtpRegisterWaitForSingleObject(
  25. HANDLE hObject,
  26. WAITORTIMERCALLBACKFUNC Callback,
  27. PVOID Context,
  28. ULONG dwMilliseconds
  29. );
  30. class CAsyncConnection
  31. {
  32. private:
  33. DWORD m_Signature; //Signature of Object
  34. DWORD m_TimeOut; //The amount of time that we wait for a connect
  35. DWORD m_IpAddress; //The connected IP Address
  36. DWORD m_Port; //The port we are connected to
  37. DWORD m_Error; //Holds any error that happened
  38. DWORD NextIpToTry; //The next IP address to try to connect to
  39. LONG m_cActiveThreads;
  40. LONG m_cRefCount;
  41. BOOL m_IsFirstTime; //Are we doing a gethostbyname() or a real connect
  42. BOOL m_fCloseSocket; //Should this class close the socket in the destructor
  43. BOOL m_fTimedOut; //Did we time out ?
  44. HANDLE m_RegisterWaitHandle; //The NT thread pool handle
  45. HANDLE m_SignalHandle; //The handle we give the thread pool to wait on
  46. SOCKET AsyncSock; //The socket we are connected to
  47. USERCALLBACKFUNC m_CallBackFunc; //The users callback function
  48. char m_HostName[100]; //Holds the machine we are trying to conenct to
  49. //
  50. // Maximum IP address stored for the instance, 20 smmes to be a commonly
  51. // used value both for DNS and remoteq
  52. //
  53. enum { _MAX_HOSTENT_IP_ADDRESSES = 20};
  54. // A copy of the HOSTENT structure for the instance's own IP addresses
  55. // We have other buffers to save the contents of HOSTENT
  56. // We will not take more than _MAX_HOSTENT_IP_ADDRESSES addresses.
  57. struct hostent m_Hostent;
  58. struct in_addr *m_pLocalIpAddresses[_MAX_HOSTENT_IP_ADDRESSES + 1];
  59. struct in_addr m_LocalIpAddresses[_MAX_HOSTENT_IP_ADDRESSES + 1];
  60. public:
  61. CAsyncConnection(DWORD PortNo, DWORD TimeOut, char * Host, USERCALLBACKFUNC CallBack);
  62. virtual ~CAsyncConnection();
  63. SOCKET GetSockethandle(void) const {return AsyncSock;}
  64. BOOL IsFirstTime(void) const {return m_IsFirstTime;}
  65. DWORD GetConnectedIpAddress(void) const {return m_IpAddress;}
  66. DWORD GetPortNum(void) const {return m_Port;}
  67. DWORD GetErrorCode(void) const {return m_Error;}
  68. void SetErrorCode(DWORD ErrorCode) {m_Error = ErrorCode;}
  69. void SetConnectedIpAddress (DWORD IpAddress) {m_IpAddress = IpAddress;}
  70. void SetFirstTime(BOOL fFirstTime) {m_IsFirstTime = fFirstTime;}
  71. void SetCloseSocketFlag(BOOL bCloseFlag) {m_fCloseSocket = bCloseFlag;}
  72. void SetTimedOutFlag (BOOL bTimedOut) {m_fTimedOut = bTimedOut;}
  73. void SignalObject(void)
  74. {
  75. IncRefCount();
  76. SetEvent(m_SignalHandle);
  77. }
  78. void SetWaitHandleToNull (void) {m_RegisterWaitHandle = NULL;}
  79. BOOL ConnectToHost(void);
  80. BOOL IssueTcpConnect(SOCKADDR_IN &connect_addr);
  81. BOOL GetTimedOutFlag (void) const {return m_fTimedOut;}
  82. virtual BOOL SetSocketOptions(void) { return TRUE;}
  83. virtual BOOL CheckIpAddress(DWORD IpAddress, DWORD PortNum) {return TRUE;}
  84. void SetNewHost(const char * NewHost)
  85. {
  86. lstrcpyn(m_HostName, NewHost, sizeof(m_HostName) - 1);
  87. m_IsFirstTime = TRUE;
  88. NextIpToTry = 0;
  89. }
  90. char * GetHostName(void) {return m_HostName;}
  91. BOOL AsyncConnectSuccessfull(void);
  92. virtual void IncNextIpToTry (void) {NextIpToTry++;}
  93. virtual BOOL IsMoreIpAddresses(void) const
  94. {
  95. return(m_Hostent.h_addr_list[NextIpToTry] != NULL);
  96. }
  97. BOOL ResetSocketToBlockingMode(void)
  98. {
  99. unsigned int Block = 1;
  100. int fRet = FALSE;
  101. if(AsyncSock != INVALID_SOCKET)
  102. {
  103. WSAEventSelect(AsyncSock, m_SignalHandle, 0);
  104. fRet = ioctlsocket(AsyncSock, FIONBIO, (u_long FAR *) &Block);
  105. }
  106. return fRet;
  107. }
  108. BOOL ExecuteUserCallBackRoutine(PVOID ThisPtr, BOOLEAN fTimedOut)
  109. {
  110. return m_CallBackFunc(ThisPtr, fTimedOut);
  111. }
  112. virtual BOOL MakeFirstAsyncConnect(void);
  113. BOOL MakeNewConnection(void);
  114. BOOL DoGetHostByName(void);
  115. static void AsyncConnectCallback(PVOID ThisPtr, BOOLEAN fTimedOut);
  116. void CloseAsyncSocket(void)
  117. {
  118. int RetVal = 0;
  119. int Error = 0;
  120. TraceFunctEnterEx((LPARAM) this, "CAsyncConnection::CloseAsyncSocket");
  121. if(AsyncSock != INVALID_SOCKET)
  122. {
  123. ResetSocketToBlockingMode();
  124. RetVal = closesocket(AsyncSock);
  125. if(RetVal != 0)
  126. {
  127. Error = GetLastError();
  128. ErrorTrace((LPARAM) this, "%s had error %d closing the socket", m_HostName, Error);
  129. }
  130. AsyncSock = INVALID_SOCKET;
  131. }
  132. TraceFunctLeaveEx((LPARAM)this);
  133. }
  134. BOOL IsValid (void) const{return ( m_Signature == ASYNCCON_SIGNATURE); }
  135. BOOL InitializeAsyncConnect(void)
  136. {
  137. BOOL fRet = FALSE;
  138. m_SignalHandle = CreateEvent(NULL, FALSE, FALSE, NULL);
  139. if(m_SignalHandle != NULL)
  140. {
  141. m_RegisterWaitHandle = SmtpRegisterWaitForSingleObject(m_SignalHandle, AsyncConnectCallback, this, m_TimeOut);
  142. if(m_RegisterWaitHandle != NULL)
  143. {
  144. fRet = TRUE;
  145. //kick things off.
  146. SignalObject();
  147. }
  148. }
  149. return fRet;
  150. }
  151. BOOL UnRegisterCallback(void)
  152. {
  153. if(m_RegisterWaitHandle)
  154. {
  155. SmtpUnregisterWait(m_RegisterWaitHandle);
  156. m_RegisterWaitHandle = NULL;
  157. }
  158. return TRUE;
  159. }
  160. BOOL RegisterCallbackInfo(HANDLE SigHandle, WAITORTIMERCALLBACKFUNC Callback, DWORD TimeOut)
  161. {
  162. m_SignalHandle = SigHandle;
  163. m_TimeOut = TimeOut;
  164. m_RegisterWaitHandle = SmtpRegisterWaitForSingleObject(m_SignalHandle, Callback, this, TimeOut);
  165. return (m_RegisterWaitHandle != NULL);
  166. }
  167. BOOL ReStartAsyncConnect(void)
  168. {
  169. m_RegisterWaitHandle = SmtpRegisterWaitForSingleObject(m_SignalHandle, AsyncConnectCallback, this, m_TimeOut);
  170. return (m_RegisterWaitHandle != NULL);
  171. }
  172. void StoreHostent(struct hostent *pHostent);
  173. BOOL ConnectToHost(DWORD IpAddress);
  174. LONG IncThreadCount(void) { return InterlockedIncrement( &m_cActiveThreads ); }
  175. LONG DecThreadCount(void) { return InterlockedDecrement( &m_cActiveThreads ); }
  176. LONG GetThreadCount(void) const {return m_cActiveThreads;}
  177. LONG IncRefCount(void) { return InterlockedIncrement( &m_cRefCount ); }
  178. LONG DecRefCount(void) { return InterlockedDecrement( &m_cRefCount ); }
  179. LONG GetRefCount(void) const {return m_cRefCount;}
  180. };
  181. #endif