码迷,mamicode.com
首页 > 系统相关 > 详细

Macosx Setdns

时间:2015-03-20 09:17:50      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:macosx dns systemcon

通过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

谢谢各位,欢迎交流并指正。

---- 大茶园丁@戴维营教育

http://io.diveinedu.com

http://www.diveinedu.com

https://github.com/DiveinEdu-CN

Macosx Setdns

标签:macosx dns systemcon

原文地址:http://blog.csdn.net/diveinedu/article/details/44486811

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!