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 if (raw_val.getClass() == bindings.LDKNetAddress.Hostname.class) {
37 return new Hostname(ptr, (bindings.LDKNetAddress.Hostname)raw_val);
39 assert false; return null; // Unreachable without extending the (internal) bindings interface
43 * An IPv4 address/port on which the peer is listening.
45 public final static class IPv4 extends NetAddress {
47 * The 4-byte IPv4 address
49 public final byte[] addr;
51 * The port on which the node is listening
53 public final short port;
54 private IPv4(long ptr, bindings.LDKNetAddress.IPv4 obj) {
61 * An IPv6 address/port on which the peer is listening.
63 public final static class IPv6 extends NetAddress {
65 * The 16-byte IPv6 address
67 public final byte[] addr;
69 * The port on which the node is listening
71 public final short port;
72 private IPv6(long ptr, bindings.LDKNetAddress.IPv6 obj) {
79 * An old-style Tor onion address/port on which the peer is listening.
81 * This field is deprecated and the Tor network generally no longer supports V2 Onion
82 * addresses. Thus, the details are not parsed here.
84 public final static class OnionV2 extends NetAddress {
85 public final byte[] onion_v2;
86 private OnionV2(long ptr, bindings.LDKNetAddress.OnionV2 obj) {
88 this.onion_v2 = obj.onion_v2;
92 * A new-style Tor onion address/port on which the peer is listening.
93 * To create the human-readable \"hostname\", concatenate ed25519_pubkey, checksum, and version,
94 * wrap as base32 and append \".onion\".
96 public final static class OnionV3 extends NetAddress {
98 * The ed25519 long-term public key of the peer
100 public final byte[] ed25519_pubkey;
102 * The checksum of the pubkey and version, as included in the onion address
104 public final short checksum;
106 * The version byte, as defined by the Tor Onion v3 spec.
108 public final byte version;
110 * The port on which the node is listening
112 public final short port;
113 private OnionV3(long ptr, bindings.LDKNetAddress.OnionV3 obj) {
115 this.ed25519_pubkey = obj.ed25519_pubkey;
116 this.checksum = obj.checksum;
117 this.version = obj.version;
118 this.port = obj.port;
122 * A hostname/port on which the peer is listening.
124 public final static class Hostname extends NetAddress {
126 * The hostname on which the node is listening.
128 public final org.ldk.structs.Hostname hostname;
130 * The port on which the node is listening.
132 public final short port;
133 private Hostname(long ptr, bindings.LDKNetAddress.Hostname obj) {
135 long hostname = obj.hostname;
136 org.ldk.structs.Hostname hostname_hu_conv = null; if (hostname < 0 || hostname > 4096) { hostname_hu_conv = new org.ldk.structs.Hostname(null, hostname); }
137 if (hostname_hu_conv != null) { hostname_hu_conv.ptrs_to.add(this); };
138 this.hostname = hostname_hu_conv;
139 this.port = obj.port;
143 long ret = bindings.NetAddress_clone_ptr(this.ptr);
144 Reference.reachabilityFence(this);
149 * Creates a copy of the NetAddress
151 public NetAddress clone() {
152 long ret = bindings.NetAddress_clone(this.ptr);
153 Reference.reachabilityFence(this);
154 if (ret >= 0 && ret <= 4096) { return null; }
155 org.ldk.structs.NetAddress ret_hu_conv = org.ldk.structs.NetAddress.constr_from_ptr(ret);
156 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
161 * Utility method to constructs a new IPv4-variant NetAddress
163 public static NetAddress ipv4(byte[] addr, short port) {
164 long ret = bindings.NetAddress_ipv4(InternalUtils.check_arr_len(addr, 4), port);
165 Reference.reachabilityFence(addr);
166 Reference.reachabilityFence(port);
167 if (ret >= 0 && ret <= 4096) { return null; }
168 org.ldk.structs.NetAddress ret_hu_conv = org.ldk.structs.NetAddress.constr_from_ptr(ret);
169 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
174 * Utility method to constructs a new IPv6-variant NetAddress
176 public static NetAddress ipv6(byte[] addr, short port) {
177 long ret = bindings.NetAddress_ipv6(InternalUtils.check_arr_len(addr, 16), port);
178 Reference.reachabilityFence(addr);
179 Reference.reachabilityFence(port);
180 if (ret >= 0 && ret <= 4096) { return null; }
181 org.ldk.structs.NetAddress ret_hu_conv = org.ldk.structs.NetAddress.constr_from_ptr(ret);
182 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
187 * Utility method to constructs a new OnionV2-variant NetAddress
189 public static NetAddress onion_v2(byte[] a) {
190 long ret = bindings.NetAddress_onion_v2(InternalUtils.check_arr_len(a, 12));
191 Reference.reachabilityFence(a);
192 if (ret >= 0 && ret <= 4096) { return null; }
193 org.ldk.structs.NetAddress ret_hu_conv = org.ldk.structs.NetAddress.constr_from_ptr(ret);
194 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
199 * Utility method to constructs a new OnionV3-variant NetAddress
201 public static NetAddress onion_v3(byte[] ed25519_pubkey, short checksum, byte version, short port) {
202 long ret = bindings.NetAddress_onion_v3(InternalUtils.check_arr_len(ed25519_pubkey, 32), checksum, version, port);
203 Reference.reachabilityFence(ed25519_pubkey);
204 Reference.reachabilityFence(checksum);
205 Reference.reachabilityFence(version);
206 Reference.reachabilityFence(port);
207 if (ret >= 0 && ret <= 4096) { return null; }
208 org.ldk.structs.NetAddress ret_hu_conv = org.ldk.structs.NetAddress.constr_from_ptr(ret);
209 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
214 * Utility method to constructs a new Hostname-variant NetAddress
216 public static NetAddress hostname(Hostname hostname, short port) {
217 long ret = bindings.NetAddress_hostname(hostname == null ? 0 : hostname.ptr, port);
218 Reference.reachabilityFence(hostname);
219 Reference.reachabilityFence(port);
220 if (ret >= 0 && ret <= 4096) { return null; }
221 org.ldk.structs.NetAddress ret_hu_conv = org.ldk.structs.NetAddress.constr_from_ptr(ret);
222 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
223 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(hostname); };
228 * Checks if two NetAddresss contain equal inner contents.
229 * This ignores pointers and is_owned flags and looks at the values in fields.
231 public boolean eq(NetAddress b) {
232 boolean ret = bindings.NetAddress_eq(this.ptr, b == null ? 0 : b.ptr);
233 Reference.reachabilityFence(this);
234 Reference.reachabilityFence(b);
238 @Override public boolean equals(Object o) {
239 if (!(o instanceof NetAddress)) return false;
240 return this.eq((NetAddress)o);
243 * Serialize the NetAddress object into a byte array which can be read by NetAddress_read
245 public byte[] write() {
246 byte[] ret = bindings.NetAddress_write(this.ptr);
247 Reference.reachabilityFence(this);
252 * Read a NetAddress from a byte array, created by NetAddress_write
254 public static Result_NetAddressDecodeErrorZ read(byte[] ser) {
255 long ret = bindings.NetAddress_read(ser);
256 Reference.reachabilityFence(ser);
257 if (ret >= 0 && ret <= 4096) { return null; }
258 Result_NetAddressDecodeErrorZ ret_hu_conv = Result_NetAddressDecodeErrorZ.constr_from_ptr(ret);