CPP   61

netaddress

Guest on 20th June 2022 06:12:54 AM

  1. /* -jk  Exploring gethostbyname with the intent of wrappign it in an object */
  2.  
  3. /* Old School C headers for Socket stuff */
  4. #include <stdio.h>          /* stderr, stdout */
  5. #include <netdb.h>          /* hostent struct, gethostbyname() */
  6. #include <arpa/inet.h>      /* inet_ntoa() to format IP address */
  7. #include <netinet/in.h>     /* in_addr structure */
  8.  
  9. /* C++ headers */
  10. #include <cstdlib>
  11. #include <cstddef>
  12. #include <fstream>
  13. #include <iostream>
  14. #include <string>
  15.  
  16. using namespace std;
  17.  
  18.  
  19.  
  20. int main(int argc, char **argv) {
  21.    struct hostent *host;     /* host information */
  22.    struct in_addr h_addr;    /* internet address */
  23.    int i=0;
  24.  
  25.  
  26.             if (argc != 2) {
  27.               fprintf(stderr, "USAGE: %s <inet_address>\n", argv[0]);
  28.               exit(1);
  29.             }
  30.             if ((host = gethostbyname(argv[1])) == NULL) {
  31.               fprintf(stderr, "(mini) nslookup failed on '%s'\n", argv[1]);
  32.               exit(1);
  33.             }
  34.  
  35.             fprintf(stdout, " Hostname %s\n", host->h_name);
  36.                
  37.             for (i=0; host->h_aliases[i] != '\0'; i++ ) {
  38.             fprintf(stdout, "  Aliases %s\n", host->h_aliases[i]);
  39.             }
  40.  
  41.        //     for (i=0; host->h_addr_list[i] != '\0'; i++ ) {
  42.             h_addr.s_addr = *((unsigned long *) host->h_addr_list[i]);
  43.             fprintf(stdout, "Addresses %s\n", inet_ntoa(h_addr));
  44. //          }
  45.             exit(0);
  46. }

Raw Paste


Login or Register to edit or fork this paste. It's free.