Re: Detecting cable unplug/Network failure
Re: Detecting cable unplug/Network failure
- Subject: Re: Detecting cable unplug/Network failure
- From: Peter Sichel <email@hidden>
- Date: Mon, 2 Mar 2009 13:18:13 -0500
On Mar 2, 2009, at 12:27 PM, David García wrote:
I'm quite new in Mac programming, and I've found some problems where
I got stuck in the network. The matter is that, I need to know when
there's a physical network error (most immediate cable being
unplugged)....
I would like to get some advice in the best way to solve that.
It sounds like you want to be notified when an interface goes down or
comes back up. In Mac OS X, there are a couple ways you might do
this. One way is to open a PF_SYSTEM socket and request to be
notified of the desired Kernel Events. You'll probably want a helper
tool that can run with root privileges to open your kernel event
socket and then use BSD descriptor passing to pass it back to your
application.
// assume we are executing as SUID root
// get a raw kernel event socket
newfd = socket(PF_SYSTEM, SOCK_RAW, SYSPROTO_EVENT);
if ( SendFD(STDOUT_FILENO, newfd ) == 0 ) {
perror( "failed to send fd" );
return 1;
}
close(newfd); // we're done with descriptor
Once you have one of these, you can get kernel event messages <sys/
kern_event.h> like KEV_DL_IF_DETACHING or KEV_DL_LINK_OFF as defined
in <net/if.h>.
The other way which is probably easier is to use the System
Configuration Framework and ask to be notified when certain keys
change like the IPv4 address list. It turns out this is a pretty
reasonable way to be notified when interfaces come and go since the
callback will tell you what changed. Apple published some sample code
on this a while ago which I vaguely recall was part of the MoreSCF
collection.
<http://developer.apple.com/samplecode/Networking/index-date.html>
Kind Regards,
- Peter Sichel
Sustainable Softworks
http://www.sustworks.com
_______________________________________________
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