|
|
|
|
|
|
| Author |
Message |
Pascal Bourguignon *nix forums addict
Joined: 08 Mar 2005
Posts: 62
|
Posted: Wed Jun 28, 2006 1:51 pm Post subject:
Re: finding the IP address of the local machine.
|
|
|
"vineeth" <nvineeth@gmail.com> writes:
| 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.
|
Yes. That's because computers don't get IP addresses on an IP network.
Only interfaces get IP addresses.
So you must first get the list of interfaces to this computer, then
the list of IP adresses to each interface.
Try:
ifconfig(1) ifconfig -a
On linux:
ip( ip addr show
netdevice(7)
--
__Pascal Bourguignon__ http://www.informatimago.com/
CAUTION: The mass of this product contains the energy equivalent of
85 million tons of TNT per net ounce of weight. |
|
| Back to top |
|
 |
Michael Wojcik *nix forums Guru
Joined: 10 Apr 2005
Posts: 336
|
Posted: Fri Oct 14, 2005 4:13 pm Post subject:
Re: finding the IP address of the local machine.
|
|
|
In article <dimkmc$o8a$1@nntp.webmaster.com>, "David Schwartz" <davids@webmaster.com> writes:
| Quote: | "Michael Wojcik" <mwojcik@newsguy.com> wrote in message
news:dimi0q0200c@news4.newsguy.com...
In article <dijm5i$mc5$1@nntp.webmaster.com>, "David Schwartz"
davids@webmaster.com> writes:
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 if NAT is being used, the source address of the packets the
host receives will not be an actual IP address of the peer.
[...]
I agree. However, this still doesn't argue against my point that you
should want an address for the peer that is globally unique and routable
over the network, and that will be the source address of the data you
received, not the address the other end sent them from.
|
Ah, I see your point. I thought you were asking, in effect, "why
wouldn't the source address always work (as a callback address)?",
not pointing out that the peer's actual (possibly unreachable) IP
address was even less useful.
In reality, neither may be useful, but (as in the case of the 3Com
ISDN modem I mentioned) there are some situations where the source
address might be more useful than the peer's actual address. I
agree with you there.
--
Michael Wojcik michael.wojcik@microfocus.com |
|
| Back to top |
|
 |
maxim.yegorushkin@gmail.c *nix forums Guru Wannabe
Joined: 27 Jul 2005
Posts: 182
|
Posted: Fri Oct 14, 2005 6:51 am Post subject:
Re: finding the IP address of the local machine.
|
|
|
Michael Wojcik wrote:
| Quote: | In article <dijm5i$mc5$1@nntp.webmaster.com>, "David Schwartz" <davids@webmaster.com> writes:
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 if NAT is being used, the source address of the packets the
host receives will not be an actual IP address of the peer. Replies
back to that address and source port will work, because the NAT
system will route them; but arbitrary SYNs to that address probably
will not, because the NAT system won't have any reason to direct them
to the (interface on the) system that originated the first
conversation.
|
I don't see how these SYN/NAT concerns apply here. The IP addresses
stated in SDP are used for video/audio streams which are delivered via
RTP, which is a UDP based protocol. For traversing NATs with UDP they
use techniques like UDP hole punching. A good paper on the subject is
referred here http://en.wikipedia.org/wiki/UDP_hole_punching. |
|
| Back to top |
|
 |
David Schwartz *nix forums Guru
Joined: 26 Feb 2005
Posts: 914
|
Posted: Thu Oct 13, 2005 9:48 pm Post subject:
Re: finding the IP address of the local machine.
|
|
|
"Michael Wojcik" <mwojcik@newsguy.com> wrote in message
news:dimi0q0200c@news4.newsguy.com...
| Quote: | In article <dijm5i$mc5$1@nntp.webmaster.com>, "David Schwartz"
davids@webmaster.com> writes:
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 if NAT is being used, the source address of the packets the
host receives will not be an actual IP address of the peer.
|
Right, but it will be a more useful address than the actual IP address
of the peer.
| Quote: | Replies
back to that address and source port will work, because the NAT
system will route them; but arbitrary SYNs to that address probably
will not, because the NAT system won't have any reason to direct them
to the (interface on the) system that originated the first
conversation.
|
If the host is behind NAT, SYNs to the real IP address *definitely*
won't work. So this argument actually supports my position that you should
use the address you got the packets from, not the address the other machine
sent them from.
| Quote: | Some NAT systems use heuristics to route incoming SYNs - for example,
if machine A is behind a NAT and it opens a conversation to machine
B, and no other system behind the NAT opens a conversation to machine
B, and machine B sends a SYN to the NAT address within a certain
time, some NATs will automatically route it to machine A. I had a
3Com ISDN modem with NAT that could be configured to do that. But
it's not foolproof, universally implemented, or always desirable.
|
I agree. However, this still doesn't argue against my point that you
should want an address for the peer that is globally unique and routable
over the network, and that will be the source address of the data you
received, not the address the other end sent them from.
DS |
|
| Back to top |
|
 |
Michael Wojcik *nix forums Guru
Joined: 10 Apr 2005
Posts: 336
|
Posted: Thu Oct 13, 2005 9:02 pm Post subject:
Re: finding the IP address of the local machine.
|
|
|
In article <dijm5i$mc5$1@nntp.webmaster.com>, "David Schwartz" <davids@webmaster.com> writes:
| 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 if NAT is being used, the source address of the packets the
host receives will not be an actual IP address of the peer. Replies
back to that address and source port will work, because the NAT
system will route them; but arbitrary SYNs to that address probably
will not, because the NAT system won't have any reason to direct them
to the (interface on the) system that originated the first
conversation.
Some NAT systems use heuristics to route incoming SYNs - for example,
if machine A is behind a NAT and it opens a conversation to machine
B, and no other system behind the NAT opens a conversation to machine
B, and machine B sends a SYN to the NAT address within a certain
time, some NATs will automatically route it to machine A. I had a
3Com ISDN modem with NAT that could be configured to do that. But
it's not foolproof, universally implemented, or always desirable.
--
Michael Wojcik michael.wojcik@microfocus.com
Please enjoy the stereo action fully that will surprise you. -- Pizzicato Five |
|
| Back to top |
|
 |
Nils O. Selåsdal *nix forums Guru Wannabe
Joined: 09 Mar 2005
Posts: 199
|
Posted: Thu Oct 13, 2005 12:50 pm 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?
What if it should connect to another machine ? |
A common scenario is when the SIP signalling travels through
proxy servers, but you want audio to go directly between the peers.
Another scenario is when all call control processing is done one a
call server, and the media paths to be opened are between two media
gateways. |
|
| 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 |
|
 |
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 |
|
 |
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 |
|
 |
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 |
|
 |
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 |
|
 |
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 |
|
 |
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 |
|
 |
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 |
|
 |
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 |
|
 |
Google
|
|
| Back to top |
|
 |
|
|
The time now is Tue Dec 02, 2008 5:53 am | All times are GMT
|
|
Tax | Refinance | Gas Suppliers | Ringtones | Mortgage Calculator
|
|
Copyright © 2004-2005 DeniX Solutions SRL
|
|
|
|
Other DeniX Solutions sites:
Unix/Linux blog |
electronics forum |
medicine forum |
science forum |
|
|
Privacy Policy
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|