| 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 |
#include <Python.h> |
| 8 |
#include <errno.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <sys/types.h> |
| 11 |
#include <sys/socket.h> |
| 12 |
#include <sys/ioctl.h> |
| 13 |
#include <net/if.h> |
| 14 |
#include <unistd.h> |
| 15 |
|
| 16 |
#ifdef PSUTIL_SUNOS10 |
| 17 |
#include "arch/solaris/v10/ifaddrs.h" |
| 18 |
#elif PSUTIL_AIX |
| 19 |
#include "arch/aix/ifaddrs.h" |
| 20 |
#else |
| 21 |
#include <ifaddrs.h> |
| 22 |
#endif |
| 23 |
|
| 24 |
#if defined(PSUTIL_LINUX) |
| 25 |
#include <netdb.h> |
| 26 |
#include <linux/types.h> |
| 27 |
#include <linux/if_packet.h> |
| 28 |
#endif |
| 29 |
#if defined(PSUTIL_BSD) || defined(PSUTIL_OSX) |
| 30 |
#include <netdb.h> |
| 31 |
#include <netinet/in.h> |
| 32 |
#include <net/if_dl.h> |
| 33 |
#include <sys/sockio.h> |
| 34 |
#include <net/if_media.h> |
| 35 |
#include <net/if.h> |
| 36 |
#endif |
| 37 |
#if defined(PSUTIL_SUNOS) |
| 38 |
#include <netdb.h> |
| 39 |
#include <sys/sockio.h> |
| 40 |
#endif |
| 41 |
#if defined(PSUTIL_AIX) |
| 42 |
#include <netdb.h> |
| 43 |
#endif |
| 44 |
|
| 45 |
#include "../../arch/all/init.h" |
| 46 |
|
| 47 |
|
| 48 |
/* |
| 49 |
* Translate a sockaddr struct into a Python string. |
| 50 |
* Return None if address family is not AF_INET* or AF_PACKET. |
| 51 |
*/ |
| 52 |
PyObject * |
| 53 |
psutil_convert_ipaddr(struct sockaddr *addr, int family) { |
| 54 |
char buf[NI_MAXHOST]; |
| 55 |
int err; |
| 56 |
int addrlen; |
| 57 |
size_t n; |
| 58 |
size_t len; |
| 59 |
const char *data; |
| 60 |
char *ptr; |
| 61 |
|
| 62 |
if (addr == NULL) { |
| 63 |
Py_INCREF(Py_None); |
| 64 |
return Py_None; |
| 65 |
} |
| 66 |
else if (family == AF_INET || family == AF_INET6) { |
| 67 |
if (family == AF_INET) |
| 68 |
addrlen = sizeof(struct sockaddr_in); |
| 69 |
else |
| 70 |
addrlen = sizeof(struct sockaddr_in6); |
| 71 |
err = getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, |
| 72 |
NI_NUMERICHOST); |
| 73 |
if (err != 0) { |
| 74 |
// XXX we get here on FreeBSD when processing 'lo' / AF_INET6 |
| 75 |
// broadcast. Not sure what to do other than returning None. |
| 76 |
// ifconfig does not show anything BTW. |
| 77 |
// PyErr_Format(PyExc_RuntimeError, gai_strerror(err)); |
| 78 |
// return NULL; |
| 79 |
Py_INCREF(Py_None); |
| 80 |
return Py_None; |
| 81 |
} |
| 82 |
else { |
| 83 |
return Py_BuildValue("s", buf); |
| 84 |
} |
| 85 |
} |
| 86 |
#ifdef PSUTIL_LINUX |
| 87 |
else if (family == AF_PACKET) { |
| 88 |
struct sockaddr_ll *lladdr = (struct sockaddr_ll *)addr; |
| 89 |
len = lladdr->sll_halen; |
| 90 |
data = (const char *)lladdr->sll_addr; |
| 91 |
} |
| 92 |
#elif defined(PSUTIL_BSD) || defined(PSUTIL_OSX) |
| 93 |
else if (addr->sa_family == AF_LINK) { |
| 94 |
// Note: prior to Python 3.4 socket module does not expose |
| 95 |
// AF_LINK so we'll do. |
| 96 |
struct sockaddr_dl *dladdr = (struct sockaddr_dl *)addr; |
| 97 |
len = dladdr->sdl_alen; |
| 98 |
data = LLADDR(dladdr); |
| 99 |
} |
| 100 |
#endif |
| 101 |
else { |
| 102 |
// unknown family |
| 103 |
Py_INCREF(Py_None); |
| 104 |
return Py_None; |
| 105 |
} |
| 106 |
|
| 107 |
// AF_PACKET or AF_LINK |
| 108 |
if (len > 0) { |
| 109 |
ptr = buf; |
| 110 |
for (n = 0; n < len; ++n) { |
| 111 |
sprintf(ptr, "%02x:", data[n] & 0xff); |
| 112 |
ptr += 3; |
| 113 |
} |
| 114 |
*--ptr = '\0'; |
| 115 |
return Py_BuildValue("s", buf); |
| 116 |
} |
| 117 |
else { |
| 118 |
Py_INCREF(Py_None); |
| 119 |
return Py_None; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
/* |
| 125 |
* Return NICs information a-la ifconfig as a list of tuples. |
| 126 |
* TODO: on Solaris we won't get any MAC address. |
| 127 |
*/ |
| 128 |
PyObject* |
| 129 |
psutil_net_if_addrs(PyObject* self, PyObject* args) { |
| 130 |
struct ifaddrs *ifaddr, *ifa; |
| 131 |
int family; |
| 132 |
|
| 133 |
PyObject *py_retlist = PyList_New(0); |
| 134 |
PyObject *py_tuple = NULL; |
| 135 |
PyObject *py_address = NULL; |
| 136 |
PyObject *py_netmask = NULL; |
| 137 |
PyObject *py_broadcast = NULL; |
| 138 |
PyObject *py_ptp = NULL; |
| 139 |
|
| 140 |
if (py_retlist == NULL) |
| 141 |
return NULL; |
| 142 |
if (getifaddrs(&ifaddr) == -1) { |
| 143 |
PyErr_SetFromErrno(PyExc_OSError); |
| 144 |
goto error; |
| 145 |
} |
| 146 |
|
| 147 |
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { |
| 148 |
if (!ifa->ifa_addr) |
| 149 |
continue; |
| 150 |
family = ifa->ifa_addr->sa_family; |
| 151 |
py_address = psutil_convert_ipaddr(ifa->ifa_addr, family); |
| 152 |
// If the primary address can't be determined just skip it. |
| 153 |
// I've never seen this happen on Linux but I did on FreeBSD. |
| 154 |
if (py_address == Py_None) |
| 155 |
continue; |
| 156 |
if (py_address == NULL) |
| 157 |
goto error; |
| 158 |
py_netmask = psutil_convert_ipaddr(ifa->ifa_netmask, family); |
| 159 |
if (py_netmask == NULL) |
| 160 |
goto error; |
| 161 |
|
| 162 |
if (ifa->ifa_flags & IFF_BROADCAST) { |
| 163 |
py_broadcast = psutil_convert_ipaddr(ifa->ifa_broadaddr, family); |
| 164 |
Py_INCREF(Py_None); |
| 165 |
py_ptp = Py_None; |
| 166 |
} |
| 167 |
else if (ifa->ifa_flags & IFF_POINTOPOINT) { |
| 168 |
py_ptp = psutil_convert_ipaddr(ifa->ifa_dstaddr, family); |
| 169 |
Py_INCREF(Py_None); |
| 170 |
py_broadcast = Py_None; |
| 171 |
} |
| 172 |
else { |
| 173 |
Py_INCREF(Py_None); |
| 174 |
Py_INCREF(Py_None); |
| 175 |
py_broadcast = Py_None; |
| 176 |
py_ptp = Py_None; |
| 177 |
} |
| 178 |
|
| 179 |
if ((py_broadcast == NULL) || (py_ptp == NULL)) |
| 180 |
goto error; |
| 181 |
py_tuple = Py_BuildValue( |
| 182 |
"(siOOOO)", |
| 183 |
ifa->ifa_name, |
| 184 |
family, |
| 185 |
py_address, |
| 186 |
py_netmask, |
| 187 |
py_broadcast, |
| 188 |
py_ptp |
| 189 |
); |
| 190 |
|
| 191 |
if (! py_tuple) |
| 192 |
goto error; |
| 193 |
if (PyList_Append(py_retlist, py_tuple)) |
| 194 |
goto error; |
| 195 |
Py_CLEAR(py_tuple); |
| 196 |
Py_CLEAR(py_address); |
| 197 |
Py_CLEAR(py_netmask); |
| 198 |
Py_CLEAR(py_broadcast); |
| 199 |
Py_CLEAR(py_ptp); |
| 200 |
} |
| 201 |
|
| 202 |
freeifaddrs(ifaddr); |
| 203 |
return py_retlist; |
| 204 |
|
| 205 |
error: |
| 206 |
if (ifaddr != NULL) |
| 207 |
freeifaddrs(ifaddr); |
| 208 |
Py_DECREF(py_retlist); |
| 209 |
Py_XDECREF(py_tuple); |
| 210 |
Py_XDECREF(py_address); |
| 211 |
Py_XDECREF(py_netmask); |
| 212 |
Py_XDECREF(py_broadcast); |
| 213 |
Py_XDECREF(py_ptp); |
| 214 |
return NULL; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
/* |
| 219 |
* Return NIC MTU. References: |
| 220 |
* http://www.i-scream.org/libstatgrab/ |
| 221 |
*/ |
| 222 |
PyObject * |
| 223 |
psutil_net_if_mtu(PyObject *self, PyObject *args) { |
| 224 |
char *nic_name; |
| 225 |
int sock = -1; |
| 226 |
int ret; |
| 227 |
#ifdef PSUTIL_SUNOS10 |
| 228 |
struct lifreq lifr; |
| 229 |
#else |
| 230 |
struct ifreq ifr; |
| 231 |
#endif |
| 232 |
|
| 233 |
if (! PyArg_ParseTuple(args, "s", &nic_name)) |
| 234 |
return NULL; |
| 235 |
|
| 236 |
sock = socket(AF_INET, SOCK_DGRAM, 0); |
| 237 |
if (sock == -1) |
| 238 |
goto error; |
| 239 |
|
| 240 |
#ifdef PSUTIL_SUNOS10 |
| 241 |
PSUTIL_STRNCPY(lifr.lifr_name, nic_name, sizeof(lifr.lifr_name)); |
| 242 |
ret = ioctl(sock, SIOCGIFMTU, &lifr); |
| 243 |
#else |
| 244 |
PSUTIL_STRNCPY(ifr.ifr_name, nic_name, sizeof(ifr.ifr_name)); |
| 245 |
ret = ioctl(sock, SIOCGIFMTU, &ifr); |
| 246 |
#endif |
| 247 |
if (ret == -1) |
| 248 |
goto error; |
| 249 |
close(sock); |
| 250 |
|
| 251 |
#ifdef PSUTIL_SUNOS10 |
| 252 |
return Py_BuildValue("i", lifr.lifr_mtu); |
| 253 |
#else |
| 254 |
return Py_BuildValue("i", ifr.ifr_mtu); |
| 255 |
#endif |
| 256 |
|
| 257 |
error: |
| 258 |
if (sock != -1) |
| 259 |
close(sock); |
| 260 |
return PyErr_SetFromErrno(PyExc_OSError); |
| 261 |
} |
| 262 |
|
| 263 |
static int |
| 264 |
append_flag(PyObject *py_retlist, const char * flag_name) |
| 265 |
{ |
| 266 |
PyObject *py_str = NULL; |
| 267 |
|
| 268 |
py_str = PyUnicode_FromString(flag_name); |
| 269 |
if (! py_str) |
| 270 |
return 0; |
| 271 |
if (PyList_Append(py_retlist, py_str)) { |
| 272 |
Py_DECREF(py_str); |
| 273 |
return 0; |
| 274 |
} |
| 275 |
Py_CLEAR(py_str); |
| 276 |
|
| 277 |
return 1; |
| 278 |
} |
| 279 |
|
| 280 |
/* |
| 281 |
* Get all of the NIC flags and return them. |
| 282 |
*/ |
| 283 |
PyObject * |
| 284 |
psutil_net_if_flags(PyObject *self, PyObject *args) { |
| 285 |
char *nic_name; |
| 286 |
int sock = -1; |
| 287 |
int ret; |
| 288 |
struct ifreq ifr; |
| 289 |
PyObject *py_retlist = PyList_New(0); |
| 290 |
short int flags; |
| 291 |
|
| 292 |
if (py_retlist == NULL) |
| 293 |
return NULL; |
| 294 |
|
| 295 |
if (! PyArg_ParseTuple(args, "s", &nic_name)) |
| 296 |
goto error; |
| 297 |
|
| 298 |
sock = socket(AF_INET, SOCK_DGRAM, 0); |
| 299 |
if (sock == -1) { |
| 300 |
psutil_PyErr_SetFromOSErrnoWithSyscall("socket(SOCK_DGRAM)"); |
| 301 |
goto error; |
| 302 |
} |
| 303 |
|
| 304 |
PSUTIL_STRNCPY(ifr.ifr_name, nic_name, sizeof(ifr.ifr_name)); |
| 305 |
ret = ioctl(sock, SIOCGIFFLAGS, &ifr); |
| 306 |
if (ret == -1) { |
| 307 |
psutil_PyErr_SetFromOSErrnoWithSyscall("ioctl(SIOCGIFFLAGS)"); |
| 308 |
goto error; |
| 309 |
} |
| 310 |
|
| 311 |
close(sock); |
| 312 |
sock = -1; |
| 313 |
|
| 314 |
flags = ifr.ifr_flags & 0xFFFF; |
| 315 |
|
| 316 |
// Linux/glibc IFF flags: https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/gnu/net/if.h;h=251418f82331c0426e58707fe4473d454893b132;hb=HEAD |
| 317 |
// macOS IFF flags: https://opensource.apple.com/source/xnu/xnu-792/bsd/net/if.h.auto.html |
| 318 |
// AIX IFF flags: https://www.ibm.com/support/pages/how-hexadecimal-flags-displayed-ifconfig-are-calculated |
| 319 |
// FreeBSD IFF flags: https://www.freebsd.org/cgi/man.cgi?query=if_allmulti&apropos=0&sektion=0&manpath=FreeBSD+10-current&format=html |
| 320 |
|
| 321 |
#ifdef IFF_UP |
| 322 |
// Available in (at least) Linux, macOS, AIX, BSD |
| 323 |
if (flags & IFF_UP) |
| 324 |
if (!append_flag(py_retlist, "up")) |
| 325 |
goto error; |
| 326 |
#endif |
| 327 |
#ifdef IFF_BROADCAST |
| 328 |
// Available in (at least) Linux, macOS, AIX, BSD |
| 329 |
if (flags & IFF_BROADCAST) |
| 330 |
if (!append_flag(py_retlist, "broadcast")) |
| 331 |
goto error; |
| 332 |
#endif |
| 333 |
#ifdef IFF_DEBUG |
| 334 |
// Available in (at least) Linux, macOS, BSD |
| 335 |
if (flags & IFF_DEBUG) |
| 336 |
if (!append_flag(py_retlist, "debug")) |
| 337 |
goto error; |
| 338 |
#endif |
| 339 |
#ifdef IFF_LOOPBACK |
| 340 |
// Available in (at least) Linux, macOS, BSD |
| 341 |
if (flags & IFF_LOOPBACK) |
| 342 |
if (!append_flag(py_retlist, "loopback")) |
| 343 |
goto error; |
| 344 |
#endif |
| 345 |
#ifdef IFF_POINTOPOINT |
| 346 |
// Available in (at least) Linux, macOS, BSD |
| 347 |
if (flags & IFF_POINTOPOINT) |
| 348 |
if (!append_flag(py_retlist, "pointopoint")) |
| 349 |
goto error; |
| 350 |
#endif |
| 351 |
#ifdef IFF_NOTRAILERS |
| 352 |
// Available in (at least) Linux, macOS, AIX |
| 353 |
if (flags & IFF_NOTRAILERS) |
| 354 |
if (!append_flag(py_retlist, "notrailers")) |
| 355 |
goto error; |
| 356 |
#endif |
| 357 |
#ifdef IFF_RUNNING |
| 358 |
// Available in (at least) Linux, macOS, AIX, BSD |
| 359 |
if (flags & IFF_RUNNING) |
| 360 |
if (!append_flag(py_retlist, "running")) |
| 361 |
goto error; |
| 362 |
#endif |
| 363 |
#ifdef IFF_NOARP |
| 364 |
// Available in (at least) Linux, macOS, BSD |
| 365 |
if (flags & IFF_NOARP) |
| 366 |
if (!append_flag(py_retlist, "noarp")) |
| 367 |
goto error; |
| 368 |
#endif |
| 369 |
#ifdef IFF_PROMISC |
| 370 |
// Available in (at least) Linux, macOS, BSD |
| 371 |
if (flags & IFF_PROMISC) |
| 372 |
if (!append_flag(py_retlist, "promisc")) |
| 373 |
goto error; |
| 374 |
#endif |
| 375 |
#ifdef IFF_ALLMULTI |
| 376 |
// Available in (at least) Linux, macOS, BSD |
| 377 |
if (flags & IFF_ALLMULTI) |
| 378 |
if (!append_flag(py_retlist, "allmulti")) |
| 379 |
goto error; |
| 380 |
#endif |
| 381 |
#ifdef IFF_MASTER |
| 382 |
// Available in (at least) Linux |
| 383 |
if (flags & IFF_MASTER) |
| 384 |
if (!append_flag(py_retlist, "master")) |
| 385 |
goto error; |
| 386 |
#endif |
| 387 |
#ifdef IFF_SLAVE |
| 388 |
// Available in (at least) Linux |
| 389 |
if (flags & IFF_SLAVE) |
| 390 |
if (!append_flag(py_retlist, "slave")) |
| 391 |
goto error; |
| 392 |
#endif |
| 393 |
#ifdef IFF_MULTICAST |
| 394 |
// Available in (at least) Linux, macOS, BSD |
| 395 |
if (flags & IFF_MULTICAST) |
| 396 |
if (!append_flag(py_retlist, "multicast")) |
| 397 |
goto error; |
| 398 |
#endif |
| 399 |
#ifdef IFF_PORTSEL |
| 400 |
// Available in (at least) Linux |
| 401 |
if (flags & IFF_PORTSEL) |
| 402 |
if (!append_flag(py_retlist, "portsel")) |
| 403 |
goto error; |
| 404 |
#endif |
| 405 |
#ifdef IFF_AUTOMEDIA |
| 406 |
// Available in (at least) Linux |
| 407 |
if (flags & IFF_AUTOMEDIA) |
| 408 |
if (!append_flag(py_retlist, "automedia")) |
| 409 |
goto error; |
| 410 |
#endif |
| 411 |
#ifdef IFF_DYNAMIC |
| 412 |
// Available in (at least) Linux |
| 413 |
if (flags & IFF_DYNAMIC) |
| 414 |
if (!append_flag(py_retlist, "dynamic")) |
| 415 |
goto error; |
| 416 |
#endif |
| 417 |
#ifdef IFF_OACTIVE |
| 418 |
// Available in (at least) macOS, BSD |
| 419 |
if (flags & IFF_OACTIVE) |
| 420 |
if (!append_flag(py_retlist, "oactive")) |
| 421 |
goto error; |
| 422 |
#endif |
| 423 |
#ifdef IFF_SIMPLEX |
| 424 |
// Available in (at least) macOS, AIX, BSD |
| 425 |
if (flags & IFF_SIMPLEX) |
| 426 |
if (!append_flag(py_retlist, "simplex")) |
| 427 |
goto error; |
| 428 |
#endif |
| 429 |
#ifdef IFF_LINK0 |
| 430 |
// Available in (at least) macOS, BSD |
| 431 |
if (flags & IFF_LINK0) |
| 432 |
if (!append_flag(py_retlist, "link0")) |
| 433 |
goto error; |
| 434 |
#endif |
| 435 |
#ifdef IFF_LINK1 |
| 436 |
// Available in (at least) macOS, BSD |
| 437 |
if (flags & IFF_LINK1) |
| 438 |
if (!append_flag(py_retlist, "link1")) |
| 439 |
goto error; |
| 440 |
#endif |
| 441 |
#ifdef IFF_LINK2 |
| 442 |
// Available in (at least) macOS, BSD |
| 443 |
if (flags & IFF_LINK2) |
| 444 |
if (!append_flag(py_retlist, "link2")) |
| 445 |
goto error; |
| 446 |
#endif |
| 447 |
#ifdef IFF_D2 |
| 448 |
// Available in (at least) AIX |
| 449 |
if (flags & IFF_D2) |
| 450 |
if (!append_flag(py_retlist, "d2")) |
| 451 |
goto error; |
| 452 |
#endif |
| 453 |
|
| 454 |
return py_retlist; |
| 455 |
|
| 456 |
error: |
| 457 |
Py_DECREF(py_retlist); |
| 458 |
if (sock != -1) |
| 459 |
close(sock); |
| 460 |
return NULL; |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
/* |
| 465 |
* Inspect NIC flags, returns a bool indicating whether the NIC is |
| 466 |
* running. References: |
| 467 |
* http://www.i-scream.org/libstatgrab/ |
| 468 |
*/ |
| 469 |
PyObject * |
| 470 |
psutil_net_if_is_running(PyObject *self, PyObject *args) { |
| 471 |
char *nic_name; |
| 472 |
int sock = -1; |
| 473 |
int ret; |
| 474 |
struct ifreq ifr; |
| 475 |
|
| 476 |
if (! PyArg_ParseTuple(args, "s", &nic_name)) |
| 477 |
return NULL; |
| 478 |
|
| 479 |
sock = socket(AF_INET, SOCK_DGRAM, 0); |
| 480 |
if (sock == -1) |
| 481 |
goto error; |
| 482 |
|
| 483 |
PSUTIL_STRNCPY(ifr.ifr_name, nic_name, sizeof(ifr.ifr_name)); |
| 484 |
ret = ioctl(sock, SIOCGIFFLAGS, &ifr); |
| 485 |
if (ret == -1) |
| 486 |
goto error; |
| 487 |
|
| 488 |
close(sock); |
| 489 |
if ((ifr.ifr_flags & IFF_RUNNING) != 0) |
| 490 |
return Py_BuildValue("O", Py_True); |
| 491 |
else |
| 492 |
return Py_BuildValue("O", Py_False); |
| 493 |
|
| 494 |
error: |
| 495 |
if (sock != -1) |
| 496 |
close(sock); |
| 497 |
return PyErr_SetFromErrno(PyExc_OSError); |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
// net_if_stats() macOS/BSD implementation. |
| 503 |
#ifdef PSUTIL_HAS_NET_IF_DUPLEX_SPEED |
| 504 |
|
| 505 |
int psutil_get_nic_speed(int ifm_active) { |
| 506 |
// Determine NIC speed. Taken from: |
| 507 |
// http://www.i-scream.org/libstatgrab/ |
| 508 |
// Assuming only ETHER devices |
| 509 |
switch(IFM_TYPE(ifm_active)) { |
| 510 |
case IFM_ETHER: |
| 511 |
switch(IFM_SUBTYPE(ifm_active)) { |
| 512 |
#if defined(IFM_HPNA_1) && ((!defined(IFM_10G_LR)) \ |
| 513 |
|| (IFM_10G_LR != IFM_HPNA_1)) |
| 514 |
// HomePNA 1.0 (1Mb/s) |
| 515 |
case(IFM_HPNA_1): |
| 516 |
return 1; |
| 517 |
#endif |
| 518 |
// 10 Mbit |
| 519 |
case(IFM_10_T): // 10BaseT - RJ45 |
| 520 |
case(IFM_10_2): // 10Base2 - Thinnet |
| 521 |
case(IFM_10_5): // 10Base5 - AUI |
| 522 |
case(IFM_10_STP): // 10BaseT over shielded TP |
| 523 |
case(IFM_10_FL): // 10baseFL - Fiber |
| 524 |
return 10; |
| 525 |
// 100 Mbit |
| 526 |
case(IFM_100_TX): // 100BaseTX - RJ45 |
| 527 |
case(IFM_100_FX): // 100BaseFX - Fiber |
| 528 |
case(IFM_100_T4): // 100BaseT4 - 4 pair cat 3 |
| 529 |
case(IFM_100_VG): // 100VG-AnyLAN |
| 530 |
case(IFM_100_T2): // 100BaseT2 |
| 531 |
return 100; |
| 532 |
// 1000 Mbit |
| 533 |
case(IFM_1000_SX): // 1000BaseSX - multi-mode fiber |
| 534 |
case(IFM_1000_LX): // 1000baseLX - single-mode fiber |
| 535 |
case(IFM_1000_CX): // 1000baseCX - 150ohm STP |
| 536 |
#if defined(IFM_1000_TX) && !defined(PSUTIL_OPENBSD) |
| 537 |
#define HAS_CASE_IFM_1000_TX 1 |
| 538 |
// FreeBSD 4 and others (but NOT OpenBSD) -> #define IFM_1000_T in net/if_media.h |
| 539 |
case(IFM_1000_TX): |
| 540 |
#endif |
| 541 |
#ifdef IFM_1000_FX |
| 542 |
case(IFM_1000_FX): |
| 543 |
#endif |
| 544 |
#if defined(IFM_1000_T) && (!HAS_CASE_IFM_1000_TX || IFM_1000_T != IFM_1000_TX) |
| 545 |
case(IFM_1000_T): |
| 546 |
#endif |
| 547 |
return 1000; |
| 548 |
#if defined(IFM_10G_SR) || defined(IFM_10G_LR) || defined(IFM_10G_CX4) \ |
| 549 |
|| defined(IFM_10G_T) |
| 550 |
#ifdef IFM_10G_SR |
| 551 |
case(IFM_10G_SR): |
| 552 |
#endif |
| 553 |
#ifdef IFM_10G_LR |
| 554 |
case(IFM_10G_LR): |
| 555 |
#endif |
| 556 |
#ifdef IFM_10G_CX4 |
| 557 |
case(IFM_10G_CX4): |
| 558 |
#endif |
| 559 |
#ifdef IFM_10G_TWINAX |
| 560 |
case(IFM_10G_TWINAX): |
| 561 |
#endif |
| 562 |
#ifdef IFM_10G_TWINAX_LONG |
| 563 |
case(IFM_10G_TWINAX_LONG): |
| 564 |
#endif |
| 565 |
#ifdef IFM_10G_T |
| 566 |
case(IFM_10G_T): |
| 567 |
#endif |
| 568 |
return 10000; |
| 569 |
#endif |
| 570 |
#if defined(IFM_2500_SX) |
| 571 |
#ifdef IFM_2500_SX |
| 572 |
case(IFM_2500_SX): |
| 573 |
#endif |
| 574 |
return 2500; |
| 575 |
#endif // any 2.5GBit stuff... |
| 576 |
// We don't know what it is |
| 577 |
default: |
| 578 |
return 0; |
| 579 |
} |
| 580 |
break; |
| 581 |
|
| 582 |
#ifdef IFM_TOKEN |
| 583 |
case IFM_TOKEN: |
| 584 |
switch(IFM_SUBTYPE(ifm_active)) { |
| 585 |
case IFM_TOK_STP4: // Shielded twisted pair 4m - DB9 |
| 586 |
case IFM_TOK_UTP4: // Unshielded twisted pair 4m - RJ45 |
| 587 |
return 4; |
| 588 |
case IFM_TOK_STP16: // Shielded twisted pair 16m - DB9 |
| 589 |
case IFM_TOK_UTP16: // Unshielded twisted pair 16m - RJ45 |
| 590 |
return 16; |
| 591 |
#if defined(IFM_TOK_STP100) || defined(IFM_TOK_UTP100) |
| 592 |
#ifdef IFM_TOK_STP100 |
| 593 |
case IFM_TOK_STP100: // Shielded twisted pair 100m - DB9 |
| 594 |
#endif |
| 595 |
#ifdef IFM_TOK_UTP100 |
| 596 |
case IFM_TOK_UTP100: // Unshielded twisted pair 100m - RJ45 |
| 597 |
#endif |
| 598 |
return 100; |
| 599 |
#endif |
| 600 |
// We don't know what it is |
| 601 |
default: |
| 602 |
return 0; |
| 603 |
} |
| 604 |
break; |
| 605 |
#endif |
| 606 |
|
| 607 |
#ifdef IFM_FDDI |
| 608 |
case IFM_FDDI: |
| 609 |
switch(IFM_SUBTYPE(ifm_active)) { |
| 610 |
// We don't know what it is |
| 611 |
default: |
| 612 |
return 0; |
| 613 |
} |
| 614 |
break; |
| 615 |
#endif |
| 616 |
case IFM_IEEE80211: |
| 617 |
switch(IFM_SUBTYPE(ifm_active)) { |
| 618 |
case IFM_IEEE80211_FH1: // Frequency Hopping 1Mbps |
| 619 |
case IFM_IEEE80211_DS1: // Direct Sequence 1Mbps |
| 620 |
return 1; |
| 621 |
case IFM_IEEE80211_FH2: // Frequency Hopping 2Mbps |
| 622 |
case IFM_IEEE80211_DS2: // Direct Sequence 2Mbps |
| 623 |
return 2; |
| 624 |
case IFM_IEEE80211_DS5: // Direct Sequence 5Mbps |
| 625 |
return 5; |
| 626 |
case IFM_IEEE80211_DS11: // Direct Sequence 11Mbps |
| 627 |
return 11; |
| 628 |
case IFM_IEEE80211_DS22: // Direct Sequence 22Mbps |
| 629 |
return 22; |
| 630 |
// We don't know what it is |
| 631 |
default: |
| 632 |
return 0; |
| 633 |
} |
| 634 |
break; |
| 635 |
|
| 636 |
default: |
| 637 |
return 0; |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
/* |
| 643 |
* Return stats about a particular network interface. |
| 644 |
* References: |
| 645 |
* http://www.i-scream.org/libstatgrab/ |
| 646 |
*/ |
| 647 |
PyObject * |
| 648 |
psutil_net_if_duplex_speed(PyObject *self, PyObject *args) { |
| 649 |
char *nic_name; |
| 650 |
int sock = -1; |
| 651 |
int ret; |
| 652 |
int duplex; |
| 653 |
int speed; |
| 654 |
struct ifreq ifr; |
| 655 |
struct ifmediareq ifmed; |
| 656 |
|
| 657 |
if (! PyArg_ParseTuple(args, "s", &nic_name)) |
| 658 |
return NULL; |
| 659 |
|
| 660 |
sock = socket(AF_INET, SOCK_DGRAM, 0); |
| 661 |
if (sock == -1) |
| 662 |
return PyErr_SetFromErrno(PyExc_OSError); |
| 663 |
PSUTIL_STRNCPY(ifr.ifr_name, nic_name, sizeof(ifr.ifr_name)); |
| 664 |
|
| 665 |
// speed / duplex |
| 666 |
memset(&ifmed, 0, sizeof(struct ifmediareq)); |
| 667 |
strlcpy(ifmed.ifm_name, nic_name, sizeof(ifmed.ifm_name)); |
| 668 |
ret = ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmed); |
| 669 |
if (ret == -1) { |
| 670 |
speed = 0; |
| 671 |
duplex = 0; |
| 672 |
} |
| 673 |
else { |
| 674 |
speed = psutil_get_nic_speed(ifmed.ifm_active); |
| 675 |
if ((ifmed.ifm_active | IFM_FDX) == ifmed.ifm_active) |
| 676 |
duplex = 2; |
| 677 |
else if ((ifmed.ifm_active | IFM_HDX) == ifmed.ifm_active) |
| 678 |
duplex = 1; |
| 679 |
else |
| 680 |
duplex = 0; |
| 681 |
} |
| 682 |
|
| 683 |
close(sock); |
| 684 |
return Py_BuildValue("[ii]", duplex, speed); |
| 685 |
} |
| 686 |
#endif // PSUTIL_HAS_NET_IF_DUPLEX_SPEED |