Merge pull request #56 from TheBlueMatt/main
[ldk-java] / src / main / java / org / ldk / structs / NetAddress.java
1 package org.ldk.structs;
2
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
5 import org.ldk.util.*;
6 import java.util.Arrays;
7 import javax.annotation.Nullable;
8
9
10 /**
11  * An address which can be used to connect to a remote peer
12  */
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 {
18                 super.finalize();
19                 if (ptr != 0) { bindings.NetAddress_free(ptr); }
20         }
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);
25                 }
26                 if (raw_val.getClass() == bindings.LDKNetAddress.IPv6.class) {
27                         return new IPv6(ptr, (bindings.LDKNetAddress.IPv6)raw_val);
28                 }
29                 if (raw_val.getClass() == bindings.LDKNetAddress.OnionV2.class) {
30                         return new OnionV2(ptr, (bindings.LDKNetAddress.OnionV2)raw_val);
31                 }
32                 if (raw_val.getClass() == bindings.LDKNetAddress.OnionV3.class) {
33                         return new OnionV3(ptr, (bindings.LDKNetAddress.OnionV3)raw_val);
34                 }
35                 assert false; return null; // Unreachable without extending the (internal) bindings interface
36         }
37
38         /**
39          * An IPv4 address/port on which the peer is listening.
40          */
41         public final static class IPv4 extends NetAddress {
42                 /**
43                  * The 4-byte IPv4 address
44                 */
45                 public final byte[] addr;
46                 /**
47                  * The port on which the node is listening
48                 */
49                 public final short port;
50                 private IPv4(long ptr, bindings.LDKNetAddress.IPv4 obj) {
51                         super(null, ptr);
52                         this.addr = obj.addr;
53                         this.port = obj.port;
54                 }
55         }
56         /**
57          * An IPv6 address/port on which the peer is listening.
58          */
59         public final static class IPv6 extends NetAddress {
60                 /**
61                  * The 16-byte IPv6 address
62                 */
63                 public final byte[] addr;
64                 /**
65                  * The port on which the node is listening
66                 */
67                 public final short port;
68                 private IPv6(long ptr, bindings.LDKNetAddress.IPv6 obj) {
69                         super(null, ptr);
70                         this.addr = obj.addr;
71                         this.port = obj.port;
72                 }
73         }
74         /**
75          * An old-style Tor onion address/port on which the peer is listening.
76          */
77         public final static class OnionV2 extends NetAddress {
78                 /**
79                  * The bytes (usually encoded in base32 with \".onion\" appended)
80                 */
81                 public final byte[] addr;
82                 /**
83                  * The port on which the node is listening
84                 */
85                 public final short port;
86                 private OnionV2(long ptr, bindings.LDKNetAddress.OnionV2 obj) {
87                         super(null, ptr);
88                         this.addr = obj.addr;
89                         this.port = obj.port;
90                 }
91         }
92         /**
93          * A new-style Tor onion address/port on which the peer is listening.
94          * To create the human-readable \"hostname\", concatenate ed25519_pubkey, checksum, and version,
95          * wrap as base32 and append \".onion\".
96          */
97         public final static class OnionV3 extends NetAddress {
98                 /**
99                  * The ed25519 long-term public key of the peer
100                 */
101                 public final byte[] ed25519_pubkey;
102                 /**
103                  * The checksum of the pubkey and version, as included in the onion address
104                 */
105                 public final short checksum;
106                 /**
107                  * The version byte, as defined by the Tor Onion v3 spec.
108                 */
109                 public final byte version;
110                 /**
111                  * The port on which the node is listening
112                 */
113                 public final short port;
114                 private OnionV3(long ptr, bindings.LDKNetAddress.OnionV3 obj) {
115                         super(null, ptr);
116                         this.ed25519_pubkey = obj.ed25519_pubkey;
117                         this.checksum = obj.checksum;
118                         this.version = obj.version;
119                         this.port = obj.port;
120                 }
121         }
122         long clone_ptr() {
123                 long ret = bindings.NetAddress_clone_ptr(this.ptr);
124                 return ret;
125         }
126
127         /**
128          * Creates a copy of the NetAddress
129          */
130         public NetAddress clone() {
131                 long ret = bindings.NetAddress_clone(this.ptr);
132                 if (ret >= 0 && ret <= 4096) { return null; }
133                 NetAddress ret_hu_conv = NetAddress.constr_from_ptr(ret);
134                 ret_hu_conv.ptrs_to.add(this);
135                 return ret_hu_conv;
136         }
137
138         /**
139          * Utility method to constructs a new IPv4-variant NetAddress
140          */
141         public static NetAddress ipv4(byte[] addr, short port) {
142                 long ret = bindings.NetAddress_ipv4(InternalUtils.check_arr_len(addr, 4), port);
143                 if (ret >= 0 && ret <= 4096) { return null; }
144                 NetAddress ret_hu_conv = NetAddress.constr_from_ptr(ret);
145                 ret_hu_conv.ptrs_to.add(ret_hu_conv);
146                 return ret_hu_conv;
147         }
148
149         /**
150          * Utility method to constructs a new IPv6-variant NetAddress
151          */
152         public static NetAddress ipv6(byte[] addr, short port) {
153                 long ret = bindings.NetAddress_ipv6(InternalUtils.check_arr_len(addr, 16), port);
154                 if (ret >= 0 && ret <= 4096) { return null; }
155                 NetAddress ret_hu_conv = NetAddress.constr_from_ptr(ret);
156                 ret_hu_conv.ptrs_to.add(ret_hu_conv);
157                 return ret_hu_conv;
158         }
159
160         /**
161          * Utility method to constructs a new OnionV2-variant NetAddress
162          */
163         public static NetAddress onion_v2(byte[] addr, short port) {
164                 long ret = bindings.NetAddress_onion_v2(InternalUtils.check_arr_len(addr, 10), port);
165                 if (ret >= 0 && ret <= 4096) { return null; }
166                 NetAddress ret_hu_conv = NetAddress.constr_from_ptr(ret);
167                 ret_hu_conv.ptrs_to.add(ret_hu_conv);
168                 return ret_hu_conv;
169         }
170
171         /**
172          * Utility method to constructs a new OnionV3-variant NetAddress
173          */
174         public static NetAddress onion_v3(byte[] ed25519_pubkey, short checksum, byte version, short port) {
175                 long ret = bindings.NetAddress_onion_v3(InternalUtils.check_arr_len(ed25519_pubkey, 32), checksum, version, port);
176                 if (ret >= 0 && ret <= 4096) { return null; }
177                 NetAddress ret_hu_conv = NetAddress.constr_from_ptr(ret);
178                 ret_hu_conv.ptrs_to.add(ret_hu_conv);
179                 return ret_hu_conv;
180         }
181
182         /**
183          * Serialize the NetAddress object into a byte array which can be read by NetAddress_read
184          */
185         public byte[] write() {
186                 byte[] ret = bindings.NetAddress_write(this.ptr);
187                 return ret;
188         }
189
190         /**
191          * Read a NetAddress from a byte array, created by NetAddress_write
192          */
193         public static Result_NetAddressDecodeErrorZ read(byte[] ser) {
194                 long ret = bindings.NetAddress_read(ser);
195                 if (ret >= 0 && ret <= 4096) { return null; }
196                 Result_NetAddressDecodeErrorZ ret_hu_conv = Result_NetAddressDecodeErrorZ.constr_from_ptr(ret);
197                 return ret_hu_conv;
198         }
199
200 }