niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » Unix internals
finding the IP address of the local machine.
Post new topic   Reply to topic Page 1 of 2 [21 Posts] View previous topic :: View next topic
Goto page:  1, 2 Next
Author Message
vineeth
*nix forums beginner


Joined: 11 Oct 2005
Posts: 2

PostPosted: Tue Oct 11, 2005 1:24 pm    Post subject: finding the IP address of the local machine. Reply with 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.
Back to top
maxim.yegorushkin@gmail.c
*nix forums Guru Wannabe


Joined: 27 Jul 2005
Posts: 182

PostPosted: Tue Oct 11, 2005 1:24 pm    Post subject: Re: finding the IP address of the local machine. Reply with quote

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

PostPosted: Tue Oct 11, 2005 1:24 pm    Post subject: Re: finding the IP address of the local machine. Reply with quote

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

PostPosted: Tue Oct 11, 2005 1:28 pm    Post subject: Re: finding the IP address of the local machine. Reply with quote

try 'ifconfig'
Back to top
Pascal Bourguignon
*nix forums addict


Joined: 08 Mar 2005
Posts: 62

PostPosted: Tue Oct 11, 2005 1:55 pm    Post subject: Re: finding the IP address of the local machine. Reply with quote

"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

PostPosted: Tue Oct 11, 2005 3:44 pm    Post subject: Re: finding the IP address of the local machine. Reply with quote

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

PostPosted: Tue Oct 11, 2005 4:23 pm    Post subject: Re: finding the IP address of the local machine. Reply with quote

"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

PostPosted: Tue Oct 11, 2005 9:47 pm    Post subject: Re: finding the IP address of the local machine. Reply with quote

"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

PostPosted: Wed Oct 12, 2005 4:00 am    Post subject: Re: finding the IP address of the local machine. Reply with quote

"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

PostPosted: Wed Oct 12, 2005 10:49 am    Post subject: Re: finding the IP address of the local machine. Reply with quote

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 Smile 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

PostPosted: Wed Oct 12, 2005 1:08 pm    Post subject: Re: finding the IP address of the local machine. Reply with 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...

Here is the correct Smile 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

PostPosted: Wed Oct 12, 2005 3:10 pm    Post subject: Re: finding the IP address of the local machine. Reply with quote

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

PostPosted: Wed Oct 12, 2005 6:54 pm    Post subject: Re: finding the IP address of the local machine. Reply with quote

<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

PostPosted: Wed Oct 12, 2005 9:44 pm    Post subject: Re: finding the IP address of the local machine. Reply with quote

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 Smile 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

PostPosted: Thu Oct 13, 2005 7:14 am    Post subject: Re: finding the IP address of the local machine. Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 2 [21 Posts] Goto page:  1, 2 Next
View previous topic :: View next topic
The time now is Thu Jan 08, 2009 9:30 am | All times are GMT
navigation Forum index » Programming » Unix internals
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts spoofed local email addresses watkykjy Postfix 0 Thu Nov 20, 2008 8:49 am
No new posts Postfix ldap and Rewriting sender email address endfx Postfix 2 Thu Apr 17, 2008 9:34 pm
No new posts recipient address restriction 3dd13 Postfix 0 Wed Mar 26, 2008 1:46 pm
No new posts Postfix sending problem for local domain remote email monkey_magix Postfix 0 Mon Sep 10, 2007 10:17 am
No new posts Bug#379087: ITP: libcomplearn -- data-compression based i... Rudi Cilibrasi devel 0 Fri Jul 21, 2006 7:40 am

eBay | Remortgages | Credit Cards | Loans | Credit Card Consolidation
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
[ Time: 0.7722s ][ Queries: 16 (0.5797s) ][ GZIP on - Debug on ]