HardwareId.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package VisionX::Tools
19  * @author Kai Welke (kai dot welke at kit dot edu)
20  * @date 2011
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #include "HardwareId.h"
26 
27 
28 // *******************************************************
29 // linux implementation
30 // *******************************************************
31 #ifdef __linux__
32 #include <unistd.h>
33 #include <cerrno>
34 #include <netinet/in.h>
35 #include <net/if.h>
36 #include <sys/ioctl.h>
37 #include <cstdio>
38 #include <cstring>
39 
40 namespace visionx
41 {
42  std::string tools::getHardwareId()
43  {
44 #ifndef SIOCGIFADDR
45  return "00:00:00:00:00:00";
46 #endif
47 
48  int fd;
49  struct ifreq ifr;
50  std::string mac;
51 
52  // create socket
53  fd = socket(AF_INET, SOCK_DGRAM, 0);
54  ifr.ifr_addr.sa_family = AF_INET;
55  strncpy(ifr.ifr_name, "eth0", IFNAMSIZ - 1);
56 
57  // retrieve hardware address
58  ioctl(fd, SIOCGIFHWADDR, &ifr);
59 
60  int nError = errno;
61 
62  if (nError == 19)
63  {
64  printf("No ethernet adapter found. Using dummy MAC\n");
65  mac = "00:00:00:00:00:00";
66  }
67  else
68  {
69  char temp[1024];
70  sprintf(temp, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
71  (unsigned char)ifr.ifr_hwaddr.sa_data[0],
72  (unsigned char)ifr.ifr_hwaddr.sa_data[1],
73  (unsigned char)ifr.ifr_hwaddr.sa_data[2],
74  (unsigned char)ifr.ifr_hwaddr.sa_data[3],
75  (unsigned char)ifr.ifr_hwaddr.sa_data[4],
76  (unsigned char)ifr.ifr_hwaddr.sa_data[5]);
77 
78  mac = temp;
79  }
80 
81  // close socket
82  close(fd);
83 
84  return mac;
85  }
86 #endif /* __linux__ */
87 
88  // *******************************************************
89  // windows implementation
90  // *******************************************************
91 #ifdef __WINDOWS__
92 #include <winsock2.h>
93 #include <iphlpapi.h>
94 #include <Winerror.h>
95 
96  std::string tools::getHardwareId()
97  {
98 
99  std::string mac;
100  IP_ADAPTER_INFO AdapterInfo[128];
101  DWORD dwBufLen = sizeof(AdapterInfo);
102  DWORD dwStatus = GetAdaptersInfo(AdapterInfo, &dwBufLen);
103 
104  // network card not found
105  if (dwStatus != ERROR_SUCCESS)
106  {
107  return "00:00:00:00:00:00";
108  }
109 
110  PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo;
111  char szBuffer[512];
112 
113  while (pAdapterInfo)
114  {
115  if (pAdapterInfo->Type == MIB_IF_TYPE_ETHERNET)
116  {
117  sprintf_s(szBuffer, sizeof(szBuffer), "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
118  pAdapterInfo->Address[0],
119  pAdapterInfo->Address[1],
120  pAdapterInfo->Address[2],
121  pAdapterInfo->Address[3],
122  pAdapterInfo->Address[4],
123  pAdapterInfo->Address[5]);
124 
125  mac = szBuffer;
126  return mac;
127  }
128 
129  pAdapterInfo = pAdapterInfo->Next;
130  }
131 
132  return "00:00:00:00:00:00";
133  }
134 #endif /* __WINDOWS__ */
135 
136 }
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
HardwareId.h
visionx::tools::getHardwareId
std::string getHardwareId()
Retrieve hardware id to identify local machine.