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
|
/*
* ZeroTier One - Network Virtualization Everywhere
* Copyright (C) 2011-2015 ZeroTier, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* --
*
* ZeroTier may be used and distributed under the terms of the GPLv3, which
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
*
* If you would like to embed ZeroTier into a commercial application or
* redistribute it in a modified binary form, please contact ZeroTier Networks
* LLC. Start here: http://www.zerotier.com/
*/
package com.zerotier.one;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import com.zerotier.sdk.Event;
import com.zerotier.sdk.EventListener;
import com.zerotier.sdk.Node;
import com.zerotier.sdk.PacketSender;
import com.zerotier.sdk.ResultCode;
import com.zerotier.sdk.Version;
import com.zerotier.sdk.VirtualNetworkConfig;
import com.zerotier.sdk.VirtualNetworkConfigListener;
import com.zerotier.sdk.VirtualNetworkConfigOperation;
import com.zerotier.sdk.VirtualNetworkFrameListener;
public class OneService extends Thread implements Runnable, PacketSender,
EventListener, VirtualNetworkConfigListener,
VirtualNetworkFrameListener {
private Node _node;
private int _port;
private DatagramSocket _udpSocket;
private ServerSocket _tcpSocket;
private DataStore _ds;
private long _nextBackgroundTaskDeadline = 0;
private final Thread _udpReceiveThread = new Thread() {
@Override
public void run() {
try {
long[] bgtask = new long[1];
byte[] buffer = new byte[16384];
while(true) {
bgtask[0] = 0;
DatagramPacket p = new DatagramPacket(buffer, buffer.length);
try {
_udpSocket.receive(p);
if(p.getLength() > 0)
{
System.out.println("Got Data From: " + p.getAddress().toString() +":" + p.getPort());
_node.processWirePacket(System.currentTimeMillis(), new InetSocketAddress(p.getAddress(), p.getPort()), 0, p.getData(), bgtask);
_nextBackgroundTaskDeadline = bgtask[0];
}
} catch (SocketTimeoutException e) {}
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
public OneService(DataStoreFileProvider prov, int port) {
this._port = port;
this._ds = new DataStore(prov);
try {
_udpSocket = new DatagramSocket(_port);
_udpSocket.setSoTimeout(100);
_tcpSocket = new ServerSocket();
_tcpSocket.bind(new InetSocketAddress("127.0.0.1", _port));
} catch (SocketException e) {
e.printStackTrace();
return;
} catch (IOException e) {
e.printStackTrace();
return;
}
_udpReceiveThread.start();
_node = new Node(
System.currentTimeMillis(),
_ds,
_ds,
this,
this,
this,
this);
}
@Override
public void run() {
if(_node == null)
return;
while(true) {
try {
long dl = _nextBackgroundTaskDeadline;
long now = System.currentTimeMillis();
if (dl <= now) {
long[] returnDeadline = {0};
ResultCode rc = _node.processBackgroundTasks(now, returnDeadline);
_nextBackgroundTaskDeadline = returnDeadline[0];
if(rc != ResultCode.RESULT_OK) {
System.out.println(rc.toString());
}
}
long delay = (dl > now) ? (dl - now) : 100;
Thread.sleep(delay);
} catch (Exception ex) {
System.out.println("Exception in run loop: " + ex.getMessage());
ex.printStackTrace();
}
}
}
@Override
public int onSendPacketRequested(InetSocketAddress addr,
int linkDesperation, byte[] packetData) {
System.out.println("onSendPacketRequested to: " + addr.getHostString() +":"+ addr.getPort() + " ");
if(_udpSocket == null)
return -1;
try {
DatagramPacket p = new DatagramPacket(packetData, packetData.length, addr);
_udpSocket.send(p);
System.out.println("Sent");
} catch (Exception e) {
System.out.println("Error sending datagram: " + e.getMessage());
return -1;
}
return 0;
}
@Override
public void onVirtualNetworkFrame(long nwid, long srcMac, long destMac,
long etherType, long vlanId, byte[] frameData) {
// TODO Auto-generated method stub
}
@Override
public int onNetworkConfigurationUpdated(long nwid,
VirtualNetworkConfigOperation op, VirtualNetworkConfig config) {
// TODO Auto-generated method stub
return 0;
}
@Override
public void onEvent(Event event) {
// TODO Auto-generated method stub
}
@Override
public void onNetworkError(Event event, InetSocketAddress source) {
// TODO Auto-generated method stub
}
@Override
public void onOutOfDate(Version newVersion) {
// TODO Auto-generated method stub
}
@Override
public void onTrace(String message) {
// TODO Auto-generated method stub
}
}
|