Re: Is there a clean way to get my default IP address.
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: Josh Graessley <email@hidden>
- Date: Tue, 6 Dec 2005 10:40:59 -0800
In case anyone was going to copy and paste this code, please be aware
that you need to call freeifaddrs on ifap if getifaddrs succeeded.
-josh
On Dec 5, 2005, at 11:50 PM, Dalton Hamilton wrote:
Her is a small c routing that will give you your IP address. Of
course, there are multiple interfaces on a system and this example
will grab the en0 IP address. As I'm sure you are aware, "netstat -
in" will give you a list of interface names.
#include <util.h>
#include <ifaddrs.h>
#include <arpa/inet.h>
main()
{
struct ifaddrs *ifap, *ifa;
struct sockaddr_in *sa;
getifaddrs(&ifap);
for (ifa=ifap; ifa; ifa = ifa->ifa_next)
{
sa = (struct sockaddr_in *)ifa->ifa_addr;
if (sa->sin_family != AF_INET)
continue;
if(ifa->ifa_name == NULL)
continue;
if(strcmp(ifa->ifa_name,"en0") != 0)
continue;
if (ifa->ifa_addr == NULL)
continue;
printf("The IP for en0 is: %s\n",inet_ntoa(sa-
>sin_addr));
}
}
On Dec 5, 2005, at 10:28 PM, 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:
40apple.com
This email sent to email@hidden
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
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