Skip to content
This repository has been archived by the owner on Jan 14, 2021. It is now read-only.

Support utf-8 service name #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

using System;
using System.Runtime.InteropServices;
using System.Text;

namespace Mono.Zeroconf.Providers.Bonjour
{
Expand All @@ -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")]
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
using System.Net;
using System.Threading;
using System.Runtime.InteropServices;
using System.Text;

namespace Mono.Zeroconf.Providers.Bonjour
{
Expand Down Expand Up @@ -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) {
Expand All @@ -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();

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}

Expand All @@ -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);
}
}

Expand Down