views:

113

answers:

2

Hi all,

First off, you guys are all really helpful - thank you a lot!

Is there a way to figure out what type of network a Mac is connected to? WiFi? Cable? USB Modem? Any Cocoa / Foundation framework I can use to figure that out?

Thank you, Matthias

+2  A: 

You can tell whether the Mac is using WiFi, whether it is using a modem, and whether it is using hard Ethernet (which could be cable, DSL, or something else). Note that these are not mutually exclusive, there may be multiple connections of a single type (I have two Ethernet ports, for example), and that being connected to an IP network does not necessarily mean connection to the Internet.

You'll probably find the System Configuration API helpful. See also the framework reference.

You may also want to look at the source for HardwareGrowler in the Extras folder of the Growl source code.

Peter Hosey
well, after playing around with SCNetworkInterface... I just can't figure out what the active / currently used connection is.
Matthias Gansrigler
That's not singular. There may be multiple connections to IP networks. Anyway, while SCNetworkInterface may be easier (I haven't looked very deeply at it), the way I'm more familiar with is SCDynamicStore. You can use scutil to browse the dynamic store yourself and get an idea of where you have to look. See also http://developer.apple.com/mac/library/documentation/Networking/Reference/SCDynamicStoreKey/Reference/reference.html#//apple_ref/c/func/SCDynamicStoreKeyCreateNetworkInterfaceEntity .
Peter Hosey
A: 

You could ask for the route table and see what the default route is, that would tell you to some degree of accuracy what the active connection is.

This obviously has a lot to do with how you define 'some degree of accuracy'. Generally the main internet connection will also be the default route. I'm assuming you also want active connection to mean 'internet connection'.

netstat -rn Routing tables

Internet: Destination Gateway Flags Refs Use Netif Expire

default 192.168.0.1 UGSc 22 14 en1

so for me, my default route goes out the en1 interface. I think you need to be a lot more specific on what you mean by 'active/current' means to you.

CAVEAT: just because my default gw is on en1, does not necessarily mean that's where all my inet traffic goes. (it happens to be true in my case, but don't count on it as truth, but would probably be correct more than half the time).

Craig