Skip to content

Commit a673a23

Browse files
committed
addrwatch: Various fixes
Makefile changes include: * Include syslog output module * Move main binary (back) to /usr/sbin, as it is system administration related and requires superuser privileges New patches: * 003-add-space-for-null-byte.patch - from fln/addrwatch@374cfd2 * 004-more-specific-library-linking.patch - from fln/addrwatch@27b57d9 * 005-use-c99-format-macro-constants.patch - from fln/addrwatch#28 Init script changes include: * Fix command-line option names and format (from https://forum.openwrt.org/t/cant-start-addrwatch-service/60499/3) * Always use the --quiet command-line option, as the procd instance is not configured to capture stdout/stderr * Change the syslog config option to start the syslog output module Signed-off-by: Jeffery To <jeffery.to@gmail.com>
1 parent 1f9aa31 commit a673a23

5 files changed

Lines changed: 264 additions & 12 deletions

File tree

net/addrwatch/Makefile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk
99

1010
PKG_NAME:=addrwatch
1111
PKG_VERSION:=1.0.2
12-
PKG_RELEASE:=2
12+
PKG_RELEASE:=3
1313

1414
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
1515
PKG_SOURCE_URL:=https://github.com/fln/addrwatch/releases/download/v$(PKG_VERSION)
@@ -22,6 +22,7 @@ PKG_LICENSE_FILES:=COPYING
2222
PKG_INSTALL:=1
2323
PKG_BUILD_PARALLEL:=1
2424
PKG_BUILD_DEPENDS:=USE_UCLIBC:argp-standalone USE_MUSL:argp-standalone
25+
PKG_FIXUP:=autoreconf
2526

2627
include $(INCLUDE_DIR)/package.mk
2728

@@ -45,8 +46,9 @@ define Package/addrwatch/conffiles
4546
endef
4647

4748
define Package/addrwatch/install
48-
$(INSTALL_DIR) $(1)/usr/bin $(1)/etc/config $(1)/etc/init.d
49-
$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/bin/addrwatch $(1)/usr/bin/
49+
$(INSTALL_DIR) $(1)/usr/sbin $(1)/etc/config $(1)/etc/init.d
50+
$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/bin/addrwatch $(1)/usr/sbin/
51+
$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/bin/addrwatch_syslog $(1)/usr/sbin/
5052
$(INSTALL_BIN) ./files/addrwatch.init $(1)/etc/init.d/addrwatch
5153
$(INSTALL_CONF) ./files/addrwatch.config $(1)/etc/config/addrwatch
5254
endef

net/addrwatch/files/addrwatch.init

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,13 @@ start_instance() {
3636
done
3737

3838
procd_open_instance
39-
procd_set_param command /usr/sbin/addrwatch
40-
[ "$syslog" -eq 1 ] && procd_append_param command --syslog
41-
[ -n "$output" ] && procd_append_param command --output "$output"
42-
[ "$quiet" -eq 1 ] && procd_append_param command --quiet
43-
[ "$verbose" -eq 1 ] && procd_append_param command --verbose
44-
[ "$ipv4only" -eq 1 ] && procd_append_param command --ipv4only
45-
[ "$ipv6only" -eq 1 ] && procd_append_param command --ipv6only
46-
[ -n "$hashsize" ] && procd_append_param command --hashsize "$hashsize"
47-
[ -n "$ratelimit" ] && procd_append_param command --ratelimit "$ratelimit"
39+
procd_set_param command /usr/sbin/addrwatch --quiet
40+
[ -n "$output" ] && procd_append_param command "--output=$output"
41+
[ "$verbose" -eq 1 ] && procd_append_param command "--verbose"
42+
[ "$ipv4only" -eq 1 ] && procd_append_param command "--ipv4-only"
43+
[ "$ipv6only" -eq 1 ] && procd_append_param command "--ipv6-only"
44+
[ -n "$hashsize" ] && procd_append_param command "--hashsize=$hashsize"
45+
[ -n "$ratelimit" ] && procd_append_param command "--ratelimit=$ratelimit"
4846
for blitem in $blacklist; do
4947
procd_append_param command "--blacklist=$blitem"
5048
done
@@ -57,6 +55,17 @@ start_instance() {
5755
done
5856
procd_close_trigger
5957
procd_close_instance
58+
59+
[ "$syslog" -eq 1 ] && {
60+
if [ -x /usr/sbin/addrwatch_syslog ]; then
61+
procd_open_instance
62+
procd_set_param command /usr/sbin/addrwatch_syslog
63+
procd_set_param respawn
64+
procd_close_instance
65+
else
66+
echo "Cannot find /usr/sbin/addrwatch_syslog" >&2
67+
fi
68+
}
6069
}
6170

6271
start_service() {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
From 374cfd2cabe4db9882d8a210adff430cc579f859 Mon Sep 17 00:00:00 2001
2+
From: Julius Kriukas <julius@kriukas.lt>
3+
Date: Sun, 8 Mar 2020 12:46:55 +0200
4+
Subject: [PATCH] Use HOST_NAME_MAX+1 to add space for null byte
5+
6+
---
7+
src/addrwatch.c | 2 +-
8+
1 file changed, 1 insertion(+), 1 deletion(-)
9+
10+
--- a/src/addrwatch.c
11+
+++ b/src/addrwatch.c
12+
@@ -501,7 +501,7 @@ int main(int argc, char *argv[])
13+
argp_parse(&argp, argc, argv, 0, &optind, 0);
14+
15+
if (!cfg.hostname) {
16+
- cfg.hostname_len = HOST_NAME_MAX;
17+
+ cfg.hostname_len = HOST_NAME_MAX + 1;
18+
cfg.hostname = (char *)calloc(cfg.hostname_len, sizeof(char));
19+
gethostname(cfg.hostname, cfg.hostname_len);
20+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
From 1988f6228225e10bccc50941798f1e1b4ca1ff62 Mon Sep 17 00:00:00 2001
2+
From: Jeffery To <jeffery.to@gmail.com>
3+
Date: Fri, 18 Jun 2021 15:46:47 +0800
4+
Subject: [PATCH] More specific library linking
5+
6+
Currently, the main binary and all output modules are linked to the same
7+
set of libraries. This changes the linking so that only the main binary
8+
is linked to pcap, and only addrwatch_mysql is linked to mysqlclient.
9+
10+
This allows the main binary and output modules to be packaged separately
11+
with fewer dependencies for each individual package.
12+
---
13+
configure.ac | 4 ++--
14+
src/Makefile.am | 3 ++-
15+
2 files changed, 4 insertions(+), 3 deletions(-)
16+
17+
--- a/configure.ac
18+
+++ b/configure.ac
19+
@@ -12,7 +12,7 @@ optional_modules=""
20+
AC_SUBST([optional_modules])
21+
22+
# Checks for libraries.
23+
-AC_CHECK_LIB([pcap], [pcap_open_live])
24+
+AC_CHECK_LIB([pcap], [pcap_open_live], :)
25+
AC_CHECK_LIB([rt], [shm_open])
26+
27+
PKG_CHECK_MODULES(LIBEVENT, [libevent >= 1.4], , [
28+
@@ -46,7 +46,7 @@ AC_ARG_ENABLE([sqlite3],
29+
)
30+
AC_ARG_ENABLE([mysql],
31+
AS_HELP_STRING([--enable-mysql], [Enable MySQL database output]),
32+
- AC_CHECK_LIB([mysqlclient], [mysql_real_connect], , [
33+
+ AC_CHECK_LIB([mysqlclient], [mysql_real_connect], :, [
34+
AC_MSG_ERROR([Unable to find libmysqlclient.])
35+
])
36+
optional_modules="${optional_modules} addrwatch_mysql"
37+
--- a/src/Makefile.am
38+
+++ b/src/Makefile.am
39+
@@ -9,5 +9,6 @@ addrwatch_stdout_SOURCES = addrwatch_std
40+
addrwatch_syslog_SOURCES = addrwatch_syslog.c shm_client.c shm_client.h
41+
addrwatch_mysql_SOURCES = addrwatch_mysql.c shm_client.c shm_client.h util.c util.h
42+
43+
-addrwatch_LDADD = @LIBEVENT_LIBS@
44+
+addrwatch_LDADD = @LIBEVENT_LIBS@ -lpcap
45+
+addrwatch_mysql_LDADD = -lmysqlclient
46+
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
From 11f224baeede709a181a9ccb01558ff39432a994 Mon Sep 17 00:00:00 2001
2+
From: Jeffery To <jeffery.to@gmail.com>
3+
Date: Mon, 5 Jul 2021 04:23:19 +0800
4+
Subject: [PATCH] Use C99 format macro constants for timestamp and vlan_tag
5+
6+
Since timestamp and vlan_tag in the shm_log_entry struct are C99 fixed
7+
width integer types (uint64_t and uint16_t), the cross-platform way to
8+
print these values is to use the corresponding format macro
9+
constants[1], PRIu64 and PRIu16.
10+
11+
This also adjusts the places where the time_t timestamp value is
12+
printed, casting it to uint64_t, for consistency.
13+
14+
Fixes https://github.com/fln/addrwatch/issues/25
15+
Fixes 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>

0 commit comments

Comments
 (0)