[NSD] Add new GetTxtRecords api (#3353)
authorSeonah Moon <31679495+SeonahMoon@users.noreply.github.com>
Tue, 31 Aug 2021 03:52:26 +0000 (12:52 +0900)
committerGitHub <noreply@github.com>
Tue, 31 Aug 2021 03:52:26 +0000 (12:52 +0900)
* [NSD] Add new GetTxtRecords api

Related PR: https://github.com/Samsung/TizenFX/pull/2418

* Update src/Tizen.Network.Nsd/Tizen.Network.Nsd/DnssdService.cs

Co-authored-by: WonYoung Choi <wy80.choi@samsung.com>
* Update src/Tizen.Network.Nsd/Tizen.Network.Nsd/DnssdService.cs

Co-authored-by: WonYoung Choi <wy80.choi@samsung.com>
* Fix build error

Error: Tizen.Network.Nsd/DnssdService.cs(273,20): error CS0128: A local variable or function named 'txtValue' is already defined in this scope

Co-authored-by: WonYoung Choi <wy80.choi@samsung.com>
src/Tizen.Network.Nsd/Interop/Interop.Nsd.cs
src/Tizen.Network.Nsd/Tizen.Network.Nsd/DnssdService.cs

index 4c78c27..87697b2 100755 (executable)
@@ -74,7 +74,7 @@ internal static partial class Interop
             [DllImport(Libraries.Dnssd, EntryPoint = "dnssd_service_get_port")]
             internal static extern int GetPort(uint service, out int port);
             [DllImport(Libraries.Dnssd, EntryPoint = "dnssd_service_get_all_txt_record")]
-            internal static extern int GetAllTxtRecord(uint service, out ushort length, out byte[] value);
+            internal static extern int GetAllTxtRecord(uint service, out ushort length, out IntPtr value);
         }
 
         internal static class Ssdp
@@ -114,4 +114,9 @@ internal static partial class Interop
             internal static extern int StopBrowsing(uint browser);
         }
     }
+    internal static partial class Libc
+    {
+        [DllImport(Libraries.Libc, EntryPoint = "free")]
+        public static extern void Free(IntPtr userData);
+    }
 }
index 1209f28..494a726 100755 (executable)
@@ -18,6 +18,8 @@ using System;
 using System.Text;
 using System.Threading;
 using System.Net;
+using System.Runtime.InteropServices;
+using System.Collections.Generic;
 
 namespace Tizen.Network.Nsd
 {
@@ -218,14 +220,30 @@ namespace Tizen.Network.Nsd
             }
         }
 
-        private void GetTxtRecord(out ushort length, out byte[] value)
+        /// <summary>
+        /// Returns raw TXT records.
+        /// </summary>
+        /// <returns>Returns empty bytes array in case TXT record has not been set, else returns raw TXT record.</returns>
+        /// <since_tizen> 9 </since_tizen>
+        /// <feature>http://tizen.org/feature/network.service_discovery.dnssd</feature>
+        /// <exception cref="NotSupportedException">Thrown when DNS-SD is not supported.</exception>
+        /// <exception cref="InvalidOperationException">Thrown when any other error occurred.</exception>
+        public byte[] GetRawTXTRecords()
         {
-            int ret = Interop.Nsd.Dnssd.GetAllTxtRecord(_serviceHandle, out length, out value);
+            int ret = Interop.Nsd.Dnssd.GetAllTxtRecord(_serviceHandle, out ushort length, out IntPtr data);
             if (ret != (int)DnssdError.None)
             {
                 Log.Error(Globals.LogTag, "Failed to get the TXT record, Error: " + (DnssdError)ret);
                 NsdErrorFactory.ThrowDnssdException(ret);
             }
+            byte[] value = Array.Empty<byte>();
+            if (length > 0)
+            {
+                value = new byte[length];
+                Marshal.Copy(data, value, 0, length);
+                Interop.Libc.Free(data);
+            }
+            return value;
         }
 
         /// <summary>
@@ -251,19 +269,14 @@ namespace Tizen.Network.Nsd
                 Log.Error(Globals.LogTag, "Failed to add the TXT record, Error: " + (DnssdError)ret);
                 NsdErrorFactory.ThrowDnssdException(ret);
             }
-
-            byte[] txtValue;
-            ushort txtLength;
-            GetTxtRecord(out txtLength, out txtValue);
-
-            ret = Interop.Nsd.Dnssd.SetRecord(_serviceHandle, _dnsRecordtype, txtLength, txtValue);
+            byte[] txtValue = GetRawTXTRecords();
+            ret = Interop.Nsd.Dnssd.SetRecord(_serviceHandle, _dnsRecordtype, (ushort)txtValue.Length, txtValue);
             if (ret != (int)DnssdError.None)
             {
                 Log.Error(Globals.LogTag, "Failed to set the DNS resource record, Error: " + (DnssdError)ret);
                 NsdErrorFactory.ThrowDnssdException(ret);
             }
         }
-
         /// <summary>
         /// Removes the TXT record.
         /// </summary>
@@ -281,19 +294,6 @@ namespace Tizen.Network.Nsd
                 Log.Error(Globals.LogTag, "Failed to remove the TXT record, Error: " + (DnssdError)ret);
                 NsdErrorFactory.ThrowDnssdException(ret);
             }
-
-            byte[] txtValue;
-            ushort txtLength;
-            GetTxtRecord(out txtLength, out txtValue);
-            if (txtLength == 0)
-            {
-                ret = Interop.Nsd.Dnssd.UnsetRecord(_serviceHandle, _dnsRecordtype);
-                if (ret != (int)DnssdError.None)
-                {
-                    Log.Error(Globals.LogTag, "Failed to unset the DNS resource record, Error: " + (DnssdError)ret);
-                    NsdErrorFactory.ThrowDnssdException(ret);
-                }
-            }
         }
 
         /// <summary>
@@ -376,7 +376,7 @@ namespace Tizen.Network.Nsd
         /// <summary>
         /// Destroys the DnssdService object.
         /// </summary>
-         ~DnssdService()
+        ~DnssdService()
         {
             Dispose(false);
         }