sta: remove duplicate code
[project/usteer.git] / remote.c
index ac725153658a5c8c654d85a84e04cf08d53a1368..cb4f3ddc350d8b61e89fb2e6e6090a8a1f236ab8 100644 (file)
--- a/remote.c
+++ b/remote.c
@@ -17,6 +17,8 @@
  *   Copyright (C) 2020 John Crispin <john@phrozen.org> 
  */
 
+#define _GNU_SOURCE
+
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
@@ -153,9 +155,11 @@ static void
 interface_add_station(struct usteer_remote_node *node, struct blob_attr *data)
 {
        struct sta *sta;
-       struct sta_info *si;
+       struct sta_info *si, *local_si;
        struct apmsg_sta msg;
+       struct usteer_node *local_node;
        bool create;
+       bool connect_change;
 
        if (!parse_apmsg_sta(&msg, data)) {
                MSG(DEBUG, "Cannot parse station in message\n");
@@ -175,9 +179,26 @@ interface_add_station(struct usteer_remote_node *node, struct blob_attr *data)
        if (!si)
                return;
 
+       connect_change = si->connected != msg.connected;
        si->connected = msg.connected;
        si->signal = msg.signal;
        si->seen = current_time - msg.seen;
+       si->last_connected = current_time - msg.last_connected;
+
+       /* Check if client roamed to this foreign node */
+       if ((connect_change || create) && si->connected == STA_CONNECTED) {
+               for_each_local_node(local_node) {
+                       local_si = usteer_sta_info_get(sta, local_node, NULL);
+                       if (!local_si)
+                               continue;
+
+                       if (current_time - local_si->last_connected < config.roam_process_timeout) {
+                               node->node.roam_events.target++;
+                               break;
+                       }
+               }
+       }
+
        usteer_sta_info_update_timeout(si, msg.timeout);
 }
 
@@ -236,6 +257,7 @@ interface_get_node(struct usteer_remote_host *host, const char *name)
 
        node = calloc_a(sizeof(*node), &buf, addr_len + 1 + strlen(name) + 1);
        node->node.type = NODE_TYPE_REMOTE;
+       node->node.created = current_time;
 
        sprintf(buf, "%s#%s", host->addr, name);
        node->node.avl.key = buf;
@@ -517,6 +539,7 @@ static void interface_send_msg(struct interface *iface, struct blob_attr *data){
 static void usteer_send_sta_info(struct sta_info *sta)
 {
        int seen = current_time - sta->seen;
+       int last_connected = !!sta->connected ? 0 : current_time - sta->last_connected;
        void *c;
 
        c = blob_nest_start(&buf, 0);
@@ -524,6 +547,7 @@ static void usteer_send_sta_info(struct sta_info *sta)
        blob_put_int8(&buf, APMSG_STA_CONNECTED, !!sta->connected);
        blob_put_int32(&buf, APMSG_STA_SIGNAL, sta->signal);
        blob_put_int32(&buf, APMSG_STA_SEEN, seen);
+       blob_put_int32(&buf, APMSG_STA_LAST_CONNECTED, last_connected);
        blob_put_int32(&buf, APMSG_STA_TIMEOUT, config.local_sta_timeout - seen);
        blob_nest_end(&buf, c);
 }
@@ -620,17 +644,16 @@ usteer_send_update_timer(struct uloop_timeout *t)
        struct usteer_node *node;
        void *c;
 
-       if (avl_is_empty(&local_nodes) && !host_info_blob)
-               return;
-
        usteer_update_time();
        uloop_timeout_set(t, config.remote_update_interval);
 
-       c = usteer_update_init();
-       for_each_local_node(node)
-               usteer_send_node(node, NULL);
+       if (!avl_is_empty(&local_nodes) || host_info_blob) {
+               c = usteer_update_init();
+               for_each_local_node(node)
+                       usteer_send_node(node, NULL);
 
-       usteer_update_send(c);
+               usteer_update_send(c);
+       }
        usteer_check_timeout();
 }