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.

198 lines
6.4 KiB

  1. #ifndef _DROPDIR_HXX_
  2. #define _DROPDIR_HXX_
  3. #define ERROR_OBJECT_DELETED ERROR_ACCESS_DENIED
  4. #define PRIVATE_OPTIMAL_BUFFER_SIZE 64 * 1024
  5. #define PRIVATE_LINE_BUFFER_SIZE 1024
  6. #define INC_REF(u) InterlockedIncrement((LONG*)&u)
  7. #define DEC_REF(u) InterlockedDecrement((LONG*)&u)
  8. #ifndef QWORD
  9. typedef unsigned __int64 QWORD;
  10. #define LODWORD(qw) (DWORD)(0xffffffff&(qw))
  11. #define HIDWORD(qw) (DWORD)(((QWORD)(qw))>>32)
  12. #define MAKEQWORD(dwLow,dwHigh) (((QWORD)(dwLow))|(((QWORD)(dwHigh))<<32))
  13. #endif
  14. #define SAFE_ADDREF(p) if( p ) { p->AddRef(); }
  15. #define SAFE_DELETE(p) if( p ) { delete p; p = NULL; }
  16. #define SAFE_RELEASE(p) if( p ) { p->Release(); p = NULL; }
  17. #define SAFE_SHUTRELEASE(p) if( p ) { p->Shutdown(); p->Release(); p = NULL; }
  18. #define SAFE_COTASKMEMFREE(p) if( p ) { CoTaskMemFree( p ); p = NULL; }
  19. #define SAFE_SYSFREESTRING(p) if( p ) { SysFreeString( p ); p = NULL; }
  20. #define SAFE_ARRAYDELETE(p) if( p ) { delete [] p; p = NULL; }
  21. //////////////////////////////////////////////////////////////////////////////
  22. class CAutoTrace
  23. {
  24. public:
  25. CAutoTrace( LPARAM p, LPSTR psz );
  26. ~CAutoTrace();
  27. private:
  28. LPARAM m_p;
  29. LPSTR m_psz;
  30. CAutoTrace(){}
  31. };
  32. //////////////////////////////////////////////////////////////////////////////
  33. inline CAutoTrace::CAutoTrace( LPARAM p, LPSTR psz )
  34. {
  35. TraceFunctEnterEx((LPARAM) p, psz);
  36. m_p = p;
  37. m_psz = psz;
  38. }
  39. //////////////////////////////////////////////////////////////////////////////
  40. inline CAutoTrace::~CAutoTrace()
  41. {
  42. char *___pszFunctionName = m_psz;
  43. TraceFunctLeaveEx((LPARAM)m_p);
  44. }
  45. //////////////////////////////////////////////////////////////////////////////
  46. #define DECL_TRACE( a, b ) char *___pszFunctionName = b; \
  47. CAutoTrace __xxCAutoTracexx__( a, b )
  48. //////////////////////////////////////////////////////////////////////////////
  49. #define X_SENDER_HEADER "x-sender: "
  50. #define X_RECEIVER_HEADER "x-receiver: "
  51. #define X_HEADER_EOLN "\r\n"
  52. #define MAX_HEADER_SIZE (sizeof(X_RECEIVER_HEADER))
  53. #define INVALID_RCPT_IDX_VALUE 0xFFFFFFFF
  54. #define DROPDIR_SIG 'aDDR'
  55. #define DROPDIR_SIG_FREE 'fDDR'
  56. typedef struct _DROPDIR_READ_OVERLAPPED
  57. {
  58. FH_OVERLAPPED Overlapped;
  59. PVOID ThisPtr;
  60. } DROPDIR_READ_OVERLAPPED, *PDROPDIR_READ_OVERLAPPED;
  61. enum DROPDIR_READ_STATE
  62. {
  63. DROPDIR_READ_NULL,
  64. DROPDIR_READ_X_SENDER,
  65. DROPDIR_READ_X_RECEIVER,
  66. DROPDIR_READ_DATA
  67. };
  68. enum DROPDIR_WRITE_STATE
  69. {
  70. DROPDIR_WRITE_NULL,
  71. DROPDIR_WRITE_DATA,
  72. DROPDIR_WRITE_CRLF,
  73. DROPDIR_WRITE_SETPOS
  74. };
  75. class CDropDir
  76. {
  77. public:
  78. //use CPool for better memory management
  79. static CPool m_Pool;
  80. // override the mem functions to use CPool functions
  81. void *operator new (size_t cSize) { return m_Pool.Alloc(); }
  82. void operator delete (void *pInstance) { m_Pool.Free(pInstance); }
  83. ULONG AddRef()
  84. {
  85. return( INC_REF( m_cRef ) );
  86. }
  87. ULONG Release()
  88. {
  89. LONG uHint = DEC_REF( m_cRef );
  90. if( 0 == uHint )
  91. {
  92. delete this;
  93. return 0;
  94. }
  95. return uHint;
  96. }
  97. CDropDir();
  98. HRESULT CopyMailToDropDir( ISMTPConnection *pISMTPConnection,
  99. const char *DropDirectory,
  100. IMailMsgProperties *pIMsg,
  101. PVOID AdvContext,
  102. DWORD NumRcpts,
  103. DWORD *rgRcptIndexList,
  104. SMTP_SERVER_INSTANCE *pParentInst);
  105. HRESULT OnIoWriteCompletion( DWORD cbSize, DWORD dwErr, OVERLAPPED *lpo );
  106. HRESULT OnIoReadCompletion( DWORD cbSize, DWORD dwErr, OVERLAPPED *lpo );
  107. VOID SetHr( HRESULT hr )
  108. {
  109. if(FAILED(hr) )
  110. {
  111. m_hr = hr;
  112. }
  113. }
  114. private:
  115. HANDLE CreateDropFile(const char * DropDir);
  116. HRESULT ReadFile( IN LPVOID pBuffer, IN DWORD cbSize );
  117. HRESULT WriteFile( IN LPVOID pBuffer, IN DWORD cbSize );
  118. HRESULT CreateXHeaders();
  119. HRESULT CreateXRecvHeaders();
  120. HRESULT CopyMessage();
  121. HRESULT OnCopyMessageRead( DWORD dwBytesRead, DWORD dwErr );
  122. HRESULT OnCopyMessageWrite( DWORD cbWritten );
  123. HRESULT DoLastFileOp();
  124. HRESULT AdjustFilePos();
  125. BOOL CheckIfAllRcptsHandled();
  126. HRESULT SetAllRcptsHandled();
  127. ~CDropDir();
  128. private:
  129. DWORD m_dwSig;
  130. LONG m_cRef;
  131. HRESULT m_hr;
  132. DROPDIR_READ_STATE m_ReadState;
  133. DROPDIR_WRITE_STATE m_WriteState;
  134. DWORD m_idxRecips;
  135. QWORD m_cbWriteOffset;
  136. QWORD m_cbReadOffset;
  137. QWORD m_cbMsgWritten;
  138. DWORD m_NumRcpts;
  139. PVOID m_AdvContext;
  140. SMTP_SERVER_INSTANCE *m_pParentInst;
  141. DWORD *m_rgRcptIndexList;
  142. IMailMsgProperties *m_pIMsg;
  143. IMailMsgBind *m_pBindInterface;
  144. ISMTPConnection *m_pISMTPConnection;
  145. IMailMsgRecipients *m_pIMsgRecips;
  146. HANDLE m_hDrop;
  147. PATQ_CONTEXT m_pAtqContext;
  148. PFIO_CONTEXT m_hMail;
  149. DROPDIR_READ_OVERLAPPED m_ReadOverlapped;
  150. OVERLAPPED m_WriteOverlapped;
  151. MessageAck m_MsgAck;
  152. CHAR m_acCrLfDotCrLf[5];
  153. CHAR m_acLastBytes[5];
  154. char m_szDropDir[MAX_PATH +1];
  155. char m_szDropFile[MAX_PATH +1];
  156. char m_szBuffer[PRIVATE_OPTIMAL_BUFFER_SIZE];
  157. };
  158. VOID
  159. DropDirWriteCompletion(
  160. PVOID pvContext,
  161. DWORD cbWritten,
  162. DWORD dwCompletionStatus,
  163. OVERLAPPED * lpo
  164. );
  165. VOID
  166. DropDirReadCompletion(
  167. PFIO_CONTEXT pContext,
  168. PFH_OVERLAPPED lpo,
  169. DWORD cbRead,
  170. DWORD dwCompletionStatus
  171. );
  172. #endif //_DROPDIR_HXX_