如果macOS或某些虚拟接口可用,下面的代码将不再在IPv6上工作。
我总是得到错误的 getnameinfo() failed: Unknown error (ai_family not supported)
getnameinfo() failed: Unknown error (ai_family not supported)
你知道这有什么不对吗?我只需要一个正确的网络接口与ipv4和互联网。
这个问题首先出现在macOS Sierra身上。
#include "jni.h" #include "bla_nativeclasses_JNISubNetMask.h" #include <arpa/inet.h> #include <sys/socket.h> #include <netdb.h> #include <ifaddrs.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> static jobjectArray make_row(JNIEnv *env, jsize count, const char* elements[]) jclass stringClass = (*env)->FindClass(env, "java/lang/String"); jobjectArray row = (*env)->NewObjectArray(env, count, stringClass, 0); jsize i; for (i = 0; i < count; ++i) { (*env)->SetObjectArrayElement(env, row, i, (*env)->NewStringUTF(env, elements[i])); return row; JNIEXPORT jobjectArray JNICALL Java_bla_JNISubNetMask_getSubNetMask(JNIEnv *env, jobject jobj){ struct ifaddrs *ifaddr, *ifa; int family, s ,s2; int i = 0; int count = 0; char host[NI_MAXHOST]; char subnet[NI_MAXHOST]; char *tmp = NULL; const char* net[1000]; if (getifaddrs(&ifaddr) == -1) { perror("getifaddrs"); exit(EXIT_FAILURE); /* Walk through linked list, maintaining head pointer so we can free list later */ for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { if (ifa->ifa_addr == NULL) continue; if (ifa->ifa_addr->sa_family != AF_INET) continue; s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); s2 = getnameinfo(ifa->ifa_netmask, sizeof(struct sockaddr_in), subnet, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); if (s != 0 || s2 != 0) { printf("getnameinfo() failed: %s (%s)\n", gai_strerror(s), gai_strerror(s2)); exit(EXIT_FAILURE); tmp = (char *)malloc(100*sizeof(char)); strcpy (tmp,ifa->ifa_name); net[i++] = tmp; tmp = (char *)malloc(100*sizeof(char)); strcpy (tmp,host); net[i++] = tmp; tmp = (char *)malloc(100*sizeof(char)); strcpy (tmp,subnet); net[i++] = tmp;