• R/O
  • SSH
  • HTTPS

eirrepo: Commit


Commit MetaInfo

Revision189 (tree)
Time2019-01-25 05:59:43
Authorquiret

Log Message

- fixed a bug with empty packets where it blocked while waiting for an empty TCP packet

Change Summary

Incremental Difference

--- sendmail_milter_lib/include/milter_lib.h (revision 188)
+++ sendmail_milter_lib/include/milter_lib.h (revision 189)
@@ -64,6 +64,12 @@
6464 bool noEndOfHeaders = false;
6565 };
6666
67+// Possible milter actions after message.
68+enum class eMilterAction
69+{
70+ ACCEPT, CONTINUE, DISCARD, REJECT, TEMPFAIL, CUSTOM
71+};
72+
6773 // Interface for accepting milter commands.
6874 struct MilterResponder
6975 {
@@ -71,42 +77,42 @@
7177 {
7278 return;
7379 }
74- virtual bool OnBody( TidesNetwork::NetSocket *socket, const void *data, size_t dataLen )
80+ virtual eMilterAction OnBody( TidesNetwork::NetSocket *socket, const void *data, size_t dataLen )
7581 {
76- return true;
82+ return eMilterAction::CONTINUE;
7783 }
78- virtual bool OnConnect( TidesNetwork::NetSocket *socket, basicString <char> hostName, char family, std::uint16_t port, basicString <char> address )
84+ virtual eMilterAction OnConnect( TidesNetwork::NetSocket *socket, basicString <char> hostName, char family, std::uint16_t port, basicString <char> address )
7985 {
80- return true;
86+ return eMilterAction::CONTINUE;
8187 }
8288 // Macros should be handled by the milter receiver itself.
83- virtual bool OnEndOfBody( TidesNetwork::NetSocket *socket )
89+ virtual eMilterAction OnEndOfBody( TidesNetwork::NetSocket *socket )
8490 {
85- return true;
91+ return eMilterAction::CONTINUE;
8692 }
87- virtual bool OnHelo( TidesNetwork::NetSocket *socket, basicString <char> helo )
93+ virtual eMilterAction OnHelo( TidesNetwork::NetSocket *socket, basicString <char> helo )
8894 {
89- return true;
95+ return eMilterAction::CONTINUE;
9096 }
91- virtual bool OnMailHeader( TidesNetwork::NetSocket *socket, basicString <char> name, basicString <char> value )
97+ virtual eMilterAction OnMailHeader( TidesNetwork::NetSocket *socket, basicString <char> name, basicString <char> value )
9298 {
93- return true;
99+ return eMilterAction::CONTINUE;
94100 }
95- virtual bool OnMailFrom( TidesNetwork::NetSocket *socket, basicVector <basicString <char>> senders )
101+ virtual eMilterAction OnMailFrom( TidesNetwork::NetSocket *socket, basicVector <basicString <char>> senders )
96102 {
97- return true;
103+ return eMilterAction::CONTINUE;
98104 }
99- virtual bool OnEndOfHeaders( TidesNetwork::NetSocket *socket )
105+ virtual eMilterAction OnEndOfHeaders( TidesNetwork::NetSocket *socket )
100106 {
101- return true;
107+ return eMilterAction::CONTINUE;
102108 }
103109 virtual void OnOptionNegotiation( TidesNetwork::NetSocket *socket, const MilterNegotiationData& remoteData, MilterNegotiationData& negOut )
104110 {
105111 return;
106112 }
107- virtual bool OnMailRecipients( TidesNetwork::NetSocket *socket, basicVector <basicString <char>> recipients )
113+ virtual eMilterAction OnMailRecipients( TidesNetwork::NetSocket *socket, basicVector <basicString <char>> recipients )
108114 {
109- return true;
115+ return eMilterAction::CONTINUE;
110116 }
111117 virtual void OnQuit( TidesNetwork::NetSocket *socket )
112118 {
--- sendmail_milter_lib/src/main.cpp (revision 188)
+++ sendmail_milter_lib/src/main.cpp (revision 189)
@@ -1,6 +1,7 @@
11 #include "milter_lib.h"
22
33 #include <sdk/Map.h>
4+#include <sdk/Endian.h>
45
56 #include <sdk/MemoryUtils.stream.h>
67
@@ -265,15 +266,27 @@
265266 return;
266267 }
267268
268-AINLINE void milter_respond_accept_or_reject( TidesNetwork::NetSocket *socket, bool acceptOrReject )
269+AINLINE void milter_respond_by_action( TidesNetwork::NetSocket *socket, eMilterAction action )
269270 {
270- if ( acceptOrReject )
271+ switch( action )
271272 {
273+ case eMilterAction::ACCEPT:
272274 milter_respond_accept( socket );
273- }
274- else
275- {
275+ break;
276+ case eMilterAction::CONTINUE:
277+ milter_respond_continue( socket );
278+ break;
279+ case eMilterAction::DISCARD:
280+ milter_respond_discard( socket );
281+ break;
282+ case eMilterAction::REJECT:
276283 milter_respond_reject( socket );
284+ break;
285+ case eMilterAction::TEMPFAIL:
286+ milter_respond_reject( socket );
287+ break;
288+ case eMilterAction::CUSTOM:
289+ break;
277290 }
278291 }
279292
@@ -308,14 +321,17 @@
308321
309322 packetSize -= 1;
310323
311- // Read the data of the packet.
312- void *packetData = CRTHeapAllocator::Allocate( nullptr, packetSize, 1 );
324+ void *packetData = nullptr;
313325
314- assert( packetData != nullptr );
315-
316- try
326+ if ( packetSize > 0 )
317327 {
328+ // Read the data of the packet.
329+ packetData = CRTHeapAllocator::Allocate( nullptr, packetSize, 1 );
330+
331+ assert( packetData != nullptr );
332+
318333 // Do the read.
334+ try
319335 {
320336 size_t readCount = accSock->Read( packetData, packetSize );
321337
@@ -324,7 +340,16 @@
324340 throw eir_exception();
325341 }
326342 }
343+ catch( ... )
344+ {
345+ CRTHeapAllocator::Free( nullptr, packetData );
327346
347+ throw;
348+ }
349+ }
350+
351+ try
352+ {
328353 // Process the packet.
329354 nullBufAllocMan <std::uint32_t> man;
330355 memoryBufferStream <std::uint32_t, nullBufAllocMan <std::uint32_t>, true, false> stream( packetData, packetSize, man );
@@ -333,7 +358,7 @@
333358
334359 if ( packetCmd == SMFIC_ABORT )
335360 {
336- // Reset our informations about commands.
361+ // Reset our collected information about commands (like macro values).
337362 this->cmdInfoMap.Clear();
338363
339364 responder->OnAbort( accSock );
@@ -340,9 +365,9 @@
340365 }
341366 else if ( packetCmd == SMFIC_BODY )
342367 {
343- bool doAccept = responder->OnBody( accSock, packetData, packetSize );
368+ eMilterAction action = responder->OnBody( accSock, packetData, packetSize );
344369
345- milter_respond_accept_or_reject( accSock, doAccept );
370+ milter_respond_by_action( accSock, action );
346371 }
347372 else if ( packetCmd == SMFIC_CONNECT )
348373 {
@@ -353,12 +378,12 @@
353378 std::uint16_t portNumber = stream.ReadUInt16_EX();
354379 basicString <char> address = read_null_terminated_string( stream );
355380
356- bool doAccept = responder->OnConnect(
381+ eMilterAction action = responder->OnConnect(
357382 accSock,
358383 std::move( hostName ), family, portNumber, std::move( address )
359384 );
360385
361- milter_respond_accept_or_reject( accSock, doAccept );
386+ milter_respond_by_action( accSock, action );
362387 }
363388 else if ( packetCmd == SMFIC_MACRO )
364389 {
@@ -392,9 +417,9 @@
392417 }
393418 else if ( packetCmd == SMFIC_BODYEOB )
394419 {
395- bool doAccept = responder->OnEndOfBody( accSock );
420+ eMilterAction action = responder->OnEndOfBody( accSock );
396421
397- milter_respond_accept_or_reject( accSock, doAccept );
422+ milter_respond_by_action( accSock, action );
398423 }
399424 else if ( packetCmd == SMFIC_HELO )
400425 {
@@ -401,9 +426,9 @@
401426 // Fetch the helo string.
402427 basicString <char> helo = read_null_terminated_string( stream );
403428
404- bool doAccept = responder->OnHelo( accSock, std::move( helo ) );
429+ eMilterAction action = responder->OnHelo( accSock, std::move( helo ) );
405430
406- milter_respond_accept_or_reject( accSock, doAccept );
431+ milter_respond_by_action( accSock, action );
407432 }
408433 else if ( packetCmd == SMFIC_HEADER )
409434 {
@@ -410,9 +435,9 @@
410435 basicString <char> headerName = read_null_terminated_string( stream );
411436 basicString <char> headerValue = read_null_terminated_string( stream );
412437
413- bool doAccept = responder->OnMailHeader( accSock, std::move( headerName ), std::move( headerValue ) );
438+ eMilterAction action = responder->OnMailHeader( accSock, std::move( headerName ), std::move( headerValue ) );
414439
415- milter_respond_accept_or_reject( accSock, doAccept );
440+ milter_respond_by_action( accSock, action );
416441 }
417442 else if ( packetCmd == SMFIC_MAIL )
418443 {
@@ -426,15 +451,15 @@
426451 senders.AddToBack( std::move( fromItem ) );
427452 }
428453
429- bool doAccept = responder->OnMailFrom( accSock, std::move( senders ) );
454+ eMilterAction action = responder->OnMailFrom( accSock, std::move( senders ) );
430455
431- milter_respond_accept_or_reject( accSock, doAccept );
456+ milter_respond_by_action( accSock, action );
432457 }
433458 else if ( packetCmd == SMFIC_EOH )
434459 {
435- bool doAccept = responder->OnEndOfHeaders( accSock );
460+ eMilterAction action = responder->OnEndOfHeaders( accSock );
436461
437- milter_respond_accept_or_reject( accSock, doAccept );
462+ milter_respond_by_action( accSock, action );
438463 }
439464 else if ( packetCmd == SMFIC_OPTNEG )
440465 {
@@ -542,9 +567,9 @@
542567 recipients.AddToBack( std::move( item ) );
543568 }
544569
545- bool doAccept = responder->OnMailRecipients( accSock, std::move( recipients ) );
570+ eMilterAction action = responder->OnMailRecipients( accSock, std::move( recipients ) );
546571
547- milter_respond_accept_or_reject( accSock, doAccept );
572+ milter_respond_by_action( accSock, action );
548573 }
549574 else if ( packetCmd == SMFIC_QUIT )
550575 {
@@ -561,12 +586,18 @@
561586 }
562587 catch( ... )
563588 {
564- CRTHeapAllocator::Free( nullptr, packetData );
589+ if ( packetData != nullptr )
590+ {
591+ CRTHeapAllocator::Free( nullptr, packetData );
592+ }
565593
566594 throw;
567595 }
568596
569- // Do not forget to clean up.
570- CRTHeapAllocator::Free( nullptr, packetData );
597+ if ( packetData != nullptr )
598+ {
599+ // Do not forget to clean up.
600+ CRTHeapAllocator::Free( nullptr, packetData );
601+ }
571602 }
572603 }
\ No newline at end of file
Show on old repository browser