[pisa-src] r1743 - trunk/pisacd/cdservers.c

  • From: Thomas Jansen <mithi@xxxxxxxxx>
  • To: pisa-src@xxxxxxxxxxxxx
  • Date: Mon, 23 Nov 2009 14:21:42 +0100

Author: tjansen
Date: Mon Nov 23 14:21:42 2009
New Revision: 1743

Log:
Added function pisa_servers_build_domain_name.

Modified:
   trunk/pisacd/cdservers.c

Modified: trunk/pisacd/cdservers.c
==============================================================================
--- trunk/pisacd/cdservers.c    Mon Nov 23 13:26:00 2009        (r1742)
+++ trunk/pisacd/cdservers.c    Mon Nov 23 14:21:42 2009        (r1743)
@@ -17,6 +17,10 @@
 #include "libpisa/cfg.h"
 #include "libpisa/nat.h"
 
+/* Suffix for a DNS lookup to map a virtual local IP to a service. Does not
+ * contain a leading dot. */
+#define PISA_LOCAL_IP_TO_SERVICE_SUFFIX "pisa"
+
 /**
  * Add a server connection if it passes sanity checks. If it is a relay type
  * connection, connect to it immediately. Otherwise keep it in the
@@ -184,6 +188,38 @@
 }
 
 /**
+ * Generate a domain name for looking up a virtual local IP.
+ *
+ * @param addr virtual local IP address
+ * @param buf destination buffer for the created named
+ * @param size buffer size
+ * @return true on success, false otherwise
+ */
+static bool pisa_servers_build_domain_name(struct in_addr *addr, char *buf, 
size_t size)
+{
+       union {
+               uint32_t complete;
+               unsigned char byte[4];
+       } a;
+
+       assert(addr);
+       assert(buf);
+
+       /* Make sure that we have enough space. The maximum length for a
+        * virtual local IP address is 16 bytes:
+        *   4 numbers with a maximum of 3 digits
+        * + 4 dots (3 between the numbers, one before the suffix) */
+       if (size < strlen(PISA_LOCAL_IP_TO_SERVICE_SUFFIX) + 16)
+               return false;
+
+       a.complete = addr->s_addr;
+
+       snprintf(buf, size, "%i.%i.%i.%i.%s", a.byte[3], a.byte[2], a.byte[1], 
a.byte[0], PISA_LOCAL_IP_TO_SERVICE_SUFFIX);
+
+       return true;
+}
+
+/**
  * Retrieve the NAT mapping and the conmgr entry from DNS.
  *
  * TODO: Remove the NAT mapping if an error occurs. Otherwise we might have a
@@ -199,7 +235,7 @@
 {
        pisa_nat_mapping *nat = (pisa_nat_mapping *) data;
        char local_str[INET_ADDRSTRLEN], remote_str[INET_ADDRSTRLEN];
-       char hit_str[INET6_ADDRSTRLEN];
+       char hit_str[INET6_ADDRSTRLEN], dn[128];
        pisa_conmgr_entry *entry;
        struct in6_addr hit;
        struct in_addr remote;
@@ -211,6 +247,9 @@
 
        pisa_nat_upgrade_preliminary(cd_ctx.natlist, nat, &remote, entry, NULL);
 
+       pisa_servers_build_domain_name(&nat->local_private, dn, sizeof(dn));
+       PISA_DEBUG(PL_CONFIG, "domain name: %s\n", dn);
+
        inet_ntop(AF_INET, &nat->local_private, local_str, sizeof(local_str));
        inet_ntop(AF_INET, &nat->remote.ipv4, remote_str, sizeof(remote_str));
        inet_ntop(AF_INET6, &nat->remote.hit, hit_str, sizeof(hit_str));

Other related posts:

  • » [pisa-src] r1743 - trunk/pisacd/cdservers.c - Thomas Jansen