r14464: Don't include ndr_BASENAME.h files unless strictly required, instead
[jelmer/samba4-debian.git] / source / nbt_server / wins / winsdb.c
index e3c02e05f07a33babc2e1e8ec941205e1479dbdf..a0b65bec2583a114bc70a6db9fd9bf78255ad239 100644 (file)
 #include "nbt_server/wins/winsdb.h"
 #include "lib/ldb/include/ldb.h"
 #include "lib/ldb/include/ldb_errors.h"
+#include "librpc/gen_ndr/ndr_nbt.h"
 #include "system/time.h"
+#include "db_wrap.h"
+#include "system/network.h"
+#include "netif/netif.h"
 
 uint64_t winsdb_get_maxVersion(struct winsdb_handle *h)
 {
@@ -310,22 +314,30 @@ failed:
 
 /*
  encode the winsdb_addr("address") attribute like this:
+ non-static record:
  "172.31.1.1;winsOwner:172.31.9.202;expireTime:20050923032330.0Z;"
+ static record:
+ "172.31.1.1"
 */
-static int ldb_msg_add_winsdb_addr(struct ldb_message *msg, 
+static int ldb_msg_add_winsdb_addr(struct ldb_message *msg, struct winsdb_record *rec,
                                   const char *attr_name, struct winsdb_addr *addr)
 {
        struct ldb_val val;
        const char *str;
-       char *expire_time;
 
-       expire_time = ldb_timestring(msg, addr->expire_time);
-       if (!expire_time) return -1;
-       str = talloc_asprintf(msg, "%s;winsOwner:%s;expireTime:%s;",
-                             addr->address, addr->wins_owner,
-                             expire_time);
-       talloc_free(expire_time);
-       if (!str) return -1;
+       if (rec->is_static) {
+               str = talloc_strdup(msg, addr->address);
+               if (!str) return -1;
+       } else {
+               char *expire_time;
+               expire_time = ldb_timestring(msg, addr->expire_time);
+               if (!expire_time) return -1;
+               str = talloc_asprintf(msg, "%s;winsOwner:%s;expireTime:%s;",
+                                     addr->address, addr->wins_owner,
+                                     expire_time);
+               talloc_free(expire_time);
+               if (!str) return -1;
+       }
 
        val.data = discard_const_p(uint8_t, str);
        val.length = strlen(str);
@@ -345,11 +357,134 @@ struct winsdb_addr **winsdb_addr_list_make(TALLOC_CTX *mem_ctx)
        return addresses;
 }
 
-struct winsdb_addr **winsdb_addr_list_add(struct winsdb_addr **addresses, const char *address,
-                                         const char *wins_owner, time_t expire_time)
+static int winsdb_addr_sort_list (struct winsdb_addr **p1, struct winsdb_addr **p2, void *opaque)
 {
-       size_t len = winsdb_addr_list_length(addresses);
+       struct winsdb_addr *a1 = talloc_get_type(*p1, struct winsdb_addr);
+       struct winsdb_addr *a2 = talloc_get_type(*p2, struct winsdb_addr);
+       struct winsdb_handle *h= talloc_get_type(opaque, struct winsdb_handle);
+       BOOL a1_owned = False;
+       BOOL a2_owned = False;
+
+       /*
+        * first the owned addresses with the newest to the oldest address
+        * then the replica addresses with the newest to the oldest address
+        */
+       if (a2->expire_time != a1->expire_time) {
+               return a2->expire_time - a1->expire_time;
+       }
+
+       if (strcmp(a2->wins_owner, h->local_owner) == 0) {
+               a2_owned = True;
+       }
+
+       if (strcmp(a1->wins_owner, h->local_owner) == 0) {
+               a1_owned = True;
+       }
+
+       return a2_owned - a1_owned;
+}
+
+struct winsdb_addr **winsdb_addr_list_add(struct winsdb_handle *h, const struct winsdb_record *rec,
+                                         struct winsdb_addr **addresses, const char *address,
+                                         const char *wins_owner, time_t expire_time,
+                                         BOOL is_name_registration)
+{
+       struct winsdb_addr *old_addr = NULL;
+       size_t len = 0;
+       size_t i;
+       BOOL found_old_replica = False;
+
+       /*
+        * count the addresses and maybe
+        * find an old entry for the new address
+        */
+       for (i=0; addresses[i]; i++) {
+               if (old_addr) continue;
+               if (strcmp(addresses[i]->address, address) == 0) {
+                       old_addr = addresses[i];
+               }
+       }
+       len = i;
+
+       /*
+        * the address is already there
+        * and we can replace it
+        */
+       if (old_addr) {
+               goto remove_old_addr;
+       }
+
+       /*
+        * if we don't have 25 addresses already,
+        * we can just add the new address
+        */
+       if (len < 25) {
+               goto add_new_addr;
+       }
 
+       /*
+        * if we haven't found the address,
+        * and we have already have 25 addresses
+        * if so then we need to do the following:
+        * - if it isn't a name registration, then just ignore the new address
+        * - if it is a name registration, then first search for 
+        *   the oldest replica and if there's no replica address
+        *   search the oldest owned address
+        */
+       if (!is_name_registration) {
+               return addresses;
+       }
+
+       /*
+        * find the oldest replica address, if there's no replica
+        * record at all, find the oldest owned address
+        */
+       for (i=0; addresses[i]; i++) {
+               BOOL cur_is_replica = False;
+               /* find out if the current address is a replica */
+               if (strcmp(addresses[i]->wins_owner, h->local_owner) != 0) {
+                       cur_is_replica = True;
+               }
+
+               /*
+                * if we already found a replica address and the current address
+                * is not a replica, then skip it
+                */
+               if (found_old_replica && !cur_is_replica) continue;
+
+               /*
+                * if we found the first replica address, reset the address
+                * that would be replaced
+                */
+               if (!found_old_replica && cur_is_replica) {
+                       found_old_replica = True;
+                       old_addr = addresses[i];
+                       continue;
+               }
+
+               /*
+                * if the first address isn't a replica, just start with 
+                * the first one
+                */
+               if (!old_addr) {
+                       old_addr = addresses[i];
+                       continue;
+               }
+
+               /*
+                * see if we find an older address
+                */
+               if (addresses[i]->expire_time < old_addr->expire_time) {
+                       old_addr = addresses[i];
+                       continue;
+               }
+       }
+
+remove_old_addr:
+       winsdb_addr_list_remove(addresses, old_addr->address);
+       len --;
+
+add_new_addr:
        addresses = talloc_realloc(addresses, addresses, struct winsdb_addr *, len + 2);
        if (!addresses) return NULL;
 
@@ -375,6 +510,8 @@ struct winsdb_addr **winsdb_addr_list_add(struct winsdb_addr **addresses, const
 
        addresses[len+1] = NULL;
 
+       ldb_qsort(addresses, len+1 , sizeof(addresses[0]), h, (ldb_qsort_cmp_fn_t)winsdb_addr_sort_list);
+
        return addresses;
 }
 
@@ -387,7 +524,6 @@ void winsdb_addr_list_remove(struct winsdb_addr **addresses, const char *address
                        break;
                }
        }
-       if (!addresses[i]) return;
 
        for (; addresses[i]; i++) {
                addresses[i] = addresses[i+1];
@@ -617,13 +753,6 @@ struct ldb_message *winsdb_message(struct ldb_context *ldb,
        struct ldb_message *msg = ldb_msg_new(mem_ctx);
        if (msg == NULL) goto failed;
 
-       if (rec->is_static && rec->state == WREPL_STATE_ACTIVE) {
-               rec->expire_time = get_time_t_max();
-               for (i=0;rec->addresses[i];i++) {
-                       rec->addresses[i]->expire_time = rec->expire_time;
-               }
-       }
-
        /* make sure we don't put in corrupted records */
        addr_count = winsdb_addr_list_length(rec->addresses);
        if (rec->state == WREPL_STATE_ACTIVE && addr_count == 0) {
@@ -652,12 +781,15 @@ struct ldb_message *winsdb_message(struct ldb_context *ldb,
        ret |= ldb_msg_add_fmt(msg, "recordState", "%u", rec->state);
        ret |= ldb_msg_add_fmt(msg, "nodeType", "%u", rec->node);
        ret |= ldb_msg_add_fmt(msg, "isStatic", "%u", rec->is_static);
-       ret |= ldb_msg_add_string(msg, "expireTime", expire_time);
+       ret |= ldb_msg_add_empty(msg, "expireTime", 0);
+       if (!(rec->is_static && rec->state == WREPL_STATE_ACTIVE)) {
+               ret |= ldb_msg_add_string(msg, "expireTime", expire_time);
+       }
        ret |= ldb_msg_add_fmt(msg, "versionID", "%llu", (long long)rec->version);
        ret |= ldb_msg_add_string(msg, "winsOwner", rec->wins_owner);
        ret |= ldb_msg_add_empty(msg, "address", 0);
        for (i=0;rec->addresses[i];i++) {
-               ret |= ldb_msg_add_winsdb_addr(msg, "address", rec->addresses[i]);
+               ret |= ldb_msg_add_winsdb_addr(msg, rec, "address", rec->addresses[i]);
        }
        ret |= ldb_msg_add_empty(msg, "registeredBy", 0);
        if (rec->registered_by) {