[Java] Properly convert strings to/from Modified UTF-8
[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 java.lang.ref.Reference;
8 import javax.annotation.Nullable;
9
10
11 /**
12  * An address which can be used to connect to a remote peer.
13  */
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 {
19                 super.finalize();
20                 if (ptr != 0) { bindings.NetAddress_free(ptr); }
21         }
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);
26                 }
27                 if (raw_val.getClass() == bindings.LDKNetAddress.IPv6.class) {
28                         return new IPv6(ptr, (bindings.LDKNetAddress.IPv6)raw_val);
29                 }
30                 if (raw_val.getClass() == bindings.LDKNetAddress.OnionV2.class) {
31                         return new OnionV2(ptr, (bindings.LDKNetAddress.OnionV2)raw_val);
32                 }
33                 if (raw_val.getClass() == bindings.LDKNetAddress.OnionV3.class) {
34                         return new OnionV3(ptr, (bindings.LDKNetAddress.OnionV3)raw_val);
35                 }
36                 if (raw_val.getClass() == bindings.LDKNetAddress.Hostname.class) {
37                         return new Hostname(ptr, (bindings.LDKNetAddress.Hostname)raw_val);
38                 }
39                 assert false; return null; // Unreachable without extending the (internal) bindings interface
40         }
41
42         /**
43          * An IPv4 address/port on which the peer is listening.
44          */
45         public final static class IPv4 extends NetAddress {
46                 /**
47                  * The 4-byte IPv4 address
48                 */
49                 public final byte[] addr;
50                 /**
51                  * The port on which the node is listening
52                 */
53                 public final short port;
54                 private IPv4(long ptr, bindings.LDKNetAddress.IPv4 obj) {
55                         super(null, ptr);
56                         this.addr = obj.addr;
57                         this.port = obj.port;
58                 }
59         }
60         /**
61          * An IPv6 address/port on which the peer is listening.
62          */
63         public final static class IPv6 extends NetAddress {
64                 /**
65                  * The 16-byte IPv6 address
66                 */
67                 public final byte[] addr;
68                 /**
69                  * The port on which the node is listening
70                 */
71                 public final short port;
72                 private IPv6(long ptr, bindings.LDKNetAddress.IPv6 obj) {
73                         super(null, ptr);
74                         this.addr = obj.addr;
75                         this.port = obj.port;
76                 }
77         }
78         /**
79          * An old-style Tor onion address/port on which the peer is listening.
80          * 
81          * This field is deprecated and the Tor network generally no longer supports V2 Onion
82          * addresses. Thus, the details are not parsed here.
83          */
84         public final static class OnionV2 extends NetAddress {
85                 public final byte[] onion_v2;
86                 private OnionV2(long ptr, bindings.LDKNetAddress.OnionV2 obj) {
87                         super(null, ptr);
88                         this.onion_v2 = obj.onion_v2;
89                 }
90         }
91         /**
92          * A new-style Tor onion address/port on which the peer is listening.
93          * 
94          * To create the human-readable \"hostname\", concatenate the 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         /**
123          * A hostname/port on which the peer is listening.
124          */
125         public final static class Hostname extends NetAddress {
126                 /**
127                  * The hostname on which the node is listening.
128                 */
129                 public final org.ldk.structs.Hostname hostname;
130                 /**
131                  * The port on which the node is listening.
132                 */
133                 public final short port;
134                 private Hostname(long ptr, bindings.LDKNetAddress.Hostname obj) {
135                         super(null, ptr);
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;
141                 }
142         }
143         long clone_ptr() {
144                 long ret = bindings.NetAddress_clone_ptr(this.ptr);
145                 Reference.reachabilityFence(this);
146                 return ret;
147         }
148
149         /**
150          * Creates a copy of the NetAddress
151          */
152         public NetAddress clone() {
153                 long ret = bindings.NetAddress_clone(this.ptr);
154                 Reference.reachabilityFence(this);
155                 if (ret >= 0 && ret <= 4096) { return null; }
156                 org.ldk.structs.NetAddress ret_hu_conv = org.ldk.structs.NetAddress.constr_from_ptr(ret);
157                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
158                 return ret_hu_conv;
159         }
160
161         /**
162          * Utility method to constructs a new IPv4-variant NetAddress
163          */
164         public static NetAddress ipv4(byte[] addr, short port) {
165                 long ret = bindings.NetAddress_ipv4(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.NetAddress ret_hu_conv = org.ldk.structs.NetAddress.constr_from_ptr(ret);
170                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
171                 return ret_hu_conv;
172         }
173
174         /**
175          * Utility method to constructs a new IPv6-variant NetAddress
176          */
177         public static NetAddress ipv6(byte[] addr, short port) {
178                 long ret = bindings.NetAddress_ipv6(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.NetAddress ret_hu_conv = org.ldk.structs.NetAddress.constr_from_ptr(ret);
183                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
184                 return ret_hu_conv;
185         }
186
187         /**
188          * Utility method to constructs a new OnionV2-variant NetAddress
189          */
190         public static NetAddress onion_v2(byte[] a) {
191                 long ret = bindings.NetAddress_onion_v2(InternalUtils.check_arr_len(a, 12));
192                 Reference.reachabilityFence(a);
193                 if (ret >= 0 && ret <= 4096) { return null; }
194                 org.ldk.structs.NetAddress ret_hu_conv = org.ldk.structs.NetAddress.constr_from_ptr(ret);
195                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
196                 return ret_hu_conv;
197         }
198
199         /**
200          * Utility method to constructs a new OnionV3-variant NetAddress
201          */
202         public static NetAddress onion_v3(byte[] ed25519_pubkey, short checksum, byte version, short port) {
203                 long ret = bindings.NetAddress_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.NetAddress ret_hu_conv = org.ldk.structs.NetAddress.constr_from_ptr(ret);
210                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
211                 return ret_hu_conv;
212         }
213
214         /**
215          * Utility method to constructs a new Hostname-variant NetAddress
216          */
217         public static NetAddress hostname(org.ldk.structs.Hostname hostname, short port) {
218                 long ret = bindings.NetAddress_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.NetAddress ret_hu_conv = org.ldk.structs.NetAddress.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); };
225                 return ret_hu_conv;
226         }
227
228         /**
229          * Checks if two NetAddresss contain equal inner contents.
230          * This ignores pointers and is_owned flags and looks at the values in fields.
231          */
232         public boolean eq(org.ldk.structs.NetAddress b) {
233                 boolean ret = bindings.NetAddress_eq(this.ptr, b == null ? 0 : b.ptr);
234                 Reference.reachabilityFence(this);
235                 Reference.reachabilityFence(b);
236                 return ret;
237         }
238
239         @Override public boolean equals(Object o) {
240                 if (!(o instanceof NetAddress)) return false;
241                 return this.eq((NetAddress)o);
242         }
243         /**
244          * Serialize the NetAddress object into a byte array which can be read by NetAddress_read
245          */
246         public byte[] write() {
247                 byte[] ret = bindings.NetAddress_write(this.ptr);
248                 Reference.reachabilityFence(this);
249                 return ret;
250         }
251
252         /**
253          * Read a NetAddress from a byte array, created by NetAddress_write
254          */
255         public static Result_NetAddressDecodeErrorZ read(byte[] ser) {
256                 long ret = bindings.NetAddress_read(ser);
257                 Reference.reachabilityFence(ser);
258                 if (ret >= 0 && ret <= 4096) { return null; }
259                 Result_NetAddressDecodeErrorZ ret_hu_conv = Result_NetAddressDecodeErrorZ.constr_from_ptr(ret);
260                 return ret_hu_conv;
261         }
262
263 }