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)); } } }
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(); } }
/// <summary> /// Start new broadcast service /// </summary> /// <param name="type">Type of discovery</param> /// <param name="nameToBroadcast">The name of the service that needs to be broadcasted</param> /// <param name="physicalLocation">The physical location of the service that needs to be broadcasted</param> /// <param name="code">code to be broadcasted (e.g. device id)</param> /// <param name="addressToBroadcast">The address of the service that needs to be broadcasted</param> public void Start( DiscoveryType type, string nameToBroadcast, string physicalLocation, string code, Uri addressToBroadcast) { DiscoveryType = type; switch ( DiscoveryType ) { case DiscoveryType.WsDiscovery: { Ip = Net.GetIp( IpType.All ); Port = 7985; Address = "http://" + Ip + ":" + Port + "/"; _discoveryHost = new ServiceHost( new DiscoveyService() ); var serviceEndpoint = _discoveryHost.AddServiceEndpoint( typeof( IDiscovery ), new WebHttpBinding(), Net.GetUrl( Ip, Port, "" ) ); serviceEndpoint.Behaviors.Add( new WebHttpBehavior() ); var broadcaster = new EndpointDiscoveryBehavior(); broadcaster.Extensions.Add( nameToBroadcast.ToXElement<string>() ); broadcaster.Extensions.Add( physicalLocation.ToXElement<string>() ); broadcaster.Extensions.Add( addressToBroadcast.ToString().ToXElement<string>() ); broadcaster.Extensions.Add( code.ToXElement<string>() ); serviceEndpoint.Behaviors.Add( broadcaster ); _discoveryHost.Description.Behaviors.Add( new ServiceDiscoveryBehavior() ); _discoveryHost.Description.Endpoints.Add( new UdpDiscoveryEndpoint() ); _discoveryHost.Open(); IsRunning = true; Debug.WriteLine( DiscoveryType.ToString() + " is started" ); } break; case DiscoveryType.Zeroconf: { _service = new RegisterService { Name = nameToBroadcast, RegType = "_am._tcp", ReplyDomain = "local", Port = 3689 }; // TxtRecords are optional var txtRecord = new TxtRecord { { "name", nameToBroadcast }, { "addr", addressToBroadcast.ToString() }, { "loc", physicalLocation }, { "code", code } }; _service.TxtRecord = txtRecord; _service.Response += service_Response; _service.Register(); Debug.WriteLine(DiscoveryType.ToString() + " is started"); } break; } }
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 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(); } }
public TxtRecordEnumerator(TxtRecord record) { this.record = record; }