我得到一个路由信息协议(RIP)数据包,我可以从这个数据包中得到源路由器的链路本地地址。但是我无法通过链接本地地址设置路由,我需要源路由器的全局链接地址。我应该如何用C编写代码?谢谢。我试过了,但它不起作用。
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
int main () {
int sockfd;
struct addrinfo hints, *servinfo, *p;
int rv;
char str[INET6_ADDRSTRLEN] = "";
char port[] = "521";
char addr[] ="fe80::a00:27ff:fe22:4d87";
struct sockaddr_in6 sa;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET6;
hints.ai_socktype = SOCK_DGRAM;
//inet_pton(AF_INET6, "fe80::a00:27ff:fe22:4d87", &(sa.sin6_addr));
//hints.ai_addr = (void *)&sa;
hints.ai_flags = AI_NUMERICHOST;
if ((rv = getaddrinfo(addr, NULL, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 0;
}
for(p = servinfo; p != NULL; p = p->ai_next) {
if (p->ai_family == AF_INET6) {
inet_ntop(AF_INET6, (struct in6_addr *)p->ai_addr->sa_data, str, INET6_ADDRSTRLEN);
printf("%s\n", str);
}
}
freeaddrinfo(servinfo); // all done with this structure
return 0;
}