欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

记一次Multicast DNS(mdns)的问题-(linux 下ifconfig)

程序员文章站 2022-04-17 08:47:55
前言:近期在查询mdns的问题过程一波三折,问题总结下来可以分为三块,都是比较基础性的问题: 1.网关ifconfig查询地址 2.多个程序同时占用一个端口号占用编码问题 3.mdn...

前言:近期在查询mdns的问题过程一波三折,问题总结下来可以分为三块,都是比较基础性的问题:

1.网关ifconfig查询地址

2.多个程序同时占用一个端口号占用编码问题

3.mdns协议及实现源码

mdns是用于局域网发现的一种协议,网关搭载的linux系统,首先得确定其使用的是哪个地址作为通讯,说一个最简单的指令:ifconfig,虽然早就知道这个指令,但是其实这个指令包含比较多的基础知识。

用我使用的openwrt路由器输入ifconfig:

br-lan    Link encap:Ethernet  HWaddr 04:A1:51:9A:03:67  
          inet addr:198.100.100.1  Bcast:198.100.100.255  Mask:255.255.255.0
          inet6 addr: fdbb:e076:9029::1/60 Scope:Global
          inet6 addr: fe80::6a1:51ff:fe9a:367/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:37358673 errors:0 dropped:0 overruns:0 frame:0
          TX packets:49805372 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:10001210378 (9.3 GiB)  TX bytes:49536338006 (46.1 GiB)

eth0      Link encap:Ethernet  HWaddr BE:7F:7D:5D:09:D9  
          inet6 addr: fe80::bc7f:7dff:fe5d:9d9/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:71274265 errors:0 dropped:0 overruns:383 frame:0
          TX packets:67972711 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:23375869 (22.2 MiB)  TX bytes:4047624223 (3.7 GiB)
          Interrupt:4 

eth0.1    Link encap:Ethernet  HWaddr BE:7F:7D:5D:09:D9  
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:27485423 errors:0 dropped:0 overruns:0 frame:0
          TX packets:32246955 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:8388027931 (7.8 GiB)  TX bytes:32142311436 (29.9 GiB)

eth0.2    Link encap:Ethernet  HWaddr 04:A1:51:9A:03:68  
          inet addr:193.168.1.125  Bcast:193.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::6a1:51ff:fe9a:368/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:43788842 errors:0 dropped:0 overruns:0 frame:0
          TX packets:35727507 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:46186987470 (43.0 GiB)  TX bytes:10288278423 (9.5 GiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:10489 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10489 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:1066479 (1.0 MiB)  TX bytes:1066479 (1.0 MiB)

wlan0     Link encap:Ethernet  HWaddr 04:A1:51:9A:03:67  
          inet6 addr: fe80::6a1:51ff:fe9a:367/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:4839004 errors:0 dropped:0 overruns:0 frame:0
          TX packets:5852922 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:670518236 (639.4 MiB)  TX bytes:1589348208 (1.4 GiB)

wlan1     Link encap:Ethernet  HWaddr 04:A1:51:9A:03:69  
          inet6 addr: fe80::6a1:51ff:fe9a:369/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:5443036 errors:0 dropped:0 overruns:0 frame:0
          TX packets:9474688 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:1115674576 (1.0 GiB)  TX bytes:1268032438 (1.1 GiB)

此处br-lan,eth0,eth0.1…代表的是什么意思呢?

通过查阅,知道了其实在系统文件(network ,wireless)中针对有线和无线早有配置。

root@OpenWrt:/etc/config# cat network 

config interface 'loopback'
        option ifname 'lo'
        option proto 'static'
        option ipaddr '127.0.0.1'
        option netmask '255.0.0.0'

config globals 'globals'
        option ula_prefix 'fdbb:e076:9029::/48'

config interface 'lan'
        option ifname 'eth0.1'
        option force_link '1'
        option type 'bridge'
        option proto 'static'
        option netmask '255.255.255.0'
        option ip6assign '60'
        option macaddr '04:a1:51:9a:03:67'
        option ipaddr '198.100.100.1'

config interface 'wan'
        option ifname 'eth0.2'
        option proto 'dhcp'
        option macaddr '04:a1:51:9a:03:68'

config interface 'wan6'
        option ifname 'eth0.2'
        option proto 'dhcpv6'

config switch
        option name 'switch0'
        option reset '1'
        option enable_vlan '1'

config switch_vlan
        option device 'switch0'
        option vlan '1'
        option ports '0t 1 2 3 4'

config switch_vlan
        option device 'switch0'
        option vlan '2'
        option ports '0t 5'

root@OpenWrt:/etc/config# cat wireless 

config wifi-device 'radio0'
        option type 'mac80211'
        option hwmode '11g'
        option path 'platform/ar934x_wmac'
        option htmode 'HT20'
        option txpower '22'
        option country 'US'
        option channel '6'

config wifi-device 'radio1'
        option type 'mac80211'
        option channel '36'
        option hwmode '11a'
        option path 'pci0000:00/0000:00:00.0'
        option htmode 'HT20'
        option txpower '14'
        option country 'US'

config wifi-iface
        option device 'radio1'
        option network 'lan'
        option mode 'ap'
        option ssid 'jthlw-cj5G'
        option encryption 'psk-mixed'
        option key '11223344'

config wifi-iface
        option device 'radio0'
        option mode 'ap'
        option network 'lan'
        option ssid 'jthlw-cj'
        option encryption 'psk-mixed'
        option key 'cjcjcj12345'

首先粘贴一张盗版的openwrt的架构:

记一次Multicast DNS(mdns)的问题-(linux 下ifconfig)

网关的两个网卡:eth0,eht2分别为一个有线一个无线网卡,这是两个真实的网卡,是有对应的mac地址的。

通过配置

config interface 'lan'

       option ifname 'eth0.1'

       option force_link '1'

       option type 'bridge'

       option proto 'static'

       option netmask '255.255.255.0'

       option ip6assign '60'

       option macaddr '04:a1:51:9a:03:67'

       option ipaddr '198.100.100.1'

 

config interface 'wan'

       option ifname 'eth0.2'

       option proto 'dhcp'

       option macaddr '04:a1:51:9a:03:68'

可以看出:eth0是有线网卡,eth0.1,eth0.2从上面通过switch虚拟出来,eth0.1对应LAN口,eth0.2对应WAN口,其地址都可以手动修改。

而radio0, radio1对应的是无线网卡(2.4G ,5G)

而br-lan是虚拟出来的,用于统一管理LAN

root@OpenWrt:/etc/config# brctl show
bridge name     bridge id               STP enabled     interfaces
br-lan          7fff.04a1519a0367       no              eth0.1
                                                        wlan1
                                                        wlan0

所以可以看出,eth0.2用于WAN口,其上一级的IP为:193.168.1.125;eth0.1用于管理LAN口,子LAN口的IP都由198.100.100.1进行分配。

(以上只是自己对网关很浅的一些认识,更多的可以关注下面引用的博客,并且诸如防火墙(firewall的设置可以参见:、etc/config 下面的firewall文件)))