Open main menu

Get IP With OTInetGetInterfaceInfo

>I'm trying to get the IP assigned to the current host and call OTInetGetInterfaceInfo to do so. The problem 
I'm having is there are no interfaces defined until an application such as Telnet makes a connection. 
>>From then out the interface is defined. Can someone explain what's going on 
>and the correct way to get the current IP. 

Yes, this is a major pain. Here's what I currently do: 

/*---------------------------------------------------------------------------- 
NetGetMyAddr

Get this Mac's IP address.

Exit: function result = error code.
*addr = the IP address of this Mac.

With Open Transport, if the Mac has more than one IP interface, the IP address of the default interface is
returned.
----------------------------------------------------------------------------*/ 

OSErr NetGetMyAddr (unsigned long *addr) {
struct GetAddrParamBlock pBlock;
OSErr err = noErr, giveTimeErr = noErr;
unsigned long myAddr;
InetInterfaceInfo ifaceInfo;
TMyOTInetSvcInfo svcInfo;

if (gHaveOT) {

err = OTInetGetInterfaceInfo(&ifaceInfo, kDefaultInetInterface); if (err == kOTNotFoundErr) {
/* If OT is not loaded, the OTInetGetInterfaceInfo call fails with 
error kOTNotFoundErr. In this case, we open an inet services endpoint temporarily just to force OT to load, and we try the call again. */
err = MyOTOpenInetServices(&svcInfo);
if (err != noErr) return TranslateErrorCode(err); err = OTInetGetInterfaceInfo(&ifaceInfo, kDefaultInetInterface); OTCloseProvider(svcInfo.ref);
}
if (err != noErr) return TranslateErrorCode(err); myAddr = ifaceInfo.fAddress;

} else {

memset(&pBlock, 0, sizeof(pBlock));
pBlock.ioResult = 1;
pBlock.csCode = ipctlGetAddr;
pBlock.ioCRefNum = gRefNum;
PBControlAsync((ParmBlkPtr)&pBlock);
while (pBlock.ioResult > 0) {
giveTimeErr = (*gGiveTime)();
if (err == noErr) err = giveTimeErr;
}
if (err != noErr) return TranslateErrorCode(err); err = pBlock.ioResult;
if (err != noErr) return TranslateErrorCode(err); myAddr = pBlock.ourAddress;

}

*addr = myAddr;
return noErr;
}