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

Added getting IPv4 netmask in Windows 7 and above #874

Merged
merged 2 commits into from
Sep 23, 2016
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
36 changes: 34 additions & 2 deletions psutil/_psutil_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -2911,9 +2911,18 @@ psutil_net_if_addrs(PyObject *self, PyObject *args) {
unsigned int i = 0;
ULONG family;
PCTSTR intRet;
PCTSTR netmaskIntRet;
char *ptr;
char buff[100];
DWORD bufflen = 100;
char netmask_buff[100];
DWORD netmask_bufflen = 100;
DWORD dwRetVal = 0;
#if (_WIN32_WINNT >= 0x0600) // Windows Vista and above
ULONG converted_netmask;
UINT netmask_bits;
struct in_addr in_netmask;
#endif
PIP_ADAPTER_ADDRESSES pAddresses = NULL;
PIP_ADAPTER_ADDRESSES pCurrAddresses = NULL;
PIP_ADAPTER_UNICAST_ADDRESS pUnicast = NULL;
Expand All @@ -2923,6 +2932,7 @@ psutil_net_if_addrs(PyObject *self, PyObject *args) {
PyObject *py_address = NULL;
PyObject *py_mac_address = NULL;
PyObject *py_nic_name = NULL;
PyObject *py_netmask = NULL;

if (py_retlist == NULL)
return NULL;
Expand All @@ -2935,6 +2945,7 @@ psutil_net_if_addrs(PyObject *self, PyObject *args) {
while (pCurrAddresses) {
pUnicast = pCurrAddresses->FirstUnicastAddress;

netmaskIntRet = NULL;
py_nic_name = NULL;
py_nic_name = PyUnicode_FromWideChar(
pCurrAddresses->FriendlyName,
Expand Down Expand Up @@ -2996,6 +3007,15 @@ psutil_net_if_addrs(PyObject *self, PyObject *args) {
pUnicast->Address.lpSockaddr;
intRet = inet_ntop(AF_INET, &(sa_in->sin_addr), buff,
bufflen);
#if (_WIN32_WINNT >= 0x0600) // Windows Vista and above
netmask_bits = pUnicast->OnLinkPrefixLength;
dwRetVal = ConvertLengthToIpv4Mask(netmask_bits, &converted_netmask);
if (dwRetVal == NO_ERROR) {
in_netmask.s_addr = converted_netmask;
netmaskIntRet = inet_ntop(AF_INET, &in_netmask, netmask_buff,
netmask_bufflen);
}
#endif
}
else if (family == AF_INET6) {
struct sockaddr_in6 *sa_in6 = (struct sockaddr_in6 *)
Expand All @@ -3021,15 +3041,25 @@ psutil_net_if_addrs(PyObject *self, PyObject *args) {
if (py_address == NULL)
goto error;

Py_INCREF(Py_None);
if (netmaskIntRet != NULL) {
#if PY_MAJOR_VERSION >= 3
py_netmask = PyUnicode_FromString(netmask_buff);
#else
py_netmask = PyString_FromString(netmask_buff);
#endif
} else {
Py_INCREF(Py_None);
py_netmask = Py_None;
}

Py_INCREF(Py_None);
Py_INCREF(Py_None);
py_tuple = Py_BuildValue(
"(OiOOOO)",
py_nic_name,
family,
py_address,
Py_None, // netmask (not supported)
py_netmask,
Py_None, // broadcast (not supported)
Py_None // ptp (not supported on Windows)
);
Expand All @@ -3040,6 +3070,7 @@ psutil_net_if_addrs(PyObject *self, PyObject *args) {
goto error;
Py_DECREF(py_tuple);
Py_DECREF(py_address);
Py_DECREF(py_netmask);

pUnicast = pUnicast->Next;
}
Expand All @@ -3058,6 +3089,7 @@ psutil_net_if_addrs(PyObject *self, PyObject *args) {
Py_XDECREF(py_tuple);
Py_XDECREF(py_address);
Py_XDECREF(py_nic_name);
Py_XDECREF(py_netmask);
return NULL;
}

Expand Down