In a
previous post about inspecting network connection settings on the PalmOS, I presented one piece of very incorrect/misleading information of particular interest to developers working on networked applications. I presented a code snippet to retrieve the assigned IP address when connecting via DHCP to a server. (Giving credit where credit is due, my error was very kindly pointed out to me by Henk Jonas.)
In my previous article, the snippet showed that you could retrieve the assigned IP address simply by querying the netIFSettingActualIPAddr using NetLibSettingGet(). In fact, the problem is slightly more complicated. The method that Henk pointed me to is implemented by looping through the NetLib interfaces after a connection has been established. While looping through the interfaces, you must look for the ones that are up and that are not the loopback interface. Once you have found an interface that is up, only then can you query netIFSettingActualIPAddr using NetLibIFSettingGet(). Here is one way to implement this:
UInt16 index = 0;
Err err = errNone;
while (err == errNone)
{
UInt16 idx;
UInt32 creatorID;
UInt16 instance;
//Retrieve the creator ID and instance of the indexed Interface
err = NetLibIFGet(netLibRefNum, index, &creatorID, &instance);
if (err == errNone)
{
//Check to see if the interface is up
UInt8 isUp = false;
UInt16 size = sizeof(isUp);
NetLibIFSettingGet(netLibRefNum,
creatorID,
instance,
netIFSettingUp,
&isUp,
&size);
//Make sure the interface is up and is not the
//loopback interface
if (isUp && creatorID != 'loop')
{
//Once we get here, it's simply a matter of
//grabbing the IP address
UInt32 address;
size = sizeof(address);
NetLibIFSettingGet(netLibRefNum,
creatorID,
instance,
netIFSettingActualIPAddr,
&address,
&size);
//Once you get here, you have to do something
//with the IP address. Do you just get the
//first one and stop, or do you want all the
//assigned IP addresses of Up Interfaces?
//That determines whether or not you want to
//break out of this loop. I leave that as an
//exercise for the reader. -Jon
}
//Go on to the next Interface
index++;
}
}
As always, I hope this helps you in your endeavors, and if you feel like it, I'd love to hear about it!
-Jon
|