e31cf0e3af7a6b3258f56b4fcf64cc023fa47771
[ldk-java] / src / main / java / org / ldk / structs / SocketAddress.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 SocketAddress extends CommonBase {
16         private SocketAddress(Object _dummy, long ptr) { super(ptr); }
17         @Override @SuppressWarnings("deprecation")
18         protected void finalize() throws Throwable {
19                 super.finalize();
20                 if (ptr != 0) { bindings.SocketAddress_free(ptr); }
21         }
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);
26                 }
27                 if (raw_val.getClass() == bindings.LDKSocketAddress.TcpIpV6.class) {
28                         return new TcpIpV6(ptr, (bindings.LDKSocketAddress.TcpIpV6)raw_val);
29                 }
30                 if (raw_val.getClass() == bindings.LDKSocketAddress.OnionV2.class) {
31                         return new OnionV2(ptr, (bindings.LDKSocketAddress.OnionV2)raw_val);
32                 }
33                 if (raw_val.getClass() == bindings.LDKSocketAddress.OnionV3.class) {
34                         return new OnionV3(ptr, (bindings.LDKSocketAddress.OnionV3)raw_val);
35                 }
36                 if (raw_val.getClass() == bindings.LDKSocketAddress.Hostname.class) {
37                         return new Hostname(ptr, (bindings.LDKSocketAddress.Hostname)raw_val);
38                 }
39                 assert false; return null; // Unreachable without extending the (internal) bindings interface
40         }
41
42         /**
43          * An IPv4 address and port on which the peer is listening.
44          */
45         public final static class TcpIpV4 extends SocketAddress {
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 TcpIpV4(long ptr, bindings.LDKSocketAddress.TcpIpV4 obj) {
55                         super(null, ptr);
56                         this.addr = obj.addr;
57                         this.port = obj.port;
58                 }
59         }
60         /**
61          * An IPv6 address and port on which the peer is listening.
62          */
63         public final static class TcpIpV6 extends SocketAddress {
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 TcpIpV6(long ptr, bindings.LDKSocketAddress.TcpIpV6 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 SocketAddress {
85                 public final byte[] onion_v2;
86                 private OnionV2(long ptr, bindings.LDKSocketAddress.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 SocketAddress {
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.LDKSocketAddress.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 SocketAddress {
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.LDKSocketAddress.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.SocketAddress_clone_ptr(this.ptr);
145                 Reference.reachabilityFence(this);
146                 return ret;
147         }
148
149         /**
150          * Creates a copy of the SocketAddress
151          */
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); };
158                 return ret_hu_conv;
159         }
160
161         /**
162          * Utility method to constructs a new TcpIpV4-variant SocketAddress
163          */
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); };
171                 return ret_hu_conv;
172         }
173
174         /**
175          * Utility method to constructs a new TcpIpV6-variant SocketAddress
176          */
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); };
184                 return ret_hu_conv;
185         }
186
187         /**
188          * Utility method to constructs a new OnionV2-variant SocketAddress
189          */
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); };
196                 return ret_hu_conv;
197         }
198
199         /**
200          * Utility method to constructs a new OnionV3-variant SocketAddress
201          */
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); };
211                 return ret_hu_conv;
212         }
213
214         /**
215          * Utility method to constructs a new Hostname-variant SocketAddress
216          */
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); };
225                 return ret_hu_conv;
226         }
227
228         /**
229          * Checks if two SocketAddresss 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.SocketAddress b) {
233                 boolean ret = bindings.SocketAddress_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 SocketAddress)) return false;
241                 return this.eq((SocketAddress)o);
242         }
243         /**
244          * Serialize the SocketAddress object into a byte array which can be read by SocketAddress_read
245          */
246         public byte[] write() {
247                 byte[] ret = bindings.SocketAddress_write(this.ptr);
248                 Reference.reachabilityFence(this);
249                 return ret;
250         }
251
252         /**
253          * Read a SocketAddress from a byte array, created by SocketAddress_write
254          */
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);
260                 return ret_hu_conv;
261         }
262
263         /**
264          * Read a SocketAddress object from a string
265          */
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);
271                 return ret_hu_conv;
272         }
273
274 }