blob: 44aed5f5077193ccd3a202a19e2ac305afed712b [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001From 11f224baeede709a181a9ccb01558ff39432a994 Mon Sep 17 00:00:00 2001
2From: Jeffery To <jeffery.to@gmail.com>
3Date: Mon, 5 Jul 2021 04:23:19 +0800
4Subject: [PATCH] Use C99 format macro constants for timestamp and vlan_tag
5
6Since timestamp and vlan_tag in the shm_log_entry struct are C99 fixed
7width integer types (uint64_t and uint16_t), the cross-platform way to
8print these values is to use the corresponding format macro
9constants[1], PRIu64 and PRIu16.
10
11This also adjusts the places where the time_t timestamp value is
12printed, casting it to uint64_t, for consistency.
13
14Fixes https://github.com/fln/addrwatch/issues/25
15Fixes https://github.com/fln/addrwatch/issues/26
16
17[1]: https://en.cppreference.com/w/c/types/integer#Format_macro_constants
18---
19 configure.ac | 2 +-
20 src/addrwatch.c | 2 +-
21 src/addrwatch_stdout.c | 2 +-
22 src/addrwatch_syslog.c | 2 +-
23 src/base64.h | 2 +-
24 src/common.h | 2 +-
25 src/mcache.h | 2 +-
26 src/output_flatfile.c | 4 ++--
27 src/parse.c | 2 +-
28 src/shm.h | 2 +-
29 src/shm_client.c | 2 +-
30 src/storage.c | 2 +-
31 src/util.h | 2 +-
32 13 files changed, 14 insertions(+), 14 deletions(-)
33
34--- a/configure.ac
35+++ b/configure.ac
36@@ -53,7 +53,7 @@ AC_ARG_ENABLE([mysql],
37 )
38
39 # Checks for header files.
40-AC_CHECK_HEADERS([arpa/inet.h netinet/in.h stdint.h stdlib.h syslog.h unistd.h])
41+AC_CHECK_HEADERS([arpa/inet.h netinet/in.h inttypes.h stdlib.h syslog.h unistd.h])
42
43 # Checks for typedefs, structures, and compiler characteristics.
44 AC_C_INLINE
45--- a/src/addrwatch.c
46+++ b/src/addrwatch.c
47@@ -3,7 +3,7 @@
48 #include <limits.h>
49 #include <pwd.h>
50 #include <signal.h>
51-#include <stdint.h>
52+#include <inttypes.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56--- a/src/addrwatch_stdout.c
57+++ b/src/addrwatch_stdout.c
58@@ -16,7 +16,7 @@ void process_entry(struct shm_log_entry
59 ip4_ntoa(e->ip_address, ip_str);
60 }
61
62- printf("%lu %s %u %s %s %s\n", e->timestamp, e->interface, e->vlan_tag,
63+ printf("%" PRIu64 " %s %" PRIu16 " %s %s %s\n", e->timestamp, e->interface, e->vlan_tag,
64 mac_str, ip_str, pkt_origin_str[e->origin]);
65 }
66
67--- a/src/addrwatch_syslog.c
68+++ b/src/addrwatch_syslog.c
69@@ -18,7 +18,7 @@ void process_entry(struct shm_log_entry
70 ip4_ntoa(e->ip_address, ip_str);
71 }
72
73- syslog(LOG_INFO, "%lu %s %u %s %s %s", e->timestamp, e->interface,
74+ syslog(LOG_INFO, "%" PRIu64 " %s %" PRIu16 " %s %s %s", e->timestamp, e->interface,
75 e->vlan_tag, mac_str, ip_str, pkt_origin_str[e->origin]);
76 }
77
78--- a/src/base64.h
79+++ b/src/base64.h
80@@ -2,7 +2,7 @@
81 #define BASE64_H
82
83 #include "addrwatch.h"
84-#include <stdint.h>
85+#include <inttypes.h>
86
87 void base64_encode(const uint8_t *src, char *dst, int ssize, int dsize);
88 char *base64_encode_packet(struct pkt *p);
89--- a/src/common.h
90+++ b/src/common.h
91@@ -2,7 +2,7 @@
92 #define COMMON_H
93
94 #include <arpa/inet.h>
95-#include <stdint.h>
96+#include <inttypes.h>
97 #include <stdio.h>
98 #include <sys/socket.h>
99
100--- a/src/mcache.h
101+++ b/src/mcache.h
102@@ -6,7 +6,7 @@
103
104 #include <sys/types.h>
105 #include <netinet/if_ether.h>
106-#include <stdint.h>
107+#include <inttypes.h>
108
109 struct mcache_node {
110 uint8_t l2_addr[ETHER_ADDR_LEN];
111--- a/src/output_flatfile.c
112+++ b/src/output_flatfile.c
113@@ -22,8 +22,8 @@ void output_flatfile_reload()
114 void output_flatfile_save(struct pkt *p, char *mac_str, char *ip_str)
115 {
116 if (cfg.data_fd) {
117- fprintf(cfg.data_fd, "%lu %s %u %s %s %s\n",
118- p->pcap_header->ts.tv_sec, p->ifc->name, p->vlan_tag,
119+ fprintf(cfg.data_fd, "%" PRIu64 " %s %" PRIu16 " %s %s %s\n",
120+ (uint64_t)p->pcap_header->ts.tv_sec, p->ifc->name, p->vlan_tag,
121 mac_str, ip_str, pkt_origin_str[p->origin]);
122 fflush(cfg.data_fd);
123 }
124--- a/src/parse.c
125+++ b/src/parse.c
126@@ -1,4 +1,4 @@
127-//#include <stdint.h>
128+//#include <inttypes.h>
129 //#include <stdio.h>
130 //#include <stdlib.h>
131
132--- a/src/shm.h
133+++ b/src/shm.h
134@@ -4,7 +4,7 @@
135 #include <net/if.h>
136 #include <netinet/in.h>
137 #include <netinet/if_ether.h>
138-#include <stdint.h>
139+#include <inttypes.h>
140 #include <sys/socket.h>
141
142 #define DEFAULT_SHM_LOG_NAME "/addrwatch-shm-log"
143--- a/src/shm_client.c
144+++ b/src/shm_client.c
145@@ -2,7 +2,7 @@
146
147 #include <fcntl.h>
148 #include <net/if.h>
149-#include <stdint.h>
150+#include <inttypes.h>
151 #include <stdlib.h>
152 #include <sys/mman.h>
153 #include <sys/stat.h>
154--- a/src/storage.c
155+++ b/src/storage.c
156@@ -129,7 +129,7 @@ void save_pairing(struct pkt *p)
157
158 output_shm_save(p, mac_str, ip_str);
159 if (!cfg.quiet) {
160- printf("%lu %s %u %s %s %s\n", tstamp, p->ifc->name,
161+ printf("%" PRIu64 " %s %" PRIu16 " %s %s %s\n", (uint64_t)tstamp, p->ifc->name,
162 p->vlan_tag, mac_str, ip_str, pkt_origin_str[p->origin]);
163 fflush(stdout);
164 }
165--- a/src/util.h
166+++ b/src/util.h
167@@ -5,7 +5,7 @@
168 #include "config.h"
169 #endif
170
171-#include <stdint.h>
172+#include <inttypes.h>
173 #include <stdio.h>
174
175 #include <syslog.h>