Skip to content

Commit

Permalink
#250: return the same NIC names as the other net_* functions
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Feb 16, 2015
1 parent f18f2c4 commit 9631bb7
Showing 1 changed file with 43 additions and 14 deletions.
57 changes: 43 additions & 14 deletions psutil/_psutil_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -3163,10 +3163,7 @@ psutil_net_if_addrs(PyObject *self, PyObject *args)

/*
* Provides stats about NIC interfaces installed on the system.
* TODO: filter what's not a NIC
* TODO: return the same (friendly) names as net_io_counters() and
net_if_addrs()
* TODO: get 'duplex' (currently it's hard coded to '2', aka
* TODO: get 'duplex' (currently it's hard coded to '2', aka
'full duplex')
*/
static PyObject *
Expand All @@ -3177,18 +3174,28 @@ psutil_net_if_stats(PyObject *self, PyObject *args)
DWORD dwRetVal = 0;
MIB_IFTABLE *pIfTable;
MIB_IFROW *pIfRow;

PIP_ADAPTER_ADDRESSES pAddresses = NULL;
PIP_ADAPTER_ADDRESSES pCurrAddresses = NULL;
char friendly_name[MAX_PATH];
char descr[MAX_PATH];
int ifname_found;

PyObject *py_retdict = PyDict_New();
PyObject *py_ifc_info = NULL;
PyObject *py_is_up = NULL;

if (py_retdict == NULL)
return NULL;

pAddresses = psutil_get_nic_addresses();
if (pAddresses == NULL)
goto error;

pIfTable = (MIB_IFTABLE *) malloc(sizeof (MIB_IFTABLE));
if (pIfTable == NULL) {
PyErr_NoMemory();
goto error;
}
}
dwSize = sizeof(MIB_IFTABLE);
if (GetIfTable(pIfTable, &dwSize, FALSE) == ERROR_INSUFFICIENT_BUFFER) {
free(pIfTable);
Expand All @@ -3206,8 +3213,30 @@ psutil_net_if_stats(PyObject *self, PyObject *args)
}

for (i = 0; i < (int) pIfTable->dwNumEntries; i++) {
pIfRow = (MIB_IFROW *) & pIfTable->table[i];

pIfRow = (MIB_IFROW *) & pIfTable->table[i];

// GetIfTable is not able to give us NIC with "friendly names"
// so we determine them via GetAdapterAddresses() which
// provides friendly names *and* descriptions and find the
// ones that match.
ifname_found = 0;
pCurrAddresses = pAddresses;
while (pCurrAddresses) {
sprintf(descr, "%wS", pCurrAddresses->Description);
if (lstrcmp(descr, pIfRow->bDescr) == 0) {
sprintf(friendly_name, "%wS", pCurrAddresses->FriendlyName);
ifname_found = 1;
break;
}
pCurrAddresses = pCurrAddresses->Next;
}
if (ifname_found == 0) {
// Name not found means GetAdapterAddresses() doesn't list
// this NIC, only GetIfTable, meaning it's not really a NIC
// interface so we skip it.
continue;
}

// is up?
if((pIfRow->dwOperStatus == MIB_IF_OPER_STATUS_CONNECTED ||
pIfRow->dwOperStatus == MIB_IF_OPER_STATUS_OPERATIONAL) &&
Expand All @@ -3218,24 +3247,24 @@ psutil_net_if_stats(PyObject *self, PyObject *args)
py_is_up = Py_False;
}
Py_INCREF(py_is_up);

py_ifc_info = Py_BuildValue(
"(Oikk)",
py_is_up,
"(Oikk)",
py_is_up,
2, // there's no way to know duplex so let's assume 'full'
pIfRow->dwSpeed / 1000000, // expressed in bytes, we want Mb
pIfRow->dwMtu
);
if (!py_ifc_info)
goto error;
if (PyDict_SetItemString(py_retdict, pIfRow->bDescr, py_ifc_info))
if (PyDict_SetItemString(py_retdict, friendly_name, py_ifc_info))
goto error;
Py_DECREF(py_ifc_info);
}

free(pIfTable);
return py_retdict;

error:
Py_XDECREF(py_is_up);
Py_XDECREF(py_ifc_info);
Expand Down

0 comments on commit 9631bb7

Please sign in to comment.