|
| 1 | +package org.ipfs; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import java.net.*; |
| 5 | +import java.util.*; |
| 6 | + |
| 7 | +public class Protocol { |
| 8 | + public static int LENGTH_PREFIXED_VAR_SIZE = -1; |
| 9 | + |
| 10 | + enum Type { |
| 11 | + IP4(4, 32, "ip4"), |
| 12 | + TCP(6, 16, "tcp"), |
| 13 | + UDP(17, 16, "udp"), |
| 14 | + DCCP(33, 16, "dccp"), |
| 15 | + IP6(41, 128, "ip6"), |
| 16 | + SCTP(132, 16, "sctp"), |
| 17 | + UTP(301, 0, "utp"), |
| 18 | + UDT(302, 0, "udt"), |
| 19 | + IPFS(421, LENGTH_PREFIXED_VAR_SIZE, "ipfs"), |
| 20 | + HTTPS(443, 0, "https"), |
| 21 | + HTTP(480, 0, "http"); |
| 22 | + |
| 23 | + public final int code, size; |
| 24 | + public final String name; |
| 25 | + private final byte[] encoded; |
| 26 | + |
| 27 | + Type(int code, int size, String name) { |
| 28 | + this.code = code; |
| 29 | + this.size = size; |
| 30 | + this.name = name; |
| 31 | + this.encoded = encode(code); |
| 32 | + } |
| 33 | + |
| 34 | + static byte[] encode(int code) { |
| 35 | + byte[] varint = new byte[(32 - Integer.numberOfLeadingZeros(code)+6)/7]; |
| 36 | + putUvarint(varint, code); |
| 37 | + return varint; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public final Type type; |
| 42 | + |
| 43 | + public Protocol(Type type) { |
| 44 | + this.type = type; |
| 45 | + } |
| 46 | + |
| 47 | + public void appendCode(OutputStream out) throws IOException { |
| 48 | + out.write(type.encoded); |
| 49 | + } |
| 50 | + |
| 51 | + public int size() { |
| 52 | + return type.size; |
| 53 | + } |
| 54 | + |
| 55 | + public String name() { |
| 56 | + return type.name; |
| 57 | + } |
| 58 | + |
| 59 | + public int code() { |
| 60 | + return type.code; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public String toString() { |
| 65 | + return name(); |
| 66 | + } |
| 67 | + |
| 68 | + public byte[] addressToBytes(String addr) { |
| 69 | + try { |
| 70 | + switch (type) { |
| 71 | + case IP4: |
| 72 | + return Inet4Address.getByName(addr).getAddress(); |
| 73 | + case IP6: |
| 74 | + return Inet6Address.getByName(addr).getAddress(); |
| 75 | + case TCP: |
| 76 | + case UDP: |
| 77 | + case DCCP: |
| 78 | + case SCTP: |
| 79 | + int x = Integer.parseInt(addr); |
| 80 | + if (x > 65536) |
| 81 | + throw new IllegalStateException("Failed to parse "+type.name+" address "+addr + " (> 65536"); |
| 82 | + return new byte[]{(byte)(x >>8), (byte)x}; |
| 83 | + case IPFS: |
| 84 | + Multihash hash = Multihash.fromBase58(addr); |
| 85 | + ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
| 86 | + byte[] hashBytes = hash.toBytes(); |
| 87 | + byte[] varint = new byte[(32 - Integer.numberOfLeadingZeros(hashBytes.length)+6)/7]; |
| 88 | + putUvarint(varint, hashBytes.length); |
| 89 | + bout.write(varint); |
| 90 | + bout.write(hashBytes); |
| 91 | + return bout.toByteArray(); |
| 92 | + } |
| 93 | + } catch (IOException e) { |
| 94 | + throw new RuntimeException(e); |
| 95 | + } |
| 96 | + throw new IllegalStateException("Failed to parse address: "+addr); |
| 97 | + } |
| 98 | + |
| 99 | + public String readAddress(InputStream in) throws IOException { |
| 100 | + int sizeForAddress = sizeForAddress(in); |
| 101 | + byte[] buf; |
| 102 | + switch (type) { |
| 103 | + case IP4: |
| 104 | + buf = new byte[sizeForAddress]; |
| 105 | + in.read(buf); |
| 106 | + return Inet4Address.getByAddress(buf).toString().substring(1); |
| 107 | + case IP6: |
| 108 | + buf = new byte[sizeForAddress]; |
| 109 | + in.read(buf); |
| 110 | + return Inet6Address.getByAddress(buf).toString().substring(1); |
| 111 | + case TCP: |
| 112 | + case UDP: |
| 113 | + case DCCP: |
| 114 | + case SCTP: |
| 115 | + return Integer.toString((in.read() << 8) | (in.read())); |
| 116 | + case IPFS: |
| 117 | + buf = new byte[sizeForAddress]; |
| 118 | + in.read(buf); |
| 119 | + return new Multihash(buf).toBase58(); |
| 120 | + } |
| 121 | + throw new IllegalStateException("Unimplemented protocl type: "+type.name); |
| 122 | + } |
| 123 | + |
| 124 | + public int sizeForAddress(InputStream in) throws IOException { |
| 125 | + if (type.size > 0) |
| 126 | + return type.size/8; |
| 127 | + if (type.size == 0) |
| 128 | + return 0; |
| 129 | + return (int)readVarint(in); |
| 130 | + } |
| 131 | + |
| 132 | + static int putUvarint(byte[] buf, long x) { |
| 133 | + int i = 0; |
| 134 | + while (x >= 0x80) { |
| 135 | + buf[i] = (byte)(x | 0x80); |
| 136 | + x >>= 7; |
| 137 | + i++; |
| 138 | + } |
| 139 | + buf[i] = (byte)x; |
| 140 | + return i + 1; |
| 141 | + } |
| 142 | + |
| 143 | + static long readVarint(InputStream in) throws IOException { |
| 144 | + long x = 0; |
| 145 | + int s=0; |
| 146 | + for (int i=0; i < 10; i++) { |
| 147 | + int b = in.read(); |
| 148 | + if (b == -1) |
| 149 | + throw new EOFException(); |
| 150 | + if (b < 0x80) { |
| 151 | + if (i > 9 || i == 9 && b > 1) { |
| 152 | + throw new IllegalStateException("Overflow reading varint" +(-(i + 1))); |
| 153 | + } |
| 154 | + return x | (((long)b) << s); |
| 155 | + } |
| 156 | + x |= ((long)b & 0x7f) << s; |
| 157 | + s += 7; |
| 158 | + } |
| 159 | + throw new IllegalStateException("Varint too long!"); |
| 160 | + } |
| 161 | + |
| 162 | + private static Map<String, Protocol> byName = new HashMap<>(); |
| 163 | + private static Map<Integer, Protocol> byCode = new HashMap<>(); |
| 164 | + |
| 165 | + static { |
| 166 | + for (Protocol.Type t: Protocol.Type.values()) { |
| 167 | + Protocol p = new Protocol(t); |
| 168 | + byName.put(p.name(), p); |
| 169 | + byCode.put(p.code(), p); |
| 170 | + } |
| 171 | + |
| 172 | + } |
| 173 | + |
| 174 | + public static Protocol get(String name) { |
| 175 | + if (byName.containsKey(name)) |
| 176 | + return byName.get(name); |
| 177 | + throw new IllegalStateException("No protocol with name: "+name); |
| 178 | + } |
| 179 | + |
| 180 | + public static Protocol get(int code) { |
| 181 | + if (byCode.containsKey(code)) |
| 182 | + return byCode.get(code); |
| 183 | + throw new IllegalStateException("No protocol with code: "+code); |
| 184 | + } |
| 185 | +} |
0 commit comments