Advertisement
peterzig

[PWŚS] beta HOSTNAME to FILE

Mar 6th, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.24 KB | None | 0 0
  1. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  2. #define _CRT_SECURE_NO_WARNINGS
  3.  
  4. #include <WinSock2.h>
  5. #include <stdio.h>
  6.  
  7. #pragma comment(lib,"ws2_32.lib")
  8.  
  9. FILE *plik = fopen("IP_LIST.txt", "a");
  10.  
  11. int main(int argc, char **argv)
  12. {
  13.     int i = 0;
  14.     //-----------------------------------------
  15.     // Declare and initialize variables
  16.     WSADATA wsaData;
  17.     int iResult; // error code
  18.  
  19.     DWORD dwError;
  20.  
  21.     struct hostent *remoteHost;
  22.     char *host_name;
  23.     struct in_addr addr;
  24.  
  25.     // Initialize Winsock
  26.     iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
  27.     if (iResult != 0)
  28.     {
  29.         printf("WSAStartup failed: %d\n", iResult);
  30.         getchar();
  31.         return 1;
  32.     }
  33.  
  34.     char buff[64];
  35.     printf("Podaj adres strony: ");
  36.     scanf(" %s", &buff);
  37.    
  38.     host_name = buff;
  39.  
  40.  
  41.     printf("Calling gethostbyname with %s\n", host_name);
  42.     remoteHost = gethostbyname(host_name);
  43.    
  44.  
  45.     if (remoteHost == NULL)
  46.     {
  47.         dwError = WSAGetLastError();
  48.         if (dwError != 0) {
  49.             if (dwError == WSAHOST_NOT_FOUND) {
  50.                 printf("Host not found\n");
  51.                 return 1;
  52.             }
  53.             else if (dwError == WSANO_DATA) {
  54.                 printf("No data record found\n");
  55.                 return 1;
  56.             }
  57.             else {
  58.                 printf("Function failed with error: %ld\n", dwError);
  59.                 return 1;
  60.             }
  61.         }
  62.     }
  63.     else
  64.     {
  65.         printf("Function returned:\n");
  66.         printf("\tOfficial name: %s\n", remoteHost->h_name);
  67.         printf("\tAlternate names: %s\n", remoteHost->h_aliases);
  68.         printf("\tAddress type: ");
  69.         switch (remoteHost->h_addrtype) {
  70.         case AF_INET:
  71.             printf("AF_INET\n");
  72.            
  73.             while (remoteHost->h_addr_list[i]) {
  74.                 if (remoteHost->h_addrtype == AF_INET) {
  75.                     printf("\t\tIP Address #%d %s\n", i, inet_ntoa(*(in_addr*)remoteHost->h_addr_list[i++]));
  76.                     if (plik != NULL || !feof(plik)) {
  77.                         fprintf(plik, "IP Address #%d %s\n", i, inet_ntoa(*(in_addr*)remoteHost->h_addr_list[i]));
  78.  
  79.                     //fputs(inet_ntoa(*(in_addr*)remoteHost->h_addr_list[i]), plik); /* zapisz nasz łańcuch w pliku */
  80.                     }
  81.                 }
  82.                 fclose(plik);
  83.             }  
  84.            
  85.             break;
  86.         case AF_INET6:
  87.             printf("AF_INET\n");
  88.             break;
  89.         case AF_NETBIOS:
  90.             printf("AF_NETBIOS\n");
  91.             break;
  92.         default:
  93.             printf(" %d\n", remoteHost->h_addrtype);
  94.             break;
  95.         }
  96.         printf("\tAddress length: %d\n", remoteHost->h_length);
  97.         getchar();
  98.     }
  99.     getchar();
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement