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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
|
/*
* Copyright (c)2013-2021 ZeroTier, Inc.
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file in the project's root directory.
*
* Change Date: 2026-01-01
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2.0 of the Apache License.
*/
/****/
package com.zerotier.sockets;
import com.zerotier.sockets.*;
import java.io.*;
import java.net.*;
/**
* Implements Socket-like behavior over ZeroTier
*
* @author ZeroTier, Inc.
*/
public class ZeroTierSocket {
// File descriptor from lower native layer
private int _zfd = -1;
private int _family = -1;
private int _type = -1;
private int _protocol = -1;
// State flags
private boolean _isClosed = false;
private boolean _isConnected = false;
private boolean _isBound = false;
private boolean _inputHasBeenShutdown = false;
private boolean _outputHasBeenShutdown = false;
// Input and Output streams
private ZeroTierInputStream _inputStream = new ZeroTierInputStream();
private ZeroTierOutputStream _outputStream = new ZeroTierOutputStream();
// The remote address to which the ZeroTierSocket is connected
private InetAddress _remoteAddr;
private int _remotePort;
private InetAddress _localAddr;
private int _localPort;
private void setNativeFileDescriptor(int fd)
{
_zfd = fd;
_inputStream.zfd = fd;
_outputStream.zfd = fd;
}
public int getNativeFileDescriptor()
{
return _zfd;
}
private ZeroTierSocket(int family, int type, int protocol, int zfd)
{
_family = family;
_type = type;
_protocol = protocol;
setNativeFileDescriptor(zfd);
// Since we only call this from accept() we will mark it as connected
_isConnected = true;
}
public ZeroTierSocket(String remoteAddr, int remotePort) throws IOException
{
_protocol = 0;
_type = ZeroTierNative.ZTS_SOCK_STREAM;
InetAddress address = InetAddress.getByName(remoteAddr);
if (address instanceof Inet6Address) {
_family = ZeroTierNative.ZTS_AF_INET6;
}
else if (address instanceof Inet4Address) {
_family = ZeroTierNative.ZTS_AF_INET;
}
_zfd = ZeroTierNative.zts_bsd_socket(_family, _type, _protocol);
setNativeFileDescriptor(_zfd);
int err;
if ((err = ZeroTierNative.zts_connect(_zfd, remoteAddr, remotePort, 0)) < 0) {
throw new IOException("Error while connecting to remote host (" + err + ")");
}
_isConnected = true;
}
/**
* Create a new ZeroTierSocket with the given attributes
* @param family The socket family
* @param type The socket type
* @param protocol Supported protocol
*
* @exception IOException when an I/O error occurs
*/
public ZeroTierSocket(int family, int type, int protocol) throws IOException
{
if (_zfd > -1) {
throw new IOException("This socket has already been created (fd=" + _zfd + ")");
}
_zfd = ZeroTierNative.zts_bsd_socket(family, type, protocol);
if (_zfd < 0) {
throw new IOException("Error while creating socket (" + _zfd + ")");
}
_family = family;
_type = type;
_protocol = protocol;
setNativeFileDescriptor(_zfd);
}
/**
* Connect to a remote host
* @param remoteAddr Remote address to which this socket should connect
* @param remotePort Remote port to which this socket should connect
*
* @exception IOException when an I/O error occurs
*/
public void connect(InetAddress remoteAddr, int remotePort) throws IOException
{
if (_zfd < 0) {
throw new IOException("Invalid socket (fd < 0)");
}
if ((remoteAddr instanceof Inet4Address) && _family != ZeroTierNative.ZTS_AF_INET) {
throw new IOException("Invalid address type. Socket is of type AF_INET");
}
if ((remoteAddr instanceof Inet6Address) && _family != ZeroTierNative.ZTS_AF_INET6) {
throw new IOException("Invalid address type. Socket is of type AF_INET6");
}
int err;
if ((err = ZeroTierNative.zts_connect(_zfd, remoteAddr.getHostAddress(), remotePort, 0)) < 0) {
throw new IOException("Error while connecting to remote host (" + err + ")");
}
_isConnected = true;
}
/**
* Connect to a remote host
* @param remoteAddr Remote address to which this socket should connect
* @param remotePort Remote port to which this socket should connect
*
* @exception IOException when an I/O error occurs
*/
public void connect(String remoteAddr, int remotePort) throws IOException
{
InetAddress remoteInetAddr = InetAddress.getByName(remoteAddr);
connect(remoteInetAddr, remotePort);
}
/**
* Connect to a remote host
* @param remoteAddr Remote address to which this socket should connect
*
* @exception IOException when an I/O error occurs
*/
public void connect(SocketAddress remoteAddr) throws IOException
{
int remotePort = ((InetSocketAddress)remoteAddr).getPort();
connect(((InetSocketAddress)remoteAddr).getHostString(), remotePort);
}
/**
* Bind to a local address
* @param localAddr Local address to which this socket should bind
* @param localPort Local port to which this socket should bind
*
* @exception IOException when an I/O error occurs
*/
public void bind(InetAddress localAddr, int localPort) throws IOException
{
if (_zfd < 0) {
throw new IOException("Invalid socket (fd < 0)");
}
if ((localAddr instanceof Inet4Address) && _family != ZeroTierNative.ZTS_AF_INET) {
throw new IOException("Invalid address type. Socket is of type AF_INET");
}
if ((localAddr instanceof Inet6Address) && _family != ZeroTierNative.ZTS_AF_INET6) {
throw new IOException("Invalid address type. Socket is of type AF_INET6");
}
int err;
if ((err = ZeroTierNative.zts_bind(_zfd, localAddr.getHostAddress(), localPort)) < 0) {
throw new IOException("Error while connecting to remote host (" + err + ")");
}
_localPort = localPort;
_isBound = true;
}
/**
* Bind to a local address
* @param localAddr Local address to which this socket should bind
* @param localPort Local port to which this socket should bind
*
* @exception IOException when an I/O error occurs
*/
public void bind(String localAddr, int localPort) throws IOException
{
InetAddress localInetAddr = InetAddress.getByName(localAddr);
bind(localInetAddr, localPort);
}
/**
* Put the ZeroTierSocket into a listening state
* @param backlog Size of connection backlog
*
* @exception IOException when an I/O error occurs
*/
public void listen(int backlog) throws IOException
{
if (_zfd < 0) {
throw new IOException("Invalid socket (fd < 0)");
}
if (backlog < 0) {
throw new IOException("Invalid backlog value");
}
int err;
if ((err = ZeroTierNative.zts_bsd_listen(_zfd, backlog)) < 0) {
throw new IOException("Error while putting socket into listening state (" + err + ")");
}
}
/**
* Accept incoming connections on this ZeroTierSocket
* @return New ZeroTierSocket representing the accepted connection
* @exception IOException when an I/O error occurs
*/
public ZeroTierSocket accept() throws IOException
{
if (_zfd < 0) {
throw new IOException("Invalid socket (fd < 0)");
}
int acceptedFd = -1;
ZeroTierSocketAddress addr = new ZeroTierSocketAddress();
if ((acceptedFd = ZeroTierNative.zts_bsd_accept(_zfd, addr)) < 0) {
throw new IOException("Error while accepting connection (" + acceptedFd + ")");
}
return new ZeroTierSocket(_family, _type, _protocol, acceptedFd);
}
/**
* Close the ZeroTierSocket.
*
* @exception IOException when an I/O error occurs
*/
public void close() throws IOException
{
if (_zfd < 0) {
throw new IOException("Invalid socket (fd < 0)");
}
ZeroTierNative.zts_bsd_close(_zfd);
_isClosed = true;
}
/**
* Return whether keepalive is enabled.
* @return true or false
* @exception SocketException when an error occurs in the native socket layer
*/
public boolean getKeepAlive() throws SocketException
{
if (_isClosed) {
throw new SocketException("Error: ZeroTierSocket is closed");
}
return ZeroTierNative.zts_get_keepalive(_zfd) == 1;
}
/**
* Get the local port to which this ZeroTierSocket is bound
* @return Local port
*/
public int getLocalPort()
{
if (! _isBound) {
return -1;
}
return _localPort;
}
/**
* Get the local address to which this ZeroTierSocket is bound
* @return Local address
*/
public InetAddress getLocalAddress()
{
if (! _isBound) {
return null;
}
return _localAddr;
}
/**
* Get the remote port to which this ZeroTierSocket is connected
* @return Remote port
*/
public int getRemotePort()
{
if (! _isConnected) {
return -1;
}
return _remotePort;
}
/**
* Get the remote address to which this ZeroTierSocket is connected
* @return Remote address
*/
public InetAddress getRemoteAddress()
{
if (! _isConnected) {
return null;
}
return _remoteAddr;
}
/**
* Get the remote address to which this ZeroTierSocket is connected. Same as getRemoteAddress()
* @return Remote address
*/
public InetAddress getInetAddress()
{
if (! _isConnected) {
return null;
}
return _remoteAddr;
}
/**
* Get the local endpoint address to which this socket is bound to
* @return Local endpoint address
*/
public SocketAddress getLocalSocketAddress()
{
if (! _isConnected) {
return null;
}
return new InetSocketAddress(_remoteAddr, _remotePort);
}
/**
* Return the size of the receive buffer for the ZeroTierSocket's ZeroTierInputStream (SO_RCVBUF)
* @return Size of the receive buffer
* @exception SocketException when an error occurs in the native socket layer
*/
public int getReceiveBufferSize() throws SocketException
{
if (_isClosed) {
throw new SocketException("Error: ZeroTierSocket is closed");
}
return ZeroTierNative.zts_get_recv_buf_size(_zfd);
}
/**
* Return the size of the send buffer for the ZeroTierSocket's ZeroTierOutputStream (SO_SNDBUF)
* @return Size of the send buffer
* @exception SocketException when an error occurs in the native socket layer
*/
public int getSendBufferSize() throws SocketException
{
if (_isClosed) {
throw new SocketException("Error: ZeroTierSocket is closed");
}
return ZeroTierNative.zts_get_send_buf_size(_zfd);
}
/**
* Return whether address reuse is enabled on this ZeroTierSocket (SO_REUSEADDR)
* @return true or false
* @exception SocketException when an error occurs in the native socket layer
*/
public boolean getReuseAddress() throws SocketException
{
if (_isClosed) {
throw new SocketException("Error: ZeroTierSocket is closed");
}
return ZeroTierNative.zts_get_reuse_addr(_zfd) == 1;
}
/**
* Return the amount of time that a ZeroTierSocket will linger after closure (SO_LINGER)
* @return Nothing.
* @exception SocketException when an error occurs in the native socket layer
*/
public int getSoLingerTime() throws SocketException
{
if (_isClosed) {
throw new SocketException("Error: ZeroTierSocket is closed");
}
return ZeroTierNative.zts_get_linger_value(_zfd);
}
/**
* Get the ZeroTierSocket's timeout value (SO_RCVTIMEO)
* @return Nothing.
* @exception SocketException when an error occurs in the native socket layer
*/
public int getSoTimeout() throws SocketException
{
if (_isClosed) {
throw new SocketException("Error: ZeroTierSocket is closed");
}
return ZeroTierNative.zts_get_recv_timeout(_zfd);
}
/**
* Return whether TCP no-delay is enabled (TCP_NODELAY)
* @return true or false
* @exception SocketException when an error occurs in the native socket layer
*/
public boolean tcpNoDelayEnabled() throws SocketException
{
if (_isClosed) {
throw new SocketException("Error: ZeroTierSocket is closed");
}
return ZeroTierNative.zts_get_no_delay(_zfd) == 1;
}
/**
* Return whether this ZeroTierSocket is bound to a local address
* @return true or false
*/
public boolean isBound()
{
return _isBound;
}
/**
* Return whether this ZeroTierSocket has been closed
* @return true or false
*/
public boolean isClosed()
{
return _isClosed;
}
/**
* Return whether this ZeroTierSocket is connected to a remote address
* @return true or false
*/
public boolean isConnected()
{
return _isConnected;
}
/**
* Disable the input-aspect of the ZeroTierSocket.
*
* @exception SocketException when an error occurs in the native socket layer
*/
public void shutdownInput() throws SocketException
{
if (! _isConnected) {
throw new SocketException("Error: ZeroTierSocket is not connected");
}
if (_isClosed) {
throw new SocketException("Error: ZeroTierSocket is closed");
}
if (_inputHasBeenShutdown) {
throw new SocketException("Error: ZeroTierSocket input has been shut down");
}
ZeroTierNative.zts_bsd_shutdown(_zfd, ZeroTierNative.ZTS_SHUT_RD);
_inputHasBeenShutdown = true;
}
/**
* Disable the output-aspect of the ZeroTierSocket.
*
* @exception SocketException when an error occurs in the native socket layer
*/
public void shutdownOutput() throws SocketException
{
if (! _isConnected) {
throw new SocketException("Error: ZeroTierSocket is not connected");
}
if (_isClosed) {
throw new SocketException("Error: ZeroTierSocket is closed");
}
if (_outputHasBeenShutdown) {
throw new SocketException("Error: ZeroTierSocket output has been shut down");
}
ZeroTierNative.zts_bsd_shutdown(_zfd, ZeroTierNative.ZTS_SHUT_WR);
_outputHasBeenShutdown = true;
}
/**
* Return a reference to the ZeroTierInputStream used by this ZeroTierSocket
* @return A reference to the ZeroTierInputStream
* @exception SocketException when an error occurs in the native socket layer
*/
public ZeroTierInputStream getInputStream() throws SocketException
{
if (! _isConnected) {
throw new SocketException("Error: ZeroTierSocket is not connected");
}
if (_isClosed) {
throw new SocketException("Error: ZeroTierSocket is closed");
}
if (_inputHasBeenShutdown) {
throw new SocketException("Error: ZeroTierSocket input has been shut down");
}
return _inputStream;
}
/**
* Return a reference to the ZeroTierOutputStream used by this ZeroTierSocket
* @return A reference to the ZeroTierOutputStream
* @exception SocketException when an error occurs in the native socket layer
*/
public ZeroTierOutputStream getOutputStream() throws SocketException
{
if (! _isConnected) {
throw new SocketException("Error: ZeroTierSocket is not connected");
}
if (_isClosed) {
throw new SocketException("Error: ZeroTierSocket is closed");
}
if (_outputHasBeenShutdown) {
throw new SocketException("Error: ZeroTierSocket output has been shut down");
}
return _outputStream;
}
/**
* Return whether the input-aspect of the ZeroTierSocket has been disabled.
* @return true or false
*/
public boolean inputStreamHasBeenShutdown()
{
return _inputHasBeenShutdown;
}
/**
* Return whether the output-aspect of the ZeroTierSocket has been disabled.
* @return true or false
*/
public boolean outputStreamHasBeenShutdown()
{
return _outputHasBeenShutdown;
}
/**
* Enable or disable the keepalive setting (SO_KEEPALIVE)
* @param enabled Whether SO_KEEPALIVE is enabled.
*
* @exception SocketException when an error occurs in the native socket layer
*/
public void setKeepAliveEnabled(boolean enabled) throws SocketException
{
if (_isClosed) {
throw new SocketException("Error: ZeroTierSocket is closed");
}
if (ZeroTierNative.zts_set_keepalive(_zfd, (enabled ? 1 : 0)) != ZeroTierNative.ZTS_ERR_OK) {
throw new SocketException("Error: Could not set SO_KEEPALIVE");
}
}
/**
* Set the size of the receive buffer for the ZeroTierSocket's ZeroTierInputStream.
* @param bufferSize Size of receive buffer
*
* @exception SocketException when an error occurs in the native socket layer
*/
public void setReceiveBufferSize(int bufferSize) throws SocketException
{
if (_isClosed) {
throw new SocketException("Error: ZeroTierSocket is closed");
}
if (bufferSize <= 0) {
throw new IllegalArgumentException("Error: bufferSize <= 0");
}
if (ZeroTierNative.zts_set_recv_buf_size(_zfd, bufferSize) != ZeroTierNative.ZTS_ERR_OK) {
throw new SocketException("Error: Could not set receive buffer size");
}
}
/**
* Enable or disable the re-use of addresses (SO_REUSEADDR)
* @param enabled Whether SO_REUSEADDR is enabled
*
* @exception SocketException when an error occurs in the native socket layer
*/
public void setReuseAddress(boolean enabled) throws SocketException
{
if (_isClosed) {
throw new SocketException("Error: ZeroTierSocket is closed");
}
if (ZeroTierNative.zts_set_reuse_addr(_zfd, (enabled ? 1 : 0)) != ZeroTierNative.ZTS_ERR_OK) {
throw new SocketException("Error: Could not set SO_REUSEADDR");
}
}
/**
* Set the size of the send buffer for the ZeroTierSocket's ZeroTierOutputStream (SO_SNDBUF)
* @param bufferSize Size of send buffer
*
* @exception SocketException when an error occurs in the native socket layer
*/
public void setSendBufferSize(int bufferSize) throws SocketException
{
if (_isClosed) {
throw new SocketException("Error: ZeroTierSocket is closed");
}
if (bufferSize <= 0) {
throw new IllegalArgumentException("Error: bufferSize <= 0");
}
if (ZeroTierNative.zts_set_send_buf_size(_zfd, bufferSize) != ZeroTierNative.ZTS_ERR_OK) {
throw new SocketException("Error: Could not set SO_SNDBUF");
}
}
/**
* Set the amount of time that a ZeroTierSocket will linger after closure (SO_LINGER)
* @param enabled Whether SO_LINGER is enabled
* @param lingerTime SO_LINGER time
*
* @exception SocketException when an error occurs in the native socket layer
*/
public void setSoLinger(boolean enabled, int lingerTime) throws SocketException
{
if (_isClosed) {
throw new SocketException("Error: ZeroTierSocket is closed");
}
if (lingerTime < 0) {
throw new IllegalArgumentException("Error: lingerTime < 0");
}
if (ZeroTierNative.zts_set_linger(_zfd, (enabled ? 1 : 0), lingerTime) != ZeroTierNative.ZTS_ERR_OK) {
throw new SocketException("Error: Could not set ZTS_SO_LINGER");
}
}
/**
* Set the timeout value for SO_RCVTIMEO
* @param timeout Socket receive timeout value.
*
* @exception SocketException when an error occurs in the native socket layer
*/
public void setSoTimeout(int timeout) throws SocketException
{
if (_isClosed) {
throw new SocketException("Error: ZeroTierSocket is closed");
}
if (timeout < 0) {
throw new IllegalArgumentException("Error: SO_TIMEOUT < 0");
}
// TODO: This is incorrect
if (ZeroTierNative.zts_set_recv_timeout(_zfd, timeout, timeout) != ZeroTierNative.ZTS_ERR_OK) {
throw new SocketException("Error: Could not set SO_RCVTIMEO");
}
}
/**
* Enable or disable TCP_NODELAY
* @param enabled Whether TCP_NODELAY is enabled
*
* @exception SocketException when an error occurs in the native socket layer
*/
public void setTcpNoDelayEnabled(boolean enabled) throws SocketException
{
if (_isClosed) {
throw new SocketException("Error: ZeroTierSocket is closed");
}
if (ZeroTierNative.zts_set_no_delay(_zfd, (enabled ? 1 : 0)) != ZeroTierNative.ZTS_ERR_OK) {
throw new SocketException("Error: Could not set TCP_NODELAY");
}
}
}
|