Example #1
0
        private static void OnRegisterReply(ServiceRef sdRef, ServiceFlags flags, ServiceError errorCode,
                                            string name, string regtype, string domain, IntPtr context)
        {
            var handle                    = GCHandle.FromIntPtr(context);
            var registerService           = handle.Target as RegisterService;
            RegisterServiceEventArgs args = new RegisterServiceEventArgs();

            args.Service      = registerService;
            args.IsRegistered = false;
            args.ServiceError = (ServiceErrorCode)errorCode;

            if (errorCode == ServiceError.NoError)
            {
                registerService.Name        = name;
                registerService.RegType     = regtype;
                registerService.ReplyDomain = domain;
                args.IsRegistered           = true;
            }

            RegisterServiceEventHandler handler = registerService.Response;

            if (handler != null)
            {
                handler(registerService, args);
            }
        }
Example #2
0
        private static void OnBrowseReply(ServiceRef sdRef, ServiceFlags flags, uint interfaceIndex, ServiceError errorCode,
                                          string serviceName, string regtype, string replyDomain, IntPtr context)
        {
            var           handle         = GCHandle.FromIntPtr(context);
            var           serviceBrowser = handle.Target as ServiceBrowser;
            BrowseService service        = new BrowseService();

            service.Flags           = flags;
            service.Name            = serviceName;
            service.RegType         = regtype;
            service.ReplyDomain     = replyDomain;
            service.InterfaceIndex  = interfaceIndex;
            service.AddressProtocol = serviceBrowser.address_protocol;

            Log.To.Discovery.V(TAG, "{0} (0x{1}) entered OnBrowseReply (found={2} flags={3})",
                               serviceBrowser, sdRef.Raw.ToString("X"), service, flags);

            ServiceBrowseEventArgs args = new ServiceBrowseEventArgs(
                service, (flags & ServiceFlags.MoreComing) != 0);

            if ((flags & ServiceFlags.Add) != 0)
            {
                lock (serviceBrowser.service_table) {
                    if (serviceBrowser.service_table.ContainsKey(serviceName))
                    {
                        serviceBrowser.service_table[serviceName] = service;
                    }
                    else
                    {
                        serviceBrowser.service_table.Add(serviceName, service);
                    }
                }

                ServiceBrowseEventHandler handler = serviceBrowser._serviceAdded;
                if (handler != null)
                {
                    handler(serviceBrowser, args);
                }
            }
            else
            {
                lock (serviceBrowser.service_table) {
                    if (serviceBrowser.service_table.ContainsKey(serviceName))
                    {
                        serviceBrowser.service_table.Remove(serviceName);
                    }
                }

                ServiceBrowseEventHandler handler = serviceBrowser._serviceRemoved;
                if (handler != null)
                {
                    handler(serviceBrowser, args);
                }

                service.Dispose();
            }
        }
Example #3
0
        private static void OnResolveReply(ServiceRef sdRef, ServiceFlags flags, uint interfaceIndex,
                                           ServiceError errorCode, string fullname, string hosttarget, ushort port, ushort txtLen,
                                           IntPtr txtRecord, IntPtr context)
        {
            var handle        = GCHandle.FromIntPtr(context);
            var browseService = handle.Target as BrowseService;

            Log.To.Discovery.V(Tag, "Resolve reply received for {0} (0x{1}), entering DNSServiceQueryRecord next",
                               browseService, sdRef.Raw.ToString("X"));
            browseService.is_resolved     = true;
            browseService.resolve_pending = false;

            browseService.InterfaceIndex = interfaceIndex;
            browseService.FullName       = fullname;
            browseService.Port           = (ushort)IPAddress.NetworkToHostOrder((short)port);
            browseService.TxtRecord      = new TxtRecord(txtLen, txtRecord);
            sdRef.Deallocate();

            // Run an A query to resolve the IP address
            ServiceRef sd_ref;

            if (browseService.AddressProtocol == AddressProtocol.Any || browseService.AddressProtocol == AddressProtocol.IPv4)
            {
                ServiceError error = Native.DNSServiceQueryRecord(out sd_ref, ServiceFlags.None, interfaceIndex,
                                                                  hosttarget, ServiceType.A, ServiceClass.IN, browseService.query_record_reply_handler, context);

                if (error != ServiceError.NoError)
                {
                    Log.To.Discovery.W(Tag, "Error in DNSServiceQueryRecord {0}", error);
                    browseService.Error(browseService, new ServiceErrorEventArgs("ResolveReply (IPv4)", error));
                    sd_ref.Deallocate();
                    return;
                }

                sd_ref.ProcessSingle(ServiceParams.Timeout);
            }

            if (browseService.AddressProtocol == AddressProtocol.Any || browseService.AddressProtocol == AddressProtocol.IPv6)
            {
                ServiceError error = Native.DNSServiceQueryRecord(out sd_ref, ServiceFlags.None, interfaceIndex,
                                                                  hosttarget, ServiceType.AAAA, ServiceClass.IN, browseService.query_record_reply_handler, context);

                if (error != ServiceError.NoError)
                {
                    if (error != ServiceError.NoError)
                    {
                        Log.To.Discovery.W(Tag, "Error in DNSServiceQueryRecord {0}", error);
                        browseService.Error(browseService, new ServiceErrorEventArgs("ResolveReply (IPv6)", error));
                        sd_ref.Deallocate();
                        return;
                    }
                }

                sd_ref.ProcessSingle(ServiceParams.Timeout);
            }
        }
Example #4
0
        private void OnResolveReply(ServiceRef sdRef, ServiceFlags flags, uint interfaceIndex,
                                    ServiceError errorCode, IntPtr fullname, string hosttarget, ushort port, ushort txtLen,
                                    IntPtr txtRecord, IntPtr contex)
        {
            is_resolved     = true;
            resolve_pending = false;

            InterfaceIndex  = interfaceIndex;
            FullName        = Native.Utf8toString(fullname);
            this.port       = (ushort)IPAddress.NetworkToHostOrder((short)port);
            TxtRecord       = new TxtRecord(txtLen, txtRecord);
            this.hosttarget = hosttarget;

            sdRef.Deallocate();

            // Run an A query to resolve the IP address
            ServiceRef sd_ref;

            if (AddressProtocol == AddressProtocol.Any || AddressProtocol == AddressProtocol.IPv4)
            {
                ServiceError error = Native.DNSServiceQueryRecord(out sd_ref, ServiceFlags.None, interfaceIndex,
                                                                  hosttarget, ServiceType.A, ServiceClass.IN, query_record_reply_handler, IntPtr.Zero);

                if (error != ServiceError.NoError)
                {
                    throw new ServiceErrorException(error);
                }

                sd_ref.Process();
            }

            if (AddressProtocol == AddressProtocol.Any || AddressProtocol == AddressProtocol.IPv6)
            {
                ServiceError error = Native.DNSServiceQueryRecord(out sd_ref, ServiceFlags.None, interfaceIndex,
                                                                  hosttarget, ServiceType.AAAA, ServiceClass.IN, query_record_reply_handler, IntPtr.Zero);

                if (error != ServiceError.NoError)
                {
                    throw new ServiceErrorException(error);
                }

                sd_ref.Process();
            }

            if (hostentry.AddressList != null)
            {
                ServiceResolvedEventHandler handler = Resolved;
                if (handler != null)
                {
                    handler(this, new ServiceResolvedEventArgs(this));
                }
            }
        }
Example #5
0
        private void OnBrowseReply(ServiceRef sdRef, ServiceFlags flags, uint interfaceIndex, ServiceError errorCode,
                                   IntPtr serviceName, string regtype, string replyDomain, IntPtr context)
        {
            string        name    = Native.Utf8toString(serviceName);
            BrowseService service = new BrowseService();

            service.Flags           = flags;
            service.Name            = name;
            service.RegType         = regtype;
            service.ReplyDomain     = replyDomain;
            service.InterfaceIndex  = interfaceIndex;
            service.AddressProtocol = address_protocol;

            ServiceBrowseEventArgs args = new ServiceBrowseEventArgs(
                service, (flags & ServiceFlags.MoreComing) != 0);

            if ((flags & ServiceFlags.Add) != 0)
            {
                lock (service_table) {
                    if (service_table.ContainsKey(name))
                    {
                        service_table[name] = service;
                    }
                    else
                    {
                        service_table.Add(name, service);
                    }
                }

                ServiceBrowseEventHandler handler = ServiceAdded;
                if (handler != null)
                {
                    handler(this, args);
                }
            }
            else
            {
                lock (service_table) {
                    if (service_table.ContainsKey(name))
                    {
                        service_table.Remove(name);
                    }
                }

                ServiceBrowseEventHandler handler = ServiceRemoved;
                if (handler != null)
                {
                    handler(this, args);
                }
            }
        }
        public void Stop()
        {
            if (sd_ref != ServiceRef.Zero)
            {
                sd_ref.Deallocate();
                sd_ref = ServiceRef.Zero;
            }

            if (thread != null)
            {
                thread.Abort();
                thread = null;
            }
        }
        private static void OnResolveReply(ServiceRef sdRef, ServiceFlags flags, uint interfaceIndex,
                                           ServiceError errorCode, string fullname, string hosttarget, ushort port, ushort txtLen,
                                           IntPtr txtRecord, IntPtr context)
        {
            var handle        = GCHandle.FromIntPtr(context);
            var browseService = handle.Target as BrowseService;

            browseService.is_resolved     = true;
            browseService.resolve_pending = false;

            browseService.InterfaceIndex = interfaceIndex;
            browseService.FullName       = fullname;
            browseService.port           = port;
            browseService.TxtRecord      = new TxtRecord(txtLen, txtRecord);

            sdRef.Deallocate();

            // Run an A query to resolve the IP address
            ServiceRef sd_ref;

            if (browseService.AddressProtocol == AddressProtocol.Any || browseService.AddressProtocol == AddressProtocol.IPv4)
            {
                ServiceError error = Native.DNSServiceQueryRecord(out sd_ref, ServiceFlags.None, interfaceIndex,
                                                                  hosttarget, ServiceType.A, ServiceClass.IN, browseService.query_record_reply_handler, context);

                if (error != ServiceError.NoError)
                {
                    throw new ServiceErrorException(error);
                }

                sd_ref.Process();
            }

            if (browseService.AddressProtocol == AddressProtocol.Any || browseService.AddressProtocol == AddressProtocol.IPv6)
            {
                ServiceError error = Native.DNSServiceQueryRecord(out sd_ref, ServiceFlags.None, interfaceIndex,
                                                                  hosttarget, ServiceType.AAAA, ServiceClass.IN, browseService.query_record_reply_handler, context);

                if (error != ServiceError.NoError)
                {
                    throw new ServiceErrorException(error);
                }

                sd_ref.Process();
            }
        }
Example #8
0
        private void OnResolveReply(ServiceRef sdRef, ServiceFlags flags, uint interfaceIndex,
                                    ServiceError errorCode, string fullname, string hosttarget, ushort port, ushort txtLen,
                                    IntPtr txtRecord, IntPtr contex)
        {
            is_resolved     = true;
            resolve_pending = false;

            InterfaceIndex = interfaceIndex;
            FullName       = fullname;
            this.port      = port;
            TxtRecord      = new TxtRecord(txtLen, txtRecord);

            sdRef.Deallocate();

            // Run an A query to resolve the IP address
            ServiceRef sd_ref;

            if (AddressProtocol == AddressProtocol.Any || AddressProtocol == AddressProtocol.IPv4)
            {
                ServiceError error = Native.DNSServiceQueryRecord(out sd_ref, ServiceFlags.None, interfaceIndex,
                                                                  hosttarget, ServiceType.A, ServiceClass.IN, query_record_reply_handler, IntPtr.Zero);

                if (error != ServiceError.NoError)
                {
                    throw new ServiceErrorException(error);
                }

                sd_ref.Process();
            }

            if (AddressProtocol == AddressProtocol.Any || AddressProtocol == AddressProtocol.IPv6)
            {
                ServiceError error = Native.DNSServiceQueryRecord(out sd_ref, ServiceFlags.None, interfaceIndex,
                                                                  hosttarget, ServiceType.AAAA, ServiceClass.IN, query_record_reply_handler, IntPtr.Zero);

                if (error != ServiceError.NoError)
                {
                    throw new ServiceErrorException(error);
                }

                sd_ref.Process();
            }
        }
Example #9
0
        private void OnRegisterReply(ServiceRef sdRef, ServiceFlags flags, ServiceError errorCode,
            string name, string regtype, string domain, IntPtr context)
        {
            RegisterServiceEventArgs args = new RegisterServiceEventArgs();

            args.Service = this;
            args.IsRegistered = false;
            args.ServiceError = (ServiceErrorCode)errorCode;

            if(errorCode == ServiceError.NoError) {
                Name = name;
                RegType = regtype;
                ReplyDomain = domain;
                args.IsRegistered = true;
            }

            RegisterServiceEventHandler handler = Response;
            if(handler != null) {
                handler(this, args);
            }
        }
Example #10
0
 public static extern ServiceError DNSServiceQueryRecord(out ServiceRef sdRef, ServiceFlags flags,
                                                         uint interfaceIndex, string fullname, ServiceType rrtype, ServiceClass rrclass,
                                                         DNSServiceQueryRecordReply callBack, IntPtr context);
Example #11
0
 public static extern ServiceError DNSServiceRegister(out ServiceRef sdRef, ServiceFlags flags,
                                                      uint interfaceIndex, byte[] name, string regtype, string domain, string host, ushort port,
                                                      ushort txtLen, byte [] txtRecord, DNSServiceRegisterReply callBack, IntPtr context);
Example #12
0
 public static extern ServiceError DNSServiceResolve(out ServiceRef sdRef, ServiceFlags flags,
                                                     uint interfaceIndex, byte[] name, string regtype, string domain, DNSServiceResolveReply callBack,
                                                     IntPtr context);
Example #13
0
 public static extern ServiceError DNSServiceCreateConnection(out ServiceRef sdRef);
Example #14
0
 public static extern ServiceError DNSServiceResolve(out ServiceRef sdRef, ServiceFlags flags,
     uint interfaceIndex, string name, string regtype, string domain, DNSServiceResolveReply callBack,
     IntPtr context);
Example #15
0
        private void OnBrowseReply(ServiceRef sdRef, ServiceFlags flags, uint interfaceIndex, ServiceError errorCode, 
            string serviceName, string regtype, string replyDomain, IntPtr context)
        {
            BrowseService service = new BrowseService();
            service.Flags = flags;
            service.Name = serviceName;
            service.RegType = regtype;
            service.ReplyDomain = replyDomain;
            service.InterfaceIndex = interfaceIndex;
            service.AddressProtocol = address_protocol;

            ServiceBrowseEventArgs args = new ServiceBrowseEventArgs(
                service, (flags & ServiceFlags.MoreComing) != 0);

            if((flags & ServiceFlags.Add) != 0) {
                lock (service_table) {
                    if (service_table.ContainsKey (serviceName)) {
                        service_table[serviceName] = service;
                    } else {
                        service_table.Add (serviceName, service);
                    }
                }

                ServiceBrowseEventHandler handler = ServiceAdded;
                if(handler != null) {
                    handler(this, args);
                }
            } else {
                lock (service_table) {
                    if (service_table.ContainsKey (serviceName)) {
                        service_table.Remove (serviceName);
                    }
                }

                ServiceBrowseEventHandler handler = ServiceRemoved;
                if(handler != null) {
                    handler(this, args);
                }
            }
        }
Example #16
0
        private static void OnQueryRecordReply(ServiceRef sdRef, ServiceFlags flags, uint interfaceIndex,
                                               ServiceError errorCode, string fullname, ServiceType rrtype, ServiceClass rrclass, ushort rdlen,
                                               IntPtr rdata, uint ttl, IntPtr context)
        {
            var handle        = GCHandle.FromIntPtr(context);
            var browseService = handle.Target as BrowseService;

            switch (rrtype)
            {
            case ServiceType.A:
                IPAddress address;

                if (rdlen == 4)
                {
                    // ~4.5 times faster than Marshal.Copy into byte[4]
                    uint address_raw = (uint)(Marshal.ReadByte(rdata, 3) << 24);
                    address_raw |= (uint)(Marshal.ReadByte(rdata, 2) << 16);
                    address_raw |= (uint)(Marshal.ReadByte(rdata, 1) << 8);
                    address_raw |= (uint)Marshal.ReadByte(rdata, 0);

                    address = new IPAddress(address_raw);
                }
                else if (rdlen == 16)
                {
                    byte [] address_raw = new byte[rdlen];
                    Marshal.Copy(rdata, address_raw, 0, rdlen);
                    address = new IPAddress(address_raw, interfaceIndex);
                }
                else
                {
                    break;
                }

                if (browseService.hostentry == null)
                {
                    browseService.hostentry          = new IPHostEntry();
                    browseService.hostentry.HostName = browseService.hosttarget;
                }

                if (browseService.hostentry.AddressList != null)
                {
                    ArrayList list = new ArrayList(browseService.hostentry.AddressList);
                    list.Add(address);
                    browseService.hostentry.AddressList = list.ToArray(typeof(IPAddress)) as IPAddress [];
                }
                else
                {
                    browseService.hostentry.AddressList = new IPAddress [] { address };
                }

                Log.To.Discovery.V(Tag, "Query record reply received for {0} (0x{1})", browseService, sdRef.Raw.ToString("X"));
                ServiceResolvedEventHandler handler = browseService._resolved;
                if (handler != null)
                {
                    handler(browseService, new ServiceResolvedEventArgs(browseService));
                }

                break;

            case ServiceType.TXT:
                if (browseService.TxtRecord != null)
                {
                    browseService.TxtRecord.Dispose();
                }

                browseService.TxtRecord = new TxtRecord(rdlen, rdata);
                break;

            default:
                break;
            }

            sdRef.Deallocate();
        }
        private static void OnQueryRecordReply(ServiceRef sdRef, ServiceFlags flags, uint interfaceIndex,
            ServiceError errorCode, string fullname, ServiceType rrtype, ServiceClass rrclass, ushort rdlen, 
            IntPtr rdata, uint ttl, IntPtr context)
        {
            var handle = GCHandle.FromIntPtr(context);
            var browseService = handle.Target as BrowseService;
            switch(rrtype) {
                case ServiceType.A:
                    IPAddress address;

                    if(rdlen == 4) {   
                        // ~4.5 times faster than Marshal.Copy into byte[4]
                        uint address_raw = (uint)(Marshal.ReadByte (rdata, 3) << 24);
                        address_raw |= (uint)(Marshal.ReadByte (rdata, 2) << 16);
                        address_raw |= (uint)(Marshal.ReadByte (rdata, 1) << 8);
                        address_raw |= (uint)Marshal.ReadByte (rdata, 0);

                        address = new IPAddress(address_raw);
                    } else if(rdlen == 16) {
                        byte [] address_raw = new byte[rdlen];
                        Marshal.Copy(rdata, address_raw, 0, rdlen);
                        address = new IPAddress(address_raw, interfaceIndex);
                    } else {
                        break;
                    }

                    if(browseService.hostentry == null) {
                        browseService.hostentry = new IPHostEntry();
                        browseService.hostentry.HostName = browseService.hosttarget;
                    }
                    
                    if(browseService.hostentry.AddressList != null) {
                        ArrayList list = new ArrayList(browseService.hostentry.AddressList);
                        list.Add(address);
                        browseService.hostentry.AddressList = list.ToArray(typeof(IPAddress)) as IPAddress [];
                    } else {
                        browseService.hostentry.AddressList = new IPAddress [] { address };
                    }

                    Log.To.Discovery.V(Tag, "Query record reply received for {0} (0x{1})", browseService, sdRef.Raw.ToString("X"));
                    ServiceResolvedEventHandler handler = browseService._resolved;
                    if(handler != null) {
                        handler(browseService, new ServiceResolvedEventArgs(browseService));
                    }
                    
                    break;
                case ServiceType.TXT:
                    if(browseService.TxtRecord != null) {
                        browseService.TxtRecord.Dispose();
                    }
            
                    browseService.TxtRecord = new TxtRecord(rdlen, rdata);
                    break;
                default:
                    break;
            }
            
            sdRef.Deallocate();
        }
 private static void OnResolveReply(ServiceRef sdRef, ServiceFlags flags, uint interfaceIndex,
     ServiceError errorCode, string fullname, string hosttarget, ushort port, ushort txtLen, 
     IntPtr txtRecord, IntPtr context)
 {
     var handle = GCHandle.FromIntPtr(context);
     var browseService = handle.Target as BrowseService;
     Log.To.Discovery.V(Tag, "Resolve reply received for {0} (0x{1}), entering DNSServiceQueryRecord next", 
         browseService, sdRef.Raw.ToString("X"));
     browseService.is_resolved = true;
     browseService.resolve_pending = false;
     
     browseService.InterfaceIndex = interfaceIndex;
     browseService.FullName = fullname;
     browseService.Port = (ushort)IPAddress.NetworkToHostOrder((short)port);
     browseService.TxtRecord = new TxtRecord(txtLen, txtRecord);
     sdRef.Deallocate();
     
     // Run an A query to resolve the IP address
     ServiceRef sd_ref;
     
     if (browseService.AddressProtocol == AddressProtocol.Any || browseService.AddressProtocol == AddressProtocol.IPv4) {
         
         ServiceError error = Native.DNSServiceQueryRecord(out sd_ref, ServiceFlags.None, interfaceIndex,
             hosttarget, ServiceType.A, ServiceClass.IN, browseService.query_record_reply_handler, context);
         
         if(error != ServiceError.NoError) {
             Log.To.Discovery.W(Tag, "Error in DNSServiceQueryRecord {0}", error);
             browseService.Error(browseService, new ServiceErrorEventArgs("ResolveReply (IPv4)", error));
             sd_ref.Deallocate();
             return;
         }
     
         sd_ref.ProcessSingle(ServiceParams.Timeout);
     }
     
     if (browseService.AddressProtocol == AddressProtocol.Any || browseService.AddressProtocol == AddressProtocol.IPv6) {
         ServiceError error = Native.DNSServiceQueryRecord(out sd_ref, ServiceFlags.None, interfaceIndex,
             hosttarget, ServiceType.AAAA, ServiceClass.IN, browseService.query_record_reply_handler, context);
         
         if(error != ServiceError.NoError) {
             if(error != ServiceError.NoError) {
                 Log.To.Discovery.W(Tag, "Error in DNSServiceQueryRecord {0}", error);
                 browseService.Error(browseService, new ServiceErrorEventArgs("ResolveReply (IPv6)", error));
                 sd_ref.Deallocate();
                 return;
             }
         }
     
         sd_ref.ProcessSingle(ServiceParams.Timeout);
     }
 }
Example #19
0
        private void OnRegisterReply(ServiceRef sdRef, ServiceFlags flags, ServiceError errorCode,
            string name, string regtype, string domain, IntPtr context)
        {
            RegisterServiceEventArgs args = new RegisterServiceEventArgs();

            args.Service = this;
            args.IsRegistered = false;
            args.ServiceError = (ServiceErrorCode)errorCode;

            if(errorCode == ServiceError.NoError) {
                Name = name;
                RegType = regtype;
                ReplyDomain = domain;
                args.IsRegistered = true;
            }

            RegisterServiceEventHandler handler = Response;
            if(handler != null) {
                handler(this, args);
            }
        }
Example #20
0
 public static extern ServiceError DNSServiceQueryRecord(out ServiceRef sdRef, ServiceFlags flags, 
     uint interfaceIndex, string fullname, ServiceType rrtype, ServiceClass rrclass, 
     DNSServiceQueryRecordReply callBack, IntPtr context);
        private void OnResolveReply(ServiceRef sdRef, ServiceFlags flags, uint interfaceIndex,
            ServiceError errorCode, string fullname, string hosttarget, ushort port, ushort txtLen, 
            IntPtr txtRecord, IntPtr contex)
        {
            is_resolved = true;
            resolve_pending = false;

            InterfaceIndex = interfaceIndex;
            FullName = fullname;
            this.port = port;
            TxtRecord = new TxtRecord(txtLen, txtRecord);

            sdRef.Deallocate();

            // Run an A query to resolve the IP address
            ServiceRef sd_ref;

            if (AddressProtocol == AddressProtocol.Any || AddressProtocol == AddressProtocol.IPv4) {
                ServiceError error = Native.DNSServiceQueryRecord(out sd_ref, ServiceFlags.None, interfaceIndex,
                    hosttarget, ServiceType.A, ServiceClass.IN, query_record_reply_handler, IntPtr.Zero);

                if(error != ServiceError.NoError) {
                    throw new ServiceErrorException(error);
                }

                sd_ref.Process();
            }

            // RichardF: fix
            //http://bryanprice.info/2011/01/29/mono-zeroconf-on-windows/
            if (/*AddressProtocol == AddressProtocol.Any || */ AddressProtocol == AddressProtocol.IPv6) {
                ServiceError error = Native.DNSServiceQueryRecord(out sd_ref, ServiceFlags.None, interfaceIndex,
                    hosttarget, ServiceType.A6, ServiceClass.IN, query_record_reply_handler, IntPtr.Zero);

                if(error != ServiceError.NoError) {
                    throw new ServiceErrorException(error);
                }

                sd_ref.Process();
            }
        }
 private static void OnRegisterReply(ServiceRef sdRef, ServiceFlags flags, ServiceError errorCode,
     string name, string regtype, string domain, IntPtr context)
 {
     var handle = GCHandle.FromIntPtr(context);
     var registerService = handle.Target as RegisterService;
     RegisterServiceEventArgs args = new RegisterServiceEventArgs();
     
     args.Service = registerService;
     args.IsRegistered = false;
     args.ServiceError = (ServiceErrorCode)errorCode;
     
     if(errorCode == ServiceError.NoError) {
         registerService.Name = name;
         registerService.RegType = regtype;
         registerService.ReplyDomain = domain;
         args.IsRegistered = true;
     }
     
     RegisterServiceEventHandler handler = registerService.Response;
     if(handler != null) {
         handler(registerService, args);
     }
 }
        private static void OnBrowseReply(ServiceRef sdRef, ServiceFlags flags, uint interfaceIndex, ServiceError errorCode, 
            string serviceName, string regtype, string replyDomain, IntPtr context)
        {
            var handle = GCHandle.FromIntPtr(context);
            var serviceBrowser = handle.Target as ServiceBrowser;
            BrowseService service = new BrowseService();
            service.Flags = flags;
            service.Name = serviceName;
            service.RegType = regtype;
            service.ReplyDomain = replyDomain;
            service.InterfaceIndex = interfaceIndex;
            service.AddressProtocol = serviceBrowser.address_protocol;

            Log.To.Discovery.V(TAG, "{0} (0x{1}) entered OnBrowseReply (found={2} flags={3})", 
                serviceBrowser, sdRef.Raw.ToString("X"), service, flags);
            
            ServiceBrowseEventArgs args = new ServiceBrowseEventArgs(
                service, (flags & ServiceFlags.MoreComing) != 0);
            
            if((flags & ServiceFlags.Add) != 0) {
                lock (serviceBrowser.service_table) {
                    if (serviceBrowser.service_table.ContainsKey (serviceName)) {
                        serviceBrowser.service_table[serviceName] = service;
                    } else {
                        serviceBrowser.service_table.Add (serviceName, service);
                    }
                }
                
                ServiceBrowseEventHandler handler = serviceBrowser._serviceAdded;
                if(handler != null) {
                    handler(serviceBrowser, args);
                }
            } else {
                lock (serviceBrowser.service_table) {
                    if (serviceBrowser.service_table.ContainsKey (serviceName)) {
                        serviceBrowser.service_table.Remove (serviceName);
                    }
                }
                
                ServiceBrowseEventHandler handler = serviceBrowser._serviceRemoved;
                if(handler != null) {
                    handler(serviceBrowser, args);
                }

                service.Dispose();
            }
        }
Example #24
0
 public static extern ServiceError DNSServiceCreateConnection(out ServiceRef sdRef);
Example #25
0
        private void OnQueryRecordReply(ServiceRef sdRef, ServiceFlags flags, uint interfaceIndex,
                                        ServiceError errorCode, string fullname, ServiceType rrtype, ServiceClass rrclass, ushort rdlen,
                                        IntPtr rdata, uint ttl, IntPtr context)
        {
            switch (rrtype)
            {
            case ServiceType.A:
                IPAddress address;

                if (rdlen == 4)
                {
                    // ~4.5 times faster than Marshal.Copy into byte[4]
                    uint address_raw = (uint)(Marshal.ReadByte(rdata, 3) << 24);
                    address_raw |= (uint)(Marshal.ReadByte(rdata, 2) << 16);
                    address_raw |= (uint)(Marshal.ReadByte(rdata, 1) << 8);
                    address_raw |= (uint)Marshal.ReadByte(rdata, 0);

                    address = new IPAddress(address_raw);
                }
                else if (rdlen == 16)
                {
                    byte [] address_raw = new byte[rdlen];
                    Marshal.Copy(rdata, address_raw, 0, rdlen);
                    address = new IPAddress(address_raw, interfaceIndex);
                }
                else
                {
                    break;
                }

                if (hostentry == null)
                {
                    hostentry          = new IPHostEntry();
                    hostentry.HostName = hosttarget;
                }

                if (hostentry.AddressList != null)
                {
                    ArrayList list = new ArrayList(hostentry.AddressList);
                    list.Add(address);
                    hostentry.AddressList = list.ToArray(typeof(IPAddress)) as IPAddress [];
                }
                else
                {
                    hostentry.AddressList = new IPAddress [] { address };
                }

                ServiceResolvedEventHandler handler = Resolved;
                if (handler != null)
                {
                    handler(this, new ServiceResolvedEventArgs(this));
                }

                break;

            case ServiceType.TXT:
                if (TxtRecord != null)
                {
                    TxtRecord.Dispose();
                }

                TxtRecord = new TxtRecord(rdlen, rdata);
                break;

            default:
                break;
            }

            sdRef.Deallocate();
        }
        private static void OnResolveReply(ServiceRef sdRef, ServiceFlags flags, uint interfaceIndex,
            ServiceError errorCode, string fullname, string hosttarget, ushort port, ushort txtLen, 
            IntPtr txtRecord, IntPtr context)
        {
            var handle = GCHandle.FromIntPtr(context);
            var browseService = handle.Target as BrowseService;
            browseService.is_resolved = true;
            browseService.resolve_pending = false;
            
            browseService.InterfaceIndex = interfaceIndex;
            browseService.FullName = fullname;
            browseService.Port = (ushort)IPAddress.NetworkToHostOrder((short)port);
            browseService.TxtRecord = new TxtRecord(txtLen, txtRecord);

            sdRef.Deallocate();
            
            // Run an A query to resolve the IP address
            ServiceRef sd_ref;
            
            if (browseService.AddressProtocol == AddressProtocol.Any || browseService.AddressProtocol == AddressProtocol.IPv4) {
                ServiceError error = Native.DNSServiceQueryRecord(out sd_ref, ServiceFlags.None, interfaceIndex,
                    hosttarget, ServiceType.A, ServiceClass.IN, browseService.query_record_reply_handler, context);
                
                if(error != ServiceError.NoError) {
                    throw new ServiceErrorException(error);
                }
            
                sd_ref.Process(ServiceParams.Timeout);
            }
            
            if (browseService.AddressProtocol == AddressProtocol.Any || browseService.AddressProtocol == AddressProtocol.IPv6) {
                ServiceError error = Native.DNSServiceQueryRecord(out sd_ref, ServiceFlags.None, interfaceIndex,
                    hosttarget, ServiceType.AAAA, ServiceClass.IN, browseService.query_record_reply_handler, context);
                
                if(error != ServiceError.NoError) {
                    throw new ServiceErrorException(error);
                }
            
                sd_ref.Process(ServiceParams.Timeout);
            }
        }
Example #27
0
        public void Stop()
        {
            if(sd_ref != ServiceRef.Zero) {
                sd_ref.Deallocate();
                sd_ref = ServiceRef.Zero;
            }

            if(thread != null) {
                thread.Abort();
                thread = null;
            }
        }
Example #28
0
        private void OnResolveReply(ServiceRef sdRef, ServiceFlags flags, uint interfaceIndex,
            ServiceError errorCode, string fullname, string hosttarget, ushort port, ushort txtLen, 
            IntPtr txtRecord, IntPtr contex)
        {
            is_resolved = true;
            resolve_pending = false;
            
            InterfaceIndex = interfaceIndex;
            FullName = fullname;
			this.port = (ushort)IPAddress.NetworkToHostOrder((short)port);
            TxtRecord = new TxtRecord(txtLen, txtRecord);

            sdRef.Deallocate();
            
            // Run an A query to resolve the IP address
            ServiceRef sd_ref;
            
            if (AddressProtocol == AddressProtocol.Any || AddressProtocol == AddressProtocol.IPv4) {
                ServiceError error = Native.DNSServiceQueryRecord(out sd_ref, ServiceFlags.None, interfaceIndex,
                    hosttarget, ServiceType.A, ServiceClass.IN, query_record_reply_handler, IntPtr.Zero);
                
                if(error != ServiceError.NoError) {
                    throw new ServiceErrorException(error);
                }
            
                sd_ref.Process();
            }
            
            if (AddressProtocol == AddressProtocol.Any || AddressProtocol == AddressProtocol.IPv6) {
                ServiceError error = Native.DNSServiceQueryRecord(out sd_ref, ServiceFlags.None, interfaceIndex,
                    hosttarget, ServiceType.A6, ServiceClass.IN, query_record_reply_handler, IntPtr.Zero);
                
                if(error != ServiceError.NoError) {
                    throw new ServiceErrorException(error);
                }
            
                sd_ref.Process();
            }
        }
Example #29
0
        private void OnQueryRecordReply(ServiceRef sdRef, ServiceFlags flags, uint interfaceIndex,
            ServiceError errorCode, string fullname, ServiceType rrtype, ServiceClass rrclass, ushort rdlen, 
            IntPtr rdata, uint ttl, IntPtr context)
        {
            switch(rrtype) {
                case ServiceType.A:
                    IPAddress address;

                    if(rdlen == 4) {   
                        // ~4.5 times faster than Marshal.Copy into byte[4]
                        uint address_raw = (uint)(Marshal.ReadByte (rdata, 3) << 24);
                        address_raw |= (uint)(Marshal.ReadByte (rdata, 2) << 16);
                        address_raw |= (uint)(Marshal.ReadByte (rdata, 1) << 8);
                        address_raw |= (uint)Marshal.ReadByte (rdata, 0);

                        address = new IPAddress(address_raw);
                    } else if(rdlen == 16) {
                        byte [] address_raw = new byte[rdlen];
                        Marshal.Copy(rdata, address_raw, 0, rdlen);
                        address = new IPAddress(address_raw, interfaceIndex);
                    } else {
                        break;
                    }

                    if(hostentry == null) {
                        hostentry = new IPHostEntry();
                        hostentry.HostName = hosttarget;
                    }
                    
                    if(hostentry.AddressList != null) {
                        ArrayList list = new ArrayList(hostentry.AddressList);
                        list.Add(address);
                        hostentry.AddressList = list.ToArray(typeof(IPAddress)) as IPAddress [];
                    } else {
                        hostentry.AddressList = new IPAddress [] { address };
                    }
                    
                    ServiceResolvedEventHandler handler = Resolved;
                    if(handler != null) {
                        handler(this, new ServiceResolvedEventArgs(this));
                    }
                    
                    break;
                case ServiceType.TXT:
                    if(TxtRecord != null) {
                        TxtRecord.Dispose();
                    }
            
                    TxtRecord = new TxtRecord(rdlen, rdata);
                    break;
                default:
                    break;
            }
            
            sdRef.Deallocate();
        }
Example #30
0
 public static extern ServiceError DNSServiceRegister(out ServiceRef sdRef, ServiceFlags flags,
     uint interfaceIndex, string name, string regtype, string domain, string host, ushort port,
     ushort txtLen, byte [] txtRecord, DNSServiceRegisterReply callBack, IntPtr context);