42abb61cbab778f394923f41c58e5cdbd7f9070f
[ldk-java] / c_sharp / src / org / ldk / structs / SocketAddress.cs
1 using org.ldk.impl;
2 using org.ldk.enums;
3 using org.ldk.util;
4 using System;
5
6 namespace org { namespace ldk { namespace structs {
7
8 /**
9  * An address which can be used to connect to a remote peer.
10  */
11 public class SocketAddress : CommonBase {
12         protected SocketAddress(object _dummy, long ptr) : base(ptr) { }
13         ~SocketAddress() {
14                 if (ptr != 0) { bindings.SocketAddress_free(ptr); }
15         }
16
17         internal static SocketAddress constr_from_ptr(long ptr) {
18                 long raw_ty = bindings.LDKSocketAddress_ty_from_ptr(ptr);
19                 switch (raw_ty) {
20                         case 0: return new SocketAddress_TcpIpV4(ptr);
21                         case 1: return new SocketAddress_TcpIpV6(ptr);
22                         case 2: return new SocketAddress_OnionV2(ptr);
23                         case 3: return new SocketAddress_OnionV3(ptr);
24                         case 4: return new SocketAddress_Hostname(ptr);
25                         default:
26                                 throw new ArgumentException("Impossible enum variant");
27                 }
28         }
29
30         /** A SocketAddress of type TcpIpV4 */
31         public class SocketAddress_TcpIpV4 : SocketAddress {
32                 /**
33                  * The 4-byte IPv4 address
34                  */
35                 public byte[] addr;
36                 /**
37                  * The port on which the node is listening
38                  */
39                 public short port;
40                 internal SocketAddress_TcpIpV4(long ptr) : base(null, ptr) {
41                         long addr = bindings.LDKSocketAddress_TcpIpV4_get_addr(ptr);
42                         byte[] addr_conv = InternalUtils.decodeUint8Array(addr);
43                         this.addr = addr_conv;
44                         this.port = bindings.LDKSocketAddress_TcpIpV4_get_port(ptr);
45                 }
46         }
47         /** A SocketAddress of type TcpIpV6 */
48         public class SocketAddress_TcpIpV6 : SocketAddress {
49                 /**
50                  * The 16-byte IPv6 address
51                  */
52                 public byte[] addr;
53                 /**
54                  * The port on which the node is listening
55                  */
56                 public short port;
57                 internal SocketAddress_TcpIpV6(long ptr) : base(null, ptr) {
58                         long addr = bindings.LDKSocketAddress_TcpIpV6_get_addr(ptr);
59                         byte[] addr_conv = InternalUtils.decodeUint8Array(addr);
60                         this.addr = addr_conv;
61                         this.port = bindings.LDKSocketAddress_TcpIpV6_get_port(ptr);
62                 }
63         }
64         /** A SocketAddress of type OnionV2 */
65         public class SocketAddress_OnionV2 : SocketAddress {
66                 public byte[] onion_v2;
67                 internal SocketAddress_OnionV2(long ptr) : base(null, ptr) {
68                         long onion_v2 = bindings.LDKSocketAddress_OnionV2_get_onion_v2(ptr);
69                         byte[] onion_v2_conv = InternalUtils.decodeUint8Array(onion_v2);
70                         this.onion_v2 = onion_v2_conv;
71                 }
72         }
73         /** A SocketAddress of type OnionV3 */
74         public class SocketAddress_OnionV3 : SocketAddress {
75                 /**
76                  * The ed25519 long-term public key of the peer
77                  */
78                 public byte[] ed25519_pubkey;
79                 /**
80                  * The checksum of the pubkey and version, as included in the onion address
81                  */
82                 public short checksum;
83                 /**
84                  * The version byte, as defined by the Tor Onion v3 spec.
85                  */
86                 public byte version;
87                 /**
88                  * The port on which the node is listening
89                  */
90                 public short port;
91                 internal SocketAddress_OnionV3(long ptr) : base(null, ptr) {
92                         long ed25519_pubkey = bindings.LDKSocketAddress_OnionV3_get_ed25519_pubkey(ptr);
93                         byte[] ed25519_pubkey_conv = InternalUtils.decodeUint8Array(ed25519_pubkey);
94                         this.ed25519_pubkey = ed25519_pubkey_conv;
95                         this.checksum = bindings.LDKSocketAddress_OnionV3_get_checksum(ptr);
96                         this.version = bindings.LDKSocketAddress_OnionV3_get_version(ptr);
97                         this.port = bindings.LDKSocketAddress_OnionV3_get_port(ptr);
98                 }
99         }
100         /** A SocketAddress of type Hostname */
101         public class SocketAddress_Hostname : SocketAddress {
102                 /**
103                  * The hostname on which the node is listening.
104                  */
105                 public Hostname hostname;
106                 /**
107                  * The port on which the node is listening.
108                  */
109                 public short port;
110                 internal SocketAddress_Hostname(long ptr) : base(null, ptr) {
111                         long hostname = bindings.LDKSocketAddress_Hostname_get_hostname(ptr);
112                         org.ldk.structs.Hostname hostname_hu_conv = null; if (hostname < 0 || hostname > 4096) { hostname_hu_conv = new org.ldk.structs.Hostname(null, hostname); }
113                         if (hostname_hu_conv != null) { hostname_hu_conv.ptrs_to.AddLast(this); };
114                         this.hostname = hostname_hu_conv;
115                         this.port = bindings.LDKSocketAddress_Hostname_get_port(ptr);
116                 }
117         }
118         internal long clone_ptr() {
119                 long ret = bindings.SocketAddress_clone_ptr(this.ptr);
120                 GC.KeepAlive(this);
121                 return ret;
122         }
123
124         /**
125          * Creates a copy of the SocketAddress
126          */
127         public SocketAddress clone() {
128                 long ret = bindings.SocketAddress_clone(this.ptr);
129                 GC.KeepAlive(this);
130                 if (ret >= 0 && ret <= 4096) { return null; }
131                 org.ldk.structs.SocketAddress ret_hu_conv = org.ldk.structs.SocketAddress.constr_from_ptr(ret);
132                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
133                 return ret_hu_conv;
134         }
135
136         /**
137          * Utility method to constructs a new TcpIpV4-variant SocketAddress
138          */
139         public static SocketAddress tcp_ip_v4(byte[] addr, short port) {
140                 long ret = bindings.SocketAddress_tcp_ip_v4(InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(addr, 4)), port);
141                 GC.KeepAlive(addr);
142                 GC.KeepAlive(port);
143                 if (ret >= 0 && ret <= 4096) { return null; }
144                 org.ldk.structs.SocketAddress ret_hu_conv = org.ldk.structs.SocketAddress.constr_from_ptr(ret);
145                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(ret_hu_conv); };
146                 return ret_hu_conv;
147         }
148
149         /**
150          * Utility method to constructs a new TcpIpV6-variant SocketAddress
151          */
152         public static SocketAddress tcp_ip_v6(byte[] addr, short port) {
153                 long ret = bindings.SocketAddress_tcp_ip_v6(InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(addr, 16)), port);
154                 GC.KeepAlive(addr);
155                 GC.KeepAlive(port);
156                 if (ret >= 0 && ret <= 4096) { return null; }
157                 org.ldk.structs.SocketAddress ret_hu_conv = org.ldk.structs.SocketAddress.constr_from_ptr(ret);
158                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(ret_hu_conv); };
159                 return ret_hu_conv;
160         }
161
162         /**
163          * Utility method to constructs a new OnionV2-variant SocketAddress
164          */
165         public static SocketAddress onion_v2(byte[] a) {
166                 long ret = bindings.SocketAddress_onion_v2(InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(a, 12)));
167                 GC.KeepAlive(a);
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.AddLast(ret_hu_conv); };
171                 return ret_hu_conv;
172         }
173
174         /**
175          * Utility method to constructs a new OnionV3-variant SocketAddress
176          */
177         public static SocketAddress onion_v3(byte[] ed25519_pubkey, short checksum, byte version, short port) {
178                 long ret = bindings.SocketAddress_onion_v3(InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(ed25519_pubkey, 32)), checksum, version, port);
179                 GC.KeepAlive(ed25519_pubkey);
180                 GC.KeepAlive(checksum);
181                 GC.KeepAlive(version);
182                 GC.KeepAlive(port);
183                 if (ret >= 0 && ret <= 4096) { return null; }
184                 org.ldk.structs.SocketAddress ret_hu_conv = org.ldk.structs.SocketAddress.constr_from_ptr(ret);
185                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(ret_hu_conv); };
186                 return ret_hu_conv;
187         }
188
189         /**
190          * Utility method to constructs a new Hostname-variant SocketAddress
191          */
192         public static SocketAddress hostname(org.ldk.structs.Hostname hostname, short port) {
193                 long ret = bindings.SocketAddress_hostname(hostname == null ? 0 : hostname.ptr, port);
194                 GC.KeepAlive(hostname);
195                 GC.KeepAlive(port);
196                 if (ret >= 0 && ret <= 4096) { return null; }
197                 org.ldk.structs.SocketAddress ret_hu_conv = org.ldk.structs.SocketAddress.constr_from_ptr(ret);
198                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(ret_hu_conv); };
199                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(hostname); };
200                 return ret_hu_conv;
201         }
202
203         /**
204          * Checks if two SocketAddresss contain equal inner contents.
205          * This ignores pointers and is_owned flags and looks at the values in fields.
206          */
207         public bool eq(org.ldk.structs.SocketAddress b) {
208                 bool ret = bindings.SocketAddress_eq(this.ptr, b == null ? 0 : b.ptr);
209                 GC.KeepAlive(this);
210                 GC.KeepAlive(b);
211                 return ret;
212         }
213
214         public override bool Equals(object o) {
215                 if (!(o is SocketAddress)) return false;
216                 return this.eq((SocketAddress)o);
217         }
218         /**
219          * Serialize the SocketAddress object into a byte array which can be read by SocketAddress_read
220          */
221         public byte[] write() {
222                 long ret = bindings.SocketAddress_write(this.ptr);
223                 GC.KeepAlive(this);
224                 if (ret >= 0 && ret <= 4096) { return null; }
225                 byte[] ret_conv = InternalUtils.decodeUint8Array(ret);
226                 return ret_conv;
227         }
228
229         /**
230          * Read a SocketAddress from a byte array, created by SocketAddress_write
231          */
232         public static Result_SocketAddressDecodeErrorZ read(byte[] ser) {
233                 long ret = bindings.SocketAddress_read(InternalUtils.encodeUint8Array(ser));
234                 GC.KeepAlive(ser);
235                 if (ret >= 0 && ret <= 4096) { return null; }
236                 Result_SocketAddressDecodeErrorZ ret_hu_conv = Result_SocketAddressDecodeErrorZ.constr_from_ptr(ret);
237                 return ret_hu_conv;
238         }
239
240         /**
241          * Get the string representation of a SocketAddress object
242          */
243         public string to_str() {
244                 long ret = bindings.SocketAddress_to_str(this.ptr);
245                 GC.KeepAlive(this);
246                 if (ret >= 0 && ret <= 4096) { return null; }
247                 string ret_conv = InternalUtils.decodeString(ret);
248                 return ret_conv;
249         }
250
251         /**
252          * Read a SocketAddress object from a string
253          */
254         public static Result_SocketAddressSocketAddressParseErrorZ from_str(string s) {
255                 long ret = bindings.SocketAddress_from_str(InternalUtils.encodeString(s));
256                 GC.KeepAlive(s);
257                 if (ret >= 0 && ret <= 4096) { return null; }
258                 Result_SocketAddressSocketAddressParseErrorZ ret_hu_conv = Result_SocketAddressSocketAddressParseErrorZ.constr_from_ptr(ret);
259                 return ret_hu_conv;
260         }
261
262 }
263 } } }