Advertisement
Alan468

WinSock DNSy

Mar 7th, 2017
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  2.  
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <vector>
  6. #include <winsock2.h>
  7. using namespace std;
  8. #pragma comment (lib, "Ws2_32.lib")
  9.  
  10. int main() {
  11.     WSADATA wsaData;
  12.  
  13.     int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
  14.  
  15.     if (iResult != NO_ERROR)
  16.         printf("Error at WSAStartup().\n");
  17.     else
  18.         printf("WSAStartup() is OK.\n");
  19.  
  20.     hostent* remoteHost;
  21.     char* ip;
  22.     char* host_name;
  23.     unsigned int addr;
  24.  
  25.     printf("Input name/IP of host: ");
  26.     host_name = (char*)malloc(sizeof(char*) * 128);
  27.     fgets(host_name, 128, stdin);
  28.  
  29.     if (isalpha(host_name[0])) {
  30.         printf("\nUsing gethostbyname()...\n");
  31.  
  32.         host_name[strlen(host_name) - 1] = '\0';
  33.         remoteHost = gethostbyname(host_name);
  34.  
  35.         vector<string> tab;
  36.         int i = 0;
  37.         while (remoteHost->h_addr_list[i]) {               //podsumowujÄ…c:
  38.             if (remoteHost->h_addrtype == AF_INET) {             //char* w wektorze, push_back i fgets/gets
  39.                 tab.push_back(  string(inet_ntoa(*((in_addr*)remoteHost->h_addr_list[i])))  );
  40.                 i++;
  41.             }
  42.         }
  43.         //ip = inet_ntoa(*(struct in_addr *)*remoteHost->h_addr_list);
  44.  
  45.         //printf("IP address is: %s.\n", ip);
  46.         //printf("Address type: %i.\n\n", remoteHost->h_addrtype);
  47.  
  48.         for (int a = 0; a < tab.size(); a++) {
  49.             printf("IP address is: %s\n", tab[a].c_str());
  50.             //printf("Address type: %i.\n\n", remoteHost->h_addrtype[a]);
  51.         }
  52.     }
  53.     else {
  54.         printf("\nUsing gethostbyaddr()...\n");
  55.  
  56.         addr = inet_addr(host_name);
  57.         remoteHost = gethostbyaddr((char *)&addr, 4, AF_INET);
  58.  
  59.         printf("Hostname is: %s.\n", remoteHost->h_name);
  60.         printf("Address type: %i.\n\n", remoteHost->h_addrtype);
  61.     }
  62.  
  63.     if (WSAGetLastError() != 0) {
  64.         if (WSAGetLastError() == 11001)
  65.             printf("Host not found...\nExiting.\n");
  66.     }
  67.     else
  68.         printf("error #: %ld\n", WSAGetLastError());
  69.  
  70.     _getch();
  71.     WSACleanup();
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement