简体   繁体   English

Objective-C:检查OSX中的防火墙状态?

[英]Objective-C: Check Firewall status in OSX?

My objective-c app needs to be aware if the firewall in OSX is running, so it can tell the user to turn it off or create a new rule. 我的Objective-C应用程序需要知道OSX中的防火墙是否正在运行,因此它可以告诉用户将其关闭或创建新规则。

Also, is it possible to create rules directly from my app so users never need to handle networking issues? 另外,是否可以直接从我的应用程序创建规则,使用户永远不需要处理网络问题?

John 约翰

I am writing a function that will provide you the status of OSX firewall :) 我正在写一个函数,它将为您提供OSX防火墙的状态:)

-(BOOL)getFirewallStatus{


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSSystemDomainMask, YES);

    NSString *path = [paths objectAtIndex:0];

    path = [NSString stringWithFormat:@"%@/%@",path,@"Preferences/com.apple.alf.plist"];

    path = [path stringByReplacingOccurrencesOfString:@"/System"
                                           withString:@""];




    NSDictionary* _dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];


    // firewall status
    int status = [[_dictionary valueForKey:@"globalstate"] integerValue];

    if (status == 0)
    {
        return NO;
    }

    return  YES;
}

If your application is being run by the user (ie, double-clicked in the Finder), any attempt by your application to create a socket listener will prompt the user to allow/deny that listener - and subsequently adjust the firewall settings accordingly - without any programmatic intervention on the part of your application. 如果您的应用程序正在由用户运行(即在Finder中双击),则您的应用程序尝试创建套接字侦听器的任何尝试都会提示用户允许/拒绝该侦听器-随后相应地调整防火墙设置-而无需应用程序方面的任何程序干预。

If the firewall in question is your router (a problem I recently had to deal with), you have a few options. 如果有问题的防火墙是您的路由器(我最近不得不处理这个问题),那么您有几种选择。 The best supported option is Bonjour/mDNSResponder (as long as you don't want to support a double-nat'ed situation). 最好的受支持的选项是Bonjour / mDNSResponder(只要您不想支持重复的情况)。 Apple provides an Objective-C wrapper application around the rather obtuse dns_sd.h: Apple提供了一个相当陈旧的dns_sd.h的Objective-C包装器应用程序:

http://developer.apple.com/library/mac/#samplecode/PortMapper/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007879-Intro-DontLinkElementID_2 http://developer.apple.com/library/mac/#samplecode/PortMapper/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007879-Intro-DontLinkElementID_2

Going the 3rd party route, take a look at TCM Port Mapper. 沿着第三方路线,看看TCM Port Mapper。 It uses some deprecated features and it'll take a bit of effort to get it running with ARC support (if that's important to you). 它使用了一些已过时的功能,并且需要一些努力才能使其在ARC支持下运行(如果这对您很重要)。

http://code.google.com/p/tcmportmapper/ http://code.google.com/p/tcmportmapper/

Both support UPnP and NAT-PMP. 两者都支持UPnP和NAT-PMP。

Finally, if your application is running as a daemon (without a user interface), you're going to have to become acquainted with ipfw. 最后,如果您的应用程序作为守护程序运行(没有用户界面),则您将必须熟悉ipfw。 Brace yourself. 振作起来。 Google for "ipfw os x". Google适用于“ ipfw os x”。 StackOverflow is preventing me from posting more than two links. StackOverflow阻止我发布两个以上的链接。 Brilliant. 辉煌。

Hope this helps.... 希望这可以帮助....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM