| Author |
Message |
vineeth *nix forums beginner
Joined: 11 Oct 2005
Posts: 2
|
Posted: Tue Oct 11, 2005 1:24 pm Post subject:
finding the IP address of the local machine.
|
|
|
hello,
Can anyone please tell me the way to find out the IP address of the
machine.
currently i am using gethostent(), but this always returns 127.0.0.1
pls share your ideas. |
|
| Back to top |
|
 |
maxim.yegorushkin@gmail.c *nix forums Guru Wannabe
Joined: 27 Jul 2005
Posts: 182
|
Posted: Tue Oct 11, 2005 1:24 pm Post subject:
Re: finding the IP address of the local machine.
|
|
|
vineeth wrote:
| Quote: | hello,
Can anyone please tell me the way to find out the IP address of the
machine.
currently i am using gethostent(), but this always returns 127.0.0.1
pls share your ideas.
|
man netdevice |
|
| Back to top |
|
 |
PP *nix forums beginner
Joined: 11 Apr 2005
Posts: 10
|
Posted: Tue Oct 11, 2005 1:24 pm Post subject:
Re: finding the IP address of the local machine.
|
|
|
vineeth wrote:
| Quote: | currently i am using gethostent(), but this always returns 127.0.0.1
pls share your ideas.
|
Hello there, try this function...
/* getip: returns a string containing hosts ip address, it receives
* hostname parameter */
char * getip(char * hname)
{
int x, ip_len = 16;
char * str = NULL, * ip = NULL;
struct hostent * hent = NULL;
hent = gethostbyname(hname);
str = hent -> h_addr_list[0];
ip = malloc(ip_len * sizeof(char));
memset(ip, EOS, ip_len);
x = (*str < 0) ? (256 + *str) : *str;
sprintf(ip, "%d",x); str++;
x = (*str < 0) ? (256 + *str) : *str;
sprintf(ip, "%s.%d",ip, x); str++;
x = (*str < 0) ? (256 + *str) : *str;
sprintf(ip, "%s.%d",ip, x); str++;
x = (*str < 0) ? (256 + *str) : *str;
sprintf(ip, "%s.%d%c",ip, x, EOS);
return (ip);
} |
|
| Back to top |
|
 |
mwql *nix forums beginner
Joined: 11 Oct 2005
Posts: 9
|
Posted: Tue Oct 11, 2005 1:28 pm Post subject:
Re: finding the IP address of the local machine.
|
|
|
|
try 'ifconfig' |
|
| Back to top |
|
 |
Pascal Bourguignon *nix forums addict
Joined: 08 Mar 2005
Posts: 62
|
Posted: Tue Oct 11, 2005 1:55 pm Post subject:
Re: finding the IP address of the local machine.
|
|
|
"vineeth" <nvineeth@gmail.com> writes:
| Quote: | Can anyone please tell me the way to find out the IP address of the
machine.
currently i am using gethostent(), but this always returns 127.0.0.1
pls share your ideas.
|
A machine has no IP address. Interfaces have.
--
__Pascal Bourguignon__ http://www.informatimago.com/ |
|
| Back to top |
|
 |
maxim.yegorushkin@gmail.c *nix forums Guru Wannabe
Joined: 27 Jul 2005
Posts: 182
|
Posted: Tue Oct 11, 2005 3:44 pm Post subject:
Re: finding the IP address of the local machine.
|
|
|
PP wrote:
| Quote: | vineeth wrote:
currently i am using gethostent(), but this always returns 127.0.0.1
pls share your ideas.
Hello there, try this function...
|
gethostbyname is a resolver function. It checks if the host name is
"localhost" and if so returns 127.0.0.1, otherwise it does a DNS
lookup. |
|
| Back to top |
|
 |
Floyd L. Davidson *nix forums Guru
Joined: 08 Mar 2005
Posts: 405
|
Posted: Tue Oct 11, 2005 4:23 pm Post subject:
Re: finding the IP address of the local machine.
|
|
|
"PP" <prasad_3483@yahoo.co.in> wrote:
| Quote: | Hello there, try this function...
|
Here's a correct way to do it (required header files may
vary from one platform to another).
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <arpa/inet.h>
int main(int argc, char **argv)
{
struct hostent *he;
char **s;
if (2 != argc) {
fprintf(stderr,"Usage: %s hostname\n", argv[0]);
exit(EXIT_FAILURE);
}
if (NULL == (he = gethostbyname(argv[1]))) {
char *why;
switch (h_errno) {
case HOST_NOT_FOUND:
why = "Host is unknown";
break;
case TRY_AGAIN:
why = "Temporary DNS failure, try again";
break;
case NO_RECOVERY:
why = "Unrecoverable DNS error";
break;
case NO_DATA:
why = "Host has no IP address";
break;
default:
why = "Unknown error";
break;
}
fprintf(stderr,"Error: %s: %s\n", argv[1], why);
return EXIT_FAILURE;
}
printf("%s", argv[1]);
if (he->h_name[0]) {
printf(" (%s)", he->h_name);
}
printf("\n Addresses:\n");
s = he->h_addr_list;
while (*s) {
printf(" %s\n", inet_ntoa(*(struct in_addr *) *s));
++s;
}
return EXIT_SUCCESS;
}
--
Floyd L. Davidson <http://www.apaflo.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) floyd@apaflo.com |
|
| Back to top |
|
 |
Floyd L. Davidson *nix forums Guru
Joined: 08 Mar 2005
Posts: 405
|
Posted: Tue Oct 11, 2005 9:47 pm Post subject:
Re: finding the IP address of the local machine.
|
|
|
"Maxim Yegorushkin" <maxim.yegorushkin@gmail.com> wrote:
| Quote: | PP wrote:
vineeth wrote:
currently i am using gethostent(), but this always returns 127.0.0.1
pls share your ideas.
Hello there, try this function...
gethostbyname is a resolver function. It checks if the host name is
"localhost" and if so returns 127.0.0.1, otherwise it does a DNS
lookup.
|
Actually it just simply does a DNS. If your /etc/hosts file has
localhost as something other than 127.0.0.1, then getshostbyname()
will see that and return whatever is there.
If there is no localhost listed in your /etc/hosts, the DNS will
extend to a remote server, and that will tell you it is 127.0.0.1.
--
Floyd L. Davidson <http://www.apaflo.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) floyd@apaflo.com |
|
| Back to top |
|
 |
David Schwartz *nix forums Guru
Joined: 26 Feb 2005
Posts: 914
|
Posted: Wed Oct 12, 2005 4:00 am Post subject:
Re: finding the IP address of the local machine.
|
|
|
"vineeth" <nvineeth@gmail.com> wrote in message
news:1129022687.868697.277240@g47g2000cwa.googlegroups.com...
| Quote: | hello,
Can anyone please tell me the way to find out the IP address of the
machine.
currently i am using gethostent(), but this always returns 127.0.0.1
pls share your ideas.
|
127.0.0.1 is the IP address of the local machine, isn't it? If the
question was, "what IP address should I connect to if I want to connect to
the local machine", then you have the answer. Presumably, the real question
is something else. Without knowing the real question, it is very hard to
give you the right answer. Why do you want to know? What do you plan to do
with this information?
DS |
|
| Back to top |
|
 |
Andrei Voropaev *nix forums beginner
Joined: 30 Mar 2005
Posts: 42
|
Posted: Wed Oct 12, 2005 10:49 am Post subject:
Re: finding the IP address of the local machine.
|
|
|
In comp.unix.programmer vineeth <nvineeth@gmail.com> wrote:
| Quote: | Can anyone please tell me the way to find out the IP address of the
machine.
currently i am using gethostent(), but this always returns 127.0.0.1
pls share your ideas.
|
Depends on what you really want If you just want to create a listener
that shall accept connections from the outside, then use INADDR_ANY.
This will bind your socket to all available interfaces/addresses. Read
man 7 ip.
--
Minds, like parachutes, function best when open |
|
| Back to top |
|
 |
kironv@gmail.com *nix forums beginner
Joined: 12 Oct 2005
Posts: 1
|
Posted: Wed Oct 12, 2005 1:08 pm Post subject:
Re: finding the IP address of the local machine.
|
|
|
You need this info for protocols like SDP ( for ex: invite in SIP )
where you want to send the IP address of your machine and the other
machine will connect to it...
Here is the correct program which prints the IP addresses of all the
interfaces found in the machine.
You have to take the eth0 ip address.
____
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/sockios.h>
#include <asm/ioctl.h>
#include <netinet/in.h>
#include <linux/if.h>
#include <errno.h>
/* max number of interfaces */
#define MAX_IF_NUM 10
int main()
{
struct ifconf selfcfg;
struct ifreq selfreq[MAX_IF_NUM];
struct in_addr inaddr;
int sock, i;
selfcfg.ifc_ifcu.ifcu_req = (struct ifreq*)(&selfreq);
selfcfg.ifc_len = MAX_IF_NUM * sizeof(struct ifreq);
sock = socket(PF_INET, SOCK_DGRAM, 0);
if (ioctl(sock, SIOCGIFCONF, &selfcfg))
{
perror("ioctl:");
return -1;
}
for (i = 0; i < (selfcfg.ifc_len / sizeof(struct ifreq)); i++)
{
inaddr.s_addr = (*(int
*)(&selfreq[i].ifr_ifru.ifru_addr.sa_data[2]));
printf("%s:\t%s \n", selfreq[i].ifr_ifrn.ifrn_name,
inet_ntoa(inaddr));
}
if (close(sock))
{
perror("close:");
}
return 0;
}
_____
done! |
|
| Back to top |
|
 |
maxim.yegorushkin@gmail.c *nix forums Guru Wannabe
Joined: 27 Jul 2005
Posts: 182
|
Posted: Wed Oct 12, 2005 3:10 pm Post subject:
Re: finding the IP address of the local machine.
|
|
|
kironv@gmail.com wrote:
| Quote: | You need this info for protocols like SDP ( for ex: invite in SIP )
where you want to send the IP address of your machine and the other
machine will connect to it...
|
For SIP you have to use STUN to discover your IP address, since it's
quite likely the client is behind a NAT. |
|
| Back to top |
|
 |
David Schwartz *nix forums Guru
Joined: 26 Feb 2005
Posts: 914
|
Posted: Wed Oct 12, 2005 6:54 pm Post subject:
Re: finding the IP address of the local machine.
|
|
|
<kironv@gmail.com> wrote in message
news:1129122488.343468.236090@g49g2000cwa.googlegroups.com...
| Quote: | You need this info for protocols like SDP ( for ex: invite in SIP )
where you want to send the IP address of your machine and the other
machine will connect to it...
|
Why wouldn't it just connect to the machine it received the information
from using the address that was used to send the information to it?
DS |
|
| Back to top |
|
 |
Michael Wojcik *nix forums Guru
Joined: 10 Apr 2005
Posts: 336
|
Posted: Wed Oct 12, 2005 9:44 pm Post subject:
Re: finding the IP address of the local machine.
|
|
|
In article <1129122488.343468.236090@g49g2000cwa.googlegroups.com>, kironv@gmail.com writes:
| Quote: | You need this info for protocols like SDP ( for ex: invite in SIP )
where you want to send the IP address of your machine and the other
machine will connect to it...
|
Callback protocols only work when you provide them with the correct
(or rather *a* correct) return address. If the host is multihomed,
it may be that not all addresses are reachable by the peer,[1] or
that not all of them will accept the return connection (eg because of
firewalling or a listener bound to a particular interface), or that
some interfaces are suboptimal (eg because they're further from the
peer in the network topology). If the host is behind a NATing router,
it may well be that none of the local interfaces are suitable for
callback.
Programmatically discovering an appropriate local IP address for
callback is not trivial. (Having the peer return the source address
from an initial outbound connection should work in many cases, but is
still prone to failure under NAT.)
| Quote: | Here is the correct program which prints the IP addresses of all the
interfaces found in the machine.
|
If the machine is running Linux and has no more than 10 interfaces
with IP addresses.
| Quote: | You have to take the eth0 ip address.
|
And who says that the OP's program should be using the address for
eth0? Who says that the systems the OP's program will run on even
*have* an eth0?
The OP needs to explain what problem he's trying to solve, but my
guess is that there is no easy solution. Multihoming, multiple
routes, and so on are a features of TCP/IP; they're very useful, but
they eliminate simplistic solutions to some problems. And NAT is a
fact of life.
1. Indeed, if we include the loopback interface, most hosts are
multihomed, and at least one interface's address won't work for
callback.
--
Michael Wojcik michael.wojcik@microfocus.com
HTML is as readable as C. You can take this either way. -- Charlie Gibbs |
|
| Back to top |
|
 |
maxim.yegorushkin@gmail.c *nix forums Guru Wannabe
Joined: 27 Jul 2005
Posts: 182
|
Posted: Thu Oct 13, 2005 7:14 am Post subject:
Re: finding the IP address of the local machine.
|
|
|
David Schwartz wrote:
| Quote: | kironv@gmail.com> wrote in message
news:1129122488.343468.236090@g49g2000cwa.googlegroups.com...
You need this info for protocols like SDP ( for ex: invite in SIP )
where you want to send the IP address of your machine and the other
machine will connect to it...
Why wouldn't it just connect to the machine it received the information
from using the address that was used to send the information to it?
|
Because that's the way SDP was designed.
http://www.ietf.org/rfc/rfc2327.txt |
|
| Back to top |
|
 |
Google
|
|
| Back to top |
|
 |
|