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 SocketAddress extends CommonBase {
16 private SocketAddress(Object _dummy, long ptr) { super(ptr); }
17 @Override @SuppressWarnings("deprecation")
18 protected void finalize() throws Throwable {
20 if (ptr != 0) { bindings.SocketAddress_free(ptr); }
22 static SocketAddress constr_from_ptr(long ptr) {
23 bindings.LDKSocketAddress raw_val = bindings.LDKSocketAddress_ref_from_ptr(ptr);
24 if (raw_val.getClass() == bindings.LDKSocketAddress.TcpIpV4.class) {
25 return new TcpIpV4(ptr, (bindings.LDKSocketAddress.TcpIpV4)raw_val);
27 if (raw_val.getClass() == bindings.LDKSocketAddress.TcpIpV6.class) {
28 return new TcpIpV6(ptr, (bindings.LDKSocketAddress.TcpIpV6)raw_val);
30 if (raw_val.getClass() == bindings.LDKSocketAddress.OnionV2.class) {
31 return new OnionV2(ptr, (bindings.LDKSocketAddress.OnionV2)raw_val);
33 if (raw_val.getClass() == bindings.LDKSocketAddress.OnionV3.class) {
34 return new OnionV3(ptr, (bindings.LDKSocketAddress.OnionV3)raw_val);
36 if (raw_val.getClass() == bindings.LDKSocketAddress.Hostname.class) {
37 return new Hostname(ptr, (bindings.LDKSocketAddress.Hostname)raw_val);
39 assert false; return null; // Unreachable without extending the (internal) bindings interface
43 * An IPv4 address and port on which the peer is listening.
45 public final static class TcpIpV4 extends SocketAddress {
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 TcpIpV4(long ptr, bindings.LDKSocketAddress.TcpIpV4 obj) {
61 * An IPv6 address and port on which the peer is listening.
63 public final static class TcpIpV6 extends SocketAddress {
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 TcpIpV6(long ptr, bindings.LDKSocketAddress.TcpIpV6 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 SocketAddress {
85 public final byte[] onion_v2;
86 private OnionV2(long ptr, bindings.LDKSocketAddress.OnionV2 obj) {
88 this.onion_v2 = obj.onion_v2;
92 * A new-style Tor onion address/port on which the peer is listening.
94 * To create the human-readable \"hostname\", concatenate the ED25519 pubkey, checksum, and version,
95 * wrap as base32 and append \".onion\".
97 public final static class OnionV3 extends SocketAddress {
99 * The ed25519 long-term public key of the peer
101 public final byte[] ed25519_pubkey;
103 * The checksum of the pubkey and version, as included in the onion address
105 public final short checksum;
107 * The version byte, as defined by the Tor Onion v3 spec.
109 public final byte version;
111 * The port on which the node is listening
113 public final short port;
114 private OnionV3(long ptr, bindings.LDKSocketAddress.OnionV3 obj) {
116 this.ed25519_pubkey = obj.ed25519_pubkey;
117 this.checksum = obj.checksum;
118 this.version = obj.version;
119 this.port = obj.port;
123 * A hostname/port on which the peer is listening.
125 public final static class Hostname extends SocketAddress {
127 * The hostname on which the node is listening.
129 public final org.ldk.structs.Hostname hostname;
131 * The port on which the node is listening.
133 public final short port;
134 private Hostname(long ptr, bindings.LDKSocketAddress.Hostname obj) {
136 long hostname = obj.hostname;
137 org.ldk.structs.Hostname hostname_hu_conv = null; if (hostname < 0 || hostname > 4096) { hostname_hu_conv = new org.ldk.structs.Hostname(null, hostname); }
138 if (hostname_hu_conv != null) { hostname_hu_conv.ptrs_to.add(this); };
139 this.hostname = hostname_hu_conv;
140 this.port = obj.port;
144 long ret = bindings.SocketAddress_clone_ptr(this.ptr);
145 Reference.reachabilityFence(this);
150 * Creates a copy of the SocketAddress
152 public SocketAddress clone() {
153 long ret = bindings.SocketAddress_clone(this.ptr);
154 Reference.reachabilityFence(this);
155 if (ret >= 0 && ret <= 4096) { return null; }
156 org.ldk.structs.SocketAddress ret_hu_conv = org.ldk.structs.SocketAddress.constr_from_ptr(ret);
157 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
162 * Utility method to constructs a new TcpIpV4-variant SocketAddress
164 public static SocketAddress tcp_ip_v4(byte[] addr, short port) {
165 long ret = bindings.SocketAddress_tcp_ip_v4(InternalUtils.check_arr_len(addr, 4), port);
166 Reference.reachabilityFence(addr);
167 Reference.reachabilityFence(port);
168 if (ret >= 0 && ret <= 4096) { return null; }
169 org.ldk.structs.SocketAddress ret_hu_conv = org.ldk.structs.SocketAddress.constr_from_ptr(ret);
170 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
175 * Utility method to constructs a new TcpIpV6-variant SocketAddress
177 public static SocketAddress tcp_ip_v6(byte[] addr, short port) {
178 long ret = bindings.SocketAddress_tcp_ip_v6(InternalUtils.check_arr_len(addr, 16), port);
179 Reference.reachabilityFence(addr);
180 Reference.reachabilityFence(port);
181 if (ret >= 0 && ret <= 4096) { return null; }
182 org.ldk.structs.SocketAddress ret_hu_conv = org.ldk.structs.SocketAddress.constr_from_ptr(ret);
183 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
188 * Utility method to constructs a new OnionV2-variant SocketAddress
190 public static SocketAddress onion_v2(byte[] a) {
191 long ret = bindings.SocketAddress_onion_v2(InternalUtils.check_arr_len(a, 12));
192 Reference.reachabilityFence(a);
193 if (ret >= 0 && ret <= 4096) { return null; }
194 org.ldk.structs.SocketAddress ret_hu_conv = org.ldk.structs.SocketAddress.constr_from_ptr(ret);
195 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
200 * Utility method to constructs a new OnionV3-variant SocketAddress
202 public static SocketAddress onion_v3(byte[] ed25519_pubkey, short checksum, byte version, short port) {
203 long ret = bindings.SocketAddress_onion_v3(InternalUtils.check_arr_len(ed25519_pubkey, 32), checksum, version, port);
204 Reference.reachabilityFence(ed25519_pubkey);
205 Reference.reachabilityFence(checksum);
206 Reference.reachabilityFence(version);
207 Reference.reachabilityFence(port);
208 if (ret >= 0 && ret <= 4096) { return null; }
209 org.ldk.structs.SocketAddress ret_hu_conv = org.ldk.structs.SocketAddress.constr_from_ptr(ret);
210 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
215 * Utility method to constructs a new Hostname-variant SocketAddress
217 public static SocketAddress hostname(org.ldk.structs.Hostname hostname, short port) {
218 long ret = bindings.SocketAddress_hostname(hostname == null ? 0 : hostname.ptr, port);
219 Reference.reachabilityFence(hostname);
220 Reference.reachabilityFence(port);
221 if (ret >= 0 && ret <= 4096) { return null; }
222 org.ldk.structs.SocketAddress ret_hu_conv = org.ldk.structs.SocketAddress.constr_from_ptr(ret);
223 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
224 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(hostname); };
229 * Checks if two SocketAddresss contain equal inner contents.
230 * This ignores pointers and is_owned flags and looks at the values in fields.
232 public boolean eq(org.ldk.structs.SocketAddress b) {
233 boolean ret = bindings.SocketAddress_eq(this.ptr, b == null ? 0 : b.ptr);
234 Reference.reachabilityFence(this);
235 Reference.reachabilityFence(b);
239 @Override public boolean equals(Object o) {
240 if (!(o instanceof SocketAddress)) return false;
241 return this.eq((SocketAddress)o);
244 * Serialize the SocketAddress object into a byte array which can be read by SocketAddress_read
246 public byte[] write() {
247 byte[] ret = bindings.SocketAddress_write(this.ptr);
248 Reference.reachabilityFence(this);
253 * Read a SocketAddress from a byte array, created by SocketAddress_write
255 public static Result_SocketAddressDecodeErrorZ read(byte[] ser) {
256 long ret = bindings.SocketAddress_read(ser);
257 Reference.reachabilityFence(ser);
258 if (ret >= 0 && ret <= 4096) { return null; }
259 Result_SocketAddressDecodeErrorZ ret_hu_conv = Result_SocketAddressDecodeErrorZ.constr_from_ptr(ret);
264 * Read a SocketAddress object from a string
266 public static Result_SocketAddressSocketAddressParseErrorZ from_str(java.lang.String s) {
267 long ret = bindings.SocketAddress_from_str(s);
268 Reference.reachabilityFence(s);
269 if (ret >= 0 && ret <= 4096) { return null; }
270 Result_SocketAddressSocketAddressParseErrorZ ret_hu_conv = Result_SocketAddressSocketAddressParseErrorZ.constr_from_ptr(ret);