How to disable internet access using Xcode/Cocoa

Discussion in 'Mac Programming' started by jhclare, Nov 14, 2014.

  1. jhclare macrumors newbie

    Nov 14, 2014
    Hi all,

    I wonder if someone can help.

    I have a software background and know C/C++ pretty well.

    However, Mac application development is new to me and I'm currently learning Xcode, Cocoa and Objective C.

    I'm wondering if there is a way to disable a Mac's internet connection for a specified time. I'm still learning the basics of Cocoa, and not sure if there's a way of doing this using the NS classes or do I need to use lower level stuff?

    Also, should I be learning Swift over Objective-C?


  2. cqexbesd macrumors regular

    Jun 4, 2009
    It depends on exactly what you mean by disabling an internet connection.

    If you want to jettison all network access you could just run ifconfig -l to list all the interfaces and ifconfig down each one. Of course that looses access to all networks, not just the Internet. It is possible to reimplement ifconfig functionality in your app but its probably not worth it. NB you will need root permission to use ifconfig.

    Alternatively, and perhaps it bit more elegant you could use the ipfw command to deny all traffic (though be careful to keep lo0 free). That would give you some more flexibility to, e.g. keep access to the local network segment.
  3. jhclare thread starter macrumors newbie

    Nov 14, 2014
    Thanks for the info!

    How would I go about doing that? Are there special NS classes to do that kind of thing?

    I basically want all network traffic disabled temporarily for a specified period of time.


  4. mfram, Nov 15, 2014
    Last edited: Nov 15, 2014

    mfram macrumors 65816

    Jan 23, 2010
    San Diego, CA USA
    I don't know of any NS classes that are going to configure the network. I looked in the API docs and didn't see anything offhand. It might be there, but I doubt it. I believe you will need lower-level Unix APIs.

    It's important to really understand what you mean by 'disable Internet access'. Does that mean configure a firewall? Does that mean turn off a network interface? You need to be specific.

    Computers have multiple network interfaces on them. It's probably not a good idea to turn off the loopback interface. It could break normal operation of the machine.

    The underlying API that programs like 'ifconfig' use is to perform ioctl() operations on socket devices. If you want to call the underlying APIs...

    1. Open a socket device. Probably a UDP socket is sufficient.
    2. Call ioctl() on the socket device with SIOCGIFCONF to get a list of interfaces.
    3. Loop through and look at the interface names and avoid loopback interfaces.
    4. Figure out which interfaces are active with SIOCGIFFLAGS
    5. Turn those interfaces off with SIOCSIFFLAGS
    6. Wait a while..
    7. Turn those interfaces back on.

    The best way to see how these APIs are called are by looking at the source code for programs like 'ifconfig'. That will be available on the Apple Open Source website.

    It's going to take root privileges to change the network configuration no matter which method you use.

    To emulate this on the command line, use the shell.

    'ifconfig -l' to list the interfaces.

    Choose the one that's active on your Mac. Probably something like 'en0'. But you'll have to check which one you are using.

    Use 'ifconfig en0 down' to shut the interface down.
    Use 'ifconfig en0 up' to turn it back on.

    If that's the behavior you want, then you will need to look at the ifconfig source code to see how the underlying API is called as stated above. I suppose you could create a shell process to just call 'ifconfig', but that just feels so dirty when you could call the underlying API much more easily.
  5. jhclare thread starter macrumors newbie

    Nov 14, 2014
    Thanks for all that info, very useful!

    However, I'd like to avoid having to use root if possible.

    I'm basically writing an application for minimal distractions while writing. I want to disable internet (ok, let's just say Wi-Fi to keep it simple) for a specified time.

    I also want the application to be full-screen and to prevent the user switching tasks during that time period! Is this possible?



Share This Page