3
我有以下用於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消息嗎?
你正在設置什麼'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