1 /*
2 * Copyright 2017-2022 The OSHI Project Contributors
3 * SPDX-License-Identifier: MIT
4 */
5 package oshi.jna.platform.unix;
6
7 import com.sun.jna.Library;
8 import com.sun.jna.NativeLong;
9 import com.sun.jna.Pointer;
10 import com.sun.jna.Structure;
11 import com.sun.jna.Structure.FieldOrder;
12 import com.sun.jna.platform.unix.LibCAPI;
13 import com.sun.jna.ptr.PointerByReference;
14
15 import oshi.util.Util;
16
17 /**
18 * C library with code common to all *nix-based operating systems. This class should be considered non-API as it may be
19 * removed if/when its code is incorporated into the JNA project.
20 */
21 public interface CLibrary extends LibCAPI, Library {
22
23 int AI_CANONNAME = 2;
24
25 int UT_LINESIZE = 32;
26 int UT_NAMESIZE = 32;
27 int UT_HOSTSIZE = 256;
28 int LOGIN_PROCESS = 6; // Session leader of a logged in user.
29 int USER_PROCESS = 7; // Normal process.
30
31 @FieldOrder({ "sa_family", "sa_data" })
32 class Sockaddr extends Structure {
33 public short sa_family;
34 public byte[] sa_data = new byte[14];
35
36 public static class ByReference extends Sockaddr implements Structure.ByReference {
37 }
38 }
39
40 @FieldOrder({ "ai_flags", "ai_family", "ai_socktype", "ai_protocol", "ai_addrlen", "ai_addr", "ai_canonname",
41 "ai_next" })
42 class Addrinfo extends Structure implements AutoCloseable {
43 public int ai_flags;
44 public int ai_family;
45 public int ai_socktype;
46 public int ai_protocol;
47 public int ai_addrlen;
48 public Sockaddr.ByReference ai_addr;
49 public String ai_canonname;
50 public ByReference ai_next;
51
52 public static class ByReference extends Addrinfo implements Structure.ByReference {
53 }
54
55 public Addrinfo() {
56 }
57
58 public Addrinfo(Pointer p) {
59 super(p);
60 read();
61 }
62
63 @Override
64 public void close() {
65 Util.freeMemory(getPointer());
66 }
67 }
68
69 /*
70 * Between macOS and FreeBSD there are multiple versions of some tcp/udp/ip stats structures. Since we only need a
71 * few of the hundreds of fields, we can improve performance by selectively reading the ints from the appropriate
72 * offsets, which are consistent across the structure. These classes include the common fields and offsets.
73 */
74
75 class BsdTcpstat {
76 public int tcps_connattempt; // 0
77 public int tcps_accepts; // 4
78 public int tcps_drops; // 12
79 public int tcps_conndrops; // 16
80 public int tcps_sndpack; // 64
81 public int tcps_sndrexmitpack; // 72
82 public int tcps_rcvpack; // 104
83 public int tcps_rcvbadsum; // 112
84 public int tcps_rcvbadoff; // 116
85 public int tcps_rcvmemdrop; // 120
86 public int tcps_rcvshort; // 124
87 }
88
89 class BsdUdpstat {
90 public int udps_ipackets; // 0
91 public int udps_hdrops; // 4
92 public int udps_badsum; // 8
93 public int udps_badlen; // 12
94 public int udps_opackets; // 36
95 public int udps_noportmcast; // 48
96 public int udps_rcv6_swcsum; // 64
97 public int udps_snd6_swcsum; // 89
98 }
99
100 class BsdIpstat {
101 public int ips_total; // 0
102 public int ips_badsum; // 4
103 public int ips_tooshort; // 8
104 public int ips_toosmall; // 12
105 public int ips_badhlen; // 16
106 public int ips_badlen; // 20
107 public int ips_delivered; // 56
108 }
109
110 class BsdIp6stat {
111 public long ip6s_total; // 0
112 public long ip6s_localout; // 88
113 }
114
115 /**
116 * Returns the process ID of the calling process. The ID is guaranteed to be unique and is useful for constructing
117 * temporary file names.
118 *
119 * @return the process ID of the calling process.
120 */
121 int getpid();
122
123 /**
124 * Given node and service, which identify an Internet host and a service, getaddrinfo() returns one or more addrinfo
125 * structures, each of which contains an Internet address that can be specified in a call to bind(2) or connect(2).
126 *
127 * @param node a numerical network address or a network hostname, whose network addresses are looked up and
128 * resolved.
129 * @param service sets the port in each returned address structure.
130 * @param hints specifies criteria for selecting the socket address structures returned in the list pointed to by
131 * res.
132 * @param res returned address structure
133 * @return 0 on success; sets errno on failure
134 */
135 int getaddrinfo(String node, String service, Addrinfo hints, PointerByReference res);
136
137 /**
138 * Frees the memory that was allocated for the dynamically allocated linked list res.
139 *
140 * @param res Pointer to linked list returned by getaddrinfo
141 */
142 void freeaddrinfo(Pointer res);
143
144 /**
145 * Translates getaddrinfo error codes to a human readable string, suitable for error reporting.
146 *
147 * @param e Error code from getaddrinfo
148 * @return A human-readable version of the error code
149 */
150 String gai_strerror(int e);
151
152 /**
153 * Rewinds the file pointer to the beginning of the utmp file. It is generally a good idea to call it before any of
154 * the other functions.
155 */
156 void setutxent();
157
158 /**
159 * Closes the utmp file. It should be called when the user code is done accessing the file with the other functions.
160 */
161 void endutxent();
162
163 /**
164 * The sysctl() function retrieves system information and allows processes with appropriate privileges to set system
165 * information. The information available from sysctl() consists of integers, strings, and tables.
166 *
167 * The state is described using a "Management Information Base" (MIB) style name, listed in name, which is a namelen
168 * length array of integers.
169 *
170 * The information is copied into the buffer specified by oldp. The size of the buffer is given by the location
171 * specified by oldlenp before the call, and that location gives the amount of data copied after a successful call
172 * and after a call that returns with the error code ENOMEM. If the amount of data available is greater than the
173 * size of the buffer supplied, the call supplies as much data as fits in the buffer provided and returns with the
174 * error code ENOMEM. If the old value is not desired, oldp and oldlenp should be set to NULL.
175 *
176 * The size of the available data can be determined by calling sysctl() with the NULL argument for oldp. The size of
177 * the available data will be returned in the location pointed to by oldlenp. For some operations, the amount of
178 * space may change often. For these operations, the system attempts to round up so that the returned size is large
179 * enough for a call to return the data shortly thereafter.
180 *
181 * To set a new value, newp is set to point to a buffer of length newlen from which the requested value is to be
182 * taken. If a new value is not to be set, newp should be set to NULL and newlen set to 0.
183 *
184 * @param name MIB array of integers
185 * @param namelen length of the MIB array
186 * @param oldp Information retrieved
187 * @param oldlenp Size of information retrieved
188 * @param newp Information to be written
189 * @param newlen Size of information to be written
190 * @return 0 on success; sets errno on failure
191 */
192 int sysctl(int[] name, int namelen, Pointer oldp, size_t.ByReference oldlenp, Pointer newp, size_t newlen);
193
194 /**
195 * The sysctlbyname() function accepts an ASCII representation of the name and internally looks up the integer name
196 * vector. Apart from that, it behaves the same as the standard sysctl() function.
197 *
198 * @param name ASCII representation of the MIB name
199 * @param oldp Information retrieved
200 * @param oldlenp Size of information retrieved
201 * @param newp Information to be written
202 * @param newlen Size of information to be written
203 * @return 0 on success; sets errno on failure
204 */
205 int sysctlbyname(String name, Pointer oldp, size_t.ByReference oldlenp, Pointer newp, size_t newlen);
206
207 /**
208 * The sysctlnametomib() function accepts an ASCII representation of the name, looks up the integer name vector, and
209 * returns the numeric representation in the mib array pointed to by mibp. The number of elements in the mib array
210 * is given by the location specified by sizep before the call, and that location gives the number of entries copied
211 * after a successful call. The resulting mib and size may be used in subsequent sysctl() calls to get the data
212 * associated with the requested ASCII name. This interface is intended for use by applications that want to
213 * repeatedly request the same variable (the sysctl() function runs in about a third the time as the same request
214 * made via the sysctlbyname() function).
215 *
216 * The number of elements in the mib array can be determined by calling sysctlnametomib() with the NULL argument for
217 * mibp.
218 *
219 * The sysctlnametomib() function is also useful for fetching mib prefixes. If size on input is greater than the
220 * number of elements written, the array still contains the additional elements which may be written
221 * programmatically.
222 *
223 * @param name ASCII representation of the name
224 * @param mibp Integer array containing the corresponding name vector.
225 * @param sizep On input, number of elements in the returned array; on output, the number of entries copied.
226 * @return 0 on success; sets errno on failure
227 */
228 int sysctlnametomib(String name, Pointer mibp, size_t.ByReference sizep);
229
230 int open(String absolutePath, int i);
231
232 // Last argument is really off_t
233 ssize_t pread(int fildes, Pointer buf, size_t nbyte, NativeLong offset);
234 }