summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Smeding <tom.smeding@gmail.com>2019-11-04 12:21:38 +0100
committerTom Smeding <tom.smeding@gmail.com>2019-11-04 12:21:38 +0100
commitfa1ae45491a8e41ccde4e0b366c37ec2a067bae9 (patch)
tree0a54d3746f3472aaad06b903829cb241850a39f7
parentcf6fbbec9e2a04217a135924fd2bf209be488223 (diff)
Let xxd take stream argument
-rw-r--r--client.c2
-rw-r--r--icmp.h1
-rw-r--r--server.c4
-rw-r--r--util.c20
-rw-r--r--util.h3
5 files changed, 16 insertions, 14 deletions
diff --git a/client.c b/client.c
index 0f8bde1..7c0eb71 100644
--- a/client.c
+++ b/client.c
@@ -52,7 +52,7 @@ int main(void) {
if (FD_ISSET(d_fd, &inset) && icmpd_peek(d)) {
struct icmpd_received msg = icmpd_recv(d);
printf("Recv: %zu\n", msg.length);
- xxd(msg.data, msg.length);
+ xxd(stdout, msg.data, msg.length);
}
}
}
diff --git a/icmp.h b/icmp.h
index eed1714..c242207 100644
--- a/icmp.h
+++ b/icmp.h
@@ -1,5 +1,6 @@
#pragma once
+#include <stddef.h>
#include <stdint.h>
diff --git a/server.c b/server.c
index b99e4ea..2ace5b5 100644
--- a/server.c
+++ b/server.c
@@ -53,7 +53,7 @@ int main(void) {
if (FD_ISSET(server_fd, &inset) && icmpd_peek(server)) {
struct icmpd_received msg = icmpd_recv(server);
printf("Server recv: %zu\n", msg.length);
- xxd(msg.data, msg.length);
+ xxd(stdout, msg.data, msg.length);
if (conn) {
printf("Message received while already connected\n");
@@ -76,7 +76,7 @@ int main(void) {
if (conn && FD_ISSET(conn_fd, &inset) && icmpd_peek(conn)) {
struct icmpd_received msg = icmpd_recv(conn);
printf("Connection recv: %zu\n", msg.length);
- xxd(msg.data, msg.length);
+ xxd(stdout, msg.data, msg.length);
free(msg.data);
}
}
diff --git a/util.c b/util.c
index 9c7e1a3..7bd9025 100644
--- a/util.c
+++ b/util.c
@@ -13,27 +13,27 @@ int uniqid(void) {
return i++;
}
-void xxd(const void *buf_, size_t length) {
+void xxd(FILE *stream, const void *buf_, size_t length) {
unsigned char *buf = (unsigned char*)buf_;
for (size_t cursor = 0; cursor < length;) {
- printf("%08zx:", cursor);
+ fprintf(stream, "%08zx:", cursor);
for (int i = 0; i < 16; i++) {
- if (i % 2 == 0) printf(" ");
- if (i % 8 == 0) printf(" ");
- if (cursor + i < length) printf("%02x", (unsigned)buf[cursor + i]);
- else printf(" ");
+ if (i % 2 == 0) fprintf(stream, " ");
+ if (i % 8 == 0) fprintf(stream, " ");
+ if (cursor + i < length) fprintf(stream, "%02x", (unsigned)buf[cursor + i]);
+ else fprintf(stream, " ");
}
- printf(" |");
+ fprintf(stream, " |");
for (int i = 0; i < 16 && cursor + i < length; i++) {
- if (isprint(buf[cursor + i])) printf("%c", buf[cursor + i]);
- else printf(".");
+ if (isprint(buf[cursor + i])) fprintf(stream, "%c", buf[cursor + i]);
+ else fprintf(stream, ".");
}
- printf("|\n");
+ fprintf(stream, "|\n");
cursor += 16;
}
diff --git a/util.h b/util.h
index 88ad996..898f10e 100644
--- a/util.h
+++ b/util.h
@@ -1,12 +1,13 @@
#pragma once
+#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
int uniqid(void);
-void xxd(const void *buf, size_t length);
+void xxd(FILE *stream, const void *buf, size_t length);
ssize_t readall(int fd, void *data, size_t length);
ssize_t writeall(int fd, const void *data, size_t length);
int maxi(int a, int b);