Added SILC_SWAB_[16/32] to swab integer byte order.
[silc.git] / apps / silcd / serverid.c
index fb29b468412e6f2b828f4949aacd2b222385094e..7d8fdea5c1f059e28cf8ffd9d779677c8d2c75f4 100644 (file)
@@ -1,16 +1,16 @@
 /*
 
-  id.c
+  serverid.c
 
-  Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
+  Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 1997 - 2001 Pekka Riikonen
+  Copyright (C) 1997 - 2002 Pekka Riikonen
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
-  
+
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 /* Creates a Server ID. Newly created Server ID is returned to the
    new_id argument. */
 
-void silc_id_create_server_id(int sock, SilcRng rng, SilcServerID **new_id)
+void silc_id_create_server_id(const char *ip, SilcUInt16 port, SilcRng rng,
+                             SilcServerID **new_id)
 {
-  struct sockaddr_in server;
-  int rval, len;
-
   SILC_LOG_DEBUG(("Creating new Server ID"));
 
   *new_id = silc_calloc(1, sizeof(**new_id));
 
-  /* Get IP address */
-  len = sizeof(server);
-  rval = getsockname(sock, (struct sockaddr *)&server, &len);
-  if (rval < 0) {
-    SILC_LOG_ERROR(("Could not get IP address: %s", strerror(errno)));
+  /* Create the ID */
+
+  if (!silc_net_addr2bin(ip, (*new_id)->ip.data,
+                        sizeof((*new_id)->ip.data))) {
     silc_free(*new_id);
     *new_id = NULL;
     return;
   }
 
-  /* Create the ID */
-  /* XXX Does not support IPv6 */
-  SILC_PUT32_MSB(server.sin_addr.s_addr, (*new_id)->ip.data);
-  (*new_id)->ip.data_len = 4;
-  (*new_id)->port = server.sin_port;
+  (*new_id)->ip.data_len = silc_net_is_ip4(ip) ? 4 : 16;
+  (*new_id)->port = SILC_SWAB_16(port);
   (*new_id)->rnd = silc_rng_get_rn16(rng);
 
   SILC_LOG_DEBUG(("New ID (%s)", silc_id_render(*new_id, SILC_ID_SERVER)));
@@ -67,13 +61,16 @@ bool silc_id_create_client_id(SilcServer server,
 {
   unsigned char hash[16];
   bool finding = FALSE;
+  char nick[128 + 1];
 
   SILC_LOG_DEBUG(("Creating new Client ID"));
 
   *new_id = silc_calloc(1, sizeof(**new_id));
 
   /* Create hash of the nickanem */
-  silc_hash_make(md5hash, nickname, strlen(nickname), hash);
+  memset(nick, 0, sizeof(nick));
+  silc_to_lower(nickname, nick, sizeof(nick) - 1);
+  silc_hash_make(md5hash, nick, strlen(nick), hash);
 
   /* Create the ID */
   memcpy((*new_id)->ip.data, server_id->ip.data, server_id->ip.data_len);
@@ -131,10 +128,8 @@ bool silc_id_create_channel_id(SilcServer server,
   while (1) {
     if (!silc_idlist_find_channel_by_id(server->local_list, 
                                        *new_id, NULL))
-      if (!silc_idlist_find_channel_by_id(server->global_list, 
-                                         *new_id, NULL))
-       break;
-    
+      break;
+
     (*new_id)->rnd++;
     
     if (finding && (*new_id)->rnd == 0)
@@ -150,3 +145,26 @@ bool silc_id_create_channel_id(SilcServer server,
 
   return TRUE;
 }
+
+/* Checks whether the `server_id' is valid.  It must be based to the
+   IP address provided in the `remote' socket connection. */
+
+bool silc_id_is_valid_server_id(SilcServer server,
+                               SilcServerID *server_id,
+                               SilcSocketConnection remote)
+{
+  unsigned char ip[16];
+
+  if (!silc_net_addr2bin(remote->ip, ip, sizeof(ip)))
+    return FALSE;
+
+  if (silc_net_is_ip4(remote->ip)) {
+    if (!memcmp(server_id->ip.data, ip, 4))
+      return TRUE;
+  } else {
+    if (!memcmp(server_id->ip.data, ip, 16))
+      return TRUE;
+  }
+
+  return FALSE;
+}