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
27 #include <sys/ioctl.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
36 #define _LIBC_INTERNAL_
44 #include <glib/gprintf.h>
47 static const char *epid_ns_uuid
= "fcacfb03-8a73-46ef-91b1-e5ebeeaba4fe";
49 #define UUID_OFFSET_TO_LAST_SEGMENT 24
51 void readUUID(const char *string
, sipe_uuid_t
*uuid
)
54 sscanf(string
, "%08x-%04hx-%04hx-%02hhx%02hhx-", &uuid
->time_low
55 , &uuid
->time_mid
, &uuid
->time_hi_and_version
56 , &uuid
->clock_seq_hi_and_reserved
57 , &uuid
->clock_seq_low
);
61 sscanf(&string
[UUID_OFFSET_TO_LAST_SEGMENT
+i
*2], "%02hhx", &uuid
->node
[i
]);
65 void printUUID(sipe_uuid_t
*uuid
, char *string
)
69 sprintf(string
, "%08x-%04x-%04x-%02x%02x-", uuid
->time_low
70 , uuid
->time_mid
, uuid
->time_hi_and_version
71 , uuid
->clock_seq_hi_and_reserved
77 pos
+= sprintf(&string
[pos
], "%02x", uuid
->node
[i
]);
81 void createUUIDfromHash(sipe_uuid_t
*uuid
, const unsigned char *hash
)
83 memcpy(uuid
, hash
, sizeof(sipe_uuid_t
));
84 uuid
->time_hi_and_version
&= 0x0FFF;
85 uuid
->time_hi_and_version
|= 0x5000;
86 uuid
->clock_seq_hi_and_reserved
&= 0x3F;
87 uuid
->clock_seq_hi_and_reserved
|= 0x80;
90 char *generateUUIDfromEPID(const gchar
*epid
)
93 PurpleCipherContext
*ctx
;
94 unsigned char hash
[20];
97 readUUID(epid_ns_uuid
, &result
);
98 memcpy(buf
, &result
, sizeof(sipe_uuid_t
));
99 strcpy(&buf
[sizeof(sipe_uuid_t
)], epid
);
101 ctx
= purple_cipher_context_new_by_name("sha1", NULL
);
102 purple_cipher_context_append(ctx
, (guchar
*) buf
, strlen(buf
));
103 purple_cipher_context_digest(ctx
, sizeof(hash
), hash
, NULL
);
104 purple_cipher_context_destroy(ctx
);
106 createUUIDfromHash(&result
, hash
);
107 printUUID(&result
, buf
);
108 return g_strdup(buf
);
112 long mac_addr_sys (const unsigned char *addr
)
114 /* implementation for Linux */
122 s
= socket(AF_INET
, SOCK_DGRAM
, 0);
127 ifc
.ifc_len
= sizeof(buf
);
129 ioctl(s
, SIOCGIFCONF
, &ifc
);
132 for (i
= ifc
.ifc_len
/ sizeof(struct ifreq
); --i
>= 0; IFR
++) {
134 strcpy(ifr
.ifr_name
, IFR
->ifr_name
);
135 if (ioctl(s
, SIOCGIFFLAGS
, &ifr
) == 0) {
136 if (! (ifr
.ifr_flags
& IFF_LOOPBACK
)) {
138 if (ioctl(s
, SIOCGIFMAC
, &ifr
) == 0) {
140 if (ioctl(s
, SIOCGIFHWADDR
, &ifr
) == 0) {
151 memmove((void *)addr
, ifr
.ifr_ifru
.ifru_addr
.sa_data
, 6);
161 static char *get_mac_address_win(const char *ip_address
)
163 IP_ADAPTER_INFO AdapterInfo
[16]; // for up to 16 NICs
164 DWORD ulOutBufLen
= sizeof(AdapterInfo
);
165 PIP_ADAPTER_INFO pAdapter_res
= NULL
;
166 PIP_ADAPTER_INFO pAdapter
= NULL
;
171 if ((dwRetVal
= GetAdaptersInfo(AdapterInfo
, &ulOutBufLen
)) == NO_ERROR
) {
172 pAdapter
= AdapterInfo
;
173 pAdapter_res
= pAdapter
;
175 if (!g_ascii_strcasecmp(pAdapter
->IpAddressList
.IpAddress
.String
, ip_address
)) {
176 pAdapter_res
= pAdapter
;
179 pAdapter
= pAdapter
->Next
;
182 printf("GetAdaptersInfo failed with error: %d\n", dwRetVal
);
185 if (pAdapter_res
&& pAdapter_res
->AddressLength
) {
187 for (i
= 0; i
< pAdapter_res
->AddressLength
; i
++) {
188 g_sprintf(&nmac
[(i
*2)], "%02X", (int)pAdapter_res
->Address
[i
]);
190 res
= g_strndup(nmac
, pAdapter_res
->AddressLength
* 2);
191 printf("NIC: %s, IP Address: %s, MAC Address: %s\n", pAdapter_res
->Description
, pAdapter_res
->IpAddressList
.IpAddress
.String
, res
);
193 res
= g_strdup("01010101");
196 //@TODO free AdapterInfo
201 gchar
* sipe_uuid_get_macaddr(const char *ip_address
)
205 long mac_add
= mac_addr_sys(addr
);
210 for (i
= 0,j
=0; i
< 6; i
++,j
+=2) {
211 g_sprintf(&nmac
[j
], "%02X", addr
[i
]);
213 return g_strdup(nmac
);
215 return g_strdup("01010101"); //Default
217 return get_mac_address_win(ip_address
);