Network protocol: sctp flow control transmission protocol

Get into the habit of writing together! This is the 16th day of my participation in the "Nuggets Daily New Plan·April Update Challenge", click to view the event details .

Introduction

To talk about network protocols, it must be inseparable from the seven-layer model of OSI (Open System Interconnection). We generally focus on the layers above the network layer, such as the network layer where IPV4 and IPV6 are located, the transport layer where TCP UDP is located, and the application layer where HTTP FTP is located.

The sctp protocol to be talked about today, the full name is Stream Control Transmission Protocol, translated into Chinese is the Stream Control Transmission Protocol. It was proposed by the IETF in RFC 4960.

The transport layer already has widely used TCP and UDP protocols, so why invent an SCTP protocol?

Obviously, SCTP protocol is an improvement of TCP and UDP protocols. Specifically, SCTP protocol not only provides the message-oriented characteristics of UDP protocol, but also has the reliability, sequential transmission and congestion control functions of TCP protocol, and also provides Features multi-homing and redundant paths for increased resiliency and reliability.

This article will explain in detail the implementation principle and protocol details of SCTP.

What's wrong with TCP

What's so bad about TCP? Of course TCP is good, we know that UDP is an unreliable message transmission method, and TCP is a reliable message transmission method.

UDP and TCP have been used in a very wide range of application scenarios. However, a product or protocol cannot be perfect, and there must be some shortcomings. Let's take a look at the shortcomings of TCP.

The biggest difference between TCP and UDP is that TCP is reliable, which means that TCP provides a method for transmitting data reliably over the Internet.

However, in order to ensure reliable data transmission, TCP imposes some restrictions on transmission.

For example, in order to ensure the reliability of data transmission, TCP needs to strictly require the order of data transmission. For example, a data packet is split into three parts labeled A, B, and C respectively. Then for the receiver, the packet A must be accepted first, followed by B and C. If B is received first, then the receiver will need to let the sender retransmit the packet.

With such strict requirements on packet ordering, unnecessary data delays and message blocking can occur.

因为TCP是面向数据流的,为了标记数据流中的不同记录,TCP中的数据需要额外添加一些标记或者编码来对记录进行区分。

另外,为了提升传输效率,避免发送多个小数据包的情况,TCP还可能会对其进行优化,也就是说等等多个小的数据包将其合并为一个大的数据包。如果不希望这样的优化,那么需要在TCP数据包中设置PSH标志,明确该请求是无延迟的传输请求。

最后TCP还容易收到DOS(denial-of-service)攻击。

sctp的特点

既然TCP还有诸多的缺点,那么新的SCTP协议又有什么特点呢?

SCTP主要有两大特点,第一个特点就是Message-based,也就是说SCTP是面向消息的。SCTP传输的是一系列的消息,一个消息是一组字节。

相比之下TCP传输的是字节流。

SCTP中的一条消息可以拆分成为多个数据块,每个数据块中的所有数据都来自同一个用户。当这些数据需要在IP中进行传输的时候,SCTP会将这些数据包打包成为SCTP packets,每个SCTP packet,都包含一个packet header,如果需要的话还可以包含控制块,最后跟着的是数据块。

怎么理解TCP包和SCTP包的不同呢?

举个例子,当客户端以TCP协议向服务器端发送多条消息的时候,如果消息非常短,为了提升传输效率,TCP可能会将这些不同作用的小的数据放到同一个TCP包中。在服务器端接收的过程中也是一次收取这个TCP包中的所有数据,然后由应用程序本身来进行TCP包中底层数据的拆分。

对于SCTP包来说,一个SCTP包中可以包含多个data chunks,不同的data chunks可以包含不同用户的不同消息,因为SCTP包已经对不同的消息进行了区分,所以对于服务器端的读取来说就相对容易很多。

下面是一个SCTP包的基本结构:

从上图可以看出,前面的蓝色部分的12个字节是SCTP包的包头,其中前面的两个字节是源端口号,接着的两个字节是目标端口号,然后4个字节是验证标记,最后的4个直接是校验位,总共12个字节。

header后面就是data chunks,也就是数据块,每个chunk都包含一个类型位,flags位和长度位,后面跟着的是chunk的具体数据。

SCTP的另外一个特点就是multi-streaming,多流指的是SCTP能够并行传输多个独立的数据流,比如在访问网页的时候可以同时传输网页中的图像和网页的文本。

为什么可以这样操作呢?这也是由SCTP数据包的结构来决定的,我们可以看到SCTP的数据包中可以包含多个data chunks,这些data chunks可以包含不同的数据流过来的数据,所以面向消息的SCTP可以实现并行传输不同数据源数据的功能。

SCTP的另外一个特性就是Multihoming,Multihoming是multiple+homing的合成词,字面上的意思就是多个home。

这是什么意思呢?

我们知道对于TCP协议来说,客户端和服务器端都只有一个,属于一对一进行连接的情况,如果这个连接两端的而任何一个IP或者端口不可以,那么整个TCP的连接就崩溃了。

那么TCP可不可以发展为类似LSB负载均衡的模式呢?如果一个IP不可以,自动重连到备用的IP地址。

SCTP就是TCP协议的升级版本,它在增强可靠性方面做了优化。

具体而言,每个SCTP的节点都会使用心跳的机制定时检查远程节点的主IP地址和备用的冗余IP地址的可达性。在SCTP中一个节点可以绑定多个IP地址。

SCTP节点将会根据收到的远程节点的心跳返回值来确认具体的访问信息。

因为客户端节点和服务器端节点的个数可能是不同的,所以SCTP可以分为对称的multihoming和非对称的multihoming。

下面三个图分别是对称multihoming和两个非对称multihoming的情况:

最后,SCTP的安全性也有所提升。相较于TCP的三次握手相比,SCTP具有四次握手。

TCP的三次握手可能导致SYN攻击。

What is a SYN attack? Let's review the TCP three-way handshake process. First, the client sends a SYN A request to the server. After receiving the SYN request, the server will cache the SYN request, and then return SYN B to the client. ACK A+1 , After receiving the reply, the client verifies the value of ACK, and then sends ACK B+1 to the server again. After the server receives the B+1 request, it finally confirms the identity of the client and the connection is established.

In the above process, because the server needs to cache the SYN messages of the TCP client, if the server receives a large number of SYN messages, it will cause a SYN attack.

In contrast, after SCTP receives the connection request from the client, it does not allocate memory and cache it immediately, but returns a COOKIE to the client. When the client requests again, it needs to bring the cookie information, and the server will verify the identity of the client through cookie verification, and then the connection will be finally established. Thereby avoiding the SYN attack of TCP.

Summarize

To sum up, SCTP can guarantee orderly and reliable transmission of countless data streams, and can support Multihoming, where each node can contain multiple IP addresses, enabling transparent failover between redundant network paths. And improve the verification and confirmation mechanism to prevent SYN attacks.

SCTP is a very good protocol. For commonly used operating systems, support for SCTP is also provided in specific versions. For example, in linux, the SCTP protocol is supported on the kernel version 2.4.

If you are on Windows or MAC, you need to install additional third-party drivers. The Windows driver is called SctpDrv kernel driver, and the driver on MAC is called SCTP Network Kernel Extension for Mac OS X.

This article has been included in www.flydean.com/21-sctp/

The most popular interpretation, the most profound dry goods, the most concise tutorials, and many tricks you don't know are waiting for you to discover!

Welcome to pay attention to my official account: "Program those things", understand technology, understand you better!

Guess you like

Origin juejin.im/post/7087150774576709662