1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import javax.annotation.Nullable;
11 * An address which can be used to connect to a remote peer
13 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
14 public class NetAddress extends CommonBase {
15 private NetAddress(Object _dummy, long ptr) { super(ptr); }
16 @Override @SuppressWarnings("deprecation")
17 protected void finalize() throws Throwable {
19 if (ptr != 0) { bindings.NetAddress_free(ptr); }
21 static NetAddress constr_from_ptr(long ptr) {
22 bindings.LDKNetAddress raw_val = bindings.LDKNetAddress_ref_from_ptr(ptr);
23 if (raw_val.getClass() == bindings.LDKNetAddress.IPv4.class) {
24 return new IPv4(ptr, (bindings.LDKNetAddress.IPv4)raw_val);
26 if (raw_val.getClass() == bindings.LDKNetAddress.IPv6.class) {
27 return new IPv6(ptr, (bindings.LDKNetAddress.IPv6)raw_val);
29 if (raw_val.getClass() == bindings.LDKNetAddress.OnionV2.class) {
30 return new OnionV2(ptr, (bindings.LDKNetAddress.OnionV2)raw_val);
32 if (raw_val.getClass() == bindings.LDKNetAddress.OnionV3.class) {
33 return new OnionV3(ptr, (bindings.LDKNetAddress.OnionV3)raw_val);
35 assert false; return null; // Unreachable without extending the (internal) bindings interface
38 public final static class IPv4 extends NetAddress {
40 * The 4-byte IPv4 address
42 public final byte[] addr;
44 * The port on which the node is listening
46 public final short port;
47 private IPv4(long ptr, bindings.LDKNetAddress.IPv4 obj) {
53 public final static class IPv6 extends NetAddress {
55 * The 16-byte IPv6 address
57 public final byte[] addr;
59 * The port on which the node is listening
61 public final short port;
62 private IPv6(long ptr, bindings.LDKNetAddress.IPv6 obj) {
68 public final static class OnionV2 extends NetAddress {
70 * The bytes (usually encoded in base32 with \".onion\" appended)
72 public final byte[] addr;
74 * The port on which the node is listening
76 public final short port;
77 private OnionV2(long ptr, bindings.LDKNetAddress.OnionV2 obj) {
83 public final static class OnionV3 extends NetAddress {
85 * The ed25519 long-term public key of the peer
87 public final byte[] ed25519_pubkey;
89 * The checksum of the pubkey and version, as included in the onion address
91 public final short checksum;
93 * The version byte, as defined by the Tor Onion v3 spec.
95 public final byte version;
97 * The port on which the node is listening
99 public final short port;
100 private OnionV3(long ptr, bindings.LDKNetAddress.OnionV3 obj) {
102 this.ed25519_pubkey = obj.ed25519_pubkey;
103 this.checksum = obj.checksum;
104 this.version = obj.version;
105 this.port = obj.port;
109 * Creates a copy of the NetAddress
111 public NetAddress clone() {
112 long ret = bindings.NetAddress_clone(this.ptr);
113 if (ret < 1024) { return null; }
114 NetAddress ret_hu_conv = NetAddress.constr_from_ptr(ret);
115 ret_hu_conv.ptrs_to.add(this);
120 * Utility method to constructs a new IPv4-variant NetAddress
122 public static NetAddress ipv4(byte[] addr, short port) {
123 long ret = bindings.NetAddress_ipv4(addr, port);
124 if (ret < 1024) { return null; }
125 NetAddress ret_hu_conv = NetAddress.constr_from_ptr(ret);
126 ret_hu_conv.ptrs_to.add(ret_hu_conv);
131 * Utility method to constructs a new IPv6-variant NetAddress
133 public static NetAddress ipv6(byte[] addr, short port) {
134 long ret = bindings.NetAddress_ipv6(addr, port);
135 if (ret < 1024) { return null; }
136 NetAddress ret_hu_conv = NetAddress.constr_from_ptr(ret);
137 ret_hu_conv.ptrs_to.add(ret_hu_conv);
142 * Utility method to constructs a new OnionV2-variant NetAddress
144 public static NetAddress onion_v2(byte[] addr, short port) {
145 long ret = bindings.NetAddress_onion_v2(addr, port);
146 if (ret < 1024) { return null; }
147 NetAddress ret_hu_conv = NetAddress.constr_from_ptr(ret);
148 ret_hu_conv.ptrs_to.add(ret_hu_conv);
153 * Utility method to constructs a new OnionV3-variant NetAddress
155 public static NetAddress onion_v3(byte[] ed25519_pubkey, short checksum, byte version, short port) {
156 long ret = bindings.NetAddress_onion_v3(ed25519_pubkey, checksum, version, port);
157 if (ret < 1024) { return null; }
158 NetAddress ret_hu_conv = NetAddress.constr_from_ptr(ret);
159 ret_hu_conv.ptrs_to.add(ret_hu_conv);
164 * Serialize the NetAddress object into a byte array which can be read by NetAddress_read
166 public byte[] write() {
167 byte[] ret = bindings.NetAddress_write(this.ptr);
172 * Read a NetAddress from a byte array, created by NetAddress_write
174 public static Result_NetAddressDecodeErrorZ read(byte[] ser) {
175 long ret = bindings.NetAddress_read(ser);
176 if (ret < 1024) { return null; }
177 Result_NetAddressDecodeErrorZ ret_hu_conv = Result_NetAddressDecodeErrorZ.constr_from_ptr(ret);