Changeset 14545


Ignore:
Timestamp:
Jun 24, 2015, 9:24:41 PM (6 years ago)
Author:
mikedld
Message:

#5964: Another follow-up commit for #5456 (encrypted communication with libevent 2.1.x)

Decrypt only what is asked for, not more. This fixes previous commit
which changed semantics by processing the buffer to the very end,
starting with correct offset though.

Refactor encryption/decryption to a single function which callback.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/peer-io.c

    r14544 r14545  
    10421042**/
    10431043
     1044static inline void
     1045processBuffer (tr_crypto        * crypto,
     1046               struct evbuffer  * buffer,
     1047               size_t             offset,
     1048               size_t             size,
     1049               void            (* callback) (tr_crypto *, size_t, const void *, void *))
     1050{
     1051    struct evbuffer_ptr pos;
     1052    struct evbuffer_iovec iovec;
     1053
     1054    evbuffer_ptr_set (buffer, &pos, offset, EVBUFFER_PTR_SET);
     1055
     1056    do
     1057    {
     1058        if (evbuffer_peek (buffer, size, &pos, &iovec, 1) <= 0)
     1059            break;
     1060
     1061        callback (crypto, iovec.iov_len, iovec.iov_base, iovec.iov_base);
     1062
     1063        assert (size >= iovec.iov_len);
     1064        size -= iovec.iov_len;
     1065    }
     1066    while (!evbuffer_ptr_set (buffer, &pos, iovec.iov_len, EVBUFFER_PTR_ADD));
     1067
     1068    assert (size == 0);
     1069}
     1070
    10441071static void
    10451072addDatatype (tr_peerIo * io, size_t byteCount, bool isPieceData)
     
    10521079}
    10531080
    1054 static void
    1055 maybeEncryptBuffer (tr_peerIo * io, struct evbuffer * buf)
     1081static inline void
     1082maybeEncryptBuffer (tr_peerIo       * io,
     1083                    struct evbuffer * buf,
     1084                    size_t            offset,
     1085                    size_t            size)
    10561086{
    10571087    if (io->encryption_type == PEER_ENCRYPTION_RC4)
    1058     {
    1059         struct evbuffer_ptr pos;
    1060         struct evbuffer_iovec iovec;
    1061         evbuffer_ptr_set (buf, &pos, 0, EVBUFFER_PTR_SET);
    1062         do {
    1063             if (evbuffer_peek (buf, -1, &pos, &iovec, 1) != 1)
    1064               break;
    1065             tr_cryptoEncrypt (&io->crypto, iovec.iov_len, iovec.iov_base, iovec.iov_base);
    1066         } while (!evbuffer_ptr_set (buf, &pos, iovec.iov_len, EVBUFFER_PTR_ADD));
    1067     }
     1088        processBuffer (&io->crypto, buf, offset, size, &tr_cryptoEncrypt);
    10681089}
    10691090
     
    10721093{
    10731094    const size_t byteCount = evbuffer_get_length (buf);
    1074     maybeEncryptBuffer (io, buf);
     1095    maybeEncryptBuffer (io, buf, 0, byteCount);
    10751096    evbuffer_add_buffer (io->outbuf, buf);
    10761097    addDatatype (io, byteCount, isPieceData);
     
    11281149***/
    11291150
     1151static inline void
     1152maybeDecryptBuffer (tr_peerIo       * io,
     1153                    struct evbuffer * buf,
     1154                    size_t            offset,
     1155                    size_t            size)
     1156{
     1157    if (io->encryption_type == PEER_ENCRYPTION_RC4)
     1158        processBuffer (&io->crypto, buf, offset, size, &tr_cryptoDecrypt);
     1159}
     1160
    11301161void
    11311162tr_peerIoReadBytesToBuf (tr_peerIo * io, struct evbuffer * inbuf, struct evbuffer * outbuf, size_t byteCount)
     
    11431174    evbuffer_free (tmp);
    11441175
    1145     /* decrypt if needed */
    1146     if (io->encryption_type == PEER_ENCRYPTION_RC4) {
    1147         struct evbuffer_ptr pos;
    1148         struct evbuffer_iovec iovec;
    1149         evbuffer_ptr_set (outbuf, &pos, old_length, EVBUFFER_PTR_SET);
    1150         do {
    1151             if (evbuffer_peek (outbuf, -1, &pos, &iovec, 1) != 1)
    1152               break;
    1153             tr_cryptoDecrypt (&io->crypto, iovec.iov_len, iovec.iov_base, iovec.iov_base);
    1154             byteCount -= iovec.iov_len;
    1155         } while (!evbuffer_ptr_set (outbuf, &pos, iovec.iov_len, EVBUFFER_PTR_ADD));
    1156     }
     1176    maybeDecryptBuffer (io, outbuf, old_length, byteCount);
    11571177}
    11581178
Note: See TracChangeset for help on using the changeset viewer.