void VTcpServer::run() { while (runThread().active()) { VTcpSession* newTCPSession = accept(); if (newTCPSession == NULL) { SET_DEBUG_ERROR(VNetError, "error in accept", WSAGetLastError()); break; } if (!newTCPSession->open()) { error = newTCPSession->error; continue; } VTcpSessionThread* thread = new VTcpSessionThread(this, newTCPSession); //thread->moveToThread(runThread().m_qthread); // gilgil temp 2012.08.16 thread->name = className(); threadList.lock(); threadList.append(thread); threadList.unlock(); if (!thread->open()) { error = thread->error; break; } } }
VTcpSession* VTcpServer::accept() { socklen_t size; SOCKADDR_IN sockAddr; SOCKET newHandle; VTcpSession* newTCPSession; if (acceptSession->handle == INVALID_SOCKET) { SET_ERROR(VError, "invalid handle", VERR_INVALID_HANDLE); goto _error; } // ------------------------------------------------------------------------ // accept // ------------------------------------------------------------------------ size = sizeof(sockAddr); newHandle = ::accept(acceptSession->handle, (SOCKADDR*)&sockAddr, &size); if (newHandle == INVALID_SOCKET) { SET_DEBUG_ERROR(VNetError, "error in accept", WSAGetLastError()); goto _error; } newTCPSession = new VTcpSession; newTCPSession->owner = this; newTCPSession->handle = newHandle; newTCPSession->addr = sockAddr; return newTCPSession; _error: return NULL; }
int VSslSession::doRead(char* buf, int size) { VLock lock(stateReadCs); // gilgil temp 2014.03.14 int res = SSL_read(con, buf, size); // sleep(1); // gilgil temp 2014.02.28 if (res < 0) { SET_DEBUG_ERROR(VSslError, qformat("SSL_read return %d", res), SSL_get_error(con, res)); return VERR_FAIL; } if (res == 0) // SSL_ERROR_ZERO_RETURN? { SET_DEBUG_ERROR(VSslError, "SSL_read return zero", SSL_get_error(con, res)); return VERR_FAIL; } return res; }
int SnoopWinDivert::read(SnoopPacket* packet) { SnoopWinDivertLib& lib = SnoopWinDivertLib::instance(); if (!lib.ok) { SET_ERROR(WinDivertError, "can not load WinDivert", VERR_LOAD_FAIL); return VERR_FAIL; } packet->clear(); UINT readLen; BOOL res = lib.WinDivertRecv(handle, this->pktData + sizeof(ETH_HDR), MAXBUF - sizeof(ETH_HDR), &packet->divertAddr, &readLen); if (!res) { DWORD lastError = GetLastError(); SET_DEBUG_ERROR(WinDivertError, qformat("WinDivertRecv return FALSE last error=%d(0x%x)", lastError, lastError), lastError); return VERR_FAIL; } readLen += sizeof(ETH_HDR); // LOG_DEBUG("ifIdx=%u subIfIdx=%u Direction=%u readLen=%u", packet->divertAddr.IfIdx, packet->divertAddr.SubIfIdx, packet->divertAddr.Direction, readLen); // gilgil temp 2013.12.05 ETH_HDR* ethHdr = (ETH_HDR*)pktData; ethHdr->ether_dhost = Mac::cleanMac(); ethHdr->ether_shost = Mac::cleanMac(); ethHdr->ether_type = htons(ETHERTYPE_IP); this->pktHdr.caplen = readLen; this->pktHdr.len = readLen; packet->pktData = this->pktData; packet->pktHdr = &this->pktHdr; packet->ethHdr = ethHdr; if (autoCorrectChecksum) { if (SnoopEth::isIp(packet->ethHdr, &packet->ipHdr)) { if (SnoopIp::isTcp(packet->ipHdr, &packet->tcpHdr)) packet->tcpHdr->th_sum = htons(SnoopTcp::checksum(packet->ipHdr, packet->tcpHdr)); else if (SnoopIp::isUdp(packet->ipHdr, &packet->udpHdr)) packet->udpHdr->uh_sum = htons(SnoopUdp::checksum(packet->ipHdr, packet->udpHdr)); packet->ipHdr->ip_sum = htons(SnoopIp::checksum(packet->ipHdr)); } } return (int)readLen; }
int SnoopWinDivert::write(u_char* buf, int size, WINDIVERT_ADDRESS* divertAddr) { SnoopWinDivertLib& lib = SnoopWinDivertLib::instance(); if (!lib.ok) { SET_ERROR(WinDivertError, "can not load WinDivert", VERR_LOAD_FAIL); return false; } UINT writeLen; BOOL res = lib.WinDivertSend(handle, buf + sizeof(ETH_HDR), (UINT)size - sizeof(ETH_HDR), divertAddr, &writeLen); if (!res) { DWORD lastError = GetLastError(); SET_DEBUG_ERROR(WinDivertError, qformat("WinDivertSend return FALSE last error=%d(0x%x)", lastError, lastError), lastError); return VERR_FAIL; } return (int)writeLen; }