added gnutls backend and moved backend code into new files
[ircu2.10.12-pk.git] / ircd / numnicks.c
index fe9a5fa43282dea1a3678a59ccf3fa0f83d4aa77..66e432135d23db284df65b3e1c1b0a03518bd2c8 100644 (file)
@@ -16,7 +16,7 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
-/* @file
+/** @file
  * @brief Implementation of numeric nickname operations.
  * @version $Id$
  */
@@ -26,6 +26,7 @@
 #include "client.h"
 #include "ircd.h"
 #include "ircd_alloc.h"
+#include "ircd_log.h"
 #include "ircd_string.h"
 #include "match.h"
 #include "s_bsd.h"
 #include "s_misc.h"
 #include "struct.h"
 
-#include <assert.h>
+/* #include <assert.h> -- Now using assert in ircd_log.h */
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
 
-/** @page numnicks %Numeric Nicks
+/** @page numnicks Numeric Nicks
  * %Numeric nicks (numnicks) are new as of version ircu2.10.00beta1.
  *
  * The idea is as follows:
@@ -47,7 +48,7 @@
  * replaced by a 5 character string: YYXXX
  * Where 'YY' represents the server, and 'XXX' the nick on that server.
  *
- * 'YYXXX' should not interfer with the input parser, and therefore is
+ * 'YYXXX' should not interfere with the input parser, and therefore is
  * not allowed to contain spaces or a ':'.
  * Also, 'YY' can't start with a '+' because of m_server().
  *
@@ -87,7 +88,7 @@ static struct Client* server_list[NN_MAX_SERVER];
  *
  * '\\0' : Because we use '\\0' as end of line.
  *
- * ' '  : Because parse_*() uses this as parameter seperator.
+ * ' '  : Because parse_*() uses this as parameter separator.
  *
  * ':'  : Because parse_server() uses this to detect if a prefix is a
  *        numeric or a name.
@@ -438,12 +439,19 @@ struct Client* find_match_server(char *mask)
  * @param[out] buf Output buffer to write to.
  * @param[in] addr IP address to encode.
  * @param[in] count Number of bytes writable to \a buf.
+ * @param[in] v6_ok If non-zero, peer understands base-64 encoded IPv6 addresses.
  */
-const char* iptobase64(char* buf, const struct irc_in_addr* addr, unsigned int count)
+const char* iptobase64(char* buf, const struct irc_in_addr* addr, unsigned int count, int v6_ok)
 {
   if (irc_in_addr_is_ipv4(addr)) {
     assert(count >= 6);
-    inttobase64(buf, (htons(addr->in6_16[6]) << 16) | htons(addr->in6_16[7]), 6);
+    inttobase64(buf, (ntohs(addr->in6_16[6]) << 16) | ntohs(addr->in6_16[7]), 6);
+  } else if (!v6_ok) {
+    assert(count >= 6);
+    if (addr->in6_16[0] == htons(0x2002))
+        inttobase64(buf, (ntohs(addr->in6_16[1]) << 16) | ntohs(addr->in6_16[2]), 6);
+    else
+        strcpy(buf, "AAAAAA");
   } else {
     unsigned int max_start, max_zeros, curr_zeros, zero, ii;
     char *output = buf;
@@ -494,14 +502,18 @@ void base64toip(const char* input, struct irc_in_addr* addr)
   memset(addr, 0, sizeof(*addr));
   if (strlen(input) == 6) {
     unsigned int in = base64toint(input);
-    addr->in6_16[6] = htons(in >> 16);
-    addr->in6_16[7] = htons(in & 65535);
+    /* An all-zero address should stay that way. */
+    if (in) {
+      addr->in6_16[5] = htons(65535);
+      addr->in6_16[6] = htons(in >> 16);
+      addr->in6_16[7] = htons(in & 65535);
+    }
   } else {
     unsigned int pos = 0;
     do {
       if (*input == '_') {
         unsigned int left;
-        for (left = (25 - strlen(input)) / 3; left; left--)
+        for (left = (25 - strlen(input)) / 3 - pos; left; left--)
           addr->in6_16[pos++] = 0;
         input++;
       } else {
@@ -509,7 +521,6 @@ void base64toip(const char* input, struct irc_in_addr* addr)
         accum = (accum << NUMNICKLOG) | convert2n[(unsigned char)*input++];
         accum = (accum << NUMNICKLOG) | convert2n[(unsigned char)*input++];
         addr->in6_16[pos++] = ntohs(accum);
-        input += 3;
       }
     } while (pos < 8);
   }