| 1 |
/* |
| 2 |
* Copyright (c) 2009, Giampaolo Rodola'. All rights reserved. |
| 3 |
* Use of this source code is governed by a BSD-style license that can be |
| 4 |
* found in the LICENSE file. |
| 5 |
*/ |
| 6 |
|
| 7 |
// Fixes clash between winsock2.h and windows.h |
| 8 |
#define WIN32_LEAN_AND_MEAN |
| 9 |
|
| 10 |
#include <Python.h> |
| 11 |
#include <windows.h> |
| 12 |
#include <wchar.h> |
| 13 |
#include <ws2tcpip.h> |
| 14 |
|
| 15 |
#include "../../arch/all/init.h" |
| 16 |
|
| 17 |
|
| 18 |
static PIP_ADAPTER_ADDRESSES |
| 19 |
psutil_get_nic_addresses(void) { |
| 20 |
ULONG bufferLength = 0; |
| 21 |
PIP_ADAPTER_ADDRESSES buffer; |
| 22 |
|
| 23 |
if (GetAdaptersAddresses(AF_UNSPEC, 0, NULL, NULL, &bufferLength) |
| 24 |
!= ERROR_BUFFER_OVERFLOW) |
| 25 |
{ |
| 26 |
PyErr_SetString(PyExc_RuntimeError, |
| 27 |
"GetAdaptersAddresses() syscall failed."); |
| 28 |
return NULL; |
| 29 |
} |
| 30 |
|
| 31 |
buffer = malloc(bufferLength); |
| 32 |
if (buffer == NULL) { |
| 33 |
PyErr_NoMemory(); |
| 34 |
return NULL; |
| 35 |
} |
| 36 |
memset(buffer, 0, bufferLength); |
| 37 |
|
| 38 |
if (GetAdaptersAddresses(AF_UNSPEC, 0, NULL, buffer, &bufferLength) |
| 39 |
!= ERROR_SUCCESS) |
| 40 |
{ |
| 41 |
free(buffer); |
| 42 |
PyErr_SetString(PyExc_RuntimeError, |
| 43 |
"GetAdaptersAddresses() syscall failed."); |
| 44 |
return NULL; |
| 45 |
} |
| 46 |
|
| 47 |
return buffer; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
/* |
| 52 |
* Return a Python list of named tuples with overall network I/O information |
| 53 |
*/ |
| 54 |
PyObject * |
| 55 |
psutil_net_io_counters(PyObject *self, PyObject *args) { |
| 56 |
DWORD dwRetVal = 0; |
| 57 |
MIB_IF_ROW2 *pIfRow = NULL; |
| 58 |
PIP_ADAPTER_ADDRESSES pAddresses = NULL; |
| 59 |
PIP_ADAPTER_ADDRESSES pCurrAddresses = NULL; |
| 60 |
PyObject *py_retdict = PyDict_New(); |
| 61 |
PyObject *py_nic_info = NULL; |
| 62 |
PyObject *py_nic_name = NULL; |
| 63 |
|
| 64 |
if (py_retdict == NULL) |
| 65 |
return NULL; |
| 66 |
pAddresses = psutil_get_nic_addresses(); |
| 67 |
if (pAddresses == NULL) |
| 68 |
goto error; |
| 69 |
pCurrAddresses = pAddresses; |
| 70 |
|
| 71 |
while (pCurrAddresses) { |
| 72 |
py_nic_name = NULL; |
| 73 |
py_nic_info = NULL; |
| 74 |
|
| 75 |
pIfRow = (MIB_IF_ROW2 *) malloc(sizeof(MIB_IF_ROW2)); |
| 76 |
if (pIfRow == NULL) { |
| 77 |
PyErr_NoMemory(); |
| 78 |
goto error; |
| 79 |
} |
| 80 |
|
| 81 |
SecureZeroMemory((PVOID)pIfRow, sizeof(MIB_IF_ROW2)); |
| 82 |
pIfRow->InterfaceIndex = pCurrAddresses->IfIndex; |
| 83 |
dwRetVal = GetIfEntry2(pIfRow); |
| 84 |
if (dwRetVal != NO_ERROR) { |
| 85 |
PyErr_SetString(PyExc_RuntimeError, |
| 86 |
"GetIfEntry() or GetIfEntry2() syscalls failed."); |
| 87 |
goto error; |
| 88 |
} |
| 89 |
|
| 90 |
py_nic_info = Py_BuildValue( |
| 91 |
"(KKKKKKKK)", |
| 92 |
pIfRow->OutOctets, |
| 93 |
pIfRow->InOctets, |
| 94 |
(pIfRow->OutUcastPkts + pIfRow->OutNUcastPkts), |
| 95 |
(pIfRow->InUcastPkts + pIfRow->InNUcastPkts), |
| 96 |
pIfRow->InErrors, |
| 97 |
pIfRow->OutErrors, |
| 98 |
pIfRow->InDiscards, |
| 99 |
pIfRow->OutDiscards); |
| 100 |
if (!py_nic_info) |
| 101 |
goto error; |
| 102 |
|
| 103 |
py_nic_name = PyUnicode_FromWideChar( |
| 104 |
pCurrAddresses->FriendlyName, |
| 105 |
wcslen(pCurrAddresses->FriendlyName)); |
| 106 |
|
| 107 |
if (py_nic_name == NULL) |
| 108 |
goto error; |
| 109 |
if (PyDict_SetItem(py_retdict, py_nic_name, py_nic_info)) |
| 110 |
goto error; |
| 111 |
Py_CLEAR(py_nic_name); |
| 112 |
Py_CLEAR(py_nic_info); |
| 113 |
|
| 114 |
free(pIfRow); |
| 115 |
pCurrAddresses = pCurrAddresses->Next; |
| 116 |
} |
| 117 |
|
| 118 |
free(pAddresses); |
| 119 |
return py_retdict; |
| 120 |
|
| 121 |
error: |
| 122 |
Py_XDECREF(py_nic_name); |
| 123 |
Py_XDECREF(py_nic_info); |
| 124 |
Py_DECREF(py_retdict); |
| 125 |
if (pAddresses != NULL) |
| 126 |
free(pAddresses); |
| 127 |
if (pIfRow != NULL) |
| 128 |
free(pIfRow); |
| 129 |
return NULL; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
/* |
| 134 |
* Return NICs addresses. |
| 135 |
*/ |
| 136 |
PyObject * |
| 137 |
psutil_net_if_addrs(PyObject *self, PyObject *args) { |
| 138 |
unsigned int i = 0; |
| 139 |
ULONG family; |
| 140 |
PCTSTR intRet; |
| 141 |
PCTSTR netmaskIntRet; |
| 142 |
char *ptr; |
| 143 |
char buff_addr[1024]; |
| 144 |
char buff_macaddr[1024]; |
| 145 |
char buff_netmask[1024]; |
| 146 |
DWORD dwRetVal = 0; |
| 147 |
ULONG converted_netmask; |
| 148 |
UINT netmask_bits; |
| 149 |
struct in_addr in_netmask; |
| 150 |
PIP_ADAPTER_ADDRESSES pAddresses = NULL; |
| 151 |
PIP_ADAPTER_ADDRESSES pCurrAddresses = NULL; |
| 152 |
PIP_ADAPTER_UNICAST_ADDRESS pUnicast = NULL; |
| 153 |
|
| 154 |
PyObject *py_retlist = PyList_New(0); |
| 155 |
PyObject *py_tuple = NULL; |
| 156 |
PyObject *py_address = NULL; |
| 157 |
PyObject *py_mac_address = NULL; |
| 158 |
PyObject *py_nic_name = NULL; |
| 159 |
PyObject *py_netmask = NULL; |
| 160 |
|
| 161 |
if (py_retlist == NULL) |
| 162 |
return NULL; |
| 163 |
|
| 164 |
pAddresses = psutil_get_nic_addresses(); |
| 165 |
if (pAddresses == NULL) |
| 166 |
goto error; |
| 167 |
pCurrAddresses = pAddresses; |
| 168 |
|
| 169 |
while (pCurrAddresses) { |
| 170 |
pUnicast = pCurrAddresses->FirstUnicastAddress; |
| 171 |
|
| 172 |
netmaskIntRet = NULL; |
| 173 |
py_nic_name = NULL; |
| 174 |
py_nic_name = PyUnicode_FromWideChar( |
| 175 |
pCurrAddresses->FriendlyName, |
| 176 |
wcslen(pCurrAddresses->FriendlyName)); |
| 177 |
if (py_nic_name == NULL) |
| 178 |
goto error; |
| 179 |
|
| 180 |
// MAC address |
| 181 |
if (pCurrAddresses->PhysicalAddressLength != 0) { |
| 182 |
ptr = buff_macaddr; |
| 183 |
*ptr = '\0'; |
| 184 |
for (i = 0; i < (int) pCurrAddresses->PhysicalAddressLength; i++) { |
| 185 |
if (i == (pCurrAddresses->PhysicalAddressLength - 1)) { |
| 186 |
sprintf_s(ptr, _countof(buff_macaddr), "%.2X\n", |
| 187 |
(int)pCurrAddresses->PhysicalAddress[i]); |
| 188 |
} |
| 189 |
else { |
| 190 |
sprintf_s(ptr, _countof(buff_macaddr), "%.2X-", |
| 191 |
(int)pCurrAddresses->PhysicalAddress[i]); |
| 192 |
} |
| 193 |
ptr += 3; |
| 194 |
} |
| 195 |
*--ptr = '\0'; |
| 196 |
|
| 197 |
py_mac_address = Py_BuildValue("s", buff_macaddr); |
| 198 |
if (py_mac_address == NULL) |
| 199 |
goto error; |
| 200 |
|
| 201 |
Py_INCREF(Py_None); |
| 202 |
Py_INCREF(Py_None); |
| 203 |
Py_INCREF(Py_None); |
| 204 |
py_tuple = Py_BuildValue( |
| 205 |
"(OiOOOO)", |
| 206 |
py_nic_name, |
| 207 |
-1, // this will be converted later to AF_LINK |
| 208 |
py_mac_address, |
| 209 |
Py_None, // netmask (not supported) |
| 210 |
Py_None, // broadcast (not supported) |
| 211 |
Py_None // ptp (not supported on Windows) |
| 212 |
); |
| 213 |
if (! py_tuple) |
| 214 |
goto error; |
| 215 |
if (PyList_Append(py_retlist, py_tuple)) |
| 216 |
goto error; |
| 217 |
Py_CLEAR(py_tuple); |
| 218 |
Py_CLEAR(py_mac_address); |
| 219 |
} |
| 220 |
|
| 221 |
// find out the IP address associated with the NIC |
| 222 |
if (pUnicast != NULL) { |
| 223 |
for (i = 0; pUnicast != NULL; i++) { |
| 224 |
family = pUnicast->Address.lpSockaddr->sa_family; |
| 225 |
if (family == AF_INET) { |
| 226 |
struct sockaddr_in *sa_in = (struct sockaddr_in *) |
| 227 |
pUnicast->Address.lpSockaddr; |
| 228 |
intRet = inet_ntop(AF_INET, &(sa_in->sin_addr), buff_addr, |
| 229 |
sizeof(buff_addr)); |
| 230 |
if (!intRet) |
| 231 |
goto error; |
| 232 |
netmask_bits = pUnicast->OnLinkPrefixLength; |
| 233 |
dwRetVal = ConvertLengthToIpv4Mask( |
| 234 |
netmask_bits, &converted_netmask); |
| 235 |
if (dwRetVal == NO_ERROR) { |
| 236 |
in_netmask.s_addr = converted_netmask; |
| 237 |
netmaskIntRet = inet_ntop( |
| 238 |
AF_INET, &in_netmask, buff_netmask, |
| 239 |
sizeof(buff_netmask)); |
| 240 |
if (!netmaskIntRet) |
| 241 |
goto error; |
| 242 |
} |
| 243 |
} |
| 244 |
else if (family == AF_INET6) { |
| 245 |
struct sockaddr_in6 *sa_in6 = (struct sockaddr_in6 *) |
| 246 |
pUnicast->Address.lpSockaddr; |
| 247 |
intRet = inet_ntop(AF_INET6, &(sa_in6->sin6_addr), |
| 248 |
buff_addr, sizeof(buff_addr)); |
| 249 |
if (!intRet) |
| 250 |
goto error; |
| 251 |
} |
| 252 |
else { |
| 253 |
// we should never get here |
| 254 |
pUnicast = pUnicast->Next; |
| 255 |
continue; |
| 256 |
} |
| 257 |
|
| 258 |
py_address = PyUnicode_FromString(buff_addr); |
| 259 |
if (py_address == NULL) |
| 260 |
goto error; |
| 261 |
|
| 262 |
if (netmaskIntRet != NULL) { |
| 263 |
py_netmask = PyUnicode_FromString(buff_netmask); |
| 264 |
} |
| 265 |
else { |
| 266 |
Py_INCREF(Py_None); |
| 267 |
py_netmask = Py_None; |
| 268 |
} |
| 269 |
|
| 270 |
Py_INCREF(Py_None); |
| 271 |
Py_INCREF(Py_None); |
| 272 |
py_tuple = Py_BuildValue( |
| 273 |
"(OiOOOO)", |
| 274 |
py_nic_name, |
| 275 |
family, |
| 276 |
py_address, |
| 277 |
py_netmask, |
| 278 |
Py_None, // broadcast (not supported) |
| 279 |
Py_None // ptp (not supported on Windows) |
| 280 |
); |
| 281 |
|
| 282 |
if (! py_tuple) |
| 283 |
goto error; |
| 284 |
if (PyList_Append(py_retlist, py_tuple)) |
| 285 |
goto error; |
| 286 |
Py_CLEAR(py_tuple); |
| 287 |
Py_CLEAR(py_address); |
| 288 |
Py_CLEAR(py_netmask); |
| 289 |
|
| 290 |
pUnicast = pUnicast->Next; |
| 291 |
} |
| 292 |
} |
| 293 |
Py_CLEAR(py_nic_name); |
| 294 |
pCurrAddresses = pCurrAddresses->Next; |
| 295 |
} |
| 296 |
|
| 297 |
free(pAddresses); |
| 298 |
return py_retlist; |
| 299 |
|
| 300 |
error: |
| 301 |
if (pAddresses) |
| 302 |
free(pAddresses); |
| 303 |
Py_DECREF(py_retlist); |
| 304 |
Py_XDECREF(py_tuple); |
| 305 |
Py_XDECREF(py_address); |
| 306 |
Py_XDECREF(py_nic_name); |
| 307 |
Py_XDECREF(py_netmask); |
| 308 |
return NULL; |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
/* |
| 313 |
* Provides stats about NIC interfaces installed on the system. |
| 314 |
* TODO: get 'duplex' (currently it's hard coded to '2', aka 'full duplex') |
| 315 |
*/ |
| 316 |
PyObject * |
| 317 |
psutil_net_if_stats(PyObject *self, PyObject *args) { |
| 318 |
int i; |
| 319 |
DWORD dwSize = 0; |
| 320 |
DWORD dwRetVal = 0; |
| 321 |
MIB_IFTABLE *pIfTable; |
| 322 |
MIB_IFROW *pIfRow; |
| 323 |
PIP_ADAPTER_ADDRESSES pAddresses = NULL; |
| 324 |
PIP_ADAPTER_ADDRESSES pCurrAddresses = NULL; |
| 325 |
char descr[MAX_PATH]; |
| 326 |
int ifname_found; |
| 327 |
|
| 328 |
PyObject *py_nic_name = NULL; |
| 329 |
PyObject *py_retdict = PyDict_New(); |
| 330 |
PyObject *py_ifc_info = NULL; |
| 331 |
PyObject *py_is_up = NULL; |
| 332 |
|
| 333 |
if (py_retdict == NULL) |
| 334 |
return NULL; |
| 335 |
|
| 336 |
pAddresses = psutil_get_nic_addresses(); |
| 337 |
if (pAddresses == NULL) |
| 338 |
goto error; |
| 339 |
|
| 340 |
pIfTable = (MIB_IFTABLE *) malloc(sizeof (MIB_IFTABLE)); |
| 341 |
if (pIfTable == NULL) { |
| 342 |
PyErr_NoMemory(); |
| 343 |
goto error; |
| 344 |
} |
| 345 |
dwSize = sizeof(MIB_IFTABLE); |
| 346 |
if (GetIfTable(pIfTable, &dwSize, FALSE) == ERROR_INSUFFICIENT_BUFFER) { |
| 347 |
free(pIfTable); |
| 348 |
pIfTable = (MIB_IFTABLE *) malloc(dwSize); |
| 349 |
if (pIfTable == NULL) { |
| 350 |
PyErr_NoMemory(); |
| 351 |
goto error; |
| 352 |
} |
| 353 |
} |
| 354 |
// Make a second call to GetIfTable to get the actual |
| 355 |
// data we want. |
| 356 |
if ((dwRetVal = GetIfTable(pIfTable, &dwSize, FALSE)) != NO_ERROR) { |
| 357 |
PyErr_SetString(PyExc_RuntimeError, "GetIfTable() syscall failed"); |
| 358 |
goto error; |
| 359 |
} |
| 360 |
|
| 361 |
for (i = 0; i < (int) pIfTable->dwNumEntries; i++) { |
| 362 |
pIfRow = (MIB_IFROW *) & pIfTable->table[i]; |
| 363 |
|
| 364 |
// GetIfTable is not able to give us NIC with "friendly names" |
| 365 |
// so we determine them via GetAdapterAddresses() which |
| 366 |
// provides friendly names *and* descriptions and find the |
| 367 |
// ones that match. |
| 368 |
ifname_found = 0; |
| 369 |
pCurrAddresses = pAddresses; |
| 370 |
while (pCurrAddresses) { |
| 371 |
sprintf_s(descr, MAX_PATH, "%wS", pCurrAddresses->Description); |
| 372 |
if (lstrcmp(descr, pIfRow->bDescr) == 0) { |
| 373 |
py_nic_name = PyUnicode_FromWideChar( |
| 374 |
pCurrAddresses->FriendlyName, |
| 375 |
wcslen(pCurrAddresses->FriendlyName)); |
| 376 |
if (py_nic_name == NULL) |
| 377 |
goto error; |
| 378 |
ifname_found = 1; |
| 379 |
break; |
| 380 |
} |
| 381 |
pCurrAddresses = pCurrAddresses->Next; |
| 382 |
} |
| 383 |
if (ifname_found == 0) { |
| 384 |
// Name not found means GetAdapterAddresses() doesn't list |
| 385 |
// this NIC, only GetIfTable, meaning it's not really a NIC |
| 386 |
// interface so we skip it. |
| 387 |
continue; |
| 388 |
} |
| 389 |
|
| 390 |
// is up? |
| 391 |
if ((pIfRow->dwOperStatus == MIB_IF_OPER_STATUS_CONNECTED || |
| 392 |
pIfRow->dwOperStatus == MIB_IF_OPER_STATUS_OPERATIONAL) && |
| 393 |
pIfRow->dwAdminStatus == 1 ) { |
| 394 |
py_is_up = Py_True; |
| 395 |
} |
| 396 |
else { |
| 397 |
py_is_up = Py_False; |
| 398 |
} |
| 399 |
Py_INCREF(py_is_up); |
| 400 |
|
| 401 |
py_ifc_info = Py_BuildValue( |
| 402 |
"(Oikk)", |
| 403 |
py_is_up, |
| 404 |
2, // there's no way to know duplex so let's assume 'full' |
| 405 |
pIfRow->dwSpeed / 1000000, // expressed in bytes, we want Mb |
| 406 |
pIfRow->dwMtu |
| 407 |
); |
| 408 |
if (!py_ifc_info) |
| 409 |
goto error; |
| 410 |
if (PyDict_SetItem(py_retdict, py_nic_name, py_ifc_info)) |
| 411 |
goto error; |
| 412 |
Py_CLEAR(py_nic_name); |
| 413 |
Py_CLEAR(py_ifc_info); |
| 414 |
} |
| 415 |
|
| 416 |
free(pIfTable); |
| 417 |
free(pAddresses); |
| 418 |
return py_retdict; |
| 419 |
|
| 420 |
error: |
| 421 |
Py_XDECREF(py_is_up); |
| 422 |
Py_XDECREF(py_ifc_info); |
| 423 |
Py_XDECREF(py_nic_name); |
| 424 |
Py_DECREF(py_retdict); |
| 425 |
if (pIfTable != NULL) |
| 426 |
free(pIfTable); |
| 427 |
if (pAddresses != NULL) |
| 428 |
free(pAddresses); |
| 429 |
return NULL; |
| 430 |
} |