问题描述
我有以下用于UDP服务器的C代码,它将绑定到辅助接口tap0的ipv6地址.
I have the following C code for UDP server which would bind to the ipv6 address of the secondary interface tap0.
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具有以下详细信息:
Device tap0 has following details:
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
我使用以下命令运行以上代码:
I run the above code with the following command:
./server -A aaaa::1
但是它无法在tap0上收听到达地址aaaaa :: 1的消息.我可以在tap0接口上看到Wireshark转储上的数据包.有趣的是,从上述命令(和代码)运行的服务器可以通过本地主机接收消息.因此,如果执行以下命令,服务器将收到消息:
But it's not able to listen to the messages coming to the address aaaa::1 on tap0. I can see the packets on the wireshark dump on tap0 interface. Interestingly, the server ran from the above command(and code) can receive the messages through the localhost. So, if I execute the following command, the server receives the messages:
nc -6 -vvv -u aaaa::1 61616 < /tmp/send_to_node_raw
以上命令的发送和接收是通过本地主机完成的.有什么方法可以通过编程方式在辅助接口上接收UDP/IPv6消息?
The above command sending and reception is done through localhost.Is there any way I can programatically receive UDP/IPv6 messages on secondary interfaces?
推荐答案
问题必须在其他地方.使用上面的代码,我可以分别使用aaaa :: 1/64和aaaaa :: 2/2/64作为本地和远程地址,成功地从另一个节点接收数据包.
The problem must be somewhere else. Using the code above I can successfully receive packets from another node using aaaa::1/64 and aaaa::2/64 as the local and remote addresses, respectively.
尝试绑定到::,看看它是否可以那样工作.还要尝试使用netcat6绑定到aaaa :: 1并从另一个netcat6实例接收.
Try to bind to :: and see if it works that way. Also try to use netcat6 to bind to aaaa::1 and receive from another netcat6 instance.
这篇关于无法在辅助接口上接收UDP/IPv6数据包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!