Phr0zen_Penguin

getIP.c (POSIX)

Feb 14th, 2015
732
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.95 KB | None | 0 0
  1. /*==================================================================================================
  2.   --------------------------------------------------------------------------------------------------
  3.   [getIP.c]
  4.   Get the IP address of a target hostname (on POSIX systems).
  5.  
  6.         (c) Damion 'Phr0z3n.dev' Tapper, 2015.
  7.         Email: [email protected]
  8.         Website: L337Stuff.Blogspot.com
  9.  
  10.   NOTE:
  11.   Another version of this code was uploaded to Cplusplus.com by the user mborja.  He/she stated that
  12.   it is bare bones code and has not been extensively tested.  The user also stated that improvements
  13.   are welcomed.
  14.  
  15.   Upon inspecting the code (without testing it yet), I discovered a devastating number of fundamental
  16.   anomalies:
  17.   1. variables were incompletely declared (which would undoubtedly lead to numerous compile-time errors).
  18.   2. extrapolating from the included header files, the code was meant to be implemented on POSIX systems,
  19.      but there was no comment stating that fact at all.
  20.   3. the code is written in C++:
  21.      a. a very bad idea on POSIX systems, being that the majority of code on such systems are written/
  22.         implemented in C also,
  23.      b. the only part of the code that utilized any true C++ functionality was the part used merely
  24.         for the (file) logging of the acquired information.
  25.  
  26.   With the preceding mentioned anomalies, I was forced to draw one conclusion:
  27.   Either was the code not tested at all, or some serious amount of clipboard malfunctioning caused it
  28.   to be inappropriately pasted for posting.  For the second reason, he can be forgiven.
  29.  
  30.   Without further ado, I decided to put my problem-solving skills to work...
  31.   --------------------------------------------------------------------------------------------------
  32.   ==================================================================================================*/
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <netdb.h>
  36. #include <arpa/inet.h>
  37. #include <time.h>
  38.  
  39. /**
  40.  * Comment the following line to prevent the logging of the acquired information to a log file.
  41.  **/
  42. #define ADDR_LOG
  43.  
  44. #ifdef ADDR_LOG
  45.     #define LOG_FILE    "ipaddr.log"
  46. #endif // ADDR_LOG
  47.  
  48. int main(int argc, char*argv[])
  49. {
  50.     struct  hostent *record;
  51.     struct  in_addr *address;
  52.             char    *ip_address;
  53.  
  54.         if(argc != 2)
  55.         {
  56.             printf("usage: %s <hostname>\n", argv[0], argv[1]);
  57.             exit(EXIT_FAILURE);
  58.         }
  59.         else
  60.             record = gethostbyname(argv[1]);
  61.  
  62.         if(record == NULL)
  63.         {
  64.             printf("%s is unavailable\n", argv[1]);
  65.             exit(EXIT_FAILURE);
  66.         }
  67.         else
  68.         {
  69.             address = (struct in_addr *)record->h_addr;
  70.             ip_address = inet_ntoa(*address);
  71.         }
  72.  
  73.     printf("HOST: %s ADDR: %s\n", argv[1], ip_address);
  74.  
  75. #ifdef ADDR_LOG
  76.     /**
  77.      * Log this information to 'ipaddr.log'
  78.      **/
  79.     {
  80.         /**
  81.          * This method of declaring variables in the middle of the code in C, is very much conventional
  82.          * and pro-ANSI, only because the enclosing block is provided.  Be wary of the variables'
  83.          * scope though.
  84.          **/
  85.                 time_t  rawTime;
  86.         struct  tm      *pTm;
  87.                 FILE    *pFile;
  88.  
  89.         /**
  90.          * Get the current time:
  91.          **/
  92.         time(&rawTime);
  93.  
  94.         pTm = gmtime(&rawTime);
  95.  
  96.         /**
  97.          * Open the file for writing:
  98.          *
  99.          * NOTE:
  100.          * Somewhere along the line, a more secure version of this code should be implemented:
  101.          * fopen_s, localtime_s, printf_s, fprintf_s, etc.
  102.          **/
  103.         pFile = fopen(LOG_FILE, "a");
  104.  
  105.             if(pFile == NULL)
  106.             {
  107.                 printf("Error accessing '%s'.\nMake sure you have the relevant permissions for the path/file.", LOG_FILE);
  108.                 exit(EXIT_FAILURE);
  109.             }
  110.             else
  111.             {
  112.                     /**
  113.                      * Write to the file:
  114.                      **/
  115.                     if(fprintf(pFile, "%d:%d:%d HOST: %s ADDR: %s\n", (pTm->tm_hour), pTm->tm_min, pTm->tm_sec, argv[1], ip_address) < 0)
  116.                     {
  117.                         printf("Error writing to %s.\nMake sure you have write permissions for the path/file.", LOG_FILE);
  118.                     }
  119.                     else
  120.                         printf("Host/address successfully logged.");
  121.  
  122.                 fclose(pFile);
  123.             }
  124.     }
  125. #endif // ADDR_LOG
  126.  
  127.     return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment