SHARE
TWEET

ifconfig-like output 2

a guest Jul 17th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <sys/ioctl.h>
  2. #include <net/if.h>
  3. #include <netinet/in.h>
  4. #include <stdio.h>
  5. #include <arpa/inet.h>
  6.  
  7. int main(void)
  8. {
  9.     char          buf[1024];
  10.     struct ifconf ifc;
  11.     struct ifreq *ifr;
  12.     int           sck;
  13.     int           nInterfaces;
  14.     int           i;
  15.  
  16. /* Get a socket handle. */
  17.     sck = socket(AF_INET, SOCK_DGRAM, 0);
  18.     if(sck < 0)
  19.     {
  20.         perror("socket");
  21.         return 1;
  22.     }
  23.  
  24. /* Query available interfaces. */
  25.     ifc.ifc_len = sizeof(buf);
  26.     ifc.ifc_buf = buf;
  27.     if(ioctl(sck, SIOCGIFCONF, &ifc) < 0)
  28.     {
  29.         perror("ioctl(SIOCGIFCONF)");
  30.         return 1;
  31.     }
  32.  
  33. /* Iterate through the list of interfaces. */
  34.     ifr         = ifc.ifc_req;
  35.     nInterfaces = ifc.ifc_len / sizeof(struct ifreq);
  36.     for(i = 0; i < nInterfaces; i++)
  37.     {
  38.         struct ifreq *item = &ifr[i];
  39.  
  40.     /* Show the device name and IP address */
  41.         printf("%s: IP %s",
  42.                item->ifr_name,
  43.                inet_ntoa(((struct sockaddr_in *)&item->ifr_addr)->sin_addr));
  44.  
  45.  
  46.     /* Get the broadcast address (added by Eric) */
  47.         if(ioctl(sck, SIOCGIFBRDADDR, item) >= 0)
  48.             printf(", BROADCAST %s", inet_ntoa(((struct sockaddr_in *)&item->ifr_broadaddr)->sin_addr));
  49.         printf("\n");
  50.     }
  51.  
  52.         return 0;
  53. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top