Debian Codegraph @

psutil/arch/windows/socks.c

raw
1 /*
2 * Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
7 // Fixes clash between winsock2.h and windows.h
8 #define WIN32_LEAN_AND_MEAN
9
10 #include <Python.h>
11 #include <windows.h>
12 #include <ws2tcpip.h>
13
14 #include "../../arch/all/init.h"
15
16
17 #define BYTESWAP_USHORT(x) ((((USHORT)(x) << 8) | ((USHORT)(x) >> 8)) & 0xffff)
18 #define STATUS_UNSUCCESSFUL 0xC0000001
19
20 // Note about GetExtended[Tcp|Udp]Table syscalls: due to other processes
21 // being active on the machine, it's possible that the size of the table
22 // increases between the moment we query the size and the moment we query
23 // the data. Therefore we retry if that happens. See:
24 // https://github.com/giampaolo/psutil/pull/1335
25 // https://github.com/giampaolo/psutil/issues/1294
26
27
28 static PVOID __GetExtendedTcpTable(ULONG family) {
29 DWORD err;
30 PVOID table;
31 ULONG size = 0;
32 TCP_TABLE_CLASS class = TCP_TABLE_OWNER_PID_ALL;
33
34 GetExtendedTcpTable(NULL, &size, FALSE, family, class, 0);
35 // reserve 25% more space to be sure
36 size = size + (size / 2 / 2);
37
38 table = malloc(size);
39 if (table == NULL) {
40 PyErr_NoMemory();
41 return NULL;
42 }
43
44 err = GetExtendedTcpTable(table, &size, FALSE, family, class, 0);
45 if (err == NO_ERROR)
46 return table;
47
48 free(table);
49 if (err == ERROR_INSUFFICIENT_BUFFER || err == STATUS_UNSUCCESSFUL) {
50 psutil_debug("GetExtendedTcpTable: retry with different bufsize");
51 return __GetExtendedTcpTable(family);
52 }
53
54 PyErr_SetString(PyExc_RuntimeError, "GetExtendedTcpTable failed");
55 return NULL;
56 }
57
58
59 static PVOID __GetExtendedUdpTable(ULONG family) {
60 DWORD err;
61 PVOID table;
62 ULONG size = 0;
63 UDP_TABLE_CLASS class = UDP_TABLE_OWNER_PID;
64
65 GetExtendedUdpTable(NULL, &size, FALSE, family, class, 0);
66 // reserve 25% more space
67 size = size + (size / 2 / 2);
68
69 table = malloc(size);
70 if (table == NULL) {
71 PyErr_NoMemory();
72 return NULL;
73 }
74
75 err = GetExtendedUdpTable(table, &size, FALSE, family, class, 0);
76 if (err == NO_ERROR)
77 return table;
78
79 free(table);
80 if (err == ERROR_INSUFFICIENT_BUFFER || err == STATUS_UNSUCCESSFUL) {
81 psutil_debug("GetExtendedUdpTable: retry with different bufsize");
82 return __GetExtendedUdpTable(family);
83 }
84
85 PyErr_SetString(PyExc_RuntimeError, "GetExtendedUdpTable failed");
86 return NULL;
87 }
88
89
90 #define psutil_conn_decref_objs() \
91 Py_DECREF(_AF_INET); \
92 Py_DECREF(_AF_INET6);\
93 Py_DECREF(_SOCK_STREAM);\
94 Py_DECREF(_SOCK_DGRAM);
95
96
97 /*
98 * Return a list of network connections opened by a process
99 */
100 PyObject *
101 psutil_net_connections(PyObject *self, PyObject *args) {
102 static long null_address[4] = { 0, 0, 0, 0 };
103 DWORD pid;
104 int pid_return;
105 PVOID table = NULL;
106 PMIB_TCPTABLE_OWNER_PID tcp4Table;
107 PMIB_UDPTABLE_OWNER_PID udp4Table;
108 PMIB_TCP6TABLE_OWNER_PID tcp6Table;
109 PMIB_UDP6TABLE_OWNER_PID udp6Table;
110 ULONG i;
111 CHAR addressBufferLocal[65];
112 CHAR addressBufferRemote[65];
113
114 PyObject *py_retlist = NULL;
115 PyObject *py_conn_tuple = NULL;
116 PyObject *py_af_filter = NULL;
117 PyObject *py_type_filter = NULL;
118 PyObject *py_addr_tuple_local = NULL;
119 PyObject *py_addr_tuple_remote = NULL;
120 PyObject *_AF_INET = PyLong_FromLong((long)AF_INET);
121 PyObject *_AF_INET6 = PyLong_FromLong((long)AF_INET6);
122 PyObject *_SOCK_STREAM = PyLong_FromLong((long)SOCK_STREAM);
123 PyObject *_SOCK_DGRAM = PyLong_FromLong((long)SOCK_DGRAM);
124
125 if (! PyArg_ParseTuple(args, _Py_PARSE_PID "OO", &pid, &py_af_filter,
126 &py_type_filter))
127 {
128 goto error;
129 }
130
131 if (!PySequence_Check(py_af_filter) || !PySequence_Check(py_type_filter)) {
132 psutil_conn_decref_objs();
133 PyErr_SetString(PyExc_TypeError, "arg 2 or 3 is not a sequence");
134 return NULL;
135 }
136
137 if (pid != -1) {
138 pid_return = psutil_pid_is_running(pid);
139 if (pid_return == 0) {
140 psutil_conn_decref_objs();
141 return NoSuchProcess("psutil_pid_is_running");
142 }
143 else if (pid_return == -1) {
144 psutil_conn_decref_objs();
145 return NULL;
146 }
147 }
148
149 py_retlist = PyList_New(0);
150 if (py_retlist == NULL) {
151 psutil_conn_decref_objs();
152 return NULL;
153 }
154
155 // TCP IPv4
156
157 if ((PySequence_Contains(py_af_filter, _AF_INET) == 1) &&
158 (PySequence_Contains(py_type_filter, _SOCK_STREAM) == 1))
159 {
160 table = NULL;
161 py_conn_tuple = NULL;
162 py_addr_tuple_local = NULL;
163 py_addr_tuple_remote = NULL;
164
165 table = __GetExtendedTcpTable(AF_INET);
166 if (table == NULL)
167 goto error;
168 tcp4Table = table;
169 for (i = 0; i < tcp4Table->dwNumEntries; i++) {
170 if (pid != -1) {
171 if (tcp4Table->table[i].dwOwningPid != pid) {
172 continue;
173 }
174 }
175
176 if (tcp4Table->table[i].dwLocalAddr != 0 ||
177 tcp4Table->table[i].dwLocalPort != 0)
178 {
179 struct in_addr addr;
180
181 addr.S_un.S_addr = tcp4Table->table[i].dwLocalAddr;
182 RtlIpv4AddressToStringA(&addr, addressBufferLocal);
183 py_addr_tuple_local = Py_BuildValue(
184 "(si)",
185 addressBufferLocal,
186 BYTESWAP_USHORT(tcp4Table->table[i].dwLocalPort));
187 }
188 else {
189 py_addr_tuple_local = PyTuple_New(0);
190 }
191
192 if (py_addr_tuple_local == NULL)
193 goto error;
194
195 // On Windows <= XP, remote addr is filled even if socket
196 // is in LISTEN mode in which case we just ignore it.
197 if ((tcp4Table->table[i].dwRemoteAddr != 0 ||
198 tcp4Table->table[i].dwRemotePort != 0) &&
199 (tcp4Table->table[i].dwState != MIB_TCP_STATE_LISTEN))
200 {
201 struct in_addr addr;
202
203 addr.S_un.S_addr = tcp4Table->table[i].dwRemoteAddr;
204 RtlIpv4AddressToStringA(&addr, addressBufferRemote);
205 py_addr_tuple_remote = Py_BuildValue(
206 "(si)",
207 addressBufferRemote,
208 BYTESWAP_USHORT(tcp4Table->table[i].dwRemotePort));
209 }
210 else
211 {
212 py_addr_tuple_remote = PyTuple_New(0);
213 }
214
215 if (py_addr_tuple_remote == NULL)
216 goto error;
217
218 py_conn_tuple = Py_BuildValue(
219 "(iiiNNiI)",
220 -1,
221 AF_INET,
222 SOCK_STREAM,
223 py_addr_tuple_local,
224 py_addr_tuple_remote,
225 tcp4Table->table[i].dwState,
226 tcp4Table->table[i].dwOwningPid);
227 if (!py_conn_tuple)
228 goto error;
229 if (PyList_Append(py_retlist, py_conn_tuple))
230 goto error;
231 Py_CLEAR(py_conn_tuple);
232 }
233
234 free(table);
235 table = NULL;
236 }
237
238 // TCP IPv6
239 if ((PySequence_Contains(py_af_filter, _AF_INET6) == 1) &&
240 (PySequence_Contains(py_type_filter, _SOCK_STREAM) == 1) &&
241 (RtlIpv6AddressToStringA != NULL))
242 {
243 table = NULL;
244 py_conn_tuple = NULL;
245 py_addr_tuple_local = NULL;
246 py_addr_tuple_remote = NULL;
247
248 table = __GetExtendedTcpTable(AF_INET6);
249 if (table == NULL)
250 goto error;
251 tcp6Table = table;
252 for (i = 0; i < tcp6Table->dwNumEntries; i++)
253 {
254 if (pid != -1) {
255 if (tcp6Table->table[i].dwOwningPid != pid) {
256 continue;
257 }
258 }
259
260 if (memcmp(tcp6Table->table[i].ucLocalAddr, null_address, 16)
261 != 0 || tcp6Table->table[i].dwLocalPort != 0)
262 {
263 struct in6_addr addr;
264
265 memcpy(&addr, tcp6Table->table[i].ucLocalAddr, 16);
266 RtlIpv6AddressToStringA(&addr, addressBufferLocal);
267 py_addr_tuple_local = Py_BuildValue(
268 "(si)",
269 addressBufferLocal,
270 BYTESWAP_USHORT(tcp6Table->table[i].dwLocalPort));
271 }
272 else {
273 py_addr_tuple_local = PyTuple_New(0);
274 }
275
276 if (py_addr_tuple_local == NULL)
277 goto error;
278
279 // On Windows <= XP, remote addr is filled even if socket
280 // is in LISTEN mode in which case we just ignore it.
281 if ((memcmp(tcp6Table->table[i].ucRemoteAddr, null_address, 16)
282 != 0 ||
283 tcp6Table->table[i].dwRemotePort != 0) &&
284 (tcp6Table->table[i].dwState != MIB_TCP_STATE_LISTEN))
285 {
286 struct in6_addr addr;
287
288 memcpy(&addr, tcp6Table->table[i].ucRemoteAddr, 16);
289 RtlIpv6AddressToStringA(&addr, addressBufferRemote);
290 py_addr_tuple_remote = Py_BuildValue(
291 "(si)",
292 addressBufferRemote,
293 BYTESWAP_USHORT(tcp6Table->table[i].dwRemotePort));
294 }
295 else {
296 py_addr_tuple_remote = PyTuple_New(0);
297 }
298
299 if (py_addr_tuple_remote == NULL)
300 goto error;
301
302 py_conn_tuple = Py_BuildValue(
303 "(iiiNNiI)",
304 -1,
305 AF_INET6,
306 SOCK_STREAM,
307 py_addr_tuple_local,
308 py_addr_tuple_remote,
309 tcp6Table->table[i].dwState,
310 tcp6Table->table[i].dwOwningPid);
311 if (!py_conn_tuple)
312 goto error;
313 if (PyList_Append(py_retlist, py_conn_tuple))
314 goto error;
315 Py_CLEAR(py_conn_tuple);
316 }
317
318 free(table);
319 table = NULL;
320 }
321
322 // UDP IPv4
323
324 if ((PySequence_Contains(py_af_filter, _AF_INET) == 1) &&
325 (PySequence_Contains(py_type_filter, _SOCK_DGRAM) == 1))
326 {
327 table = NULL;
328 py_conn_tuple = NULL;
329 py_addr_tuple_local = NULL;
330 py_addr_tuple_remote = NULL;
331 table = __GetExtendedUdpTable(AF_INET);
332 if (table == NULL)
333 goto error;
334 udp4Table = table;
335 for (i = 0; i < udp4Table->dwNumEntries; i++)
336 {
337 if (pid != -1) {
338 if (udp4Table->table[i].dwOwningPid != pid) {
339 continue;
340 }
341 }
342
343 if (udp4Table->table[i].dwLocalAddr != 0 ||
344 udp4Table->table[i].dwLocalPort != 0)
345 {
346 struct in_addr addr;
347
348 addr.S_un.S_addr = udp4Table->table[i].dwLocalAddr;
349 RtlIpv4AddressToStringA(&addr, addressBufferLocal);
350 py_addr_tuple_local = Py_BuildValue(
351 "(si)",
352 addressBufferLocal,
353 BYTESWAP_USHORT(udp4Table->table[i].dwLocalPort));
354 }
355 else {
356 py_addr_tuple_local = PyTuple_New(0);
357 }
358
359 if (py_addr_tuple_local == NULL)
360 goto error;
361
362 py_conn_tuple = Py_BuildValue(
363 "(iiiNNiI)",
364 -1,
365 AF_INET,
366 SOCK_DGRAM,
367 py_addr_tuple_local,
368 PyTuple_New(0),
369 PSUTIL_CONN_NONE,
370 udp4Table->table[i].dwOwningPid);
371 if (!py_conn_tuple)
372 goto error;
373 if (PyList_Append(py_retlist, py_conn_tuple))
374 goto error;
375 Py_CLEAR(py_conn_tuple);
376 }
377
378 free(table);
379 table = NULL;
380 }
381
382 // UDP IPv6
383
384 if ((PySequence_Contains(py_af_filter, _AF_INET6) == 1) &&
385 (PySequence_Contains(py_type_filter, _SOCK_DGRAM) == 1) &&
386 (RtlIpv6AddressToStringA != NULL))
387 {
388 table = NULL;
389 py_conn_tuple = NULL;
390 py_addr_tuple_local = NULL;
391 py_addr_tuple_remote = NULL;
392 table = __GetExtendedUdpTable(AF_INET6);
393 if (table == NULL)
394 goto error;
395 udp6Table = table;
396 for (i = 0; i < udp6Table->dwNumEntries; i++) {
397 if (pid != -1) {
398 if (udp6Table->table[i].dwOwningPid != pid) {
399 continue;
400 }
401 }
402
403 if (memcmp(udp6Table->table[i].ucLocalAddr, null_address, 16)
404 != 0 || udp6Table->table[i].dwLocalPort != 0)
405 {
406 struct in6_addr addr;
407
408 memcpy(&addr, udp6Table->table[i].ucLocalAddr, 16);
409 RtlIpv6AddressToStringA(&addr, addressBufferLocal);
410 py_addr_tuple_local = Py_BuildValue(
411 "(si)",
412 addressBufferLocal,
413 BYTESWAP_USHORT(udp6Table->table[i].dwLocalPort));
414 }
415 else {
416 py_addr_tuple_local = PyTuple_New(0);
417 }
418
419 if (py_addr_tuple_local == NULL)
420 goto error;
421
422 py_conn_tuple = Py_BuildValue(
423 "(iiiNNiI)",
424 -1,
425 AF_INET6,
426 SOCK_DGRAM,
427 py_addr_tuple_local,
428 PyTuple_New(0),
429 PSUTIL_CONN_NONE,
430 udp6Table->table[i].dwOwningPid);
431 if (!py_conn_tuple)
432 goto error;
433 if (PyList_Append(py_retlist, py_conn_tuple))
434 goto error;
435 Py_CLEAR(py_conn_tuple);
436 }
437
438 free(table);
439 table = NULL;
440 }
441
442 psutil_conn_decref_objs();
443 return py_retlist;
444
445 error:
446 psutil_conn_decref_objs();
447 Py_XDECREF(py_conn_tuple);
448 Py_XDECREF(py_addr_tuple_local);
449 Py_XDECREF(py_addr_tuple_remote);
450 Py_DECREF(py_retlist);
451 if (table != NULL)
452 free(table);
453 return NULL;
454 }