#include #include "libsockets.h" #include "libkeef.h" #if defined (UNIX) #include // for signal() #include // for SIGBUS, etc. #include #include #else if defined(WIN32) // Windows #include // for closesocket() #include // for _getcwd() #include // for getpid() #define sleep(SECS) Sleep(SECS*1000) #define close(SOCKET) closesocket(SOCKET) #define read(SOCKET,BUF,LEN) recv(SOCKET,BUF,LEN,/*flags=*/0) #define write(SOCKET,BUF,LEN) send(SOCKET,BUF,LEN,/*flags=*/0) #endif #include // for time(), strftime(), etc. static char *g_pszIniFile = "monitor.ini"; static char *g_pszDebugLog = "monitor.dbg"; static char *g_pszErrorLog = "monitor.err"; static char *g_pszOutFile = "monitor.dat"; /*global*/int g_fDebug = 0; #define ErrorLog printf #define DebugLog if (g_fDebug) printf #define MAXLEN_HOSTNAME 200 #ifndef PATH_SLASH #ifdef UNIX #define PATH_SLASH '/' #else #define PATH_SLASH '\\' #endif #endif #ifndef TXTNL #define TXTNL "\r\n" #endif //----------------------------------------------------------------------------- static char *g_Synopsis[] = { //----------------------------------------------------------------------------- " ", "monitor, (c) Copyright 1999 Ridgeware, Inc.", " ", "usage: monitor [-v] [ []]", " ", "where:", " -v : verbosity for low level sockets debugging", " : defaults to '/'", " : defaults to 80", " ", "History:", " 12 Jul 99 : keef started", " 03 Aug 99 : keef resumed", " ", NULL }; //----------------------------------------------------------------------------- static void NormalExit () //----------------------------------------------------------------------------- { DebugLog ("monitor: exiting normally\n"); #ifdef WIN32 WSACleanup(); #endif exit (0); } // NormalExit //----------------------------------------------------------------------------- static void ErrorExit (int iErrorValue) //----------------------------------------------------------------------------- { ErrorLog (" |\n"); ErrorLog (" \\ | | | /\n"); ErrorLog (" -- CRASHED --\n"); ErrorLog (" / | | | \\\n"); ErrorLog (" |\n"); #ifdef WIN32 WSACleanup(); #endif exit (iErrorValue); } // ErrorExit //----------------------------------------------------------------------------- static void SynopsisAndExit () //----------------------------------------------------------------------------- { for (int ii=0; g_Synopsis[ii]; ++ii) { printf ("%s\n", g_Synopsis[ii]); } NormalExit(); } // SynopsisAndExit //----------------------------------------------------------------------------- static BOOL ValidateHost (struct sockaddr_in* pAddrServer, char* pszHostname, char* szCanonical) //----------------------------------------------------------------------------- { memset ((void *)pAddrServer, 0, sizeof(struct sockaddr_in)); pAddrServer->sin_family = AF_INET; //pAddrServer->sin_port = htons(iPortNum); // - - - - - - - - - - - - - - - - - - - - - - // look at first char to determine if hostname or IP address: // - - - - - - - - - - - - - - - - - - - - - - if ((pszHostname[0] >= '0') && (pszHostname[0] <= '9')) { u_long ulAddress = inet_addr (pszHostname); if ((int)ulAddress == -1) { ErrorLog ("invalid hostname IP address '%s'\n", pszHostname); return (FALSE); } #if defined(hp) || defined(aix) || defined(bsd) pAddrServer->sin_addr.s_addr = ulAddress; //see: /usr/include/netinet/in.h, line 99 #else pAddrServer->sin_addr.S_un.S_addr = ulAddress; //see: /usr/include/netinet/in.h, line 118 #endif // see if we can translate the IP address into canonical hostname... struct hostent* pHost = gethostbyaddr ((char*)&ulAddress, sizeof(ulAddress), AF_INET); if (pHost) { strncpy (szCanonical, pHost->h_name, MAXLEN_HOSTNAME); szCanonical[MAXLEN_HOSTNAME] = 0; } } // - - - - - - - - - - - - - - - - - - - - - - // resolve hostname into an IP address: // - - - - - - - - - - - - - - - - - - - - - - else { struct hostent* pHost = gethostbyname (pszHostname); if (!pHost) { ErrorLog ("could not resolve hostname '%s' into an IP address\n", pszHostname); return (FALSE); } memcpy (&(pAddrServer->sin_addr), pHost->h_addr_list[0], sizeof(pAddrServer->sin_addr)); u_long ulAddress; #if defined(hp) || defined(aix) || defined(bsd) ulAddress = pAddrServer->sin_addr.s_addr; #else ulAddress = pAddrServer->sin_addr.S_un.S_addr; #endif strncpy (szCanonical, pHost->h_name, MAXLEN_HOSTNAME); szCanonical[MAXLEN_HOSTNAME] = 0; } return (TRUE); } // ValidateHost //----------------------------------------------------------------------------- static int SendCommand (struct sockaddr_in* pAddrServer, char* pszHostname, char* pszCommand) //----------------------------------------------------------------------------- // Don't send the EOBATCH { int fdSocket; DebugLog ("SendCommand(): opening new socket to daemon...\n"); if ((fdSocket = socket (AF_INET, SOCK_STREAM, 0)) < 0) { ErrorLog ("Can't open stream socket\n"); return (-1); // error } DebugLog ("SendCommand(): new fdSocket = %d, now trying a connect...\n", fdSocket); #ifdef UNIX // sanity check: 0=stdin, 1=stdout, 2=stderr, we should always be getting --> 3 <-- if (fdSocket != 3) { DebugLog ("SendCommand(): UNEXPECTED SOCKET NUMBER --> %d <--\n", fdSocket); DebugLog ("SendCommand(): 0=stdin, 1=stdout, 2=stderr, shouldn't we get 3 ???\n"); } #endif if (connect (fdSocket, (struct sockaddr *) pAddrServer, sizeof(struct sockaddr_in)) < 0) { DebugLog ("SendCommand(): connect returned and ERROR (errno=%d)\n", errno); ErrorLog ("Hostname '%s' is not reachable (errno=%d)\n", pszHostname, errno); TranslateErrno (); } DebugLog ("SendCommand(): connect happened, let's keep going...\n"); DebugLog ("SendCommand(): calling SocketWrite() to fdSocket = %d\n", fdSocket); SocketWrite (fdSocket, pszCommand); DebugLog ("SendCommand(): sent '%s'\n", pszCommand); return (fdSocket); } // SendCommand //------------------------------------------------------------------------------ main (int argc, char* argv[]) //------------------------------------------------------------------------------ { char* pszHostname = NULL; char* pszURL = "/"; char* pszPortNum = "80"; char** pArgArray[] = {&pszHostname, &pszURL, &pszPortNum, NULL}; int iThisArg = 0; int iMaxArgs = 3; for (int ii=1; ii= iMaxArgs) { printf ("monitor: argument '%s' not understood.\n", argv[ii]); SynopsisAndExit(); } *(pArgArray[iThisArg]) = argv[ii]; ++iThisArg; } } // check required args: if (!pszHostname) SynopsisAndExit (); int iPortNum = atoi(pszPortNum); #ifdef WIN32 WORD wVersionRequested = MAKEWORD(1,1); WSADATA wsaData; if (WSAStartup (wVersionRequested, &wsaData)) { ErrorLog ("main(): could not find a compatible WINSOCK DLL\n"); ErrorExit (-1); } #endif struct sockaddr_in AddrServer; char szCanonical[MAXLEN_HOSTNAME+1]; if (!ValidateHost (&AddrServer, pszHostname, szCanonical)) ErrorExit (1); AddrServer.sin_port = htons(iPortNum); char szCommand[500+1]; sprintf (szCommand, "GET %s HTTP/1.0", pszURL); DebugLog ("+----------------------------------------------------------------------\n"); DebugLog ("| %s (%d.%d.%d.%d) : %d - %s\n", szCanonical, InAddrB1 (AddrServer.sin_addr.s_addr), InAddrB2 (AddrServer.sin_addr.s_addr), InAddrB3 (AddrServer.sin_addr.s_addr), InAddrB4 (AddrServer.sin_addr.s_addr), iPortNum, szCommand); DebugLog("+----------------------------------------------------------------------\n"); strcat (szCommand, "\n\n"); int fdSocket = SendCommand (&AddrServer, szCanonical, szCommand); if (fdSocket <= 0) ErrorExit (1); #define MAXLEN_BUFFER 2000 static char szLine[MAXLEN_BUFFER+1]; char szProtocol[20+1]; char szServer[100+1]; int iLineNum = 0; while (SocketReadLine (fdSocket, szLine, MAXLEN_BUFFER)) { // HTTP/1.1 200 OK // Server: Netscape-Enterprise/3.6 SP1 // Date: Mon, 12 Jul 1999 18:27:13 GMT // Last-modified: 01-01-1970 01:01:01 GMT // Set-cookie: LLBEAN=SELF:1:092834l...; path=/; domain=.llbean.com; expires=Fri, 02 Aug 2002 20:28:31 GMT // Content-type: text/html // Last-modified: Wed, 21 Apr 1999 16:37:37 GMT // Content-length: 890 // Accept-ranges: bytes // Connection: close // // // ... DebugLog ("| %s\n", szLine); if (++iLineNum == 1) { strncpy (szProtocol, szLine, 20); szLine[20] = 0; } else if (strmatchN (szLine, "Server: ")) { strncpy (szServer, szLine+strlen("Server: "), 100); } } DebugLog ("+----------------------------------------------------------------------\n"); DebugLog ("CloseSocket(): closing fdSocket = %d...\n", fdSocket); close (fdSocket); return (0); } // main - monitor