Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
example of adding ipv6 address via ioctl(SIOCAIFADDR_IN6)
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/in_var.h>
#include <arpa/inet.h>
#include <netinet6/nd6.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
int main() {
struct in6_aliasreq ifr6;
int fd;
memset(&ifr6, 0, sizeof ifr6);
strcpy(ifr6.ifra_name, "en0");
ifr6.ifra_addr.sin6_len = sizeof ifr6.ifra_addr;
ifr6.ifra_addr.sin6_family = AF_INET6;
if (inet_pton(AF_INET6, "2001:da8:ecd1::1", &ifr6.ifra_addr.sin6_addr) != 1) {
perror("inet_pton");
return -1;
}
ifr6.ifra_prefixmask.sin6_len = sizeof ifr6.ifra_prefixmask;
memset(&ifr6.ifra_prefixmask.sin6_addr, 0xff, 8); /* 8 * 8 = 64 bit */
/* important!!! */
ifr6.ifra_lifetime.ia6t_vltime = ifr6.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
fd = socket(PF_INET6, SOCK_DGRAM, 0);
if (fd < 0) {
perror("socket");
return -1;
}
if (ioctl(fd, SIOCAIFADDR_IN6, &ifr6) < 0) {
perror("ioctl");
close(fd);
return -2;
}
close(fd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment