Skip to content

Commit

Permalink
#376: MAC address on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Oct 5, 2014
1 parent aabc21e commit b1189a3
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion psutil/_psutil_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -3020,6 +3020,7 @@ psutil_net_if_addrs(PyObject *self, PyObject *args)
ULONG outBufLen = 0;
ULONG iterations = 0;
PCTSTR WSAAPI intRet;
char *ptr;
char buff[100];
char ifname[MAX_PATH];
DWORD bufflen = 100;
Expand All @@ -3030,6 +3031,7 @@ psutil_net_if_addrs(PyObject *self, PyObject *args)
PyObject *py_retlist = PyList_New(0);
PyObject *py_tuple = NULL;
PyObject *py_address = NULL;
PyObject *py_mac_address = NULL;


// allocate a 15 KB buffer to start with
Expand Down Expand Up @@ -3060,6 +3062,43 @@ psutil_net_if_addrs(PyObject *self, PyObject *args)
pCurrAddresses = pAddresses;
while (pCurrAddresses) {
pUnicast = pCurrAddresses->FirstUnicastAddress;
sprintf(ifname, "%wS", pCurrAddresses->FriendlyName);

// MAC address
if (pCurrAddresses->PhysicalAddressLength != 0) {
ptr = buff;
*ptr = '\0';
for (i = 0; i < (int) pCurrAddresses->PhysicalAddressLength; i++) {
if (i == (pCurrAddresses->PhysicalAddressLength - 1)) {
sprintf(ptr, "%.2X\n",
(int)pCurrAddresses->PhysicalAddress[i]);
}
else {
sprintf(ptr, "%.2X-",
(int)pCurrAddresses->PhysicalAddress[i]);
}
ptr += 3;
}
*--ptr = '\0';

py_mac_address = PyString_FromString(buff);
Py_INCREF(Py_None);
Py_INCREF(Py_None);
py_tuple = Py_BuildValue(
"(siOOO)",
ifname,
-1, // this will be converted later to AF_LINK
py_mac_address,
Py_None,
Py_None
);
if (! py_tuple)
goto error;
if (PyList_Append(py_retlist, py_tuple))
goto error;
Py_DECREF(py_tuple);
Py_DECREF(py_mac_address);
}

// find out the IP address associated with the NIC
if (pUnicast != NULL) {
Expand Down Expand Up @@ -3087,7 +3126,6 @@ psutil_net_if_addrs(PyObject *self, PyObject *args)
PyErr_SetFromWindowsErr(GetLastError());
goto error;
}
sprintf(ifname, "%wS", pCurrAddresses->FriendlyName);
py_address = PyString_FromString(buff);
if (py_address == NULL)
goto error;
Expand Down

0 comments on commit b1189a3

Please sign in to comment.