网络编程之Broadcasting(一)

时间:2021-12-31 10:24:21

1. Introduction

网络编程之Broadcasting(一)

  上图所体现的一些重点:

  • Multicasting support is optional in IPv4, but mandatory in IPv6.
  • Broadcasting support is not provided in IPv6. Any IPv4 application that uses broadcasting must be recoded for IPv6 to use multicasting instead.
  • Broadcasting and multicasting require datagram transport such as UDP or raw IP; they cannot work with TCP.TCP并不支持广播和组播,这点和它自身需要建立点对点的连接特性有关。

  广播的作用:

  • One use for broadcasting is to locate a server on the local subnet when the server is assumed to be on the local subnet but its unicast IP address is not known.确定主机的位置,此时使用广播,根据返回的数据包来判断
  • Another use is to minimize the network traffic on a LAN when there are multiple clients communicating with a single server.使用广播减少网络数据量,不需要逐一去和client进行通信
      

  广播的实际例子,一些例子可以组播代替:

  • ARP — Although this is a protocol that lies underneath IPv4, and not a user application, ARP broadcasts a request on the local subnet that says, ‘‘Will the system with an IP address of a.b.c.d please identify yourself and tell me your hardware address?’’ ARP uses link-layer broadcast, not IP-layer, but is an example of a use of broadcasting.ARP才前面也见过,其使用数据链路层的广播发送请求,获取目标主机的物理地址。
  • DHCP — The client assumes a server or relay is on the local subnet and sends its request to the broadcast address.
  • Network Time Protocol (NTP) – But on a broadcast LAN, instead of making each of the clients poll a single server, the server can broadcast the current time every 64 seconds for all the clients on the local subnet, reducing the amount of network traffic.
  • Routing daemons – The oldest routing daemon, routed, which implements RIP version 1, broadcasts its routing table on a LAN. This feature can also be used by hosts on the LAN listening to these routing announcements and updating their routing tables accordingly. RIP version 2 permits the use of either multicast or broadcast.

2. Broadcast Addresses

2.1 Subnet-directed broadcast address

  广播地址有两种形式,第一种形式,例如:xx.xx.xx.255/24{subnetid, hostid}中的hostid全为1:
  网络编程之Broadcasting(一)

 关于上图,几点值得强调的地方:

  • The router receives a unicast IP datagram on the 192.168.123/24 subnet with a destination address of 192.168.42.255 (the subnet-directed broadcast address of another interface). The router normally does not forward the datagram on to the 192.168.42/24 subnet. Some systems have a configuration option that allows subnet-directed broadcasts to be forwarded.对于另一个接口的广播数据包,路由器默认并不转发该数据包。
  • Forwarding subnet-directed broadcasts enables a class of denial-of-service attacks called ‘‘amplification’’ attacks; for instance, sending an ICMP echo request to a subnet-directed broadcast address can cause multiple replies to be sent for a single request. Combined with a forged IP source address, this results in a bandwidth utilization attack against the victim system, so it’s advisable to leave this configuration option off.第二点给出了不希望转发子网广播地址数据包的原因

2.2 Limited broadcast address

  这种广播地址,具有固定的格式255.255.255.255,UNP关于该种类型的广播地址,做出了以下几点说明:

  • 255.255.255.255 is to be used as the destination address during the bootstrap process by applications such as BOOTP and DHCP, which do not yet know the node’s IP address.
  • What does a host do when an application sends a UDP datagram to 255.255.255.255?
    • Most hosts allow this (assuming the process has set the SO_BROADCAST socket option) and convert the destination address to the subnet-directed broadcast address of the outgoing interface.
  • What does a multihomed host do when the application sends a UDP datagram to 255.255.255.255?
    • Some systems send a single broadcast on the primary interface (the first interface that was configured) with the destination IP address set to the subnet-directed
      broadcast address of that interface (p. 736 of TCPv2).
    • Other systems send one copy of the datagram out from each broadcast-capable interface.
    • For portability, however, if an application needs to send a broadcast out from all broadcast-capable interfaces, it should obtain the interface configuration and do one sendto for each broadcast-capable interface with the destination set to that interface’s broadcast address.

3. Unicast versus Broadcast

3.1 Unicast

网络编程之Broadcasting(一)
  
  该部分内容还是蛮重要的,可以大致了解整个网络的数据发送过程,整理UNP中的内容如下,首先看单播:

  • The subnet address of the Ethernet is 192.168.42/24 with 24 bits in the network mask, leaving 8 bits for the host ID. The application on the left host calls sendto on a UDP socket, sending a datagram to 192.168.42.3, port 7433.
  • The UDP layer prepends a UDP header and passes the UDP datagram to the IP layer. IP prepends an IPv4 header, determines the outgoing interface, and in the case of an Ethernet, ARP is invoked to map the destination IP address to the corresponding Ethernet address: 00:0a:95:79: bc:b4. The packet is then sent as an Ethernet frame with that 48-bit address as the destination Ethernet address. The frame type field of the Ethernet frame will be 0x0800, specifying an IPv4 packet. The frame type for an IPv6 packet is 0x86dd.该数据包顺着stack逐层添加相关协议头部,最后调用ARP获取目标地址的MAC地址。

  • The Ethernet interface on the host in the middle sees the frame pass by and compares the destination Ethernet address to its own Ethernet address 00:04:ac:17:bf: 38). Since they are not equal, the interface ignores the frame. With a unicast frame, there is no overhead whatsoever to this host. The interface ignores the frame.中间的主机会将数据包中的MAC地址和自己的MAC地址对比,如果不同,则忽略该数据包。

  • The Ethernet interface on the host on the right also sees the frame pass by, and when it compares the destination Ethernet address with its own Ethernet address, they are equal. This interface reads in the entire frame, probably generates a hardware interrupt when the frame is complete, and the device driver reads the frame from the interface memory. Since the frame type is 0x0800, the packet is placed on the IP input queue.在最右边的主机,发现数据包中的MAC地址和自己的MAC地址匹配,将会进一步处理该数据包,并递送到IP层输入队列中。
  • When the IP layer processes the packet, it first compares the destination IP address (192.168.42.3) with all of its own IP addresses. Since the destination address is one of the host’s own IP addresses, the packet is accepted.在IP层的时候,对比数据包中的IP地址和自身的IP地址,如果匹配则进一步处理。
  • The IP layer then looks at the protocol field in the IPv4 header, and its value will be 17 for UDP. The IP datagram is passed to UDP. The UDP layer looks at the destination port (and possibly the source port, too, if the UDP socket is connected), and in our example, places the datagram onto the appropriate socket receive queue. The process is awakened, if necessary, to read the newly received datagram.在IP层查看数据包中相关协议域,经过处理后,再传递到UDP或者TCP层中,再由传输层的协议将数据包放置相关socket的接收队列中。

3.2 Broadcast

  接着看广播方面的内容:
网络编程之Broadcasting(一)

  • When the host on the left sends the datagram, it notices that the destination IP address is the subnet-directed broadcast address and maps this into the Ethernet address of 48 one bits: ff:ff:ff:ff:ff:ff. This causes every Ethernet interface on the subnet to receive the frame. The two hosts on the right of this figure that are running IPv4 will both receive the frame.在广播的时候,该广播地址对应的MAC地址为ff:ff:ff:ff:ff:ff,意味着在该子网的其他主机都会接收该数据包
  • Since the Ethernet frame type is 0x0800, both hosts pass the packet to the IP layer. Since the destination IP address matches the broadcast address for each of the two hosts, and since the protocol field is 17 (UDP), both hosts pass the packet up to UDP.上图右边的两个主机都会将数据包传递到UDP层
  • The host on the right passes the UDP datagram to the application that has bound UDP port 520. Nothing special needs to be done by an application to receive a broadcast UDP datagram: It just creates a UDP socket and binds the application’s port number to the socket. (We assume the IP address bound is INADDR_ANY, which is typical.) On the host in the middle, no application has bound UDP port 520. The host’s UDP code then discards the received datagram. 中间的主机因为UDP端口号的不同,而丢弃了数据包

  在该例子中,UNP强调了以下几点:

  • In this example, we also show the datagram that is output by the host on the left being delivered to itself. This is a property of broadcasts: By definition, a broadcast goes to every host on the subnet, which includes the sender.根据广播的定义,发送广播的主机也会收到该广播数据包
  • We also assume that the sending application has bound the port that it is sending to (520), so it will receive a copy of each broadcast datagram it sends. 上图假设发送的广播的主机绑定的UDP端口也为520
  • Every IPv4 host on the subnet that is not participating in the application must completely process the broadcast UDP datagram all the way up the protocol stack, through and including the UDP layer, before discarding the datagram.上图所反映的一个问题:虽然中间的主机端口不对,但是他自身也完整的处理了UDP的数据包,到UDP层才丢弃该数据包。
  • every non-IP host on the subnet must also receive the entire frame at the datalink layer before discarding the frame (assuming the host does not support the frame type, which would be 0x0800 for an IPv4 datagram). For applications that generate IP datagrams at a high rate (audio or video, for example), this unnecessary processing can severely affect these other hosts on the subnet.如果主机的应用产生了大量的广播数据包,这对其他不相关的主机的性能也会造成影响。