我试图根据http://www.ietf.org/rfc/rfc3542.txt附录C中指定的示例在发送之前在数据包中设置Hop-By-Hop选项。我在使用API时没有看到任何错误,但未在发送的数据包中设置HBH选项。
这是我的代码:
set_rtr_alert_opt (struct msghdr *msg, struct cmsghdr *cmsg)
{
int ralen = 0, currentlen = 0;
void *hbhbuf = NULL, *optp = NULL;
u_int8_t rtr_alert;
rtr_alert = htons(IP6_ALERT_MLD);
/*
* Set the Router Alert option into the cmsg
*/
ralen = inet6_opt_init(NULL, 0);
if (ralen == -1) {
return(FALSE);
}
ralen = inet6_opt_append(NULL, 0, ralen, IP6OPT_ROUTER_ALERT, 2,
2, NULL);
if (ralen == -1) {
return(FALSE);
}
ralen = inet6_opt_finish(NULL, 0, ralen);
if (ralen == -1) {
return(FALSE);
}
cmsg->cmsg_len = CMSG_LEN(ralen);
cmsg->cmsg_level = IPPROTO_IPV6;
cmsg->cmsg_type = IPV6_HOPOPTS;
hbhbuf = CMSG_DATA(cmsg);
msg->msg_controllen += ALIGN(cmsg->cmsg_len);
currentlen = inet6_opt_init(hbhbuf, ralen);
if (currentlen == -1) {
return(FALSE);
}
currentlen = inet6_opt_append(hbhbuf, ralen, currentlen,
IP6OPT_ROUTER_ALERT, 2, 2, &optp);
if (currentlen == -1) {
return(FALSE);
}
inet6_opt_set_val(optp, 0, &rtr_alert, sizeof(rtr_alert));
currentlen = inet6_opt_finish(hbhbuf, ralen, currentlen);
if (currentlen == -1) {
return(FALSE);
}
return(TRUE);
}
在此之后我调用sendmsg()来发送数据包。数据包确实熄灭但数据包中没有HBH选项。