Advertisement
Guest User

Untitled

a guest
May 29th, 2016
565
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*  Head request example
  2. *  View with tabsize = 4
  3. *   Part of this is from the Winsock networking tutorial by Thomas Bleeker
  4. *   Visit www.MadWizard.org
  5. */
  6. #include <iostream>
  7. #pragma warning(disable:4996)
  8. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  9. #define WIN32_MEAN_AND_LEAN
  10.  
  11. #include <winsock2.h>
  12. #include <windows.h>
  13.  
  14. #include <time.h>
  15.  
  16.  
  17.  
  18. using namespace std;
  19.  
  20. class HRException
  21. {
  22. public:
  23.     HRException() :
  24.         m_pMessage("") {}
  25.     virtual ~HRException() {}
  26.     HRException(const char *pMessage) :
  27.         m_pMessage(pMessage) {}
  28.     const char * what() { return m_pMessage; }
  29. private:
  30.     const char *m_pMessage;
  31. };
  32.  
  33. const int  REQ_WINSOCK_VER = 2; // Minimum winsock version required
  34. const char DEF_SERVER_NAME[] = "0.pl.pool.ntp.org";
  35. const int  SERVER_PORT = 123;
  36. const int  TEMP_BUFFER_SIZE = 1024;
  37.  
  38. const char msg[48] = { 010,0,0,0,0,0,0,0,0 };
  39.  
  40.  
  41. // IP number typedef for IPv4
  42. typedef unsigned long IPNumber;
  43.  
  44. IPNumber FindHostIP(const char *pServerName)
  45. {
  46.     HOSTENT *pHostent;
  47.  
  48.     // Get hostent structure for hostname:
  49.     if (!(pHostent = gethostbyname(pServerName)))
  50.         throw HRException("could not resolve hostname.");
  51.    
  52.     // Extract primary IP address from hostent structure:
  53.     if (pHostent->h_addr_list && pHostent->h_addr_list[0])
  54.         return *reinterpret_cast<IPNumber*>(pHostent->h_addr_list[0]);
  55.  
  56.     return 0;
  57. }
  58.  
  59. void FillSockAddr(sockaddr_in *pSockAddr, const char *pServerName, int portNumber)
  60. {
  61.     // Set family, port and find IP
  62.     pSockAddr->sin_family = AF_INET;
  63.     pSockAddr->sin_port = htons(portNumber);
  64.     pSockAddr->sin_addr.S_un.S_addr = FindHostIP(pServerName);
  65. }
  66.  
  67. bool RequestHeaders(const char *pServername)
  68. {
  69.     SOCKET      hSocket = INVALID_SOCKET;
  70.     char        tempBuffer [TEMP_BUFFER_SIZE];
  71.     sockaddr_in sockAddr = { 0 };
  72.     bool        bSuccess = true;
  73.  
  74.     try
  75.     {
  76.         // Lookup hostname and fill sockaddr_in structure:
  77.         cout << "Looking up hostname " << pServername << "... ";
  78.         FillSockAddr(&sockAddr, pServername, SERVER_PORT);
  79.         cout << "found.\n";
  80.  
  81.         // Create socket
  82.         cout << "Creating socket... ";
  83.         if ((hSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET)
  84.             throw HRException("could not create socket.");
  85.         cout << "created.\n";
  86.  
  87.         // Connect to server
  88.         cout << "Attempting to connect to " << inet_ntoa(sockAddr.sin_addr)
  89.             << ":" << SERVER_PORT << "... ";
  90.         if (connect(hSocket, reinterpret_cast<sockaddr*>(&sockAddr), sizeof(sockAddr)) != 0)
  91.             throw HRException("could not connect.");
  92.         cout << "connected.\n";
  93.        
  94.         cout << "Sending request... ";
  95.  
  96.         // send request part 1
  97.    
  98.         if (send(hSocket, msg, sizeof(msg) , 0) == SOCKET_ERROR)
  99.             throw HRException("failed to send data.");
  100.  
  101.    
  102.  
  103.         cout << "request sent.\n";
  104.  
  105.         cout << "Dumping received data...\n\n";
  106.    
  107.         time_t tmit;
  108.         int bytes =  recv(hSocket, tempBuffer, sizeof(tempBuffer), 0);  // <-- the problem
  109.         cout << "bytes received: " << bytes << endl;
  110.         tmit = ntohl(((time_t*)tempBuffer)[4]);
  111.         tmit -= 2208988800U;
  112.    
  113.         time_t now = time(0);
  114.    
  115.         cout<<"NTP time: "  << ctime(&tmit);
  116.        
  117.         cout << "Pc  time: " << ctime(&now);
  118.        
  119.         time_t difference = tmit - now;
  120.         if (difference != 0)
  121.         {
  122.             cout << "Your system time is " << (difference < 0 ? -difference : difference)
  123.                 << (difference > 0 ? " seconds slower" : " seconds faster") << " than it should" << endl;
  124.         }
  125.         else
  126.             cout << "Your system time is fine!\n";
  127.  
  128.  
  129.        
  130.        
  131.            
  132.    
  133.     }
  134.     catch (HRException e)
  135.     {
  136.         cerr << "\nError: " << e.what() << endl;
  137.         bSuccess = false;
  138.     }
  139.  
  140.     if (hSocket != INVALID_SOCKET)
  141.     {
  142.         closesocket(hSocket);
  143.     }
  144.     return bSuccess;
  145. }
  146.  
  147. int main(int argc, char* argv[])
  148. {
  149.    
  150.     int iRet = 1;
  151.     WSADATA wsaData;
  152.  
  153.     cout << "Initializing winsock... ";
  154.  
  155.     if (WSAStartup(MAKEWORD(REQ_WINSOCK_VER, 0), &wsaData) == 0)
  156.     {
  157.         // Check if major version is at least REQ_WINSOCK_VER
  158.         if (LOBYTE(wsaData.wVersion) >= REQ_WINSOCK_VER)
  159.         {
  160.             cout << "initialized.\n";
  161.  
  162.             // Set default hostname:
  163.             const char *pHostname = DEF_SERVER_NAME;
  164.  
  165.             // Set custom hostname if given on the commandline:
  166.             if (argc > 1)
  167.                 pHostname = argv[1];
  168.  
  169.             iRet = !RequestHeaders(pHostname);
  170.         }
  171.         else
  172.         {
  173.             cerr << "required version not supported!";
  174.         }
  175.  
  176.         cout << "Cleaning up winsock... ";
  177.  
  178.         // Cleanup winsock
  179.         if (WSACleanup() != 0)
  180.         {
  181.             cerr << "cleanup failed!\n";
  182.             iRet = 1;
  183.         }
  184.         cout << "done.\n";
  185.     }
  186.     else
  187.     {
  188.         cerr << "startup failed!\n";
  189.     }
  190.     int x;
  191.     cin >> x;
  192.    
  193.     return iRet;
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement