[Java] Properly convert strings to/from Modified UTF-8
[ldk-java] / src / main / java / org / ldk / structs / NetAddress.java
index 242e169a35d64570e086c56e86eabf13dc665c22..9714ec7c1e866130b2346cdae8ea0d4b5d6d7864 100644 (file)
@@ -4,11 +4,12 @@ import org.ldk.impl.bindings;
 import org.ldk.enums.*;
 import org.ldk.util.*;
 import java.util.Arrays;
+import java.lang.ref.Reference;
 import javax.annotation.Nullable;
 
 
 /**
- * An address which can be used to connect to a remote peer
+ * An address which can be used to connect to a remote peer.
  */
 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
 public class NetAddress extends CommonBase {
@@ -32,6 +33,9 @@ public class NetAddress extends CommonBase {
                if (raw_val.getClass() == bindings.LDKNetAddress.OnionV3.class) {
                        return new OnionV3(ptr, (bindings.LDKNetAddress.OnionV3)raw_val);
                }
+               if (raw_val.getClass() == bindings.LDKNetAddress.Hostname.class) {
+                       return new Hostname(ptr, (bindings.LDKNetAddress.Hostname)raw_val);
+               }
                assert false; return null; // Unreachable without extending the (internal) bindings interface
        }
 
@@ -73,25 +77,21 @@ public class NetAddress extends CommonBase {
        }
        /**
         * An old-style Tor onion address/port on which the peer is listening.
+        * 
+        * This field is deprecated and the Tor network generally no longer supports V2 Onion
+        * addresses. Thus, the details are not parsed here.
         */
        public final static class OnionV2 extends NetAddress {
-               /**
-                * The bytes (usually encoded in base32 with \".onion\" appended)
-               */
-               public final byte[] addr;
-               /**
-                * The port on which the node is listening
-               */
-               public final short port;
+               public final byte[] onion_v2;
                private OnionV2(long ptr, bindings.LDKNetAddress.OnionV2 obj) {
                        super(null, ptr);
-                       this.addr = obj.addr;
-                       this.port = obj.port;
+                       this.onion_v2 = obj.onion_v2;
                }
        }
        /**
         * A new-style Tor onion address/port on which the peer is listening.
-        * To create the human-readable \"hostname\", concatenate ed25519_pubkey, checksum, and version,
+        * 
+        * To create the human-readable \"hostname\", concatenate the ED25519 pubkey, checksum, and version,
         * wrap as base32 and append \".onion\".
         */
        public final static class OnionV3 extends NetAddress {
@@ -119,8 +119,30 @@ public class NetAddress extends CommonBase {
                        this.port = obj.port;
                }
        }
+       /**
+        * A hostname/port on which the peer is listening.
+        */
+       public final static class Hostname extends NetAddress {
+               /**
+                * The hostname on which the node is listening.
+               */
+               public final org.ldk.structs.Hostname hostname;
+               /**
+                * The port on which the node is listening.
+               */
+               public final short port;
+               private Hostname(long ptr, bindings.LDKNetAddress.Hostname obj) {
+                       super(null, ptr);
+                       long hostname = obj.hostname;
+                       org.ldk.structs.Hostname hostname_hu_conv = null; if (hostname < 0 || hostname > 4096) { hostname_hu_conv = new org.ldk.structs.Hostname(null, hostname); }
+                       if (hostname_hu_conv != null) { hostname_hu_conv.ptrs_to.add(this); };
+                       this.hostname = hostname_hu_conv;
+                       this.port = obj.port;
+               }
+       }
        long clone_ptr() {
                long ret = bindings.NetAddress_clone_ptr(this.ptr);
+               Reference.reachabilityFence(this);
                return ret;
        }
 
@@ -129,9 +151,10 @@ public class NetAddress extends CommonBase {
         */
        public NetAddress clone() {
                long ret = bindings.NetAddress_clone(this.ptr);
+               Reference.reachabilityFence(this);
                if (ret >= 0 && ret <= 4096) { return null; }
-               NetAddress ret_hu_conv = NetAddress.constr_from_ptr(ret);
-               ret_hu_conv.ptrs_to.add(this);
+               org.ldk.structs.NetAddress ret_hu_conv = org.ldk.structs.NetAddress.constr_from_ptr(ret);
+               if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
                return ret_hu_conv;
        }
 
@@ -140,9 +163,11 @@ public class NetAddress extends CommonBase {
         */
        public static NetAddress ipv4(byte[] addr, short port) {
                long ret = bindings.NetAddress_ipv4(InternalUtils.check_arr_len(addr, 4), port);
+               Reference.reachabilityFence(addr);
+               Reference.reachabilityFence(port);
                if (ret >= 0 && ret <= 4096) { return null; }
-               NetAddress ret_hu_conv = NetAddress.constr_from_ptr(ret);
-               ret_hu_conv.ptrs_to.add(ret_hu_conv);
+               org.ldk.structs.NetAddress ret_hu_conv = org.ldk.structs.NetAddress.constr_from_ptr(ret);
+               if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
                return ret_hu_conv;
        }
 
@@ -151,20 +176,23 @@ public class NetAddress extends CommonBase {
         */
        public static NetAddress ipv6(byte[] addr, short port) {
                long ret = bindings.NetAddress_ipv6(InternalUtils.check_arr_len(addr, 16), port);
+               Reference.reachabilityFence(addr);
+               Reference.reachabilityFence(port);
                if (ret >= 0 && ret <= 4096) { return null; }
-               NetAddress ret_hu_conv = NetAddress.constr_from_ptr(ret);
-               ret_hu_conv.ptrs_to.add(ret_hu_conv);
+               org.ldk.structs.NetAddress ret_hu_conv = org.ldk.structs.NetAddress.constr_from_ptr(ret);
+               if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
                return ret_hu_conv;
        }
 
        /**
         * Utility method to constructs a new OnionV2-variant NetAddress
         */
-       public static NetAddress onion_v2(byte[] addr, short port) {
-               long ret = bindings.NetAddress_onion_v2(InternalUtils.check_arr_len(addr, 10), port);
+       public static NetAddress onion_v2(byte[] a) {
+               long ret = bindings.NetAddress_onion_v2(InternalUtils.check_arr_len(a, 12));
+               Reference.reachabilityFence(a);
                if (ret >= 0 && ret <= 4096) { return null; }
-               NetAddress ret_hu_conv = NetAddress.constr_from_ptr(ret);
-               ret_hu_conv.ptrs_to.add(ret_hu_conv);
+               org.ldk.structs.NetAddress ret_hu_conv = org.ldk.structs.NetAddress.constr_from_ptr(ret);
+               if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
                return ret_hu_conv;
        }
 
@@ -173,17 +201,51 @@ public class NetAddress extends CommonBase {
         */
        public static NetAddress onion_v3(byte[] ed25519_pubkey, short checksum, byte version, short port) {
                long ret = bindings.NetAddress_onion_v3(InternalUtils.check_arr_len(ed25519_pubkey, 32), checksum, version, port);
+               Reference.reachabilityFence(ed25519_pubkey);
+               Reference.reachabilityFence(checksum);
+               Reference.reachabilityFence(version);
+               Reference.reachabilityFence(port);
+               if (ret >= 0 && ret <= 4096) { return null; }
+               org.ldk.structs.NetAddress ret_hu_conv = org.ldk.structs.NetAddress.constr_from_ptr(ret);
+               if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
+               return ret_hu_conv;
+       }
+
+       /**
+        * Utility method to constructs a new Hostname-variant NetAddress
+        */
+       public static NetAddress hostname(org.ldk.structs.Hostname hostname, short port) {
+               long ret = bindings.NetAddress_hostname(hostname == null ? 0 : hostname.ptr, port);
+               Reference.reachabilityFence(hostname);
+               Reference.reachabilityFence(port);
                if (ret >= 0 && ret <= 4096) { return null; }
-               NetAddress ret_hu_conv = NetAddress.constr_from_ptr(ret);
-               ret_hu_conv.ptrs_to.add(ret_hu_conv);
+               org.ldk.structs.NetAddress ret_hu_conv = org.ldk.structs.NetAddress.constr_from_ptr(ret);
+               if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
+               if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(hostname); };
                return ret_hu_conv;
        }
 
+       /**
+        * Checks if two NetAddresss contain equal inner contents.
+        * This ignores pointers and is_owned flags and looks at the values in fields.
+        */
+       public boolean eq(org.ldk.structs.NetAddress b) {
+               boolean ret = bindings.NetAddress_eq(this.ptr, b == null ? 0 : b.ptr);
+               Reference.reachabilityFence(this);
+               Reference.reachabilityFence(b);
+               return ret;
+       }
+
+       @Override public boolean equals(Object o) {
+               if (!(o instanceof NetAddress)) return false;
+               return this.eq((NetAddress)o);
+       }
        /**
         * Serialize the NetAddress object into a byte array which can be read by NetAddress_read
         */
        public byte[] write() {
                byte[] ret = bindings.NetAddress_write(this.ptr);
+               Reference.reachabilityFence(this);
                return ret;
        }
 
@@ -192,6 +254,7 @@ public class NetAddress extends CommonBase {
         */
        public static Result_NetAddressDecodeErrorZ read(byte[] ser) {
                long ret = bindings.NetAddress_read(ser);
+               Reference.reachabilityFence(ser);
                if (ret >= 0 && ret <= 4096) { return null; }
                Result_NetAddressDecodeErrorZ ret_hu_conv = Result_NetAddressDecodeErrorZ.constr_from_ptr(ret);
                return ret_hu_conv;