Debian Codegraph @

psutil/arch/openbsd/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
8 #include <Python.h>
9 #include <sys/sysctl.h>
10 #include <sys/socket.h>
11 #include <kvm.h>
12 #define _KERNEL // silence compiler warning
13 #include <sys/file.h> // DTYPE_SOCKET
14 #include <netdb.h> // INET6_ADDRSTRLEN, in6_addr
15 #undef _KERNEL
16
17 #include "../../arch/all/init.h"
18
19
20 PyObject *
21 psutil_net_connections(PyObject *self, PyObject *args) {
22 pid_t pid;
23 int i;
24 int cnt;
25 int state;
26 int lport;
27 int rport;
28 char lip[INET6_ADDRSTRLEN];
29 char rip[INET6_ADDRSTRLEN];
30 int inseq;
31
32 char errbuf[_POSIX2_LINE_MAX];
33 kvm_t *kd = NULL;
34
35 struct kinfo_file *kif;
36 struct kinfo_file *ikf;
37 struct in6_addr laddr6;
38
39 PyObject *py_retlist = PyList_New(0);
40 PyObject *py_tuple = NULL;
41 PyObject *py_laddr = NULL;
42 PyObject *py_raddr = NULL;
43 PyObject *py_lpath = NULL;
44 PyObject *py_af_filter = NULL;
45 PyObject *py_type_filter = NULL;
46 PyObject *py_family = NULL;
47 PyObject *_type = NULL;
48
49
50 if (py_retlist == NULL)
51 return NULL;
52 if (! PyArg_ParseTuple(args, _Py_PARSE_PID "OO", &pid, &py_af_filter,
53 &py_type_filter)) {
54 goto error;
55 }
56 if (!PySequence_Check(py_af_filter) || !PySequence_Check(py_type_filter)) {
57 PyErr_SetString(PyExc_TypeError, "arg 2 or 3 is not a sequence");
58 goto error;
59 }
60
61 kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
62 if (! kd) {
63 convert_kvm_err("kvm_openfiles", errbuf);
64 goto error;
65 }
66
67 ikf = kvm_getfiles(kd, KERN_FILE_BYPID, -1, sizeof(*ikf), &cnt);
68 if (! ikf) {
69 psutil_PyErr_SetFromOSErrnoWithSyscall("kvm_getfiles");
70 goto error;
71 }
72
73 for (int i = 0; i < cnt; i++) {
74 const struct kinfo_file *kif = ikf + i;
75 py_tuple = NULL;
76 py_laddr = NULL;
77 py_raddr = NULL;
78 py_lpath = NULL;
79
80 // apply filters
81 if (kif->f_type != DTYPE_SOCKET)
82 continue;
83 if (pid != -1 && kif->p_pid != (uint32_t)pid)
84 continue;
85 py_family = PyLong_FromLong((long)kif->so_family);
86 inseq = PySequence_Contains(py_af_filter, py_family);
87 Py_DECREF(py_family);
88 if (inseq == 0)
89 continue;
90 _type = PyLong_FromLong((long)kif->so_type);
91 inseq = PySequence_Contains(py_type_filter, _type);
92 Py_DECREF(_type);
93 if (inseq == 0)
94 continue;
95
96 // IPv4 / IPv6 socket
97 if ((kif->so_family == AF_INET) || (kif->so_family == AF_INET6)) {
98 // status
99 if (kif->so_type == SOCK_STREAM)
100 state = kif->t_state;
101 else
102 state = PSUTIL_CONN_NONE;
103
104 // local & remote port
105 lport = ntohs(kif->inp_lport);
106 rport = ntohs(kif->inp_fport);
107
108 // local addr
109 inet_ntop(kif->so_family, &kif->inp_laddru, lip, sizeof(lip));
110 py_laddr = Py_BuildValue("(si)", lip, lport);
111 if (! py_laddr)
112 goto error;
113
114 // remote addr
115 if (rport != 0) {
116 inet_ntop(kif->so_family, &kif->inp_faddru, rip, sizeof(rip));
117 py_raddr = Py_BuildValue("(si)", rip, rport);
118 }
119 else {
120 py_raddr = Py_BuildValue("()");
121 }
122 if (! py_raddr)
123 goto error;
124
125 // populate tuple and list
126 py_tuple = Py_BuildValue(
127 "(iiiNNil)",
128 kif->fd_fd,
129 kif->so_family,
130 kif->so_type,
131 py_laddr,
132 py_raddr,
133 state,
134 kif->p_pid
135 );
136 if (! py_tuple)
137 goto error;
138 if (PyList_Append(py_retlist, py_tuple))
139 goto error;
140 Py_DECREF(py_tuple);
141 }
142 // UNIX socket
143 else if (kif->so_family == AF_UNIX) {
144 py_lpath = PyUnicode_DecodeFSDefault(kif->unp_path);
145 if (! py_lpath)
146 goto error;
147
148 py_tuple = Py_BuildValue(
149 "(iiiOsil)",
150 kif->fd_fd,
151 kif->so_family,
152 kif->so_type,
153 py_lpath,
154 "", // raddr
155 PSUTIL_CONN_NONE,
156 kif->p_pid
157 );
158 if (! py_tuple)
159 goto error;
160 if (PyList_Append(py_retlist, py_tuple))
161 goto error;
162 Py_DECREF(py_lpath);
163 Py_DECREF(py_tuple);
164 Py_INCREF(Py_None);
165 }
166 }
167
168 kvm_close(kd);
169 return py_retlist;
170
171 error:
172 Py_XDECREF(py_tuple);
173 Py_XDECREF(py_laddr);
174 Py_XDECREF(py_raddr);
175 Py_DECREF(py_retlist);
176 if (kd != NULL)
177 kvm_close(kd);
178 return NULL;
179 }