【Network】TCP/IP

Posted by 西维蜀黍 on 2019-05-08, Last Modified on 2024-05-02

什么是 TCP/IP

The OSI model describes an idealized network communications with a family of protocols. TCP/IP does not correspond to this model directly.

TCP/IP either combines several OSI layers into a single layer, or does not use certain layers at all. The following table shows the layers of the Solaris implementation of TCP/IP. The table lists the layers from the topmost layer (application) to the lowest (physical network).

OSI Ref. Layer No. OSI Layer Equivalent TCP/IP Layer TCP/IP Protocol Examples
5,6,7 Application, session, presentation Application NFS, NIS+, DNS, telnet, ftp, rlogin, rsh, rcp, RIP, RDISC, SNMP, and others
4 Transport Transport TCP, UDP
3 Network Internet (Network) IP, ARP, ICMP
2, 1 Data link, Physical Data link PPP, Ethernet IEEE 802.2

链路层(Link Layer),有时也称作数据链路层(Data-link Layer)网络接口层(Network Interface Layer),通常包括操作系统中的设备驱动程序和计算机中对应的网络接口卡。它们一起处理与电缆(或其他任何传输媒介)的物理接口细节。

Frame

以太帧(Ethernet Frame)

网络层(Internet Layer)

网络层, 有时也称作互联网层, 处理分组在网络中的活动, 例如分组的选路。

在 TCP/IP协议族中,网络层协议包括 IP 协议(网际协议),ICMP协议(In ternet互联网控制报文协议),以及IGMP协议(Internet组管理协议)。

IP datagrams(IP 数据报)

链路层(Link Layer / Data-link Layer / Network Interface Layer)

The data-link layer identifies the network protocol type of the packet, in this instance TCP/IP.

The data-link layer defines details of how data is physically sent through the network, including how bits are electrically or optically signaled by hardware devices that interface directly with a network medium, such as coaxial cable, optical fiber, or twisted pair copper wire.

The data-link layer also provides erlror control and “framing.”

Examples of data-link layer protocols are Ethernet IEEE 802.2 framing and Point-to-Point Protocol (PPP) framing.

IEEE 802.3 Ethernet Frame Format

网络层/互联网层(Internet Layer)

网络层, 有时也称作互联网层, 处理分组在网络中的活动, 例如分组的选路。

This layer, also known as the network layer, accepts and delivers packets for the network.

Internet layer pack data into data packets known as IP datagrams, which contain source and destination address (logical address or IP address) information that is used to forward the datagrams between hosts and across networks. The Internet layer is also responsible for routing of IP datagrams.

This layer includes the powerful Internet Protocol (IP), the Address Resolution Protocol (ARP), and the Internet Control Message Protocol (ICMP).

IP Protocol

The IP protocol and its associated routing protocols are possibly the most significant of the entire TCP/IP suite. IP is responsible for the following:

  • IP addressing – The IP addressing conventions are part of the IP protocol. Chapter 3, Planning Your TCP/IP Network (Task) describes IPv4 addressing in detail and Chapter 14, IPv6 (Overview) describes IPv6 addressing in detail.
  • Host-to-host communications – IP determines the path a packet must take, based on the receiving host’s IP address.
  • Packet formatting – IP assembles packets into units that are known as IP datagrams. Datagrams are fully described in Internet Layer.
  • Fragmentation – If a packet is too large for transmission over the network media, IP on the sending host breaks the packet into smaller fragments. IP on the receiving host then reconstructs the fragments into the original packet.

ARP Protocol

The Address Resolution Protocol (ARP) conceptually exists between the data-link and Internet layers. ARP assists IP in directing datagrams to the appropriate receiving host by mapping Ethernet addresses (48 bits long) to known IP addresses (32 bits long).

我们知道每一块以太网卡都有一个MAC地址,这个地址是唯一的,那么IP包是如何知道这个MAC地址的?这就是ARP协议的工作。

ARP(地址解析)协议是一种解析协议,本来主机是完全不知道这个IP对应的是哪个主机的哪个接口,当主机要发送一个IP包的时候,会首先查一下自己的ARP高速缓存(就是一个IP-MAC地址对应表缓存),如果查询的IP-MAC值对不存在,那么主机就向网络发送一个ARP协议广播包,这个广播包里面就有待查询的IP地址,而直接收到这份广播的包的所有主机都会查询自己的IP地址,如果收到广播包的某一个主机发现自己符合条件,那么就准备好一个包含自己的MAC地址的ARP包传送给发送ARP广播的主机,而广播主机拿到ARP包后会更新自己的ARP缓存(就是存放IP-MAC对应表的地方)。发送广播的主机就会用新的ARP缓存数据准备好数据链路层的的数据包发送工作。

arp -a 可以查询自己的arp缓存

这样的高速缓存是有时限的,一般是20分钟(伯克利系统的衍生系统)。

ICMP Protocol

Internet Control Message Protocol (ICMP) detects and reports network error conditions. ICMP reports on the following:

  • Dropped packets – Packets that arrive too fast to be processed
  • Connectivity failure – A destination host that cannot be reached)
  • Redirection – Redirecting a sending host to use another router

The ping Command contains more information on the operating system commands that use ICMP for error detection.

ping

ping可以说是ICMP的最著名的应用,是TCP/IP协议的一部分。利用“ping”命令可以检查网络是否连通,可以很好地帮助我们分析和判定网络故障。

例如:当我们某一个网站上不去的时候。通常会ping一下这个网站。ping会回显出一些有用的信息。一般的信息如下:

ping这个单词源自声纳定位,而这个程序的作用也确实如此,它利用ICMP协议包来侦测另一个主机是否可达。原理是用类型码为0的ICMP发请 求,受到请求的主机则用类型码为8的ICMP回应。

ping程序来计算间隔时间,并计算有多少个包被送达。用户就可以判断网络大致的情况。我们可以看到, ping给出来了传送的时间和TTL的数据。

Traceroute

Traceroute是用来侦测主机到目的主机之间所经路由情况的重要工具,也是最便利的工具。

Traceroute的原理是非常非常的有意思,它收到到目的主机的IP后,首先给目的主机发送一个TTL=1的UDP数据包,而经过的第一个路由器收到这个数据包以后,就自动把TTL减1,而TTL变为0以后,路由器就把这个包给抛弃了,并同时产生一个主机不可达的ICMP数据报给主机。主机收到这个数据报以后再发一个TTL=2的UDP数据报给目的主机,然后刺激第二个路由器给主机发ICMP数据报。如此往复直到到达目的主机。这样,traceroute就拿到了所有的路由器IP。

##DHCP

如果逐一为每一台主机设置 IP 地址会是非常繁琐的事情。特别是在移动使用笔记本电脑、只能终端以及平板电脑等设备时,每移动到一个新的地方,都要重新设置 IP 地址。

于是,为了实现自动设置 IP 地址、统一管理 IP 地址分配,就产生了 DHCP(Dynamic Host Configuration Protocol)协议。有了 DHCP,计算机只要连接到网络,就可以进行 TCP/IP 通信。也就是说,DHCP 让即插即用变得可能。

DHCP 不仅在 IPv4 中,在 IPv6 中也可以使用。

NAT

  • NAT(Network Address Translator)是用于在本地网络中使用私有地址,在连接互联网时转而使用全局 IP 地址的技术。
  • 除转换 IP 地址外,还出现了可以转换 TCP、UDP 端口号的 NAPT(Network Address Ports Translator)技术,由此可以实现用一个全局 IP 地址与多个主机的通信。
  • NAT(NAPT)实际上是为正在面临地址枯竭的 IPv4 而开发的技术。不过,在 IPv6 中为了提高网络安全也在使用 NAT,在 IPv4 和 IPv6 之间的相互通信当中常常使用 NAT-PT。

传输层(Transport Layer)

传输层主要为两台主机上的应用程序提供端到端的通信。

在 TCP/IP协议族中, 有两个互不相同的传输协议:TCP(传输控制协议)和UDP(用户数据报协议)。

  • TCP为两台主机提供高可靠性的数据通信。它所做的工作包括把应用程序交给它的数据分成合适的小块交给下面的网络层,确认接收到的分组,设置发送最后确认分组的超时时钟等。由于运输层提供了高可靠性的端到端的通信,因此应用层可以忽略所有这些细节。
    • TCP 协议面向有连接,能正确处理丢包,传输顺序错乱的问题,但是为了建立与断开连接,需要至少7次的发包收包,资源浪费
  • 而另一方面, UDP则为应用层提供一种非常简单的服务。 它只是把称作数据报的分组 从一台主机发送到另一台主机, 但并不保证该数据报能到达另一端。 任何必需的可靠性必须由应用层来提供。 这两种运输层协议分别在不同的应用程序中有不同的用途,这一点将在后面看到。
    • UDP 面向无连接,不管对方有没有收到,如果要得到通知,需要通过应用层

**传输层(Transport Layer)**主要为两台主机上的应用程序提供端到端的通信。

在 TCP/IP协议族中, 有两个互不相同的传输协议:TCP(传输控制协议)和UDP(用户数据报协议)。

  • TCP为两台主机提供高可靠性的数据通信。它所做的工作包括把应用程序交给它的数据分成合适的小块交给下面的网络层,确认接收到的分组,设置发送最后确认分组的超时时钟等。由于运输层提供了高可靠性的端到端的通信,因此应用层可以忽略所有这些细节。
  • 而另一方面, UDP则为应用层提供一种非常简单的服务。 它只是把称作数据报的分组从一台主机发送到另一台主机, 但并不保证该数据报能到达另一端。 任何必需的可靠性必须由应用层来提供。 这两种运输层协议分别在不同的应用程序中有不同的用途,这一点将在后面看到。

TCP Protocol

TCP enables applications to communicate with each other as though connected by a physical circuit. TCP sends data in a form that appears to be transmitted in a character-by-character fashion, rather than as discrete packets. This transmission consists of a starting point, which opens the connection, the entire transmission in byte order, and an ending point, which closes the connection.

TCP attaches a header onto the transmitted data. This header contains a large number of parameters that help processes on the sending machine connect to peer processes on the receiving machine.

TCP confirms that a packet has reached its destination by establishing an end-to-end connection between sending and receiving hosts. TCP is therefore considered a “reliable, connection-oriented” protocol.

TCP段(TCP Segment)

UDP Protocol

UDP, the other transport layer protocol, provides datagram delivery service. UDP does not verify connections between receiving and sending hosts. Because UDP eliminates the processes of establishing and verifying connections, applications that send small amounts of data use UDP rather than TCP.

应用层(Application Layer)

应用层负责处理特定的应用程序细节。

The application layer defines standard Internet services and network applications that anyone can use. These services work with the transport layer to send and receive data. Many application layer protocols exist. The following list shows examples of application layer protocols:

  • Standard TCP/IP services such as the ftp, tftp, and telnet commands
  • UNIX “r” commands, such as rlogin and rsh
  • Name services, such as NIS+ and domain name system (DNS)
  • File services, such as the NFS service
  • Simple Network Management Protocol (SNMP), which enables network management
  • RIP and RDISC routing protocols

Reference