[vlc-commits] upnp: implement getPreferedAdapter for macOS

Luca Barbato git at videolan.org
Sun May 19 10:47:37 CEST 2019


vlc/vlc-3.0 | branch: master | Luca Barbato <lu_zero at gentoo.org> | Sat May 18 16:41:32 2019 +0200| [6a8889ec40d05e12e1d49171d63ed62bdfb0e2bb] | committer: Felix Paul Kühne

upnp: implement getPreferedAdapter for macOS

Signed-off-by: Felix Paul Kühne <felix at feepk.net>
(cherry picked from commit d041a6cd3c7c7d29c8b36aeeab528d417738b532)

> http://git.videolan.org/gitweb.cgi/vlc/vlc-3.0.git/?a=commit;h=6a8889ec40d05e12e1d49171d63ed62bdfb0e2bb
---

 modules/services_discovery/Makefile.am |  3 ++
 modules/services_discovery/upnp.cpp    | 63 ++++++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+)

diff --git a/modules/services_discovery/Makefile.am b/modules/services_discovery/Makefile.am
index efd9cf6708..e070d6463b 100644
--- a/modules/services_discovery/Makefile.am
+++ b/modules/services_discovery/Makefile.am
@@ -33,6 +33,9 @@ libupnp_plugin_la_LDFLAGS = $(AM_LDFLAGS) -rpath '$(sddir)'
 libupnp_plugin_la_LIBADD = $(UPNP_LIBS)
 EXTRA_LTLIBRARIES += libupnp_plugin.la
 sd_LTLIBRARIES += $(LTLIBupnp)
+if HAVE_OSX
+libupnp_plugin_la_LDFLAGS += -Wl,-framework,CoreFoundation,-framework,SystemConfiguration
+endif
 
 libpulselist_plugin_la_SOURCES = services_discovery/pulse.c
 libpulselist_plugin_la_CFLAGS = $(AM_CFLAGS) $(PULSE_CFLAGS)
diff --git a/modules/services_discovery/upnp.cpp b/modules/services_discovery/upnp.cpp
index 127f562837..26152942ca 100644
--- a/modules/services_discovery/upnp.cpp
+++ b/modules/services_discovery/upnp.cpp
@@ -1542,11 +1542,74 @@ done:
 
 #ifdef UPNP_ENABLE_IPV6
 
+#ifdef __APPLE__
+#include <TargetConditionals.h>
+#endif
+
+#if defined(TARGET_OS_MAC) && TARGET_OS_MAC
+#include <CoreFoundation/CoreFoundation.h>
+#include <SystemConfiguration/SystemConfiguration.h>
+#include "vlc_charset.h"
+
+inline char *FromCFString(const CFStringRef cfString,
+                          const CFStringEncoding cfStringEncoding)
+{
+    // Try the quick way to obtain the buffer
+    const char *tmpBuffer = CFStringGetCStringPtr(cfString, cfStringEncoding);
+    if (tmpBuffer != NULL) {
+        return strdup(tmpBuffer);
+    }
+
+    // The quick way did not work, try the long way
+    CFIndex length = CFStringGetLength(cfString);
+    CFIndex maxSize = CFStringGetMaximumSizeForEncoding(length, cfStringEncoding);
+
+    // If result would exceed LONG_MAX, kCFNotFound is returned
+    if (unlikely(maxSize == kCFNotFound)) {
+        return NULL;
+    }
+
+    // Account for the null terminator
+    maxSize++;
+
+    char *buffer = (char *)malloc(maxSize);
+    if (unlikely(buffer == NULL)) {
+        return NULL;
+    }
+
+    // Copy CFString in requested encoding to buffer
+    Boolean success = CFStringGetCString(cfString, buffer, maxSize, cfStringEncoding);
+
+    if (!success)
+        FREENULL(buffer);
+    return buffer;
+}
+
+inline char *getPreferedAdapter()
+{
+    SCDynamicStoreRef session = SCDynamicStoreCreate(NULL, CFSTR("session"), NULL, NULL);
+    CFDictionaryRef q = (CFDictionaryRef) SCDynamicStoreCopyValue(session, CFSTR("State:/Network/Global/IPv4"));
+    char *returnValue = NULL;
+
+    if (q != NULL) {
+        const void *val;
+        if (CFDictionaryGetValueIfPresent(q, CFSTR("PrimaryInterface"), &val)) {
+            returnValue = FromCFString((CFStringRef)val, kCFStringEncodingUTF8);
+        }
+    }
+    CFRelease(q);
+    CFRelease(session);
+
+    return returnValue;
+}
+#else
+
 static char *getPreferedAdapter()
 {
     return NULL;
 }
 
+#endif
 #else
 
 static char *getIpv4ForMulticast()



More information about the vlc-commits mailing list