core: sort IPv6 paths before IPv4 paths
authorPavel Šimerda <pavlix@pavlix.net>
Tue, 14 Jan 2014 14:34:52 +0000 (15:34 +0100)
committerPavel Šimerda <pavlix@pavlix.net>
Tue, 14 Jan 2014 14:48:27 +0000 (15:48 +0100)
TODO
lib/backend.c
tests/data/any
tests/data/localhost
tests/data/services

diff --git a/TODO b/TODO
index afd9ae7a62e8c2991f72c92a57e3770f61d69788..f369c09c4c3b5e4e3da40a0553864d3f9dfb38f8 100644 (file)
--- a/TODO
+++ b/TODO
@@ -2,10 +2,11 @@
 
 ## Core library
 
- * Return items in the right order
-    - http://tools.ietf.org/html/rfc6724
+ * Improve path ordering
     - use priority from DNS SRV
-    - randomize order using weight from DNS SRV
+    - use weight from DNS SRV for randomized ordering
+    - use rules from http://tools.ietf.org/html/rfc6724
+    - use /etc/gai.conf
  * Support multiple concurrent queries per channel
  * Support reverse lookup
  * Improve socket API and happy eyeballs
index 8df323c03f04e54a8288f7da57bcd37928d03ced..a827361d115cd325567bbb3820ac6de0560a9ac0 100644 (file)
@@ -106,6 +106,17 @@ path_callback(int socktype, int protocol, int port, void *user_data)
                        0, 0);
 }
 
+static int
+path_cmp(struct netresolve_path *p1, struct netresolve_path *p2)
+{
+       if (p1->node.family == AF_INET6 && p2->node.family == AF_INET)
+               return -1;
+       if (p1->node.family == AF_INET && p2->node.family == AF_INET6)
+               return 1;
+
+       return 0;
+}
+
 void
 netresolve_backend_add_path(netresolve_query_t channel,
                int family, const void *address, int ifindex,
@@ -114,6 +125,7 @@ netresolve_backend_add_path(netresolve_query_t channel,
 {
        struct netresolve_response *response = &channel->response;
        struct netresolve_path path;
+       int i;
 
        if (family == AF_UNIX && socktype == -1) {
                netresolve_backend_add_path(channel, family, address, 0, SOCK_STREAM, 0, 0, 0, 0);
@@ -152,8 +164,14 @@ netresolve_backend_add_path(netresolve_query_t channel,
        path.priority = priority;
        path.weight = weight;
 
+       for (i = 0; i < response->pathcount; i++)
+               if (path_cmp(&path, &response->paths[i]) < 0)
+                       break;
+
        response->paths = realloc(response->paths, (response->pathcount + 1) * sizeof path);
-       memcpy(&response->paths[response->pathcount++], &path, sizeof path);
+       memmove(&response->paths[i+1], &response->paths[i],
+                       (response->pathcount++ - i) * sizeof *response->paths);
+       memcpy(&response->paths[i], &path, sizeof path);
 
        debug("added path: %s", netresolve_get_path_string(channel, response->pathcount - 1));
 
index bf2a55f72d521236197dad1d9778e54910c7bdb3..cfe61bd4b19ccadf3f9599dc5dc6e8035a4f69a2 100644 (file)
@@ -1,4 +1,4 @@
 response netresolve 0.0.1
-path 0.0.0.0 any any 0 0 0
 path :: any any 0 0 0
+path 0.0.0.0 any any 0 0 0
 
index c5119a221ed2ab87e484bc1d2671ecebbfa5a84f..4271fa10fb2ca302a8d23c34fff6259f77be8e30 100644 (file)
@@ -1,4 +1,4 @@
 response netresolve 0.0.1
-path 127.0.0.1 any any 0 0 0
 path ::1 any any 0 0 0
+path 127.0.0.1 any any 0 0 0
 
index e4c19696a004df488317b79a5862d803177c8ac8..4d13e7213f51843e083a222fd4a43b4f714561ef 100644 (file)
@@ -1,6 +1,6 @@
 response netresolve 0.0.1
-path 0.0.0.0 stream tcp 1021 0 0
-path 0.0.0.0 dgram udp 1021 0 0
 path :: stream tcp 1021 0 0
 path :: dgram udp 1021 0 0
+path 0.0.0.0 stream tcp 1021 0 0
+path 0.0.0.0 dgram udp 1021 0 0
 
This page took 0.052577 seconds and 5 git commands to generate.