diff options
author | Florian Dold <florian.dold@gmail.com> | 2013-09-04 11:20:04 +0000 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2013-09-04 11:20:04 +0000 |
commit | 31621bb353138c8c5db7f54c103f513bba2e28d1 (patch) | |
tree | 4b90af04c51eda9ed3e3d9dc37c76d0970841a7f /src | |
parent | e88b6c3c7d70ce630d4b55ba226ef1b739f509c6 (diff) | |
download | gnunet-java-31621bb353138c8c5db7f54c103f513bba2e28d1.tar.gz gnunet-java-31621bb353138c8c5db7f54c103f513bba2e28d1.zip |
- blacklist is now has its own handle
- mesh fixed and tested
- voting template
- Ed25519+ecdh implemented and tested
Diffstat (limited to 'src')
31 files changed, 893 insertions, 204 deletions
diff --git a/src/main/java/org/gnunet/construct/MsgMap.txt b/src/main/java/org/gnunet/construct/MsgMap.txt deleted file mode 100644 index 30a4656..0000000 --- a/src/main/java/org/gnunet/construct/MsgMap.txt +++ /dev/null | |||
@@ -1,46 +0,0 @@ | |||
1 | org.gnunet.util.Resolver$Address|0=org.gnunet.util.Resolver$TextualAddress | ||
2 | org.gnunet.util.Resolver$Address|1=org.gnunet.util.Resolver$NumericAddress | ||
3 | org.gnunet.util.GnunetMessage$Body|68=org.gnunet.core.DisconnectNotifyMessage | ||
4 | org.gnunet.util.GnunetMessage$Body|274=org.gnunet.mesh.TunnelDestroyMessage | ||
5 | org.gnunet.util.GnunetMessage$Body|1=org.gnunet.util.TestMessage | ||
6 | org.gnunet.util.GnunetMessage$Body|70=org.gnunet.core.NotifyInboundTrafficMessage | ||
7 | org.gnunet.util.GnunetMessage$Body|273=org.gnunet.mesh.TunnelCreateMessage | ||
8 | org.gnunet.util.GnunetMessage$Body|71=org.gnunet.core.NotifyOutboundTrafficMessage | ||
9 | org.gnunet.util.GnunetMessage$Body|272=org.gnunet.mesh.ClientConnectMessage | ||
10 | org.gnunet.util.GnunetMessage$Body|64=org.gnunet.core.InitMessage | ||
11 | org.gnunet.util.GnunetMessage$Body|4=org.gnunet.util.Resolver$GetMessage | ||
12 | org.gnunet.util.GnunetMessage$Body|65=org.gnunet.core.InitReplyMessage | ||
13 | org.gnunet.util.GnunetMessage$Body|5=org.gnunet.util.Resolver$ResolverResponse | ||
14 | org.gnunet.util.GnunetMessage$Body|143=org.gnunet.dht.ClientGetMessage | ||
15 | org.gnunet.util.GnunetMessage$Body|67=org.gnunet.core.ConnectNotifyMessage | ||
16 | org.gnunet.util.GnunetMessage$Body|142=org.gnunet.dht.ClientPutMessage | ||
17 | org.gnunet.util.GnunetMessage$Body|76=org.gnunet.core.SendMessage | ||
18 | org.gnunet.util.GnunetMessage$Body|286=org.gnunet.mesh.LocalAckMessage | ||
19 | org.gnunet.util.GnunetMessage$Body|74=org.gnunet.core.SendMessageRequest | ||
20 | org.gnunet.util.GnunetMessage$Body|75=org.gnunet.core.SendMessageReady | ||
21 | org.gnunet.util.GnunetMessage$Body|153=org.gnunet.dht.MonitorStartStop | ||
22 | org.gnunet.util.GnunetMessage$Body|155=org.gnunet.dht.ClientPutConfirmationMessage | ||
23 | org.gnunet.util.GnunetMessage$Body|323=org.gnunet.nse.UpdateMessage | ||
24 | org.gnunet.util.GnunetMessage$Body|260=org.gnunet.mesh.DataMessage | ||
25 | org.gnunet.util.GnunetMessage$Body|321=org.gnunet.nse.StartMessage | ||
26 | org.gnunet.util.GnunetMessage$Body|144=org.gnunet.dht.ClientGetStopMessage | ||
27 | org.gnunet.util.GnunetMessage$Body|145=org.gnunet.dht.ClientResultMessage | ||
28 | org.gnunet.util.GnunetMessage$Body|332=org.gnunet.peerinfo.InfoMessage | ||
29 | org.gnunet.util.GnunetMessage$Body|333=org.gnunet.peerinfo.InfoEnd | ||
30 | org.gnunet.util.GnunetMessage$Body|149=org.gnunet.dht.MonitorGetMessage | ||
31 | org.gnunet.util.GnunetMessage$Body|331=org.gnunet.peerinfo.ListAllPeersMessage | ||
32 | org.gnunet.util.GnunetMessage$Body|150=org.gnunet.dht.MonitorGetRespMessage | ||
33 | org.gnunet.util.GnunetMessage$Body|151=org.gnunet.dht.MonitorPutMessage | ||
34 | org.gnunet.util.GnunetMessage$Body|171=org.gnunet.statistics.GetResponseEndMessage | ||
35 | org.gnunet.util.GnunetMessage$Body|170=org.gnunet.statistics.GetResponseMessage | ||
36 | org.gnunet.util.GnunetMessage$Body|169=org.gnunet.statistics.GetMessage | ||
37 | org.gnunet.util.GnunetMessage$Body|168=org.gnunet.statistics.SetMessage | ||
38 | org.gnunet.util.GnunetMessage$Body|374=org.gnunet.transport.RequestConnectMessage | ||
39 | org.gnunet.util.GnunetMessage$Body|173=org.gnunet.statistics.WatchResponseMessage | ||
40 | org.gnunet.util.GnunetMessage$Body|172=org.gnunet.statistics.WatchMessage | ||
41 | org.gnunet.util.GnunetMessage$Body|524=org.gnunet.consensus.ConcludeMessage | ||
42 | org.gnunet.util.GnunetMessage$Body|521=org.gnunet.consensus.InsertElementMessage | ||
43 | org.gnunet.util.GnunetMessage$Body|523=org.gnunet.consensus.NewElementMessage | ||
44 | org.gnunet.util.GnunetMessage$Body|360=org.gnunet.transport.StartMessage | ||
45 | org.gnunet.construct.MessageUnion|525=org.gnunet.consensus.ConcludeDoneMessage | ||
46 | # generated 2013/08/22 21:14:59 | ||
diff --git a/src/main/java/org/gnunet/construct/ReflectUtil.java b/src/main/java/org/gnunet/construct/ReflectUtil.java index 1115641..b05a829 100644 --- a/src/main/java/org/gnunet/construct/ReflectUtil.java +++ b/src/main/java/org/gnunet/construct/ReflectUtil.java | |||
@@ -78,7 +78,7 @@ public class ReflectUtil { | |||
78 | * An enumeration of all built-in type that can store integers. | 78 | * An enumeration of all built-in type that can store integers. |
79 | */ | 79 | */ |
80 | public enum NumFieldType { | 80 | public enum NumFieldType { |
81 | BIGNUM, BYTE_PRIM, SHORT_PRIM, INT_PRIM, LONG_PRIM, CHAR_PRIM | 81 | BIGNUM, BYTE_PRIM, SHORT_PRIM, INT_PRIM, LONG_PRIM, BOOLEAN, CHAR_PRIM |
82 | } | 82 | } |
83 | 83 | ||
84 | /** | 84 | /** |
@@ -107,6 +107,8 @@ public class ReflectUtil { | |||
107 | targetType = NumFieldType.CHAR_PRIM; | 107 | targetType = NumFieldType.CHAR_PRIM; |
108 | } else if (f.getType().equals(BigInteger.class)) { | 108 | } else if (f.getType().equals(BigInteger.class)) { |
109 | targetType = NumFieldType.BIGNUM; | 109 | targetType = NumFieldType.BIGNUM; |
110 | } else if (f.getType().equals(Boolean.TYPE)) { | ||
111 | targetType = NumFieldType.BOOLEAN; | ||
110 | } else { | 112 | } else { |
111 | throw new AssertionError( | 113 | throw new AssertionError( |
112 | "expected numeric type, got: " + f.getType()); | 114 | "expected numeric type, got: " + f.getType()); |
@@ -134,6 +136,9 @@ public class ReflectUtil { | |||
134 | case BIGNUM: | 136 | case BIGNUM: |
135 | targetField.set(obj, BigInteger.valueOf(val)); | 137 | targetField.set(obj, BigInteger.valueOf(val)); |
136 | break; | 138 | break; |
139 | case BOOLEAN: | ||
140 | targetField.setBoolean(obj, (val != 0)); | ||
141 | break; | ||
137 | } | 142 | } |
138 | } catch (IllegalArgumentException e) { | 143 | } catch (IllegalArgumentException e) { |
139 | throw new AssertionError("cannot access field"); | 144 | throw new AssertionError("cannot access field"); |
@@ -163,8 +168,10 @@ public class ReflectUtil { | |||
163 | return targetField.getByte(obj); | 168 | return targetField.getByte(obj); |
164 | case CHAR_PRIM: | 169 | case CHAR_PRIM: |
165 | return targetField.getChar(obj); | 170 | return targetField.getChar(obj); |
171 | case BOOLEAN: | ||
172 | return targetField.getBoolean(obj) ? 1 : 0; | ||
166 | case BIGNUM: | 173 | case BIGNUM: |
167 | throw new RuntimeException("get() called on NumField that is a BigInteger"); | 174 | throw new RuntimeException("get() called on NumField that is a BigInteger (getBig() must be used instead)"); |
168 | default: | 175 | default: |
169 | throw new AssertionError("unreachable"); | 176 | throw new AssertionError("unreachable"); |
170 | } | 177 | } |
diff --git a/src/main/java/org/gnunet/construct/parsers/IntegerParser.java b/src/main/java/org/gnunet/construct/parsers/IntegerParser.java index 4f71aa5..e991f41 100644 --- a/src/main/java/org/gnunet/construct/parsers/IntegerParser.java +++ b/src/main/java/org/gnunet/construct/parsers/IntegerParser.java | |||
@@ -21,9 +21,11 @@ | |||
21 | package org.gnunet.construct.parsers; | 21 | package org.gnunet.construct.parsers; |
22 | 22 | ||
23 | import org.gnunet.construct.Message; | 23 | import org.gnunet.construct.Message; |
24 | import org.gnunet.construct.ProtocolViolationException; | ||
24 | import org.gnunet.construct.ReflectUtil; | 25 | import org.gnunet.construct.ReflectUtil; |
25 | 26 | ||
26 | import java.lang.reflect.Field; | 27 | import java.lang.reflect.Field; |
28 | import java.nio.BufferUnderflowException; | ||
27 | import java.nio.ByteBuffer; | 29 | import java.nio.ByteBuffer; |
28 | import java.util.List; | 30 | import java.util.List; |
29 | 31 | ||
@@ -59,10 +61,14 @@ public class IntegerParser implements Parser { | |||
59 | @Override | 61 | @Override |
60 | public int parse(final ByteBuffer srcBuf, int frameOffset, Message frameObj, final Message dstObj, List<Field> | 62 | public int parse(final ByteBuffer srcBuf, int frameOffset, Message frameObj, final Message dstObj, List<Field> |
61 | frameSizePath) { | 63 | frameSizePath) { |
62 | if (targetField.isBig()) { | 64 | try { |
63 | targetField.set(dstObj, IntegerUtil.readBigInteger(srcBuf, isSigned, byteSize)); | 65 | if (targetField.isBig()) { |
64 | } else { | 66 | targetField.set(dstObj, IntegerUtil.readBigInteger(srcBuf, isSigned, byteSize)); |
65 | targetField.set(dstObj, IntegerUtil.readLong(srcBuf, isSigned, byteSize)); | 67 | } else { |
68 | targetField.set(dstObj, IntegerUtil.readLong(srcBuf, isSigned, byteSize)); | ||
69 | } | ||
70 | } catch (BufferUnderflowException e) { | ||
71 | throw new ProtocolViolationException("Underflow while parsing " + targetField); | ||
66 | } | 72 | } |
67 | return byteSize; | 73 | return byteSize; |
68 | } | 74 | } |
diff --git a/src/main/java/org/gnunet/core/Core.java b/src/main/java/org/gnunet/core/Core.java index 2895e41..02756db 100644 --- a/src/main/java/org/gnunet/core/Core.java +++ b/src/main/java/org/gnunet/core/Core.java | |||
@@ -292,6 +292,24 @@ public class Core { | |||
292 | } | 292 | } |
293 | 293 | ||
294 | /** | 294 | /** |
295 | * Helper function to retrieve the peer identity with the given configuration via CORE. | ||
296 | * Should <b>not</b> be used unless there is no other means to obtain the peer identity. | ||
297 | * | ||
298 | * @param cfg | ||
299 | * @param cont | ||
300 | */ | ||
301 | public static void withPeerIdentity(Configuration cfg, final PeerIdentityContinuation cont) { | ||
302 | final Core core = new Core(cfg); | ||
303 | core.init(new InitCallback() { | ||
304 | @Override | ||
305 | public void onInit(PeerIdentity myIdentity) { | ||
306 | core.disconnect(); | ||
307 | cont.cont(myIdentity); | ||
308 | } | ||
309 | }); | ||
310 | } | ||
311 | |||
312 | /** | ||
295 | * Observe outgoing message headers from core. | 313 | * Observe outgoing message headers from core. |
296 | * @param h callback | 314 | * @param h callback |
297 | */ | 315 | */ |
diff --git a/src/main/java/org/gnunet/core/PeerIdentityContinuation.java b/src/main/java/org/gnunet/core/PeerIdentityContinuation.java new file mode 100644 index 0000000..e29af45 --- /dev/null +++ b/src/main/java/org/gnunet/core/PeerIdentityContinuation.java | |||
@@ -0,0 +1,7 @@ | |||
1 | package org.gnunet.core; | ||
2 | |||
3 | import org.gnunet.util.PeerIdentity; | ||
4 | |||
5 | public interface PeerIdentityContinuation { | ||
6 | public void cont(PeerIdentity peerIdentity); | ||
7 | } | ||
diff --git a/src/main/java/org/gnunet/mesh/DataMessage.java b/src/main/java/org/gnunet/mesh/DataMessage.java index 92546c8..a5ced12 100644 --- a/src/main/java/org/gnunet/mesh/DataMessage.java +++ b/src/main/java/org/gnunet/mesh/DataMessage.java | |||
@@ -9,10 +9,10 @@ import org.gnunet.util.PeerIdentity; | |||
9 | * | 9 | * |
10 | * @author Florian Dold | 10 | * @author Florian Dold |
11 | */ | 11 | */ |
12 | @UnionCase(260) | 12 | @UnionCase(285) |
13 | public class DataMessage implements GnunetMessage.Body { | 13 | public class DataMessage implements GnunetMessage.Body { |
14 | @UInt32 | 14 | @UInt32 |
15 | public int tid; | 15 | public long tid; |
16 | @FillWith | 16 | @FillWith |
17 | @UInt8 | 17 | @UInt8 |
18 | public byte[] payload; | 18 | public byte[] payload; |
diff --git a/src/main/java/org/gnunet/mesh/LocalAckMessage.java b/src/main/java/org/gnunet/mesh/LocalAckMessage.java index 6a09411..4466009 100644 --- a/src/main/java/org/gnunet/mesh/LocalAckMessage.java +++ b/src/main/java/org/gnunet/mesh/LocalAckMessage.java | |||
@@ -12,7 +12,5 @@ import org.gnunet.util.GnunetMessage; | |||
12 | @UnionCase(286) | 12 | @UnionCase(286) |
13 | public class LocalAckMessage implements GnunetMessage.Body { | 13 | public class LocalAckMessage implements GnunetMessage.Body { |
14 | @UInt32 | 14 | @UInt32 |
15 | public int tid; | 15 | public long tid; |
16 | @UInt32 | ||
17 | public int maxPid; | ||
18 | } | 16 | } |
diff --git a/src/main/java/org/gnunet/mesh/Mesh.java b/src/main/java/org/gnunet/mesh/Mesh.java index a6bf602..b66cc6f 100644 --- a/src/main/java/org/gnunet/mesh/Mesh.java +++ b/src/main/java/org/gnunet/mesh/Mesh.java | |||
@@ -47,13 +47,13 @@ public class Mesh { | |||
47 | * For tunnels created by the client, the bit in this | 47 | * For tunnels created by the client, the bit in this |
48 | * mask is always set. | 48 | * mask is always set. |
49 | */ | 49 | */ |
50 | private static final int TUNNEL_ID_CLI = 0x80000000; | 50 | private static final long TUNNEL_ID_CLI = 0x80000000L; |
51 | 51 | ||
52 | /** | 52 | /** |
53 | * For tunnels created by the server, the bit in this | 53 | * For tunnels created by the server, the bit in this |
54 | * mask is always set. | 54 | * mask is always set. |
55 | */ | 55 | */ |
56 | private static final int TUNNEL_ID_SERV = 0xB0000000; | 56 | private static final long TUNNEL_ID_SERV = 0xB0000000L; |
57 | 57 | ||
58 | /** | 58 | /** |
59 | * Disable buffering on intermediate nodes (for minimum latency). | 59 | * Disable buffering on intermediate nodes (for minimum latency). |
@@ -96,13 +96,13 @@ public class Mesh { | |||
96 | /** | 96 | /** |
97 | * Mapping from the tunnel's ID to the tunnel object. | 97 | * Mapping from the tunnel's ID to the tunnel object. |
98 | */ | 98 | */ |
99 | private Map<Integer,Tunnel> tunnelMap = new HashMap<Integer,Tunnel>(); | 99 | private Map<Long,Tunnel> tunnelMap = new HashMap<Long,Tunnel>(); |
100 | 100 | ||
101 | /** | 101 | /** |
102 | * Counter for generating fresh tunnel ID's | 102 | * Counter for generating fresh tunnel ID's |
103 | * when creating new tunnels. | 103 | * when creating new tunnels. |
104 | */ | 104 | */ |
105 | private int next_tid = 1; | 105 | private long next_tid = 1; |
106 | 106 | ||
107 | /** | 107 | /** |
108 | * A tunnel to a remote peer. | 108 | * A tunnel to a remote peer. |
@@ -113,9 +113,9 @@ public class Mesh { | |||
113 | private final int opt; | 113 | private final int opt; |
114 | public final PeerIdentity peer; | 114 | public final PeerIdentity peer; |
115 | public final int port; | 115 | public final int port; |
116 | protected int tunnelId; | 116 | protected long tunnelId; |
117 | private boolean receive_done_expected = false; | 117 | private boolean receive_done_expected = false; |
118 | int ack_count = 1; | 118 | int ack_count = 0; |
119 | 119 | ||
120 | /** | 120 | /** |
121 | * Canceler for the currently submitted envelope. | 121 | * Canceler for the currently submitted envelope. |
@@ -153,19 +153,25 @@ public class Mesh { | |||
153 | * @param nobuffer | 153 | * @param nobuffer |
154 | * @param reliable | 154 | * @param reliable |
155 | */ | 155 | */ |
156 | public Tunnel(PeerIdentity peer, int tunnelId, int port, boolean nobuffer, boolean reliable) { | 156 | public Tunnel(PeerIdentity peer, long tunnelId, int port, boolean nobuffer, boolean reliable) { |
157 | int my_opt = 0; | 157 | int my_opt = 0; |
158 | if (reliable) | 158 | if (reliable) |
159 | my_opt |= OPTION_RELIABLE; | 159 | my_opt |= OPTION_RELIABLE; |
160 | if (nobuffer) | 160 | if (nobuffer) |
161 | my_opt |= OPTION_NOBUFFER; | 161 | my_opt |= OPTION_NOBUFFER; |
162 | if (0 == tunnelId) | 162 | if (0 == tunnelId) { |
163 | this.tunnelId = ((next_tid++) | TUNNEL_ID_CLI) & ~TUNNEL_ID_SERV; | 163 | this.tunnelId = next_tid++; |
164 | else | 164 | this.tunnelId &= ~TUNNEL_ID_SERV; |
165 | this.tunnelId |= TUNNEL_ID_CLI; | ||
166 | } | ||
167 | else { | ||
165 | this.tunnelId = tunnelId; | 168 | this.tunnelId = tunnelId; |
169 | } | ||
166 | this.peer = peer; | 170 | this.peer = peer; |
167 | this.port = port; | 171 | this.port = port; |
168 | this.opt = my_opt; | 172 | this.opt = my_opt; |
173 | logger.debug("registering tunnel {}", this.tunnelId); | ||
174 | tunnelMap.put(this.tunnelId, this); | ||
169 | } | 175 | } |
170 | 176 | ||
171 | public void receiveDone() { | 177 | public void receiveDone() { |
@@ -180,20 +186,24 @@ public class Mesh { | |||
180 | public void destroy() { | 186 | public void destroy() { |
181 | TunnelDestroyMessage m = new TunnelDestroyMessage(); | 187 | TunnelDestroyMessage m = new TunnelDestroyMessage(); |
182 | m.tunnel_id = tunnelId; | 188 | m.tunnel_id = tunnelId; |
189 | m.reserved = new byte[32]; | ||
183 | client.send(m); | 190 | client.send(m); |
191 | tunnelMap.remove(m.tunnel_id); | ||
184 | } | 192 | } |
185 | 193 | ||
186 | @Override | 194 | @Override |
187 | protected void submit(Envelope ev) { | 195 | protected void submit(Envelope ev) { |
196 | logger.debug("submitting data message on tunnel {}", tunnelId); | ||
188 | if (ack_count <= 0) | 197 | if (ack_count <= 0) |
189 | throw new AssertionError(); | 198 | throw new AssertionError(); |
190 | DataMessage m = new DataMessage(); | 199 | DataMessage m = new DataMessage(); |
191 | m.payload = Construct.toBinary(GnunetMessage.fromBody(ev.message)); | 200 | m.payload = Construct.toBinary(GnunetMessage.fromBody(ev.message)); |
201 | m.tid = tunnelId; | ||
192 | Envelope mesh_ev = new Envelope(m); | 202 | Envelope mesh_ev = new Envelope(m); |
193 | mesh_ev.notifySent(new NotifySentHandler() { | 203 | mesh_ev.notifySent(new NotifySentHandler() { |
194 | @Override | 204 | @Override |
195 | public void onSent() { | 205 | public void onSent() { |
196 | envelopeCanceler = null; | 206 | envelopeCanceler = null; reportMessageSent(); |
197 | } | 207 | } |
198 | }); | 208 | }); |
199 | client.send(mesh_ev); | 209 | client.send(mesh_ev); |
@@ -217,12 +227,20 @@ public class Mesh { | |||
217 | context = newContext; | 227 | context = newContext; |
218 | } | 228 | } |
219 | 229 | ||
230 | public void handleAck() { | ||
231 | ack_count++; | ||
232 | logger.debug("got ack for tunnel id " + tunnelId); | ||
233 | if (ack_count == 1) { | ||
234 | reportReadyForSubmit(); | ||
235 | } | ||
236 | } | ||
220 | } | 237 | } |
221 | 238 | ||
222 | private class MeshMessageReceiver extends RunaboutMessageReceiver { | 239 | private class MeshMessageReceiver extends RunaboutMessageReceiver { |
223 | public void visit(TunnelCreateMessage m) { | 240 | public void visit(TunnelCreateMessage m) { |
224 | Tunnel t = new Tunnel(m.otherEnd, m.tunnel_id, m.port, | 241 | Tunnel t = new Tunnel(m.otherEnd, m.tunnel_id, m.port, |
225 | (m.opt & OPTION_NOBUFFER) != 0, (m.opt & OPTION_NOBUFFER) != 0); | 242 | (m.opt & OPTION_NOBUFFER) != 0, (m.opt & OPTION_NOBUFFER) != 0); |
243 | logger.debug("inbound tunnel {}", m.tunnel_id); | ||
226 | if (inboundTunnelHandler != null) { | 244 | if (inboundTunnelHandler != null) { |
227 | inboundTunnelHandler.onInboundTunnel(t, m.otherEnd); | 245 | inboundTunnelHandler.onInboundTunnel(t, m.otherEnd); |
228 | } | 246 | } |
@@ -240,9 +258,13 @@ public class Mesh { | |||
240 | } | 258 | } |
241 | 259 | ||
242 | public void visit(LocalAckMessage m) { | 260 | public void visit(LocalAckMessage m) { |
261 | logger.debug("got LocalAckMessage for {}", m.tid); | ||
243 | Tunnel t = tunnelMap.get(m.tid); | 262 | Tunnel t = tunnelMap.get(m.tid); |
244 | if (t != null) | 263 | if (t != null) { |
245 | t.ack_count += 1; | 264 | t.handleAck(); |
265 | } else { | ||
266 | logger.warn("tunnel id for local ack not found"); | ||
267 | } | ||
246 | } | 268 | } |
247 | 269 | ||
248 | public void visit(TunnelDestroyMessage m) { | 270 | public void visit(TunnelDestroyMessage m) { |
@@ -286,6 +308,8 @@ public class Mesh { | |||
286 | this.ports = ports; | 308 | this.ports = ports; |
287 | this.inboundTunnelHandler = inboundTunnelHandler; | 309 | this.inboundTunnelHandler = inboundTunnelHandler; |
288 | 310 | ||
311 | logger.debug("mesh handle created"); | ||
312 | |||
289 | client = new Client("mesh", cfg); | 313 | client = new Client("mesh", cfg); |
290 | client.installReceiver(new MeshMessageReceiver()); | 314 | client.installReceiver(new MeshMessageReceiver()); |
291 | ClientConnectMessage ccm = new ClientConnectMessage(); | 315 | ClientConnectMessage ccm = new ClientConnectMessage(); |
diff --git a/src/main/java/org/gnunet/mesh/TunnelCreateMessage.java b/src/main/java/org/gnunet/mesh/TunnelCreateMessage.java index eaa4d6c..84ea035 100644 --- a/src/main/java/org/gnunet/mesh/TunnelCreateMessage.java +++ b/src/main/java/org/gnunet/mesh/TunnelCreateMessage.java | |||
@@ -14,7 +14,7 @@ import org.gnunet.util.PeerIdentity; | |||
14 | @UnionCase(273) | 14 | @UnionCase(273) |
15 | public class TunnelCreateMessage implements GnunetMessage.Body { | 15 | public class TunnelCreateMessage implements GnunetMessage.Body { |
16 | @UInt32 | 16 | @UInt32 |
17 | public int tunnel_id; | 17 | public long tunnel_id; |
18 | 18 | ||
19 | @NestedMessage(optional = false) | 19 | @NestedMessage(optional = false) |
20 | public PeerIdentity otherEnd; | 20 | public PeerIdentity otherEnd; |
diff --git a/src/main/java/org/gnunet/mesh/TunnelDestroyMessage.java b/src/main/java/org/gnunet/mesh/TunnelDestroyMessage.java index bce60bb..3fa973f 100644 --- a/src/main/java/org/gnunet/mesh/TunnelDestroyMessage.java +++ b/src/main/java/org/gnunet/mesh/TunnelDestroyMessage.java | |||
@@ -1,8 +1,11 @@ | |||
1 | package org.gnunet.mesh; | 1 | package org.gnunet.mesh; |
2 | 2 | ||
3 | import org.gnunet.construct.FixedSizeIntegerArray; | ||
4 | import org.gnunet.construct.NestedMessage; | ||
3 | import org.gnunet.construct.UInt32; | 5 | import org.gnunet.construct.UInt32; |
4 | import org.gnunet.construct.UnionCase; | 6 | import org.gnunet.construct.UnionCase; |
5 | import org.gnunet.util.GnunetMessage; | 7 | import org.gnunet.util.GnunetMessage; |
8 | import org.gnunet.util.PeerIdentity; | ||
6 | 9 | ||
7 | /** | 10 | /** |
8 | * ... | 11 | * ... |
@@ -12,5 +15,13 @@ import org.gnunet.util.GnunetMessage; | |||
12 | @UnionCase(274) | 15 | @UnionCase(274) |
13 | public class TunnelDestroyMessage implements GnunetMessage.Body { | 16 | public class TunnelDestroyMessage implements GnunetMessage.Body { |
14 | @UInt32 | 17 | @UInt32 |
15 | public int tunnel_id; | 18 | public long tunnel_id; |
19 | |||
20 | @FixedSizeIntegerArray(bitSize = 8, signed = false, length = 32) | ||
21 | public byte[] reserved; | ||
22 | @UInt32 | ||
23 | public int port; | ||
24 | |||
25 | @UInt32 | ||
26 | public int opt; | ||
16 | } | 27 | } |
diff --git a/src/main/java/org/gnunet/mq/MessageQueue.java b/src/main/java/org/gnunet/mq/MessageQueue.java index 4df3ae4..cab1003 100644 --- a/src/main/java/org/gnunet/mq/MessageQueue.java +++ b/src/main/java/org/gnunet/mq/MessageQueue.java | |||
@@ -11,10 +11,19 @@ import java.util.LinkedList; | |||
11 | public abstract class MessageQueue { | 11 | public abstract class MessageQueue { |
12 | private LinkedList<Envelope> queued_envelopes = new LinkedList<Envelope>(); | 12 | private LinkedList<Envelope> queued_envelopes = new LinkedList<Envelope>(); |
13 | private LinkedList<Envelope> prefered_queued_envelopes = new LinkedList<Envelope>(); | 13 | private LinkedList<Envelope> prefered_queued_envelopes = new LinkedList<Envelope>(); |
14 | protected Envelope current_envelope; | 14 | protected Envelope currentEnvelope; |
15 | private boolean readyForSubmit; | ||
15 | 16 | ||
17 | /** | ||
18 | * Submit a message. Once the message can't be canceled, | ||
19 | * reportMessageSent must be called. | ||
20 | * @param ev envelope to submit | ||
21 | */ | ||
16 | protected abstract void submit(Envelope ev); | 22 | protected abstract void submit(Envelope ev); |
17 | 23 | ||
24 | /** | ||
25 | * Cancel a message that already has been submitted. | ||
26 | */ | ||
18 | protected abstract void retract(); | 27 | protected abstract void retract(); |
19 | 28 | ||
20 | public void send(GnunetMessage.Body body) { | 29 | public void send(GnunetMessage.Body body) { |
@@ -25,6 +34,16 @@ public abstract class MessageQueue { | |||
25 | sendPrefered(new Envelope(body)); | 34 | sendPrefered(new Envelope(body)); |
26 | } | 35 | } |
27 | 36 | ||
37 | public void send(Envelope ev) { | ||
38 | queued_envelopes.addLast(ev); | ||
39 | trySubmitNext(); | ||
40 | } | ||
41 | |||
42 | public void sendPrefered(Envelope ev) { | ||
43 | prefered_queued_envelopes.addLast(ev); | ||
44 | trySubmitNext(); | ||
45 | } | ||
46 | |||
28 | private Envelope pollNextEnvelope() { | 47 | private Envelope pollNextEnvelope() { |
29 | if (!prefered_queued_envelopes.isEmpty()) | 48 | if (!prefered_queued_envelopes.isEmpty()) |
30 | return prefered_queued_envelopes.removeFirst(); | 49 | return prefered_queued_envelopes.removeFirst(); |
@@ -33,36 +52,33 @@ public abstract class MessageQueue { | |||
33 | return null; | 52 | return null; |
34 | } | 53 | } |
35 | 54 | ||
36 | public void send(Envelope ev) { | 55 | protected void trySubmitNext() { |
37 | if (null == current_envelope) { | 56 | if (currentEnvelope != null || !readyForSubmit) { |
38 | current_envelope = ev; | 57 | return; |
39 | submit(current_envelope); | 58 | } |
40 | } else { | 59 | Envelope ev = pollNextEnvelope(); |
41 | queued_envelopes.addLast(ev); | 60 | if (ev == null) { |
61 | return; | ||
42 | } | 62 | } |
63 | currentEnvelope = ev; | ||
64 | readyForSubmit = false; | ||
65 | submit(currentEnvelope); | ||
43 | } | 66 | } |
44 | 67 | ||
45 | public void sendPrefered(Envelope ev) { | 68 | protected void reportReadyForSubmit() { |
46 | if (null == current_envelope) { | 69 | if (readyForSubmit) { |
47 | current_envelope = ev; | 70 | throw new AssertionError("message queue reported 'ready for submit' twice"); |
48 | submit(current_envelope); | ||
49 | } else { | ||
50 | prefered_queued_envelopes.addLast(ev); | ||
51 | } | 71 | } |
72 | readyForSubmit = true; | ||
73 | trySubmitNext(); | ||
52 | } | 74 | } |
53 | 75 | ||
54 | protected void reportMessageSent() { | 76 | protected void reportMessageSent() { |
55 | if (null == current_envelope) | 77 | if (null == currentEnvelope) |
56 | throw new AssertionError(); | 78 | throw new AssertionError(); |
57 | current_envelope.invokeSentNotification(); | 79 | currentEnvelope.invokeSentNotification(); |
58 | next(); | 80 | currentEnvelope = null; |
59 | } | 81 | trySubmitNext(); |
60 | |||
61 | private void next() { | ||
62 | current_envelope = pollNextEnvelope(); | ||
63 | if (current_envelope == null) | ||
64 | return; | ||
65 | submit(current_envelope); | ||
66 | } | 82 | } |
67 | 83 | ||
68 | /** | 84 | /** |
@@ -71,11 +87,11 @@ public abstract class MessageQueue { | |||
71 | * @param ev the envelope to cancel | 87 | * @param ev the envelope to cancel |
72 | */ | 88 | */ |
73 | /* pkg-private */ void cancelEnvelope(Envelope ev) { | 89 | /* pkg-private */ void cancelEnvelope(Envelope ev) { |
74 | if (null == current_envelope) | 90 | if (null == currentEnvelope) |
75 | throw new AssertionError(); | 91 | throw new AssertionError(); |
76 | if (ev == current_envelope) { | 92 | if (ev == currentEnvelope) { |
77 | retract(); | 93 | retract(); |
78 | next(); | 94 | trySubmitNext(); |
79 | } else { | 95 | } else { |
80 | queued_envelopes.remove(ev); | 96 | queued_envelopes.remove(ev); |
81 | prefered_queued_envelopes.remove(ev); | 97 | prefered_queued_envelopes.remove(ev); |
diff --git a/src/main/java/org/gnunet/testbed/Controller.java b/src/main/java/org/gnunet/testbed/Controller.java index 7372986..863c6fe 100644 --- a/src/main/java/org/gnunet/testbed/Controller.java +++ b/src/main/java/org/gnunet/testbed/Controller.java | |||
@@ -37,7 +37,6 @@ public class Controller { | |||
37 | client = new Client("testbed", host.cfg); | 37 | client = new Client("testbed", host.cfg); |
38 | } | 38 | } |
39 | 39 | ||
40 | |||
41 | /** | 40 | /** |
42 | * Create the given peer at the specified host using the given | 41 | * Create the given peer at the specified host using the given |
43 | * controller. If the given controller is not running on the target | 42 | * controller. If the given controller is not running on the target |
@@ -70,7 +69,6 @@ public class Controller { | |||
70 | return null; | 69 | return null; |
71 | } | 70 | } |
72 | 71 | ||
73 | |||
74 | /** | 72 | /** |
75 | * Stop the given controller (also will terminate all peers and | 73 | * Stop the given controller (also will terminate all peers and |
76 | * controllers dependent on this controller). This function | 74 | * controllers dependent on this controller). This function |
diff --git a/src/main/java/org/gnunet/testbed/ControllerProc.java b/src/main/java/org/gnunet/testbed/ControllerProc.java index 9eaca2c..69961c7 100644 --- a/src/main/java/org/gnunet/testbed/ControllerProc.java +++ b/src/main/java/org/gnunet/testbed/ControllerProc.java | |||
@@ -13,20 +13,29 @@ import java.util.zip.Deflater; | |||
13 | * the testbed helper on a remote machine. | 13 | * the testbed helper on a remote machine. |
14 | */ | 14 | */ |
15 | public class ControllerProc { | 15 | public class ControllerProc { |
16 | final Helper helper; | 16 | private Helper helper; |
17 | 17 | ||
18 | 18 | ||
19 | public class ControllerProcReceiver extends RunaboutMessageReceiver { | 19 | public class ControllerProcReceiver extends RunaboutMessageReceiver { |
20 | public void visit(HelperReplyMessage m) { | 20 | public void visit(HelperReplyMessage m) { |
21 | System.out.println("got controller proc message"); | ||
21 | 22 | ||
22 | } | 23 | } |
24 | |||
23 | @Override | 25 | @Override |
24 | public void handleError() { | 26 | public void handleError() { |
25 | 27 | throw new AssertionError(); | |
26 | } | 28 | } |
27 | } | 29 | } |
28 | 30 | ||
29 | /** | 31 | /** |
32 | * Create a controller proc. Nothing will hapen until ControllerProc.start is called. | ||
33 | */ | ||
34 | public void ControllerProc() { | ||
35 | |||
36 | } | ||
37 | |||
38 | /** | ||
30 | * Starts a controller process at the given host. The given host's configration | 39 | * Starts a controller process at the given host. The given host's configration |
31 | * is used as a Template configuration to use for the remote controller; the | 40 | * is used as a Template configuration to use for the remote controller; the |
32 | * remote controller will be started with a slightly modified configuration | 41 | * remote controller will be started with a slightly modified configuration |
@@ -47,12 +56,22 @@ public class ControllerProc { | |||
47 | * (synchronous errors will be signalled by returning NULL). This | 56 | * (synchronous errors will be signalled by returning NULL). This |
48 | * parameter cannot be NULL. | 57 | * parameter cannot be NULL. |
49 | */ | 58 | */ |
50 | public ControllerProc(String trustedIP, Host host, ControllerStatusCallback cb) { | 59 | public void start(String trustedIP, Host host, ControllerStatusCallback cb) { |
51 | if (host.isLocal()) { | 60 | if (host.isLocal()) { |
52 | helper = new Helper(false, "gnunet-testbed-helper", null, new ControllerProcReceiver()); | 61 | helper = new Helper(false, "gnunet-helper-testbed", null, new ControllerProcReceiver()); |
53 | } else { | 62 | } else { |
54 | throw new AssertionError("not implemented yet"); | 63 | throw new AssertionError("not implemented yet"); |
55 | } | 64 | } |
65 | helper.send(makeHelperInitMessage(trustedIP, host)); | ||
66 | } | ||
67 | |||
68 | /** | ||
69 | * Stop the controller process (also will terminate all peers and controllers | ||
70 | * dependent on this controller). This function blocks until the testbed has | ||
71 | * been fully terminated (!). The controller status cb will not be called. | ||
72 | */ | ||
73 | public void stop() { | ||
74 | throw new AssertionError("not implemented"); | ||
56 | } | 75 | } |
57 | 76 | ||
58 | 77 | ||
diff --git a/src/main/java/org/gnunet/testing/TestingSubsystem.java b/src/main/java/org/gnunet/testing/TestingSubsystem.java index da7f2e4..f36412c 100644 --- a/src/main/java/org/gnunet/testing/TestingSubsystem.java +++ b/src/main/java/org/gnunet/testing/TestingSubsystem.java | |||
@@ -85,15 +85,8 @@ public class TestingSubsystem { | |||
85 | 85 | ||
86 | cfg = new Configuration(); | 86 | cfg = new Configuration(); |
87 | cfg.parse(cfgFileName); | 87 | cfg.parse(cfgFileName); |
88 | |||
89 | try { | ||
90 | if (p.getErrorStream().available() != 0) { | ||
91 | throw new TestingSetup.SetupException("error starting service"); | ||
92 | } | ||
93 | } catch (IOException e) { | ||
94 | throw new TestingSetup.SetupException(e); | ||
95 | } | ||
96 | } | 88 | } |
89 | |||
97 | public void destroy() { | 90 | public void destroy() { |
98 | try { | 91 | try { |
99 | writer.write("q\n"); | 92 | writer.write("q\n"); |
diff --git a/src/main/java/org/gnunet/transport/AddressIterateMessage.java b/src/main/java/org/gnunet/transport/AddressIterateMessage.java index 59ea6a8..4fd4405 100644 --- a/src/main/java/org/gnunet/transport/AddressIterateMessage.java +++ b/src/main/java/org/gnunet/transport/AddressIterateMessage.java | |||
@@ -5,6 +5,7 @@ import org.gnunet.construct.NestedMessage; | |||
5 | import org.gnunet.construct.UInt32; | 5 | import org.gnunet.construct.UInt32; |
6 | import org.gnunet.construct.UnionCase; | 6 | import org.gnunet.construct.UnionCase; |
7 | import org.gnunet.util.AbsoluteTime; | 7 | import org.gnunet.util.AbsoluteTime; |
8 | import org.gnunet.util.GnunetMessage; | ||
8 | import org.gnunet.util.PeerIdentity; | 9 | import org.gnunet.util.PeerIdentity; |
9 | 10 | ||
10 | 11 | ||
@@ -13,7 +14,7 @@ import org.gnunet.util.PeerIdentity; | |||
13 | * asking for binary addresses known for a peer. | 14 | * asking for binary addresses known for a peer. |
14 | */ | 15 | */ |
15 | @UnionCase(380) | 16 | @UnionCase(380) |
16 | public class AddressIterateMessage { | 17 | public class AddressIterateMessage implements GnunetMessage.Body { |
17 | /** | 18 | /** |
18 | * One shot call or continous replies? | 19 | * One shot call or continous replies? |
19 | */ | 20 | */ |
diff --git a/src/main/java/org/gnunet/transport/Blacklist.java b/src/main/java/org/gnunet/transport/Blacklist.java new file mode 100644 index 0000000..d17858d --- /dev/null +++ b/src/main/java/org/gnunet/transport/Blacklist.java | |||
@@ -0,0 +1,55 @@ | |||
1 | package org.gnunet.transport; | ||
2 | |||
3 | import org.gnunet.util.Client; | ||
4 | import org.gnunet.util.Configuration; | ||
5 | import org.gnunet.util.PeerIdentity; | ||
6 | import org.gnunet.util.RunaboutMessageReceiver; | ||
7 | |||
8 | /** | ||
9 | * Transport blacklist. | ||
10 | */ | ||
11 | public abstract class Blacklist { | ||
12 | /** | ||
13 | * Client connecting to the transport service. | ||
14 | */ | ||
15 | private Client client; | ||
16 | |||
17 | private final class TransportReceiver extends RunaboutMessageReceiver { | ||
18 | void visit(BlacklistQueryMessage m) { | ||
19 | boolean allowed = isAllowed(m.peer); | ||
20 | BlacklistReplyMessage mr = new BlacklistReplyMessage(); | ||
21 | mr.is_allowed = allowed; | ||
22 | mr.peer = m.peer; | ||
23 | client.send(mr); | ||
24 | } | ||
25 | |||
26 | @Override | ||
27 | public void handleError() { | ||
28 | client.reconnect(); | ||
29 | client.send(new BlacklistInitMessage()); | ||
30 | } | ||
31 | } | ||
32 | |||
33 | /** | ||
34 | * Install a blacklist callback. The service will be queried for all | ||
35 | * existing connections as well as any fresh connections to check if | ||
36 | * they are permitted. | ||
37 | * The blacklist is active until the Transport handle is destroyed. | ||
38 | * When the transport handle that installed the blacklist is destroyed, | ||
39 | * all hosts that were denied in the past will automatically be | ||
40 | * whitelisted again. This is the only way to re-enable | ||
41 | * connections from peers that were previously blacklisted. | ||
42 | */ | ||
43 | public Blacklist(Configuration cfg) { | ||
44 | client = new Client("transport", cfg); | ||
45 | client.send(new BlacklistInitMessage()); | ||
46 | client.installReceiver(new TransportReceiver()); | ||
47 | } | ||
48 | |||
49 | public void destroy() { | ||
50 | client.disconnect(); | ||
51 | client = null; | ||
52 | } | ||
53 | |||
54 | protected abstract boolean isAllowed(PeerIdentity peer); | ||
55 | } | ||
diff --git a/src/main/java/org/gnunet/transport/BlacklistCallback.java b/src/main/java/org/gnunet/transport/BlacklistCallback.java deleted file mode 100644 index b763dec..0000000 --- a/src/main/java/org/gnunet/transport/BlacklistCallback.java +++ /dev/null | |||
@@ -1,12 +0,0 @@ | |||
1 | package org.gnunet.transport; | ||
2 | |||
3 | import org.gnunet.util.PeerIdentity; | ||
4 | |||
5 | /** | ||
6 | * ... | ||
7 | * | ||
8 | * @author Florian Dold | ||
9 | */ | ||
10 | public interface BlacklistCallback { | ||
11 | boolean isAllowed(PeerIdentity peerIdentity); | ||
12 | } | ||
diff --git a/src/main/java/org/gnunet/transport/BlacklistQueryMessage.java b/src/main/java/org/gnunet/transport/BlacklistQueryMessage.java new file mode 100644 index 0000000..4947dd4 --- /dev/null +++ b/src/main/java/org/gnunet/transport/BlacklistQueryMessage.java | |||
@@ -0,0 +1,16 @@ | |||
1 | package org.gnunet.transport; | ||
2 | |||
3 | import org.gnunet.construct.NestedMessage; | ||
4 | import org.gnunet.construct.UInt32; | ||
5 | import org.gnunet.construct.UnionCase; | ||
6 | import org.gnunet.util.GnunetMessage; | ||
7 | import org.gnunet.util.PeerIdentity; | ||
8 | |||
9 | @UnionCase(370) | ||
10 | public class BlacklistQueryMessage implements GnunetMessage.Body { | ||
11 | @UInt32 | ||
12 | public byte reserved; | ||
13 | |||
14 | @NestedMessage | ||
15 | public PeerIdentity peer; | ||
16 | } | ||
diff --git a/src/main/java/org/gnunet/transport/BlacklistReplyMessage.java b/src/main/java/org/gnunet/transport/BlacklistReplyMessage.java new file mode 100644 index 0000000..9319c97 --- /dev/null +++ b/src/main/java/org/gnunet/transport/BlacklistReplyMessage.java | |||
@@ -0,0 +1,16 @@ | |||
1 | package org.gnunet.transport; | ||
2 | |||
3 | import org.gnunet.construct.NestedMessage; | ||
4 | import org.gnunet.construct.UInt32; | ||
5 | import org.gnunet.construct.UnionCase; | ||
6 | import org.gnunet.util.GnunetMessage; | ||
7 | import org.gnunet.util.PeerIdentity; | ||
8 | |||
9 | @UnionCase(371) | ||
10 | public class BlacklistReplyMessage implements GnunetMessage.Body { | ||
11 | @UInt32 | ||
12 | public boolean is_allowed; | ||
13 | |||
14 | @NestedMessage | ||
15 | public PeerIdentity peer; | ||
16 | } | ||
diff --git a/src/main/java/org/gnunet/transport/Transport.java b/src/main/java/org/gnunet/transport/Transport.java index b9881b6..f05cee5 100644 --- a/src/main/java/org/gnunet/transport/Transport.java +++ b/src/main/java/org/gnunet/transport/Transport.java | |||
@@ -19,12 +19,6 @@ public class Transport { | |||
19 | 19 | ||
20 | boolean init_requested; | 20 | boolean init_requested; |
21 | 21 | ||
22 | /** | ||
23 | * Blacklist callback, null if there is no active blacklist | ||
24 | * for this handle. | ||
25 | */ | ||
26 | BlacklistCallback blacklistCallback; | ||
27 | |||
28 | private final class TransportReceiver extends RunaboutMessageReceiver { | 22 | private final class TransportReceiver extends RunaboutMessageReceiver { |
29 | @Override | 23 | @Override |
30 | public void handleError() { | 24 | public void handleError() { |
@@ -113,27 +107,6 @@ public class Transport { | |||
113 | } | 107 | } |
114 | 108 | ||
115 | /** | 109 | /** |
116 | * Install a blacklist callback. The service will be queried for all | ||
117 | * existing connections as well as any fresh connections to check if | ||
118 | * they are permitted. | ||
119 | * The blacklist is active until the Transport handle is destroyed. | ||
120 | * When the transport handle that installed the blacklist is destroyed, | ||
121 | * all hosts that were denied in the past will automatically be | ||
122 | * whitelisted again. This is the only way to re-enable | ||
123 | * connections from peers that were previously blacklisted. | ||
124 | * | ||
125 | * @param blacklistCallback callback to invoke to check if connections are allowed | ||
126 | */ | ||
127 | public void blacklist(BlacklistCallback blacklistCallback) { | ||
128 | if (this.blacklistCallback != null) | ||
129 | throw new AssertionError("there is already a blacklist"); | ||
130 | if (blacklistCallback == null) | ||
131 | throw new AssertionError("blacklist callback may not be null"); | ||
132 | this.blacklistCallback = blacklistCallback; | ||
133 | client.send(new BlacklistInitMessage()); | ||
134 | } | ||
135 | |||
136 | /** | ||
137 | * Return all the known addresses for a specific peer or all peers. | 110 | * Return all the known addresses for a specific peer or all peers. |
138 | * Returns continuously all address if one_shot is set to false | 111 | * Returns continuously all address if one_shot is set to false |
139 | * <p/> | 112 | * <p/> |
diff --git a/src/main/java/org/gnunet/util/Client.java b/src/main/java/org/gnunet/util/Client.java index 18c206b..899fac8 100644 --- a/src/main/java/org/gnunet/util/Client.java +++ b/src/main/java/org/gnunet/util/Client.java | |||
@@ -121,6 +121,8 @@ public class Client extends MessageQueue { | |||
121 | throw new Configuration.ConfigurationException(String.format("hostname of service '%s' empty", serviceName)); | 121 | throw new Configuration.ConfigurationException(String.format("hostname of service '%s' empty", serviceName)); |
122 | } | 122 | } |
123 | reconnect(); | 123 | reconnect(); |
124 | // we don't have to wait for any acks, but can send right away! | ||
125 | reportReadyForSubmit(); | ||
124 | } | 126 | } |
125 | 127 | ||
126 | /** | 128 | /** |
@@ -133,6 +135,8 @@ public class Client extends MessageQueue { | |||
133 | this.hostname = hostname; | 135 | this.hostname = hostname; |
134 | this.port = port; | 136 | this.port = port; |
135 | reconnect(); | 137 | reconnect(); |
138 | // we don't have to wait for any acks, but can send right away! | ||
139 | reportReadyForSubmit(); | ||
136 | } | 140 | } |
137 | 141 | ||
138 | 142 | ||
@@ -286,6 +290,7 @@ public class Client extends MessageQueue { | |||
286 | public void cont(boolean success) { | 290 | public void cont(boolean success) { |
287 | currentSubmit = null; | 291 | currentSubmit = null; |
288 | reportMessageSent(); | 292 | reportMessageSent(); |
293 | reportReadyForSubmit(); | ||
289 | } | 294 | } |
290 | }); | 295 | }); |
291 | } | 296 | } |
diff --git a/src/main/java/org/gnunet/util/Connection.java b/src/main/java/org/gnunet/util/Connection.java index 1fd1458..1c569c4 100644 --- a/src/main/java/org/gnunet/util/Connection.java +++ b/src/main/java/org/gnunet/util/Connection.java | |||
@@ -214,7 +214,7 @@ public class Connection { | |||
214 | int n = connectionChannel.read(recvBuffer); | 214 | int n = connectionChannel.read(recvBuffer); |
215 | if (n == -1) { | 215 | if (n == -1) { |
216 | currentReceiveHelper = null; | 216 | currentReceiveHelper = null; |
217 | logger.warn("lost connection to service"); | 217 | logger.warn("lost connection to service, {}", connectionChannel.socket().toString()); |
218 | connectionChannel.close(); | 218 | connectionChannel.close(); |
219 | connectionChannel = null; | 219 | connectionChannel = null; |
220 | if (Connection.this.currentTransmitHelper != null) { | 220 | if (Connection.this.currentTransmitHelper != null) { |
@@ -384,7 +384,8 @@ public class Connection { | |||
384 | String.format("tried to send message with binary size %s but size in header %s", | 384 | String.format("tried to send message with binary size %s but size in header %s", |
385 | b.length, gm.header.messageSize)); | 385 | b.length, gm.header.messageSize)); |
386 | } | 386 | } |
387 | logger.debug("sending message (size={},type={})", b.length, gm.header.messageType); | 387 | logger.debug("sending message (size={},type={}) over {}", |
388 | new String[] {String.valueOf(b.length), String.valueOf(gm.header.messageType), connectionChannel.socket().toString()}); | ||
388 | if (transmitBuffer.remaining() < b.length) { | 389 | if (transmitBuffer.remaining() < b.length) { |
389 | ByteBuffer buf = ByteBuffer.allocate(b.length + transmitBuffer.capacity()); | 390 | ByteBuffer buf = ByteBuffer.allocate(b.length + transmitBuffer.capacity()); |
390 | transmitBuffer.flip(); | 391 | transmitBuffer.flip(); |
diff --git a/src/main/java/org/gnunet/util/CryptoECC.java b/src/main/java/org/gnunet/util/CryptoECC.java new file mode 100644 index 0000000..a3ef01e --- /dev/null +++ b/src/main/java/org/gnunet/util/CryptoECC.java | |||
@@ -0,0 +1,360 @@ | |||
1 | package org.gnunet.util; | ||
2 | |||
3 | import org.gnunet.construct.FixedSizeIntegerArray; | ||
4 | import org.gnunet.construct.Message; | ||
5 | import org.gnunet.construct.ProtocolViolationException; | ||
6 | import org.gnunet.construct.UInt32; | ||
7 | |||
8 | import java.math.BigInteger; | ||
9 | import java.nio.ByteBuffer; | ||
10 | import java.security.MessageDigest; | ||
11 | import java.security.NoSuchAlgorithmException; | ||
12 | |||
13 | /** | ||
14 | * Implementation of the Ed25519 public-key signature system. | ||
15 | * Original version written and placed into the public domain by k3d3 (https://github.com/k3d3/ed25519-java). | ||
16 | * See also http://ed25519.cr.yp.to/. | ||
17 | */ | ||
18 | public class CryptoECC { | ||
19 | |||
20 | /** | ||
21 | * Private ECC key. | ||
22 | */ | ||
23 | public static final class PrivateKey implements Message { | ||
24 | /** | ||
25 | * Value of the private key, represents a number modulo q. | ||
26 | * The number is stored as little endian. | ||
27 | */ | ||
28 | @FixedSizeIntegerArray(bitSize = 8, signed = false, length = 32) | ||
29 | public byte[] d; | ||
30 | } | ||
31 | |||
32 | /** | ||
33 | * Public ECC key. | ||
34 | */ | ||
35 | public static final class PublicKey implements Message { | ||
36 | /** | ||
37 | * x-coordinate of the point on the curve. | ||
38 | * The number is stored as little endian. | ||
39 | */ | ||
40 | @FixedSizeIntegerArray(bitSize = 8, signed = false, length = 32) | ||
41 | public byte[] x; | ||
42 | |||
43 | /** | ||
44 | * y-coordinate of the point on the curve. | ||
45 | * The number is stored as little endian. | ||
46 | */ | ||
47 | @FixedSizeIntegerArray(bitSize = 8, signed = false, length = 32) | ||
48 | public byte[] y; | ||
49 | |||
50 | } | ||
51 | |||
52 | /** | ||
53 | * ECC Signature. | ||
54 | */ | ||
55 | public static final class Signature implements Message { | ||
56 | /** | ||
57 | * R-value of the signature. | ||
58 | * The number is stored as little endian. | ||
59 | */ | ||
60 | @FixedSizeIntegerArray(bitSize = 8, signed = false, length = 32) | ||
61 | public byte[] r; | ||
62 | |||
63 | /** | ||
64 | * S-value of the signature. | ||
65 | * The number is stored as little endian. | ||
66 | */ | ||
67 | @FixedSizeIntegerArray(bitSize = 8, signed = false, length = 32) | ||
68 | public byte[] s; | ||
69 | } | ||
70 | |||
71 | // curve parameter b | ||
72 | private static final int b = 256; | ||
73 | // curve parameter q | ||
74 | private static final BigInteger q = new BigInteger("57896044618658097711785492504343953926634992332820282019728792003956564819949"); | ||
75 | // q-3 | ||
76 | private static final BigInteger qm2 = new BigInteger("57896044618658097711785492504343953926634992332820282019728792003956564819947"); | ||
77 | // q-3 | ||
78 | private static final BigInteger qp3 = new BigInteger("57896044618658097711785492504343953926634992332820282019728792003956564819952"); | ||
79 | private static final BigInteger l = new BigInteger("7237005577332262213973186563042994240857116359379907606001950938285454250989"); | ||
80 | private static final BigInteger d = new BigInteger("-4513249062541557337682894930092624173785641285191125241628941591882900924598840740"); | ||
81 | private static final BigInteger I = new BigInteger("19681161376707505956807079304988542015446066515923890162744021073123829784752"); | ||
82 | private static final BigInteger By = new BigInteger("46316835694926478169428394003475163141307993866256225615783033603165251855960"); | ||
83 | private static final BigInteger Bx = new BigInteger("15112221349535400772501151409588531511454012693041857206046113283949847762202"); | ||
84 | private static final BigInteger[] B = {Bx.mod(q),By.mod(q)}; | ||
85 | // 2^255 | ||
86 | private static final BigInteger un = new BigInteger("57896044618658097711785492504343953926634992332820282019728792003956564819967"); | ||
87 | |||
88 | static final private MessageDigest sha512; | ||
89 | static { | ||
90 | try { | ||
91 | sha512 = MessageDigest.getInstance("SHA-512"); | ||
92 | } catch (NoSuchAlgorithmException e) { | ||
93 | throw new RuntimeException("SHA-512 not available"); | ||
94 | } | ||
95 | } | ||
96 | |||
97 | /** | ||
98 | * Computes the multiplicative inverse of x modulo q using Euler's theorem. | ||
99 | * | ||
100 | * @param x the group element to invert | ||
101 | * @return the inverse of x modulo q | ||
102 | */ | ||
103 | private static BigInteger inv(BigInteger x) { | ||
104 | return x.modPow(qm2, q); | ||
105 | } | ||
106 | |||
107 | /** | ||
108 | * Compute the x-component of a point on our curve from | ||
109 | * the y-coordinate. | ||
110 | * | ||
111 | * @param y the y-coordinate of a point | ||
112 | * @return the x-coordinate of the point (x,y) on the curve | ||
113 | */ | ||
114 | private static BigInteger xrecover(BigInteger y) { | ||
115 | BigInteger y2 = y.multiply(y); | ||
116 | BigInteger xx = (y2.subtract(BigInteger.ONE)).multiply(inv(d.multiply(y2).add(BigInteger.ONE))); | ||
117 | BigInteger x = xx.modPow(qp3.divide(BigInteger.valueOf(8)), q); | ||
118 | if (!x.multiply(x).subtract(xx).mod(q).equals(BigInteger.ZERO)) x = (x.multiply(I).mod(q)); | ||
119 | if (!x.mod(BigInteger.valueOf(2)).equals(BigInteger.ZERO)) x = q.subtract(x); | ||
120 | return x; | ||
121 | } | ||
122 | |||
123 | /** | ||
124 | * Implements the group operation (twisted Edwards addition) on our curve. | ||
125 | * | ||
126 | * @param P a point on the curve | ||
127 | * @param Q another point on the curve | ||
128 | * @return P+Q | ||
129 | */ | ||
130 | private static BigInteger[] edwards(BigInteger[] P, BigInteger[] Q) { | ||
131 | BigInteger x1 = P[0]; | ||
132 | BigInteger y1 = P[1]; | ||
133 | BigInteger x2 = Q[0]; | ||
134 | BigInteger y2 = Q[1]; | ||
135 | BigInteger dtemp = d.multiply(x1).multiply(x2).multiply(y1).multiply(y2); | ||
136 | BigInteger x3 = ((x1.multiply(y2)).add((x2.multiply(y1)))).multiply(inv(BigInteger.ONE.add(dtemp))); | ||
137 | BigInteger y3 = ((y1.multiply(y2)).add((x1.multiply(x2)))).multiply(inv(BigInteger.ONE.subtract(dtemp))); | ||
138 | return new BigInteger[]{x3.mod(q), y3.mod(q)}; | ||
139 | } | ||
140 | |||
141 | /** | ||
142 | * Multiply a point on the curve with a constant. | ||
143 | * | ||
144 | * @param P point on the curve | ||
145 | * @param e constant | ||
146 | * @return eP | ||
147 | */ | ||
148 | private static BigInteger[] scalarmult(BigInteger[] P, BigInteger e) { | ||
149 | if (e.equals(BigInteger.ZERO)) { | ||
150 | return new BigInteger[]{BigInteger.ZERO, BigInteger.ONE}; | ||
151 | } | ||
152 | BigInteger[] Q = scalarmult(P, e.shiftRight(1)); | ||
153 | Q = edwards(Q, Q); | ||
154 | if (e.testBit(0)) Q = edwards(Q, P); | ||
155 | return Q; | ||
156 | } | ||
157 | |||
158 | /** | ||
159 | * Encode an integer to binary format. | ||
160 | * | ||
161 | * @param y integer to encode | ||
162 | * @return encoded integer as byte array | ||
163 | */ | ||
164 | private static byte[] encodeint(BigInteger y) { | ||
165 | byte[] in = y.toByteArray(); | ||
166 | // reverse the array | ||
167 | for (int i = 0; i < in.length / 2; i++) { | ||
168 | byte tmp = in[i]; | ||
169 | in[i] = in[in.length - i - 1]; | ||
170 | in[in.length - i - 1] = tmp; | ||
171 | } | ||
172 | return in; | ||
173 | } | ||
174 | |||
175 | /** | ||
176 | * Encode a point to binary format. | ||
177 | * | ||
178 | * @param P point to encode | ||
179 | * @return encoded point as byte array | ||
180 | */ | ||
181 | private static byte[] encodepoint(BigInteger[] P) { | ||
182 | BigInteger x = P[0]; | ||
183 | BigInteger y = P[1]; | ||
184 | byte[] out = encodeint(y); | ||
185 | out[out.length-1] |= (x.testBit(0) ? 0x80 : 0); | ||
186 | return out; | ||
187 | } | ||
188 | |||
189 | /** | ||
190 | * Get return the i-th bit in the given array of bytes h. | ||
191 | * | ||
192 | * @param h array of bytes | ||
193 | * @param i bit index | ||
194 | * @return i-th bit in h | ||
195 | */ | ||
196 | private static int bit(byte[] h, int i) { | ||
197 | return h[i/8] >> (i%8) & 1; | ||
198 | } | ||
199 | |||
200 | static private BigInteger computePublicKeyCoefficient(PrivateKey sk) { | ||
201 | byte[] h = sha512.digest(sk.d); | ||
202 | BigInteger a = BigInteger.valueOf(2).pow(b-2); | ||
203 | for (int i=3; i < (b - 2); i++) { | ||
204 | BigInteger apart = BigInteger.valueOf(2).pow(i).multiply(BigInteger.valueOf(bit(h,i))); | ||
205 | a = a.add(apart); | ||
206 | } | ||
207 | return a; | ||
208 | } | ||
209 | |||
210 | /** | ||
211 | * Derive the public key from the private key 'sk', | ||
212 | * | ||
213 | * @param sk private key | ||
214 | * @return public key derived from 'sk' | ||
215 | */ | ||
216 | static public PublicKey computePublicKey(PrivateKey sk) { | ||
217 | BigInteger a = computePublicKeyCoefficient(sk); | ||
218 | BigInteger[] A = scalarmult(B, a); | ||
219 | PublicKey publicKey = new PublicKey(); | ||
220 | publicKey.x = encodeint(A[0]); | ||
221 | publicKey.y = encodeint(A[1]); | ||
222 | return publicKey; | ||
223 | } | ||
224 | |||
225 | /** | ||
226 | * Hash the data in m and return 2^h(m) | ||
227 | * | ||
228 | * @param m data to hash | ||
229 | * @return 2^h(m) | ||
230 | */ | ||
231 | static private BigInteger Hint(byte[] m) { | ||
232 | final byte[] h = sha512.digest(m); | ||
233 | for (int i = 0; i < 32; i++) { | ||
234 | byte tmp = h[i]; | ||
235 | h[i] = h[63 - i]; | ||
236 | h[63 - i] = tmp; | ||
237 | } | ||
238 | return new BigInteger(1, h); | ||
239 | } | ||
240 | |||
241 | /** | ||
242 | * Sign a message. | ||
243 | * | ||
244 | * @param m the message to sign | ||
245 | * @param sk the private (secret) key | ||
246 | * @param pk the public key, derived from 'sk', but passed as a | ||
247 | * parameter for performance reasons | ||
248 | * @return a signature on m | ||
249 | */ | ||
250 | public static Signature sign(byte[] m, PrivateKey sk, PublicKey pk) { | ||
251 | byte[] compressed_pk = encodepoint(new BigInteger[]{decodeint(pk.x), decodeint(pk.y)}); | ||
252 | byte[] h = sha512.digest(sk.d); | ||
253 | BigInteger a = BigInteger.valueOf(2).pow(b-2); | ||
254 | for (int i = 3; i < (b - 2); i++) { | ||
255 | a = a.add(BigInteger.valueOf(2).pow(i).multiply(BigInteger.valueOf(bit(h,i)))); | ||
256 | } | ||
257 | ByteBuffer rsub = ByteBuffer.allocate((b/8)+m.length); | ||
258 | rsub.put(h, b/8, b/4-b/8).put(m); | ||
259 | BigInteger r = Hint(rsub.array()); | ||
260 | BigInteger[] R = scalarmult(B,r); | ||
261 | |||
262 | Signature sig = new Signature(); | ||
263 | sig.r = encodepoint(R); | ||
264 | |||
265 | ByteBuffer buf = ByteBuffer.allocate(32 + compressed_pk.length + m.length); | ||
266 | buf.put(encodepoint(R)).put(compressed_pk).put(m); | ||
267 | |||
268 | BigInteger S = r.add(Hint(buf.array()).multiply(a)).mod(l); | ||
269 | sig.s = encodeint(S); | ||
270 | |||
271 | return sig; | ||
272 | } | ||
273 | |||
274 | /** | ||
275 | * Check if a point is on the curve. | ||
276 | * | ||
277 | * @param P point to check | ||
278 | * @return whether the point P is on the curve | ||
279 | */ | ||
280 | private static boolean isoncurve(BigInteger[] P) { | ||
281 | BigInteger x = P[0]; | ||
282 | BigInteger y = P[1]; | ||
283 | BigInteger xx = x.multiply(x); | ||
284 | BigInteger yy = y.multiply(y); | ||
285 | BigInteger dxxyy = d.multiply(yy).multiply(xx); | ||
286 | return xx.negate().add(yy).subtract(BigInteger.ONE).subtract(dxxyy).mod(q).equals(BigInteger.ZERO); | ||
287 | } | ||
288 | |||
289 | /** | ||
290 | * Decode an integer from its binary form. | ||
291 | * | ||
292 | * @param s the binary form if the integer | ||
293 | * @return the decoded integer | ||
294 | */ | ||
295 | private static BigInteger decodeint(byte[] s) { | ||
296 | byte[] out = new byte[s.length]; | ||
297 | for (int i=0;i<s.length;i++) { | ||
298 | out[i] = s[s.length-1-i]; | ||
299 | } | ||
300 | return new BigInteger(out).and(un); | ||
301 | } | ||
302 | |||
303 | /** | ||
304 | * Decode a curve point from its compressed form. | ||
305 | * | ||
306 | * @param s the compressed point data | ||
307 | * @return the uncompressed point, null if not a valid point | ||
308 | */ | ||
309 | private static BigInteger[] decodepoint(byte[] s) { | ||
310 | byte[] ybyte = new byte[s.length]; | ||
311 | for (int i=0;i<s.length;i++) { | ||
312 | ybyte[i] = s[s.length-1-i]; | ||
313 | } | ||
314 | BigInteger y = new BigInteger(ybyte).and(un); | ||
315 | BigInteger x = xrecover(y); | ||
316 | if ((x.testBit(0)?1:0) != bit(s, b-1)) { | ||
317 | x = q.subtract(x); | ||
318 | } | ||
319 | BigInteger[] P = {x,y}; | ||
320 | if (!isoncurve(P)) | ||
321 | return null; | ||
322 | return P; | ||
323 | } | ||
324 | |||
325 | /** | ||
326 | * Verify the validity of a signature on a message. | ||
327 | * | ||
328 | * @param sig signature | ||
329 | * @param m message | ||
330 | * @param pk public key of the signature creator | ||
331 | * @return whether the signature is valid | ||
332 | */ | ||
333 | public static boolean verify(Signature sig, byte[] m, PublicKey pk) { | ||
334 | BigInteger[] R = decodepoint(sig.r); | ||
335 | BigInteger[] A = new BigInteger[]{decodeint(pk.x), decodeint(pk.y)}; | ||
336 | BigInteger S = decodeint(sig.s); | ||
337 | ByteBuffer Stemp = ByteBuffer.allocate(32 + 32 + m.length); | ||
338 | Stemp.put(encodepoint(R)).put(encodepoint(A)).put(m); | ||
339 | BigInteger h = Hint(Stemp.array()); | ||
340 | BigInteger[] ra = scalarmult(B,S); | ||
341 | BigInteger[] rb = edwards(R,scalarmult(A,h)); | ||
342 | return ra[0].equals(rb[0]) && ra[1].equals(rb[1]); | ||
343 | } | ||
344 | |||
345 | /** | ||
346 | * Derive key material from a public and a private ECC key. | ||
347 | * | ||
348 | * @param privateKey private key to use for the ECDH (x) | ||
349 | * @param publicKey public key to use for the ECDH (yG) | ||
350 | * @return key material (xyG) | ||
351 | */ | ||
352 | public static HashCode ecdh(PrivateKey privateKey, PublicKey publicKey) { | ||
353 | BigInteger[] publicPoint = new BigInteger[]{decodeint(publicKey.x), decodeint(publicKey.y)}; | ||
354 | BigInteger coeff = computePublicKeyCoefficient(privateKey); | ||
355 | BigInteger[] R = scalarmult(publicPoint, coeff); | ||
356 | // FIXME: this is *not* equivalent to the GNUnet C implementation, which hashes an s-expr | ||
357 | sha512.update(R[0].toByteArray()); | ||
358 | return new HashCode(sha512.digest(R[1].toByteArray())); | ||
359 | } | ||
360 | } | ||
diff --git a/src/main/java/org/gnunet/util/Helper.java b/src/main/java/org/gnunet/util/Helper.java index e8c1723..46451b0 100644 --- a/src/main/java/org/gnunet/util/Helper.java +++ b/src/main/java/org/gnunet/util/Helper.java | |||
@@ -6,7 +6,10 @@ import org.gnunet.construct.MessageLoader; | |||
6 | import org.gnunet.construct.ProtocolViolationException; | 6 | import org.gnunet.construct.ProtocolViolationException; |
7 | import org.gnunet.mq.Envelope; | 7 | import org.gnunet.mq.Envelope; |
8 | import org.gnunet.mq.MessageQueue; | 8 | import org.gnunet.mq.MessageQueue; |
9 | import org.slf4j.Logger; | ||
10 | import org.slf4j.LoggerFactory; | ||
9 | 11 | ||
12 | import java.io.File; | ||
10 | import java.io.IOError; | 13 | import java.io.IOError; |
11 | import java.io.IOException; | 14 | import java.io.IOException; |
12 | import java.nio.Buffer; | 15 | import java.nio.Buffer; |
@@ -18,17 +21,20 @@ import java.util.LinkedList; | |||
18 | import java.util.List; | 21 | import java.util.List; |
19 | 22 | ||
20 | /** | 23 | /** |
21 | * Process that we can communicate with standard GNUnet messages over stdin/stdout. | 24 | * Process that we can communicate to with standard GNUnet messages over stdin/stdout. |
22 | */ | 25 | */ |
23 | public class Helper extends MessageQueue { | 26 | public class Helper extends MessageQueue { |
27 | private static final Logger logger = LoggerFactory | ||
28 | .getLogger(Helper.class); | ||
24 | 29 | ||
25 | private final ProcessBuilder processBuilder; | 30 | private final ProcessBuilder processBuilder; |
26 | private final RunaboutMessageReceiver receiver; | 31 | private final RunaboutMessageReceiver receiver; |
27 | private Process process; | 32 | private Process process; |
28 | |||
29 | private volatile GnunetMessage.Body writeMessage; | 33 | private volatile GnunetMessage.Body writeMessage; |
34 | private WriteThread writeThread; | ||
35 | private ReadThread readThread; | ||
30 | 36 | ||
31 | private final class WriteThread implements Runnable { | 37 | private final class WriteThread extends Thread { |
32 | @Override | 38 | @Override |
33 | public void run() { | 39 | public void run() { |
34 | GnunetMessage.Body msg; | 40 | GnunetMessage.Body msg; |
@@ -62,7 +68,7 @@ public class Helper extends MessageQueue { | |||
62 | } | 68 | } |
63 | } | 69 | } |
64 | 70 | ||
65 | private final class ReadThread implements Runnable { | 71 | private final class ReadThread extends Thread { |
66 | private ByteBuffer buffer; | 72 | private ByteBuffer buffer; |
67 | ReadableByteChannel channel; | 73 | ReadableByteChannel channel; |
68 | 74 | ||
@@ -133,6 +139,15 @@ public class Helper extends MessageQueue { | |||
133 | } | 139 | } |
134 | } | 140 | } |
135 | 141 | ||
142 | private String getBinaryPath(String binaryName) { | ||
143 | if (!binaryName.contains("gnunet")) | ||
144 | return binaryName; | ||
145 | String prefix = System.getenv("GNUNET_PREFIX"); | ||
146 | if (prefix == null || prefix.isEmpty()) | ||
147 | return binaryName; | ||
148 | // FIXME: this is not portable at all | ||
149 | return prefix + "/lib/" + "gnunet/" + "libexec/" + binaryName; | ||
150 | } | ||
136 | 151 | ||
137 | public Helper(boolean withControlPipe, String binaryName, List<String> argv, | 152 | public Helper(boolean withControlPipe, String binaryName, List<String> argv, |
138 | RunaboutMessageReceiver receiver) { | 153 | RunaboutMessageReceiver receiver) { |
@@ -141,7 +156,7 @@ public class Helper extends MessageQueue { | |||
141 | if (binaryName == null) { | 156 | if (binaryName == null) { |
142 | throw new AssertionError(); | 157 | throw new AssertionError(); |
143 | } | 158 | } |
144 | command.add(binaryName); | 159 | command.add(getBinaryPath(binaryName)); |
145 | if (argv != null) | 160 | if (argv != null) |
146 | command.addAll(argv); | 161 | command.addAll(argv); |
147 | processBuilder = new ProcessBuilder(command); | 162 | processBuilder = new ProcessBuilder(command); |
@@ -150,6 +165,14 @@ public class Helper extends MessageQueue { | |||
150 | } catch (IOException e) { | 165 | } catch (IOException e) { |
151 | throw new IOError(e); | 166 | throw new IOError(e); |
152 | } | 167 | } |
168 | |||
169 | writeThread = new WriteThread(); | ||
170 | readThread = new ReadThread(); | ||
171 | |||
172 | writeThread.start(); | ||
173 | readThread.start(); | ||
174 | |||
175 | reportReadyForSubmit(); | ||
153 | } | 176 | } |
154 | 177 | ||
155 | /** | 178 | /** |
@@ -191,12 +214,14 @@ public class Helper extends MessageQueue { | |||
191 | 214 | ||
192 | @Override | 215 | @Override |
193 | protected void submit(Envelope ev) { | 216 | protected void submit(Envelope ev) { |
217 | logger.debug("submitting envelope to helper thread"); | ||
194 | synchronized (this) { | 218 | synchronized (this) { |
195 | if (writeMessage != null) | 219 | if (writeMessage != null) |
196 | throw new AssertionError("message queue not implemented correctly"); | 220 | throw new AssertionError("message queue not implemented correctly"); |
197 | writeMessage = ev.message; | 221 | writeMessage = ev.message; |
198 | notifyAll(); | 222 | notifyAll(); |
199 | } | 223 | } |
224 | reportReadyForSubmit(); | ||
200 | } | 225 | } |
201 | 226 | ||
202 | @Override | 227 | @Override |
diff --git a/src/main/java/org/gnunet/util/Scheduler.java b/src/main/java/org/gnunet/util/Scheduler.java index 78ea288..ba7098f 100644 --- a/src/main/java/org/gnunet/util/Scheduler.java +++ b/src/main/java/org/gnunet/util/Scheduler.java | |||
@@ -44,6 +44,16 @@ public class Scheduler { | |||
44 | // cumulative number of tasks in the ready lists | 44 | // cumulative number of tasks in the ready lists |
45 | private static volatile int readyCount = 0; | 45 | private static volatile int readyCount = 0; |
46 | 46 | ||
47 | /** | ||
48 | * Priority for Tasks. | ||
49 | */ | ||
50 | public enum Priority { | ||
51 | IDLE, BACKGROUND, DEFAULT, HIGH, UI, URGENT, SHUTDOWN; | ||
52 | |||
53 | // how many different priorities do we have? | ||
54 | private static final int numberOfPriorities = Priority.values().length; | ||
55 | } | ||
56 | |||
47 | // for every priority, there is a list of tasks that is definitely ready to run | 57 | // for every priority, there is a list of tasks that is definitely ready to run |
48 | @SuppressWarnings("unchecked") | 58 | @SuppressWarnings("unchecked") |
49 | final private static LinkedList<TaskConfiguration>[] readyLists = new LinkedList[Priority.numberOfPriorities]; | 59 | final private static LinkedList<TaskConfiguration>[] readyLists = new LinkedList[Priority.numberOfPriorities]; |
@@ -108,16 +118,6 @@ public class Scheduler { | |||
108 | 118 | ||
109 | 119 | ||
110 | /** | 120 | /** |
111 | * Priority for Tasks. | ||
112 | */ | ||
113 | public enum Priority { | ||
114 | IDLE, BACKGROUND, DEFAULT, HIGH, UI, URGENT, SHUTDOWN; | ||
115 | |||
116 | // how many different priorities do we have? | ||
117 | private static final int numberOfPriorities = Priority.values().length; | ||
118 | } | ||
119 | |||
120 | /** | ||
121 | * Reasons for executing a task. | 121 | * Reasons for executing a task. |
122 | */ | 122 | */ |
123 | public enum Reason { | 123 | public enum Reason { |
diff --git a/src/main/resources/org/gnunet/construct/MsgMap.txt b/src/main/resources/org/gnunet/construct/MsgMap.txt index 2ca1246..ad2a204 100644 --- a/src/main/resources/org/gnunet/construct/MsgMap.txt +++ b/src/main/resources/org/gnunet/construct/MsgMap.txt | |||
@@ -1,46 +1,53 @@ | |||
1 | org.gnunet.util.Resolver$Address|0=org.gnunet.util.Resolver$TextualAddress | 1 | org.gnunet.util.Resolver$Address|0=org.gnunet.util.Resolver$TextualAddress |
2 | org.gnunet.util.Resolver$Address|1=org.gnunet.util.Resolver$NumericAddress | 2 | org.gnunet.util.Resolver$Address|1=org.gnunet.util.Resolver$NumericAddress |
3 | org.gnunet.util.GnunetMessage$Body|68=org.gnunet.core.DisconnectNotifyMessage | ||
4 | org.gnunet.util.GnunetMessage$Body|274=org.gnunet.mesh.TunnelDestroyMessage | ||
5 | org.gnunet.util.GnunetMessage$Body|1=org.gnunet.util.TestMessage | 3 | org.gnunet.util.GnunetMessage$Body|1=org.gnunet.util.TestMessage |
6 | org.gnunet.util.GnunetMessage$Body|70=org.gnunet.core.NotifyInboundTrafficMessage | 4 | org.gnunet.util.GnunetMessage$Body|274=org.gnunet.mesh.TunnelDestroyMessage |
7 | org.gnunet.util.GnunetMessage$Body|273=org.gnunet.mesh.TunnelCreateMessage | 5 | org.gnunet.util.GnunetMessage$Body|273=org.gnunet.mesh.TunnelCreateMessage |
8 | org.gnunet.util.GnunetMessage$Body|71=org.gnunet.core.NotifyOutboundTrafficMessage | ||
9 | org.gnunet.util.GnunetMessage$Body|272=org.gnunet.mesh.ClientConnectMessage | 6 | org.gnunet.util.GnunetMessage$Body|272=org.gnunet.mesh.ClientConnectMessage |
10 | org.gnunet.util.GnunetMessage$Body|64=org.gnunet.core.InitMessage | ||
11 | org.gnunet.util.GnunetMessage$Body|4=org.gnunet.util.Resolver$GetMessage | 7 | org.gnunet.util.GnunetMessage$Body|4=org.gnunet.util.Resolver$GetMessage |
12 | org.gnunet.util.GnunetMessage$Body|65=org.gnunet.core.InitReplyMessage | ||
13 | org.gnunet.util.GnunetMessage$Body|5=org.gnunet.util.Resolver$ResolverResponse | 8 | org.gnunet.util.GnunetMessage$Body|5=org.gnunet.util.Resolver$ResolverResponse |
14 | org.gnunet.util.GnunetMessage$Body|143=org.gnunet.dht.ClientGetMessage | 9 | org.gnunet.util.GnunetMessage$Body|143=org.gnunet.dht.ClientGetMessage |
15 | org.gnunet.util.GnunetMessage$Body|67=org.gnunet.core.ConnectNotifyMessage | ||
16 | org.gnunet.util.GnunetMessage$Body|142=org.gnunet.dht.ClientPutMessage | 10 | org.gnunet.util.GnunetMessage$Body|142=org.gnunet.dht.ClientPutMessage |
17 | org.gnunet.util.GnunetMessage$Body|76=org.gnunet.core.SendMessage | ||
18 | org.gnunet.util.GnunetMessage$Body|286=org.gnunet.mesh.LocalAckMessage | 11 | org.gnunet.util.GnunetMessage$Body|286=org.gnunet.mesh.LocalAckMessage |
19 | org.gnunet.util.GnunetMessage$Body|74=org.gnunet.core.SendMessageRequest | 12 | org.gnunet.util.GnunetMessage$Body|285=org.gnunet.mesh.DataMessage |
20 | org.gnunet.util.GnunetMessage$Body|75=org.gnunet.core.SendMessageReady | ||
21 | org.gnunet.util.GnunetMessage$Body|153=org.gnunet.dht.MonitorStartStop | 13 | org.gnunet.util.GnunetMessage$Body|153=org.gnunet.dht.MonitorStartStop |
22 | org.gnunet.util.GnunetMessage$Body|155=org.gnunet.dht.ClientPutConfirmationMessage | 14 | org.gnunet.util.GnunetMessage$Body|155=org.gnunet.dht.ClientPutConfirmationMessage |
23 | org.gnunet.util.GnunetMessage$Body|323=org.gnunet.nse.UpdateMessage | ||
24 | org.gnunet.util.GnunetMessage$Body|260=org.gnunet.mesh.DataMessage | ||
25 | org.gnunet.util.GnunetMessage$Body|321=org.gnunet.nse.StartMessage | ||
26 | org.gnunet.util.GnunetMessage$Body|144=org.gnunet.dht.ClientGetStopMessage | 15 | org.gnunet.util.GnunetMessage$Body|144=org.gnunet.dht.ClientGetStopMessage |
27 | org.gnunet.util.GnunetMessage$Body|145=org.gnunet.dht.ClientResultMessage | 16 | org.gnunet.util.GnunetMessage$Body|145=org.gnunet.dht.ClientResultMessage |
28 | org.gnunet.util.GnunetMessage$Body|332=org.gnunet.peerinfo.InfoMessage | ||
29 | org.gnunet.util.GnunetMessage$Body|333=org.gnunet.peerinfo.InfoEnd | ||
30 | org.gnunet.util.GnunetMessage$Body|149=org.gnunet.dht.MonitorGetMessage | 17 | org.gnunet.util.GnunetMessage$Body|149=org.gnunet.dht.MonitorGetMessage |
31 | org.gnunet.util.GnunetMessage$Body|331=org.gnunet.peerinfo.ListAllPeersMessage | ||
32 | org.gnunet.util.GnunetMessage$Body|150=org.gnunet.dht.MonitorGetRespMessage | 18 | org.gnunet.util.GnunetMessage$Body|150=org.gnunet.dht.MonitorGetRespMessage |
33 | org.gnunet.util.GnunetMessage$Body|151=org.gnunet.dht.MonitorPutMessage | 19 | org.gnunet.util.GnunetMessage$Body|151=org.gnunet.dht.MonitorPutMessage |
34 | org.gnunet.util.GnunetMessage$Body|171=org.gnunet.statistics.GetResponseEndMessage | 20 | org.gnunet.util.GnunetMessage$Body|171=org.gnunet.statistics.GetResponseEndMessage |
35 | org.gnunet.util.GnunetMessage$Body|170=org.gnunet.statistics.GetResponseMessage | 21 | org.gnunet.util.GnunetMessage$Body|170=org.gnunet.statistics.GetResponseMessage |
36 | org.gnunet.util.GnunetMessage$Body|169=org.gnunet.statistics.GetMessage | 22 | org.gnunet.util.GnunetMessage$Body|169=org.gnunet.statistics.GetMessage |
37 | org.gnunet.util.GnunetMessage$Body|168=org.gnunet.statistics.SetMessage | 23 | org.gnunet.util.GnunetMessage$Body|168=org.gnunet.statistics.SetMessage |
38 | org.gnunet.util.GnunetMessage$Body|374=org.gnunet.transport.RequestConnectMessage | ||
39 | org.gnunet.util.GnunetMessage$Body|173=org.gnunet.statistics.WatchResponseMessage | 24 | org.gnunet.util.GnunetMessage$Body|173=org.gnunet.statistics.WatchResponseMessage |
40 | org.gnunet.util.GnunetMessage$Body|172=org.gnunet.statistics.WatchMessage | 25 | org.gnunet.util.GnunetMessage$Body|172=org.gnunet.statistics.WatchMessage |
41 | org.gnunet.util.GnunetMessage$Body|524=org.gnunet.consensus.ConcludeMessage | 26 | org.gnunet.util.GnunetMessage$Body|524=org.gnunet.consensus.ConcludeMessage |
42 | org.gnunet.util.GnunetMessage$Body|521=org.gnunet.consensus.InsertElementMessage | 27 | org.gnunet.util.GnunetMessage$Body|521=org.gnunet.consensus.InsertElementMessage |
43 | org.gnunet.util.GnunetMessage$Body|523=org.gnunet.consensus.NewElementMessage | 28 | org.gnunet.util.GnunetMessage$Body|523=org.gnunet.consensus.NewElementMessage |
29 | org.gnunet.util.GnunetMessage$Body|68=org.gnunet.core.DisconnectNotifyMessage | ||
30 | org.gnunet.util.GnunetMessage$Body|70=org.gnunet.core.NotifyInboundTrafficMessage | ||
31 | org.gnunet.util.GnunetMessage$Body|71=org.gnunet.core.NotifyOutboundTrafficMessage | ||
32 | org.gnunet.util.GnunetMessage$Body|64=org.gnunet.core.InitMessage | ||
33 | org.gnunet.util.GnunetMessage$Body|65=org.gnunet.core.InitReplyMessage | ||
34 | org.gnunet.util.GnunetMessage$Body|67=org.gnunet.core.ConnectNotifyMessage | ||
35 | org.gnunet.util.GnunetMessage$Body|76=org.gnunet.core.SendMessage | ||
36 | org.gnunet.util.GnunetMessage$Body|74=org.gnunet.core.SendMessageRequest | ||
37 | org.gnunet.util.GnunetMessage$Body|75=org.gnunet.core.SendMessageReady | ||
38 | org.gnunet.util.GnunetMessage$Body|460=org.gnunet.testbed.ControllerInitMessage | ||
39 | org.gnunet.util.GnunetMessage$Body|323=org.gnunet.nse.UpdateMessage | ||
40 | org.gnunet.util.GnunetMessage$Body|321=org.gnunet.nse.StartMessage | ||
41 | org.gnunet.util.GnunetMessage$Body|332=org.gnunet.peerinfo.InfoMessage | ||
42 | org.gnunet.util.GnunetMessage$Body|333=org.gnunet.peerinfo.InfoEnd | ||
43 | org.gnunet.util.GnunetMessage$Body|331=org.gnunet.peerinfo.ListAllPeersMessage | ||
44 | org.gnunet.util.GnunetMessage$Body|374=org.gnunet.transport.RequestConnectMessage | ||
45 | org.gnunet.util.GnunetMessage$Body|369=org.gnunet.transport.BlacklistInitMessage | ||
46 | org.gnunet.util.GnunetMessage$Body|371=org.gnunet.transport.BlacklistReplyMessage | ||
47 | org.gnunet.util.GnunetMessage$Body|370=org.gnunet.transport.BlacklistQueryMessage | ||
48 | org.gnunet.util.GnunetMessage$Body|380=org.gnunet.transport.AddressIterateMessage | ||
49 | org.gnunet.util.GnunetMessage$Body|496=org.gnunet.testbed.HelperReplyMessage | ||
50 | org.gnunet.util.GnunetMessage$Body|495=org.gnunet.testbed.HelperInitMessage | ||
44 | org.gnunet.util.GnunetMessage$Body|360=org.gnunet.transport.StartMessage | 51 | org.gnunet.util.GnunetMessage$Body|360=org.gnunet.transport.StartMessage |
45 | org.gnunet.construct.MessageUnion|525=org.gnunet.consensus.ConcludeDoneMessage | 52 | org.gnunet.construct.MessageUnion|525=org.gnunet.consensus.ConcludeDoneMessage |
46 | # generated 2013/08/22 21:29:40 | 53 | # generated 2013/09/04 11:05:50 |
diff --git a/src/main/resources/org/gnunet/voting/template.espec b/src/main/resources/org/gnunet/voting/template.espec new file mode 100644 index 0000000..b1c9a01 --- /dev/null +++ b/src/main/resources/org/gnunet/voting/template.espec | |||
@@ -0,0 +1,36 @@ | |||
1 | [election] | ||
2 | |||
3 | # human-readable topic description of the election | ||
4 | TOPIC = | ||
5 | |||
6 | # choices for the voter, separated by a double slash | ||
7 | CHOICES = yes//no | ||
8 | |||
9 | # starting time of the election, yy/mm/dd hh:mm:ss UTC+x | ||
10 | # authorities affirm that they are available for the election from this point in time | ||
11 | ELECTION_START = | ||
12 | |||
13 | # deadline for vote submission, yy/mm/dd hh:mm:ss UTC+x | ||
14 | # must be later than ELECTION_START | ||
15 | ELECTION_END = | ||
16 | |||
17 | # peer identity of the certificate authority for voting eligibility | ||
18 | CA = | ||
19 | |||
20 | # public key of the election issuer | ||
21 | ISSUER_PUB = | ||
22 | |||
23 | # the signature of the issuer will be filled in by gnunet-vote-call | ||
24 | # ISSUER_SIGNATURE = | ||
25 | |||
26 | |||
27 | [authorities] | ||
28 | # specified as <authority-alias> = <peer-identity>, one entry for each authority; e.g. | ||
29 | # awesome_authority_one = 123abc | ||
30 | |||
31 | |||
32 | [authority-affirmation] | ||
33 | # will be filled in by gnunet-vote-call once | ||
34 | # authorities agreed to participate, with one entry per authority | ||
35 | # <authority-alias> = <signature> | ||
36 | # where the signature is on the | ||
diff --git a/src/test/java/org/gnunet/construct/ConstructTest.java b/src/test/java/org/gnunet/construct/ConstructTest.java index ed6ac51..01754ea 100644 --- a/src/test/java/org/gnunet/construct/ConstructTest.java +++ b/src/test/java/org/gnunet/construct/ConstructTest.java | |||
@@ -6,6 +6,8 @@ import org.junit.Test; | |||
6 | import java.util.Random; | 6 | import java.util.Random; |
7 | 7 | ||
8 | /** | 8 | /** |
9 | * Miscellaneous tests for org.gnunet.construct. | ||
10 | * | ||
9 | * @author Florian Dold | 11 | * @author Florian Dold |
10 | */ | 12 | */ |
11 | public class ConstructTest { | 13 | public class ConstructTest { |
@@ -17,6 +19,24 @@ public class ConstructTest { | |||
17 | public byte[] bytes; | 19 | public byte[] bytes; |
18 | } | 20 | } |
19 | 21 | ||
22 | public static class BoolTestMessage implements Message { | ||
23 | @UInt32 | ||
24 | public boolean x1; | ||
25 | @Int16 | ||
26 | public boolean x2; | ||
27 | } | ||
28 | |||
29 | @Test | ||
30 | public void test_bool_conversion() { | ||
31 | BoolTestMessage m = new BoolTestMessage(); | ||
32 | m.x1 = true; | ||
33 | m.x2 = true; | ||
34 | byte[] bin = Construct.toBinary(m); | ||
35 | BoolTestMessage m2 = Construct.parseAs(bin, BoolTestMessage.class); | ||
36 | Assert.assertEquals(m.x1, m2.x1); | ||
37 | Assert.assertEquals(m.x2, m2.x2); | ||
38 | } | ||
39 | |||
20 | @Test | 40 | @Test |
21 | public void test_ByteFill() { | 41 | public void test_ByteFill() { |
22 | ByteFillTestMessage msg = new ByteFillTestMessage(); | 42 | ByteFillTestMessage msg = new ByteFillTestMessage(); |
diff --git a/src/test/java/org/gnunet/mesh/MeshTest.java b/src/test/java/org/gnunet/mesh/MeshTest.java index ad5eaa3..f8694de 100644 --- a/src/test/java/org/gnunet/mesh/MeshTest.java +++ b/src/test/java/org/gnunet/mesh/MeshTest.java | |||
@@ -1,5 +1,7 @@ | |||
1 | package org.gnunet.mesh; | 1 | package org.gnunet.mesh; |
2 | 2 | ||
3 | import org.gnunet.core.Core; | ||
4 | import org.gnunet.core.PeerIdentityContinuation; | ||
3 | import org.gnunet.testing.TestingFixture; | 5 | import org.gnunet.testing.TestingFixture; |
4 | import org.gnunet.testing.TestingSubsystem; | 6 | import org.gnunet.testing.TestingSubsystem; |
5 | import org.gnunet.util.*; | 7 | import org.gnunet.util.*; |
@@ -16,32 +18,42 @@ public class MeshTest extends TestingFixture { | |||
16 | public Mesh m1; | 18 | public Mesh m1; |
17 | public Mesh m2; | 19 | public Mesh m2; |
18 | public void visit(TestMessage m) { | 20 | public void visit(TestMessage m) { |
21 | System.out.println("got test message"); | ||
19 | m1.destroy(); | 22 | m1.destroy(); |
20 | m2.destroy(); | 23 | m2.destroy(); |
21 | } | 24 | } |
22 | } | 25 | } |
23 | //@Test | 26 | @Test (timeout = 5000) |
24 | public void test_mesh_send() { | 27 | public void test_mesh_send() { |
25 | Program.configureLogging("DEBUG"); | 28 | Program.configureLogging("DEBUG"); |
26 | final TestingSubsystem ts = new TestingSubsystem("mesh"); | 29 | // we want the full arm here (starts both mesh and core) |
27 | Configuration cfg = ts.getConfiguration(); | 30 | final TestingSubsystem ts = new TestingSubsystem("arm"); |
28 | MessageHandler1 mh = new MessageHandler1(); | 31 | final Configuration cfg = ts.getConfiguration(); |
29 | final Mesh mesh1 = new Mesh(cfg, null, null, null); | ||
30 | final Mesh mesh2 = new Mesh(cfg, new InboundTunnelHandler() { | ||
31 | @Override | ||
32 | public void onInboundTunnel(Mesh.Tunnel tunnel, PeerIdentity initiator) { | ||
33 | 32 | ||
34 | } | 33 | final MessageHandler1 mh = new MessageHandler1(); |
35 | }, new TunnelEndHandler() { | 34 | // FIXME: use CryptoECC instead of Core once available and compatible |
35 | Core.withPeerIdentity(cfg, new PeerIdentityContinuation() { | ||
36 | @Override | 36 | @Override |
37 | public void onTunnelEnd(Mesh.Tunnel tunnel) { | 37 | public void cont(PeerIdentity peerIdentity) { |
38 | 38 | System.out.println("got peer identity from core"); | |
39 | final Mesh mesh1 = new Mesh(cfg, null, null, null); | ||
40 | final Mesh mesh2 = new Mesh(cfg, new InboundTunnelHandler() { | ||
41 | @Override | ||
42 | public void onInboundTunnel(Mesh.Tunnel tunnel, PeerIdentity initiator) { | ||
43 | System.out.println("got inbound tunnel " + initiator); | ||
44 | } | ||
45 | }, new TunnelEndHandler() { | ||
46 | @Override | ||
47 | public void onTunnelEnd(Mesh.Tunnel tunnel) { | ||
48 | System.out.println("tunnel end handler called"); | ||
49 | } | ||
50 | }, mh, 42); | ||
51 | mh.m1 = mesh1; | ||
52 | mh.m2 = mesh2; | ||
53 | Mesh.Tunnel<Void> tunnel = mesh1.createTunnel(peerIdentity, 42, false, true, null); | ||
54 | tunnel.send(new TestMessage()); | ||
39 | } | 55 | } |
40 | }, mh, 42); | 56 | }); |
41 | mh.m1 = mesh1; | ||
42 | mh.m2 = mesh2; | ||
43 | Mesh.Tunnel<Void> tunnel = mesh2.createTunnel(null, 42, false, true, null); | ||
44 | tunnel.send(new TestMessage()); | ||
45 | Scheduler.run(); | 57 | Scheduler.run(); |
46 | } | 58 | } |
47 | } | 59 | } |
diff --git a/src/test/java/org/gnunet/testbed/TestbedTest.java b/src/test/java/org/gnunet/testbed/TestbedTest.java new file mode 100644 index 0000000..4e1af03 --- /dev/null +++ b/src/test/java/org/gnunet/testbed/TestbedTest.java | |||
@@ -0,0 +1,41 @@ | |||
1 | package org.gnunet.testbed; | ||
2 | |||
3 | |||
4 | import org.gnunet.testing.TestingFixture; | ||
5 | import org.gnunet.util.Configuration; | ||
6 | import org.gnunet.util.Program; | ||
7 | import org.gnunet.util.Scheduler; | ||
8 | import org.gnunet.util.Wrapper; | ||
9 | import org.junit.Assert; | ||
10 | import org.junit.Test; | ||
11 | |||
12 | public class TestbedTest extends TestingFixture { | ||
13 | |||
14 | |||
15 | @Test | ||
16 | public void test_controller_proc() { | ||
17 | final Wrapper<Boolean> success = new Wrapper<Boolean>(false); | ||
18 | new Program() { | ||
19 | |||
20 | @Override | ||
21 | public void run() { | ||
22 | // use local peer's config, does that really make sense? | ||
23 | Host h = new Host(null, null, getConfiguration(), 0); | ||
24 | final ControllerProc cp = new ControllerProc(); | ||
25 | cp.start("127.0.0.1", h, new ControllerStatusCallback() { | ||
26 | @Override | ||
27 | public void onStartupSuccess(Configuration cfg) { | ||
28 | success.set(true); | ||
29 | cp.stop(); | ||
30 | } | ||
31 | @Override | ||
32 | public void onStartupFailure() { | ||
33 | Assert.fail(); | ||
34 | //To change body of implemented methods use File | Settings | File Templates. | ||
35 | } | ||
36 | }); | ||
37 | } | ||
38 | }.start(); | ||
39 | Assert.assertTrue(success.get()); | ||
40 | } | ||
41 | } | ||
diff --git a/src/test/java/org/gnunet/util/CryptoECCTest.java b/src/test/java/org/gnunet/util/CryptoECCTest.java new file mode 100644 index 0000000..cc88409 --- /dev/null +++ b/src/test/java/org/gnunet/util/CryptoECCTest.java | |||
@@ -0,0 +1,82 @@ | |||
1 | package org.gnunet.util; | ||
2 | |||
3 | |||
4 | import org.junit.Assert; | ||
5 | import org.junit.Test; | ||
6 | |||
7 | import java.util.Random; | ||
8 | |||
9 | public class CryptoECCTest { | ||
10 | /** | ||
11 | * Check that signed messages can be verified correctly. | ||
12 | */ | ||
13 | @Test | ||
14 | public void test_sign_success() { | ||
15 | Random r = new Random(); | ||
16 | // the test uses random data, repeat it multiple times! | ||
17 | for (int i = 0; i < 10; i++) { | ||
18 | byte[] msg = new byte[16]; | ||
19 | r.nextBytes(msg); | ||
20 | |||
21 | CryptoECC.PrivateKey privateKey = new CryptoECC.PrivateKey(); | ||
22 | privateKey.d = new byte[32]; | ||
23 | r.nextBytes(privateKey.d); | ||
24 | CryptoECC.PublicKey publicKey = CryptoECC.computePublicKey(privateKey); | ||
25 | |||
26 | CryptoECC.Signature sig = CryptoECC.sign(msg, privateKey, publicKey); | ||
27 | |||
28 | boolean valid = CryptoECC.verify(sig, msg, publicKey); | ||
29 | |||
30 | Assert.assertTrue(valid); | ||
31 | } | ||
32 | } | ||
33 | |||
34 | /** | ||
35 | * Check that signature verification fails for manipulated data. | ||
36 | */ | ||
37 | @Test | ||
38 | public void test_sign_failure() { | ||
39 | Random r = new Random(); | ||
40 | // the test uses random data, repeat it multiple times! | ||
41 | for (int i = 0; i < 10; i++) { | ||
42 | byte[] msg = new byte[16]; | ||
43 | r.nextBytes(msg); | ||
44 | |||
45 | CryptoECC.PrivateKey privateKey = new CryptoECC.PrivateKey(); | ||
46 | privateKey.d = new byte[32]; | ||
47 | r.nextBytes(privateKey.d); | ||
48 | CryptoECC.PublicKey publicKey = CryptoECC.computePublicKey(privateKey); | ||
49 | |||
50 | CryptoECC.Signature sig = CryptoECC.sign(msg, privateKey, publicKey); | ||
51 | |||
52 | msg[0] = (byte) (msg[0] + 1); | ||
53 | |||
54 | boolean valid = CryptoECC.verify(sig, msg, publicKey); | ||
55 | |||
56 | Assert.assertFalse(valid); | ||
57 | } | ||
58 | } | ||
59 | |||
60 | /** | ||
61 | * Check whether ecdh key coincide | ||
62 | */ | ||
63 | @Test | ||
64 | public void test_ecdh() { | ||
65 | Random r = new Random(); | ||
66 | |||
67 | CryptoECC.PrivateKey privateAlice = new CryptoECC.PrivateKey(); | ||
68 | privateAlice.d = new byte[32]; | ||
69 | r.nextBytes(privateAlice.d); | ||
70 | CryptoECC.PublicKey publicAlice = CryptoECC.computePublicKey(privateAlice); | ||
71 | |||
72 | CryptoECC.PrivateKey privateBob = new CryptoECC.PrivateKey(); | ||
73 | privateBob.d = new byte[32]; | ||
74 | r.nextBytes(privateBob.d); | ||
75 | CryptoECC.PublicKey publicBob = CryptoECC.computePublicKey(privateBob); | ||
76 | |||
77 | HashCode ssAlice = CryptoECC.ecdh(privateAlice, publicBob); | ||
78 | HashCode ssBob = CryptoECC.ecdh(privateBob, publicAlice); | ||
79 | |||
80 | Assert.assertArrayEquals(ssAlice.data, ssBob.data); | ||
81 | } | ||
82 | } | ||