Fixed little details about the mingw support; updated the Changelog file
[siplcs.git] / src / uuid.c
blobf6083e2203a15e17979611969e17bd3672769ac5
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
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
22 #include <fcntl.h>
23 #include <unistd.h>
25 #include <sys/ioctl.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <linux/if.h>
31 #include <purple.h>
32 #include <glib.h>
33 #include "uuid.h"
35 static const char *epid_ns_uuid = "fcacfb03-8a73-46ef-91b1-e5ebeeaba4fe";
37 #define UUID_OFFSET_TO_LAST_SEGMENT 24
39 void readUUID(const char *string, sipe_uuid_t *uuid)
41 int i;
42 sscanf(string, "%08x-%04hx-%04hx-%02hhx%02hhx-", &uuid->time_low
43 , &uuid->time_mid, &uuid->time_hi_and_version
44 , &uuid->clock_seq_hi_and_reserved
45 , &uuid->clock_seq_low );
47 for(i=0;i<6;i++)
49 sscanf(&string[UUID_OFFSET_TO_LAST_SEGMENT+i*2], "%02hhx", &uuid->node[i]);
53 void printUUID(sipe_uuid_t *uuid, char *string)
55 int i;
56 size_t pos;
57 sprintf(string, "%08x-%04x-%04x-%02x%02x-", uuid->time_low
58 , uuid->time_mid, uuid->time_hi_and_version
59 , uuid->clock_seq_hi_and_reserved
60 , uuid->clock_seq_low
62 pos = strlen(string);
63 for(i=0;i<6;i++)
65 pos += sprintf(&string[pos], "%02x", uuid->node[i]);
69 void createUUIDfromHash(sipe_uuid_t *uuid, const unsigned char *hash)
71 memcpy(uuid, hash, sizeof(sipe_uuid_t));
72 uuid->time_hi_and_version &= 0x0FFF;
73 uuid->time_hi_and_version |= 0x5000;
74 uuid->clock_seq_hi_and_reserved &= 0x3F;
75 uuid->clock_seq_hi_and_reserved |= 0x80;
78 char *generateUUIDfromEPID(const gchar *epid)
80 sipe_uuid_t result;
81 PurpleCipherContext *ctx;
82 unsigned char hash[20];
83 char buf[512];
85 readUUID(epid_ns_uuid, &result);
86 memcpy(buf, &result, sizeof(sipe_uuid_t));
87 sprintf(&buf[sizeof(sipe_uuid_t)], epid);
89 ctx = purple_cipher_context_new_by_name("sha1", NULL);
90 purple_cipher_context_append(ctx, buf, strlen(buf));
91 purple_cipher_context_digest(ctx, sizeof(hash), hash, NULL);
92 purple_cipher_context_destroy(ctx);
94 createUUIDfromHash(&result, hash);
95 printUUID(&result, buf);
96 return g_strdup(buf);
99 long mac_addr_sys (const char *addr)
101 /* implementation for Linux */
102 struct ifreq ifr;
103 struct ifreq *IFR;
104 struct ifconf ifc;
105 char buf[1024];
106 int s, i;
107 int ok = 0;
109 s = socket(AF_INET, SOCK_DGRAM, 0);
110 if (s==-1) {
111 return -1;
114 ifc.ifc_len = sizeof(buf);
115 ifc.ifc_buf = buf;
116 ioctl(s, SIOCGIFCONF, &ifc);
118 IFR = ifc.ifc_req;
119 for (i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; IFR++) {
121 strcpy(ifr.ifr_name, IFR->ifr_name);
122 if (ioctl(s, SIOCGIFFLAGS, &ifr) == 0) {
123 if (! (ifr.ifr_flags & IFF_LOOPBACK)) {
124 if (ioctl(s, SIOCGIFHWADDR, &ifr) == 0) {
125 ok = 1;
126 break;
132 close(s);
133 if (ok) {
134 memmove( addr, ifr.ifr_hwaddr.sa_data, 6);
136 else {
137 return -1;
139 return 0;
142 gchar * sipe_uuid_get_macaddr()
144 guchar addr[6];
145 long mac_add = mac_addr_sys(addr);
146 gchar nmac[13];
148 if (mac_add == 0){
149 int i,j;
150 for (i = 0,j=0; i < 6; i++,j+=2) {
151 g_sprintf(&nmac[j], "%02X", addr[i]);
153 return g_strdup(nmac);
155 return g_strdup_printf("01010101"); //Default