-
Notifications
You must be signed in to change notification settings - Fork 3
/
UNetwork.java
executable file
·424 lines (399 loc) · 13.6 KB
/
UNetwork.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package jtux;
/**
Class for network system calls.
<p>
Not implemented:
<pre>
inet_addr, inet_ntoa: Replaced by inet_ntop and inet_pton.
{set/get/end}{host/net/proto/serv}ent - To Do.
gethostbyname, gethostbyaddr - replaced by getaddrinfo/getnameinfo.
getnetbyname, getnetbyaddr - To Do
getprotobyname, getprotobynumber- To Do
getservbyname, getservbyport - To Do
if_* - To Do
getsockname, getpeername - To Do
socketpair - To Do
shutdown - To Do
</pre>
*/
public class UNetwork {
static {
System.loadLibrary("jtux");
}
/**
Java version of C struct sockaddr.
<p>
It would be natural to use the sa_family field in the superclass instead
of the individually-named fields in each subclass, but the C structures
do have the individually-named fields. In C, of course, this is no problem,
and either field could be used, with an appropriate cast. In Java, the
field sa_family in s_sockaddr is never used, so given an object, the field
can't be used as a discriminant. Instead, if you don't know the address
family, use the getClass method to see what it is, or use the Jtux virtual
method get_family().
*/
public static class s_sockaddr {
/** not used */
public final int sa_family = UConstant.AF_UNSPEC;
/**
Jtux only; non-standard
*/
public int get_family() { //
return sa_family;
}
}
/**
Java version of C struct sockaddr_un.
<p>
<font size="-1"><b><i>Click {@link <a href="doc-files/synopses.html#struct sockaddr_un">here</a>} for Posix/SUS C API.</i></b></font>
*/
public static class s_sockaddr_un extends s_sockaddr {
/** AF_UNIX */
public int sun_family;
/** socket pathname */
public String sun_path = "";
public int get_family() {
return sun_family;
}
public String toString() {
return sun_path;
}
}
/**
Java version of C struct sockaddr_in.
<p>
<font size="-1"><b><i>Click {@link <a href="doc-files/synopses.html#struct_sockaddr_in">here</a>} for Posix/SUS C API.</i></b></font>
*/
public static class s_sockaddr_in extends s_sockaddr {
/** AF_INET */
public int sin_family;
/** port number (uint16_t) */
public short sin_port;
/** IPv4 address */
public s_in_addr sin_addr;
public int get_family() {
return sin_family;
}
public String toString() {
try {
return UNetwork.inet_ntop(UConstant.AF_INET, sin_addr.s_addr) +
":" + UNetwork.ntohs(sin_port);
}
catch (UErrorException e) {
return "Can't get addr string (" + e + ")";
}
}
}
/**
Java version of C struct in_addr.
<p>
<font size="-1"><b><i>Click {@link <a href="doc-files/synopses.html#struct_in_addr">here</a>} for Posix/SUS C API.</i></b></font>
*/
public static class s_in_addr {
/** IPv4 address (uint32_t) */
public int s_addr;
}
/**
Java version of C struct sockaddr_in6.
<p>
<font size="-1"><b><i>Click {@link <a href="doc-files/synopses.html#struct_sockaddr_in6">here</a>} for Posix/SUS C API.</i></b></font>
*/
public static class s_sockaddr_in6 extends s_sockaddr {
/** AF_INET6 */
public int sin6_family;
/** port number (uint16_t) */
public short sin6_port;
/** traffic class and flow information */
public int sin6_flowinfo;
/** IPv4 address */
public s_in6_addr sin6_addr;
/** set of interfaces for a scope */
public int sin6_scope_id;
public int get_family() {
return sin6_family;
}
public String toString() {
try {
return UNetwork.inet_ntop(UConstant.AF_INET6, sin6_addr.s6_addr) +
" [" + UNetwork.ntohs(sin6_port) + "]";
}
catch (UErrorException e) {
return "Can't get addr string (" + e + ")";
}
}
}
/**
Java version of C struct in6_addr.
<p>
<font size="-1"><b><i>Click {@link <a href="doc-files/synopses.html#struct_in6_addr">here</a>} for Posix/SUS C API.</i></b></font>
*/
public static class s_in6_addr {
/** IPv6 address */
public byte[] s6_addr = new byte[16];
}
/**
Java version of C struct addrinfo.
<p>
<font size="-1"><b><i>Click {@link <a href="doc-files/synopses.html#struct_addrinfo">here</a>} for Posix/SUS C API.</i></b></font>
*/
public static class s_addrinfo {
/** input flags */
public int ai_flags;
/** address family */
public int ai_family;
/** socket type */
public int ai_socktype;
/** protocol */
public int ai_protocol;
/** length of socket address */
public int ai_addrlen;
/** socket address */
public s_sockaddr ai_addr = null;
/** canonical name of service location */
public String ai_canonname = "";
/** pointer to next structure in list */
public s_addrinfo ai_next = null;
}
/**
Java version of C struct linger.
<p>
<font size="-1"><b><i>Click {@link <a href="doc-files/synopses.html#struct_linger">here</a>} for Posix/SUS C API.</i></b></font>
*/
public static class s_linger {
/** on (1) or off (0). */
public int l_onoff;
/** linger-time in seconds */
public int l_linger;
}
/**
Following class and its subclasses handle the value argument of setsockopt,
which in C is of type "const void *".
*/
public static class SockOptValue {
}
public static class SockOptValue_int extends SockOptValue {
public int value;
}
/**
Not necessary to use SockOptValue_boolean -- SockOptValue_int
works just as well, as the UNIX system call doesn't know the
difference.
*/
public static class SockOptValue_boolean extends SockOptValue {
public boolean value;
}
public static class SockOptValue_s_linger extends SockOptValue {
public s_linger value = new s_linger();
}
public static class SockOptValue_s_timeval extends SockOptValue {
public UProcess.s_timeval value = new UProcess.s_timeval();
}
/**
Java version of C struct msghd.
<p>
<font size="-1"><b><i>Click {@link <a href="doc-files/synopses.html#struct_msghdr">here</a>} for Posix/SUS C API.</i></b></font>
*/
public static class s_msghdr {
/** optional address */
public s_sockaddr msg_name;
/** size of address; ignored for Java */
public int msg_namelen = 0;
/** scatter/gather array */
public UFile.s_iovec[] msg_iov;
/** number of elements in msg_iov */
public int msg_iovlen;
/** ancillary data */
public byte[] msg_control = null;
/** ancillary data buffer len */
public int msg_controllen = 0;
/** flags on received message */
public int msg_flags;
}
/**
Jtux-only -- non-standard.
*/
public static class AddrInfoListHead {
/** pointer to first structure in list */
public s_addrinfo ai_next = null;
}
/**
Calls accept.
<p>
<font size="-1"><b><i>Click {@link <a href="doc-files/synopses.html#accept">here</a>} for Posix/SUS C API.</i></b></font>
*/
public native static int accept(int socket_fd, s_sockaddr sa, UUtil.IntHolder sa_len) throws UErrorException;
/**
Calls bind.
Not necessary to set sa_len on input.
<p>
<font size="-1"><b><i>Click {@link <a href="doc-files/synopses.html#bind">here</a>} for Posix/SUS C API.</i></b></font>
*/
public native static void bind(int socket_fd, s_sockaddr sa, int sa_len) throws UErrorException;
/**
Calls connect.
Not necessary to set sa_len on input.
<p>
<font size="-1"><b><i>Click {@link <a href="doc-files/synopses.html#connect">here</a>} for Posix/SUS C API.</i></b></font>
*/
public native static void connect(int socket_fd, s_sockaddr sa, int sa_len) throws UErrorException;
/**
Calls freeaddrinfo.
<p>
<font size="-1"><b><i>Click {@link <a href="doc-files/synopses.html#freeaddrinfo">here</a>} for Posix/SUS C API.</i></b></font>
*/
public native static void freeaddrinfo(s_addrinfo infop);
/**
Calls gai_strerror.
<p>
<font size="-1"><b><i>Click {@link <a href="doc-files/synopses.html#gai_strerror">here</a>} for Posix/SUS C API.</i></b></font>
*/
public native static String gai_strerror(int code);
/**
Calls getaddrinfo. (Replaces gethostbyname.)
<p>
<font size="-1"><b><i>Click {@link <a href="doc-files/synopses.html#getaddrinfo">here</a>} for Posix/SUS C API.</i></b></font>
*/
public native static void getaddrinfo(String nodename, String servname,
s_addrinfo hint, AddrInfoListHead infop) throws UErrorException;
/**
Calls gethostid.
<p>
<font size="-1"><b><i>Click {@link <a href="doc-files/synopses.html#gethostid">here</a>} for Posix/SUS C API.</i></b></font>
*/
public native static long gethostid();
/**
Calls gethostname.
<p>
<font size="-1"><b><i>Click {@link <a href="doc-files/synopses.html#gethostname">here</a>} for Posix/SUS C API.</i></b></font>
*/
public native static void gethostname(StringBuffer name) throws UErrorException;
/**
Calls getnameinfo. (Replaces gethostbyaddr.) sa_len not used.
<p>
<font size="-1"><b><i>Click {@link <a href="doc-files/synopses.html#getnameinfo">here</a>} for Posix/SUS C API.</i></b></font>
*/
public native static void getnameinfo(s_sockaddr sa, int sa_len,
StringBuffer nodename, StringBuffer servname, int flags) throws UErrorException;
/**
Calls getsockopt.
<p>
<font size="-1"><b><i>Click {@link <a href="doc-files/synopses.html#getsockopt">here</a>} for Posix/SUS C API.</i></b></font>
*/
public native static void getsockopt(int socket_fd, int level, int option,
SockOptValue value, UUtil.IntHolder value_len) throws UErrorException;
/**
Calls htonl.
<p>
<font size="-1"><b><i>Click {@link <a href="doc-files/synopses.html#htonl">here</a>} for Posix/SUS C API.</i></b></font>
*/
public native static int htonl(int hostnum);
/**
Calls htons.
<p>
<font size="-1"><b><i>Click {@link <a href="doc-files/synopses.html#htons">here</a>} for Posix/SUS C API.</i></b></font>
*/
public native static short htons(short hostnum);
/**
Calls inet_ntop.
<p>
<font size="-1"><b><i>Click {@link <a href="doc-files/synopses.html#inet_ntop">here</a>} for Posix/SUS C API.</i></b></font>
*/
public native static String inet_ntop(int domain, int src) throws UErrorException;
/**
Calls inet_ntop.
<p>
<font size="-1"><b><i>Click {@link <a href="doc-files/synopses.html#inet_ntop">here</a>} for Posix/SUS C API.</i></b></font>
*/
public native static String inet_ntop(int domain, byte[] src) throws UErrorException;
/**
Calls inet_pton.
<p>
<font size="-1"><b><i>Click {@link <a href="doc-files/synopses.html#inet_pton">here</a>} for Posix/SUS C API.</i></b></font>
*/
public native static void inet_pton(int domain, String src, UUtil.IntHolder dst) throws UErrorException;
/**
Calls inet_pton.
<p>
<font size="-1"><b><i>Click {@link <a href="doc-files/synopses.html#inet_pton">here</a>} for Posix/SUS C API.</i></b></font>
*/
public native static void inet_pton(int domain, String src, byte[] dst) throws UErrorException;
/**
Calls listen.
<p>
<font size="-1"><b><i>Click {@link <a href="doc-files/synopses.html#listen">here</a>} for Posix/SUS C API.</i></b></font>
*/
public native static void listen(int socket_fd, int backlog) throws UErrorException;
/**
Calls ntohl.
<p>
<font size="-1"><b><i>Click {@link <a href="doc-files/synopses.html#ntohl">here</a>} for Posix/SUS C API.</i></b></font>
*/
public native static int ntohl(int netnum);
/**
Calls ntohs.
<p>
<font size="-1"><b><i>Click {@link <a href="doc-files/synopses.html#ntohs">here</a>} for Posix/SUS C API.</i></b></font>
*/
public native static short ntohs(short netnum);
/**
Calls recv.
<p>
<font size="-1"><b><i>Click {@link <a href="doc-files/synopses.html#socket_fd">here</a>} for Posix/SUS C API.</i></b></font>
*/
public native static int recv(int socket_fd, byte[] buffer, int length,
int flags) throws UErrorException;
/**
Calls recvfrom.
sa_len need not be set on input.
<p>
<font size="-1"><b><i>Click {@link <a href="doc-files/synopses.html#recvfrom">here</a>} for Posix/SUS C API.</i></b></font>
*/
public native static int recvfrom(int socket_fd, byte[] buffer, int length,
int flags, s_sockaddr sa, UUtil.IntHolder sa_len) throws UErrorException;
/**
Calls recvmsg.
<p>
<font size="-1"><b><i>Click {@link <a href="doc-files/synopses.html#recvmsg">here</a>} for Posix/SUS C API.</i></b></font>
*/
public native static int recvmsg(int socket_fd, s_msghdr message, int flags) throws UErrorException;
/**
Calls send.
<p>
<font size="-1"><b><i>Click {@link <a href="doc-files/synopses.html#send">here</a>} for Posix/SUS C API.</i></b></font>
*/
public native static int send(int socket_fd, byte[] data, int length,
int flags) throws UErrorException;
/**
Calls sendmsg.
<p>
<font size="-1"><b><i>Click {@link <a href="doc-files/synopses.html#sendmsg">here</a>} for Posix/SUS C API.</i></b></font>
*/
public native static int sendmsg(int socket_fd, s_msghdr message, int flags) throws UErrorException;
/**
Calls sendto.
Not necessary to set sa_len on input.
<p>
<font size="-1"><b><i>Click {@link <a href="doc-files/synopses.html#sendto">here</a>} for Posix/SUS C API.</i></b></font>
*/
public native static int sendto(int socket_fd, byte[] message, int length,
int flags, s_sockaddr sa, int sa_len) throws UErrorException;
/**
Calls setsockopt.
<p>
<font size="-1"><b><i>Click {@link <a href="doc-files/synopses.html#setsockopt">here</a>} for Posix/SUS C API.</i></b></font>
*/
public native static void setsockopt(int socket_fd, int level, int option,
SockOptValue value, int value_len) throws UErrorException;
/**
Calls sockatmark.
<p>
<font size="-1"><b><i>Click {@link <a href="doc-files/synopses.html#sockatmark">here</a>} for Posix/SUS C API.</i></b></font>
*/
public native static int sockatmark(int socket_fd) throws UErrorException;
/**
Calls socket.
<p>
<font size="-1"><b><i>Click {@link <a href="doc-files/synopses.html#socket">here</a>} for Posix/SUS C API.</i></b></font>
*/
public native static int socket(int domain, int type, int protocol) throws UErrorException;
}