Re: How to find the primary network interface?
Re: How to find the primary network interface?
- Subject: Re: How to find the primary network interface?
- From: Allan Nathanson <email@hidden>
- Date: Thu, 12 Oct 2006 09:37:02 -0400
On Oct 12, 2006, at 7:27 AM, Stephane wrote:
In Mac OS X, there can be multiple network interface enabled and
running at the same time. For instance, you can be connected to the
Internet though the Ethernet Port and sharing the connection with
other computers on AirPort. But AFAIK, there is only one interface
that is used to send packets to the outside using applications such
as Safari, Mail, iChat, ftp, etc...
I would like to know what this interface is.
I had a look at the SystemConfiguration frameworks API and while I
saw some yummy features, I didn't seem to find any API to do this.
A look at the Networking sample code list didn't show anything either.
So is there a way with the system configuration framework to get
this information without having to use some "tricky" stuff such as
creating a socket to find which default network interface is going
to be used? Could it be something like enumerating all the network
configurations and finding the first one with an enabled IP address?
How about something like :
CFStringRef
getPrimaryAddress(void)
{
SCDynamicStoreRef store;
CFStringRef address = NULL;
store = SCDynamicStoreCreate(NULL, CFSTR("getPrimaryAddress"), NULL,
NULL);
if (store != NULL) {
CFStringRef globalKey;
CFDictionaryRef globalDict;
globalKey = SCDynamicStoreKeyCreateNetworkGlobalEntity(NULL,
kSCDynamicStoreDomainState,
kSCEntNetIPv4);
globalDict = SCDynamicStoreCopyValue(store, globalKey);
CFRelease(globalKey);
if (globalDict != NULL) {
CFStringRef primaryService;
primaryService = CFDictionaryGetValue(globalDict,
kSCDynamicStorePropNetPrimaryService);
if (primaryService != NULL) {
CFStringRef ipv4Key;
CFDictionaryRef serviceDict;
ipv4Key = SCDynamicStoreKeyCreateNetworkServiceEntity(NULL,
kSCDynamicStoreDomainState,
primaryService,
kSCEntNetIPv4);
serviceDict = SCDynamicStoreCopyValue(store, ipv4Key);
CFRelease(ipv4Key);
if (serviceDict != NULL) {
CFArrayRef addresses;
addresses = CFDictionaryGetValue(serviceDict,
kSCPropNetIPv4Addresses);
if ((addresses != NULL) && CFArrayGetCount(addresses) > 0)) {
// get primary service address!!!
address = CFArrayGetValueAtIndex(addresses, 0);
CFRetain(address);
}
CFRelease(serviceDict);
}
}
CFRelease(globalDict);
}
CFRelease(store);
}
return address;
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden