Hi!
Try the attached code. This function returns the IP-Address of a given
Interface.
uname/gethostbyname is not accurate. IP-addresses are bound to network
interfaces,
not to computers. uname/gethostbyname just queries the DNS. That doesn't
help most
of the times. BTW.: The hostname returned by uname/gethostname might be
an unofficial
name not listed in the DNS! Then you'll get the /etc/host entry, wich
might also
be inaccurate.
Greetings,
Michael.
CODE:
/*****************************************************
* Get the ip address of a given Interface.... *
* Usage: *
* get_if_addr("interface name") returns a char* *
* to the corresponding ip-address. *
* Example: ip_addr=get_if_addr("ppp0");
*****************************************************/
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <linux/sockios.h>
#include <stdio.h>
char *get_if_addr(str)
char *str;
{
static char gif_res[20];
int s;
struct sockaddr_in *sinp;
struct ifreq ifr;
if ((s=socket(AF_INET,SOCK_DGRAM,0))<0)
{
return (NULL);
}
strcpy(ifr.ifr_name,str);
if (ioctl(s,SIOCGIFADDR,&ifr)<0)
{
close (s);
return (NULL);
}
sinp=(struct sockaddr_in *)&ifr.ifr_addr;
strcpy (gif_res, inet_ntoa(sinp->sin_addr));
close (s);
return (gif_res);
}
> -----Urspr�ngliche Nachricht-----
> Von: centipede [mailto:[EMAIL PROTECTED]]
> Gesendet: Mittwoch, 9. Januar 2002 20:53
> An: SECURITY-BASICS
> Betreff: process to find own ip
>
>
> Hello,
>
> According to Richard Stevens in Unix Network Programming (Vol
> 1) the way
> a process can find out its host own IP (or IPs) is to call uname() or
> gethostname()
> and feed the result to gethostbyname().
> The problem is that this way certainly can't deal with dynamic IPs on
> ppp connections, for example.
> How d'ya people handle this issue ?
>
> thx,
> centipede.
>
>