Commit b9099ec4 authored by jan.koester's avatar jan.koester
Browse files

test

parent 71ea7775
Loading
Loading
Loading
Loading
+26 −17
Changes for src/event/iocp.cpp: 26 added lines, 17 removed lines.
Original line number Diff line number Diff line
@@ -260,45 +260,54 @@ namespace netplus {

					switch (pClientContext->State) {
						case IO_READ_STATE: {
						// Append the received data to the buffer
							curCon->RecvData.append(curCon->csock->_wsaBuf.buf, dwBytesTransfered);
							eargs->event->RequestEvent(*curCon, tid, args);

							if (!curCon->SendData.empty()) {
							// Switch to write state and initiate an async send
								pClientContext->State = IO_WRITE_STATE;

								// Kick one send attempt (consumes plaintext OR flushes pending record)
								buffer buf(curCon->SendData.data(), curCon->SendData.size());
								pClientContext->last_plain_consumed = curCon->csock->sendData(buf, 0);
							curCon->csock->sendData(buf);
						}
						else {
							// Stay in read state and initiate another async read
							curCon->csock->recvData(pClientContext->RecvBuf);

								// IMPORTANT: do NOT erase here. Only erase in WRITE completion path.
								// Also do NOT call sendData twice.
							} else {
								curCon->csock->recvData(pClientContext->RecvBuf, 0);
							}
						} break;
						case IO_WRITE_STATE: {
							// 1) First, erase plaintext that was consumed when we issued the last send attempt
							const size_t consumed = pClientContext->last_plain_consumed;

						if (consumed == 0) {
							// would-block style case; just re-issue send with same plaintext again
							buffer buf(curCon->SendData.data(), curCon->SendData.size());
							pClientContext->last_plain_consumed = curCon->csock->sendData(buf, 0);
							break;
							if (consumed > 0) {
								if (consumed > curCon->SendData.size()) {
									NetException e;
									e[NetException::Error] << "IOCP: consumed > SendData.size() (logic error)";
									throw e;
								}

						// erase PLAINTEXT, not dwBytesTransfered
								curCon->SendData.erase(curCon->SendData.begin(),
													curCon->SendData.begin() + consumed);
							}

							// 2) Let app layer refill ONLY when buffer is empty (prevents duplication)
							if (curCon->SendData.empty()) {
								eargs->event->ResponseEvent(*curCon, tid, args);
							}

							// 3) If there is still data to send, issue one more send attempt
							if (!curCon->SendData.empty()) {
								buffer buf(curCon->SendData.data(), curCon->SendData.size());
								pClientContext->last_plain_consumed = curCon->csock->sendData(buf, 0);
						} else {

								// If last_plain_consumed == 0, that's fine: it means TLS is flushing its internal record.
								// We rely on the next IOCP send completion to come in, and we'll try again then.
								break;
							}

							// 4) No more to send -> go back to read
							pClientContext->State = IO_READ_STATE;
							pClientContext->last_plain_consumed = 0;
							curCon->csock->recvData(pClientContext->RecvBuf, 0);
						}
					} break;
					default: {
						NetException exp;
+0 −26
Changes for src/ssl.cpp: 0 added lines, 26 removed lines.
Original line number Diff line number Diff line
@@ -283,12 +283,6 @@ namespace netplus {

        readExactRaw(*s, hdr, 5);

        std::cerr << "TLS HDR hex: "
                << std::hex
                << int(hdr[0]) << " " << int(hdr[1]) << " " << int(hdr[2]) << " "
                << int(hdr[3]) << " " << int(hdr[4])
                << std::dec << "\n";

        if (hdr[1] != 0x03) {
            netplus::NetException e;
            e[netplus::NetException::Error] << "ssl::accept: bad TLS major version";
@@ -845,31 +839,11 @@ void netplus::ssl::accept(std::unique_ptr<socket> &csock) {
        return false;
    };

    auto dumpRecHeader = [&](const std::vector<uint8_t>& rec, const char* tag) {
        if (rec.size() < 5) return;
        uint8_t ct = rec[0];
        uint16_t ver = (rec[1] << 8) | rec[2];
        uint16_t len = (rec[3] << 8) | rec[4];
        std::cerr << tag
                << " CT=0x" << std::hex << (int)ct
                << " VER=0x" << ver
                << " LEN=" << std::dec << len;
        if (ct == 0x15 && rec.size() >= 7) { // Alert
            std::cerr << " ALERT(level=" << (int)rec[5] << ", desc=" << (int)rec[6] << ")";
        }
        if (ct == 0x16 && rec.size() >= 6) { // Handshake
            std::cerr << " HS(type=0x" << std::hex << (int)rec[5] << ")";
        }
        std::cerr << std::dec << "\n";
    };

    // ---- Handshake -----------------------------------------------------------
    try {
        // 1) Read ClientHello (Handshake record)
        std::vector<uint8_t> chRec = readTlsRecord(cssock);

        dumpRecHeader(chRec, "NEXT REC");

        if (chRec.size() < 5 + 4 + 2 + 32 || chRec[0] != 0x16 || chRec[5] != 0x01) {
            throwSSL(netplus::NetException::Error, "Expected ClientHello");
        }