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 <errno.h>
34 #include <netinet/in.h>
35 #include <net/if.h>
36 #include <sys/ioctl.h>
37 #include <cstdio>
38 #include <cstring>
39 #include <iostream>
41 
42 std::string armarx::tools::getHardwareId()
43 {
44 #ifndef SIOCGIFADDR
45  return "00:00:00:00:00:00";
46 #endif
47 
48  std::string mac;
49  struct ifreq ifr;
50  struct ifconf ifc;
51  char buf[1024];
52  int success = 0;
53 
54  int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
55  if (sock == -1)
56  {
57  return "00:00:00:00:00:00";
58  }
60  {
61  close(sock);
62  };
63  ifc.ifc_len = sizeof(buf);
64  ifc.ifc_buf = buf;
65  if (ioctl(sock, SIOCGIFCONF, &ifc) == -1)
66  {
67  return "00:00:00:00:00:00";
68  }
69 
70  struct ifreq* it = ifc.ifc_req;
71  const struct ifreq* const end = it + (ifc.ifc_len / sizeof(struct ifreq));
72 
73  for (; it != end; ++it)
74  {
75  strcpy(ifr.ifr_name, it->ifr_name);
76  if (ioctl(sock, SIOCGIFFLAGS, &ifr) == 0)
77  {
78  if (!(ifr.ifr_flags & IFF_LOOPBACK)) // don't count loopback
79  {
80  if (ioctl(sock, SIOCGIFHWADDR, &ifr) == 0)
81  {
82  success = 1;
83  break;
84  }
85  }
86  }
87  else { /* handle error */ }
88  }
89 
90 
91 
92  if (success)
93  {
94  char temp[1024];
95  sprintf(temp, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
96  (unsigned char)ifr.ifr_hwaddr.sa_data[0],
97  (unsigned char)ifr.ifr_hwaddr.sa_data[1],
98  (unsigned char)ifr.ifr_hwaddr.sa_data[2],
99  (unsigned char)ifr.ifr_hwaddr.sa_data[3],
100  (unsigned char)ifr.ifr_hwaddr.sa_data[4],
101  (unsigned char)ifr.ifr_hwaddr.sa_data[5]);
102  mac = temp;
103  }
104  // std::cout << "MAC Address: " << mac << std::endl;;
105  return mac;
106 }
107 #endif /* __linux__ */
108 
109 // *******************************************************
110 // windows implementation
111 // *******************************************************
112 #ifdef __WINDOWS__
113 #include <winsock2.h>
114 #include <iphlpapi.h>
115 #include <Winerror.h>
116 
117 std::string armarx::tools::getHardwareId()
118 {
119 
120  std::string mac;
121  IP_ADAPTER_INFO AdapterInfo[128];
122  DWORD dwBufLen = sizeof(AdapterInfo);
123  DWORD dwStatus = GetAdaptersInfo(AdapterInfo, &dwBufLen);
124 
125  // network card not found
126  if (dwStatus != ERROR_SUCCESS)
127  {
128  return "00:00:00:00:00:00";
129  }
130 
131  PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo;
132  char szBuffer[512];
133 
134  while (pAdapterInfo)
135  {
136  if (pAdapterInfo->Type == MIB_IF_TYPE_ETHERNET)
137  {
138  sprintf_s(szBuffer, sizeof(szBuffer), "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
139  pAdapterInfo->Address[0],
140  pAdapterInfo->Address[1],
141  pAdapterInfo->Address[2],
142  pAdapterInfo->Address[3],
143  pAdapterInfo->Address[4],
144  pAdapterInfo->Address[5]);
145 
146  mac = szBuffer;
147  return mac;
148  }
149 
150  pAdapterInfo = pAdapterInfo->Next;
151  }
152 
153  return "00:00:00:00:00:00";
154 }
155 #endif /* __WINDOWS__ */
156 
157 // *******************************************************
158 // Apple implementation
159 // *******************************************************
160 #ifdef __APPLE__
161 
162 std::string armarx::tools::getHardwareId()
163 {
164  std::string mac = "00:00:00:00:00:00";
165  return mac;
166 }
167 #endif /* __APPLE__ */
OnScopeExit.h
ARMARX_ON_SCOPE_EXIT
#define ARMARX_ON_SCOPE_EXIT
Executes given code when the enclosing scope is left.
Definition: OnScopeExit.h:112
armarx::tools::getHardwareId
std::string getHardwareId()
Retrieve hardware id to identify local machine.
HardwareId.h
armarx::status::success
@ success