Skip to content

Instantly share code, notes, and snippets.

@shehzadkhawar
Created August 9, 2016 17:33
Show Gist options
  • Save shehzadkhawar/cebdfc01659030fb7dedb6eb6601c6a4 to your computer and use it in GitHub Desktop.
Save shehzadkhawar/cebdfc01659030fb7dedb6eb6601c6a4 to your computer and use it in GitHub Desktop.
Adds IPv6 link local address based communication to libmill
/* Convert literal IPv6 address to a binary one. */
static ipaddr mill_ipv6_literal(const char *addr, int port) {
ipaddr raddr;
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6*)&raddr;
struct sockaddr_in6 *sockv6;
char str[IPADDR_MAXSTRLEN];
int rc = inet_pton(AF_INET6, addr, &ipv6->sin6_addr);
mill_assert(rc >= 0);
if(rc == 1) {
ipv6->sin6_family = AF_INET6;
ipv6->sin6_port = htons((uint16_t)port);
/* Grab IPv6 Scope ID from the IPv6 address. */
struct ifaddrs *ifaces = NULL;
int rc = getifaddrs (&ifaces);
mill_assert (rc == 0);
mill_assert (ifaces);
struct ifaddrs *ifv6 = NULL;
struct ifaddrs *it;
for(it = ifaces; it != NULL; it = it->ifa_next) {
if(!it->ifa_addr)
continue;
if(it->ifa_addr->sa_family == AF_INET6){
sockv6 = (struct sockaddr_in6 *)it->ifa_addr;
inet_ntop(AF_INET6, &(sockv6->sin6_addr), str, IPADDR_MAXSTRLEN);
if(strcmp(str, addr) == 0){
ipv6->sin6_scope_id = if_nametoindex(it->ifa_name);
}
}
}
errno = 0;
return raddr;
}
ipv6->sin6_family = AF_UNSPEC;
errno = EINVAL;
return raddr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment