/// <summary> /// Called with the result of the call to DNSServiceRegister() in the Publish() method. /// /// If this object instance is configured with an <see cref="DNSService.InvokeableObject">InvokeableObject</see>, /// this method is called in a thread safe manner. Typically, this means it's called on the application main loop. /// </summary> /// <param name="sdRef"> /// The DNSServiceRef initialized by DNSServiceRegister(). /// </param> /// <param name="flags"> /// Currently unused, reserved for future use. /// </param> /// <param name="errorCode"> /// Will be NoError on success, otherwise will indicate the failure that occurred /// (including name conflicts, if the kDNSServiceFlagsNoAutoRename flag was used when registering.) /// Other parameters are undefined if errorCode is nonzero. /// </param> /// <param name="name"> /// The service name registered (if the application did not specify a name in /// DNSServiceRegister(), this indicates what name was automatically chosen). /// </param> /// <param name="regtype"> /// The type of service registered, as it was passed to the callout. /// </param> /// <param name="domain"> /// The domain on which the service was registered (if the application did not /// specify a domain in DNSServiceRegister(), this indicates the default domain /// on which the service was registered). /// </param> /// <param name="context"> /// The context pointer that was passed to the callout. /// </param> private void RegisterReply(IntPtr sdRef, DNSServiceFlags flags, DNSServiceErrorType errorCode, String name, String regtype, String domain, IntPtr context) { if (errorCode == DNSServiceErrorType.NoError) { // Update name, type domain to match what was actually published mName = name; mType = regtype; mDomain = domain; if (DidPublishService != null) { DidPublishService(this); } } else { Stop(); if (DidNotPublishService != null) { DNSServiceException exception = new DNSServiceException("DNSServiceRegister", errorCode); DidNotPublishService(this, exception); } } }
public static extern DNSServiceErrorType DNSServiceUpdateRecord( IntPtr sdRef, IntPtr recordRef, DNSServiceFlags flags, UInt16 rdLength, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)] byte[] rData, UInt32 ttl);
public static extern DNSServiceErrorType DNSServiceBrowse(out IntPtr sdRef, DNSServiceFlags flags, UInt32 interfaceIndex, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] String regtype, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] String domain, DNSServiceBrowseReply callBack, IntPtr context);
public static extern DNSServiceErrorType DNSServiceQueryRecord(out IntPtr sdRef, DNSServiceFlags flags, UInt32 interfaceIndex, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] String fullname, DNSServiceType rrType, DNSServiceClass rrClass, DNSServiceQueryReply callBack, IntPtr context);
/// <summary> /// Called with the result of the call to DNSServiceResolve(). /// /// If this object instance is configured with an <see cref="DNSService.InvokeableObject">InvokeableObject</see>, /// this method is called in a thread safe manner. Typically, this means it's called on the application main loop. /// </summary> /// <param name="sdRef"> /// The DNSServiceRef initialized by DNSServiceResolve(). /// </param> /// <param name="flags"> /// Currently unused, reserved for future use. /// </param> /// <param name="interfaceIndex"> /// The interface on which the service was resolved. /// </param> /// <param name="errorCode"> /// Will be NoError (0) on success, otherwise will indicate the failure that occurred. /// Other parameters are undefined if the errorCode is nonzero. /// </param> /// <param name="fullname"> /// The full service domain name, in the form [servicename].[protocol].[domain]. /// (This name is escaped following standard DNS rules, making it suitable for /// passing to standard system DNS APIs such as res_query(), or to the /// special-purpose functions included in this API that take fullname parameters.) /// </param> /// <param name="hosttarget"> /// The target hostname of the machine providing the service. This name can /// be passed to functions like gethostbyname() to identify the host's IP address. /// </param> /// <param name="port"> /// The port, in network byte order, on which connections are accepted for this service. /// </param> /// <param name="txtLen"> /// The length of the txt record, in bytes. /// </param> /// <param name="txtRecord"> /// The service's primary txt record, in standard txt record format. /// </param> /// <param name="context"> /// The context pointer that was passed to the callout. /// </param> private void ResolveReply(IntPtr sdRef, DNSServiceFlags flags, UInt32 interfaceIndex, DNSServiceErrorType errorCode, String fullname, String hosttarget, UInt16 port, UInt16 txtLen, byte[] txtRecord, IntPtr context) { if (errorCode == DNSServiceErrorType.NoError) { // Update internal variables mHostName = hosttarget; mPort = ((int)System.Net.IPAddress.NetworkToHostOrder((short)port) & 0x0000ffff); // We may want to update the txt record. // The service may not have a txt record yet if it's never been monitored or resolved before. // Also, if it's not currently being monitored, then the returned txt record may include updates if (mTXTRecordData == null || !NetService.ByteArrayCompare(mTXTRecordData, txtRecord)) { mTXTRecordData = txtRecord; // Invoke delegate if set if (DidUpdateTXT != null) { DidUpdateTXT(this); } } // At this point we have a host name, but we don't have the actual IP address. // We could use the Windows API's (System.Net.Dns.BeginGetHostEntry) to // convert from host name to IP, but they're painfully slow. // According to the following website (and my own personal testing), // using DNSServiceQueryRecord is much faster: // http://lists.apple.com/archives/Bonjour-dev/2006/Jan/msg00008.html //AsyncCallback cb = new AsyncCallback(c.AsyncGetHostEntryCallback); //IAsyncResult ar = System.Net.Dns.BeginGetHostEntry(hosttarget, cb, c); // Begin the process of looking up the IP address(es) IPLookup(); } else { Stop(); if (DidNotResolveService != null) { DNSServiceException exception = new DNSServiceException("DNSServiceResolve", errorCode); DidNotResolveService(this, exception); } } }
private void SearchForDomains(DNSServiceFlags flags) { Stop(); domainSearchReplyCb = new mDNSImports.DNSServiceDomainEnumReply(DomainSearchReply); DNSServiceErrorType err = mDNSImports.DNSServiceEnumerateDomains(out serviceHandle, flags, 0, domainSearchReplyCb, IntPtr.Zero); if (err != DNSServiceErrorType.NoError) { throw new DNSServiceException("DNSServiceEnumerateDomains", err); } SetupWatchSocket(serviceHandle); }
public static extern DNSServiceErrorType DNSServiceRegister( out IntPtr sdRef, DNSServiceFlags flags, UInt32 interfaceIndex, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] String name, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] String regtype, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] String domain, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] String host, UInt16 port, UInt16 txtLen, byte[] txtRecord, DNSServiceRegisterReply callBack, IntPtr context);
private void BrowseReply(IntPtr sdRef, DNSServiceFlags flags, UInt32 interfaceIndex, DNSServiceErrorType errorCode, String serviceName, String regtype, String replyDomain, IntPtr context) { bool moreComing = ((flags & DNSServiceFlags.MoreComing) != 0); if ((flags & DNSServiceFlags.Add) != 0) { // Add NetService newService = new NetService(replyDomain, regtype, serviceName); newService.InheritInvokeOptions(this); foundServices.Add(newService); if (DidFindService != null) { DidFindService(this, newService, moreComing); } } else { // Remove foreach (NetService service in foundServices) { if (service.Name == serviceName && service.Type == regtype && service.Domain == replyDomain) { foundServices.Remove(service); if (DidRemoveService != null) { DidRemoveService(this, service, moreComing); } break; } } } }
/// <summary> /// Called with the result of the call to DNSServiceQueryRecord() in the IPLookup() method. /// /// If this object instance is configured with an <see cref="DNSService.InvokeableObject">InvokeableObject</see>, /// this method is called in a thread safe manner. Typically, this means it's called on the application main loop. /// </summary> /// <param name="sdRef"> /// The DNSServiceRef initialized by DNSServiceQueryRecord(). /// </param> /// <param name="flags"> /// Possible values are kDNSServiceFlagsMoreComing and kDNSServiceFlagsAdd. /// The Add flag is NOT set for PTR records with a ttl of 0, i.e. "Remove" events. /// </param> /// <param name="interfaceIndex"> /// The interface on which the query was resolved. /// </param> /// <param name="errorCode"> /// Will be NoError on success, otherwise will indicate the failure that occurred. /// Other parameters are undefined if errorCode is nonzero. /// </param> /// <param name="fullname"> /// The resource record's full domain name. /// </param> /// <param name="rrType"> /// The resource record's type (e.g. kDNSServiceType_PTR, kDNSServiceType_SRV, etc) /// </param> /// <param name="rrClass"> /// The class of the resource record (usually kDNSServiceClass_IN). /// </param> /// <param name="rdLength"> /// The length, in bytes, of the resource record rdata. /// </param> /// <param name="rData"> /// The raw rdata of the resource record. /// </param> /// <param name="ttl"> /// The resource record's time to live, in seconds. /// </param> /// <param name="context"> /// The context pointer that was passed to the callout. /// </param> private void IPLookupReply(IntPtr sdRef, DNSServiceFlags flags, UInt32 interfaceIndex, DNSServiceErrorType errorCode, String fullname, DNSServiceType rrType, DNSServiceClass rrClass, UInt16 rdLength, byte[] rData, UInt32 ttl, IntPtr context) { if (errorCode == DNSServiceErrorType.NoError) { if ((flags & DNSServiceFlags.Add) > 0) { System.Net.IPAddress addr = new System.Net.IPAddress(rData); System.Net.IPEndPoint ep = new System.Net.IPEndPoint(addr, mPort); mAddresses.Add(ep); } if ((flags & DNSServiceFlags.MoreComing) == 0) { Stop(); if (DidResolveService != null) { DidResolveService(this); } } } else { Stop(); if (DidNotResolveService != null) { DNSServiceException exception = new DNSServiceException("DNSServiceQueryRecord", errorCode); DidNotResolveService(this, exception); } } }
/// <summary> /// Called with the result of the call to DNSServiceQueryRecord() in the StartMonitoring() method. /// /// If this object instance is configured with an <see cref="DNSService.InvokeableObject">InvokeableObject</see>, /// this method is called in a thread safe manner. Typically, this means it's called on the application main loop. /// </summary> /// <param name="sdRef"> /// The DNSServiceRef initialized by DNSServiceQueryRecord(). /// </param> /// <param name="flags"> /// Possible values are kDNSServiceFlagsMoreComing and kDNSServiceFlagsAdd. /// The Add flag is NOT set for PTR records with a ttl of 0, i.e. "Remove" events. /// </param> /// <param name="interfaceIndex"> /// The interface on which the query was resolved. /// </param> /// <param name="errorCode"> /// Will be NoError on success, otherwise will indicate the failure that occurred. /// Other parameters are undefined if errorCode is nonzero. /// </param> /// <param name="fullname"> /// The resource record's full domain name. /// </param> /// <param name="rrType"> /// The resource record's type (e.g. kDNSServiceType_PTR, kDNSServiceType_SRV, etc) /// </param> /// <param name="rrClass"> /// The class of the resource record (usually kDNSServiceClass_IN). /// </param> /// <param name="rdLength"> /// The length, in bytes, of the resource record rdata. /// </param> /// <param name="rData"> /// The raw rdata of the resource record. /// </param> /// <param name="ttl"> /// The resource record's time to live, in seconds. /// </param> /// <param name="context"> /// The context pointer that was passed to the callout. /// </param> private void QueryReply(IntPtr sdRef, DNSServiceFlags flags, UInt32 interfaceIndex, DNSServiceErrorType errorCode, String fullname, DNSServiceType rrType, DNSServiceClass rrClass, UInt16 rdLength, byte[] rData, UInt32 ttl, IntPtr context) { if (errorCode == DNSServiceErrorType.NoError) { mTXTRecordData = rData; if (DidUpdateTXT != null) { DidUpdateTXT(this); } } }
private void DomainSearchReply(IntPtr sdRef, DNSServiceFlags flags, UInt32 interfaceIndex, DNSServiceErrorType errorCode, String replyDomain, IntPtr context) { bool moreComing = ((flags & DNSServiceFlags.MoreComing) != 0); if ((flags & DNSServiceFlags.Add) != 0) { /* add */ if (DidFindDomain != null) { DidFindDomain(this, replyDomain, moreComing); } } else { /* remove */ if (DidRemoveDomain != null) { DidRemoveDomain(this, replyDomain, moreComing); } } }
private void SearchForDomains(DNSServiceFlags flags) { Stop(); domainSearchReplyCb = new mDNSImports.DNSServiceDomainEnumReply(DomainSearchReply); gchSelf = GCHandle.Alloc(this); DNSServiceErrorType err; err = mDNSImports.DNSServiceEnumerateDomains(out sdRef, flags, 0, domainSearchReplyCb, (IntPtr)gchSelf); if (err != DNSServiceErrorType.kDNSServiceErr_NoError) { throw new DNSServiceException("DNSServiceEnumerateDomains", err); } SetupWatchSocket(); }
public static extern DNSServiceErrorType DNSServiceEnumerateDomains( out IntPtr sdRef, DNSServiceFlags flags, UInt32 interfaceIndex, DNSServiceDomainEnumReply callBack, IntPtr context);
private void RegisterReply(IntPtr sdRef, DNSServiceFlags flags, DNSServiceErrorType errorCode, String name, String regtype, String domain, IntPtr context) { if (errorCode != DNSServiceErrorType.kDNSServiceErr_NoError) { throw new DNSServiceException("DNSServiceRegisterReply", errorCode); } }
/// <summary> /// Called with the result of the call to DNSServiceQueryRecord() in the IPLookup() method. /// /// If this object instance is configured with an <see cref="DNSService.InvokeableObject">InvokeableObject</see>, /// this method is called in a thread safe manner. Typically, this means it's called on the application main loop. /// </summary> /// <param name="sdRef"> /// The DNSServiceRef initialized by DNSServiceQueryRecord(). /// </param> /// <param name="flags"> /// Possible values are kDNSServiceFlagsMoreComing and kDNSServiceFlagsAdd. /// The Add flag is NOT set for PTR records with a ttl of 0, i.e. "Remove" events. /// </param> /// <param name="interfaceIndex"> /// The interface on which the query was resolved. /// </param> /// <param name="errorCode"> /// Will be NoError on success, otherwise will indicate the failure that occurred. /// Other parameters are undefined if errorCode is nonzero. /// </param> /// <param name="fullname"> /// The resource record's full domain name. /// </param> /// <param name="rrType"> /// The resource record's type (e.g. kDNSServiceType_PTR, kDNSServiceType_SRV, etc) /// </param> /// <param name="rrClass"> /// The class of the resource record (usually kDNSServiceClass_IN). /// </param> /// <param name="rdLength"> /// The length, in bytes, of the resource record rdata. /// </param> /// <param name="rData"> /// The raw rdata of the resource record. /// </param> /// <param name="ttl"> /// The resource record's time to live, in seconds. /// </param> /// <param name="context"> /// The context pointer that was passed to the callout. /// </param> private void IPLookupReply(IntPtr sdRef, DNSServiceFlags flags, UInt32 interfaceIndex, DNSServiceErrorType errorCode, String fullname, DNSServiceType rrType, DNSServiceClass rrClass, UInt16 rdLength, byte[] rData, UInt32 ttl, IntPtr context) { if (errorCode == DNSServiceErrorType.NoError) { if((flags & DNSServiceFlags.Add) > 0) { System.Net.IPAddress addr = new System.Net.IPAddress(rData); System.Net.IPEndPoint ep = new System.Net.IPEndPoint(addr, mPort); mAddresses.Add(ep); } if ((flags & DNSServiceFlags.MoreComing) == 0) { Stop(); if (DidResolveService != null) { DidResolveService(this); } } } else { Stop(); if (DidNotResolveService != null) { DNSServiceException exception = new DNSServiceException("DNSServiceQueryRecord", errorCode); DidNotResolveService(this, exception); } } }
private static void ResolveReply(IntPtr sdRef, DNSServiceFlags flags, UInt32 interfaceIndex, DNSServiceErrorType errorCode, String fullname, String hosttarget, UInt16 port, UInt16 txtLen, byte[] txtRecord, IntPtr context) { GCHandle gch = (GCHandle)context; NetService c = (NetService)gch.Target; Console.WriteLine("{4} fullname: {0}, hosttarget: {1}, port: {2}, txtLen: {3}", fullname, hosttarget, port, txtLen, System.Threading.Thread.CurrentThread.ManagedThreadId); /* set TXT record data */ c.TXTRecordData = txtRecord; c.mHostName = hosttarget; c.mPort = (int)System.Net.IPAddress.NetworkToHostOrder((short)port); if (c.DidUpdateTXT != null) c.DidUpdateTXT(c); AsyncCallback cb = new AsyncCallback(c.AsyncGetHostEntryCallback); IAsyncResult ar = System.Net.Dns.BeginGetHostEntry(hosttarget, cb, c); }
private void DomainSearchReply(IntPtr sdRef, DNSServiceFlags flags, UInt32 interfaceIndex, DNSServiceErrorType errorCode, String replyDomain, IntPtr context) { bool moreComing = ((flags & DNSServiceFlags.MoreComing) != 0); if ((flags & DNSServiceFlags.Add) != 0) { /* add */ if (DidFindDomain != null) DidFindDomain(this, replyDomain, moreComing); } else { /* remove */ if (DidRemoveDomain != null) DidRemoveDomain(this, replyDomain, moreComing); } }
private static void DomainSearchReply(IntPtr sdRef, DNSServiceFlags flags, UInt32 interfaceIndex, DNSServiceErrorType errorCode, String replyDomain, IntPtr context) { GCHandle gch = (GCHandle)context; NetServiceBrowser c = (NetServiceBrowser)gch.Target; bool moreComing = ((flags & DNSServiceFlags.kDNSServiceFlagsMoreComing) != 0); if ((flags & DNSServiceFlags.kDNSServiceFlagsAdd) != 0) { /* add */ if (c.DidFindDomain != null) c.DidFindDomain(c, replyDomain, moreComing); } else { /* remove */ if (c.DidRemoveDomain != null) c.DidRemoveDomain(c, replyDomain, moreComing); } }
private void BrowseReply(IntPtr sdRef, DNSServiceFlags flags, UInt32 interfaceIndex, DNSServiceErrorType errorCode, String serviceName, String regtype, String replyDomain, IntPtr context) { bool moreComing = ((flags & DNSServiceFlags.MoreComing) != 0); if ((flags & DNSServiceFlags.Add) != 0) { // Add NetService newService = new NetService(replyDomain, regtype, serviceName); newService.InheritInvokeOptions(this); foundServices.Add(newService); if (DidFindService != null) DidFindService(this, newService, moreComing); } else { // Remove foreach (NetService service in foundServices) { if (service.Name == serviceName && service.Type == regtype && service.Domain == replyDomain) { foundServices.Remove(service); if (DidRemoveService != null) DidRemoveService(this, service, moreComing); break; } } } }
private static void BrowseReply(IntPtr sdRef, DNSServiceFlags flags, UInt32 interfaceIndex, DNSServiceErrorType errorCode, String serviceName, String regtype, String replyDomain, IntPtr context) { GCHandle gch = (GCHandle)context; NetServiceBrowser c = (NetServiceBrowser)gch.Target; bool moreComing = ((flags & DNSServiceFlags.kDNSServiceFlagsMoreComing) != 0); if ((flags & DNSServiceFlags.kDNSServiceFlagsAdd) != 0) { /* add */ NetService newService = new NetService(replyDomain, regtype, serviceName); newService.InheritInvokeOptions(c); c.foundServices.Add(newService); if (c.DidFindService != null) c.DidFindService(c, newService, moreComing); } else { /* remove */ foreach (NetService service in c.foundServices) { if (service.Name == serviceName && service.Type == regtype && service.Domain == replyDomain) { c.foundServices.Remove(service); if (c.DidRemoveService != null) c.DidRemoveService(c, service, moreComing); break; } } } }