From 11f224baeede709a181a9ccb01558ff39432a994 Mon Sep 17 00:00:00 2001 From: Jeffery To Date: Mon, 5 Jul 2021 04:23:19 +0800 Subject: [PATCH] Use C99 format macro constants for timestamp and vlan_tag Since timestamp and vlan_tag in the shm_log_entry struct are C99 fixed width integer types (uint64_t and uint16_t), the cross-platform way to print these values is to use the corresponding format macro constants[1], PRIu64 and PRIu16. This also adjusts the places where the time_t timestamp value is printed, casting it to uint64_t, for consistency. Fixes https://github.com/fln/addrwatch/issues/25 Fixes https://github.com/fln/addrwatch/issues/26 [1]: https://en.cppreference.com/w/c/types/integer#Format_macro_constants --- configure.ac | 2 +- src/addrwatch.c | 2 +- src/addrwatch_stdout.c | 2 +- src/addrwatch_syslog.c | 2 +- src/base64.h | 2 +- src/common.h | 2 +- src/mcache.h | 2 +- src/output_flatfile.c | 4 ++-- src/parse.c | 2 +- src/shm.h | 2 +- src/shm_client.c | 2 +- src/storage.c | 2 +- src/util.h | 2 +- 13 files changed, 14 insertions(+), 14 deletions(-) diff --git a/configure.ac b/configure.ac index fd7b263..a341eac 100644 --- a/configure.ac +++ b/configure.ac @@ -53,7 +53,7 @@ AC_ARG_ENABLE([mysql], ) # Checks for header files. -AC_CHECK_HEADERS([arpa/inet.h netinet/in.h stdint.h stdlib.h syslog.h unistd.h]) +AC_CHECK_HEADERS([arpa/inet.h netinet/in.h inttypes.h stdlib.h syslog.h unistd.h]) # Checks for typedefs, structures, and compiler characteristics. AC_C_INLINE diff --git a/src/addrwatch.c b/src/addrwatch.c index e0d5a46..98dd73d 100644 --- a/src/addrwatch.c +++ b/src/addrwatch.c @@ -3,7 +3,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/addrwatch_stdout.c b/src/addrwatch_stdout.c index 8c8a6e9..7ba0938 100644 --- a/src/addrwatch_stdout.c +++ b/src/addrwatch_stdout.c @@ -16,7 +16,7 @@ void process_entry(struct shm_log_entry *e, void *arg) ip4_ntoa(e->ip_address, ip_str); } - printf("%lu %s %u %s %s %s\n", e->timestamp, e->interface, e->vlan_tag, + printf("%" PRIu64 " %s %" PRIu16 " %s %s %s\n", e->timestamp, e->interface, e->vlan_tag, mac_str, ip_str, pkt_origin_str[e->origin]); } diff --git a/src/addrwatch_syslog.c b/src/addrwatch_syslog.c index f9da09e..c82fd01 100644 --- a/src/addrwatch_syslog.c +++ b/src/addrwatch_syslog.c @@ -18,7 +18,7 @@ void process_entry(struct shm_log_entry *e, void *arg) ip4_ntoa(e->ip_address, ip_str); } - syslog(LOG_INFO, "%lu %s %u %s %s %s", e->timestamp, e->interface, + syslog(LOG_INFO, "%" PRIu64 " %s %" PRIu16 " %s %s %s", e->timestamp, e->interface, e->vlan_tag, mac_str, ip_str, pkt_origin_str[e->origin]); } diff --git a/src/base64.h b/src/base64.h index 25df03c..6492529 100644 --- a/src/base64.h +++ b/src/base64.h @@ -2,7 +2,7 @@ #define BASE64_H #include "addrwatch.h" -#include +#include void base64_encode(const uint8_t *src, char *dst, int ssize, int dsize); char *base64_encode_packet(struct pkt *p); diff --git a/src/common.h b/src/common.h index 8381827..e4aa298 100644 --- a/src/common.h +++ b/src/common.h @@ -2,7 +2,7 @@ #define COMMON_H #include -#include +#include #include #include diff --git a/src/mcache.h b/src/mcache.h index 78db3b5..1388f5a 100644 --- a/src/mcache.h +++ b/src/mcache.h @@ -6,7 +6,7 @@ #include #include -#include +#include struct mcache_node { uint8_t l2_addr[ETHER_ADDR_LEN]; diff --git a/src/output_flatfile.c b/src/output_flatfile.c index 412fff9..c4be71e 100644 --- a/src/output_flatfile.c +++ b/src/output_flatfile.c @@ -22,8 +22,8 @@ void output_flatfile_reload() void output_flatfile_save(struct pkt *p, char *mac_str, char *ip_str) { if (cfg.data_fd) { - fprintf(cfg.data_fd, "%lu %s %u %s %s %s\n", - p->pcap_header->ts.tv_sec, p->ifc->name, p->vlan_tag, + fprintf(cfg.data_fd, "%" PRIu64 " %s %" PRIu16 " %s %s %s\n", + (uint64_t)p->pcap_header->ts.tv_sec, p->ifc->name, p->vlan_tag, mac_str, ip_str, pkt_origin_str[p->origin]); fflush(cfg.data_fd); } diff --git a/src/parse.c b/src/parse.c index 2b352f5..92856ea 100644 --- a/src/parse.c +++ b/src/parse.c @@ -1,4 +1,4 @@ -//#include +//#include //#include //#include diff --git a/src/shm.h b/src/shm.h index 1a154e2..7e12737 100644 --- a/src/shm.h +++ b/src/shm.h @@ -4,7 +4,7 @@ #include #include #include -#include +#include #include #define DEFAULT_SHM_LOG_NAME "/addrwatch-shm-log" diff --git a/src/shm_client.c b/src/shm_client.c index 930467b..f070695 100644 --- a/src/shm_client.c +++ b/src/shm_client.c @@ -2,7 +2,7 @@ #include #include -#include +#include #include #include #include diff --git a/src/storage.c b/src/storage.c index f0c68fb..2984296 100644 --- a/src/storage.c +++ b/src/storage.c @@ -129,7 +129,7 @@ void save_pairing(struct pkt *p) output_shm_save(p, mac_str, ip_str); if (!cfg.quiet) { - printf("%lu %s %u %s %s %s\n", tstamp, p->ifc->name, + printf("%" PRIu64 " %s %" PRIu16 " %s %s %s\n", (uint64_t)tstamp, p->ifc->name, p->vlan_tag, mac_str, ip_str, pkt_origin_str[p->origin]); fflush(stdout); } diff --git a/src/util.h b/src/util.h index 7936137..757ad2d 100644 --- a/src/util.h +++ b/src/util.h @@ -5,7 +5,7 @@ #include "config.h" #endif -#include +#include #include #include