Airplay realization and problem summary of MDNs

Source: Internet
Author: User

Introduction to the implementation of MDNS mdnsresponder

I. Introduction of NOUNS

Mdnsresponder : an open source project for Apple to implement Benjour.

Bonjour : The name of Apple's open 0 configuration network standard based on the multicast Domain Name Service (multicast DNS). Bonjour technology is widely used on Mac OS as well as itunes and iphone (airplay)

zeroconf (Zero configuration Networking) : 0 Configure the Network Service specification, which is a network technology for automatic generation of available IP addresses, without the need for additional manual configuration and dedicated configuration servers. The ZEROCONF specification is presented by Apple Inc.

MDNS: Multicast domain Name Service (multicast DNS). With 5353 ports, this multicast information occurs when there is no DNS server in the intranet. MNDSS is the implementation of similar services with DNS, so that in the absence of NDS service, the host in the LAN to realize mutual discovery and communication. (The name "MDNS" was chosen because this protocol are designed to be,as much as possible, similar to conventional DNS)

Second, the realization mechanism

The Open source project Mdnsresponder supports the conversion of the service name and address of the Bonjour protocol, as well as the discovery of the service Bonjour part of the protocol. The transformation of service name and address of the Bonjour protocol and the process and DNS processes used by the discovery of services include the process of registration, service discovery, service address resolution, and connection creation, and the protocol used by the service discovery is similar to the DNS protocol. However, unlike the unicast method used by the DNS protocol, multicast is used, so it is called MDNs.

Mdnsresponder is a C code implementation, supporting a variety of platforms, on the Windows platform, it will generate a daemon mdnsresponder. On the Android platform (or the POSIX-enabled Linux platform) It is a program called MDNSD. However, both Mdnsresponder and MDNSD, app developers are simply using the APIs provided in the project to initiate requests for service registration, service queries, and service parsing, and receive processing results from them. MDNSD or Mdnsresponder as a daemon, on start-up, the user through the call Dns_sd.h API interface to achieve service registration, service query and service analysis functions

Third, the main API interface

The API for service registration is Dnsserviceregister, and the prototype is as follows.
Dnsserviceerrortype Dnssd_api Dnsserviceregister
(
Dnsserviceref *sdref,
Dnsserviceflags flags,
uint32_t InterfaceIndex,
const char *name,/* May is NULL */
const Char *regtype,
const char *domain,/* May is NULL */
const char *host,/* May is NULL */
uint16_t Port,/* In network byte order */
uint16_t Txtlen,
const void *txtrecord,/* May is NULL */
Dnsserviceregisterreply CallBack,/* May NULL */
void *context/* May is NULL */
);
The function is interpreted as follows.
Sdref represents an uninitialized Dnsservice entity whose type dnsserviceref is a pointer. The parameter is eventually allocated memory and initialized by the Dnsserviceregister function.
Flags indicates conflict handling when there is a duplicate service within the network. The default is to modify the service name sequentially. For example, to register a service named "printer", the name "printer (1)" can be renamed when conflicting duplicate names are detected.
InterfaceIndex indicates which network interfaces the service outputs to the host. A value of 1 means only native support, that is, the service is used on the loop interface.
Name indicates the name of the service and, if empty, takes the machine name.
Regtype represents a service type, expressed as a string. Bonjour requires the format "_ Service name. _ Transport Protocol", for example "_ftp._tcp". The current transport protocol only supports TCP and UDP.
Domian and host are generally empty.
Port represents the ports for the service. If the 0,bonjour is automatically assigned a.
The Txtlen and Txtrecord strings are used to describe the service.
Txtrecord format is a key-value pair (Name/value pairs) For example: 0x0A | Name=value | 0x08 | PAPER=A4 | 0x12 | Rendezvous is Cool |
The callback represents the set callback function. The result of the request that the service registers will be recalled to the client through it.
The context represents a contextual pointer that is set by the application.
When a client needs to search for a specific service within the network, it needs to use the Dnsservicebrowser API, which is prototyped as follows.
Dnsserviceerrortype Dnssd_api Dnsservicebrowse
(
Dnsserviceref *sdref,
Dnsserviceflags flags,
uint32_t InterfaceIndex,
const Char *regtype,
const char *domain,/* May is NULL */
Dnsservicebrowsereply CallBack,
void *context/* May is NULL */
);
Among them, Sdref, InterfaceIndex, Regtype, domain and context have the same meaning as dnsserviceregister. Flags have no effect in this function. Callback the callback notification interface for the Dnsservicebrowser processing result.
When the client wants to obtain the IP and port number of the specified service, it needs to use the Dnsserviceresolve API, which is prototyped as follows.
Dnsserviceerrortype Dnssd_api Dnsserviceresolve
(
Dnsserviceref *sdref,
Dnsserviceflags flags,
uint32_t InterfaceIndex,
const Char *name,
const Char *regtype,
const Char *domain,
Dnsserviceresolvereply CallBack,
void *context/* May is NULL */
);
where name, Regtype, and domain are obtained from the processing results of the DNSSERVICEBROWSE function. The callback is used to inform dnsserviceresolve of the processing results. The callback function returns the IP address and port number of the service

The implementation of Mdnsresponder on Linux

First, Project Source

http://www.opensource.apple.com/source/mDNSResponder/can only browse, no download available.

http://www.opensource.apple.com/tarballs/mDNSResponder/each version of the package file, download it directly.

This article is to choose mDNSResponder-320.10.80.

II. Introduction of Engineering catalogue

Mdnscore: Main core Protocol engine code, pure C language, each platform needs to rely on the core code.

Mdnsshared: Non-core engine code shared by multiple platforms.

Mdnsposix:posix platform-related code.

Clients: Four directories including how to use the client example code for the API provided by the background service.

Implementation in Linux requires only a few of the above directory code.

Using Mdnsposix's makefile compilation (make Os=linux) to generate the following files (/build/prod), you can modify makefile Debug=1 to generate files with debug information (/build/debug)

Compiling clients generates a DNS-SD execution file for testing and for communicating with the MNDSD service.

Where MDNSD is a background service, this service should be set up as the system starts, LIBMDNSSD is a MDNs monitoring layer (DNS-SD) used by the library LIBMDNSSD.

Dedicated equipment use files (printer, network Camera, etc)

-Mdnsclientposix

-Mdnsresponderposix

-Mdnsproxyresponderposix

To run the program on the embedded system board, you need to modify the makefile to cross-compile

Put

Ifeq ($ (findstring linux,$ (OS)), Linux)

Cflags_os =-d_gnu_source-dhave_ipv6-dnot_have_sa_len-duses_netlink-dhave_linux-dtarget_os_linux- Fno-strict-aliasing

LD = gcc–shared

Switch

CC =/OPT/MTK/GNU-TOOLCHAIN_4.8.2_2.6.35_CORTEX-A9-NEON/BIN/ARMV7A-MEDIATEK482_001_NEON-LINUX-GNUEABI-GCC

LD =/opt/mtk/gnu-toolchain_4.8.2_2.6.35_cortex-a9-neon/bin/armv7a-mediatek482_001_neon-linux-gnueabi-gcc–shared

/opt/mtk/gnu-toolchain_4.8.2_2.6.35_cortex-a9-neon/bin/armv7a-mediatek482_001_ NEON-LINUX-GNUEABI-GCC is a platform-specific compilation tool chain

Generate MDNSD to run on the board and run the generated DNS-SD./dns-sd–h you can see the test tip information for the DNS-SD test program. You can then modify the code in the DNS-SD.C to customize your own test items.

Here is a demo of registering the Airplay service and the RAOP service:

  • Demo:
  • {
  • #define Kraopport 50001
  • #define Kairplayport 50002
  • static Dnsserviceref airplayref = NULL;
  • static Dnsserviceref raopref = NULL;
  • Opaque16 Airplayport = {{Kairplayport >> 8, Kairplayport & 0xFF}};
  • Opaque16 Raopport = {{Kraopport >> 8, Kraopport & 0xFF}};
  • static const char airplaytxt[] =
  • "\x1a" "deviceid=0c:54:a5:56:9d:80" \
  • "\x0f" "features=0x3fff"; \
  • "\x10" "model=appletv3,1";
  • "\x0e" "srcvers=150.33";
  • static const char raoptxt[] =
  • "\x06" "tp=udp" \
  • "\x08" "Sm=false" \
  • "\x08" "Sv=false" \
  • "\x04" "ek=1" \
  • "\x06" "et=0,1" \
  • "\x06" "cn=0,1" \
  • "\x04" "ch=2" \
  • "\x05" "ss=16" \
  • "\x08" "sr=44100" \
  • "\x08" "Pw=false" \
  • "\x04" "vn=3" \
  • "\x09" "Txtvers=1";
  • Err = Dnsserviceregister (&airplayref, 0, Opinterface, "Jietools", "_airplay._tcp.", "", NULL, Airplayport.notaninteger, 0, NULL, reg_reply, NULL);
  • if (!err) Err = Dnsserviceupdaterecord (airplayref, NULL, 0, sizeof (airplaytxt)-1, airplaytxt, 0);
  • Err = Dnsserviceregister (&raopref, 0, Opinterface, "[email protected]", "_raop._tcp.", "", NULL, Raopport.notaninteger, 0, NULL, reg_reply, NULL);
  • if (!err) Err = Dnsserviceupdaterecord (raopref, NULL, 0, sizeof (raoptxt)-1, raoptxt, 0);
  • while (1) getchar ();
  • return 0;
  • }
  • #endif

The first two lines define the specified service port, and then the Airplaytxt and raoptxt two service descriptions, the following is a brief description of Airplaytxt:

"\x1a" This way of writing, is to add the length of the string before the value, for 16, the value behind the DeviceID is the physical address of the local network card, features this parameter can not be less, it is the Airplay service support features or capabilities described, other parameters can be ignored.

Raoptxt description of the content is I by the capture package copy down, have not modified, and then call two MDNs SDK in the two Api,dnsserviceregister for registration, Dnsserviceupdaterecord The Txtrecord information used to update the service.

Here are two sets of call service registration, it is important to note that if you want to implement the _airplay service, you must register the two services together, and the service name must be consistent, such as the fourth parameter is the service name "Jietools" and "[email protected]", Note the naming convention.

OK, no accident, run it, open your phone, you can find yourself registered in the Airplay service

Summary of issues

1. Choose the right test platform

Test should choose the right platform, because my purpose is to be ported to the Linux arm platform, so I choose the Linux environment to compile the test, the machine installed on the virtual machine Ubuntu, the compilation test on Ubuntu, compile and run no problem, but the mobile phone can not find the device, The use of the Project Readme test method: Mdnsresponderposix Mdnsclientposix test also did not work, the final is to recompile and put on the board to run, the mobile phone can find the registered service. Test platform cannot select virtual machine
2. How to read the printed information during the test
By default, print information is saved to/var/log/system.log, with the debug parameter (mdnsd-debug) running MDNSD, or by changing the makefile compiler to debug=1 to see the PrintOut on the window. Debugging process is recommended to print out the debug information, easy to track problems.

3, call function Dnsserviceregister will return-65549

Return-65549 is the error code, which is the case that a version of the parameter is wrong, in general, the Txtrecord parameter error prone, debug information prompts

Sep 16:06:13 localhost mdnsresponder[192]: Attempt to register record with invalid Rdata:17 Ice cube._http._tcp.local. TXT ath=/index.html

The format of the TXT record is: Length key value pair length key value pair (length byte, data, length byte, data)

The length is 16 binary representation, the key value pair is = The Left is a string, the right is a value, there is no interval between key-value pairs such as: \011txtvers=1\020path=/index.html\025note=bonjour is cool!

4. To discover services on iOS airplay, some proprietary parameters are required

For arguments to the Dnsserviceregister function, the Regtype parameter must be the service name. _ Transport protocol, and only TCP and UDP are supported, other parameters do not have special requirements. But if you want iOS to be able to discover services, _AIRPLAY._TCP and _RAOP._TCP Two services are registered, and two service name is the same, such as: Airplay's service name is Hzztools, then the RAOP service name is [email Protected], where 0C54A5569D80 is the MAC address. Parameter Txtrecord two parameters are required, DeviceID the physical address and features of the native NIC

5, after the registration service also need to update the service information

After calling Dnsserviceregister to register the service, you also need to call Dnsserviceupdaterecord to update the Txtrecord information for the service. This way the iOS can discover the service.

6, in the test process in addition to open the debug signal to track, you can also use the grab kit tools (such as: Wireshark) to analyze

Through the use of the capture tool to analyze, you can most directly analyze the network communication between the devices. Skilled use of tools can follow quickly to the problem.

Airplay realization and problem summary of MDNs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.