+#ifdef HAVE_GETIFADDRS
+struct getifaddrs_in_netns_params {
+ struct ifaddrs **ifa;
+ int netns;
+};
+
+#ifdef HAVE_SETNS
+static void *getifaddrs_in_netns_thread(void *arg) {
+ struct getifaddrs_in_netns_params *params = arg;
+
+ if(setns(params->netns, CLONE_NEWNET) == -1) {
+ meshlink_errno = MESHLINK_EINVAL;
+ return NULL;
+ }
+
+ if(getifaddrs(params->ifa) != 0) {
+ *params->ifa = NULL;
+ }
+
+ return NULL;
+}
+#endif // HAVE_SETNS
+
+static int getifaddrs_in_netns(struct ifaddrs **ifa, int netns) {
+ if(netns == -1) {
+ return getifaddrs(ifa);
+ }
+
+#ifdef HAVE_SETNS
+ struct getifaddrs_in_netns_params params = {ifa, netns};
+ pthread_t thr;
+
+ if(pthread_create(&thr, NULL, getifaddrs_in_netns_thread, ¶ms) == 0) {
+ if(pthread_join(thr, NULL) != 0) {
+ abort();
+ }
+ }
+
+ return *params.ifa ? 0 : -1;
+#else
+ return -1;
+#endif // HAVE_SETNS
+
+}
+#endif
+