Esempio n. 1
0
static int 
_conn_bstring_readhandler(struct conn_t *conn, void *ctx) {
  bstring data = (bstring)ctx;
  int ret;
  ballocmin(data, data->slen + 128);

  ret = safe_read(conn->sock, 
		  data->data + data->slen,
		  data->mlen - data->slen);

  if (ret > 0) {
#if(_debug_)
    log_dbg("bstring_read: %d bytes", ret);
#endif
    data->slen += ret;
  } else {
#if(_debug_)
    log_dbg("socket closed!");
    log_dbg("<== [%s]", data->data);
#endif
    conn_finish(conn);
  }

  return ret;
}
Esempio n. 2
0
int conn_update_write(struct conn_t *conn) {
#if(_debug_)
  log_dbg("socket writeable!");
#endif
  
  if (conn->write_pos == 0) {
    int err;
    socklen_t errlen = sizeof(err);
    if (getsockopt(conn->sock, SOL_SOCKET, SO_ERROR, 
		   &err, &errlen) || (err != 0)) {
      log_err(errno, "not connected");
      conn_finish(conn);
      return -1;
    } else {
#if(_debug_)
      /*log_dbg("RESETTING non-blocking");*/
#endif
      /*if (ndelay_off(conn->sock) < 0) {
	log_err(errno, "could not un-set non-blocking");
	}*/
    }
  }
  
  if (conn->write_pos < conn->write_buf->slen) {
    int ret = net_write(conn->sock, 
			conn->write_buf->data + conn->write_pos,
			conn->write_buf->slen - conn->write_pos);
    if (ret > 0) {
      /*log_dbg("write: %d bytes", ret);*/
      conn->write_pos += ret;
    } else if (ret < 0 || errno != EWOULDBLOCK) {
#if(_debug_)
      log_dbg("socket closed!");
#endif
      conn_finish(conn);
      return -1;
    }
  } 
  
  return 0;
}
Esempio n. 3
0
int conn_select_update(struct conn_t *conn, select_ctx *sctx) {
  if (conn->sock) {
    switch (net_select_read_fd(sctx, conn->sock)) {
    case -1:
      log_dbg("exception");
      conn_finish(conn);
      return -1;
      
    case 1:
      if (conn->read_handler)
	conn->read_handler(conn, conn->read_handler_ctx);
      break;
    }
    
    if (net_select_write_fd(sctx, conn->sock)==1)
      conn_update_write(conn);
  }
  
  return 0;
}
Esempio n. 4
0
int conn_update(struct conn_t *conn, fd_set *r, fd_set *w, fd_set *e) {

  if (conn->sock) {
    if (FD_ISSET(conn->sock, r)) {
      if (conn->read_handler) {
	conn->read_handler(conn, conn->read_handler_ctx);
      }
    }

    if (FD_ISSET(conn->sock, w)) {
      conn_update_write(conn);
    }

    if (FD_ISSET(conn->sock, e)) {
#if(_debug_)
      log_dbg("socket exception!");
#endif
      conn_finish(conn);
    }
  }

  return 0;
}
Esempio n. 5
0
int _tmain(int argc, _TCHAR* argv[])
{
	WSADATA wsaData;
	SOCKET hServSock, hClntSock;
	SOCKADDR_IN serv_addr, clnt_addr;
	FILE *fp, *f_size;
	char message[BUF_SIZE];
	int clntAddrSize, len;
	int nx = 0, ny = 0;
	int ret;
	char data[BUF_SIZE];
	HWND hWnd_desktop = GetDesktopWindow();
	HWND hWnd_console = GetConsoleWindow();
	CImage capImage;
	HDC hdc = NULL;
	nx = GetSystemMetrics(SM_CXSCREEN);
	ny = GetSystemMetrics(SM_CYSCREEN);
	if(!hWnd_desktop) return 0;
	if(!hWnd_console) return 0;
	if(!capImage.Create(nx, ny, 32)) return 0;
	DWORD dwError;
	CTime t = CTime::GetCurrentTime();
	char szPath[] = "C:\\SCC";
	char szPath2[] = "C:\\SCC\\ProcessLog";
	//ShowWindow(hWnd_console, SW_HIDE);
	char currentDirectory[BUF_SIZE];
	GetCurrentDirectoryA(BUF_SIZE, currentDirectory);
	char serviceAdd[BUF_SIZE] = "\"";

	dll_load(&wsaData);
	memset(&serv_addr, 0, sizeof(serv_addr));
	conn_init(&hServSock, &serv_addr, 1234);

	if(bind(hServSock, (SOCKADDR*)&serv_addr, sizeof(serv_addr)) == SOCKET_ERROR)
		ErrorHandling("bind() Error");

	if(listen(hServSock, 5) == SOCKET_ERROR)
		ErrorHandling("listen() Error");

	clntAddrSize = sizeof(clnt_addr);

	while(1)
	{
		accept_client(&hClntSock, &hServSock, &clnt_addr, &clntAddrSize);
		
		memset(data, 0, sizeof data);
		ret = recv(hClntSock, data, 1024, 0);

		if(ret = 0 || WSAGetLastError() ==WSAETIMEDOUT) continue;
		
		if(ret == SOCKET_ERROR)
		{
			printf("WSAGETLASTERROR\n", WSAGetLastError());
			WSACleanup(); return 0;
		}
//hello	
		if(strcmp(data, "hello") == 0)
		{
			//printf("hello\n");
			if(shutdown(hClntSock, SD_SEND) == SOCKET_ERROR)
				ErrorHandling("shutdown() Error");
		}
//Shutdown
		if(strcmp(data, "shutdown") == 0)
		{
			//printf("shutdown\n");
			system("shutdown -s -t 1");
		}
//ShutdownTest
		if(strcmp(data, "shutdownTest") == 0)
		{
			//printf("shutdown\n");
			system("shutdown -s -t 100");
		}
//Process_Terminate
		if(data[16] == '|')
		{
			char tmp[100] = {0x0,};
			int cnt = 0;

			for(int i = 17; i < strlen(data); i++)
			{
				tmp[cnt] = data[i];
				cnt++;
			}

			int pid = atoi(tmp);
			char command[1024];

			sprintf(command, "taskkill /pid %d", pid);
			system(command);
		}


//SendMessage
		if(data[11] == '|')
		{
			char tmp[100] = {0x0, };
			int cnt = 0;

			for(int i = 12; i < strlen(data); i++)
			{
				tmp[cnt] = data[i];
				cnt++;
			}

			MessageBoxA(0, tmp, "Sent from Iphone", MB_OK);
		}
//Process_Get
		if(strcmp(data, "getProcess") == 0)
		{
			if (!CreateDirectoryA(szPath2, NULL))
			{
				dwError = GetLastError();
				switch (dwError)
				{
				case ERROR_ALREADY_EXISTS: break;
				default: CreateDirectoryA(szPath, NULL); break;
				}
			}
			EnumWindows(EnumWindowsProc, NULL);
			memset(message, 0, 1024);

			CTime t = CTime::GetCurrentTime();
			char processLog_file[BUF_SIZE];
			char *processLog_file_temp = "abcd";
			char test[BUF_SIZE] = "C:\\SCC\\ProcessLog\\test.txt";
			sprintf(processLog_file, "C:\\SCC\\ProcessLog\\%d%02d%02d%02d%02d%02d.txt", t.GetYear(), t.GetMonth(), t.GetDay(), t.GetHour(), t.GetMinute(), t.GetSecond());
			processLog_file_temp = processLog_file;

			//printf("processLog_file = %s \n", processLog_file);
			//printf("processLog_file_temp = %s \n", processLog_file_temp);

			f_size = fopen(processLog_file_temp, "r");
			if(f_size)
			{
				//printf("f_size\n");
				int size;
				fseek(f_size, 0, SEEK_END);
				size = ftell(f_size);
				char file_size[BUF_SIZE];
				itoa(size, file_size, 10);
				send(hClntSock, file_size, BUF_SIZE, 0);

				fp = fopen(processLog_file_temp, "rb");
				if(fp)
				{
					//printf("Sending\n");
					while(1)
					{
						len = fread(message, sizeof(char), BUF_SIZE, fp);
						send(hClntSock, message, len, 0);

						if(feof(fp)) 
						{
							Sleep(1500);
							send(hClntSock, "0", 2, 0);
							//printf("End Of File\n");
							break;
						}
					}
					//printf("Done ...\n");
					fclose(fp);
				}
				else{printf("No\n");}
			}
			else{printf("N0\n");}

			if(shutdown(hClntSock, SD_SEND) == SOCKET_ERROR)
				ErrorHandling("shutdown() Error");
		}
//ScreenShot
		if(strcmp(data, "getScreenshot") == 0)
		{
			//printf("screenshot\n");
			if (!CreateDirectoryA(szPath, NULL))
			{
				dwError = GetLastError();
				switch (dwError)
				{
				case ERROR_ALREADY_EXISTS: break;
				default: CreateDirectoryA(szPath, NULL); break;
				}
			}
			memset(message, 0, 1024);
			hdc = capImage.GetDC();
			BitBlt(hdc, 0, 0, nx, ny, GetWindowDC(hWnd_desktop), 0, 0, SRCCOPY);
			capImage.Save(_T("C:\\SCC\\temp.jpg"), Gdiplus::ImageFormatJPEG);
			capImage.ReleaseDC();

			f_size = fopen("C:\\SCC\\temp.jpg", "r");
			int size;
			fseek(f_size, 0, SEEK_END);
			size = ftell(f_size);
			char file_size[BUF_SIZE];
			itoa(size, file_size, 10);
			//char *size = file_size;
			send(hClntSock, file_size, BUF_SIZE, 0);

			fp = fopen("C:\\SCC\\temp.jpg", "rb");
			if(fp == NULL)
			{
				ErrorHandling("File Open Error!");
				continue;
			}
			else
			{
				//printf("Sending ...\n");
				while(1)
				{
					len = fread(message, sizeof(char), BUF_SIZE, fp);
					send(hClntSock, message, len, 0);

					if(feof(fp)) 
					{
						Sleep(1500);
						send(hClntSock, "0", 2, 0);
						//printf("End Of File\n");
						break;
					}
				}
				//printf("Done ...\n");
			}
			fclose(fp);
			if(shutdown(hClntSock, SD_SEND) == SOCKET_ERROR)
				ErrorHandling("shutdown() Error");
		}
		puts(data);
	}

	if(shutdown(hClntSock, SD_SEND) == SOCKET_ERROR)
		ErrorHandling("shutdown() Error");

	len = (hClntSock, message, BUF_SIZE-1, 0);
	message[len] = 0;
	fputs(message, stdout);

	conn_finish(&hClntSock);
	
    return 0;
}