Merge branch 'mob' of git+ssh://localhost/srv/git/siplcs into mob
[siplcs.git] / src / core / uuid.c
blob84d45d39ccca53c1e1bb629579f07e3ad156e863
1 /**
2 * @file uuid.c
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <stdio.h>
20 #include <string.h>
22 #include <cipher.h>
23 #include <glib.h>
24 #include <glib/gprintf.h>
26 #include "uuid.h"
28 static const char *epid_ns_uuid = "fcacfb03-8a73-46ef-91b1-e5ebeeaba4fe";
31 * This assumes that the structure is correctly packed on all target
32 * platforms, i.e. sizeof(uuid_t) == 16
34 * See also the test added to "configure". On Windows platform we know
35 * that #pragma pack() exists and therefore can use it in the code.
38 #ifdef _WIN32
39 #pragma pack(push, 1)
40 #endif
41 typedef struct {
42 guint32 time_low;
43 guint16 time_mid;
44 guint16 time_hi_and_version;
45 guint8 clock_seq_hi_and_reserved;
46 guint8 clock_seq_low;
47 guint8 node[6];
48 } uuid_t;
49 #ifdef _WIN32
50 #pragma pack(pop)
51 #endif
53 #define UUID_OFFSET_TO_LAST_SEGMENT 24
55 static void readUUID(const char *string, uuid_t *uuid)
57 int i;
58 sscanf(string, "%08x-%04hx-%04hx-%02hhx%02hhx-", &uuid->time_low
59 , &uuid->time_mid, &uuid->time_hi_and_version
60 , &uuid->clock_seq_hi_and_reserved
61 , &uuid->clock_seq_low );
63 for(i=0;i<6;i++)
65 sscanf(&string[UUID_OFFSET_TO_LAST_SEGMENT+i*2], "%02hhx", &uuid->node[i]);
69 static void printUUID(uuid_t *uuid, char *string)
71 int i;
72 size_t pos;
73 sprintf(string, "%08x-%04x-%04x-%02x%02x-", uuid->time_low
74 , uuid->time_mid, uuid->time_hi_and_version
75 , uuid->clock_seq_hi_and_reserved
76 , uuid->clock_seq_low
78 pos = strlen(string);
79 for(i=0;i<6;i++)
81 pos += sprintf(&string[pos], "%02x", uuid->node[i]);
85 static void createUUIDfromHash(uuid_t *uuid, const unsigned char *hash)
87 memcpy(uuid, hash, sizeof(uuid_t));
88 uuid->time_hi_and_version &= 0x0FFF;
89 uuid->time_hi_and_version |= 0x5000;
90 uuid->clock_seq_hi_and_reserved &= 0x3F;
91 uuid->clock_seq_hi_and_reserved |= 0x80;
94 char *generateUUIDfromEPID(const gchar *epid)
96 uuid_t result;
97 PurpleCipherContext *ctx;
98 unsigned char hash[20];
99 char buf[512];
101 readUUID(epid_ns_uuid, &result);
102 memcpy(buf, &result, sizeof(uuid_t));
103 strcpy(&buf[sizeof(uuid_t)], epid);
105 ctx = purple_cipher_context_new_by_name("sha1", NULL);
106 purple_cipher_context_append(ctx, (guchar *) buf, strlen(buf));
107 purple_cipher_context_digest(ctx, sizeof(hash), hash, NULL);
108 purple_cipher_context_destroy(ctx);
110 createUUIDfromHash(&result, hash);
111 printUUID(&result, buf);
112 return g_strdup(buf);
116 * Generates epid from user SIP URI, hostname and IP address.
117 * Thus epid will be the same each start and
118 * not needed to be persistent.
120 * Using MAC address proved to be poorly portable solution.
122 * Must be g_free()'d
124 char *sipe_get_epid(const char *self_sip_uri,
125 const char *hostname,
126 const char *ip_address)
128 /* 6 last digits of hash */
129 #define SIPE_EPID_HASH_START 14
130 #define SIPE_EPID_HASH_END 20
131 #define SIPE_EPID_LENGTH (2 * (SIPE_EPID_HASH_END - SIPE_EPID_HASH_START + 1))
133 int i,j;
134 PurpleCipherContext *ctx;
135 unsigned char hash[SIPE_EPID_HASH_END];
136 char out[SIPE_EPID_LENGTH + 1];
137 char *buf = g_strdup_printf("%s:%s:%s", self_sip_uri, hostname, ip_address);
139 ctx = purple_cipher_context_new_by_name("sha1", NULL);
140 purple_cipher_context_append(ctx, (guchar *)buf, strlen(buf));
141 purple_cipher_context_digest(ctx, sizeof(hash), hash, NULL);
142 purple_cipher_context_destroy(ctx);
144 for (i = SIPE_EPID_HASH_START, j = 0;
145 i < SIPE_EPID_HASH_END;
146 i++, j += 2) {
147 g_sprintf(&out[j], "%02x", hash[i]);
149 out[SIPE_EPID_LENGTH] = 0;
151 g_free(buf);
152 return g_strdup(out);
156 Local Variables:
157 mode: c
158 c-file-style: "bsd"
159 indent-tabs-mode: t
160 tab-width: 8
161 End: