我正在尝试根据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 选项。