【问题标题】:sctp_connectx() gives EINVAL on FreeBSDsctp_connectx() 在 FreeBSD 上给出 EINVAL
【发布时间】:2017-10-17 03:13:58
【问题描述】:
#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'

【问题讨论】:

  • 一个好的minimal reproducible example...好的输出...尝试单独解决这个问题...为什么这个问题没有投票?
  • Linux 上的人说“EINVAL 端口或地址无效。”。希望对您有所帮助。
  • Conectx 暗示它是 1:1 接口?我认为你不能在这里使用SOCK_SEQPACKET 类型的套接字你需要使用SOCK_STREAM
  • @Stargateur 它没有帮助。相同的代码在 Linux 上运行良好。
  • @amritanshu 如果使用 SOCK_STREAM 也会出现同样的问题,

标签: c freebsd sctp


【解决方案1】:

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

所以setsockopt() 返回了错误EINVAL。根据 FreeBSD setsockopt() 手册:

[EINVAL]:在 非监听 上安装 accept_filter(9) 套接字已尝试。

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

  1. 探索您的套接字选项对于您的侦听器套接字是否正确。
  2. 检查函数htons()inet_addr() 的错误

我的建议是你不应该使用 inet_addr(),更多细节请参见手册页,根据:

使用这个函数是有问题的,因为 -1 是一个有效的地址 (255.255.255.255)。避免使用它来支持 inet_aton(), inet_pton(3) 或 getaddrinfo(3),它们提供了一种更简洁的方式 表示错误返回。

【讨论】:

    猜你喜欢
    • 2011-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-24
    • 1970-01-01
    • 2015-06-16
    • 2018-09-02
    • 1970-01-01
    相关资源
    最近更新 更多