[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] Use strmap code for client DNS.



Update of /home/or/cvsroot/src/or
In directory moria.mit.edu:/tmp/cvs-serv24772/src/or

Modified Files:
	connection_edge.c test.c 
Log Message:
Use strmap code for client DNS.

Index: connection_edge.c
===================================================================
RCS file: /home/or/cvsroot/src/or/connection_edge.c,v
retrieving revision 1.115
retrieving revision 1.116
diff -u -d -r1.115 -r1.116
--- connection_edge.c	9 Mar 2004 22:01:16 -0000	1.115
+++ connection_edge.c	20 Mar 2004 01:21:19 -0000	1.116
@@ -966,38 +966,21 @@
  *     other clients would reuse those funny addr's. Hm.
  */
 struct client_dns_entry {
-  SPLAY_ENTRY(client_dns_entry) node;
-  char *address;
   uint32_t addr;
   time_t expires;
 };
 static int client_dns_size = 0;
-static SPLAY_HEAD(client_dns_tree, client_dns_entry) client_dns_root;
-
-static int compare_client_dns_entries(struct client_dns_entry *a,
-                                      struct client_dns_entry *b)
-{
-  return strcasecmp(a->address, b->address);
-}
-
-static void client_dns_entry_free(struct client_dns_entry *ent)
-{
-  tor_free(ent->address);
-  tor_free(ent);
-}
-
-SPLAY_PROTOTYPE(client_dns_tree, client_dns_entry, node, compare_client_dns_entries);
-SPLAY_GENERATE(client_dns_tree, client_dns_entry, node, compare_client_dns_entries);
+static strmap_t *client_dns_map = NULL;
 
 void client_dns_init(void) {
-  SPLAY_INIT(&client_dns_root);
+  client_dns_map = strmap_new();
   client_dns_size = 0;
 }
 
+/* XXXX NM casei */
 static uint32_t client_dns_lookup_entry(const char *address)
 {
   struct client_dns_entry *ent;
-  struct client_dns_entry search;
   struct in_addr in;
   time_t now;
 
@@ -1008,8 +991,7 @@
            (unsigned long)ntohl(in.s_addr));
     return ntohl(in.s_addr);
   }
-  search.address = (char*)address;
-  ent = SPLAY_FIND(client_dns_tree, &client_dns_root, &search);
+  ent = strmap_get_lc(client_dns_map,address);
   if (!ent) {
     log_fn(LOG_DEBUG, "No entry found for address %s", address);
     return 0;
@@ -1017,8 +999,8 @@
     now = time(NULL);
     if (ent->expires < now) {
       log_fn(LOG_DEBUG, "Expired entry found for address %s", address);
-      SPLAY_REMOVE(client_dns_tree, &client_dns_root, ent);
-      client_dns_entry_free(ent);
+      strmap_remove_lc(client_dns_map,address);
+      tor_free(ent);
       --client_dns_size;
       return 0;
     }
@@ -1032,7 +1014,6 @@
 static void client_dns_set_entry(const char *address, uint32_t val)
 {
   struct client_dns_entry *ent;
-  struct client_dns_entry search;
   struct in_addr in;
   time_t now;
 
@@ -1041,9 +1022,8 @@
 
   if (tor_inet_aton(address, &in))
     return;
-  search.address = (char*) address;
   now = time(NULL);
-  ent = SPLAY_FIND(client_dns_tree, &client_dns_root, &search);
+  ent = strmap_get_lc(client_dns_map, address);
   if (ent) {
     in.s_addr = htonl(val);
     log_fn(LOG_DEBUG, "Updating entry for address %s: %s", address,
@@ -1055,38 +1035,35 @@
     log_fn(LOG_DEBUG, "Caching result for address %s: %s", address,
            inet_ntoa(in));
     ent = tor_malloc(sizeof(struct client_dns_entry));
-    ent->address = tor_strdup(address);
     ent->addr = val;
     ent->expires = now+MAX_DNS_ENTRY_AGE;
-    SPLAY_INSERT(client_dns_tree, &client_dns_root, ent);
+    strmap_set_lc(client_dns_map, address, ent);
     ++client_dns_size;
   }
 }
 
+static void* _remove_if_expired(const char *addr,
+                                struct client_dns_entry *ent,
+                                time_t *nowp)
+{
+  if (ent->expires < *nowp) {
+    --client_dns_size;
+    tor_free(ent);
+    return NULL;
+  } else {
+    return ent;
+  }
+}
+
 void client_dns_clean(void)
 {
-  struct client_dns_entry **expired_entries;
-  int n_expired_entries = 0;
   struct client_dns_entry *ent;
   time_t now;
-  int i;
 
   if(!client_dns_size)
     return;
-  expired_entries = tor_malloc(client_dns_size *
-                               sizeof(struct client_dns_entry *));
-
   now = time(NULL);
-  SPLAY_FOREACH(ent, client_dns_tree, &client_dns_root) {
-    if (ent->expires < now) {
-      expired_entries[n_expired_entries++] = ent;
-    }
-  }
-  for (i = 0; i < n_expired_entries; ++i) {
-    SPLAY_REMOVE(client_dns_tree, &client_dns_root, expired_entries[i]);
-    client_dns_entry_free(expired_entries[i]);
-  }
-  tor_free(expired_entries);
+  strmap_foreach(client_dns_map, (strmap_foreach_fn)_remove_if_expired, &now);
 }
 
 /*

Index: test.c
===================================================================
RCS file: /home/or/cvsroot/src/or/test.c,v
retrieving revision 1.64
retrieving revision 1.65
diff -u -d -r1.64 -r1.65
--- test.c	19 Mar 2004 22:07:24 -0000	1.64
+++ test.c	20 Mar 2004 01:21:19 -0000	1.65
@@ -539,6 +539,16 @@
 
   /* Clean up after ourselves. */
   strmap_free(map, NULL);
+
+  /* Now try some lc functions. */
+  map = strmap_new();
+  strmap_set_lc(map,"Ab.C", (void*)1);
+  test_eq(strmap_get(map,"ab.c"), (void*)1);
+  test_eq(strmap_get_lc(map,"AB.C"), (void*)1);
+  test_eq(strmap_get(map,"AB.C"), NULL);
+  test_eq(strmap_remove_lc(map,"aB.C"), (void*)1);
+  test_eq(strmap_get_lc(map,"AB.C"), NULL);
+  strmap_free(map,NULL);
 }
 
 void test_onion() {