| 1 |
// **************************************************************************** |
| 2 |
// Project: libguytools |
| 3 |
// **************************************************************************** |
| 4 |
// Programmer: Guy Voncken |
| 5 |
// Police Grand-Ducale |
| 6 |
// Service de Police Judiciaire |
| 7 |
// Section Nouvelles Technologies |
| 8 |
// **************************************************************************** |
| 9 |
// Module: System information |
| 10 |
// **************************************************************************** |
| 11 |
|
| 12 |
// Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, |
| 13 |
// 2018, 2019 |
| 14 |
// Guy Voncken |
| 15 |
// |
| 16 |
// This file is part of libguytools. |
| 17 |
// |
| 18 |
// libguytools is free software: you can redistribute it and/or modify |
| 19 |
// it under the terms of the GNU General Public License as published by |
| 20 |
// the Free Software Foundation, either version 2 of the License, or |
| 21 |
// (at your option) any later version. |
| 22 |
// |
| 23 |
// libguytools is distributed in the hope that it will be useful, |
| 24 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 25 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 26 |
// GNU General Public License for more details. |
| 27 |
// |
| 28 |
// You should have received a copy of the GNU General Public License |
| 29 |
// along with libguytools. If not, see <http://www.gnu.org/licenses/>. |
| 30 |
|
| 31 |
#include <limits.h> |
| 32 |
#include <math.h> |
| 33 |
#include <stdio.h> |
| 34 |
#include <cstdarg> |
| 35 |
|
| 36 |
#include <unistd.h> |
| 37 |
#include <sys/ioctl.h> |
| 38 |
#include <sys/utsname.h> |
| 39 |
#include <netinet/in.h> |
| 40 |
#include <linux/if.h> |
| 41 |
|
| 42 |
//#include <qapplication.h> |
| 43 |
#include <qstring.h> |
| 44 |
|
| 45 |
#include "toolconstants.h" |
| 46 |
#include "toolglobalid.h" |
| 47 |
#include "toolerror.h" |
| 48 |
#include "toolsysinfo.h" |
| 49 |
|
| 50 |
// ------------------------------------ |
| 51 |
// Constants |
| 52 |
// ------------------------------------ |
| 53 |
|
| 54 |
// ------------------------------------ |
| 55 |
// Type definitions |
| 56 |
// ------------------------------------ |
| 57 |
|
| 58 |
|
| 59 |
// ------------------------------------ |
| 60 |
// Local variables |
| 61 |
// ------------------------------------ |
| 62 |
|
| 63 |
static bool ToolSysInfoInitialized = false; |
| 64 |
|
| 65 |
// ------------------------------------ |
| 66 |
// Functions |
| 67 |
// ------------------------------------ |
| 68 |
|
| 69 |
APIRET ToolSysInfoGetMacAddr (t_pToolSysInfoMacAddr pMacAddr) |
| 70 |
{ |
| 71 |
struct ifreq Ifr; |
| 72 |
struct ifreq *pIfr; |
| 73 |
struct ifconf Ifc; |
| 74 |
char buf[1024]; |
| 75 |
int s, i, wr; |
| 76 |
bool ok = false; |
| 77 |
|
| 78 |
s = socket(AF_INET, SOCK_DGRAM, 0); |
| 79 |
if (s==-1) |
| 80 |
return TOOLSYSINFO_ERROR_SOCKET; |
| 81 |
|
| 82 |
Ifc.ifc_len = sizeof(buf); |
| 83 |
Ifc.ifc_buf = buf; |
| 84 |
ioctl(s, SIOCGIFCONF, &Ifc); |
| 85 |
|
| 86 |
pIfr = Ifc.ifc_req; |
| 87 |
for (i = Ifc.ifc_len / sizeof(struct ifreq); --i >= 0; pIfr++) |
| 88 |
{ |
| 89 |
strcpy (Ifr.ifr_name, pIfr->ifr_name); |
| 90 |
if (ioctl (s, SIOCGIFFLAGS, &Ifr) == 0) |
| 91 |
{ |
| 92 |
if (! (Ifr.ifr_flags & IFF_LOOPBACK)) |
| 93 |
{ |
| 94 |
if (ioctl(s, SIOCGIFHWADDR, &Ifr) == 0) |
| 95 |
{ |
| 96 |
ok = true; |
| 97 |
break; |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
close(s); |
| 103 |
|
| 104 |
if (!ok) |
| 105 |
return TOOLSYSINFO_ERROR_NO_ADDR; |
| 106 |
|
| 107 |
bcopy (Ifr.ifr_hwaddr.sa_data, &pMacAddr->AddrVal[0], TOOLSYSINFO_MACADDRLEN_VAL); |
| 108 |
wr = 0; |
| 109 |
for (int i=0; i<TOOLSYSINFO_MACADDRLEN_VAL; i++) |
| 110 |
wr += sprintf(&pMacAddr->AddrStr[wr], "%02X", pMacAddr->AddrVal[i]); |
| 111 |
|
| 112 |
return NO_ERROR; |
| 113 |
} |
| 114 |
|
| 115 |
APIRET ToolSysInfoUname (QString &Uname) |
| 116 |
{ |
| 117 |
struct utsname name; |
| 118 |
|
| 119 |
if (uname (&name) == -1) |
| 120 |
return TOOLSYSINFO_ERROR_UNAME; |
| 121 |
|
| 122 |
Uname = QString(name.sysname) + " " + name.nodename + " " + name.release + " " + name.version + " " + name.machine; |
| 123 |
|
| 124 |
return NO_ERROR; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
// ------------------------------ |
| 129 |
// Module initialisation |
| 130 |
// ------------------------------ |
| 131 |
|
| 132 |
APIRET ToolSysInfoInit (void) |
| 133 |
{ |
| 134 |
if (ToolSysInfoInitialized) |
| 135 |
return TOOLSYSINFO_ALREADY_INITIALISED; |
| 136 |
|
| 137 |
TOOL_CHK (TOOL_ERROR_REGISTER_CODE (ERROR_BASE_TOOL_SYSINFO)) |
| 138 |
TOOL_CHK (TOOL_ERROR_REGISTER_CODE (TOOLSYSINFO_ALREADY_INITIALISED)) |
| 139 |
TOOL_CHK (TOOL_ERROR_REGISTER_CODE (TOOLSYSINFO_ERROR_SOCKET)) |
| 140 |
TOOL_CHK (TOOL_ERROR_REGISTER_CODE (TOOLSYSINFO_ERROR_NO_ADDR)) |
| 141 |
TOOL_CHK (TOOL_ERROR_REGISTER_CODE (TOOLSYSINFO_ERROR_UNAME)) |
| 142 |
|
| 143 |
ToolSysInfoInitialized = true; |
| 144 |
|
| 145 |
return NO_ERROR; |
| 146 |
} |
| 147 |
|
| 148 |
APIRET ToolSysInfoDeInit (void) |
| 149 |
{ |
| 150 |
return NO_ERROR; |
| 151 |
} |
| 152 |
|