diff options
-rw-r--r-- | client.c | 2 | ||||
-rw-r--r-- | icmp.h | 1 | ||||
-rw-r--r-- | server.c | 4 | ||||
-rw-r--r-- | util.c | 20 | ||||
-rw-r--r-- | util.h | 3 |
5 files changed, 16 insertions, 14 deletions
@@ -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); } } } @@ -1,5 +1,6 @@ #pragma once +#include <stddef.h> #include <stdint.h> @@ -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); } } @@ -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; } @@ -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); |