Ver código fonte

SPProtoDecoder: add log prefix to log messages

ambrop7 14 anos atrás
pai
commit
7d2180e1a1
3 arquivos alterados com 22 adições e 14 exclusões
  1. 1 1
      client/DatagramPeerIO.c
  2. 15 12
      client/SPProtoDecoder.c
  3. 6 1
      client/SPProtoDecoder.h

+ 1 - 1
client/DatagramPeerIO.c

@@ -198,7 +198,7 @@ int DatagramPeerIO_Init (
     PacketPassNotifier_Init(&o->recv_notifier, FragmentProtoAssembler_GetInput(&o->recv_assembler), BReactor_PendingGroup(o->reactor));
     
     // init decoder
-    if (!SPProtoDecoder_Init(&o->recv_decoder, PacketPassNotifier_GetInput(&o->recv_notifier), o->sp_params, 2, BReactor_PendingGroup(o->reactor), twd)) {
+    if (!SPProtoDecoder_Init(&o->recv_decoder, PacketPassNotifier_GetInput(&o->recv_notifier), o->sp_params, 2, BReactor_PendingGroup(o->reactor), twd, o->user, o->logfunc)) {
         PeerLog(o, BLOG_ERROR, "SPProtoDecoder_Init failed");
         goto fail1;
     }

+ 15 - 12
client/SPProtoDecoder.c

@@ -25,12 +25,13 @@
 #include <misc/balign.h>
 #include <misc/byteorder.h>
 #include <security/BHash.h>
-#include <base/BLog.h>
 
 #include "SPProtoDecoder.h"
 
 #include <generated/blog_channel_SPProtoDecoder.h>
 
+#define PeerLog(_o, ...) BLog_LogViaFunc((_o)->logfunc, (_o)->user, BLOG_CURRENT_CHANNEL, __VA_ARGS__)
+
 static void decode_work_func (SPProtoDecoder *o)
 {
     ASSERT(o->in_len >= 0)
@@ -51,19 +52,19 @@ static void decode_work_func (SPProtoDecoder *o)
     } else {
         // input must be a multiple of blocks size
         if (in_len % o->enc_block_size != 0) {
-            BLog(BLOG_WARNING, "packet size not a multiple of block size");
+            PeerLog(o, BLOG_WARNING, "packet size not a multiple of block size");
             return;
         }
         
         // input must have an IV block
         if (in_len < o->enc_block_size) {
-            BLog(BLOG_WARNING, "packet does not have an IV");
+            PeerLog(o, BLOG_WARNING, "packet does not have an IV");
             return;
         }
         
         // check if we have encryption key
         if (!o->have_encryption_key) {
-            BLog(BLOG_WARNING, "have no encryption key");
+            PeerLog(o, BLOG_WARNING, "have no encryption key");
             return;
         }
         
@@ -79,7 +80,7 @@ static void decode_work_func (SPProtoDecoder *o)
         
         // read padding
         if (ciphertext_len < o->enc_block_size) {
-            BLog(BLOG_WARNING, "packet does not have a padding block");
+            PeerLog(o, BLOG_WARNING, "packet does not have a padding block");
             return;
         }
         int i;
@@ -88,12 +89,12 @@ static void decode_work_func (SPProtoDecoder *o)
                 break;
             }
             if (plaintext[i] != 0) {
-                BLog(BLOG_WARNING, "packet padding wrong (nonzero byte)");
+                PeerLog(o, BLOG_WARNING, "packet padding wrong (nonzero byte)");
                 return;
             }
         }
         if (i < ciphertext_len - o->enc_block_size) {
-            BLog(BLOG_WARNING, "packet padding wrong (all zeroes)");
+            PeerLog(o, BLOG_WARNING, "packet padding wrong (all zeroes)");
             return;
         }
         plaintext_len = i;
@@ -101,14 +102,14 @@ static void decode_work_func (SPProtoDecoder *o)
     
     // check for header
     if (plaintext_len < SPPROTO_HEADER_LEN(o->sp_params)) {
-        BLog(BLOG_WARNING, "packet has no header");
+        PeerLog(o, BLOG_WARNING, "packet has no header");
         return;
     }
     uint8_t *header = plaintext;
     
     // check data length
     if (plaintext_len - SPPROTO_HEADER_LEN(o->sp_params) > o->output_mtu) {
-        BLog(BLOG_WARNING, "packet too long");
+        PeerLog(o, BLOG_WARNING, "packet too long");
         return;
     }
     
@@ -135,7 +136,7 @@ static void decode_work_func (SPProtoDecoder *o)
         memcpy(header_hash, hash, o->hash_size);
         // compare hashes
         if (memcmp(hash, hash_calc, o->hash_size)) {
-            BLog(BLOG_WARNING, "packet has wrong hash");
+            PeerLog(o, BLOG_WARNING, "packet has wrong hash");
             return;
         }
     }
@@ -158,7 +159,7 @@ static void decode_work_handler (SPProtoDecoder *o)
     // check OTP
     if (SPPROTO_HAVE_OTP(o->sp_params) && o->tw_out_len >= 0) {
         if (!OTPChecker_CheckOTP(&o->otpchecker, o->tw_out_seed_id, o->tw_out_otp)) {
-            BLog(BLOG_WARNING, "packet has wrong OTP");
+            PeerLog(o, BLOG_WARNING, "packet has wrong OTP");
             o->tw_out_len = -1;
         }
     }
@@ -216,7 +217,7 @@ static void maybe_stop_work_and_ignore (SPProtoDecoder *o)
     }
 }
 
-int SPProtoDecoder_Init (SPProtoDecoder *o, PacketPassInterface *output, struct spproto_security_params sp_params, int num_otp_seeds, BPendingGroup *pg, BThreadWorkDispatcher *twd)
+int SPProtoDecoder_Init (SPProtoDecoder *o, PacketPassInterface *output, struct spproto_security_params sp_params, int num_otp_seeds, BPendingGroup *pg, BThreadWorkDispatcher *twd, void *user, BLog_logfunc logfunc)
 {
     spproto_assert_security_params(sp_params);
     ASSERT(spproto_carrier_mtu_for_payload_mtu(sp_params, PacketPassInterface_GetMTU(output)) >= 0)
@@ -226,6 +227,8 @@ int SPProtoDecoder_Init (SPProtoDecoder *o, PacketPassInterface *output, struct
     o->output = output;
     o->sp_params = sp_params;
     o->twd = twd;
+    o->user = user;
+    o->logfunc = logfunc;
     
     // init output
     PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o);

+ 6 - 1
client/SPProtoDecoder.h

@@ -31,6 +31,7 @@
 
 #include <misc/debug.h>
 #include <base/DebugObject.h>
+#include <base/BLog.h>
 #include <protocol/spproto.h>
 #include <security/BEncryption.h>
 #include <security/OTPChecker.h>
@@ -52,6 +53,8 @@ typedef struct {
     PacketPassInterface *output;
     struct spproto_security_params sp_params;
     BThreadWorkDispatcher *twd;
+    void *user;
+    BLog_logfunc logfunc;
     int output_mtu;
     int hash_size;
     int enc_block_size;
@@ -87,9 +90,11 @@ typedef struct {
  *                      receiving packets. Must be >=2 if using OTPs.
  * @param pg pending group
  * @param twd thread work dispatcher
+ * @param user argument to handlers
+ * @param logfunc function which prepends the log prefix using {@link BLog_Append}
  * @return 1 on success, 0 on failure
  */
-int SPProtoDecoder_Init (SPProtoDecoder *o, PacketPassInterface *output, struct spproto_security_params sp_params, int num_otp_seeds, BPendingGroup *pg, BThreadWorkDispatcher *twd) WARN_UNUSED;
+int SPProtoDecoder_Init (SPProtoDecoder *o, PacketPassInterface *output, struct spproto_security_params sp_params, int num_otp_seeds, BPendingGroup *pg, BThreadWorkDispatcher *twd, void *user, BLog_logfunc logfunc) WARN_UNUSED;
 
 /**
  * Frees the object.