Advertisement
danielhilst

get-ip-netmask-defaultgw.c

Feb 12th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.34 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <sys/socket.h>
  5. #include <sys/ioctl.h>
  6. #include <net/if.h>
  7. #include <arpa/inet.h>
  8. #include <string.h>
  9.  
  10. #define __sockaddr_in(x) ((struct sockaddr_in *)(x))
  11.  
  12. struct in_addr get_host_ip(const char *ifname)
  13. {
  14.     int rval;
  15.     int sock;
  16.     struct ifreq ifr;
  17.     struct in_addr error = {
  18.             .s_addr = 0,
  19.     };
  20.  
  21.     sock = socket(AF_INET, SOCK_DGRAM, 0);
  22.     if (sock == -1)
  23.             return error;
  24.  
  25.     memset(&ifr, '\0', sizeof(ifr));
  26.     strncpy(ifr.ifr_name, ifname, strlen(ifname));
  27.     rval = ioctl(sock, SIOCGIFADDR, &ifr);
  28.     if (rval)
  29.             return error;
  30.  
  31.     return __sockaddr_in(&ifr.ifr_addr)->sin_addr;
  32. }
  33.  
  34. struct in_addr get_host_netmask(const char *ifname)
  35. {
  36.     int rval;
  37.     int sock;
  38.     struct ifreq ifr;
  39.     struct in_addr error = {
  40.             .s_addr = 0,
  41.     };
  42.  
  43.  
  44.     sock = socket(AF_INET, SOCK_DGRAM, 0);
  45.     if (sock == -1)
  46.             return error;
  47.  
  48.     memset(&ifr, '\0', sizeof(ifr));
  49.     strncpy(ifr.ifr_name, ifname, strlen(ifname));
  50.     rval = ioctl(sock, SIOCGIFNETMASK, &ifr);
  51.     if (rval)
  52.             return error;
  53.  
  54.     return __sockaddr_in(&ifr.ifr_netmask)->sin_addr;
  55. }
  56.  
  57. struct in_addr get_host_default_gw(void)
  58. {
  59.         FILE *fp;
  60.         char line[BUFSIZ];
  61.         unsigned long gw;
  62.         unsigned long dest;
  63.         struct in_addr rval = {
  64.                 .s_addr = 0,
  65.         };
  66.  
  67.         fp = fopen("/proc/net/route", "r");
  68.         if (!fp)
  69.                 return rval;
  70.  
  71.         while (fgets(line, BUFSIZ, fp)) {
  72.                 char *ptr = strchr(line, '\t');
  73.  
  74.                 if (!ptr)
  75.                         return rval;
  76.  
  77.                 sscanf(ptr, "%x", &gw);
  78.                 if (gw == 0) {
  79.                         ptr = strchr(++ptr, '\t');
  80.                         if (!ptr)
  81.                                 return rval;
  82.                         sscanf(ptr, "%x", &dest);
  83.                         if (dest) {
  84.                                 rval.s_addr = dest;
  85.                                 return rval;
  86.                         }
  87.                 }
  88.         }
  89.  
  90.         return rval;
  91. }
  92.  
  93. int main(void)
  94. {
  95.         printf("NETMASK: %s\n", inet_ntoa(get_host_ip("eth0")));
  96.         printf("IP: %s\n",  inet_ntoa(get_host_netmask("eth0")));
  97.         printf("DEFAULT GW: %s\n", inet_ntoa(get_host_default_gw()));
  98.                          
  99.         return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement