]> the.earth.li Git - mqtt-arp.git/blobdiff - mqtt-arp.c
Silently drop RTM_DELNEIGH/RTM_GETNEIGH messages
[mqtt-arp.git] / mqtt-arp.c
index 2badfac8eda176fb95ff2a978163101a5f1655a2..71d8567e39653ca73b2fc75b5d70eebdc676ca02 100644 (file)
@@ -17,6 +17,7 @@
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 #include <getopt.h>
+#include <signal.h>
 #include <stdbool.h>
 #include <stdint.h>
 #include <stdlib.h>
@@ -64,6 +65,12 @@ struct ma_config {
 };
 
 bool debug = false;
+bool want_shutdown = false;
+
+void shutdown_request(int signal)
+{
+       want_shutdown = true;
+}
 
 bool mac_compare(uint8_t *a, uint8_t *b)
 {
@@ -167,7 +174,7 @@ void main_loop(struct ma_config *config, struct mosquitto *mosq, int sock)
 
        hdr = (struct nlmsghdr *) buf;
        nd = (struct ndmsg *) (hdr + 1);
-       while (1) {
+       while (!want_shutdown) {
                received = recv(sock, buf, sizeof(buf), 0);
                if (debug) {
                        t = time(NULL);
@@ -190,19 +197,20 @@ void main_loop(struct ma_config *config, struct mosquitto *mosq, int sock)
                                        nd->ndm_type);
                        }
                        attr = (struct nlattr *) (nd + 1);
-                       while (attr->nla_len > 0) {
-                               data = (((uint8_t *) attr) + 4);
+                       while (((uint8_t *) attr - buf) < hdr->nlmsg_len) {
+                               data = (((uint8_t *) attr) + NLA_HDRLEN);
                                if (attr->nla_type == NDA_LLADDR &&
                                        nd->ndm_state == NUD_REACHABLE) {
                                        mqtt_mac_presence(config, mosq,
                                                        data, true);
                                }
-                               attr = (struct nlattr *)
-                                       (((uint8_t *) attr) + attr->nla_len);
+                               attr = (struct nlattr *) (((uint8_t *) attr) +
+                                               NLA_ALIGN(attr->nla_len));
                        }
                        break;
                case RTM_DELNEIGH:
                case RTM_GETNEIGH:
+                       break;
                default:
                        printf("Unknown message type: %d\n", hdr->nlmsg_type);
                }
@@ -280,8 +288,10 @@ struct option long_options[] = {
        { "host", required_argument, 0, 'h' },
        { "location", required_argument, 0, 'l' },
        { "mac", required_argument, 0, 'm' },
+       { "password", required_argument, 0, 'P' },
        { "port", required_argument, 0, 'p' },
        { "topic", required_argument, 0, 't' },
+       { "username", required_argument, 0, 'u' },
        { "verbose", no_argument, 0, 'v' },
        { 0, 0, 0, 0 }
 };
@@ -299,7 +309,7 @@ int main(int argc, char *argv[])
        config.mqtt_port = MQTT_PORT;
 
        while (1) {
-               c = getopt_long(argc, argv, "c:h:l:m:p:t:v",
+               c = getopt_long(argc, argv, "c:h:l:m:p:P:t:u:v",
                                long_options, &option_index);
 
                if (c == -1)
@@ -335,9 +345,15 @@ int main(int argc, char *argv[])
                case 'p':
                        config.mqtt_port = atoi(optarg);
                        break;
+               case 'P':
+                       config.mqtt_password = optarg;
+                       break;
                case 't':
                        config.mqtt_topic = optarg;
                        break;
+               case 'u':
+                       config.mqtt_username = optarg;
+                       break;
                case 'v':
                        debug = true;
                        break;
@@ -354,8 +370,16 @@ int main(int argc, char *argv[])
        if (!config.location)
                config.mqtt_host = LOCATION;
 
+       signal(SIGTERM, shutdown_request);
+
        sock = netlink_init();
        mosq = mqtt_init(&config);
 
        main_loop(&config, mosq, sock);
+
+       mosquitto_disconnect(mosq);
+       mosquitto_loop_stop(mosq, true);
+       mosquitto_destroy(mosq);
+       mosquitto_lib_cleanup();
+       close(sock);
 }