open-license-manager
2014-08-04 8af6b12ee8716d2004d13bbfc81281953975b466
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
//http://stackoverflow.com/questions/16858782/how-to-obtain-almost-unique-system-identifier-in-a-cross-platform-way
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/resource.h>
#include <sys/utsname.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <assert.h>
#include <stdbool.h>
 
#ifdef DARWIN
#include <net/if_dl.h>
#include <ifaddrs.h>
#include <net/if_types.h>
#else //!DARWIN
#include <linux/if.h>
#include <linux/sockios.h>
#endif //!DARWIN
 
const char* getMachineName() {
    static struct utsname u;
 
    if (uname(&u) < 0) {
        assert(0);
        return "unknown";
    }
 
    return u.nodename;
}
 
//---------------------------------get MAC addresses ------------------------------------unsigned short-unsigned short----------
// we just need this for purposes of unique machine id. So any one or two mac's is fine.
unsigned short hashMacAddress(unsigned char* mac) {
    unsigned short hash = 0;
 
    for (unsigned int i = 0; i < 6; i++) {
        hash += (mac[i] << ((i & 1) * 8));
    }
    return hash;
}
 
void getMacHash(unsigned short *mac1, unsigned short *mac2) {
    *mac1 = 0;
    *mac2 = 0;
 
#ifdef DARWIN
 
    struct ifaddrs* ifaphead;
    if ( getifaddrs( &ifaphead ) != 0 )
    return;
 
    // iterate over the net interfaces
    bool foundMac1 = false;
    struct ifaddrs* ifap;
    for ( ifap = ifaphead; ifap; ifap = ifap->ifa_next )
    {
        struct sockaddr_dl* sdl = (struct sockaddr_dl*)ifap->ifa_addr;
        if ( sdl && ( sdl->sdl_family == AF_LINK ) && ( sdl->sdl_type == IFT_ETHER ))
        {
            if ( !foundMac1 )
            {
                foundMac1 = true;
                mac1 = hashMacAddress( (unsigned char*)(LLADDR(sdl))); //sdl->sdl_data) + sdl->sdl_nlen) );
            } else {
                mac2 = hashMacAddress( (unsigned char*)(LLADDR(sdl))); //sdl->sdl_data) + sdl->sdl_nlen) );
                break;
            }
        }
    }
 
    freeifaddrs( ifaphead );
 
#else // !DARWIN
 
    int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
    if (sock < 0)
        return;
 
    // enumerate all IP addresses of the system
    struct ifconf conf;
    char ifconfbuf[128 * sizeof(struct ifreq)];
    memset(ifconfbuf, 0, sizeof(ifconfbuf));
    conf.ifc_buf = ifconfbuf;
    conf.ifc_len = sizeof(ifconfbuf);
    if (ioctl(sock, SIOCGIFCONF, &conf)) {
        assert(0);
        return;
    }
 
    // get MAC address
    bool foundMac1 = false;
    struct ifreq* ifr;
    for (ifr = conf.ifc_req; (char*) ifr < (char*) conf.ifc_req + conf.ifc_len;
            ifr++) {
        if (ifr->ifr_addr.sa_data == (ifr + 1)->ifr_addr.sa_data)
            continue;  // duplicate, skip it
 
        if (ioctl(sock, SIOCGIFFLAGS, ifr))
            continue;  // failed to get flags, skip it
        if (ioctl(sock, SIOCGIFHWADDR, ifr) == 0) {
            if (!foundMac1) {
                foundMac1 = true;
                *mac1 = hashMacAddress(
                        (unsigned char*) &(ifr->ifr_addr.sa_data));
            } else {
                *mac2 = hashMacAddress(
                        (unsigned char*) &(ifr->ifr_addr.sa_data));
                break;
            }
        }
    }
 
    close(sock);
 
#endif // !DARWIN
 
    // sort the mac addresses. We don't want to invalidate
    // both macs if they just change order.
    if (*mac1 > *mac2) {
        unsigned short tmp = *mac2;
        *mac2 = *mac1;
        *mac1 = tmp;
    }
}
 
unsigned short getVolumeHash() {
    // we don't have a 'volume serial number' like on windows. Lets hash the system name instead.
    unsigned char* sysname = (unsigned char*) getMachineName();
    unsigned short hash = 0;
 
    for (unsigned int i = 0; sysname[i]; i++)
        hash += (sysname[i] << ((i & 1) * 8));
 
    return hash;
}
 
#ifdef DARWIN
#include <mach-o/arch.h>
unsigned short getCpuHash()
{
    const NXArchInfo* info = NXGetLocalArchInfo();
    unsigned short val = 0;
    val += (unsigned short)info->cputype;
    val += (unsigned short)info->cpusubtype;
    return val;
}
 
#else // !DARWIN
 
static void getCpuid(unsigned int* p, unsigned int ax) {
    __asm __volatile
    ( "movl %%ebx, %%esi\n\t"
            "cpuid\n\t"
            "xchgl %%ebx, %%esi"
            : "=a" (p[0]), "=S" (p[1]),
            "=c" (p[2]), "=d" (p[3])
            : "0" (ax)
    );
}
 
unsigned short getCpuHash() {
    unsigned int cpuinfo[4] = { 0, 0, 0, 0 };
    getCpuid(cpuinfo, 0);
    unsigned short hash = 0;
    unsigned int* ptr = (&cpuinfo[0]);
    for (unsigned int i = 0; i < 4; i++)
        hash += (ptr[i] & 0xFFFF) + (ptr[i] >> 16);
 
    return hash;
}
#endif // !DARWIN
 
int main() {
 
    printf("Machine: %s\n", getMachineName());
    printf("CPU: %d\n", getCpuHash());
    printf("Volume: %d\n", getVolumeHash());
    return 0;
}