Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug: incorrect network interface returned by net_io_counters(True… #670

Merged
merged 2 commits into from
Aug 26, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions psutil/_psutil_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ typedef struct _MIB_UDP6TABLE_OWNER_PID {
MIB_UDP6ROW_OWNER_PID table[ANY_SIZE];
} MIB_UDP6TABLE_OWNER_PID, *PMIB_UDP6TABLE_OWNER_PID;


PIP_ADAPTER_ADDRESSES
psutil_get_nic_addresses() {
// allocate a 15 KB buffer to start with
Expand Down Expand Up @@ -2159,7 +2158,6 @@ psutil_disk_usage(PyObject *self, PyObject *args) {
*/
static PyObject *
psutil_net_io_counters(PyObject *self, PyObject *args) {
char ifname[MAX_PATH];
DWORD dwRetVal = 0;
MIB_IFROW *pIfRow = NULL;
PIP_ADAPTER_ADDRESSES pAddresses = NULL;
Expand Down Expand Up @@ -2205,9 +2203,8 @@ psutil_net_io_counters(PyObject *self, PyObject *args) {
if (!py_nic_info)
goto error;

sprintf_s(ifname, MAX_PATH, "%wS", pCurrAddresses->FriendlyName);
py_nic_name = PyUnicode_Decode(
ifname, _tcslen(ifname), Py_FileSystemDefaultEncoding, "replace");
py_nic_name = PyUnicode_FromWideChar(pCurrAddresses->FriendlyName,
wcslen(pCurrAddresses->FriendlyName));

if (py_nic_name == NULL)
goto error;
Expand Down Expand Up @@ -2844,7 +2841,6 @@ psutil_net_if_addrs(PyObject *self, PyObject *args) {
PCTSTR intRet;
char *ptr;
char buff[100];
char ifname[MAX_PATH];
DWORD bufflen = 100;
PIP_ADAPTER_ADDRESSES pAddresses = NULL;
PIP_ADAPTER_ADDRESSES pCurrAddresses = NULL;
Expand All @@ -2854,6 +2850,7 @@ psutil_net_if_addrs(PyObject *self, PyObject *args) {
PyObject *py_tuple = NULL;
PyObject *py_address = NULL;
PyObject *py_mac_address = NULL;
PyObject *py_nic_name = NULL;

if (py_retlist == NULL)
return NULL;
Expand All @@ -2865,7 +2862,12 @@ psutil_net_if_addrs(PyObject *self, PyObject *args) {

while (pCurrAddresses) {
pUnicast = pCurrAddresses->FirstUnicastAddress;
sprintf_s(ifname, MAX_PATH, "%wS", pCurrAddresses->FriendlyName);

py_nic_name = NULL;
py_nic_name = PyUnicode_FromWideChar(pCurrAddresses->FriendlyName,
wcslen(pCurrAddresses->FriendlyName));
if (py_nic_name == NULL)
goto error;

// MAC address
if (pCurrAddresses->PhysicalAddressLength != 0) {
Expand Down Expand Up @@ -2896,8 +2898,8 @@ psutil_net_if_addrs(PyObject *self, PyObject *args) {
Py_INCREF(Py_None);
Py_INCREF(Py_None);
py_tuple = Py_BuildValue(
"(siOOOO)",
ifname,
"(OiOOOO)",
py_nic_name,
-1, // this will be converted later to AF_LINK
py_mac_address,
Py_None, // netmask (not supported)
Expand Down Expand Up @@ -2950,8 +2952,8 @@ psutil_net_if_addrs(PyObject *self, PyObject *args) {
Py_INCREF(Py_None);
Py_INCREF(Py_None);
py_tuple = Py_BuildValue(
"(siOOOO)",
ifname,
"(OiOOOO)",
py_nic_name,
family,
py_address,
Py_None, // netmask (not supported)
Expand All @@ -2969,7 +2971,7 @@ psutil_net_if_addrs(PyObject *self, PyObject *args) {
pUnicast = pUnicast->Next;
}
}

Py_DECREF(py_nic_name);
pCurrAddresses = pCurrAddresses->Next;
}

Expand All @@ -2982,6 +2984,7 @@ psutil_net_if_addrs(PyObject *self, PyObject *args) {
Py_DECREF(py_retlist);
Py_XDECREF(py_tuple);
Py_XDECREF(py_address);
Py_XDECREF(py_nic_name);
return NULL;
}

Expand Down