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 <cerrno>
33#include <cstdio>
34#include <cstring>
35
36#include <net/if.h>
37#include <netinet/in.h>
38#include <sys/ioctl.h>
39#include <unistd.h>
40
41namespace visionx
42{
43 std::string
45 {
46#ifndef SIOCGIFADDR
47 return "00:00:00:00:00:00";
48#endif
49
50 int fd;
51 struct ifreq ifr;
52 std::string mac;
53
54 // create socket
55 fd = socket(AF_INET, SOCK_DGRAM, 0);
56 ifr.ifr_addr.sa_family = AF_INET;
57 strncpy(ifr.ifr_name, "eth0", IFNAMSIZ - 1);
58
59 // retrieve hardware address
60 ioctl(fd, SIOCGIFHWADDR, &ifr);
61
62 int nError = errno;
63
64 if (nError == 19)
65 {
66 printf("No ethernet adapter found. Using dummy MAC\n");
67 mac = "00:00:00:00:00:00";
68 }
69 else
70 {
71 char temp[1024];
72 sprintf(temp,
73 "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
74 (unsigned char)ifr.ifr_hwaddr.sa_data[0],
75 (unsigned char)ifr.ifr_hwaddr.sa_data[1],
76 (unsigned char)ifr.ifr_hwaddr.sa_data[2],
77 (unsigned char)ifr.ifr_hwaddr.sa_data[3],
78 (unsigned char)ifr.ifr_hwaddr.sa_data[4],
79 (unsigned char)ifr.ifr_hwaddr.sa_data[5]);
80
81 mac = temp;
82 }
83
84 // close socket
85 close(fd);
86
87 return mac;
88 }
89#endif /* __linux__ */
90
91 // *******************************************************
92 // windows implementation
93 // *******************************************************
94#ifdef __WINDOWS__
95#include <Winerror.h>
96#include <iphlpapi.h>
97#include <winsock2.h>
98
99 std::string
101 {
102
103 std::string mac;
104 IP_ADAPTER_INFO AdapterInfo[128];
105 DWORD dwBufLen = sizeof(AdapterInfo);
106 DWORD dwStatus = GetAdaptersInfo(AdapterInfo, &dwBufLen);
107
108 // network card not found
109 if (dwStatus != ERROR_SUCCESS)
110 {
111 return "00:00:00:00:00:00";
112 }
113
114 PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo;
115 char szBuffer[512];
116
117 while (pAdapterInfo)
118 {
119 if (pAdapterInfo->Type == MIB_IF_TYPE_ETHERNET)
120 {
121 sprintf_s(szBuffer,
122 sizeof(szBuffer),
123 "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
124 pAdapterInfo->Address[0],
125 pAdapterInfo->Address[1],
126 pAdapterInfo->Address[2],
127 pAdapterInfo->Address[3],
128 pAdapterInfo->Address[4],
129 pAdapterInfo->Address[5]);
130
131 mac = szBuffer;
132 return mac;
133 }
134
135 pAdapterInfo = pAdapterInfo->Next;
136 }
137
138 return "00:00:00:00:00:00";
139 }
140#endif /* __WINDOWS__ */
141}
std::string getHardwareId()
Retrieve hardware id to identify local machine.
ArmarX headers.