2 * mqtt-arp.c - Watch the Linux ARP table to report device presence via MQTT
4 * Copyright 2018 Jonathan McDowell <noodles@earth.li>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include <sys/types.h>
28 #include <sys/socket.h>
31 #include <linux/netlink.h>
32 #include <linux/rtnetlink.h>
34 #include <mosquitto.h>
36 /* Defaults. All overridable from command line. */
37 #define MQTT_HOST "mqtt-host"
38 #define MQTT_PORT 8883
39 #define MQTT_TOPIC "location/by-mac"
40 #define LOCATION "home"
42 /* How often (in seconds) to report that we see a device */
43 #define REPORT_INTERVAL (2 * 60)
44 /* How long to wait without seeing a device before reporting it's gone */
45 #define EXPIRY_TIME (10 * 60)
46 /* Maximum number of MAC addresses to watch for */
64 struct mac_entry macs[MAX_MACS];
68 bool want_shutdown = false;
70 void shutdown_request(int signal)
75 bool mac_compare(uint8_t *a, uint8_t *b)
79 for (i = 0; i < 6; i++)
84 printf("Matched: %02x:%02x:%02x:%02x:%02x:%02x\n",
91 int mqtt_mac_presence(struct ma_config *config, struct mosquitto *mosq,
92 uint8_t *mac, bool present)
102 while (i < MAX_MACS && config->macs[i].valid) {
103 if (mac_compare(mac, config->macs[i].mac))
108 if (i >= MAX_MACS || !config->macs[i].valid)
111 config->macs[i].last_seen = t;
112 /* Report no more often than every 2 minutes */
113 if (present && config->macs[i].last_reported + REPORT_INTERVAL > t)
116 config->macs[i].last_reported = t;
118 snprintf(topic, sizeof(topic),
119 "%s/%02X:%02X:%02X:%02X:%02X:%02X",
121 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
124 printf("Publishing to %s\n", topic);
127 ret = mosquitto_publish(mosq, NULL, topic,
128 strlen(config->location), config->location,
131 ret = mosquitto_publish(mosq, NULL, topic,
132 strlen("unknown"), "unknown", 0, 0);
137 void prune_macs(struct ma_config *config, struct mosquitto *mosq)
145 while (i < MAX_MACS && config->macs[i].valid) {
146 /* Expire if we haven't seen MAC in EXPIRY_TIME */
147 if (config->macs[i].last_seen &&
148 config->macs[i].last_seen + EXPIRY_TIME < t) {
149 mqtt_mac_presence(config, mosq,
150 config->macs[i].mac, false);
151 config->macs[i].last_seen = 0;
152 config->macs[i].last_reported = 0;
158 void mosq_log_callback(struct mosquitto *mosq, void *userdata, int level,
162 printf("%i:%s\n", level, str);
165 void main_loop(struct ma_config *config, struct mosquitto *mosq, int sock)
169 struct nlmsghdr *hdr;
175 hdr = (struct nlmsghdr *) buf;
176 nd = (struct ndmsg *) (hdr + 1);
177 while (!want_shutdown) {
178 received = recv(sock, buf, sizeof(buf), 0);
181 printf("%sReceived %zd bytes:\n", ctime(&t), received);
182 printf(" Len: %d, type: %d, flags: %x, "
183 "seq: %d, pid: %d\n",
184 hdr->nlmsg_len, hdr->nlmsg_type,
185 hdr->nlmsg_flags, hdr->nlmsg_seq,
188 switch (hdr->nlmsg_type) {
191 printf(" Family: %d, interface: %d, "
192 "state: %x, flags: %x, type: %x\n",
193 nd->ndm_family, /* AF_INET etc */
195 nd->ndm_state, /* NUD_REACHABLE etc */
199 attr = (struct nlattr *) (nd + 1);
200 while (((uint8_t *) attr - buf) < hdr->nlmsg_len) {
201 data = (((uint8_t *) attr) + NLA_HDRLEN);
202 if (attr->nla_type == NDA_LLADDR &&
203 nd->ndm_state == NUD_REACHABLE) {
204 mqtt_mac_presence(config, mosq,
207 attr = (struct nlattr *) (((uint8_t *) attr) +
208 NLA_ALIGN(attr->nla_len));
215 printf("Unknown message type: %d\n", hdr->nlmsg_type);
218 prune_macs(config, mosq);
223 struct mosquitto *mqtt_init(struct ma_config *config)
225 struct mosquitto *mosq;
228 mosquitto_lib_init();
229 mosq = mosquitto_new("mqtt-arp", true, NULL);
231 printf("Couldn't allocate mosquitto structure\n");
235 mosquitto_log_callback_set(mosq, mosq_log_callback);
237 /* DTRT if username is NULL */
238 mosquitto_username_pw_set(mosq,
239 config->mqtt_username,
240 config->mqtt_password);
242 mosquitto_tls_set(mosq, config->capath,
243 NULL, NULL, NULL, NULL);
245 ret = mosquitto_connect(mosq, config->mqtt_host,
246 config->mqtt_port, 60);
248 printf("Unable to connect to MQTT server.\n");
252 ret = mosquitto_loop_start(mosq);
254 printf("Unable to start Mosquitto loop.\n");
261 int netlink_init(void)
264 struct sockaddr_nl group_addr;
266 bzero(&group_addr, sizeof(group_addr));
267 group_addr.nl_family = AF_NETLINK;
268 group_addr.nl_pid = getpid();
269 group_addr.nl_groups = RTMGRP_NEIGH;
271 sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
273 perror("Couldn't open netlink socket");
277 if (bind(sock, (struct sockaddr *) &group_addr,
278 sizeof(group_addr)) < 0) {
279 perror("Failed to bind to netlink socket");
286 struct option long_options[] = {
287 { "capath", required_argument, 0, 'c' },
288 { "host", required_argument, 0, 'h' },
289 { "location", required_argument, 0, 'l' },
290 { "mac", required_argument, 0, 'm' },
291 { "password", required_argument, 0, 'P' },
292 { "port", required_argument, 0, 'p' },
293 { "topic", required_argument, 0, 't' },
294 { "username", required_argument, 0, 'u' },
295 { "verbose", no_argument, 0, 'v' },
299 int main(int argc, char *argv[])
302 struct mosquitto *mosq;
303 struct ma_config config;
304 int option_index = 0;
308 bzero(&config, sizeof(config));
309 config.mqtt_port = MQTT_PORT;
312 c = getopt_long(argc, argv, "c:h:l:m:p:P:t:u:v",
313 long_options, &option_index);
320 config.capath = optarg;
323 config.mqtt_host = optarg;
326 config.location = optarg;
329 if (macs >= MAX_MACS) {
330 printf("Can only accept %d MAC addresses to"
331 " watch for.\n", MAX_MACS);
335 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
336 &config.macs[macs].mac[0],
337 &config.macs[macs].mac[1],
338 &config.macs[macs].mac[2],
339 &config.macs[macs].mac[3],
340 &config.macs[macs].mac[4],
341 &config.macs[macs].mac[5]);
342 config.macs[macs].valid = true;
346 config.mqtt_port = atoi(optarg);
349 config.mqtt_password = optarg;
352 config.mqtt_topic = optarg;
355 config.mqtt_username = optarg;
361 printf("Unrecognized option: %c\n", c);
366 if (!config.mqtt_host)
367 config.mqtt_host = MQTT_HOST;
368 if (!config.mqtt_topic)
369 config.mqtt_topic = MQTT_TOPIC;
370 if (!config.location)
371 config.location = LOCATION;
373 signal(SIGTERM, shutdown_request);
375 sock = netlink_init();
376 mosq = mqtt_init(&config);
378 main_loop(&config, mosq, sock);
380 mosquitto_disconnect(mosq);
381 mosquitto_loop_stop(mosq, true);
382 mosquitto_destroy(mosq);
383 mosquitto_lib_cleanup();