Solved

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

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

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
Comment Utility
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
Comment Utility
#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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

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 demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

16 Experts available now in Live!

Get 1:1 Help Now