Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Is there a clean way to get my default IP address.



Hi,

I'm trying to get my IP address under program control...
I cannot use the "gethostname/gethostbyname" combination
because I could have many devices.  Is there a Mac Carbon
call that can give me my IP address in dotted form?

For some reason,  my earlier implementation shown
below seems to "break" another call to it later on...

char *
getMyIPAddress(char *myhostname)
{
int result;
struct hostent *myHost;
struct in_addr dstAddr;
char *ip;
result = gethostname(myhostname, 100);
if (result == -1)
return NULL;
myHost = malloc(sizeof (struct hostent));
myHost = gethostbyname(myhostname); <---- this works as expected
memcpy((char *) &dstAddr, myHost->h_addr_list[0], myHost->h_length);
ip = inet_ntoa(dstAddr);
free(myHost);
return(ip);
}


This routine works fine,  but it "breaks" the program that calls this
code....  It returns my hostname in "myhostname" which is a char *
to a hostname.

The IP address it returns is a char * returned from inet_ntoa(dstAddr)
which I presume to be an allocated pointer, returning my IP address.
At least it works this way....   BUT!!!!!

later on,  when I call this block of code...

  struct hostent* he;
  if ((he = gethostbyname(buffer)) != NULL) {     <---- NOTE HERE
    if (strchr(he->h_name, '.') != NULL) {
      strncpy(buffer, he->h_name, sizeof(buffer));
    }
    else {
      WarningLog( << "local hostname does not contain a domain part");
    }
  }

As indicated above,  as soon as I call "gethostbyname" I get the
following...

*** malloc[3311]: Deallocation of a pointer not malloced: 0xa01c05f4; This could be a double free(), or free() called with the middle of an allocated block; Try setting environment variable MallocHelp to see tools to help debug
*** malloc[3311]: Deallocation of a pointer not malloced: 0x7c030378; This could be a double free(), or free() called with the middle of an allocated block; Try setting environment variable MallocHelp to see tools to help debug
*** malloc[3311]: Deallocation of a pointer not malloced: 0x48277375; This could be a double free(), or free() called with the middle of an allocated block; Try setting environment variable MallocHelp to see tools to help debug
*** malloc[3311]: Deallocation of a pointer not malloced: 0x7c601b78; This could be a double free(), or free() called


Then,  in the first routine I comment out the line...

myHost = gethostbyname(myhostname);

And by doing that,  it works just fine when I call the 2nd one.

Can someone please shed some light on this very confusing issue?

John




_______________________________________________ Do not post admin requests to the list. They will be ignored. Macnetworkprog mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/macnetworkprog/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.