Debian Codegraph @

psutil/arch/freebsd/sys_socks.c

raw
1 /*
2 * Copyright (c) 2009, Giampaolo Rodola'.
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 * Retrieves system-wide open socket connections. This is based off of
8 * sockstat utility source code:
9 * https://github.com/freebsd/freebsd/blob/master/usr.bin/sockstat/sockstat.c
10 */
11
12 #include <Python.h>
13 #include <sys/param.h>
14 #include <sys/user.h>
15 #include <sys/file.h>
16 #include <sys/socketvar.h> // for struct xsocket
17 #include <sys/un.h>
18 #include <sys/unpcb.h>
19 #include <sys/sysctl.h>
20 #include <netinet/in.h> // for xinpcb struct
21 #include <netinet/ip.h>
22 #include <netinet/in_pcb.h>
23 #include <netinet/tcp_var.h> // for struct xtcpcb
24 #include <arpa/inet.h> // for inet_ntop()
25
26 #include "../../arch/all/init.h"
27
28
29 static int
30 psutil_populate_xfiles(struct xfile **psutil_xfiles, int *psutil_nxfiles) {
31 struct xfile *new_psutil_xfiles;
32 size_t len = sizeof(struct xfile);
33
34 while (sysctlbyname("kern.file", *psutil_xfiles, &len, 0, 0) == -1) {
35 if (errno != ENOMEM) {
36 PyErr_SetFromErrno(0);
37 return 0;
38 }
39 len *= 2;
40 new_psutil_xfiles = realloc(*psutil_xfiles, len);
41 if (new_psutil_xfiles == NULL) {
42 PyErr_NoMemory();
43 return 0;
44 }
45 *psutil_xfiles = new_psutil_xfiles;
46 }
47 if (len > 0 && (*psutil_xfiles)->xf_size != sizeof(struct xfile)) {
48 PyErr_Format(PyExc_RuntimeError, "struct xfile size mismatch");
49 return 0;
50 }
51 *psutil_nxfiles = len / sizeof(struct xfile);
52 return 1;
53 }
54
55
56 static struct xfile *
57 psutil_get_file_from_sock(kvaddr_t sock, struct xfile *psutil_xfiles, int psutil_nxfiles) {
58 struct xfile *xf;
59 int n;
60
61 for (xf = psutil_xfiles, n = 0; n < psutil_nxfiles; ++n, ++xf) {
62 if (xf->xf_data == sock)
63 return xf;
64 }
65 return NULL;
66 }
67
68
69 // Reference:
70 // https://github.com/freebsd/freebsd/blob/master/usr.bin/sockstat/sockstat.c
71 static int
72 psutil_gather_inet(
73 int proto, int include_v4, int include_v6, PyObject *py_retlist,
74 struct xfile *psutil_xfiles, int psutil_nxfiles)
75 {
76 struct xinpgen *xig, *exig;
77 struct xinpcb *xip;
78 struct xtcpcb *xtp;
79 #if __FreeBSD_version >= 1200026
80 struct xinpcb *inp;
81 #else
82 struct inpcb *inp;
83 #endif
84 struct xsocket *so;
85 const char *varname = NULL;
86 size_t len, bufsize;
87 void *buf;
88 int retry;
89 int type;
90
91 PyObject *py_tuple = NULL;
92 PyObject *py_laddr = NULL;
93 PyObject *py_raddr = NULL;
94
95 switch (proto) {
96 case IPPROTO_TCP:
97 varname = "net.inet.tcp.pcblist";
98 type = SOCK_STREAM;
99 break;
100 case IPPROTO_UDP:
101 varname = "net.inet.udp.pcblist";
102 type = SOCK_DGRAM;
103 break;
104 }
105
106 buf = NULL;
107 bufsize = 8192;
108 retry = 5;
109 do {
110 for (;;) {
111 buf = realloc(buf, bufsize);
112 if (buf == NULL)
113 continue; // XXX
114 len = bufsize;
115 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
116 break;
117 if (errno != ENOMEM) {
118 PyErr_SetFromErrno(0);
119 goto error;
120 }
121 bufsize *= 2;
122 }
123 xig = (struct xinpgen *)buf;
124 exig = (struct xinpgen *)(void *)((char *)buf + len - sizeof *exig);
125 if (xig->xig_len != sizeof *xig || exig->xig_len != sizeof *exig) {
126 PyErr_Format(PyExc_RuntimeError, "struct xinpgen size mismatch");
127 goto error;
128 }
129 } while (xig->xig_gen != exig->xig_gen && retry--);
130
131 for (;;) {
132 struct xfile *xf;
133 int lport, rport, status, family;
134
135 xig = (struct xinpgen *)(void *)((char *)xig + xig->xig_len);
136 if (xig >= exig)
137 break;
138
139 switch (proto) {
140 case IPPROTO_TCP:
141 xtp = (struct xtcpcb *)xig;
142 if (xtp->xt_len != sizeof *xtp) {
143 PyErr_Format(PyExc_RuntimeError,
144 "struct xtcpcb size mismatch");
145 goto error;
146 }
147 inp = &xtp->xt_inp;
148 #if __FreeBSD_version >= 1200026
149 so = &inp->xi_socket;
150 status = xtp->t_state;
151 #else
152 so = &xtp->xt_socket;
153 status = xtp->xt_tp.t_state;
154 #endif
155 break;
156 case IPPROTO_UDP:
157 xip = (struct xinpcb *)xig;
158 if (xip->xi_len != sizeof *xip) {
159 PyErr_Format(PyExc_RuntimeError,
160 "struct xinpcb size mismatch");
161 goto error;
162 }
163 #if __FreeBSD_version >= 1200026
164 inp = xip;
165 #else
166 inp = &xip->xi_inp;
167 #endif
168 so = &xip->xi_socket;
169 status = PSUTIL_CONN_NONE;
170 break;
171 default:
172 PyErr_Format(PyExc_RuntimeError, "invalid proto");
173 goto error;
174 }
175
176 // filter
177 if ((inp->inp_vflag & INP_IPV4) && (include_v4 == 0))
178 continue;
179 if ((inp->inp_vflag & INP_IPV6) && (include_v6 == 0))
180 continue;
181
182 char lip[200], rip[200];
183
184 xf = psutil_get_file_from_sock(so->xso_so, psutil_xfiles, psutil_nxfiles);
185 if (xf == NULL)
186 continue;
187 lport = ntohs(inp->inp_lport);
188 rport = ntohs(inp->inp_fport);
189
190 if (inp->inp_vflag & INP_IPV4) {
191 family = AF_INET;
192 inet_ntop(AF_INET, &inp->inp_laddr.s_addr, lip, sizeof(lip));
193 inet_ntop(AF_INET, &inp->inp_faddr.s_addr, rip, sizeof(rip));
194 }
195 else if (inp->inp_vflag & INP_IPV6) {
196 family = AF_INET6;
197 inet_ntop(AF_INET6, &inp->in6p_laddr.s6_addr, lip, sizeof(lip));
198 inet_ntop(AF_INET6, &inp->in6p_faddr.s6_addr, rip, sizeof(rip));
199 }
200
201 // construct python tuple/list
202 py_laddr = Py_BuildValue("(si)", lip, lport);
203 if (!py_laddr)
204 goto error;
205 if (rport != 0)
206 py_raddr = Py_BuildValue("(si)", rip, rport);
207 else
208 py_raddr = Py_BuildValue("()");
209 if (!py_raddr)
210 goto error;
211 py_tuple = Py_BuildValue(
212 "iiiNNi" _Py_PARSE_PID,
213 xf->xf_fd, // fd
214 family, // family
215 type, // type
216 py_laddr, // laddr
217 py_raddr, // raddr
218 status, // status
219 xf->xf_pid // pid
220 );
221 if (!py_tuple)
222 goto error;
223 if (PyList_Append(py_retlist, py_tuple))
224 goto error;
225 Py_CLEAR(py_tuple);
226 }
227
228 free(buf);
229 return 1;
230
231 error:
232 Py_XDECREF(py_tuple);
233 Py_XDECREF(py_laddr);
234 Py_XDECREF(py_raddr);
235 free(buf);
236 return 0;
237 }
238
239
240 static int
241 psutil_gather_unix(int proto, PyObject *py_retlist,
242 struct xfile *psutil_xfiles, int psutil_nxfiles) {
243 struct xunpgen *xug, *exug;
244 struct xunpcb *xup;
245 const char *varname = NULL;
246 const char *protoname = NULL;
247 size_t len;
248 size_t bufsize;
249 void *buf;
250 int retry;
251 struct sockaddr_un *sun;
252 char path[PATH_MAX];
253
254 PyObject *py_tuple = NULL;
255 PyObject *py_lpath = NULL;
256
257 switch (proto) {
258 case SOCK_STREAM:
259 varname = "net.local.stream.pcblist";
260 protoname = "stream";
261 break;
262 case SOCK_DGRAM:
263 varname = "net.local.dgram.pcblist";
264 protoname = "dgram";
265 break;
266 }
267
268 buf = NULL;
269 bufsize = 8192;
270 retry = 5;
271
272 do {
273 for (;;) {
274 buf = realloc(buf, bufsize);
275 if (buf == NULL) {
276 PyErr_NoMemory();
277 goto error;
278 }
279 len = bufsize;
280 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
281 break;
282 if (errno != ENOMEM) {
283 PyErr_SetFromErrno(0);
284 goto error;
285 }
286 bufsize *= 2;
287 }
288 xug = (struct xunpgen *)buf;
289 exug = (struct xunpgen *)(void *)
290 ((char *)buf + len - sizeof *exug);
291 if (xug->xug_len != sizeof *xug || exug->xug_len != sizeof *exug) {
292 PyErr_Format(PyExc_RuntimeError, "struct xinpgen size mismatch");
293 goto error;
294 }
295 } while (xug->xug_gen != exug->xug_gen && retry--);
296
297 for (;;) {
298 struct xfile *xf;
299
300 xug = (struct xunpgen *)(void *)((char *)xug + xug->xug_len);
301 if (xug >= exug)
302 break;
303 xup = (struct xunpcb *)xug;
304 if (xup->xu_len != sizeof *xup)
305 goto error;
306
307 xf = psutil_get_file_from_sock(xup->xu_socket.xso_so, psutil_xfiles, psutil_nxfiles);
308 if (xf == NULL)
309 continue;
310
311 sun = (struct sockaddr_un *)&xup->xu_addr;
312 snprintf(path, sizeof(path), "%.*s",
313 (int)(sun->sun_len - (sizeof(*sun) - sizeof(sun->sun_path))),
314 sun->sun_path);
315 py_lpath = PyUnicode_DecodeFSDefault(path);
316 if (! py_lpath)
317 goto error;
318
319 py_tuple = Py_BuildValue("(iiiOsii)",
320 xf->xf_fd, // fd
321 AF_UNIX, // family
322 proto, // type
323 py_lpath, // lpath
324 "", // rath
325 PSUTIL_CONN_NONE, // status
326 xf->xf_pid); // pid
327 if (!py_tuple)
328 goto error;
329 if (PyList_Append(py_retlist, py_tuple))
330 goto error;
331 Py_DECREF(py_lpath);
332 Py_DECREF(py_tuple);
333 }
334
335 free(buf);
336 return 1;
337
338 error:
339 Py_XDECREF(py_tuple);
340 Py_XDECREF(py_lpath);
341 free(buf);
342 return 0;
343 }
344
345
346 static int
347 psutil_int_in_seq(int value, PyObject *py_seq) {
348 int inseq;
349 PyObject *py_value;
350
351 py_value = PyLong_FromLong((long)value);
352 if (py_value == NULL)
353 return -1;
354 inseq = PySequence_Contains(py_seq, py_value); // return -1 on failure
355 Py_DECREF(py_value);
356 return inseq;
357 }
358
359
360 PyObject*
361 psutil_net_connections(PyObject* self, PyObject* args) {
362 int include_v4, include_v6, include_unix, include_tcp, include_udp, psutil_nxfiles;
363 struct xfile *psutil_xfiles;
364 PyObject *py_af_filter = NULL;
365 PyObject *py_type_filter = NULL;
366 PyObject *py_retlist = PyList_New(0);
367
368 if (py_retlist == NULL)
369 return NULL;
370 if (! PyArg_ParseTuple(args, "OO", &py_af_filter, &py_type_filter)) {
371 goto error;
372 }
373 if (!PySequence_Check(py_af_filter) || !PySequence_Check(py_type_filter)) {
374 PyErr_SetString(PyExc_TypeError, "arg 2 or 3 is not a sequence");
375 goto error;
376 }
377
378 if ((include_v4 = psutil_int_in_seq(AF_INET, py_af_filter)) == -1)
379 goto error;
380 if ((include_v6 = psutil_int_in_seq(AF_INET6, py_af_filter)) == -1)
381 goto error;
382 if ((include_unix = psutil_int_in_seq(AF_UNIX, py_af_filter)) == -1)
383 goto error;
384 if ((include_tcp = psutil_int_in_seq(SOCK_STREAM, py_type_filter)) == -1)
385 goto error;
386 if ((include_udp = psutil_int_in_seq(SOCK_DGRAM, py_type_filter)) == -1)
387 goto error;
388
389 psutil_xfiles = malloc(sizeof(struct xfile));
390 if (psutil_xfiles == NULL) {
391 PyErr_NoMemory();
392 goto error;
393 }
394
395 if (psutil_populate_xfiles(&psutil_xfiles, &psutil_nxfiles) != 1)
396 goto error_free_psutil_xfiles;
397
398 // TCP
399 if (include_tcp == 1) {
400 if (psutil_gather_inet(
401 IPPROTO_TCP, include_v4, include_v6, py_retlist,
402 psutil_xfiles, psutil_nxfiles) == 0)
403 {
404 goto error_free_psutil_xfiles;
405 }
406 }
407 // UDP
408 if (include_udp == 1) {
409 if (psutil_gather_inet(
410 IPPROTO_UDP, include_v4, include_v6, py_retlist,
411 psutil_xfiles, psutil_nxfiles) == 0)
412 {
413 goto error_free_psutil_xfiles;
414 }
415 }
416 // UNIX
417 if (include_unix == 1) {
418 if (psutil_gather_unix(SOCK_STREAM, py_retlist, psutil_xfiles, psutil_nxfiles) == 0)
419 goto error_free_psutil_xfiles;
420 if (psutil_gather_unix(SOCK_DGRAM, py_retlist, psutil_xfiles, psutil_nxfiles) == 0)
421 goto error_free_psutil_xfiles;
422 }
423
424 free(psutil_xfiles);
425 return py_retlist;
426
427 error_free_psutil_xfiles:
428 free(psutil_xfiles);
429 error:
430 Py_DECREF(py_retlist);
431 return NULL;
432 }