summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Palfrader <peter@palfrader.org>2003-10-17 17:34:54 +0000
committerPeter Palfrader <peter@palfrader.org>2003-10-17 17:34:54 +0000
commit46a03a3ea3c26a65e4428bc9de036a01487aeda3 (patch)
tree6a05fbfcb47a7f08eba65fc3a0f7c3e7247a629e
parentf5d3577a642e961ff247abef37e05c2399b26736 (diff)
First go at refactoring for PathSpecs
-rw-r--r--src/Main.java23
-rw-r--r--src/org/noreply/fancydress/type3/HalfPath.java51
-rw-r--r--src/org/noreply/fancydress/type3/Message.java30
-rw-r--r--src/org/noreply/fancydress/type3/Packet.java13
-rw-r--r--src/org/noreply/fancydress/type3/Path.java10
-rw-r--r--src/org/noreply/fancydress/type3/PathSpec.java95
-rw-r--r--src/org/noreply/fancydress/type3/Payload.java49
7 files changed, 167 insertions, 104 deletions
diff --git a/src/Main.java b/src/Main.java
index b86a8b4..da89a50 100644
--- a/src/Main.java
+++ b/src/Main.java
@@ -13,17 +13,18 @@ public class Main {
/* Start the parser */
try {
DirectoryParser parser = new DirectoryParser(new DirectoryLexer(new FileReader(argv[0])));
- DirectoryMessage m = (DirectoryMessage)parser.parse().value;
- Directory dir = new Directory(m, false);
- Path path = new Path(dir,"test1 : tonga");
- RoutingDestination address = new RoutingSMTP("peter@palfrader.org");
- Payload payload = new Payload();
- Packet packet = new Packet(path, address, payload);
- MMTP.send(packet);
- /*
- FileOutputStream w = new FileOutputStream("out");
- w.write(packet.asOctets());
- */
+ DirectoryMessage dm = (DirectoryMessage)parser.parse().value;
+ Directory dir = new Directory(dm, false);
+ PathSpec path = new PathSpec(dir,"test1 : tonga", false);
+
+ RoutingSMTP destination = new RoutingSMTP("peter@palfrader.org");
+ String body = "FROM:Peter\n" +
+ "SUBJECT:test fancydress\n" +
+ "\n" +
+ "blubb\n";
+
+ Message message = new Message(path, destination, body);
+ message.send();
} catch (Exception e) {
e.printStackTrace();
diff --git a/src/org/noreply/fancydress/type3/HalfPath.java b/src/org/noreply/fancydress/type3/HalfPath.java
index 14d4af6..73dfb84 100644
--- a/src/org/noreply/fancydress/type3/HalfPath.java
+++ b/src/org/noreply/fancydress/type3/HalfPath.java
@@ -1,62 +1,11 @@
/* $Id$ */
package org.noreply.fancydress.type3;
-import org.noreply.fancydress.directory.*;
-import org.noreply.fancydress.status.*;
-import org.noreply.fancydress.type3.routing.*;
-import java.util.*;
-
public class HalfPath {
private Hop[] hops;
public HalfPath(Hop[] hops) {
this.hops = hops;
}
-
- private String[] tokenize(String path) throws Mix3Exception {
- ArrayList nicks = new ArrayList();
- int indexFrom = 0;
- int indexOf;
-
- while ((indexOf = path.indexOf(',', indexFrom)) != -1) {
- String v = path.substring(indexFrom, indexOf).trim();
- if (v.equals(""))
- throw new Mix3Exception("Invalid path.");
- nicks.add( v );
- indexFrom = indexOf + 1;
- }
- String v = path.substring(indexFrom).trim();
- if (v.equals(""))
- throw new Mix3Exception("Invalid path.");
- nicks.add( v );
-
- String[] result = new String[nicks.size()];
- for (int i=0; i<result.length; i++)
- result[i] = (String) nicks.get(i);
-
- return result;
- }
-
- public HalfPath(Directory dir, String path) throws Mix3Exception {
- String[] nicks = tokenize(path);
- hops = new Hop[nicks.length];
-
- for (int i=0; i<hops.length; i++) {
- Server server = dir.getServer(nicks[i]);
- if (server == null)
- throw new Mix3Exception("Invalid path: "+nicks[i]+" not found");
- ServerDescriptor desc = server.getDescriptor();
- IncomingMMTPSection incoming = desc.getIncomingMMTPSection();
- Routing routing;
-
- if (incoming.getHostname() != null) { /* FIXME */
- routing = new RoutingHOST(incoming.getHostname(), incoming.getPort(), server.getKeyID());
- } else {
- routing = new RoutingIP4(incoming.getIP(), incoming.getPort(), server.getKeyID());
- }
- hops[i] = new Hop(routing, desc.getPacketKey());
- }
- }
-
public Hop[] getHops() {
return hops;
}
diff --git a/src/org/noreply/fancydress/type3/Message.java b/src/org/noreply/fancydress/type3/Message.java
new file mode 100644
index 0000000..4861c85
--- /dev/null
+++ b/src/org/noreply/fancydress/type3/Message.java
@@ -0,0 +1,30 @@
+/* $Id$ */
+package org.noreply.fancydress.type3;
+
+import java.io.*;
+import java.util.zip.*;
+import org.noreply.fancydress.crypto.*;
+import org.noreply.fancydress.status.*;
+import org.noreply.fancydress.misc.Util;
+import org.noreply.fancydress.type3.routing.*;
+import org.noreply.fancydress.type3.mmtp.*;
+
+public class Message {
+ Packet[] packets;
+
+ public Message(PathSpec path, RoutingDestination destination, String body) throws Mix3Exception {
+ Payload payload = new Payload(destination, body);
+ int numberOfPackets = payload.numPackets();
+ packets = new Packet[numberOfPackets];
+ for (int i=0; i<numberOfPackets; i++) {
+ packets[i] = new Packet(path.getPath(), payload.getRoute(i), payload.getPayload(i));
+ }
+ }
+
+ public void send() throws Exception {
+ for (int i=0; i<packets.length; i++) {
+ MMTP.send(packets[i]);
+ }
+ }
+}
+
diff --git a/src/org/noreply/fancydress/type3/Packet.java b/src/org/noreply/fancydress/type3/Packet.java
index 7c6f2f1..6a28791 100644
--- a/src/org/noreply/fancydress/type3/Packet.java
+++ b/src/org/noreply/fancydress/type3/Packet.java
@@ -15,7 +15,7 @@ public class Packet {
public Packet(
Path path,
RoutingDestination address,
- Payload payload)
+ byte[] payload)
throws Mix3BadArgumentsChainTooLongException
{
ForwardLeg leg2 = new ForwardLeg(path.getSecondHalf(), address);
@@ -28,7 +28,7 @@ public class Packet {
public Packet(
HalfPath path,
SURB surb,
- Payload payload)
+ byte[] payload)
throws Mix3BadArgumentsChainTooLongException
{
ForwardLeg leg1 = new ForwardLeg(path, surb.getRoute());
@@ -39,11 +39,13 @@ public class Packet {
return s.equals(SingleLeg.MAJOR_VERSION + "." + SingleLeg.MINOR_VERSION);
}
- private void makePacket(ForwardLeg leg1, SingleLeg leg2, Payload payload) {
- byte p[] = payload.asOctets();
+ private void makePacket(ForwardLeg leg1, SingleLeg leg2, byte[] p) {
byte h1[] = leg1.asOctets();
byte h2[] = leg2.asOctets();
+ if (p.length != 28*1024)
+ throw new IllegalArgumentException("Payload needs to be 28 kb long");
+
// Phase 1
if (leg2 instanceof SURB) {
byte[] k = ((SURB) leg2).getEncryptionKey();
@@ -66,6 +68,9 @@ public class Packet {
packet = Util.concat(Util.concat(h1,h2),p);
thisRoute = leg1.getRoute();
+
+ if (packet.length != 32*1024)
+ throw new Error("packet is not 32k bytes");
}
public RoutingForward getRoute() {
diff --git a/src/org/noreply/fancydress/type3/Path.java b/src/org/noreply/fancydress/type3/Path.java
index 4289f6b..8373990 100644
--- a/src/org/noreply/fancydress/type3/Path.java
+++ b/src/org/noreply/fancydress/type3/Path.java
@@ -12,13 +12,9 @@ public class Path {
HalfPath first;
HalfPath second;
- public Path(Directory dir, String path) throws Mix3Exception {
- int crossover = path.indexOf(':');
- if (crossover < 0)
- throw new Mix3Exception("Path is not a valid path: no crossover point specified.");
-
- first = new HalfPath(dir, path.substring(0, crossover));
- second = new HalfPath(dir, path.substring(crossover+1));
+ public Path(Hop[] leg1, Hop[] leg2) {
+ first = new HalfPath(leg1);
+ second = new HalfPath(leg2);
}
public HalfPath getFirstHalf() {
diff --git a/src/org/noreply/fancydress/type3/PathSpec.java b/src/org/noreply/fancydress/type3/PathSpec.java
new file mode 100644
index 0000000..cbc608f
--- /dev/null
+++ b/src/org/noreply/fancydress/type3/PathSpec.java
@@ -0,0 +1,95 @@
+/* $Id$ */
+package org.noreply.fancydress.type3;
+
+import org.noreply.fancydress.crypto.*;
+import org.noreply.fancydress.status.*;
+import org.noreply.fancydress.directory.*;
+import org.noreply.fancydress.misc.Util;
+import org.noreply.fancydress.type3.routing.*;
+import java.net.InetAddress;
+import java.util.*;
+
+
+public class PathSpec {
+ String pathSpec;
+ Path singlePath;
+
+ private String[] tokenize(String path) throws Mix3Exception {
+ ArrayList nicks = new ArrayList();
+ int indexFrom = 0;
+ int indexOf;
+
+ while ((indexOf = path.indexOf(',', indexFrom)) != -1) {
+ String v = path.substring(indexFrom, indexOf).trim();
+ if (v.equals(""))
+ throw new Mix3Exception("Invalid path.");
+ nicks.add( v );
+ indexFrom = indexOf + 1;
+ }
+ String v = path.substring(indexFrom).trim();
+ if (v.equals(""))
+ throw new Mix3Exception("Invalid path.");
+ nicks.add( v );
+
+ String[] result = new String[nicks.size()];
+ for (int i=0; i<result.length; i++)
+ result[i] = (String) nicks.get(i);
+
+ return result;
+ }
+
+ private Hop[] parseHalfPath(Directory dir, String path) throws Mix3Exception {
+ String[] nicks = tokenize(path);
+ Hop[] hops = new Hop[nicks.length];
+
+ for (int i=0; i<hops.length; i++) {
+ Server server = dir.getServer(nicks[i]);
+ if (server == null)
+ throw new Mix3Exception("Invalid path: "+nicks[i]+" not found");
+ ServerDescriptor desc = server.getDescriptor();
+ IncomingMMTPSection incoming = desc.getIncomingMMTPSection();
+ Routing routing;
+
+ if (incoming.getHostname() != null) { /* FIXME */
+ routing = new RoutingHOST(incoming.getHostname(), incoming.getPort(), server.getKeyID());
+ } else {
+ routing = new RoutingIP4(incoming.getIP(), incoming.getPort(), server.getKeyID());
+ }
+ hops[i] = new Hop(routing, desc.getPacketKey());
+ }
+ return hops;
+ }
+
+
+ /**
+ * Create a path specification from a string.
+ *
+ * A path spec could look like "<code>Foo,Bar,?:Baz,*2,~1</code>".
+ *
+ * FIXME: right now we don't do any random foo.
+ *
+ * @param path given path
+ */
+ public PathSpec(Directory dir, String pathSpec, boolean singleLeg) throws Mix3Exception {
+ this.pathSpec = pathSpec;
+
+ int crossover = pathSpec.indexOf(':');
+ if (crossover < 0)
+ throw new Mix3Exception("Path is not a valid path: no crossover point specified.");
+
+ Hop[] leg1 = parseHalfPath(dir, pathSpec.substring(0, crossover));
+ Hop[] leg2 = parseHalfPath(dir, pathSpec.substring(crossover+1));
+ singlePath = new Path(leg1, leg2);
+ }
+
+ /**
+ * Return a path constructed from this PathSpec
+ *
+ * @param dir a directory
+ * @return path
+ */
+ public Path getPath() throws Mix3Exception {
+ return singlePath;
+ }
+}
+
diff --git a/src/org/noreply/fancydress/type3/Payload.java b/src/org/noreply/fancydress/type3/Payload.java
index 16b1b57..73027cd 100644
--- a/src/org/noreply/fancydress/type3/Payload.java
+++ b/src/org/noreply/fancydress/type3/Payload.java
@@ -8,34 +8,10 @@ import org.noreply.fancydress.misc.Util;
import org.noreply.fancydress.type3.routing.*;
public class Payload {
- byte[] thisPayload;
-
- public Payload() {
- RoutingSMTP route = new RoutingSMTP("peter@palfrader.org");
- String body = "FROM:Peter\n" +
- "SUBJECT:test fancydress\n" +
- "\n" +
- "blubb\n";
-
- /*
- // Encode a String into bytes
- String inputString = "blahblahblah??";
- byte[] input = inputString.getBytes("UTF-8");
-
- // Compress the bytes
- byte[] output = new byte[100];
- Deflater compresser = new Deflater();
- compresser.setInput(input);
- compresser.finish();
- int compressedDataLength = compresser.deflate(output);
-
- // Decompress the bytes
- Inflater decompresser = new Inflater();
- decompresser.setInput(output, 0, compressedDataLength);
- byte[] result = new byte[100];
- int resultLength = decompresser.inflate(result);
- decompresser.end();
- */
+ byte[][] payloads;
+ RoutingDestination[] route;
+
+ public Payload(RoutingDestination route, String body) {
try {
PipedOutputStream poMessage = new PipedOutputStream();
PipedInputStream piMessage = new PipedInputStream( poMessage );
@@ -77,18 +53,29 @@ public class Payload {
if (pos != 28*1024)
throw new Error("did not fill in 28k bytes");
- thisPayload = payload;
+ payloads = new byte[1][];
+ payloads[0] = payload;
} catch (Exception e) {
throw new Error(e);
};
+ this.route = new RoutingDestination[1];
+ this.route[0] = route;
// Return a singleton payload containing:
// Flag 0 | Int(15,LEN(M_C)) | Hash(M_C | PADDING) | M_C | PADDING
}
- public byte[] asOctets() {
- return thisPayload;
+ public int numPackets() {
+ return payloads.length;
+ }
+
+ public byte[] getPayload(int i) {
+ return payloads[i];
+ }
+
+ public RoutingDestination getRoute(int i) {
+ return route[i];
}
}