| 1 |
/* |
| 2 |
* Copyright (c) 2017, Arnon Yaari |
| 3 |
* All rights reserved. |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
/* Baded on code from lsof: |
| 9 |
* http://www.ibm.com/developerworks/aix/library/au-lsof.html |
| 10 |
* - dialects/aix/dproc.c:gather_proc_info |
| 11 |
* - lib/prfp.c:process_file |
| 12 |
* - dialects/aix/dsock.c:process_socket |
| 13 |
* - dialects/aix/dproc.c:get_kernel_access |
| 14 |
*/ |
| 15 |
|
| 16 |
#include <Python.h> |
| 17 |
#include <stdlib.h> |
| 18 |
#include <fcntl.h> |
| 19 |
#define _KERNEL |
| 20 |
#include <sys/file.h> |
| 21 |
#undef _KERNEL |
| 22 |
#include <sys/types.h> |
| 23 |
#include <sys/core.h> |
| 24 |
#include <sys/domain.h> |
| 25 |
#include <sys/un.h> |
| 26 |
#include <netinet/in_pcb.h> |
| 27 |
#include <arpa/inet.h> |
| 28 |
|
| 29 |
#include "../../arch/all/init.h" |
| 30 |
#include "net_kernel_structs.h" |
| 31 |
#include "net_connections.h" |
| 32 |
#include "common.h" |
| 33 |
|
| 34 |
|
| 35 |
#define NO_SOCKET (PyObject *)(-1) |
| 36 |
|
| 37 |
static int |
| 38 |
read_unp_addr( |
| 39 |
int Kd, |
| 40 |
KA_T unp_addr, |
| 41 |
char *buf, |
| 42 |
size_t buflen |
| 43 |
) { |
| 44 |
struct sockaddr_un *ua = (struct sockaddr_un *)NULL; |
| 45 |
struct sockaddr_un un; |
| 46 |
struct mbuf64 mb; |
| 47 |
int uo; |
| 48 |
|
| 49 |
if (psutil_kread(Kd, unp_addr, (char *)&mb, sizeof(mb))) { |
| 50 |
return 1; |
| 51 |
} |
| 52 |
|
| 53 |
uo = (int)(mb.m_hdr.mh_data - unp_addr); |
| 54 |
if ((uo + sizeof(struct sockaddr)) <= sizeof(mb)) |
| 55 |
ua = (struct sockaddr_un *)((char *)&mb + uo); |
| 56 |
else { |
| 57 |
if (psutil_kread(Kd, (KA_T)mb.m_hdr.mh_data, |
| 58 |
(char *)&un, sizeof(un))) { |
| 59 |
return 1; |
| 60 |
} |
| 61 |
ua = &un; |
| 62 |
} |
| 63 |
if (ua && ua->sun_path[0]) { |
| 64 |
if (mb.m_len > sizeof(struct sockaddr_un)) |
| 65 |
mb.m_len = sizeof(struct sockaddr_un); |
| 66 |
*((char *)ua + mb.m_len - 1) = '\0'; |
| 67 |
snprintf(buf, buflen, "%s", ua->sun_path); |
| 68 |
} |
| 69 |
return 0; |
| 70 |
} |
| 71 |
|
| 72 |
static PyObject * |
| 73 |
process_file(int Kd, pid32_t pid, int fd, KA_T fp) { |
| 74 |
struct file64 f; |
| 75 |
struct socket64 s; |
| 76 |
struct protosw64 p; |
| 77 |
struct domain d; |
| 78 |
struct inpcb64 inp; |
| 79 |
int fam; |
| 80 |
struct tcpcb64 t; |
| 81 |
int state = PSUTIL_CONN_NONE; |
| 82 |
unsigned char *laddr = (unsigned char *)NULL; |
| 83 |
unsigned char *raddr = (unsigned char *)NULL; |
| 84 |
int rport, lport; |
| 85 |
char laddr_str[INET6_ADDRSTRLEN]; |
| 86 |
char raddr_str[INET6_ADDRSTRLEN]; |
| 87 |
struct unpcb64 unp; |
| 88 |
char unix_laddr_str[PATH_MAX] = { 0 }; |
| 89 |
char unix_raddr_str[PATH_MAX] = { 0 }; |
| 90 |
|
| 91 |
/* Read file structure */ |
| 92 |
if (psutil_kread(Kd, fp, (char *)&f, sizeof(f))) { |
| 93 |
return NULL; |
| 94 |
} |
| 95 |
if (!f.f_count || f.f_type != DTYPE_SOCKET) { |
| 96 |
return NO_SOCKET; |
| 97 |
} |
| 98 |
|
| 99 |
if (psutil_kread(Kd, (KA_T) f.f_data, (char *) &s, sizeof(s))) { |
| 100 |
return NULL; |
| 101 |
} |
| 102 |
|
| 103 |
if (!s.so_type) { |
| 104 |
return NO_SOCKET; |
| 105 |
} |
| 106 |
|
| 107 |
if (!s.so_proto) { |
| 108 |
PyErr_SetString(PyExc_RuntimeError, "invalid socket protocol handle"); |
| 109 |
return NULL; |
| 110 |
} |
| 111 |
if (psutil_kread(Kd, (KA_T)s.so_proto, (char *)&p, sizeof(p))) { |
| 112 |
return NULL; |
| 113 |
} |
| 114 |
|
| 115 |
if (!p.pr_domain) { |
| 116 |
PyErr_SetString(PyExc_RuntimeError, "invalid socket protocol domain"); |
| 117 |
return NULL; |
| 118 |
} |
| 119 |
if (psutil_kread(Kd, (KA_T)p.pr_domain, (char *)&d, sizeof(d))) { |
| 120 |
return NULL; |
| 121 |
} |
| 122 |
|
| 123 |
fam = d.dom_family; |
| 124 |
if (fam == AF_INET || fam == AF_INET6) { |
| 125 |
/* Read protocol control block */ |
| 126 |
if (!s.so_pcb) { |
| 127 |
PyErr_SetString(PyExc_RuntimeError, "invalid socket PCB"); |
| 128 |
return NULL; |
| 129 |
} |
| 130 |
if (psutil_kread(Kd, (KA_T) s.so_pcb, (char *) &inp, sizeof(inp))) { |
| 131 |
return NULL; |
| 132 |
} |
| 133 |
|
| 134 |
if (p.pr_protocol == IPPROTO_TCP) { |
| 135 |
/* If this is a TCP socket, read its control block */ |
| 136 |
if (inp.inp_ppcb |
| 137 |
&& !psutil_kread(Kd, (KA_T)inp.inp_ppcb, |
| 138 |
(char *)&t, sizeof(t))) |
| 139 |
state = t.t_state; |
| 140 |
} |
| 141 |
|
| 142 |
if (fam == AF_INET6) { |
| 143 |
laddr = (unsigned char *)&inp.inp_laddr6; |
| 144 |
if (!IN6_IS_ADDR_UNSPECIFIED(&inp.inp_faddr6)) { |
| 145 |
raddr = (unsigned char *)&inp.inp_faddr6; |
| 146 |
rport = (int)ntohs(inp.inp_fport); |
| 147 |
} |
| 148 |
} |
| 149 |
if (fam == AF_INET) { |
| 150 |
laddr = (unsigned char *)&inp.inp_laddr; |
| 151 |
if (inp.inp_faddr.s_addr != INADDR_ANY || inp.inp_fport != 0) { |
| 152 |
raddr = (unsigned char *)&inp.inp_faddr; |
| 153 |
rport = (int)ntohs(inp.inp_fport); |
| 154 |
} |
| 155 |
} |
| 156 |
lport = (int)ntohs(inp.inp_lport); |
| 157 |
|
| 158 |
inet_ntop(fam, laddr, laddr_str, sizeof(laddr_str)); |
| 159 |
|
| 160 |
if (raddr != NULL) { |
| 161 |
inet_ntop(fam, raddr, raddr_str, sizeof(raddr_str)); |
| 162 |
return Py_BuildValue("(iii(si)(si)ii)", fd, fam, |
| 163 |
s.so_type, laddr_str, lport, raddr_str, |
| 164 |
rport, state, pid); |
| 165 |
} |
| 166 |
else { |
| 167 |
return Py_BuildValue("(iii(si)()ii)", fd, fam, |
| 168 |
s.so_type, laddr_str, lport, state, |
| 169 |
pid); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
if (fam == AF_UNIX) { |
| 175 |
if (psutil_kread(Kd, (KA_T) s.so_pcb, (char *)&unp, sizeof(unp))) { |
| 176 |
return NULL; |
| 177 |
} |
| 178 |
if ((KA_T) f.f_data != (KA_T) unp.unp_socket) { |
| 179 |
PyErr_SetString(PyExc_RuntimeError, "unp_socket mismatch"); |
| 180 |
return NULL; |
| 181 |
} |
| 182 |
|
| 183 |
if (unp.unp_addr) { |
| 184 |
if (read_unp_addr(Kd, unp.unp_addr, unix_laddr_str, |
| 185 |
sizeof(unix_laddr_str))) { |
| 186 |
return NULL; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
if (unp.unp_conn) { |
| 191 |
if (psutil_kread(Kd, (KA_T) unp.unp_conn, (char *)&unp, |
| 192 |
sizeof(unp))) { |
| 193 |
return NULL; |
| 194 |
} |
| 195 |
if (read_unp_addr(Kd, unp.unp_addr, unix_raddr_str, |
| 196 |
sizeof(unix_raddr_str))) { |
| 197 |
return NULL; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
return Py_BuildValue("(iiissii)", fd, d.dom_family, |
| 202 |
s.so_type, unix_laddr_str, unix_raddr_str, PSUTIL_CONN_NONE, |
| 203 |
pid); |
| 204 |
} |
| 205 |
return NO_SOCKET; |
| 206 |
} |
| 207 |
|
| 208 |
PyObject * |
| 209 |
psutil_net_connections(PyObject *self, PyObject *args) { |
| 210 |
PyObject *py_retlist = PyList_New(0); |
| 211 |
PyObject *py_tuple = NULL; |
| 212 |
KA_T fp; |
| 213 |
int Kd = -1; |
| 214 |
int i, np; |
| 215 |
struct procentry64 *p; |
| 216 |
struct fdsinfo64 *fds = (struct fdsinfo64 *)NULL; |
| 217 |
pid32_t requested_pid; |
| 218 |
pid32_t pid; |
| 219 |
struct procentry64 *processes = (struct procentry64 *)NULL; |
| 220 |
/* the process table */ |
| 221 |
|
| 222 |
if (py_retlist == NULL) |
| 223 |
goto error; |
| 224 |
if (! PyArg_ParseTuple(args, "i", &requested_pid)) |
| 225 |
goto error; |
| 226 |
|
| 227 |
Kd = open(KMEM, O_RDONLY, 0); |
| 228 |
if (Kd < 0) { |
| 229 |
PyErr_SetFromErrnoWithFilename(PyExc_OSError, KMEM); |
| 230 |
goto error; |
| 231 |
} |
| 232 |
|
| 233 |
processes = psutil_read_process_table(&np); |
| 234 |
if (!processes) |
| 235 |
goto error; |
| 236 |
|
| 237 |
/* Loop through processes */ |
| 238 |
for (p = processes; np > 0; np--, p++) { |
| 239 |
pid = p->pi_pid; |
| 240 |
if (requested_pid != -1 && requested_pid != pid) |
| 241 |
continue; |
| 242 |
if (p->pi_state == 0 || p->pi_state == SZOMB) |
| 243 |
continue; |
| 244 |
|
| 245 |
if (!fds) { |
| 246 |
fds = (struct fdsinfo64 *)malloc((size_t)FDSINFOSIZE); |
| 247 |
if (!fds) { |
| 248 |
PyErr_NoMemory(); |
| 249 |
goto error; |
| 250 |
} |
| 251 |
} |
| 252 |
if (getprocs64((struct procentry64 *)NULL, PROCSIZE, fds, FDSINFOSIZE, |
| 253 |
&pid, 1) |
| 254 |
!= 1) |
| 255 |
continue; |
| 256 |
|
| 257 |
/* loop over file descriptors */ |
| 258 |
for (i = 0; i < p->pi_maxofile; i++) { |
| 259 |
fp = (KA_T)fds->pi_ufd[i].fp; |
| 260 |
if (fp) { |
| 261 |
py_tuple = process_file(Kd, p->pi_pid, i, fp); |
| 262 |
if (py_tuple == NULL) |
| 263 |
goto error; |
| 264 |
if (py_tuple != NO_SOCKET) { |
| 265 |
if (PyList_Append(py_retlist, py_tuple)) |
| 266 |
goto error; |
| 267 |
Py_DECREF(py_tuple); |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
close(Kd); |
| 273 |
free(processes); |
| 274 |
if (fds != NULL) |
| 275 |
free(fds); |
| 276 |
return py_retlist; |
| 277 |
|
| 278 |
error: |
| 279 |
Py_XDECREF(py_tuple); |
| 280 |
Py_DECREF(py_retlist); |
| 281 |
if (Kd > 0) |
| 282 |
close(Kd); |
| 283 |
if (processes != NULL) |
| 284 |
free(processes); |
| 285 |
if (fds != NULL) |
| 286 |
free(fds); |
| 287 |
return NULL; |
| 288 |
} |