Skip to content

Commit 3a6d654

Browse files
committed
Add missing protocols: dns4, dns6, dnsaddr, ws, p2p-circuit
1 parent 17e1d28 commit 3a6d654

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

src/main/java/io/ipfs/multiaddr/Protocol.java

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,20 @@ enum Type {
1616
UDP(17, 16, "udp"),
1717
DCCP(33, 16, "dccp"),
1818
IP6(41, 128, "ip6"),
19+
DNS4(54, LENGTH_PREFIXED_VAR_SIZE, "dns4"),
20+
DNS6(55, LENGTH_PREFIXED_VAR_SIZE, "dns6"),
21+
DNSADDR(56, LENGTH_PREFIXED_VAR_SIZE, "dnsaddr"),
1922
SCTP(132, 16, "sctp"),
2023
UTP(301, 0, "utp"),
2124
UDT(302, 0, "udt"),
2225
UNIX(400, LENGTH_PREFIXED_VAR_SIZE, "unix"),
2326
IPFS(421, LENGTH_PREFIXED_VAR_SIZE, "ipfs"),
24-
QUIC(460, 0, "quic"),
2527
HTTPS(443, 0, "https"),
26-
HTTP(480, 0, "http"),
27-
ONION(444, 80, "onion");
28+
ONION(444, 80, "onion"),
29+
QUIC(460, 0, "quic"),
30+
WS(477, 0, "ws"),
31+
P2PCIRCUIT(290, 0, "p2p-circuit"),
32+
HTTP(480, 0, "http");
2833

2934
public final int code, size;
3035
public final String name;
@@ -92,15 +97,16 @@ public byte[] addressToBytes(String addr) {
9297
if (x > 65535)
9398
throw new IllegalStateException("Failed to parse "+type.name+" address "+addr + " (> 65535");
9499
return new byte[]{(byte)(x >>8), (byte)x};
95-
case IPFS:
100+
case IPFS: {
96101
Multihash hash = Cid.decode(addr);
97102
ByteArrayOutputStream bout = new ByteArrayOutputStream();
98103
byte[] hashBytes = hash.toBytes();
99-
byte[] varint = new byte[(32 - Integer.numberOfLeadingZeros(hashBytes.length)+6)/7];
104+
byte[] varint = new byte[(32 - Integer.numberOfLeadingZeros(hashBytes.length) + 6) / 7];
100105
putUvarint(varint, hashBytes.length);
101106
bout.write(varint);
102107
bout.write(hashBytes);
103108
return bout.toByteArray();
109+
}
104110
case ONION: {
105111
String[] split = addr.split(":");
106112
if (split.length != 2)
@@ -138,11 +144,23 @@ public byte[] addressToBytes(String addr) {
138144
dout.flush();
139145
return b.toByteArray();
140146
}
147+
case DNS4:
148+
case DNS6:
149+
case DNSADDR: {
150+
ByteArrayOutputStream bout = new ByteArrayOutputStream();
151+
byte[] hashBytes = addr.getBytes();
152+
byte[] varint = new byte[(32 - Integer.numberOfLeadingZeros(hashBytes.length) + 6) / 7];
153+
putUvarint(varint, hashBytes.length);
154+
bout.write(varint);
155+
bout.write(hashBytes);
156+
return bout.toByteArray();
157+
}
158+
default:
159+
throw new IllegalStateException("Unknown multiaddr type: " + type);
141160
}
142161
} catch (IOException e) {
143162
throw new RuntimeException(e);
144163
}
145-
throw new IllegalStateException("Failed to parse address: "+addr);
146164
}
147165

148166
public String readAddress(InputStream in) throws IOException {
@@ -175,6 +193,12 @@ public String readAddress(InputStream in) throws IOException {
175193
buf = new byte[sizeForAddress];
176194
read(in, buf);
177195
return new String(buf);
196+
case DNS4:
197+
case DNS6:
198+
case DNSADDR:
199+
buf = new byte[sizeForAddress];
200+
read(in, buf);
201+
return new String(buf);
178202
}
179203
throw new IllegalStateException("Unimplemented protocol type: "+type.name);
180204
}

src/test/java/io/ipfs/api/MultiAddressTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,21 @@ public void succeeds() {
6565
"/sctp/0",
6666
"/udp/1234",
6767
"/tcp/1234",
68+
"/dns4/ipfs.io",
69+
"/dns6/ipfs.io",
70+
"/dnsaddr/ipfs.io",
6871
"/sctp/1234",
6972
"/udp/65535",
7073
"/tcp/65535",
7174
"/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
7275
"/ipfs/zdpuAnNyxJR8tigBDe5FM2PfMq1u7fKSCsTmb4m3iJZcCq8yB",
76+
"/ipfs/zdpuAnNyxJR8tigBDe5FM2PfMq1u7fKSCsTmb4m3iJZcCq8yB/p2p-circuit",
7377
"/udp/1234/sctp/1234",
7478
"/udp/1234/udt",
7579
"/udp/1234/utp",
7680
"/tcp/1234/http",
7781
"/tcp/1234/https",
82+
"/tcp/1234/ws",
7883
"/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234",
7984
"/ip4/127.0.0.1/udp/1234",
8085
"/ip4/127.0.0.1/udp/0",

0 commit comments

Comments
 (0)