设想:

我创建了SCTP一对多套接字(以大字母开头的函数会调用相应的标准函数,并检查错误并将errno打印到stderr)

int sock_fd,msg_flags;
char readbuf[BUFFSIZE];
struct sockaddr_in servaddr, cliaddr;
struct sctp_sndrcvinfo sri;
struct sctp_event_subscribe evnts;
int stream_increment=1;
socklen_t len;
size_t rd_sz;

sock_fd = Socket( AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
bzero( &servaddr, sizeof( servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl( INADDR_ANY);
servaddr.sin_port = htons( SERV_PORT);

Bind( sock_fd, ( SA *) &servaddr, sizeof(servaddr));

bzero( &evnts, sizeof( evnts));
evnts.sctp_data_io_event = 1;
Setsockopt( sock_fd, IPPROTO_SCTP, SCTP_EVENTS, &evnts, sizeof( evnts));

Listen( sock_fd, LISTENQ);

然后,我阻止对sctp_recvmsg的调用,以在消息到达时读取消息
 rd_sz = Sctp_recvmsg( sock_fd, readbuf, sizeof( readbuf),
             (SA *)&cliaddr, &len, &sri,&msg_flags);

客户端连接后,我调用getsockopt来检索SCTP关联的当前状态
len = sizeof(struct sockaddr_in);
socklen_t retsz;
struct sctp_status status;
retsz = sizeof(status);
bzero(&status,sizeof(status));

status.sstat_assoc_id = sctp_address_to_associd(sock_fd, (SA *)&cliaddr, len);
getsockopt( sock_fd, IPPROTO_SCTP, SCTP_STATUS, &status, &retsz);

函数sctp_address_to_associd是:
sctp_assoc_t
sctp_address_to_associd(int sock_fd, struct sockaddr *sa, socklen_t salen)
{
    struct sctp_paddrparams sp;
    socklen_t siz;

    siz = sizeof(struct sctp_paddrparams);
    bzero(&sp,siz);
    memcpy(&sp.spp_address,sa,salen);
    sctp_opt_info(sock_fd,0,
           SCTP_PEER_ADDR_PARAMS, &sp, &siz);
    return(sp.spp_assoc_id);
}

为什么getsockopt返回“无效参数”? errno =22。操作系统是Linux Ubuntu 12.10。

最佳答案

sctp_address_to_associd()函数中的SCTP_PEER_ADDR_PARAMS套接字选项不能用于学习关联ID,至少在Linux上不是这样。

由于您已经从sctp_recvmsg()调用中获得了关联ID,因此请改为使用struct sctp_sndrcvinfo的关联ID:

status.sstat_assoc_id = sri.sinfo_assoc_id;
getsockopt( sock_fd, IPPROTO_SCTP, SCTP_STATUS, &status, &retsz);

关于c++ - 获取IPPROTO_SCTP,SCTP_STATUS的getsockopt “Invalid argument”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23897781/

10-10 04:44