Solved

How can we get the ip address like ifconfig using C program?

Posted on 2002-07-09
5
1,626 Views
Last Modified: 2008-02-01
How can we get the ip address like ifconfig using C program?
The question is not the same as what I asked.
The Ip I got was not same as the ifconfig.
I beleive that using ifconfig can get the true ip address
and can ping the IP.
So gethostent(),gethostbyname(), maybe retrieve the ip
address from /etc/hosts,....
Can you help me to write a code like ifconfig
which is not use gethostent with reading /etc/hosts,
(Because /etc/hosts is wrong in my computer.)
 




Because I use rc.inet1 to set the IP of mycomputer,
These files /etc/hosts , /etc/hostname  was not corrected.
That is to say,for example ,
the ip /etc/hosts is 192.168.0.1,
but in the rc.inet1 192.168.0.3,
when using ifconfig , I find the eth0 is 192.168.0.3.
I used Slakware 7.0 distribution.
0
Comment
Question by:mathematics
5 Comments
 

Expert Comment

by:dhanaspace1
ID: 7146535

some bad way & quick way
system ("ifconfig > /tmp/temp_file")
open the file
then strstr (buffer,"inet addr)
read the next token.
0
 
LVL 5

Accepted Solution

by:
bryanh earned 50 total points
ID: 7151625
Create a socket in the Ineternet domain and issue a SIOCGIFADDR ioctl to it

  #include <if.h>
  #include <in.h>
  #include <sockios.h>

  int sockfd;
  struct ifreq ifreq;
  struct sockaddr_in sockaddr;
  struct in_addr ipaddr;
  int rc;

  strcpy(ifreq.ifr_ifrn.ifrn_name, "eth0");

  sockfd=socket(PF_INET, IPPROTO_IP)
  rc = ioctl(sockfd, SIOCGIFADDR, &ifreq)

  sockaddr = (struct sockaddr_in) ifreq.ifr_ifru.ifru_addr;

  ipaddr = sockaddr.sin_addr;
0
 

Assisted Solution

by:pianopan
pianopan earned 50 total points
ID: 7213250
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <stdio.h>

int
main(int argc,char *argv[])
{
  char * host,**names,**addrs;
  struct hostent *hostinfo;

  if(argc == 1)
    {
      char myname[256];
      gethostname(myname,255);
      host=myname;
    } else if(argc != 2)
        {
          printf("Usage:%s <hostname>\n",argv[0]);
          exit(0);
        }else
  host=argv[1];

  hostinfo=gethostbyname(host);
 
  if(!hostinfo)
    {
      printf("can't get info of host: %s\n",host);
      exit(1);
    }
  printf("-------%s:infomation-----\n",host);
  printf("Offical name:%s\n",hostinfo->h_name);
  printf("Alias:\n");
  names=hostinfo->h_aliases;
  while(*names){
    printf("%s\n",*names);
    names++;
    }
  if(hostinfo->h_addrtype != AF_INET)
    {
        printf("not a IP host\n");
        exit(1);
    }
  printf("Address:\n");
  addrs=hostinfo->h_addr_list;
  while(*addrs)
    {
        printf("%s\n",inet_ntoa(*(struct in_addr *)*addrs));
        addrs++;
    }
  printf("-------info end-----\n----http://www.calihotel.net----");
  exit(0);
}
0
 
LVL 20

Expert Comment

by:jmcg
ID: 9925554
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Split: bryanh {http:#7151625} & pianopan {http:#7213250}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
need help to replace faulty disk using lv mirroring 18 164
shell script help 1 141
Debian 3 - initrd rebuild 3 69
Help with Ubuntu 14.04 LTS 10 133
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now