commit 1743e912c3347824e4fef05569bdb194a1e04a0e Author: Karsten Loesing karsten.loesing@gmx.net Date: Mon May 21 16:23:39 2012 +0200
Parse sanitized bridge descriptor version 1.0. --- .../descriptor/impl/BridgeNetworkStatusImpl.java | 32 ++++++++++++++++++- .../descriptor/impl/ExtraInfoDescriptorImpl.java | 16 ++++++++++ .../descriptor/impl/ServerDescriptorImpl.java | 16 ++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-)
diff --git a/src/org/torproject/descriptor/impl/BridgeNetworkStatusImpl.java b/src/org/torproject/descriptor/impl/BridgeNetworkStatusImpl.java index 09f2f86..581c6e5 100644 --- a/src/org/torproject/descriptor/impl/BridgeNetworkStatusImpl.java +++ b/src/org/torproject/descriptor/impl/BridgeNetworkStatusImpl.java @@ -4,6 +4,8 @@ package org.torproject.descriptor.impl;
import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Scanner; import java.util.TimeZone;
import org.torproject.descriptor.BridgeNetworkStatus; @@ -21,6 +23,11 @@ public class BridgeNetworkStatusImpl extends NetworkStatusImpl
private void setPublishedMillisFromFileName(String fileName) throws DescriptorParseException { + if (this.publishedMillis != 0L) { + /* We already learned the publication timestamp from parsing the + * "published" line. */ + return; + } if (fileName.length() == "20000101-000000-4A0CCD2DDC7995083D73F5D667100C8A5831F16D". length()) { @@ -44,8 +51,29 @@ public class BridgeNetworkStatusImpl extends NetworkStatusImpl
protected void parseHeader(byte[] headerBytes) throws DescriptorParseException { - throw new DescriptorParseException("No directory header expected in " - + "bridge network status."); + Scanner s = new Scanner(new String(headerBytes)).useDelimiter("\n"); + while (s.hasNext()) { + String line = s.next(); + String[] parts = line.split(" "); + String keyword = parts[0]; + if (keyword.equals("published")) { + this.parsePublishedLine(line, parts); + } else if (this.failUnrecognizedDescriptorLines) { + throw new DescriptorParseException("Unrecognized line '" + line + + "' in bridge network status."); + } else { + if (this.unrecognizedLines == null) { + this.unrecognizedLines = new ArrayList<String>(); + } + this.unrecognizedLines.add(line); + } + } + } + + private void parsePublishedLine(String line, String[] parts) + throws DescriptorParseException { + this.publishedMillis = ParseHelper.parseTimestampAtIndex(line, parts, + 1, 2); }
protected void parseDirSource(byte[] dirSourceBytes) diff --git a/src/org/torproject/descriptor/impl/ExtraInfoDescriptorImpl.java b/src/org/torproject/descriptor/impl/ExtraInfoDescriptorImpl.java index d8fbc8d..f6e0fd9 100644 --- a/src/org/torproject/descriptor/impl/ExtraInfoDescriptorImpl.java +++ b/src/org/torproject/descriptor/impl/ExtraInfoDescriptorImpl.java @@ -168,6 +168,8 @@ public class ExtraInfoDescriptorImpl extends DescriptorImpl this.parseBridgeStatsIpsLine(line, lineNoOpt, partsNoOpt); } else if (keyword.equals("router-signature")) { this.parseRouterSignatureLine(line, lineNoOpt, partsNoOpt); + } else if (keyword.equals("router-digest")) { + this.parseRouterDigestLine(line, lineNoOpt, partsNoOpt); } else if (line.startsWith("-----BEGIN")) { skipCrypto = true; } else if (line.startsWith("-----END")) { @@ -573,7 +575,21 @@ public class ExtraInfoDescriptorImpl extends DescriptorImpl /* Not parsing crypto parts (yet). */ }
+ private void parseRouterDigestLine(String line, String lineNoOpt, + String[] partsNoOpt) throws DescriptorParseException { + if (partsNoOpt.length != 2) { + throw new DescriptorParseException("Illegal line '" + line + "'."); + } + this.extraInfoDigest = ParseHelper.parseTwentyByteHexString(line, + partsNoOpt[1]); + } + private void calculateDigest() throws DescriptorParseException { + if (this.extraInfoDigest != null) { + /* We already learned the descriptor digest of this bridge + * descriptor from a "router-digest" line. */ + return; + } try { String ascii = new String(this.getRawDescriptorBytes(), "US-ASCII"); String startToken = "extra-info "; diff --git a/src/org/torproject/descriptor/impl/ServerDescriptorImpl.java b/src/org/torproject/descriptor/impl/ServerDescriptorImpl.java index 5237348..df3662f 100644 --- a/src/org/torproject/descriptor/impl/ServerDescriptorImpl.java +++ b/src/org/torproject/descriptor/impl/ServerDescriptorImpl.java @@ -125,6 +125,8 @@ public class ServerDescriptorImpl extends DescriptorImpl this.parseAllowSingleHopExitsLine(line, lineNoOpt, partsNoOpt); } else if (keyword.equals("dircacheport")) { this.parseDircacheportLine(line, lineNoOpt, partsNoOpt); + } else if (keyword.equals("router-digest")) { + this.parseRouterDigestLine(line, lineNoOpt, partsNoOpt); } else if (line.startsWith("-----BEGIN")) { crypto = new StringBuilder(); crypto.append(line + "\n"); @@ -459,7 +461,21 @@ public class ServerDescriptorImpl extends DescriptorImpl this.dirPort = ParseHelper.parsePort(line, partsNoOpt[1]); }
+ private void parseRouterDigestLine(String line, String lineNoOpt, + String[] partsNoOpt) throws DescriptorParseException { + if (partsNoOpt.length != 2) { + throw new DescriptorParseException("Illegal line '" + line + "'."); + } + this.serverDescriptorDigest = ParseHelper.parseTwentyByteHexString( + line, partsNoOpt[1]); + } + private void calculateDigest() throws DescriptorParseException { + if (this.serverDescriptorDigest != null) { + /* We already learned the descriptor digest of this bridge + * descriptor from a "router-digest" line. */ + return; + } try { String ascii = new String(this.getRawDescriptorBytes(), "US-ASCII"); String startToken = "router ";