diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2015-11-08 13:16:12 +0100 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2015-11-08 13:16:12 +0100 |
commit | de979004d8e4eb314a7564ead62afec95adf7780 (patch) | |
tree | 5b751458ca0b20c8b5c40d3d6fb5b6696f0532f7 | |
parent | 774834592b4c973d460063150f931b0ee7d806d8 (diff) | |
download | psutil-de979004d8e4eb314a7564ead62afec95adf7780.tar.gz |
#615: open_connections() IPv6 addr
-rw-r--r-- | psutil/_psbsd.py | 2 | ||||
-rw-r--r-- | psutil/_psutil_openbsd.c | 78 | ||||
-rw-r--r-- | test/test_psutil.py | 4 |
3 files changed, 49 insertions, 35 deletions
diff --git a/psutil/_psbsd.py b/psutil/_psbsd.py index 7b7b1d2c..c864f206 100644 --- a/psutil/_psbsd.py +++ b/psutil/_psbsd.py @@ -202,7 +202,7 @@ def net_connections(kind): ret = [] for pid in pids(): try: - cons = Process(pid).connections() + cons = Process(pid).connections(kind) except NoSuchProcess: continue else: diff --git a/psutil/_psutil_openbsd.c b/psutil/_psutil_openbsd.c index 25a2ca91..d7ad4c13 100644 --- a/psutil/_psutil_openbsd.c +++ b/psutil/_psutil_openbsd.c @@ -701,24 +701,15 @@ psutil_proc_cwd(PyObject *self, PyObject *args) { // see sys/kern/kern_sysctl.c lines 1100 and // usr.bin/fstat/fstat.c print_inet_details() char * -psutil_addr_from_addru(int family, uint32_t addr[4]) { +psutil_convert_ipv4(int family, uint32_t addr[4]) { struct in_addr a; memcpy(&a, addr, sizeof(a)); - if (family == AF_INET) { - if (a.s_addr == INADDR_ANY) - return "*"; - else - return inet_ntoa(a); - } - else { - /* XXX TODO */ - return NULL; - } + return inet_ntoa(a); } const char * -inet6_addrstr(struct in6_addr *p) +psutil_inet6_addrstr(struct in6_addr *p) { struct sockaddr_in6 sin6; static char hbuf[NI_MAXHOST]; @@ -770,9 +761,8 @@ psutil_proc_connections(PyObject *self, PyObject *args) { if (py_retlist == NULL) return NULL; - if (! PyArg_ParseTuple(args, "lOO", &pid, &py_af_filter, &py_type_filter)) { + if (! PyArg_ParseTuple(args, "lOO", &pid, &py_af_filter, &py_type_filter)) goto error; - } if (!PySequence_Check(py_af_filter) || !PySequence_Check(py_type_filter)) { PyErr_SetString(PyExc_TypeError, "arg 2 or 3 is not a sequence"); goto error; @@ -789,7 +779,9 @@ psutil_proc_connections(PyObject *self, PyObject *args) { int lport; int rport; char path[PATH_MAX]; + char addrbuf[NI_MAXHOST + 2]; int inseq; + struct in6_addr laddr6; py_tuple = NULL; py_laddr = NULL; py_raddr = NULL; @@ -800,19 +792,16 @@ psutil_proc_connections(PyObject *self, PyObject *args) { py_family = PyLong_FromLong((long)kif->so_family); inseq = PySequence_Contains(py_af_filter, py_family); Py_DECREF(py_family); - if (inseq == 0) { + if (inseq == 0) continue; - } _type = PyLong_FromLong((long)kif->so_type); inseq = PySequence_Contains(py_type_filter, _type); Py_DECREF(_type); - if (inseq == 0) { + if (inseq == 0) continue; - } // IPv4 / IPv6 socket - if ((kif->so_family == AF_INET) || - (kif->so_family == AF_INET6)) { + if ((kif->so_family == AF_INET) || (kif->so_family == AF_INET6)) { // fill status if (kif->so_type == SOCK_STREAM) state = kif->t_state; @@ -823,19 +812,45 @@ psutil_proc_connections(PyObject *self, PyObject *args) { lport = ntohs(kif->inp_lport); rport = ntohs(kif->inp_fport); - // construct python tuple/list - py_laddr = Py_BuildValue( - "(si)", - psutil_addr_from_addru(kif->so_family, kif->inp_laddru), - lport); - if (!py_laddr) - goto error; + // local address, IPv4 + if (kif->so_family == AF_INET) { + py_laddr = Py_BuildValue( + "(si)", + psutil_convert_ipv4(kif->so_family, kif->inp_laddru), + lport); + if (!py_laddr) + goto error; + } + else { + // local address, IPv6 + memcpy(&laddr6, kif->inp_laddru, sizeof(laddr6)); + (void *)(uintptr_t)kif->inp_ppcb; + snprintf(addrbuf, sizeof(addrbuf), "%s", + psutil_inet6_addrstr(&laddr6)); + py_laddr = Py_BuildValue("(si)", addrbuf, lport); + if (!py_laddr) + goto error; + } if (rport != 0) { - py_raddr = Py_BuildValue( - "(si)", - psutil_addr_from_addru(kif->so_family, kif->inp_faddru), - ntohs(kif->inp_fport)); + // remote address, IPv4 + if (kif->so_family == AF_INET) { + py_raddr = Py_BuildValue( + "(si)", + psutil_convert_ipv4( + kif->so_family, kif->inp_faddru), + rport); + } + else { + // remote address, IPv6 + memcpy(&laddr6, kif->inp_faddru, sizeof(laddr6)); + (void *)(uintptr_t)kif->inp_ppcb; + snprintf(addrbuf, sizeof(addrbuf), "%s", + psutil_inet6_addrstr(&laddr6)); + py_raddr = Py_BuildValue("(si)", addrbuf, rport); + if (!py_raddr) + goto error; + } } else { py_raddr = Py_BuildValue("()"); @@ -859,7 +874,6 @@ psutil_proc_connections(PyObject *self, PyObject *args) { } // UNIX socket else if (kif->so_family == AF_UNIX) { - py_tuple = Py_BuildValue( "(iiisOi)", kif->fd_fd, diff --git a/test/test_psutil.py b/test/test_psutil.py index 2f5a6d59..14478ff2 100644 --- a/test/test_psutil.py +++ b/test/test_psutil.py @@ -1937,8 +1937,8 @@ class TestProcess(unittest.TestCase): for c in psutil.net_connections(kind='all'): if c.pid == pid: sys_cons.append(pconn(*c[:-1])) - if BSD: - # on BSD all fds are set to -1 + if FREEBSD: + # on FreeBSD all fds are set to -1 proc_cons = [pconn(*[-1] + list(x[1:])) for x in proc_cons] self.assertEqual(sorted(proc_cons), sorted(sys_cons)) |