fixed printUUID() for x86_64 systems
[siplcs.git] / src / uuid.c
blob130a2668b8dd10c289c6a358d737b3cd839b3a85
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, 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(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(uuid_t *uuid, const unsigned char *hash)
71 memcpy(uuid, hash, sizeof(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 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(uuid_t));
87 printf("have epid == NULL? %i\n", epid == NULL);
88 printf("have epid: '%s'\n", epid);
89 printf("have sizeof uuid_t: %d\n", sizeof(uuid_t));
90 sprintf(&buf[sizeof(uuid_t)], epid);
92 ctx = purple_cipher_context_new_by_name("sha1", NULL);
93 purple_cipher_context_append(ctx, buf, strlen(buf));
94 purple_cipher_context_digest(ctx, sizeof(hash), hash, NULL);
95 purple_cipher_context_destroy(ctx);
97 createUUIDfromHash(&result, hash);
98 printUUID(&result, buf);
99 return g_strdup(buf);
102 long mac_addr_sys (const char *addr)
104 /* implementation for Linux */
105 struct ifreq ifr;
106 struct ifreq *IFR;
107 struct ifconf ifc;
108 char buf[1024];
109 int s, i;
110 int ok = 0;
112 s = socket(AF_INET, SOCK_DGRAM, 0);
113 if (s==-1) {
114 return -1;
117 ifc.ifc_len = sizeof(buf);
118 ifc.ifc_buf = buf;
119 ioctl(s, SIOCGIFCONF, &ifc);
121 IFR = ifc.ifc_req;
122 for (i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; IFR++) {
124 strcpy(ifr.ifr_name, IFR->ifr_name);
125 if (ioctl(s, SIOCGIFFLAGS, &ifr) == 0) {
126 if (! (ifr.ifr_flags & IFF_LOOPBACK)) {
127 if (ioctl(s, SIOCGIFHWADDR, &ifr) == 0) {
128 ok = 1;
129 break;
135 close(s);
136 if (ok) {
137 bcopy( ifr.ifr_hwaddr.sa_data, addr, 6);
139 else {
140 return -1;
142 return 0;
145 gchar * sipe_uuid_get_macaddr()
147 guchar addr[6];
148 long mac_add = mac_addr_sys(addr);
149 gchar nmac[13];
151 if (mac_add == 0){
152 int i,j;
153 for (i = 0,j=0; i < 6; i++,j+=2) {
154 g_sprintf(&nmac[j], "%02X", addr[i]);
156 printf("Returning mac: '%s'\n", nmac);
157 return g_strdup(nmac);
159 printf("Returning default mac\n");
160 return g_strdup_printf("01010101"); //Default