aboutsummaryrefslogtreecommitdiff
path: root/src/utils.c
diff options
context:
space:
mode:
authorToni Uhlig <matzeton@googlemail.com>2020-05-27 20:01:45 +0200
committerToni Uhlig <matzeton@googlemail.com>2020-05-27 20:01:45 +0200
commitd2e92ee748f3b3e826ffcbf665adc45637ad9045 (patch)
tree994a7b54dc9cbbad364036f25412ba4c5dc519fc /src/utils.c
parented456a32ffe6fea0cb52b5184a00a14cb0059f4a (diff)
re-enabled and improved logging of packet headers and payload
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
Diffstat (limited to 'src/utils.c')
-rw-r--r--src/utils.c30
1 files changed, 20 insertions, 10 deletions
diff --git a/src/utils.c b/src/utils.c
index cba85fe..7426ce1 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -132,25 +132,35 @@ int host_to_addr(const char * hostname, uint32_t * result)
return 0;
}
-#if 0
static const char hextab[] = "0123456789ABCDEF";
-void print_hexstr(unsigned char *buf, size_t siz) {
- char *out = (char *) calloc(3, siz+1);
- unsigned char high, low;
+void log_sendrecv_hexstr(const char *prefix, void *buf, size_t siz) {
+ if (opts.log_level != kLog_sendrecv) {
+ return;
+ }
+
+ const size_t outsiz = siz * 3;
+
+ if (outsiz + 1 > BUFSIZ) {
+ pt_log(kLog_error, "Can not print hex string with size %zu: too big\n", siz);
+ return;
+ }
+
+ char out[outsiz + 1];
+ unsigned char high, low;
- for (size_t i = 0; i < siz; ++i) {
- high = (buf[i] & 0xF0) >> 4;
- low = buf[i] & 0x0F;
+ size_t i, j;
+ for (i = 0, j = 0; j < siz && i < outsiz; i += 3, ++j) {
+ high = (((unsigned char *)buf)[j] & 0xF0) >> 4;
+ low = ((unsigned char *)buf)[j] & 0x0F;
out[i ] = hextab[high];
out[i+1] = hextab[low];
out[i+2] = ' ';
}
+ out[i] = '\0';
- printf("%s\n", out);
- free(out);
+ pt_log(kLog_sendrecv, "%s[HEX]: %s\n", prefix, out);
}
-#endif
int pt_random(void)
{