// DB-client.c #include #include #include #include #include #include #include #include #define BUFLEN 1024 #define DESCRIPT 64 typedef struct { char name[128]; char val[300]; } entry; const char separator=(char)165; HANDLE hsema; /*handle to semaphore*/ FILE *stream; void getword(char *word, char *line, char stop); char x2c(char *what); void unescape_url(char *url); void plustospace(char *str); void WriteHTML(EXTENSION_CONTROL_BLOCK *pECB,LPCSTR lpszOutput){ DWORD dwLength=lstrlen(lpszOutput); pECB->WriteClient(pECB->ConnID,(PVOID)lpszOutput,&dwLength,0); } void writelog(char report[]){ static int log=1; struct tm *newtime; time_t aclock; time( &aclock ); /* Get time in seconds */ newtime = localtime( &aclock ); /* Convert time to struct - tm form */ fprintf(stream,"%d:%s,%s",log++,report,asctime(newtime)); fflush(stream); } /*writelog*/ BOOL WINAPI GetExtensionVersion(HSE_VERSION_INFO *pVer) { pVer->dwExtensionVersion=1; strncpy(pVer->lpszExtensionDesc,"Exclusive DllMain", HSE_MAX_EXT_DLL_NAME_LEN); return TRUE; } BOOL WINAPI DllMain(HMODULE hMod,DWORD fReason,LPVOID pvRes){ switch (fReason) { case DLL_PROCESS_ATTACH: stream=fopen("semalog.txt","w"); hsema=CreateSemaphore(NULL,5,5,NULL); fprintf(stream,"Handle: %x",hsema); fflush(stream); break; case DLL_THREAD_ATTACH: break; case DLL_THREAD_DETACH: break; case DLL_PROCESS_DETACH: fflush(stream); fclose(stream); break; } return TRUE; } DWORD WINAPI HttpExtensionProc(EXTENSION_CONTROL_BLOCK *pECB) { TCHAR szTempBuffer[BUFLEN],message[BUFLEN]; DWORD dwBufferSize=BUFLEN; DWORD dwHeaderSize; //Variablers for WinSock Calls WSADATA wsadata; WORD wVer; struct sockaddr_in addr_server; SOCKET sd_cl = INVALID_SOCKET; int iErrValue,iRc=-1; DWORD dwIP; entry entries[100]; register int x,m=0; char cl[1024]; char log_report[128]; unsigned short iDefPort = 12000; char szServerName[80] = "143.233.122.207"; // First send the content-type header strcpy(szTempBuffer,"Content-type: text/html\r\n\r\n"); dwHeaderSize=strlen(szTempBuffer); pECB->ServerSupportFunction(pECB->ConnID,HSE_REQ_SEND_RESPONSE_HEADER,NULL, &dwHeaderSize,(LPDWORD) szTempBuffer); // Now Get and print all the server variables if (pECB->GetServerVariable (pECB->ConnID,"CONTENT_LENGTH", szTempBuffer,&dwBufferSize)) { WriteHTML(pECB,"
CONTENT_LENGTH = "); WriteHTML(pECB,szTempBuffer); } // Now output the data in the ECB structure itsself. /*WriteHTML(pECB,"
lpszMethod = "); WriteHTML(pECB,pECB->lpszMethod); WriteHTML(pECB,"
cbTotalBytes = "); WriteHTML(pECB,pECB->cbTotalBytes); WriteHTML(pECB,"
cbAvailable = "); WriteHTML(pECB,pECB->cbAvailable);*/ WriteHTML(pECB,"
lpbData : "); WriteHTML(pECB,pECB->lpbData); /*if(strcmp(getenv("REQUEST_METHOD"),"POST")) { printf("This script should be referenced with a METHOD of GET.\n"); printf("If you don't understand this, see this "); printf("forms overview.%c",10); exit(1); }*/ //cl=(char *) malloc(sizeof(char) * (strlen(pECB->lpbData))); strcpy(cl,pECB->lpszQueryString); /*sprintf(szTempBuffer,"
cl=%s",cl); WriteHTML(pECB,szTempBuffer);*/ if(cl == NULL) { WriteHTML(pECB,"
info = "); strcpy(szTempBuffer,"No query information to decode."); WriteHTML(pECB,szTempBuffer); } for(x=0;cl[0] != '\0';x++) { m=x; getword(entries[x].val,cl,'&'); plustospace(entries[x].val); unescape_url(entries[x].val); getword(entries[x].name,entries[x].val,'='); } /*WriteHTML(pECB,"
Entry1="); strcpy(szTempBuffer,entries[0].val); WriteHTML(pECB,szTempBuffer); sprintf(szTempBuffer,"
Entry2=%s",entries[1].val); WriteHTML(pECB,szTempBuffer);*/ WriteHTML(pECB,"
Query Results
"); WriteHTML(pECB,"
You submitted the following name/value pairs:

"); WriteHTML(pECB,"

    "); for(x=0; x <= m; x++){ sprintf(szTempBuffer,"
  • %s = %s",entries[x].name, entries[x].val); WriteHTML(pECB,szTempBuffer); } WriteHTML(pECB,"
"); if (hsema == NULL){ WriteHTML(pECB,"
No semaphore
"); } else WriteHTML(pECB,"
semaphore OK
"); //Wait for Semaphore if(WaitForSingleObject(hsema,1000)==WAIT_TIMEOUT){ WriteHTML(pECB,"
Timeout in Semaphore
"); sprintf(log_report,"ID:%d,No Semaphore",pECB->ConnID); writelog(log_report); return HSE_STATUS_ERROR; } // Initiate use of WINSOCK.DLL wVer= MAKEWORD(1,1); iRc = WSAStartup(wVer,&wsadata); sprintf(szTempBuffer, "
WSAStartup = %d",iRc); WriteHTML(pECB,szTempBuffer); // create client socket sd_cl = socket(PF_INET, SOCK_STREAM, 0); if(sd_cl == INVALID_SOCKET) { iErrValue=WSAGetLastError(); sprintf(szTempBuffer, "
Socket failure= %d",iErrValue); WriteHTML(pECB,szTempBuffer); sprintf(log_report,"ID:%d,Socket:%d",pECB->ConnID,sd_cl); writelog(log_report); ReleaseSemaphore(hsema,1,NULL); return HSE_STATUS_ERROR; } sprintf(szTempBuffer, "
Socket = %d",sd_cl); WriteHTML(pECB,szTempBuffer); // set server address fields // Set address to connect to addr_server.sin_family=PF_INET; addr_server.sin_port=htons(iDefPort); dwIP = inet_addr(szServerName); if (dwIP != INADDR_NONE) { // Have IP address addr_server.sin_addr.s_addr=dwIP; } // request connection iRc=connect(sd_cl,(struct sockaddr far *) &addr_server,sizeof(addr_server)); if (iRc== SOCKET_ERROR) { iRc = WSAGetLastError(); sprintf(szTempBuffer, "
Connect failure= %d",iRc); WriteHTML(pECB,szTempBuffer); sprintf(log_report,"ID:%d,Connect:%d",pECB->ConnID,iRc); writelog(log_report); ReleaseSemaphore(hsema,1,NULL); return HSE_STATUS_ERROR; } sprintf(log_report,"ID:%d,Connect:%d",pECB->ConnID,iRc); writelog(log_report); sprintf(szTempBuffer, "
Connect = %d",iRc); WriteHTML(pECB,szTempBuffer); sprintf(message,"%s%c%s",entries[0].val,separator,entries[1].val); iRc=send(sd_cl,(char FAR *) message, sizeof(message),0); sprintf(szTempBuffer, "
Send = %d",iRc); WriteHTML(pECB,szTempBuffer); WriteHTML(pECB,"
Sent Message :"); WriteHTML(pECB,message); WriteHTML(pECB,"

"); while((iRc=recv(sd_cl,(char FAR *) message, sizeof(message),0))>0){ WriteHTML(pECB,message); } //while WriteHTML(pECB,"
That's All!"); closesocket(sd_cl); WSACleanup(); ReleaseSemaphore(hsema,1,NULL); return HSE_STATUS_SUCCESS; }