casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CrashReportHelper.h
Go to the documentation of this file.
1 #include <sys/socket.h>
2 #ifdef __APPLE__
3 #include <net/if_dl.h>
4 #define AF_LOWLEVEL AF_LINK
5 #else
6 #include <linux/if_packet.h>
7 #define AF_LOWLEVEL AF_PACKET
8 #endif
9 #include <sys/types.h>
10 #include <ifaddrs.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <net/if.h>
14 #include <stdio.h>
15 #include <vector>
16 #include <algorithm>
17 #include <numeric>
18 #include <set>
19 #include <string>
20 #include <sstream>
21 #include <iostream>
22 #include <iomanip>
23 
24 namespace casac {
25 
27 
28 
29  typedef std::vector<unsigned> tmac;
30 
31  public:
32  std::string getUniqueId () {
33  struct ifaddrs *ifap, *ifaptr;
34  unsigned char *ptr;
35  std::set<tmac> macs;
36 
37  if (getifaddrs(&ifap) == 0) {
38  for(ifaptr = ifap; ifaptr != NULL; ifaptr = (ifaptr)->ifa_next) {
39 
40  if ( ! (ifaptr->ifa_flags & IFF_LOOPBACK) &&
41  ! (ifaptr->ifa_flags & IFF_POINTOPOINT) ) { // don't count loopback
42  // awdl is an OSX specific interface and seems to get a new Mac address assigned at every reboot
43  // so we filter that out.
44  if (((ifaptr)->ifa_addr)->sa_family == AF_LOWLEVEL && strncmp("awdl",(ifaptr)->ifa_name,4)) {
45  #ifdef __APPLE__
46  ptr = (unsigned char *)LLADDR((struct sockaddr_dl *)(ifaptr)->ifa_addr);
47  #else
48  ptr = (unsigned char *)(((struct sockaddr_ll*)ifaptr->ifa_addr)->sll_addr);
49  #endif
50  macs.insert(tmac({*ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)}));
51  /*
52  printf( "%s: %02x:%02x:%02x:%02x:%02x:%02x\n",
53  (ifaptr)->ifa_name,
54  *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5) );
55  */
56  }
57  }
58  }
59  freeifaddrs(ifap);
60 
61  tmac comb = accumulate( macs.begin( ),macs.end( ),tmac({ 0,0,0,0,0,0 }),
62  []( tmac acc, tmac mac ) {
63  tmac result(acc.size());
64  std::transform(acc.begin(), acc.end(), mac.begin(), result.begin(), std::plus<int>());
65  return result;
66  } );
67  /*
68  printf( "--------------------------------------------------\n");
69  printf( "sum collected as a possible machine uid\n" );
70  printf( "--------------------------------------------------\n");
71  printf( "hash: %03x%03x%03x%03x%03x%03x\n",
72  comb[0],comb[1],comb[2],comb[3],comb[4],comb[5] );
73  */
74  std::stringstream sstream;
75  sstream << std::hex << comb[0] << comb[1] << comb[2] << comb[3] << comb[4] << comb[5];
76  std::string hash = sstream.str();
77  return hash;
78  } else {
79  return "NA";
80  }
81  }
82  };
83 }
#define AF_LOWLEVEL
std::vector< unsigned > tmac
transform(a.begin(), a.end(), std::ostream_iterator< int >(cout,"\n"), compose(unary(h), compose(unary(f), unary(f))))
Global Functions.