• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Is there a clean way to get my default IP address.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Subject: Re: Is there a clean way to get my default IP address.
  • From: Rustam Muginov <email@hidden>
  • Date: Tue, 6 Dec 2005 10:55:11 +0300

Here is a scary but useful technote about this question:
http://developer.apple.com/technotes/tn/tn1145.html
--
Sincerely,
	Rustam Muginov

On Dec 6, 2005, at 12:28 AM, John Draper wrote:

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:
email@hidden


This email sent to email@hidden


_______________________________________________ Do not post admin requests to the list. They will be ignored. Macnetworkprog mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >Is there a clean way to get my default IP address. (From: John Draper <email@hidden>)

  • Prev by Date: Re: Is there a clean way to get my default IP address.
  • Next by Date: Re: Is there a clean way to get my default IP address.
  • Previous by thread: Re: Is there a clean way to get my default IP address.
  • Next by thread: Kernel Debugging & mbuf & opaqueness
  • Index(es):
    • Date
    • Thread