Re: How to get my IP Address ?
Re: How to get my IP Address ?
- Subject: Re: How to get my IP Address ?
- From: Marc Majka <email@hidden>
- Date: Mon, 29 Nov 2004 11:51:04 -0800
See getifaddrs(3). Your system actually has a bunch of IP addresses, so you need to know which of them is interesting in your application. The sample code below just walks through the list of interfaces returned by getifaddrs() and prints IPv4 and IPv6 addresses. If you are only interested in using, say, IPv4, you can just look for AF_INET. If you don't care about loopback, you can ignore "lo0". That will in most cases get you down to a list of between one and three addresses. If you are using a dialup / ppp interface, that will give you an IPv4 address. If you are using Ethernet, that will give you another address. If you have airport, it will give you another.
--
Marc Majka
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <ifaddrs.h>
#include <errno.h>
int
main(int argc, char *argv[])
{
struct ifaddrs *myaddrs, *ifa;
struct sockaddr_in *s4;
struct sockaddr_in6 *s6;
int status;
/* buf must be big enough for an IPv6 address (e.g. 3ffe:2fa0:1010:ca22:020a:95ff:fe8a:1cf8) */
char buf[64];
status = getifaddrs(&myaddrs);
if (status != 0)
{
perror("getifaddrs");
exit(1);
}
for (ifa = myaddrs; ifa != NULL; ifa = ifa->ifa_next)
{
if (ifa->ifa_addr == NULL) continue;
if ((ifa->ifa_flags & IFF_UP) == 0) continue;
if (ifa->ifa_addr->sa_family == AF_INET)
{
s4 = (struct sockaddr_in *)(ifa->ifa_addr);
if (inet_ntop(ifa->ifa_addr->sa_family, (void *)&(s4->sin_addr), buf, sizeof(buf)) == NULL)
{
printf("%s: inet_ntop failed!\n", ifa->ifa_name);
}
else
{
printf("%s: %s\n", ifa->ifa_name, buf);
}
}
else if (ifa->ifa_addr->sa_family == AF_INET6)
{
s6 = (struct sockaddr_in6 *)(ifa->ifa_addr);
if (inet_ntop(ifa->ifa_addr->sa_family, (void *)&(s6->sin6_addr), buf, sizeof(buf)) == NULL)
{
printf("%s: inet_ntop failed!\n", ifa->ifa_name);
}
else
{
printf("%s: %s\n", ifa->ifa_name, buf);
}
}
}
freeifaddrs(myaddrs);
exit(0);
}
On 27 Nov, 2004, at 07:53, Mark Gilbert wrote:
Folks.
I have setup a very simple socket from one machine to another. I want to send back very very simple HTML.
within the HTML I need to include my own IP address, but I am not sure how to get it.
I tried gethostbyname("localhost"), but this returns 127.0.0.1 which works on the local machine, but is not my address on the network.
For example, I happen to know that my machine is 192.169.1.104, and I can reach my machine remotely using this. How does my app learn that this is my address ?
I am sure its a really simple trick, but I am obviously missing it.
Cheers
Mark Gilbert.
--
email@hidden
Tel: +44 208 340 5677
fax: +44 870 055 7790
http://www.gallery.co.uk
New Product ! - Zen MetaCorder
Location Sound Field Recorder
http://www.metacorder.info
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden