diff --git a/src/Mono.Zeroconf.Providers.Bonjour/Mono.Zeroconf.Providers.Bonjour/BrowseService.cs b/src/Mono.Zeroconf.Providers.Bonjour/Mono.Zeroconf.Providers.Bonjour/BrowseService.cs index 1eca52e..c121600 100644 --- a/src/Mono.Zeroconf.Providers.Bonjour/Mono.Zeroconf.Providers.Bonjour/BrowseService.cs +++ b/src/Mono.Zeroconf.Providers.Bonjour/Mono.Zeroconf.Providers.Bonjour/BrowseService.cs @@ -80,7 +80,7 @@ public void Resolve(bool requery) ServiceRef sd_ref; ServiceError error = Native.DNSServiceResolve(out sd_ref, ServiceFlags.None, - InterfaceIndex, Name, RegType, ReplyDomain, resolve_reply_handler, IntPtr.Zero); + InterfaceIndex, Encoding.UTF8.GetBytes(Name), RegType, ReplyDomain, resolve_reply_handler, IntPtr.Zero); if(error != ServiceError.NoError) { throw new ServiceErrorException(error); @@ -105,14 +105,14 @@ public void RefreshTxtRecord() } private void OnResolveReply(ServiceRef sdRef, ServiceFlags flags, uint interfaceIndex, - ServiceError errorCode, string fullname, string hosttarget, ushort port, ushort txtLen, + ServiceError errorCode, IntPtr fullname, string hosttarget, ushort port, ushort txtLen, IntPtr txtRecord, IntPtr contex) { is_resolved = true; resolve_pending = false; - + InterfaceIndex = interfaceIndex; - FullName = fullname; + FullName = Native.Utf8toString(fullname); this.port = port; TxtRecord = new TxtRecord(txtLen, txtRecord); diff --git a/src/Mono.Zeroconf.Providers.Bonjour/Mono.Zeroconf.Providers.Bonjour/Native.cs b/src/Mono.Zeroconf.Providers.Bonjour/Mono.Zeroconf.Providers.Bonjour/Native.cs index 037ff3d..b222e32 100644 --- a/src/Mono.Zeroconf.Providers.Bonjour/Mono.Zeroconf.Providers.Bonjour/Native.cs +++ b/src/Mono.Zeroconf.Providers.Bonjour/Mono.Zeroconf.Providers.Bonjour/Native.cs @@ -28,6 +28,7 @@ using System; using System.Runtime.InteropServices; +using System.Text; namespace Mono.Zeroconf.Providers.Bonjour { @@ -50,7 +51,7 @@ public static class Native // DNSServiceBrowse public delegate void DNSServiceBrowseReply(ServiceRef sdRef, ServiceFlags flags, uint interfaceIndex, - ServiceError errorCode, string serviceName, string regtype, string replyDomain, + ServiceError errorCode, IntPtr serviceName, string regtype, string replyDomain, IntPtr context); [DllImport("dnssd.dll")] @@ -61,22 +62,22 @@ public static extern ServiceError DNSServiceBrowse(out ServiceRef sdRef, Service // DNSServiceResolve public delegate void DNSServiceResolveReply(ServiceRef sdRef, ServiceFlags flags, uint interfaceIndex, - ServiceError errorCode, string fullname, string hosttarget, ushort port, ushort txtLen, + ServiceError errorCode, IntPtr fullname, string hosttarget, ushort port, ushort txtLen, IntPtr txtRecord, IntPtr context); [DllImport("dnssd.dll")] public static extern ServiceError DNSServiceResolve(out ServiceRef sdRef, ServiceFlags flags, - uint interfaceIndex, string name, string regtype, string domain, DNSServiceResolveReply callBack, + uint interfaceIndex, byte[] name, string regtype, string domain, DNSServiceResolveReply callBack, IntPtr context); // DNSServiceRegister public delegate void DNSServiceRegisterReply(ServiceRef sdRef, ServiceFlags flags, ServiceError errorCode, - string name, string regtype, string domain, IntPtr context); + IntPtr name, string regtype, string domain, IntPtr context); [DllImport("dnssd.dll")] public static extern ServiceError DNSServiceRegister(out ServiceRef sdRef, ServiceFlags flags, - uint interfaceIndex, string name, string regtype, string domain, string host, ushort port, + uint interfaceIndex, byte[] name, string regtype, string domain, string host, ushort port, ushort txtLen, byte [] txtRecord, DNSServiceRegisterReply callBack, IntPtr context); // DNSServiceQueryRecord @@ -117,5 +118,15 @@ public static extern ServiceError TXTRecordSetValue(IntPtr txtRecord, byte [] ke [DllImport("dnssd.dll")] public static extern ushort TXTRecordGetCount(ushort txtLen, IntPtr txtRecord); + + public static string Utf8toString(IntPtr ptr) { + int len = 0; + while (Marshal.ReadByte(ptr, len) != 0) { + len++; + } + byte[] raw = new byte[len]; + Marshal.Copy(ptr, raw, 0, len); + return Encoding.UTF8.GetString(raw); + } } } diff --git a/src/Mono.Zeroconf.Providers.Bonjour/Mono.Zeroconf.Providers.Bonjour/RegisterService.cs b/src/Mono.Zeroconf.Providers.Bonjour/Mono.Zeroconf.Providers.Bonjour/RegisterService.cs index 3b6cb9a..0974da0 100644 --- a/src/Mono.Zeroconf.Providers.Bonjour/Mono.Zeroconf.Providers.Bonjour/RegisterService.cs +++ b/src/Mono.Zeroconf.Providers.Bonjour/Mono.Zeroconf.Providers.Bonjour/RegisterService.cs @@ -30,6 +30,7 @@ using System.Net; using System.Threading; using System.Runtime.InteropServices; +using System.Text; namespace Mono.Zeroconf.Providers.Bonjour { @@ -107,7 +108,7 @@ public void ProcessRegister() ServiceError error = Native.DNSServiceRegister(out sd_ref, auto_rename ? ServiceFlags.None : ServiceFlags.NoAutoRename, InterfaceIndex, - Name, RegType, ReplyDomain, HostTarget, (ushort)IPAddress.HostToNetworkOrder((short)port), txt_rec_length, txt_rec, + Encoding.UTF8.GetBytes(Name), RegType, ReplyDomain, HostTarget, (ushort)IPAddress.HostToNetworkOrder((short)port), txt_rec_length, txt_rec, register_reply_handler, IntPtr.Zero); if(error != ServiceError.NoError) { @@ -128,7 +129,7 @@ public void Dispose() } private void OnRegisterReply(ServiceRef sdRef, ServiceFlags flags, ServiceError errorCode, - string name, string regtype, string domain, IntPtr context) + IntPtr name, string regtype, string domain, IntPtr context) { RegisterServiceEventArgs args = new RegisterServiceEventArgs(); @@ -137,7 +138,7 @@ private void OnRegisterReply(ServiceRef sdRef, ServiceFlags flags, ServiceError args.ServiceError = (ServiceErrorCode)errorCode; if(errorCode == ServiceError.NoError) { - Name = name; + Name = Native.Utf8toString(name); RegType = regtype; ReplyDomain = domain; args.IsRegistered = true; diff --git a/src/Mono.Zeroconf.Providers.Bonjour/Mono.Zeroconf.Providers.Bonjour/ServiceBrowser.cs b/src/Mono.Zeroconf.Providers.Bonjour/Mono.Zeroconf.Providers.Bonjour/ServiceBrowser.cs index 705d642..a46d865 100644 --- a/src/Mono.Zeroconf.Providers.Bonjour/Mono.Zeroconf.Providers.Bonjour/ServiceBrowser.cs +++ b/src/Mono.Zeroconf.Providers.Bonjour/Mono.Zeroconf.Providers.Bonjour/ServiceBrowser.cs @@ -168,11 +168,12 @@ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator () } private void OnBrowseReply(ServiceRef sdRef, ServiceFlags flags, uint interfaceIndex, ServiceError errorCode, - string serviceName, string regtype, string replyDomain, IntPtr context) + IntPtr serviceName, string regtype, string replyDomain, IntPtr context) { + string name = Native.Utf8toString(serviceName); BrowseService service = new BrowseService(); service.Flags = flags; - service.Name = serviceName; + service.Name = name; service.RegType = regtype; service.ReplyDomain = replyDomain; service.InterfaceIndex = interfaceIndex; @@ -183,10 +184,10 @@ private void OnBrowseReply(ServiceRef sdRef, ServiceFlags flags, uint interfaceI if((flags & ServiceFlags.Add) != 0) { lock (service_table) { - if (service_table.ContainsKey (serviceName)) { - service_table[serviceName] = service; + if (service_table.ContainsKey (name)) { + service_table[name] = service; } else { - service_table.Add (serviceName, service); + service_table.Add (name, service); } } @@ -196,8 +197,8 @@ private void OnBrowseReply(ServiceRef sdRef, ServiceFlags flags, uint interfaceI } } else { lock (service_table) { - if (service_table.ContainsKey (serviceName)) { - service_table.Remove (serviceName); + if (service_table.ContainsKey (name)) { + service_table.Remove (name); } }