I get a Routing Information Protocol(RIP) packet, and I can get the link-local addr of the source router from this packet. But I cannot set the route by the link-local addr, I need a global link addr of the source router. How should I write the code in C? Thanks. I made a try, but it doesn't work.
#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;
}
Two things:
Routing using link-local addresses is no problem at all. The only thing you have to remember is to explicitly state the outgoing interface in addition to the link-local next hop address.
Here are a few lines from my local routing table (this is Mac OS X, but the concept is the same for i.e. Linux and Windows):
Internet6:
Destination Gateway Flags Netif Expire
default fe80::222:83ff:feb5:964b%en0 UGc en0
::1 link#1 UHL lo0
2a00:8640:1::/64 link#4 UC en0
As you can see my default gateway is fe80::222:83ff:feb5:964b%en0
. This is what my PC learned from a Router Advertisement.
In routing the next-hop (or gateway) address is literally only used to determine the next hop. On ethernet the next-hop IPv6 address is used to look up the MAC address on the local LAN where the packet should be sent, and using a link-local address for that is no problem at all. A global address could also be used, but is not required. If you don't have it: just use the link-local address.