c - sctp_connectx() 在 FreeBSD 上给出 EINVAL

标签 c freebsd sctp

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/sctp.h>
#include <stdio.h>
#include <string.h>

int main(int argc,char **argv)
{

    struct sockaddr_in remoteAddr;

    int clientSock = socket(PF_INET,SOCK_SEQPACKET,IPPROTO_SCTP);
    if(clientSock == -1) {
        perror("socket");
        return 1;
    }
    memset(&remoteAddr,0,sizeof remoteAddr);
    remoteAddr.sin_family = AF_INET;
    remoteAddr.sin_len = sizeof remoteAddr;
    remoteAddr.sin_port = htons(5555);
    remoteAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    sctp_assoc_t assoc_id = 0;
    if(sctp_connectx(clientSock,(struct sockaddr*)&remoteAddr,1, &assoc_id)!= 0) {
        perror("sctp_connectx");
        return 1;
    }
    printf("Connected! Assoc ID %d\n",(int)assoc_id);

    return 0;   
}

运行时,此代码失败:

$ clang  -Wall sctp_connect.c 
$ ./a.out 
sctp_connectx: Invalid argument
$ uname -rp
11.0-RELEASE-p9 amd64

但是我不知道哪里出了问题。 sctp_connectx () 联机帮助页表示,如果提供的地址具有无效系列或未提供地址,它将失败并返回 EINVAL - 但代码似乎并非如此。

sctp_connectx () 有几个部分可能因 EINVAL 而失败,但 truss 显示它到达了 setsockopt() 调用,因此是内核导致调用失败:

socket(PF_INET,SOCK_SEQPACKET,132)       = 3 (0x3)
mmap(0x0,2097152,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANON,-1,0x0) = 34374418432 (0x800e00000)
setsockopt(0x3,0x84,0x8007,0x800e16000,0x14)     ERR#22 'Invalid argument'

最佳答案

我认为您的查询中有答案。如果我们遵循 truss 跟踪,那么正如您所说,它在 setsockopt() 上失败。

因此 EINVAL 错误由 setsockopt() 返回。根据 FreeBSD setsockopt() 手册:

[EINVAL]: Installing an accept_filter(9) on a non-listening socket was attempted.

是错误的描述。所以我认为你应该做以下事情:

  1. 探索您的套接字选项是否与您的监听器套接字相关。
  2. 检查函数 htons()inet_addr() 的错误

我的建议是您不应该使用 inet_addr(),有关更多详细信息,请参阅手册页,如下所示:

Use of this function is problematic because -1 is a valid address (255.255.255.255). Avoid its use in favor of inet_aton(), inet_pton(3), or getaddrinfo(3), which provide a cleaner way to indicate error return.

关于c - sctp_connectx() 在 FreeBSD 上给出 EINVAL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44028555/

相关文章:

android - android native 支持 RTP 和/或 SCTP 吗?

TCP 和 SCTP 上的 Linux 内核

c++ - 在 C 或 C++ 中,我如何在没有回车或空格的情况下为程序提供输入

c - Unix C - 可移植 WEXITSTATUS

perl - 选择在关闭的 SCTP 套接字上返回 0

php - MySQL连接服务器-客户端

c - GCC 编译器无法找到 pcre.h

c - 关于递归函数的问题?

c - 当某些字段为空时,如何使用 sscanf 和 scanset 以逗号分隔的字符串提取字段

创建编译多个 C 文件以在 Minix 中使用的 Makefile