Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*==================================================================================================
- --------------------------------------------------------------------------------------------------
- [getIP.c]
- Get the IP address of a target hostname (on POSIX systems).
- (c) Damion 'Phr0z3n.dev' Tapper, 2015.
- Email: [email protected]
- Website: L337Stuff.Blogspot.com
- NOTE:
- Another version of this code was uploaded to Cplusplus.com by the user mborja. He/she stated that
- it is bare bones code and has not been extensively tested. The user also stated that improvements
- are welcomed.
- Upon inspecting the code (without testing it yet), I discovered a devastating number of fundamental
- anomalies:
- 1. variables were incompletely declared (which would undoubtedly lead to numerous compile-time errors).
- 2. extrapolating from the included header files, the code was meant to be implemented on POSIX systems,
- but there was no comment stating that fact at all.
- 3. the code is written in C++:
- a. a very bad idea on POSIX systems, being that the majority of code on such systems are written/
- implemented in C also,
- b. the only part of the code that utilized any true C++ functionality was the part used merely
- for the (file) logging of the acquired information.
- With the preceding mentioned anomalies, I was forced to draw one conclusion:
- Either was the code not tested at all, or some serious amount of clipboard malfunctioning caused it
- to be inappropriately pasted for posting. For the second reason, he can be forgiven.
- Without further ado, I decided to put my problem-solving skills to work...
- --------------------------------------------------------------------------------------------------
- ==================================================================================================*/
- #include <stdio.h>
- #include <stdlib.h>
- #include <netdb.h>
- #include <arpa/inet.h>
- #include <time.h>
- /**
- * Comment the following line to prevent the logging of the acquired information to a log file.
- **/
- #define ADDR_LOG
- #ifdef ADDR_LOG
- #define LOG_FILE "ipaddr.log"
- #endif // ADDR_LOG
- int main(int argc, char*argv[])
- {
- struct hostent *record;
- struct in_addr *address;
- char *ip_address;
- if(argc != 2)
- {
- printf("usage: %s <hostname>\n", argv[0], argv[1]);
- exit(EXIT_FAILURE);
- }
- else
- record = gethostbyname(argv[1]);
- if(record == NULL)
- {
- printf("%s is unavailable\n", argv[1]);
- exit(EXIT_FAILURE);
- }
- else
- {
- address = (struct in_addr *)record->h_addr;
- ip_address = inet_ntoa(*address);
- }
- printf("HOST: %s ADDR: %s\n", argv[1], ip_address);
- #ifdef ADDR_LOG
- /**
- * Log this information to 'ipaddr.log'
- **/
- {
- /**
- * This method of declaring variables in the middle of the code in C, is very much conventional
- * and pro-ANSI, only because the enclosing block is provided. Be wary of the variables'
- * scope though.
- **/
- time_t rawTime;
- struct tm *pTm;
- FILE *pFile;
- /**
- * Get the current time:
- **/
- time(&rawTime);
- pTm = gmtime(&rawTime);
- /**
- * Open the file for writing:
- *
- * NOTE:
- * Somewhere along the line, a more secure version of this code should be implemented:
- * fopen_s, localtime_s, printf_s, fprintf_s, etc.
- **/
- pFile = fopen(LOG_FILE, "a");
- if(pFile == NULL)
- {
- printf("Error accessing '%s'.\nMake sure you have the relevant permissions for the path/file.", LOG_FILE);
- exit(EXIT_FAILURE);
- }
- else
- {
- /**
- * Write to the file:
- **/
- 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)
- {
- printf("Error writing to %s.\nMake sure you have write permissions for the path/file.", LOG_FILE);
- }
- else
- printf("Host/address successfully logged.");
- fclose(pFile);
- }
- }
- #endif // ADDR_LOG
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment