Skip to content

Commit

Permalink
Update multiaddr to support Cid.
Browse files Browse the repository at this point in the history
Fix bug in IPv4 tolerance
  • Loading branch information
ianopolous committed May 24, 2017
1 parent 913173f commit 4ac125c
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 5 deletions.
Binary file added lib/cid.jar
Binary file not shown.
Binary file added lib/multibase.jar
Binary file not shown.
Binary file modified lib/multihash.jar
Binary file not shown.
14 changes: 12 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>io.ipfs</groupId>
<artifactId>multiaddr</artifactId>
<version>1.0.0</version>
<version>1.1.0</version>
<packaging>jar</packaging>

<name>multiaddr</name>
Expand Down Expand Up @@ -58,9 +58,19 @@
</dependency>
<dependency>
<groupId>com.github.multiformats</groupId>
<artifactId>java-multihash</artifactId>
<artifactId>java-multibase</artifactId>
<version>v1.0.0</version>
</dependency>
<dependency>
<groupId>com.github.multiformats</groupId>
<artifactId>java-multihash</artifactId>
<version>v1.1.0</version>
</dependency>
<dependency>
<groupId>com.github.ipld</groupId>
<artifactId>java-cid</artifactId>
<version>v1.1.0</version>
</dependency>
</dependencies>

<build>
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/io/ipfs/multiaddr/Protocol.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package io.ipfs.multiaddr;

import io.ipfs.multihash.Multihash;
import io.ipfs.cid.Cid;
import java.io.*;
import java.net.*;
import java.util.*;

public class Protocol {
public static int LENGTH_PREFIXED_VAR_SIZE = -1;
private static final String IPV4_REGEX = "\\A(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\z";

enum Type {
IP4(4, 32, "ip4"),
Expand Down Expand Up @@ -71,6 +73,8 @@ public byte[] addressToBytes(String addr) {
try {
switch (type) {
case IP4:
if (! addr.matches(IPV4_REGEX))
throw new IllegalStateException("Invalid IPv4 address: " + addr);
return Inet4Address.getByName(addr).getAddress();
case IP6:
return Inet6Address.getByName(addr).getAddress();
Expand All @@ -83,7 +87,7 @@ public byte[] addressToBytes(String addr) {
throw new IllegalStateException("Failed to parse "+type.name+" address "+addr + " (> 65535");
return new byte[]{(byte)(x >>8), (byte)x};
case IPFS:
Multihash hash = Multihash.fromBase58(addr);
Multihash hash = Cid.decode(addr);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] hashBytes = hash.toBytes();
byte[] varint = new byte[(32 - Integer.numberOfLeadingZeros(hashBytes.length)+6)/7];
Expand Down Expand Up @@ -141,7 +145,7 @@ public String readAddress(InputStream in) throws IOException {
case IPFS:
buf = new byte[sizeForAddress];
in.read(buf);
return new Multihash(buf).toBase58();
return Cid.cast(buf).toString();
case ONION:
byte[] host = new byte[10];
in.read(host);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.ipfs.multiaddr;
package io.ipfs.api;

import io.ipfs.multiaddr.*;
import org.junit.*;

import java.io.*;
Expand Down Expand Up @@ -66,6 +67,7 @@ public void succeeds() {
"/udp/65535",
"/tcp/65535",
"/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
"/ipfs/zdpuAnNyxJR8tigBDe5FM2PfMq1u7fKSCsTmb4m3iJZcCq8yB",
"/udp/1234/sctp/1234",
"/udp/1234/udt",
"/udp/1234/utp",
Expand Down

0 comments on commit 4ac125c

Please sign in to comment.