1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import java.lang.ref.Reference;
8 import javax.annotation.Nullable;
12 * An address which can be used to connect to a remote peer
14 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
15 public class NetAddress extends CommonBase {
16 private NetAddress(Object _dummy, long ptr) { super(ptr); }
17 @Override @SuppressWarnings("deprecation")
18 protected void finalize() throws Throwable {
20 if (ptr != 0) { bindings.NetAddress_free(ptr); }
22 static NetAddress constr_from_ptr(long ptr) {
23 bindings.LDKNetAddress raw_val = bindings.LDKNetAddress_ref_from_ptr(ptr);
24 if (raw_val.getClass() == bindings.LDKNetAddress.IPv4.class) {
25 return new IPv4(ptr, (bindings.LDKNetAddress.IPv4)raw_val);
27 if (raw_val.getClass() == bindings.LDKNetAddress.IPv6.class) {
28 return new IPv6(ptr, (bindings.LDKNetAddress.IPv6)raw_val);
30 if (raw_val.getClass() == bindings.LDKNetAddress.OnionV2.class) {
31 return new OnionV2(ptr, (bindings.LDKNetAddress.OnionV2)raw_val);
33 if (raw_val.getClass() == bindings.LDKNetAddress.OnionV3.class) {
34 return new OnionV3(ptr, (bindings.LDKNetAddress.OnionV3)raw_val);
36 assert false; return null; // Unreachable without extending the (internal) bindings interface
40 * An IPv4 address/port on which the peer is listening.
42 public final static class IPv4 extends NetAddress {
44 * The 4-byte IPv4 address
46 public final byte[] addr;
48 * The port on which the node is listening
50 public final short port;
51 private IPv4(long ptr, bindings.LDKNetAddress.IPv4 obj) {
58 * An IPv6 address/port on which the peer is listening.
60 public final static class IPv6 extends NetAddress {
62 * The 16-byte IPv6 address
64 public final byte[] addr;
66 * The port on which the node is listening
68 public final short port;
69 private IPv6(long ptr, bindings.LDKNetAddress.IPv6 obj) {
76 * An old-style Tor onion address/port on which the peer is listening.
78 * This field is deprecated and the Tor network generally no longer supports V2 Onion
79 * addresses. Thus, the details are not parsed here.
81 public final static class OnionV2 extends NetAddress {
82 public final byte[] onion_v2;
83 private OnionV2(long ptr, bindings.LDKNetAddress.OnionV2 obj) {
85 this.onion_v2 = obj.onion_v2;
89 * A new-style Tor onion address/port on which the peer is listening.
90 * To create the human-readable \"hostname\", concatenate ed25519_pubkey, checksum, and version,
91 * wrap as base32 and append \".onion\".
93 public final static class OnionV3 extends NetAddress {
95 * The ed25519 long-term public key of the peer
97 public final byte[] ed25519_pubkey;
99 * The checksum of the pubkey and version, as included in the onion address
101 public final short checksum;
103 * The version byte, as defined by the Tor Onion v3 spec.
105 public final byte version;
107 * The port on which the node is listening
109 public final short port;
110 private OnionV3(long ptr, bindings.LDKNetAddress.OnionV3 obj) {
112 this.ed25519_pubkey = obj.ed25519_pubkey;
113 this.checksum = obj.checksum;
114 this.version = obj.version;
115 this.port = obj.port;
119 long ret = bindings.NetAddress_clone_ptr(this.ptr);
120 Reference.reachabilityFence(this);
125 * Creates a copy of the NetAddress
127 public NetAddress clone() {
128 long ret = bindings.NetAddress_clone(this.ptr);
129 Reference.reachabilityFence(this);
130 if (ret >= 0 && ret <= 4096) { return null; }
131 org.ldk.structs.NetAddress ret_hu_conv = org.ldk.structs.NetAddress.constr_from_ptr(ret);
132 ret_hu_conv.ptrs_to.add(this);
137 * Utility method to constructs a new IPv4-variant NetAddress
139 public static NetAddress ipv4(byte[] addr, short port) {
140 long ret = bindings.NetAddress_ipv4(InternalUtils.check_arr_len(addr, 4), port);
141 Reference.reachabilityFence(addr);
142 Reference.reachabilityFence(port);
143 if (ret >= 0 && ret <= 4096) { return null; }
144 org.ldk.structs.NetAddress ret_hu_conv = org.ldk.structs.NetAddress.constr_from_ptr(ret);
145 ret_hu_conv.ptrs_to.add(ret_hu_conv);
150 * Utility method to constructs a new IPv6-variant NetAddress
152 public static NetAddress ipv6(byte[] addr, short port) {
153 long ret = bindings.NetAddress_ipv6(InternalUtils.check_arr_len(addr, 16), port);
154 Reference.reachabilityFence(addr);
155 Reference.reachabilityFence(port);
156 if (ret >= 0 && ret <= 4096) { return null; }
157 org.ldk.structs.NetAddress ret_hu_conv = org.ldk.structs.NetAddress.constr_from_ptr(ret);
158 ret_hu_conv.ptrs_to.add(ret_hu_conv);
163 * Utility method to constructs a new OnionV2-variant NetAddress
165 public static NetAddress onion_v2(byte[] a) {
166 long ret = bindings.NetAddress_onion_v2(InternalUtils.check_arr_len(a, 12));
167 Reference.reachabilityFence(a);
168 if (ret >= 0 && ret <= 4096) { return null; }
169 org.ldk.structs.NetAddress ret_hu_conv = org.ldk.structs.NetAddress.constr_from_ptr(ret);
170 ret_hu_conv.ptrs_to.add(ret_hu_conv);
175 * Utility method to constructs a new OnionV3-variant NetAddress
177 public static NetAddress onion_v3(byte[] ed25519_pubkey, short checksum, byte version, short port) {
178 long ret = bindings.NetAddress_onion_v3(InternalUtils.check_arr_len(ed25519_pubkey, 32), checksum, version, port);
179 Reference.reachabilityFence(ed25519_pubkey);
180 Reference.reachabilityFence(checksum);
181 Reference.reachabilityFence(version);
182 Reference.reachabilityFence(port);
183 if (ret >= 0 && ret <= 4096) { return null; }
184 org.ldk.structs.NetAddress ret_hu_conv = org.ldk.structs.NetAddress.constr_from_ptr(ret);
185 ret_hu_conv.ptrs_to.add(ret_hu_conv);
190 * Serialize the NetAddress object into a byte array which can be read by NetAddress_read
192 public byte[] write() {
193 byte[] ret = bindings.NetAddress_write(this.ptr);
194 Reference.reachabilityFence(this);
199 * Read a NetAddress from a byte array, created by NetAddress_write
201 public static Result_NetAddressDecodeErrorZ read(byte[] ser) {
202 long ret = bindings.NetAddress_read(ser);
203 Reference.reachabilityFence(ser);
204 if (ret >= 0 && ret <= 4096) { return null; }
205 Result_NetAddressDecodeErrorZ ret_hu_conv = Result_NetAddressDecodeErrorZ.constr_from_ptr(ret);