Cleaned some warnings in the code
[siplcs.git] / src / uuid.c
blob56143077bb9c46306ee4de5da8a170996a592b1a
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>
26 #ifndef _WIN32
27 #include <sys/ioctl.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <linux/if.h>
32 #else
33 #ifdef _DLL
34 #define _WS2TCPIP_H_
35 #define _WINSOCK2API_
36 #define _LIBC_INTERNAL_
37 #endif /* _DLL */
38 #include "internal.h"
39 #include <iphlpapi.h>
40 #endif /* _WIN32 */
42 #include <cipher.h>
43 #include <glib.h>
44 #include "uuid.h"
46 static const char *epid_ns_uuid = "fcacfb03-8a73-46ef-91b1-e5ebeeaba4fe";
48 #define UUID_OFFSET_TO_LAST_SEGMENT 24
50 void readUUID(const char *string, sipe_uuid_t *uuid)
52 int i;
53 sscanf(string, "%08x-%04hx-%04hx-%02hhx%02hhx-", &uuid->time_low
54 , &uuid->time_mid, &uuid->time_hi_and_version
55 , &uuid->clock_seq_hi_and_reserved
56 , &uuid->clock_seq_low );
58 for(i=0;i<6;i++)
60 sscanf(&string[UUID_OFFSET_TO_LAST_SEGMENT+i*2], "%02hhx", &uuid->node[i]);
64 void printUUID(sipe_uuid_t *uuid, char *string)
66 int i;
67 size_t pos;
68 sprintf(string, "%08x-%04x-%04x-%02x%02x-", uuid->time_low
69 , uuid->time_mid, uuid->time_hi_and_version
70 , uuid->clock_seq_hi_and_reserved
71 , uuid->clock_seq_low
73 pos = strlen(string);
74 for(i=0;i<6;i++)
76 pos += sprintf(&string[pos], "%02x", uuid->node[i]);
80 void createUUIDfromHash(sipe_uuid_t *uuid, const unsigned char *hash)
82 memcpy(uuid, hash, sizeof(sipe_uuid_t));
83 uuid->time_hi_and_version &= 0x0FFF;
84 uuid->time_hi_and_version |= 0x5000;
85 uuid->clock_seq_hi_and_reserved &= 0x3F;
86 uuid->clock_seq_hi_and_reserved |= 0x80;
89 char *generateUUIDfromEPID(const gchar *epid)
91 sipe_uuid_t result;
92 PurpleCipherContext *ctx;
93 unsigned char hash[20];
94 char buf[512];
96 readUUID(epid_ns_uuid, &result);
97 memcpy(buf, &result, sizeof(sipe_uuid_t));
98 sprintf(&buf[sizeof(sipe_uuid_t)], epid);
100 ctx = purple_cipher_context_new_by_name("sha1", NULL);
101 purple_cipher_context_append(ctx, buf, strlen(buf));
102 purple_cipher_context_digest(ctx, sizeof(hash), hash, NULL);
103 purple_cipher_context_destroy(ctx);
105 createUUIDfromHash(&result, hash);
106 printUUID(&result, buf);
107 return g_strdup(buf);
110 #ifndef _WIN32
111 long mac_addr_sys (const char *addr)
113 /* implementation for Linux */
114 struct ifreq ifr;
115 struct ifreq *IFR;
116 struct ifconf ifc;
117 char buf[1024];
118 int s, i;
119 int ok = 0;
121 s = socket(AF_INET, SOCK_DGRAM, 0);
122 if (s==-1) {
123 return -1;
126 ifc.ifc_len = sizeof(buf);
127 ifc.ifc_buf = buf;
128 ioctl(s, SIOCGIFCONF, &ifc);
130 IFR = ifc.ifc_req;
131 for (i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; IFR++) {
133 strcpy(ifr.ifr_name, IFR->ifr_name);
134 if (ioctl(s, SIOCGIFFLAGS, &ifr) == 0) {
135 if (! (ifr.ifr_flags & IFF_LOOPBACK)) {
136 if (ioctl(s, SIOCGIFHWADDR, &ifr) == 0) {
137 ok = 1;
138 break;
144 close(s);
145 if (ok) {
146 memmove((void *)addr, ifr.ifr_hwaddr.sa_data, 6);
148 else {
149 return -1;
151 return 0;
154 #else
156 static char *get_mac_address_win(const char *ip_address)
158 IP_ADAPTER_INFO AdapterInfo[16]; // for up to 16 NICs
159 DWORD ulOutBufLen = sizeof(AdapterInfo);
160 PIP_ADAPTER_INFO pAdapter_res = NULL;
161 PIP_ADAPTER_INFO pAdapter = NULL;
162 DWORD dwRetVal = 0;
163 UINT i;
164 char *res = NULL;
166 if ((dwRetVal = GetAdaptersInfo(AdapterInfo, &ulOutBufLen)) == NO_ERROR) {
167 pAdapter = AdapterInfo;
168 pAdapter_res = pAdapter;
169 while (pAdapter) {
170 if (!g_ascii_strcasecmp(pAdapter->IpAddressList.IpAddress.String, ip_address)) {
171 pAdapter_res = pAdapter;
172 break;
174 pAdapter = pAdapter->Next;
176 } else {
177 printf("GetAdaptersInfo failed with error: %d\n", dwRetVal);
180 if (pAdapter_res) {
181 gchar nmac[13];
182 for (i = 0; i < pAdapter_res->AddressLength; i++) {
183 g_sprintf(&nmac[(i*2)], "%02X", (int)pAdapter_res->Address[i]);
185 printf("NIC: %s, IP Address: %s, MAC Addres: %s\n", pAdapter_res->Description, pAdapter_res->IpAddressList.IpAddress.String, nmac);
186 res = g_strdup(nmac);
187 } else {
188 res = g_strdup("01010101");
191 //@TODO free AdapterInfo
192 return res;
194 #endif /* _WIN32 */
196 gchar * sipe_uuid_get_macaddr(const char *ip_address)
198 #ifndef _WIN32
199 guchar addr[6];
200 long mac_add = mac_addr_sys(addr);
201 gchar nmac[13];
203 if (mac_add == 0){
204 int i,j;
205 for (i = 0,j=0; i < 6; i++,j+=2) {
206 g_sprintf(&nmac[j], "%02X", addr[i]);
208 return g_strdup(nmac);
210 return g_strdup_printf("01010101"); //Default
211 #else
212 return get_mac_address_win(ip_address);
213 #endif /* _WIN32 */