-
Notifications
You must be signed in to change notification settings - Fork 48
/
ZeroTierServerSocket.java
205 lines (184 loc) · 5.72 KB
/
ZeroTierServerSocket.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
/*
* 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 SocketServer-like behavior over ZeroTier
*
* @author ZeroTier, Inc.
*/
public class ZeroTierServerSocket implements Closeable {
private ZeroTierSocket _socket;
/**
* Create an unbound ZeroTierServerSocket
*/
public ZeroTierServerSocket() throws IOException
{
_socket = new ZeroTierSocket(ZeroTierNative.ZTS_AF_INET6, ZeroTierNative.ZTS_SOCK_STREAM, 0);
}
/**
* Create a ZeroTierServerSocket bound to the given port
*/
public ZeroTierServerSocket(int localPort) throws IOException
{
_socket = new ZeroTierSocket(ZeroTierNative.ZTS_AF_INET6, ZeroTierNative.ZTS_SOCK_STREAM, 0);
_socket.bind("::", localPort);
_socket.listen(0);
}
/**
* Create a ZeroTierServerSocket bound to the given port with a backlog
*/
public ZeroTierServerSocket(int localPort, int backlog) throws IOException
{
_socket = new ZeroTierSocket(ZeroTierNative.ZTS_AF_INET6, ZeroTierNative.ZTS_SOCK_STREAM, 0);
_socket.bind("::", localPort);
_socket.listen(backlog);
}
/**
* Create a ZeroTierServerSocket bound to the given port and local address
*/
public ZeroTierServerSocket(int localPort, int backlog, InetAddress localAddr) throws IOException
{
_socket = new ZeroTierSocket(ZeroTierNative.ZTS_AF_INET6, ZeroTierNative.ZTS_SOCK_STREAM, 0);
_socket.bind(localAddr.getHostAddress(), localPort);
_socket.listen(backlog);
}
/**
* 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
{
return _socket.accept();
}
/**
* Bind to a local address
* @param localAddr Local address to which this socket should bind
*
* @exception IOException when an I/O error occurs
*/
public void bind(SocketAddress localAddr) throws IOException
{
InetSocketAddress inetAddr = (InetSocketAddress)localAddr;
_socket.bind(inetAddr.getHostName(), inetAddr.getPort());
}
/**
* Close the ZeroTierSocket.
*
* @exception IOException when an I/O error occurs
*/
public void close() throws IOException
{
_socket.close();
}
/**
* Get the remote address to which this ZeroTierSocket is bound
* @return Remote address
*/
public InetAddress getInetAddress()
{
return _socket.getLocalAddress();
}
/**
* Get the local port to which this ZeroTierSocket is bound
* @return Local port
*/
public int getLocalPort()
{
return _socket.getLocalPort();
}
/**
* Get the local address to which this ZeroTierSocket is bound
* @return Local address
*/
public SocketAddress getLocalSocketAddress()
{
return _socket.getLocalSocketAddress();
}
/**
* 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 IOException
{
return _socket.getReceiveBufferSize();
}
/**
* 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 IOException
{
return _socket.getReuseAddress();
}
/**
* 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 IOException
{
return _socket.getSoTimeout();
}
/**
* Return whether this ZeroTierSocket is bound to a local address
* @return true or false
*/
public boolean isBound()
{
return _socket.isBound();
}
/**
* Return whether this ZeroTierSocket has been closed
* @return true or false
*/
public boolean isClosed()
{
return _socket.isClosed();
}
/**
* 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 IOException
{
_socket.setReceiveBufferSize(bufferSize);
}
/**
* 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 IOException
{
_socket.setReuseAddress(enabled);
}
/**
* 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 IOException
{
_socket.setSoTimeout(timeout);
}
}