gpt4 book ai didi

c - 如何使用C编程使SCTP校验和非零

转载 作者:行者123 更新时间:2023-12-03 12:07:45 25 4
gpt4 key购买 nike

我编写了一个简单的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 client\n");
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 %d\n", brst);
}

close(sockfd);
}

编译
$ gcc sctp-server.c -lsctp -o server
$ gcc sctp-client.c -lsctp -o client

Wireshark
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

最佳答案

如本patch所述,您有可能在Linux内核中禁用了SCTP校验和。要验证这一点,请运行命令sysctl /sys/module/sctp/parameters/sctp_checksum_disable。如果输出是1,则将其更改为0,然后再次尝试测试。

关于c - 如何使用C编程使SCTP校验和非零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61734268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com