-
Notifications
You must be signed in to change notification settings - Fork 34
/
licgen.c
333 lines (289 loc) · 8.26 KB
/
licgen.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include "licgen.h"
uint64_t hash(unsigned char* Astr)
{
uint64_t hash = 5381;
int c;
while ((c = *Astr++))
hash = ((hash << 5) + hash) + c;
return hash;
}
#if defined _WIN32
#include <winsock2.h>
#include <iphlpapi.h>
#pragma comment(lib, "IPHLPAPI.lib")
#define WORKING_BUFFER_SIZE 15000
#define MAX_TRIES 3
#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
#define MAGIC "CA268A5829BB427EBBEC10F397298DB8"
#define snprintf _snprintf
typedef struct{
char name[48]; //adapter name
char mac[16]; //adapter mac address
}win_mac_t;
int enum_nics(win_mac_t *Anic, int Anum)
{
/* Declare and initialize variables */
DWORD dwRetVal = 0;
unsigned int i = 0;
int count = 0;
int len = 0;
char bit[3] = {0};
char *name;
char *pos;
// Set the flags to pass to GetAdaptersAddresses
ULONG flags = GAA_FLAG_INCLUDE_PREFIX;
// default to unspecified address family (both)
ULONG family = AF_INET;
LPVOID lpMsgBuf = NULL;
PIP_ADAPTER_ADDRESSES pAddresses = NULL;
PIP_ADAPTER_ADDRESSES pCurrAddresses = NULL;
ULONG outBufLen = 0;
ULONG Iterations = 0;
// Allocate a 15 KB buffer to start with.
outBufLen = WORKING_BUFFER_SIZE;
do {
pAddresses = (IP_ADAPTER_ADDRESSES *) MALLOC(outBufLen);
if (pAddresses == NULL) {
return -1;
}
dwRetVal = GetAdaptersAddresses(family, flags, NULL, pAddresses, &outBufLen);
if (dwRetVal == ERROR_BUFFER_OVERFLOW) {
FREE(pAddresses);
pAddresses = NULL;
} else {
break;
}
Iterations++;
} while ((dwRetVal == ERROR_BUFFER_OVERFLOW) && (Iterations < MAX_TRIES));
if (dwRetVal == NO_ERROR) {
// If successful, output some information from the data we received
pCurrAddresses = pAddresses;
while (pCurrAddresses && count < Anum) {
if (pCurrAddresses->IfType != IF_TYPE_SOFTWARE_LOOPBACK &&
pCurrAddresses->PhysicalAddressLength != 0)
{
pos = Anic[count].mac;
//get adapter name
strcpy(Anic[count].name, MAGIC); //avoid adapter name is empty
name = pCurrAddresses->AdapterName;
len = 0;
for (i = 0; i<strlen(name); ++i)
{
if (isalnum(name[i]))
Anic[count].name[len++] = name[i];
}
for (i = 0; i < (int) pCurrAddresses->PhysicalAddressLength; i++)
{
sprintf(bit, "%.2X", (int) pCurrAddresses->PhysicalAddress[i]);
strcpy(pos, bit);
pos += 2;
}
count++;
}
pCurrAddresses = pCurrAddresses->Next;
}
} else {
printf("Call to GetAdaptersAddresses failed with error: %d\n", dwRetVal);
if (dwRetVal == ERROR_NO_DATA)
printf("\tNo addresses were found for the requested parameters\n");
else {
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, dwRetVal, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
// Default language
(LPTSTR) & lpMsgBuf, 0, NULL)) {
printf("\tError: %s", lpMsgBuf);
LocalFree(lpMsgBuf);
if (pAddresses)
FREE(pAddresses);
return -1;
}
}
}
if (pAddresses) {
FREE(pAddresses);
}
return count;
}
void get_mac_sn(unsigned char Asn[17])
{
unsigned char *sn[] = {Asn};
get_all_mac_sn(sn, 1);
}
int get_all_mac_sn(unsigned char **Asn, int Anum)
{
int max = 8;
win_mac_t wmts[8] = {0};
unsigned char mb[64];
int i, j, num;
num = enum_nics(wmts, max);
for (i = 0; i<Anum && i<num; ++i)
{
memset(mb, 0, sizeof(mb));
for (j = 0; j<16; ++j)
{
if (j % 2 == 0 && j < 12)
{
Asn[i][j] = wmts[i].mac[j];
}
else
{
Asn[i][j] = wmts[i].name[j];
}
}
}
return Anum;
}
#else /* linux */
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <linux/if.h>
#include <linux/if_ether.h>
#include <linux/if_arp.h>
#include <linux/sockios.h>
#include <ifaddrs.h>
int enum_nics(char *Anic[], int Anum)
{
struct ifaddrs *ifp, *ibuf = NULL;
if (getifaddrs (&ibuf) < 0 || ibuf == NULL)
return 0;
int num = 0;
for (ifp = ibuf; ifp; ifp = ifp->ifa_next)
{
if (ifp->ifa_addr &&
(AF_PACKET == ifp->ifa_addr->sa_family) &&
strcmp(ifp->ifa_name, "lo") != 0 &&
num < Anum)
{
strcpy(Anic[num++], ifp->ifa_name);
}
}
freeifaddrs(ibuf);
return num;
}
int get_nic_mac(char Amac[13], const char* Anic)
{
struct ifreq ifreq;
int sfd;
if ((sfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
return -1;
}
strcpy(ifreq.ifr_name, Anic);
if (ioctl(sfd, SIOCGIFHWADDR, &ifreq) < 0)
{
close(sfd);
return -1;
}
snprintf(Amac, 13, "%X%X%X%X%X%X",
(unsigned char)ifreq.ifr_hwaddr.sa_data[0],
(unsigned char)ifreq.ifr_hwaddr.sa_data[1],
(unsigned char)ifreq.ifr_hwaddr.sa_data[2],
(unsigned char)ifreq.ifr_hwaddr.sa_data[3],
(unsigned char)ifreq.ifr_hwaddr.sa_data[4],
(unsigned char)ifreq.ifr_hwaddr.sa_data[5]);
close(sfd);
return 0;
}
#define BOARD_VENDOR "/sys/class/dmi/id/board_vendor"
#define BOARD_NAME "/sys/class/dmi/id/board_name"
#define BOARD_VERSION "/sys/class/dmi/id/board_version"
void get_board_info(char Ainfo[96])
{
char *file[64] = {BOARD_VENDOR, BOARD_NAME, BOARD_VERSION};
char buf[32] = {0};
char *str = NULL;
int i = 0;
int len = 0;
for (; i<3 && len < 96; ++i)
{
FILE *fp = fopen(file[i], "r");
if (!fp) continue;
str = fgets(buf, 32, fp);
if (str != NULL)
{
if (str[strlen(str) - 1] == '\n')
str[strlen(str) - 1] = '\0';
strncpy(Ainfo, buf, strlen(str));
Ainfo += strlen(str);
len += strlen(str);
}
fclose(fp);
}
}
void get_mac_sn(unsigned char Asn[17])
{
char nic[16] = {0};
char mac[13] = {0};
char board[96] = {0};
unsigned char mb[128] = {0};
char *nics[1] = {nic};
uint64_t hs;
enum_nics(nics, 1);
get_nic_mac(mac, nic);
get_board_info(board);
strncpy((char*)mb, board, strlen(board));
strncpy((char*)(mb+strlen(board)), mac, strlen(mac));
hs = hash(mb);
snprintf((char*)Asn, 17, "%016lX", hs);
}
int get_all_mac_sn(unsigned char **Asn, int Anum)
{
int max = 8;
char *pnic = (char*)calloc(max, sizeof(unsigned char)*16);
char *pmac = (char*)calloc(max, sizeof(unsigned char)*13);
char *nics[8];
char *macs[8];
char *pos1 = pnic;
char *pos2 = pmac;
char board[96] = {0};
unsigned char mb[128];
uint64_t hs;
int i;
for (i = 0; i<max; ++i)
{
nics[i] = pos1;
macs[i] = pos2;
pos1 += sizeof(unsigned char)*16;
pos2 += sizeof(unsigned char)*13;
}
get_board_info(board);
Anum = enum_nics(nics, Anum);
for (i = 0; i<Anum; ++i)
{
get_nic_mac(macs[i], nics[i]);
memset(mb, 0, sizeof(mb));
strncpy((char*)mb, board, strlen(board));
strncpy((char*)(mb+strlen(board)), macs[i], strlen(macs[i]));
hs = hash(mb);
snprintf((char*)Asn[i], 17, "%016lX", hs);
}
free(pnic);
free(pmac);
return Anum;
}
#endif
/*int main(int argc, const char *argv[])
{
int max = 8;
int i, num;
unsigned char *p = (unsigned char*)calloc(max, sizeof(unsigned char)*17);
unsigned char *sn[8];
unsigned char *pos = p;
for (i = 0; i<max; ++i)
{
sn[i] = pos;
pos += sizeof(unsigned char)*17;
}
num = get_all_mac_sn(sn, max);
for (i = 0; i<num; ++i)
{
printf("SN=%s len=%d\n", (char*)sn[i], (int)strlen((char*)sn[i]));
}
free(p);
return 0;
}*/