diff options
author | Martin Sustrik <sustrik@250bpm.com> | 2011-03-02 09:00:36 +0100 |
---|---|---|
committer | Martin Sustrik <sustrik@250bpm.com> | 2011-03-02 09:00:36 +0100 |
commit | 5fcef1cac4d9faf0279b83ba48899b0e17b8e2d5 (patch) | |
tree | cce0cb9223e29ba38c909cccaac828bc59f7557a /src/decoder.cpp | |
parent | 4c7446211a02937f3e2522aece163d417b4ad0b9 (diff) |
ZMQ_MAXMSGSIZE option added
The new option allows user to guard against peers sending
oversized messages. Connection to peer sending oversized message
is dropped.
Signed-off-by: Martin Sustrik <sustrik@250bpm.com>
Diffstat (limited to 'src/decoder.cpp')
-rw-r--r-- | src/decoder.cpp | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/src/decoder.cpp b/src/decoder.cpp index c8ca715..7fb32ae 100644 --- a/src/decoder.cpp +++ b/src/decoder.cpp @@ -25,9 +25,10 @@ #include "wire.hpp" #include "err.hpp" -zmq::decoder_t::decoder_t (size_t bufsize_) : +zmq::decoder_t::decoder_t (size_t bufsize_, int64_t maxmsgsize_) : decoder_base_t <decoder_t> (bufsize_), - destination (NULL) + destination (NULL), + maxmsgsize (maxmsgsize_) { zmq_msg_init (&in_progress); @@ -63,7 +64,13 @@ bool zmq::decoder_t::one_byte_size_ready () // in_progress is initialised at this point so in theory we should // close it before calling zmq_msg_init_size, however, it's a 0-byte // message and thus we can treat it as uninitialised... - int rc = zmq_msg_init_size (&in_progress, *tmpbuf - 1); + int rc; + if (maxmsgsize >= 0 && (int64_t) (*tmpbuf - 1) > maxmsgsize) { + rc = -1; + errno = ENOMEM; + } + else + rc = zmq_msg_init_size (&in_progress, *tmpbuf - 1); if (rc != 0 && errno == ENOMEM) { rc = zmq_msg_init (&in_progress); errno_assert (rc == 0); @@ -92,7 +99,13 @@ bool zmq::decoder_t::eight_byte_size_ready () // in_progress is initialised at this point so in theory we should // close it before calling zmq_msg_init_size, however, it's a 0-byte // message and thus we can treat it as uninitialised... - int rc = zmq_msg_init_size (&in_progress, size - 1); + int rc; + if (maxmsgsize >= 0 && (int64_t) (size - 1) > maxmsgsize) { + rc = -1; + errno = ENOMEM; + } + else + rc = zmq_msg_init_size (&in_progress, size - 1); if (rc != 0 && errno == ENOMEM) { rc = zmq_msg_init (&in_progress); errno_assert (rc == 0); |