Пример #1
0
        public static 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)
        {
            switch (Native.GetCurrentOperatingSystem())
            {
            case OperatingSystem.Windows:
                return(NativeWindows.DNSServiceRegister(out sdRef, flags, interfaceIndex, name, regtype, domain, host, port, txtLen, txtRecord, callBack, context));

            case OperatingSystem.OSX:
                return(NativeOSX.DNSServiceRegister(out sdRef, flags, interfaceIndex, name, regtype, domain, host, port, txtLen, txtRecord, callBack, context));

            default:
                throw new InvalidOperationException("The current OS is unsupported");
            }
        }
Пример #2
0
 public static extern ServiceError DNSServiceBrowse(out ServiceRef sdRef,
                                                    ServiceFlags flags,
                                                    uint interfaceIndex,
                                                    string regtype,
                                                    string domain,
                                                    DNSServiceBrowseReply callBack,
                                                    IntPtr context);
        private void OnRegisterReply(ServiceRef sdRef,
                                     ServiceFlags flags,
                                     ServiceError errorCode,
                                     IntPtr name,
                                     string regtype,
                                     string domain,
                                     IntPtr context)
        {
            var args = new RegisterServiceEventArgs
            {
                Service      = this,
                IsRegistered = false,
                ServiceError = (ServiceErrorCode)errorCode
            };


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

            RegisterServiceEventHandler handler = this.Response;

            handler?.Invoke(this, args);
        }
Пример #4
0
 public static extern ServiceError DNSServiceQueryRecord(out ServiceRef sdRef,
                                                         ServiceFlags flags,
                                                         uint interfaceIndex,
                                                         string fullname,
                                                         ServiceType rrtype,
                                                         ServiceClass rrclass,
                                                         DNSServiceQueryRecordReply callBack,
                                                         IntPtr context);
Пример #5
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);
Пример #6
0
        public static ServiceError DNSServiceCreateConnection(out ServiceRef sdRef)
        {
            switch (Native.GetCurrentOperatingSystem())
            {
            case OperatingSystem.Windows:
                return(NativeWindows.DNSServiceCreateConnection(out sdRef));

            case OperatingSystem.OSX:
                return(NativeOSX.DNSServiceCreateConnection(out sdRef));

            default:
                throw new InvalidOperationException("The current OS is unsupported");
            }
        }
Пример #7
0
        public static ServiceError DNSServiceBrowse(out ServiceRef sdRef,
                                                    ServiceFlags flags,
                                                    uint interfaceIndex,
                                                    string regtype,
                                                    string domain,
                                                    DNSServiceBrowseReply callBack,
                                                    IntPtr context)
        {
            switch (Native.GetCurrentOperatingSystem())
            {
            case OperatingSystem.Windows:
                return(NativeWindows.DNSServiceBrowse(out sdRef, flags, interfaceIndex, regtype, domain, callBack, context));

            case OperatingSystem.OSX:
                return(NativeOSX.DNSServiceBrowse(out sdRef, flags, interfaceIndex, regtype, domain, callBack, context));

            default:
                throw new InvalidOperationException("The current OS is unsupported");
            }
        }
Пример #8
0
        public static ServiceError DNSServiceQueryRecord(out ServiceRef sdRef,
                                                         ServiceFlags flags,
                                                         uint interfaceIndex,
                                                         string fullname,
                                                         ServiceType rrtype,
                                                         ServiceClass rrclass,
                                                         DNSServiceQueryRecordReply callBack,
                                                         IntPtr context)
        {
            switch (Native.GetCurrentOperatingSystem())
            {
            case OperatingSystem.Windows:
                return(NativeWindows.DNSServiceQueryRecord(out sdRef, flags, interfaceIndex, fullname, rrtype, rrclass, callBack, context));

            case OperatingSystem.OSX:
                return(NativeOSX.DNSServiceQueryRecord(out sdRef, flags, interfaceIndex, fullname, rrtype, rrclass, callBack, context));

            default:
                throw new InvalidOperationException("The current OS is unsupported");
            }
        }
Пример #9
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:
            case ServiceType.AAAA:
                IPAddress address;

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

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

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

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

                //ServiceResolvedEventHandler handler = this.Resolved ;
                //if (handler != null)
                //    handler (this, new ServiceResolvedEventArgs (this)) ;

                break;

            case ServiceType.TXT:
                this.TxtRecord?.Dispose();

                this.TxtRecord = new TxtRecord(rdlen, rdata);
                break;
            }

            if ((flags & ServiceFlags.MoreComing) != ServiceFlags.MoreComing)
            {
                sdRef.Deallocate();
            }
        }
Пример #10
0
        private void OnResolveReply(ServiceRef sdRef,
                                    ServiceFlags flags,
                                    uint interfaceIndex,
                                    ServiceError errorCode,
                                    IntPtr fullname,
                                    string hosttarget,
                                    ushort port,
                                    ushort txtLen,
                                    IntPtr txtRecord,
                                    IntPtr contex)
        {
            this.IsResolved     = true;
            this.resolvePending = false;

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

            sdRef.Deallocate();

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

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

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

                sd_ref.Process();
            }

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

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

                sd_ref.Process();
            }

            if (this.hostentry.AddressList != null)
            {
                ServiceResolvedEventHandler handler = this.Resolved;
                handler?.Invoke(this, new ServiceResolvedEventArgs(this));
            }
        }
Пример #11
0
 public static extern ServiceError DNSServiceCreateConnection(out ServiceRef sdRef);