Re: OT ping sample code
Re: OT ping sample code
- Subject: Re: OT ping sample code
- From: Larry Gerndt <email@hidden>
- Date: Mon, 12 Jan 2004 22:49:41 -0800
I should mention that Apple's "Simple Ping" example code is written with the
kind of error checking that causes code to get pretty unpretty, if you know
what I mean (i.e. incremental indents caused by "if errorr..." lines). I
found it painful to read and get a handle on, so I spent an evening
completely rewriting it using PowerPlant-based exception handling for error
checking. This really cleaned it up nicely, and I would be happy to share
it with anyone who wants it. I've tested it, and it works. One caveat: the
original allowed you to specify an array of servers to ping, but I reduced
that to just one. It could of course easily be modified to put multiples
back in.
Here's an example, comparing one of the original functions with my revised
version of it:
Original Version--------------------------------------:
int LookupHostAddress(const char* HostToPing, struct sockaddr_in*
HostAddress)
{
struct hostent * hostInformation;
//Checking input argument for validity
if ((HostToPing == NULL) || (HostAddress == NULL))
{
return(EINVAL); //invalid argument error return
}
//Initalizing host address
memset(HostAddress, 0, sizeof(struct sockaddr_in));
/* Calling inet_addr to try to interpret the input address as a
* IP string "xx.xx.xx.xx.xx. If this doesn't work then we will
* try gethostbyname which will resolve any domain names (i.e.
www.google.com)
* First Argument: The IP address expressed as a character string.
Assuming
* the string passed to us is an IP string we try to interpret the
string.
* if this doesn't work then we will try gethostbyname.
* Return Value: If properly formed input then what is returned is an
internet
* address structure. We will use this to set the internet address
structure
* in our socket address. If the request is malformed then the
constant
* INADDR_NONE will be returned for the internet address.
*/
HostAddress->sin_addr.s_addr = inet_addr(HostToPing);
//Checking to see if IP address was got correctly from character string.
if (HostAddress->sin_addr.s_addr != INADDR_NONE) //Success no error
returned!
{
//Setting socket family to internet (TCP/IP) since know it was IP
address interpreted
HostAddress->sin_family = AF_INET;
}
else //failure! now try with gethostbyname
{
/* The inet_addr call failed because the string isn't an IP address.
Instead
* we will try to interpret it as a host name (e.g. www.google.com).
* First Argument: The character string we are trying to interpret
of as a
* host name.
* Return Value: A host information structure on success which will
contain the
* information we need for connecting to the host. On failure a
NULL pointer
* will be returned
*/
hostInformation = gethostbyname(HostToPing);
if (hostInformation == NULL) //Failure!
{
//Failure unable to interpret character string as host name.
//We give up here by returning can't find host (unreachable
host).
return(EHOSTUNREACH);
}
//Setting the socket family for connecting to the host. We get this
information from
//the hostInformation structure directly
HostAddress->sin_family = hostInformation->h_addrtype;
//Now getting the internet address structure from our host
information structure.
//We copy the structure into ours using a memmove
memmove(&HostAddress->sin_addr, hostInformation->h_addr,
hostInformation->h_length);
}
/* Now for the host we have the address and connection family for the
host we can return this
* information
*/
return(0); //return zero indicating success.
}
My Version--------------------------------------:
OSStatus
LookupHostAddress(const char* inHostToPing, struct sockaddr_in*
ioHostAddress)
{
OSStatus err = noErr;
try {
bzero(ioHostAddress, sizeof(struct sockaddr_in));
ioHostAddress->sin_addr.s_addr = inet_addr(inHostToPing); //
succeeds for dotted decimal (e.g. "17.203.23.111)
if (ioHostAddress->sin_addr.s_addr != INADDR_NONE) {
ioHostAddress->sin_family = AF_INET;
} else {
struct hostent *hostInformation;
hostInformation = gethostbyname(inHostToPing); // succeeds for
DNS name (e.g. www.apple.com)
ThrowIfNil_(hostInformation);
ioHostAddress->sin_family = hostInformation->h_addrtype;
memmove(&ioHostAddress->sin_addr, hostInformation->h_addr,
hostInformation->h_length);
}
} catch(...) {
err = EHOSTUNREACH;
}
return err;
}
--
Larry Gerndt
AIM Handle: SonOfTheSonOfMan
Let the truth be told though the heavens fall -- James Garrison
_______________________________________________
macnetworkprog mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/macnetworkprog
Do not post admin requests to the list. They will be ignored.