Debian Codegraph @

psutil/arch/osx/net.c

raw
1 /*
2 * Copyright (c) 2009, Jay Loden, 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 // Networks related functions. Original code was refactored and moved
8 // from psutil/_psutil_osx.c in 2023. This is the GIT blame before the move:
9 // https://github.com/giampaolo/psutil/blame/efd7ed3/psutil/_psutil_osx.c
10
11 #include <Python.h>
12 #include <sys/socket.h>
13 #include <sys/sysctl.h>
14 #include <net/if.h>
15 #include <net/if_dl.h>
16 #include <net/route.h>
17
18 #include "../../arch/all/init.h"
19
20
21 PyObject *
22 psutil_net_io_counters(PyObject *self, PyObject *args) {
23 char *buf = NULL, *lim, *next;
24 struct if_msghdr *ifm;
25 int mib[6];
26 size_t len = 0;
27 PyObject *py_ifc_info = NULL;
28 PyObject *py_retdict = PyDict_New();
29
30 if (py_retdict == NULL)
31 return NULL;
32
33 mib[0] = CTL_NET; // networking subsystem
34 mib[1] = PF_ROUTE; // type of information
35 mib[2] = 0; // protocol (IPPROTO_xxx)
36 mib[3] = 0; // address family
37 mib[4] = NET_RT_IFLIST2; // operation
38 mib[5] = 0;
39
40 if (psutil_sysctl_malloc(mib, 6, &buf, &len) != 0)
41 goto error;
42
43 lim = buf + len;
44
45 for (next = buf; next < lim; ) {
46 // Check we have enough space for if_msghdr.
47 if ((size_t)(lim - next) < sizeof(struct if_msghdr)) {
48 psutil_debug("struct xfile size mismatch");
49 }
50
51 ifm = (struct if_msghdr *)next;
52
53 if (ifm->ifm_msglen == 0 || next + ifm->ifm_msglen > lim) {
54 psutil_debug("ifm_msglen size mismatch");
55 }
56
57 next += ifm->ifm_msglen;
58
59 if (ifm->ifm_type == RTM_IFINFO2) {
60 py_ifc_info = NULL;
61 struct if_msghdr2 *if2m = (struct if_msghdr2 *)ifm;
62
63 if ((char *)if2m + sizeof(struct if_msghdr2) > lim) {
64 psutil_debug("if_msghdr2 + sockaddr_dl mismatch");
65 }
66
67 struct sockaddr_dl *sdl = (struct sockaddr_dl *)(if2m + 1);
68
69 if ((char *)sdl + sizeof(struct sockaddr_dl) > lim) {
70 psutil_debug("not enough buffer for sockaddr_dl");
71 }
72
73 char ifc_name[32];
74 size_t namelen = sdl->sdl_nlen;
75
76 if (namelen >= sizeof(ifc_name))
77 namelen = sizeof(ifc_name) - 1;
78
79 strncpy(ifc_name, sdl->sdl_data, namelen);
80 ifc_name[namelen] = '\0';
81
82 py_ifc_info = Py_BuildValue(
83 "(KKKKKKKi)",
84 (unsigned long long)if2m->ifm_data.ifi_obytes,
85 (unsigned long long)if2m->ifm_data.ifi_ibytes,
86 (unsigned long long)if2m->ifm_data.ifi_opackets,
87 (unsigned long long)if2m->ifm_data.ifi_ipackets,
88 (unsigned long long)if2m->ifm_data.ifi_ierrors,
89 (unsigned long long)if2m->ifm_data.ifi_oerrors,
90 (unsigned long long)if2m->ifm_data.ifi_iqdrops,
91 0); // dropout not supported
92
93 if (!py_ifc_info)
94 goto error;
95
96 if (PyDict_SetItemString(py_retdict, ifc_name, py_ifc_info)) {
97 Py_CLEAR(py_ifc_info);
98 goto error;
99 }
100 Py_CLEAR(py_ifc_info);
101 }
102 }
103
104 free(buf);
105 return py_retdict;
106
107 error:
108 Py_XDECREF(py_ifc_info);
109 Py_DECREF(py_retdict);
110 if (buf != NULL)
111 free(buf);
112 return NULL;
113 }