通过C语言接口在Mac App内部对系统的DNS配置进行改动。
Mac OS X设置DNS代码
演示样例代码setDNS.c内容例如以下:
#include <SystemConfiguration/SystemConfiguration.h>
static bool setDNS(CFStringRef *resolvers, CFIndex resolvers_count)
{
SCDynamicStoreRef ds = SCDynamicStoreCreate(NULL, CFSTR("setDNS"), NULL, NULL);
CFArrayRef array = CFArrayCreate(NULL, (const void **) resolvers,
resolvers_count, &kCFTypeArrayCallBacks);
CFDictionaryRef dict = CFDictionaryCreate(NULL,
(const void **) (CFStringRef []) { CFSTR("ServerAddresses") },
(const void **) &array, 1, &kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CFArrayRef list = SCDynamicStoreCopyKeyList(ds,
CFSTR("State:/Network/(Service/.+|Global)/DNS"));
CFIndex i = 0, j = CFArrayGetCount(list);
if (j <= 0) {
return FALSE;
}
bool ret = TRUE;
while (i < j) {
ret &= SCDynamicStoreSetValue(ds, CFArrayGetValueAtIndex(list, i), dict);
i++;
}
return ret;
}
int main(int argc, const char * argv[])
{
CFStringRef resolvers[] = {
CFSTR("8.8.8.8"),
CFSTR("114.114.114.114")
};
setDNS(resolvers, (CFIndex) (sizeof resolvers / sizeof resolvers[0]));
return 0;
}
相应的Makefile文件内容:
#!/usr/bin/make -f
default: setDNS.c
cc -o setDNS setDNS.c -framework Foundation -framework SystemConfiguration
clean:
rm setDNS
谢谢各位。欢迎交流并指正。
---- 大茶园丁@戴维营教育
https://github.com/DiveinEdu-CN
版权声明:本文博主原创文章,博客,未经同意不得转载。