LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-06-2011, 04:51 AM   #1
cshong
LQ Newbie
 
Registered: Nov 2006
Posts: 24

Rep: Reputation: 0
Querying network interfaces


The following are the output of command "ifconfig -a":

Quote:
eth0 Link encap:Ethernet HWaddr c8:0a:a9:4a:d4:72
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Interrupt:46

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:44 errors:0 dropped:0 overruns:0 frame:0
TX packets:44 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:3184 (3.1 KB) TX bytes:3184 (3.1 KB)

wlan0 Link encap:Ethernet HWaddr f0:7b:cb:81:b6:a2
inet addr:192.168.1.110 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::f27b:cbff:fe81:b6a2/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:691 errors:0 dropped:0 overruns:0 frame:0
TX packets:782 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:458673 (458.6 KB) TX bytes:173774 (173.7 KB)
I want my application to display ALL interfaces (including the down interface) on my system, except the loopback interface "lo". The following are the source codes of my application:

Code:
#include <sys/ioctl.h>
#include <net/if.h>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/ethtool.h>
#include <linux/sockios.h>
#include <errno.h>
using namespace std;

int main()
{
    char buf[1024];
    ifconf ifc;
    ifreq *ifr;
    int sck;
    sck = socket(AF_INET, SOCK_DGRAM, 0);
    if (sck<0)
    {
        cout<<"Socket error."<<endl;
        return 1;
    }
    ifc.ifc_len = sizeof(buf);
    ifc.ifc_buf = buf;
    if (ioctl(sck, SIOCGIFCONF, &ifc) < 0)
    {
        cout<<"Ioctl error: SIOCGIFCONF"<<endl;
        return 1;
    }
    ifr = ifc.ifc_req;
    int nInterfaces = ifc.ifc_len / sizeof(ifreq);
    int i;
    for (i=0;i<nInterfaces;i++)
    {
        cout<<i+1<<"\t"<<ifr[i].ifr_name<<endl;
    }
    return 0;
}
The following are the output of my application:

Quote:
1 lo
2 wlan0
The interface "eth0", which is down, was not displayed, but loopback interface has been displayed. So, how can I make my application display all interfaces, including the interfaces which are down, but excluding the loopback interface?

Note: My application should not require any input from user.
 
Old 01-06-2011, 06:23 AM   #2
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
If you can print out the names of the interfaces, surely you can write an if-block
to test for the loopback itf name? Or check for the IP 127.0.0.1?
 
Old 01-06-2011, 07:58 AM   #3
cshong
LQ Newbie
 
Registered: Nov 2006
Posts: 24

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by timmeke View Post
If you can print out the names of the interfaces, surely you can write an if-block
to test for the loopback itf name? Or check for the IP 127.0.0.1?
Ok. Then, how about the interface which is down, how can I make my application display it without turn it up?
 
Old 01-06-2011, 05:19 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
How about looking at the source code for ifconfig, to see how it does it? Isn't that one of the defining beauties of Linux?

'I have a program that does what I want. How do I write a program that does the same thing?'. Never fails to amaze me how often people ask questions like that.

--- rod.
 
Old 01-06-2011, 10:44 PM   #5
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Red face

Quote:
Originally Posted by theNbomr View Post
'I have a program that does what I want. How do I write a program that does the same thing?'. Never fails to amaze me how often people ask questions like that.
Fine, fine. cshong, does this shell script do the trick for you? It does for me.
Code:
cat > 1.c <<EOD; gcc -Wall -Werror 1.c -o 1 2> /dev/null; if [ $? = 0 ]; then echo "#define HAVE_SOCKADDR_SA_LEN 1" >> config.h; fi
#include <net/if.h>
int main(void)
{
  int          alpha;
  struct ifreq beta;

  alpha=beta.ifr_addr.sa_len;

  return 0;

} /* main() */
EOD
rm -f 1; cat > 1.c <<EOD; gcc -Wall -Werror 1.c -o 1; ./1
#include <net/if.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "config.h"

int
max(int ar_alpha,
    int ar_beta
   )
{
  if(ar_alpha>ar_beta)
  {
    return ar_alpha;
  }
  else
  {
    return ar_beta;
  }

} /* max() */

int
main(void)
{
  int            lo_buffer_index;
  int            lo_buffer_length;
  int            lo_entry_length;
  int            lo_family;
  int            lo_previous_length;
  int            lo_socket;

  char          *lo_info_buffer;

  struct ifreq  *lo_info_pointer;

  struct ifconf  lo_ifconf_buffer;

  lo_socket=socket(AF_INET,SOCK_DGRAM,0);

  if(lo_socket<0) { fprintf(stderr,"socket() fail\n"); exit(1); }

  lo_previous_length=0;
  lo_buffer_length  =sizeof(struct ifreq);

  for(;;)
  {
    lo_info_buffer=malloc(lo_buffer_length);

    if(lo_info_buffer==NULL) { fprintf(stderr,"malloc() fail\n"); exit(1); }

    lo_ifconf_buffer.ifc_len=lo_buffer_length;
    lo_ifconf_buffer.ifc_buf=lo_info_buffer;

    if(ioctl(lo_socket,SIOCGIFCONF,&lo_ifconf_buffer)<0)
    {
      if((errno!=EINVAL) || (lo_previous_length!=0))
      {
        fprintf(stderr,"ioctl(SIOCGIFCONF) fail\n"); exit(1);
      }

      /* Otherwise, increase the buffer size and try again. */
    }
    else
    {
      if(lo_ifconf_buffer.ifc_len==lo_previous_length)
      {
        break;
      }

      lo_previous_length=lo_ifconf_buffer.ifc_len;
    }

    free(lo_info_buffer);

    lo_buffer_length*=2;

    if(lo_buffer_length==0) { fprintf(stderr,"mem problem\n"); exit(1); }
  }

  for(lo_buffer_index=0;
      lo_buffer_index<lo_ifconf_buffer.ifc_len;
      lo_buffer_index+=lo_entry_length
     )
  {
    lo_info_pointer=(struct ifreq *)(lo_info_buffer+lo_buffer_index);

#if defined(HAVE_SOCKADDR_SA_LEN)
    lo_entry_length
    =
    max(sizeof(struct sockaddr),
        lo_info_pointer->ifr_addr.sa_len
       );
#else
    switch(((struct sockaddr_in*)(&lo_info_pointer->ifr_addr))->sin_family)
    {
#if defined(AF_INET6)
      case AF_INET6:
      {
        lo_entry_length=sizeof(struct sockaddr_in6);

        break;
      }
#endif
      case AF_INET:
      default:
      {
        lo_entry_length=sizeof(struct sockaddr);

        break;
      }
    }
#endif

    lo_family=((struct sockaddr_in*)(&lo_info_pointer->ifr_addr))->sin_family;

    if(ioctl(lo_socket,SIOCGIFFLAGS,lo_info_pointer))
    {
      continue;
    }

    printf("%s ",lo_info_pointer->ifr_name);

    switch(lo_family)
    {
      case AF_INET : printf("IPv4"                 ); break;
      case AF_INET6: printf("IPv6"                 ); break;
      default      : printf("neither IPv4 nor IPv6"); break;
    }

    printf("\n");
  }

  close(lo_socket);

  return 0;

} /* main() */
EOD
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] /etc/network/interfaces configuration for virtual interfaces nonshatter Linux - Networking 4 10-25-2010 06:22 AM
ifup: couldn't read interfaces file "/etc/network/interfaces" debian lenny lorimer73 Linux - Networking 1 08-24-2010 03:47 PM
the network scripts to create network interfaces for 802.1q VLANs 10 and 11, Lahore Linux - Networking 0 03-18-2009 05:45 AM
For laptop - different networks in /etc/network/interfaces, or use network-manager? dmravaet Linux - Wireless Networking 4 03-17-2007 11:42 PM
Manual Network Setup Works, /etc/network/interfaces doesn't verdeboy2k Linux - Wireless Networking 0 05-31-2006 06:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration