赞
踩
前面在学习局域网设备发现的时候,学习了UPnP协议,学习到了里面的设备发现协议SSDP,SSDP设备发现。
最近又发现了一种常见的协议,用来发现局域网内的设备及服务,就是mDNS协议,这是一种零配置服务。
mDNS即多播DNS(Multicast DNS),mDNS主要实现了在没有传统DNS服务器的情况下使局域网内的主机实现相互发现和通信,使用的端口为5353,遵从dns协议,使用现有的DNS信息结构、名语法和资源记录类型。并且没有指定新的操作代码或响应代码。
在局域网中,设备和设备之前相互通信需要知道对方的ip地址的,大多数情况,设备的ip不是静态ip地址,而是通过dhcp 协议动态分配的ip 地址,如何设备发现呢,就是要mDNS大显身手,例如:现在物联网设备和app之间的通信,要么app通过广播,要么通过组播,发一些特定信息,然后某些设备应答,实现局域网设备的发现,当然mDNS比这强大的多。
组播地址: 组播地址使用的是D类地址,地址范围为:224.0.0.0—239.255.255.255
mdns 使用组播地址为: 224.0.0.251 (ipv6: FF02::FB) 端口为5353,mdns 是用于局域网内部的,并且主机的域名为.local 结尾,每个进入局域网的主机,如果开启了mDNS服务的话,都会向局域网内的所有主机组播一个消息,我是谁(域名),和我的IP地址是多少。然后其他有mdns服务的主机就会响应,也会告诉你,它是谁(域名),它的IP地址是多少。 当然设备需要服务时,就是使用mdns 查询域名对对应的ip地址,对应的设备收到该报文后同样通过组播方式应答,此时其他主机设备也是可以收到该应答报文,其他主机也会记录域名和ip 以及ttl 等,更新缓存。
比如,A主机进入局域网,开启了 mDNS 服务,并向 mDNS 服务注册以下信息:我提供 FTP 服务,我的IP是 192.168.1.101,端口是 21。当B主机进入局域网,并向 B 主机的 mDNS 服务请求,我要找局域网内 FTP 服务器,B主机的 mDNS 就会去局域网内向其他的 mDNS 询问,并且最终告诉你,有一个IP地址为 192.168.1.101,端口号是 21 的主机,也就是 A 主机提供 FTP 服务,所以 B 主机就知道了 A 主机的 IP 地址和端口号了。
通过抓取报文,可以发现他们的交流方式,就是通过多播的方式,端口为5353。
不知道是什么情况,报文是黑红色,感觉像是有些问题。但是逻辑上来看是和之前的描述类似,本机是192.168.32.202,首先上线,发送了自己的信息,然后本网络内的192.168.32.254设备做出了响应,返回了自己的属性,然后202又发送了自己的信息,最后254也返回了自己的所有服务。
(纯属猜测),等学习完DNS协议回来再做更改。
在linux下,测试mDNS,利用的avahi工具组合,其中最重要的就是avahi-browse命令
下面是一个测试返回结果
[root@localhost ~]# avahi-browse -a -r -t -v 服务器版本:avahi 0.6.31;主机名:linux.local E Ifce Prot 名称 类型 域 + enp1s0 IPv4 Jenkins [00:50:56:b8:80:44] Workstation local + enp1s0 IPv4 linux [94:09:d3:10:33:49] Workstation local + enp1s0 IPv4 ubuntu [00:0c:29:80:19:3f] Workstation local + enp1s0 IPv4 linux-2 [f0:d4:e2:e8:e4:40] Workstation local + enp1s0 IPv4 Canon iR2004/2204 _scanner._tcp local + enp1s0 IPv4 Canon iR2004/2204 _uscan._tcp local + enp1s0 IPv4 Canon iR2004/2204 _ipps._tcp local + enp1s0 IPv4 Canon iR2004/2204 Internet Printer local + enp1s0 IPv4 Canon iR2004/2204 Web Site local + enp1s0 IPv4 Canon iR2004/2204 UNIX Printer local + enp1s0 IPv4 Canon iR2004/2204 PDL Printer local = enp1s0 IPv4 linux-2 [f0:d4:e2:e8:e4:40] Workstation local hostname = [linux-2.local] address = [192.168.32.200] port = [9] txt = [] = enp1s0 IPv4 Jenkins [00:50:56:b8:80:44] Workstation local hostname = [Jenkins.local] address = [192.168.32.7] port = [9] txt = [] = enp1s0 IPv4 linux [94:09:d3:10:33:49] Workstation local hostname = [linux.local] address = [192.168.32.202] port = [9] txt = [] = enp1s0 IPv4 ubuntu [00:0c:29:80:19:3f] Workstation local hostname = [ubuntu.local] address = [192.168.32.231] port = [9] txt = [] = enp1s0 IPv4 Canon iR2004/2204 PDL Printer local hostname = [Canonb8c6d4.local] address = [192.168.32.254] port = [9100] txt = ["UUID=01c80000-fe7f-11de-8a39-f80d60b8c6d4" "PaperMax=tabloid-A3" "Punch=0" "Staple=F" "Sort=T" "Collate=T" "Bind=F" "PaperCustom=T" "Duplex=T" "Copies=T" "Color=F" "TBCP=F" "Binary=F" "Transparent=F" "usb_MDL=iR2004/2204" "usb_MFG=Canon" "adminurl=http://Canonb8c6d4.local:80/portal_top.html" "pdl=application/octet-stream" "product=(CNiR2004/2204)" "ty=Canon iR2004/2204" "priority=10" "qtotal=1" "note=" "txtvers=1"] = enp1s0 IPv4 Canon iR2004/2204 UNIX Printer local hostname = [Canonb8c6d4.local] address = [192.168.32.254] port = [515] txt = ["UUID=01c80000-fe7f-11de-8a39-f80d60b8c6d4" "PaperMax=tabloid-A3" "Punch=0" "Staple=F" "Sort=T" "Collate=T" "Bind=F" "PaperCustom=T" "Duplex=T" "Copies=T" "Color=F" "TBCP=F" "Binary=F" "Transparent=F" "usb_MDL=iR2004/2204" "usb_MFG=Canon" "adminurl=http://Canonb8c6d4.local:80/portal_top.html" "pdl=application/octet-stream" "product=(CNiR2004/2204)" "ty=Canon iR2004/2204" "priority=10" "qtotal=1" "note=" "rp=auto" "txtvers=1"] = enp1s0 IPv4 Canon iR2004/2204 Web Site local hostname = [Canonb8c6d4.local] address = [192.168.32.254] port = [80] txt = ["path=/"] = enp1s0 IPv4 Canon iR2004/2204 Internet Printer local hostname = [Canonb8c6d4.local] address = [192.168.32.254] port = [631] txt = ["kind=document,envelope,label" "mopria-certified=1.2" "print_wfds=T" "URF=V1.4,W8,RS300,IS19-20-21,OB10,CP255,DM1,IFU0,PQ4" "usb_CMD=LIPSLX,CPCA" "UUID=01c80000-fe7f-11de-8a39-f80d60b8c6d4" "TLS=1.2" "air=none" "PaperMax=tabloid-A3" "Punch=0" "Staple=F" "Sort=F" "Collate=F" "Bind=F" "PaperCustom=T" "Duplex=T" "Copies=T" "Fax=F" "Scan=T" "Color=F" "usb_MDL=iR2004/2204" "usb_MFG=Canon" "adminurl=http://Canonb8c6d4.local:80/airprint.html" "pdl=application/octet-stream,application/pdf,image/jpeg,image/pwg-raster,image/urf" "product=(CNiR2004/2204)" "ty=Canon iR2004/2204" "priority=5" "qtotal=1" "note=" "rp=ipp/print" "txtvers=1"] = enp1s0 IPv4 Canon iR2004/2204 _ipps._tcp local hostname = [Canonb8c6d4.local] address = [192.168.32.254] port = [443] txt = ["kind=document,envelope,label" "mopria-certified=1.2" "print_wfds=T" "URF=V1.4,W8,RS300,IS19-20-21,OB10,CP255,DM1,IFU0,PQ4" "usb_CMD=LIPSLX,CPCA" "UUID=01c80000-fe7f-11de-8a39-f80d60b8c6d4" "TLS=1.2" "air=none" "PaperMax=tabloid-A3" "Punch=0" "Staple=F" "Sort=F" "Collate=F" "Bind=F" "PaperCustom=T" "Duplex=T" "Copies=T" "Fax=F" "Scan=T" "Color=F" "usb_MDL=iR2004/2204" "usb_MFG=Canon" "adminurl=http://Canonb8c6d4.local:80/airprint.html" "pdl=application/octet-stream,application/pdf,image/jpeg,image/pwg-raster,image/urf" "product=(CNiR2004/2204)" "ty=Canon iR2004/2204" "priority=10" "qtotal=1" "note=" "rp=ipp/print" "txtvers=1"] = enp1s0 IPv4 Canon iR2004/2204 _uscan._tcp local hostname = [Canonb8c6d4.local] address = [192.168.32.254] port = [80] txt = ["duplex=T" "is=platen,adf" "cs=color,grayscale" "UUID=01c80000-fe7f-11de-8a39-f80d60b8c6d4" "pdl=application/octet-stream,application/pdf,image/jpeg" "note=" "ty=Canon iR2004/2204" "rs=eSCL" "representation=http://Canonb8c6d4:80/media/icon_128x128.png" "adminurl=http://Canonb8c6d4:80/index.html" "vers=2.5" "txtvers=1"] = enp1s0 IPv4 Canon iR2004/2204 _scanner._tcp local hostname = [Canonb8c6d4.local] address = [192.168.32.254] port = [8610] txt = ["scannerAvailable=1" "MDL=iR2004/2204" "MFG=Canon" "UUID=F80D60B8C6D4" "ty=Canon iR2004/2204" "adminurl=http://Canonb8c6d4.local:80/airprint.html" "note=" "txtvers=1"] :缓存用尽 + enp1s0 IPv4 Canon iR2004/2204 _privet._tcp local = enp1s0 IPv4 Canon iR2004/2204 _privet._tcp local hostname = [Canonb8c6d4.local] address = [192.168.32.254] port = [80] txt = ["cs=offline" "id=" "type=printer" "url=https://www.google.com/cloudprint" "note=" "ty=Canon iR2004/2204" "txtvers=1"] :当前所有
其中+开头的是总述,-开头的是详细描述。
总述里面按照表头,可以看到设备所在的域和设备类型。详细描述里面,可以看到设备开启的每个服务与对应端口。
设备类型通过avahi-browse -b命令可以获得
PulseAudio Sound Server PostgreSQL Server Apple TimeMachine WebDAV File Share Timbuktu Remote Desktop Control Adobe Acrobat VNC Remote Access Workstation Digital Photo Sharing Mumble Server APT Package Repository Virtual Machine Manager SSH Remote Terminal Subversion Revision Control Telnet Remote Terminal IMAP Mail Access RTP Realtime Streaming Server Secure WebDAV File Share iTunes Remote Control Apple AirPort UNIX Printer SFTP File Transfer DVD or CD Sharing Remote Disk Management iChat Presence Posta - POP3 Asterisk Exchange Web Syndication RSS Adobe Version Cue Window Shifter PDL Printer Apple Home Sharing DNS Server Microsoft Windows Network VLC Streaming OmniWeb Bookmark Sharing iTunes Audio Access KDE System Guard GnuPG/PGP HKP Key Server Distributed Compiler Bazaar iPod Touch Music Library Secure Web Site Web Site Thousand Parsec Server (Secure HTTP Tunnel) NTP Time Server Skype VoIP AirTunes Remote Audio Apple Net Assistant PulseAudio Sound Sink Network File System H.323 Telephony OLPC Presence Thousand Parsec Server (Secure) RealPlayer Shared Favorites RTSP Realtime Streaming Server PulseAudio Sound Source Apple File Sharing Remote Jukebox Internet Printer TFTP Trivial File Transfer Music Player Daemon Gobby Collaborative Editor Session Thousand Parsec Server (HTTP Tunnel) SIP Telephony LDAP Directory Server MacOS X Duplicate Machine Suppression Thousand Parsec Server FTP File Transfer SubEthaEdit Collaborative Text Editor
从前面这段消息中,我们就可以简单的发现,192.168.32.254这个打印机,开启了多个打印服务,对应不同的端口。
通过更换为参数-p,可以得到如下分号分割的方便解析的结果
服务的发布可以参考一位博主的文章
avahi发送mdns服务
刚刚过完中秋节,三天下了两天雨,疫情又有所抬头,希望不要影响大家国庆回家,让在外漂泊的孩子们都能回家看看吧。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。