+ return -1;
+}
+
+static int
+get_mask(struct vc_net_nx *addr)
+{
+ const struct AF_to_vcNET *i;
+ struct ifaddrs *head, *ifa;
+ int ret = 0;
+
+ for (i = converter; i->af; i++) {
+ if (i->vc_net == addr->type)
+ break;
+ }
+ if (!i) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ if (getifaddrs(&head) == -1)
+ return -1;
+ for (ifa = head; ifa; ifa = ifa->ifa_next) {
+ if (ifa->ifa_addr->sa_family == i->af &&
+ memcmp((char *) ifa->ifa_addr + i->offset, addr->ip, i->len) == 0) {
+ switch (addr->type) {
+ case vcNET_IVP4:
+ memcpy(&addr->mask[0], ifa->ifa_netmask + i->offset, i->len);
+ break;
+ case vcNET_IPV6: {
+ uint32_t *m = ((struct sockaddr_in6 *) ifa->ifa_netmask)->sin6_addr.s6_addr32;
+ /* optimization for the common case */
+ if ((m[1] & 1) == 1 && (m[2] & 0x80000000) == 0)
+ addr->mask[0] = 64;
+ else {
+ addr->mask[0] = 0;
+ while (m[addr->mask[0] / 32] & (addr->mask[0] % 32))
+ addr->mask[0]++;
+ }
+ break;
+ }
+ ret = 1;
+ break;
+ }
+ }
+ /* no match, use a default */
+ if (!ret) {
+ switch (addr->type) {
+ case vcNET_IPV4: addr->mask[0] = htonl(0xffffff00); break;
+ case vcNET_IPV6: addr->mask[0] = 64; break;
+ default: addr->mask[0] = 0; break;
+ }
+ }
+ freeifaddrs(head);
+ return ret;