updates.
[silc.git] / lib / silcutil / silcutil.c
index 0ee0dd6816e15f64e4e22cdd1b9ce236b4329dc7..450694b0171282a0d7434112e7f88f494ae41368 100644 (file)
@@ -1,16 +1,15 @@
 /*
 
-  silcutil.c
+  silcutil.c 
 
-  Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
+  Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 1997 - 2000 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.
-  
+  the Free Software Foundation; version 2 of the License.
+
   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
@@ -165,9 +164,13 @@ uint64 silc_file_size(const char *filename)
   int ret;
   struct stat stats;
 
+#ifndef SILC_WIN32\r
   ret = lstat(filename, &stats);
-  if (ret < 0)
-    return 0;
+#else\r
+  ret = stat(filename, &stats);\r
+#endif\r
+  if (ret < 0)\r
+    return 0;\r
 
   return (uint64)stats.st_size;
 }
@@ -413,6 +416,12 @@ bool silc_parse_userfqdn(const char *string, char **left, char **right)
   if (!string)
     return FALSE;
 
+  if (string[0] == '@') {
+    if (left)
+      *left = strdup(string);
+    return TRUE;
+  }
+
   if (strchr(string, '@')) {
     tlen = strcspn(string, "@");
     
@@ -535,12 +544,16 @@ char *silc_id_render(void *id, uint16 type)
   case SILC_ID_SERVER:
     {
       SilcServerID *server_id = (SilcServerID *)id;
-      struct in_addr ipv4;
-
       if (server_id->ip.data_len > 4) {
-
+#ifdef HAVE_IPV6
+       struct in6_addr ipv6;
+       memmove(&ipv6, server_id->ip.data, sizeof(ipv6));
+       if (!inet_ntop(AF_INET6, &ipv6, tmp, sizeof(tmp)))
+         strcat(rid, tmp);
+#endif
       } else {
-       SILC_GET32_MSB(ipv4.s_addr, server_id->ip.data);
+       struct in_addr ipv4;
+       memmove(&ipv4.s_addr, server_id->ip.data, 4);
        strcat(rid, inet_ntoa(ipv4));
       }
 
@@ -556,12 +569,16 @@ char *silc_id_render(void *id, uint16 type)
   case SILC_ID_CLIENT:
     {
       SilcClientID *client_id = (SilcClientID *)id;
-      struct in_addr ipv4;
-
       if (client_id->ip.data_len > 4) {
-
+#ifdef HAVE_IPV6
+       struct in6_addr ipv6;
+       memmove(&ipv6, client_id->ip.data, sizeof(ipv6));
+       if (!inet_ntop(AF_INET6, &ipv6, tmp, sizeof(tmp)))
+         strcat(rid, tmp);
+#endif
       } else {
-       SILC_GET32_MSB(ipv4.s_addr, client_id->ip.data);
+       struct in_addr ipv4;
+       memmove(&ipv4.s_addr, client_id->ip.data, 4);
        strcat(rid, inet_ntoa(ipv4));
       }
 
@@ -578,12 +595,16 @@ char *silc_id_render(void *id, uint16 type)
   case SILC_ID_CHANNEL:
     {
       SilcChannelID *channel_id = (SilcChannelID *)id;
-      struct in_addr ipv4;
-
       if (channel_id->ip.data_len > 4) {
-
+#ifdef HAVE_IPV6
+       struct in6_addr ipv6;
+       memmove(&ipv6, channel_id->ip.data, sizeof(ipv6));
+       if (!inet_ntop(AF_INET6, &ipv6, tmp, sizeof(tmp)))
+         strcat(rid, tmp);
+#endif
       } else {
-       SILC_GET32_MSB(ipv4.s_addr, channel_id->ip.data);
+       struct in_addr ipv4;
+       memmove(&ipv4.s_addr, channel_id->ip.data, 4);
        strcat(rid, inet_ntoa(ipv4));
       }
 
@@ -900,3 +921,31 @@ char *silc_client_chumode_char(uint32 mode)
 
   return strdup(string);
 }
+
+/* Creates fingerprint from data, usually used with SHA1 digests */
+
+char *silc_fingerprint(const unsigned char *data, uint32 data_len)
+{
+  char fingerprint[64], *cp;
+  int i;
+
+  memset(fingerprint, 0, sizeof(fingerprint));
+  cp = fingerprint;
+  for (i = 0; i < data_len; i++) {
+    snprintf(cp, sizeof(fingerprint), "%02X", data[i]);
+    cp += 2;
+    
+    if ((i + 1) % 2 == 0)
+      snprintf(cp++, sizeof(fingerprint), " ");
+
+    if ((i + 1) % 10 == 0)
+      snprintf(cp++, sizeof(fingerprint), " ");
+  }
+  i--;
+  if ((i + 1) % 2 == 0)
+    cp[-2] = 0;
+  if ((i + 1) % 10 == 0)
+    cp[-1] = 0;
+  
+  return strdup(fingerprint);
+}