updates.
[silc.git] / lib / silcutil / silcutil.c
index 36668b3571d5ec613c96062e454e108e72bbf42a..d4d56d035328d425a07c6f68c99e194824ea7694 100644 (file)
@@ -274,6 +274,7 @@ char *silc_encode_pem_file(unsigned char *data, uint32 data_len)
     pem2[i] = pem[j++];
   }
 
+  silc_free(pem);
   return pem2;
 }
 
@@ -486,7 +487,15 @@ char *silc_id_render(void *id, uint16 type)
   case SILC_ID_SERVER:
     {
       SilcServerID *server_id = (SilcServerID *)id;
-      strcat(rid, inet_ntoa(server_id->ip));
+      struct in_addr ipv4;
+
+      if (server_id->ip.data_len > 4) {
+
+      } else {
+       SILC_GET32_MSB(ipv4.s_addr, server_id->ip.data);
+       strcat(rid, inet_ntoa(ipv4));
+      }
+
       memset(tmp, 0, sizeof(tmp));
       snprintf(tmp, sizeof(tmp), ",%d,", ntohs(server_id->port));
       strcat(rid, tmp);
@@ -499,7 +508,15 @@ char *silc_id_render(void *id, uint16 type)
   case SILC_ID_CLIENT:
     {
       SilcClientID *client_id = (SilcClientID *)id;
-      strcat(rid, inet_ntoa(client_id->ip));
+      struct in_addr ipv4;
+
+      if (client_id->ip.data_len > 4) {
+
+      } else {
+       SILC_GET32_MSB(ipv4.s_addr, client_id->ip.data);
+       strcat(rid, inet_ntoa(ipv4));
+      }
+
       memset(tmp, 0, sizeof(tmp));
       snprintf(tmp, sizeof(tmp), ",%02x,", client_id->rnd);
       strcat(rid, tmp);
@@ -513,7 +530,15 @@ char *silc_id_render(void *id, uint16 type)
   case SILC_ID_CHANNEL:
     {
       SilcChannelID *channel_id = (SilcChannelID *)id;
-      strcat(rid, inet_ntoa(channel_id->ip));
+      struct in_addr ipv4;
+
+      if (channel_id->ip.data_len > 4) {
+
+      } else {
+       SILC_GET32_MSB(ipv4.s_addr, channel_id->ip.data);
+       strcat(rid, inet_ntoa(ipv4));
+      }
+
       memset(tmp, 0, sizeof(tmp));
       snprintf(tmp, sizeof(tmp), ",%d,", ntohs(channel_id->port));
       strcat(rid, tmp);
@@ -699,6 +724,11 @@ char *silc_get_username()
 {
   char *logname = NULL;
   
+  if (!getenv("LOGNAME")) {
+    fprintf(stderr, "Error: environment variable $LOGNAME not set\n");
+    return NULL;
+  }
+
   logname = strdup(getenv("LOGNAME"));
   if (!logname) {
     logname = getlogin();
@@ -738,3 +768,83 @@ char *silc_get_real_name()
 
   return realname;
 }
+
+/* Basic has function to hash strings. May be used with the SilcHashTable. */
+
+uint32 silc_hash_string(void *key)
+{
+  char *s = (char *)key;
+  uint32 h = 0, g;
+  
+  while (*s != '\0') {
+    h = (h << 4) + toupper(*s);
+    if ((g = h & 0xf0000000)) {
+      h = h ^ (g >> 24);
+      h = h ^ g;
+    }
+    s++;
+  }
+  
+  return h;
+}
+
+/* Basic hash function to hash integers. May be used with the SilcHashTable. */
+
+uint32 silc_hash_uint(void *key)
+{
+  return *(uint32 *)key;
+}
+
+/* Basic hash funtion to hash pointers. May be used with the SilcHashTable. */
+
+uint32 silc_hash_ptr(void *key)
+{
+  return (uint32)key;
+}
+
+/* Hash a Server ID. */
+
+uint32 silc_hash_server_id(void *key)
+{
+  SilcServerID *id = (SilcServerID *)key;
+  int i;
+  uint32 h;
+
+  h = id->port * id->rnd;
+  for (i = 0; i < id->ip.data_len; i++)
+    h ^= id->ip.data[i];
+
+  return h;
+}
+
+/* Hash a Client ID. */
+
+uint32 silc_hash_client_id(void *key)
+{
+  SilcClientID *id = (SilcClientID *)key;
+  int i;
+  uint32 h;
+
+  h = id->rnd;
+  for (i = 0; i < sizeof(id->hash); i++)
+    h ^= id->hash[i];
+  for (i = 0; i < id->ip.data_len; i++)
+    h ^= id->ip.data[i];
+
+  return h;
+}
+
+/* Hash a Channel ID. */
+
+uint32 silc_hash_channel_id(void *key)
+{
+  SilcChannelID *id = (SilcChannelID *)key;
+  int i;
+  uint32 h;
+
+  h = id->port * id->rnd;
+  for (i = 0; i < id->ip.data_len; i++)
+    h ^= id->ip.data[i];
+
+  return h;
+}