我写了一个简单的SCTP客户端/服务器,我通过Wireshark观察到SCTP握手。在 Wireshark 中,所有 SCTP 数据包的校验和始终为零。
如何进行非零 SCTP 校验和?
以下是 Ubuntu 18.04 上安装的库
libsctp-dev:amd64 1.0.17+dfsg-2
libsctp1:amd64 1.0.17+dfsg-2
lksctp-tools 1.0.17+dfsg-2
sctp-server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/sctp.h>
#include <arpa/inet.h>
int main(int argc, char** argv)
{
int sockfd, client_sockfd;
struct sockaddr_in serv_addr, client_addr;
struct sctp_initmsg initmsg;
int len;
int addr_count = 1;
sockfd = socket(AF_INET,SOCK_STREAM,IPPROTO_SCTP);
if(sockfd < 0)
{
fprintf(stderr,"socket descriptor is invalid.");
exit(1);
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(12345);
int brst = sctp_bindx(sockfd,(struct sockaddr*)&serv_addr,addr_count,SCTP_BINDX_ADD_ADDR);
if(brst == -1)
{
fprintf(stderr,"error while binding address.");
exit(1);
}
memset(&initmsg,0,sizeof(initmsg));
initmsg.sinit_max_instreams = 2;
initmsg.sinit_num_ostreams = 2;
brst = setsockopt(sockfd,IPPROTO_SCTP,SCTP_INITMSG,&initmsg,sizeof(initmsg));
if(brst != 0)
{
fprintf(stderr,"error while setting sctp options.n");
exit(1);
}
listen(sockfd,5);
while (1)
{
len = sizeof(client_addr);
client_sockfd = accept(sockfd, (struct sockaddr*)&client_addr, &len);
if(client_sockfd == -1)
{
fprintf(stderr,"error while accepting connection, continue.n");
continue;
}
else
{
printf("New clientn");
close(client_sockfd);
}
}
}
sctp-client.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/sctp.h>
#include <arpa/inet.h>
int main(int argc, char** argv)
{
sctp_assoc_t associd;
int sockfd;
struct sockaddr_in serv_addr;
struct sockaddr_in *addrs;
int brst;
struct sctp_initmsg initmsg;
sockfd = socket(AF_INET,SOCK_STREAM,IPPROTO_SCTP);
if(sockfd < 0)
{
fprintf(stderr,"error while initializing socket.n");
exit(1);
}
addrs = malloc(sizeof(struct sockaddr_in));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr("192.168.1.197");
serv_addr.sin_port = htons(12345);
memcpy(addrs,&serv_addr,sizeof(struct sockaddr_in));
memset(&initmsg, 0, sizeof(initmsg));
initmsg.sinit_max_instreams = 2;
initmsg.sinit_num_ostreams = 2;
brst = sctp_connectx(sockfd, (struct sockaddr*) &serv_addr, 1, &associd);
if(brst < 0)
{
fprintf(stderr, "can not connect to host.n");
exit(1);
}
else
{
printf("connect ok %dn", brst);
}
close(sockfd);
}
编译
$ gcc sctp-server.c -lsctp -o server
$ gcc sctp-client.c -lsctp -o client
钢丝鲨
Frame 828: 100 bytes on wire (800 bits), 100 bytes captured (800 bits)
Linux cooked capture
Internet Protocol Version 4, Src: 192.168.1.197, Dst: 192.168.1.197
Stream Control Transmission Protocol, Src Port: 49208 (49208), Dst Port: 12345 (12345)
Source port: 49208
Destination port: 12345
Verification tag: 0x00000000
[Association index: 0]
Checksum: 0x00000000 [unverified]
[Checksum Status: Unverified]
INIT chunk (Outbound streams: 10, inbound streams: 65535)
Chunk type: INIT (1)
0... .... = Bit: Stop processing of the packet
.0.. .... = Bit: Do not report
Chunk flags: 0x00
Chunk length: 52
Initiate tag: 0xd15eb4e4
Advertised receiver window credit (a_rwnd): 106496
Number of outbound streams: 10
Number of inbound streams: 65535
Initial TSN: 1069541083
IPv4 address parameter (Address: 192.168.1.197)
IPv4 address parameter (Address: 172.17.0.1)
Supported address types parameter (Supported types: IPv4)
ECN parameter
Forward TSN supported parameter
您有可能在 Linux 内核中禁用了 SCTP 校验和,如本修补程序中所述。要验证这一点,请运行命令sysctl /sys/module/sctp/parameters/sctp_checksum_disable
。如果输出1
则将其更改为0
,然后重试测试。