无法在辅助接口上接收UDP/IPv6数据包
问题描述:
我有以下用于UDP服务器的C代码,它将绑定到辅助接口tap0的ipv6地址。无法在辅助接口上接收UDP/IPv6数据包
context_t *
new_context(const struct sockaddr *listen_addr, size_t addr_size) {
context_t *c = malloc(sizeof(context_t));
time_t now;
int reuse = 1;
if (!listen_addr) {
fprintf(stderr, "no listen address specified\n");
return NULL;
}
srand(getpid()^time(&now));
if (!c) {
perror("init: malloc:");
return NULL;
}
memset(c, 0, sizeof(context_t));
c->sockfd = socket(listen_addr->sa_family, SOCK_DGRAM, 0);
if (c->sockfd < 0) {
perror("new_context: socket");
goto onerror;
}
if (setsockopt(c->sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
perror("setsockopt SO_REUSEADDR");
if (bind (c->sockfd, listen_addr, addr_size) < 0) {
perror("new_context: bind");
goto onerror;
}
return c;
onerror:
if (c->sockfd >= 0)
close (c->sockfd);
free(c);
return NULL;
}
context_t *
get_context(const char *ipaddress, const char *port, unsigned int scopeId) {
int s;
context_t* ctx;
struct addrinfo hints;
struct addrinfo *result, *rp;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_DGRAM; /* Coap uses UDP */
hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST | AI_NUMERICSERV | AI_ALL;
s = getaddrinfo(ipaddress, port, &hints, &result);
if (s != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
return NULL;
}
/* iterate through results until success */
for (rp = result; rp != NULL; rp = rp->ai_next) {
ctx = new_context(rp->ai_addr, rp->ai_addrlen);
if (ctx) {
if (rp->ai_family == PF_INET6) {
struct sockaddr_in6* pSadrIn6 = (struct sockaddr_in6*) rp->ai_addr;
if (pSadrIn6->sin6_scope_id == 0) {
pSadrIn6->sin6_scope_id = scopeId;
} /* End IF the scope ID wasn't set. */
}
goto finish;
}
}
fprintf(stderr, "no context available for interface '%s'\n", node);
finish:
freeaddrinfo(result);
return ctx;
}
设备tap0中有以下细节:
tap0 Link encap:Ethernet HWaddr ce:23:fc:81:7f:65
inet6 addr: fe80::cc23:fcff:fe81:7f65/64 Scope:Link
inet6 addr: aaaa::1/64 Scope:Global
我运行下面的命令上面的代码:
./server -A aaaa::1
但它不是能够听来的地址信息tap0上的aaaa :: 1。我可以在tap0接口上的wireshark转储中看到数据包。有趣的是,从上述命令运行的服务器(和代码)可以通过本地主机接收消息。因此,如果我执行以下命令,服务器将收到以下消息:
nc -6 -vvv -u aaaa::1 61616 < /tmp/send_to_node_raw
上述命令的发送和接收是通过localhost完成的。 有什么办法可以在辅助接口上以编程方式接收UDP/IPv6消息吗?
答
该问题必须在其他地方。使用上面的代码,我可以分别使用aaaa :: 1/64和aaaa :: 2/64作为本地和远程地址成功接收来自另一个节点的数据包。
尝试绑定到::并查看它是否以这种方式工作。也尝试使用netcat6绑定到aaaa :: 1并从另一个netcat6实例接收。
你正在设置什么'scopeId'?它是否是'1'? – 2011-12-22 15:21:38
这只是一个界面,真的。并且介意,绑定(2)“绑定”地址而不是接口,也就是说,最多会收到带有dst addr aaaa :: 1的数据包。 – 2011-12-22 22:54:45
@Nikolai:我将scopeId设置为tap0。 – 2011-12-23 22:18:30