curl-library
Re: Connected ?
Date: Mon, 14 Jul 2003 22:43:02 +0200
I can contribute with how it is done in Mac OS X / Darwin.
First, the following framework needs to be linked against:
#include <SystemConfiguration/SystemConfiguration.h>
Then the following function can be used:
/*!
@function SCNetworkCheckReachabilityByName
@discussion Determines if the given network host/node name is
reachable using the current network configuration.
@param nodename The node name of the desired host. This name
would
be the same as that passed to gethostbyname() or
getaddrinfo().
@param flags A pointer to memory that will be filled with a
set of SCNetworkConnectionFlags detailing the
reachability
of the specified node name.
@result TRUE if the network connection flags are valid; FALSE
if the
status could not be determined.
*/
Boolean
SCNetworkCheckReachabilityByName (
const char
*nodename,
SCNetworkConnectionFlags
*flags
);
The flags structure is described like this:
/*!
@enum SCNetworkConnectionFlags
@discussion Flags that indicate whether the specified network
nodename/address is reachable, requires a connection,
requires some user intervention in establishing the
connection, and whether the calling application must
initiate the connection using the (TBD???) API.
@constant kSCNetworkFlagsTransientConnection
This flag indicates that the specified nodename/address
can
be reached via a transient (e.g. PPP) connection.
@constant kSCNetworkFlagsReachable
This flag indicates that the specified nodename/address
can
be reached using the current network configuration.
@constant kSCNetworkFlagsConnectionRequired
This flag indicates that the specified nodename/address
can
be reached using the current network configuration but a
connection must first be established.
As an example, this status would be returned for a
dialup
connection that was not currently active but could
handle
network traffic for the target system.
@constant kSCNetworkFlagsConnectionAutomatic
This flag indicates that the specified nodename/address
can
be reached using the current network configuration but a
connection must first be established. Any traffic
directed
to the specified name/address will initiate the
connection.
@constant kSCNetworkFlagsInterventionRequired
This flag indicates that the specified nodename/address
can
be reached using the current network configuration but a
connection must first be established. In addition, some
form of user intervention will be required to establish
this connection (e.g. providing a password,
authentication
token, etc.).
@constant kSCNetworkFlagsIsLocalAddress
This flag indicates that the specified nodename/address
is one associated with a network interface on the
current
system.
@constant kSCNetworkFlagsIsDirect
This flag indicates that network traffic to the
specified
nodename/address will not go through a gateway but is
routed
directly to one of the interfaces in the system.
*/
enum {
kSCNetworkFlagsTransientConnection = 1<<0,
kSCNetworkFlagsReachable = 1<<1,
kSCNetworkFlagsConnectionRequired = 1<<2,
kSCNetworkFlagsConnectionAutomatic = 1<<3,
kSCNetworkFlagsInterventionRequired = 1<<4,
kSCNetworkFlagsIsLocalAddress = 1<<16,
kSCNetworkFlagsIsDirect = 1<<17
};
typedef uint32_t SCNetworkConnectionFlags;
The function returns the result of the query in the structure, so it is
possible to see wether it is possible to establish a connection to the
named host. Typical usage:
SCNetworkConnectionFlags flags;
if( !SCNetworkCheckReachabilityByName( "www.apple.com", &flags );
; //handle error
if( flags & kSCNetworkFlagsReachable )
; // network is reachable
Some additional documentation exists in the SCNetwork.h header, and
other SystemConfiguration headers.
/ Regards, David Remahl
On måndag 14 juli 2003, at 20.23PM, Michael Roberts wrote:
> Is there a list of "ways to figure out connectedness" for various
> OS's? That'd be a pretty interesting new feature for libcurl: a
> system-independent way of determining connectivity. People ask about
> it often enough.
>
> Michael
>
> Andrew Francis wrote:
>
> On Mon, Jul 14, 2003 at 10:37:41AM +0100, Paul Johnson wrote:
>
> Fair point. I'm on win32 trying to talk to a unix server across an
> internet
> connection. curl has initalised successfully and returned with no
> errors,
> but I need to make my own program stop right there if an auto-dial flag
> isn't set and theres no internet connection.
>
> Take a look at InternetGetConnectedState and InternetDial, in
> <wininet.h>. You want something along these lines:
>
> DWORD inetInfo;
> if(InternetGetConnectedState(&inetInfo, 0) == FALSE) {
> if(!weAreAllowedToConnect)
> bombout(); // not connected and not allowed to fix that
>
> if(InternetDial(0, 0, 0, &inetInfo, 0) != ERROR_SUCCESS)
> bombout(); // we tried to dial up but couldn't
> }
>
> I've pretty much paraphrased this from some source I have lying around
> but I wrote it eons ago and don't remember what all of those zeroes
> mean,
> so I wouldn't recommend using this blindly without going to the docs
> first.
>
> You want to do this _before_ initialising WinSock. (I believe
> curl_easy_init() takes care of that these days, so do this before
> hitting curl).
>
> Cheers
-------------------------------------------------------
This SF.Net email sponsored by: Parasoft
Error proof Web apps, automate testing & more.
Download & eval WebKing and get a free book.
www.parasoft.com/bulletproofapps1
Received on 2003-07-14