[futurebasic] Network diagnostics

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : July 2011 : Group Archive : Group : All Groups

From: Robert Purves <listrp@...>
Date: Sun, 10 Jul 2011 20:02:28 +1200
I imagine that programs relying on the internet, either in general or at a specified server, would need less tech support if they implemented something like this (in pseudocode):

long if ( fn IsInternetConnected == _false )
  doDiagnose = fn NotifyUser
  if ( doDiagnose ) then fn DiagnoseBrokenNetwork
end if

This would be run once at startup, and also when the user chooses a command "Check Internet Connection".

An obscure part of CoreFoundation has two functions that seem ideal for the heavy lifting.

[1] CFNetDiagnosticCopyNetworkStatusPassively() returns in essence a go/no-go value. 

[2] CFNetDiagnosticDiagnoseProblemInteractively() launches /System/Library/CoreServices/Network\ Diagnostics.app. The nicely designed GUI walks you through the network setup in easy stages. Network status is displayed by no fewer than 8 indicator 'LEDs'. 

The demo below behaves as expected for three cases.
- everything connected and working
- Airport turned off
- ADSL modem powered off

In another case it does not.
With everything else connected properly, if the ADSL line is unplugged from the modem, the demo wrongly reports "Connected". Correspondingly in Network Diagnostics.app the LED lights for 'Internet' and 'Server' turn green: a visual lie.
Can someone please confirm this apparent serious bug in CFNetDiagnostics? I'm running OS X 10.6.6.

Robert P.

// CFNetDiagnostics.h
#define CFNetDiagnosticRef as ptr
#define CFNetDiagnosticStatus as CFIndex
_kCFNetDiagnosticNoErr = 0
_kCFNetDiagnosticErr = -66560
_kCFNetDiagnosticConnectionUp = -66559
_kCFNetDiagnosticConnectionIndeterminate = -66558
_kCFNetDiagnosticConnectionDown = -66557
toolbox fn CFNetDiagnosticCreateWithURL( CFAllocatorRef alloc, CFURLRef url ) = CFNetDiagnosticRef
toolbox fn CFNetDiagnosticDiagnoseProblemInteractively( CFNetDiagnosticRef details ) = CFNetDiagnosticStatus
toolbox fn CFNetDiagnosticCopyNetworkStatusPassively( CFNetDiagnosticRef details, CFStringRef *description ) = CFNetDiagnosticStatus

// main
include "ConsoleWindow"

dim as CFURLRef               url
dim as CFNetDiagnosticRef     diagnostic
dim as CFNetDiagnosticStatus  status

url = fn CFURLCreateWithString( _kCFAllocatorDefault, @"http://google.com", 0 )
diagnostic = fn CFNetDiagnosticCreateWithURL( _kCFAllocatorDefault, url )
CFRelease( url )
// Two ways of using a CFNetDiagnosticRef
// [1] Get status value
status = fn CFNetDiagnosticCopyNetworkStatusPassively( diagnostic, 0 )
print "CFNetDiagnosticStatus: " status
if ( status == _kCFNetDiagnosticConnectionUp ) then print "Connected"

// [2] Run Network Diagnostics.app
print "Starting Network Diagnostics..."
// the returned value of this function is irrelevant
fn CFNetDiagnosticDiagnoseProblemInteractively( diagnostic )

CFRelease( diagnostic )