对于IPPROTO_SCTP,SCTP_STATUS,getsockopt“无效参数”

时间:2014-05-27 19:53:18

标签: c++ c sockets sctp getsockopt

情景:

我创建了SCTP一对多套接字(以大字母调用的函数调用相应的标准函数,检查错误并将错误打印到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。

1 个答案:

答案 0 :(得分:1)

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);