Solved

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

Posted on 2002-07-09
5
1,629 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Google Mini is no longer serving everything 7 161
OwnCloud Development and/or customization.. 19 1,076
check failing on yum update 5 30
Identify Linux loader 67 180
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

823 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