// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // File: UpdateIP.cpp // Created 10/04/2002 // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // Purpose: // This program is meant to demonstrate the protocol // used in connecting to a dynamic dns server and // performing a dynamic IP update. // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= #include "stdafx.h" #include "winsock.h" // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // You will need to modify these values to the host, zone // and password for the domain you wish to update. // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= #define DOMAIN_HOST "www" #define DOMAIN_ZONE "mydomain.com" #define DOMAIN_PASSWORD "password" // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // Our update server information // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= #define UPDATE_SERVER "dynamic.name-services.com." #define HTTP_PORT 80 #define RESPONSE_BUFFER 2048 // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // Function: senda() // Date: 10/04/2002 // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // Parameters: // SOCKET nSock - The valid socket which we // are to use to send the string. // const char *cText - A null terminated text // string to be sent to socket. // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // Purpose: // This function just wraps the standard send // using strlen to calculate the length, and // assumes no options. // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= int senda( SOCKET nSock, const char *cText ) { return send( nSock, cText, (int)strlen(cText), 0 ); } // =-=-=-=-=-=-=-=-=-=-=-= // End Function senda() // =-=-=-=-=-=-=-=-=-=-=-= // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // Function: TerminateConnection() // Date: 10/04/2002 // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // Parameters: // SOCKET nSock - The socket that we are to // make sure is closed. // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // Purpose: // This function is responsible for gracefully // closing a given socket if a connection is // established. // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= void TerminateConnection( SOCKET nSock ) { // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // If we have a socket that looks valid, we want to gracefully close it. // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= if( INVALID_SOCKET != nSock ) { // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // Make sure that we have signaled shutdown from our end of the socket. // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= shutdown( nSock, 1 ); // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // Here we read all remaining data that the server has to send. // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= char cFlush[1024]; while( 0 < recv( nSock, cFlush, 1024, 0 ) ); // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // Now we are free to close the socket. // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= if( 0 != closesocket( nSock ) ) printf( "Error - closesocket() failed: %d", WSAGetLastError() ); } } // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // End Function TerminateConnection() // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // Function: main() // Date: 10/04/2002 // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // Purpose: // This function performs a dynamic dns update // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // NOTE: // On error we always jump to the cleanup // routines at the end of the function. // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= int main(int argc, char **argv ) { SOCKET nSock = INVALID_SOCKET; // =-=-=-=-=-=-=-=-=-=-=-= // Initialize winsock. // =-=-=-=-=-=-=-=-=-=-=-= WSADATA wsaData; if( 0 != WSAStartup( MAKEWORD( 1, 1 ), &wsaData ) ) { printf( "Error - Failed to initialize winsock: %d\r\n", WSAGetLastError() ); goto End; } // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // Create the TCP socket that will be // used to connect to the interface. // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= nSock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ); if( INVALID_SOCKET == nSock ) { printf( "Error - Failed to create socket: %d.\r\n", WSAGetLastError() ); goto End; } // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // Look up the current IP for the update server. // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= struct hostent *pHostEnt = NULL; if( NULL == ( pHostEnt = gethostbyname( UPDATE_SERVER ) ) ) { printf( "Error - Failed to look up server %s: %d\r\n", UPDATE_SERVER, WSAGetLastError() ); goto End; } // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // Fill in the remote machines address information. // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= struct sockaddr_in SockAddr; memset( &SockAddr, 0x00, sizeof( struct sockaddr_in ) ); // Zero out the structure first SockAddr.sin_family = AF_INET; SockAddr.sin_port = htons( HTTP_PORT ); // Set the port memcpy( &SockAddr.sin_addr.S_un.S_addr, pHostEnt->h_addr_list[0], sizeof( unsigned long ) ); // Fill in the IP address that we looked up. // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // Attempt to make the TCP connection. // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= if( SOCKET_ERROR == connect( nSock, (struct sockaddr *)&SockAddr, sizeof( struct sockaddr_in ) ) ) { printf( "Error - Failed to connect to remote server: %d\r\n", WSAGetLastError() ); goto End; } // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // Note: All information is sent on the query string. // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= { // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // Start out our HTTP session by requesting a page. // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= senda( nSock, "GET /interface.asp" ); // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // Next we specify that we want to update a dns host. // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= senda( nSock, "?Command=SetDNSHost" ); // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // Next specify the domain & host that we want to update. // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= senda( nSock, "&HostName=" ); senda( nSock, DOMAIN_HOST ); senda( nSock, "&Zone=" ); senda( nSock, DOMAIN_ZONE ); // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // Optional: // The update takes an optional address parameter. If // supplied, the server updates dns to the given IP. When // omitted, the client's IP is used. // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // senda( nSock, "&Address=" ); // senda( nSock, "1.2.3.4" ); // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // Now we add the domain's update password. // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= senda( nSock, "&DomainPassword=" ); senda( nSock, DOMAIN_PASSWORD ); // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // Finally we finish off the HTTP request. // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= senda( nSock, " HTTP/1.0\r\n\r\n" ); } // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // Let the server know that we are done sending // by closing our end of the socket. // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= shutdown( nSock, 1 ); // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // Here we just read print out the server's response // until an error occurs or the server is done and // closes the socket. // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= { char cResponse[RESPONSE_BUFFER]; int nReadResult = SOCKET_ERROR; BOOL lDone = FALSE; // =-=-=-=-=-=-=-=-=-=-=-= // Loop until signaled. // =-=-=-=-=-=-=-=-=-=-=-= while( !lDone ) { // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // Attempt to read from the socket // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= nReadResult = recv( nSock, cResponse, RESPONSE_BUFFER, 0 ); // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // Handle the results of our read. // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= switch( nReadResult ) { case SOCKET_ERROR: // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // An unexpected error occurred. // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= printf( "Error: Error reading from socket: %d.\r\n", WSAGetLastError() ); case 0: // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // The server closed the connection. // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= lDone = TRUE; break; default: // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // All appearantly went well with our socket read. // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // NULL terminate the string we read & print it. // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= cResponse[nReadResult] = 0x00; printf( cResponse ); } } } End: // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= // Make sure that our socket has been properly closed. // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= TerminateConnection( nSock ); return 0; } // =-=-=-=-=-=-=-=-=-=-=-= // End Function: main() // =-=-=-=-=-=-=-=-=-=-=-=