internal void ProcessRegister() { ushort txt_rec_length = 0; byte [] txt_rec = null; if (TxtRecord != null) { txt_rec_length = ((TxtRecord)TxtRecord.BaseRecord).RawLength; txt_rec = new byte[txt_rec_length]; Marshal.Copy(((TxtRecord)TxtRecord.BaseRecord).RawBytes, txt_rec, 0, txt_rec_length); } _self = GCHandle.Alloc(this); Log.To.Discovery.V(Tag, "{0} preparing to enter DNSServiceRegister"); 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, register_reply_handler, GCHandle.ToIntPtr(_self)); if (error != ServiceError.NoError) { Log.To.Discovery.W(Tag, "Got error in DNSServiceRegister {0}", error); Error(this, new ServiceErrorEventArgs("ProcessRegister", error)); sd_ref.Deallocate(); return; } sd_ref.Process(); }
public void Dispose() { if(thread != null) { thread.Abort(); thread = null; } sd_ref.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); } }
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)); } } }
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(); } }
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(); } }
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 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 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(); } }
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); } }
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 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; 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); } }