Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 822845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:51:23+00:00 2026-05-15T02:51:23+00:00

I would like to write a piece of code which checks, for each network

  • 0

I would like to write a piece of code which checks, for each network device (e.g. eth0, lo, master devices) some statistics and configuration data about that device.
I could find the statistics data (and most of the configuration data) in /sys/class/net/…, however, I couldn’t find any C/C++ API or any entry in procfs/sysfs listing the inet addr, netmask and gateway.

Some alternatives I checked:

  • parsing the output from ifconfig/route/some other utilities: I don’t want to start a subprocess every time I need to do the inspection.
  • parsing /etc/sysconfig/network-scripts/: will give me only the start-up configuration, and not the current state.

Also, since this code is intended for a product in my workplace, where every external library is inspected thoroughly (meaning it will take me forever to add any external library) I prefer solutions which rely on Linux native API and not external libraries.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T02:51:24+00:00Added an answer on May 15, 2026 at 2:51 am

    There sure is using a struct of ifreq and ioctl() calls you can grab all interface information:

    Man page is here Ifreq manpage

    /* local interface info */
        typedef struct{
            char *iface;
            struct ether_addr hwa;
            struct in_addr ipa;
            struct in_addr bcast;
            struct in_addr nmask;
            u_short mtu;
        } ifcfg_t; 
        /*
         * Grabs local network interface information and stores in a ifcfg_t 
         * defined in network.h, returns 0 on success -1 on failure
        */
        int get_local_info(int rsock, ifcfg_t *ifcfg)
        {
            struct ifreq ifr;
    
            memset(&ifr, 0, sizeof(ifr));
            strncpy(ifr.ifr_name, ifcfg->iface, IF_NAMESIZE);
            if((ioctl(rsock, SIOCGIFHWADDR, &ifr)) == -1){
                perror("ioctl():");
                return -1;
            }
            memcpy(&(ifcfg->hwa), &ifr.ifr_hwaddr.sa_data, 6);
    
            memset(&ifr, 0, sizeof(ifr));
            strncpy(ifr.ifr_name, ifcfg->iface, IF_NAMESIZE);
            if((ioctl(rsock, SIOCGIFADDR, &ifr)) == -1){
                perror("ioctl():");
                return -1;
            }
            memcpy(&ifcfg->ipa, &(*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr, 4);
    
            memset(&ifr, 0, sizeof(ifr));
            strncpy(ifr.ifr_name, ifcfg->iface, IF_NAMESIZE);
            if((ioctl(rsock, SIOCGIFBRDADDR, &ifr)) == -1){
                perror("ioctl():");
                return -1;
            }
            memcpy(&ifcfg->bcast, &(*(struct sockaddr_in *)&ifr.ifr_broadaddr).sin_addr, 4);
    
            memset(&ifr, 0, sizeof(ifr));
            strncpy(ifr.ifr_name, ifcfg->iface, IF_NAMESIZE);
            if((ioctl(rsock, SIOCGIFNETMASK, &ifr)) == -1){
                perror("ioctl():");
                return -1;
            }
            memcpy(&ifcfg->nmask.s_addr, &(*(struct sockaddr_in *)&ifr.ifr_netmask).sin_addr, 4);
    
            memset(&ifr, 0, sizeof(ifr));
            strncpy(ifr.ifr_name, ifcfg->iface, IF_NAMESIZE);
            if((ioctl(rsock, SIOCGIFMTU, &ifr)) == -1){
                perror("ioctl():");
                return -1;
            }
            ifcfg->mtu = ifr.ifr_mtu;
    
            return 0;
        }
    

    Quick edit, this function requires that the interface has been assigned before it is called, like so:

    strcpy(if_cfg->iface, iface)
    

    Ensuring you have allocated the memory first, then call like so

    if((get_local_info(sock, if_cfg)) != 0){
        printf("Unable to get network device info\n");
        return -1;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 400k
  • Answers 400k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The difficulties of reconstituting rows in an EAV model is… May 15, 2026 at 4:05 am
  • Editorial Team
    Editorial Team added an answer use -Xbootclasspath to override the boot classpath May 15, 2026 at 4:05 am
  • Editorial Team
    Editorial Team added an answer Your best (and safest) bet is to either fetch the… May 15, 2026 at 4:05 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.