[C#] Drop reference to `assert` which makes building on Win hard
[ldk-java] / c_sharp / src / org / ldk / structs / NetAddress.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 NetAddress : CommonBase {
12         protected NetAddress(object _dummy, long ptr) : base(ptr) { }
13         ~NetAddress() {
14                 if (ptr != 0) { bindings.NetAddress_free(ptr); }
15         }
16
17         internal static NetAddress constr_from_ptr(long ptr) {
18                 long raw_ty = bindings.LDKNetAddress_ty_from_ptr(ptr);
19                 switch (raw_ty) {
20                         case 0: return new NetAddress_IPv4(ptr);
21                         case 1: return new NetAddress_IPv6(ptr);
22                         case 2: return new NetAddress_OnionV2(ptr);
23                         case 3: return new NetAddress_OnionV3(ptr);
24                         case 4: return new NetAddress_Hostname(ptr);
25                         default:
26                                 throw new ArgumentException("Impossible enum variant");
27                 }
28         }
29
30         /** A NetAddress of type IPv4 */
31         public class NetAddress_IPv4 : NetAddress {
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 NetAddress_IPv4(long ptr) : base(null, ptr) {
41                         this.addr = bindings.LDKNetAddress_IPv4_get_addr(ptr);
42                         this.port = bindings.LDKNetAddress_IPv4_get_port(ptr);
43                 }
44         }
45         /** A NetAddress of type IPv6 */
46         public class NetAddress_IPv6 : NetAddress {
47                 /**
48                  * The 16-byte IPv6 address
49                  */
50                 public byte[] addr;
51                 /**
52                  * The port on which the node is listening
53                  */
54                 public short port;
55                 internal NetAddress_IPv6(long ptr) : base(null, ptr) {
56                         this.addr = bindings.LDKNetAddress_IPv6_get_addr(ptr);
57                         this.port = bindings.LDKNetAddress_IPv6_get_port(ptr);
58                 }
59         }
60         /** A NetAddress of type OnionV2 */
61         public class NetAddress_OnionV2 : NetAddress {
62                 public byte[] onion_v2;
63                 internal NetAddress_OnionV2(long ptr) : base(null, ptr) {
64                         this.onion_v2 = bindings.LDKNetAddress_OnionV2_get_onion_v2(ptr);
65                 }
66         }
67         /** A NetAddress of type OnionV3 */
68         public class NetAddress_OnionV3 : NetAddress {
69                 /**
70                  * The ed25519 long-term public key of the peer
71                  */
72                 public byte[] ed25519_pubkey;
73                 /**
74                  * The checksum of the pubkey and version, as included in the onion address
75                  */
76                 public short checksum;
77                 /**
78                  * The version byte, as defined by the Tor Onion v3 spec.
79                  */
80                 public byte version;
81                 /**
82                  * The port on which the node is listening
83                  */
84                 public short port;
85                 internal NetAddress_OnionV3(long ptr) : base(null, ptr) {
86                         this.ed25519_pubkey = bindings.LDKNetAddress_OnionV3_get_ed25519_pubkey(ptr);
87                         this.checksum = bindings.LDKNetAddress_OnionV3_get_checksum(ptr);
88                         this.version = bindings.LDKNetAddress_OnionV3_get_version(ptr);
89                         this.port = bindings.LDKNetAddress_OnionV3_get_port(ptr);
90                 }
91         }
92         /** A NetAddress of type Hostname */
93         public class NetAddress_Hostname : NetAddress {
94                 /**
95                  * The hostname on which the node is listening.
96                  */
97                 public Hostname hostname;
98                 /**
99                  * The port on which the node is listening.
100                  */
101                 public short port;
102                 internal NetAddress_Hostname(long ptr) : base(null, ptr) {
103                         long hostname = bindings.LDKNetAddress_Hostname_get_hostname(ptr);
104                         org.ldk.structs.Hostname hostname_hu_conv = null; if (hostname < 0 || hostname > 4096) { hostname_hu_conv = new org.ldk.structs.Hostname(null, hostname); }
105                         if (hostname_hu_conv != null) { hostname_hu_conv.ptrs_to.AddLast(this); };
106                         this.hostname = hostname_hu_conv;
107                         this.port = bindings.LDKNetAddress_Hostname_get_port(ptr);
108                 }
109         }
110         internal long clone_ptr() {
111                 long ret = bindings.NetAddress_clone_ptr(this.ptr);
112                 GC.KeepAlive(this);
113                 return ret;
114         }
115
116         /**
117          * Creates a copy of the NetAddress
118          */
119         public NetAddress clone() {
120                 long ret = bindings.NetAddress_clone(this.ptr);
121                 GC.KeepAlive(this);
122                 if (ret >= 0 && ret <= 4096) { return null; }
123                 org.ldk.structs.NetAddress ret_hu_conv = org.ldk.structs.NetAddress.constr_from_ptr(ret);
124                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
125                 return ret_hu_conv;
126         }
127
128         /**
129          * Utility method to constructs a new IPv4-variant NetAddress
130          */
131         public static NetAddress ipv4(byte[] addr, short port) {
132                 long ret = bindings.NetAddress_ipv4(InternalUtils.check_arr_len(addr, 4), port);
133                 GC.KeepAlive(addr);
134                 GC.KeepAlive(port);
135                 if (ret >= 0 && ret <= 4096) { return null; }
136                 org.ldk.structs.NetAddress ret_hu_conv = org.ldk.structs.NetAddress.constr_from_ptr(ret);
137                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(ret_hu_conv); };
138                 return ret_hu_conv;
139         }
140
141         /**
142          * Utility method to constructs a new IPv6-variant NetAddress
143          */
144         public static NetAddress ipv6(byte[] addr, short port) {
145                 long ret = bindings.NetAddress_ipv6(InternalUtils.check_arr_len(addr, 16), port);
146                 GC.KeepAlive(addr);
147                 GC.KeepAlive(port);
148                 if (ret >= 0 && ret <= 4096) { return null; }
149                 org.ldk.structs.NetAddress ret_hu_conv = org.ldk.structs.NetAddress.constr_from_ptr(ret);
150                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(ret_hu_conv); };
151                 return ret_hu_conv;
152         }
153
154         /**
155          * Utility method to constructs a new OnionV2-variant NetAddress
156          */
157         public static NetAddress onion_v2(byte[] a) {
158                 long ret = bindings.NetAddress_onion_v2(InternalUtils.check_arr_len(a, 12));
159                 GC.KeepAlive(a);
160                 if (ret >= 0 && ret <= 4096) { return null; }
161                 org.ldk.structs.NetAddress ret_hu_conv = org.ldk.structs.NetAddress.constr_from_ptr(ret);
162                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(ret_hu_conv); };
163                 return ret_hu_conv;
164         }
165
166         /**
167          * Utility method to constructs a new OnionV3-variant NetAddress
168          */
169         public static NetAddress onion_v3(byte[] ed25519_pubkey, short checksum, byte version, short port) {
170                 long ret = bindings.NetAddress_onion_v3(InternalUtils.check_arr_len(ed25519_pubkey, 32), checksum, version, port);
171                 GC.KeepAlive(ed25519_pubkey);
172                 GC.KeepAlive(checksum);
173                 GC.KeepAlive(version);
174                 GC.KeepAlive(port);
175                 if (ret >= 0 && ret <= 4096) { return null; }
176                 org.ldk.structs.NetAddress ret_hu_conv = org.ldk.structs.NetAddress.constr_from_ptr(ret);
177                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(ret_hu_conv); };
178                 return ret_hu_conv;
179         }
180
181         /**
182          * Utility method to constructs a new Hostname-variant NetAddress
183          */
184         public static NetAddress hostname(org.ldk.structs.Hostname hostname, short port) {
185                 long ret = bindings.NetAddress_hostname(hostname == null ? 0 : hostname.ptr, port);
186                 GC.KeepAlive(hostname);
187                 GC.KeepAlive(port);
188                 if (ret >= 0 && ret <= 4096) { return null; }
189                 org.ldk.structs.NetAddress ret_hu_conv = org.ldk.structs.NetAddress.constr_from_ptr(ret);
190                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(ret_hu_conv); };
191                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(hostname); };
192                 return ret_hu_conv;
193         }
194
195         /**
196          * Checks if two NetAddresss contain equal inner contents.
197          * This ignores pointers and is_owned flags and looks at the values in fields.
198          */
199         public bool eq(org.ldk.structs.NetAddress b) {
200                 bool ret = bindings.NetAddress_eq(this.ptr, b == null ? 0 : b.ptr);
201                 GC.KeepAlive(this);
202                 GC.KeepAlive(b);
203                 return ret;
204         }
205
206         public override bool Equals(object o) {
207                 if (!(o is NetAddress)) return false;
208                 return this.eq((NetAddress)o);
209         }
210         /**
211          * Serialize the NetAddress object into a byte array which can be read by NetAddress_read
212          */
213         public byte[] write() {
214                 byte[] ret = bindings.NetAddress_write(this.ptr);
215                 GC.KeepAlive(this);
216                 return ret;
217         }
218
219         /**
220          * Read a NetAddress from a byte array, created by NetAddress_write
221          */
222         public static Result_NetAddressDecodeErrorZ read(byte[] ser) {
223                 long ret = bindings.NetAddress_read(ser);
224                 GC.KeepAlive(ser);
225                 if (ret >= 0 && ret <= 4096) { return null; }
226                 Result_NetAddressDecodeErrorZ ret_hu_conv = Result_NetAddressDecodeErrorZ.constr_from_ptr(ret);
227                 return ret_hu_conv;
228         }
229
230 }
231 } } }