From 8ebc3d1d73c4c8c43457543859497590aa49e1d0 Mon Sep 17 00:00:00 2001 From: Treeki Date: Sun, 19 Jan 2014 00:20:05 +0100 Subject: fix lots more bugs in both the bouncer and client --- buffer.h | 25 +++++++++++++++++++++++++ build.sh | 2 +- core.cpp | 43 +++++++++++++++++++++++++++++++------------ python_client.py | 7 ++++--- 4 files changed, 61 insertions(+), 16 deletions(-) diff --git a/buffer.h b/buffer.h index 1218809..bfba94a 100644 --- a/buffer.h +++ b/buffer.h @@ -167,6 +167,31 @@ public: // In case the buffer was too small, skip over the extra source data m_readPointer += (size - readAmount); } + + + void dump() { + for (int base = 0; base < m_size; base += 0x10) { + printf("%08x | ", base); + + int pos; + for (pos = base; (pos < m_size) && (pos < (base + 0x10)); pos++) + printf("%02x ", (uint8_t)m_data[pos]); + + if (pos < (base + 0x10)) + for (; pos < (base + 0x10); pos++) + printf(" "); + + printf("| "); + + for (pos = base; (pos < m_size) && (pos < (base + 0x10)); pos++) + if (m_data[pos] >= 32) + printf("%c", m_data[pos]); + else + printf("."); + + printf("\n"); + } + } }; #endif /* BUFFER_H */ diff --git a/build.sh b/build.sh index 8070ce6..b138188 100755 --- a/build.sh +++ b/build.sh @@ -1,4 +1,4 @@ #!/bin/sh mkdir -p binary -g++ -o binary/nb4 -std=c++11 core.cpp dns.cpp -lgnutls -pthread +g++ -o binary/nb4 -std=c++11 core.cpp dns.cpp -lgnutls -pthread -g diff --git a/core.cpp b/core.cpp index b4b7c82..71857d3 100644 --- a/core.cpp +++ b/core.cpp @@ -120,7 +120,7 @@ void SocketRWCommon::readAction() { // (Up this, maybe?) int bufSize = inputBuf.size(); int requiredSize = bufSize + 0x200; - if (requiredSize < inputBuf.capacity()) + if (requiredSize > inputBuf.capacity()) inputBuf.setCapacity(requiredSize); ssize_t amount; @@ -129,6 +129,7 @@ void SocketRWCommon::readAction() { &inputBuf.data()[bufSize], 0x200); } else { + amount = recv(sock, &inputBuf.data()[bufSize], 0x200, @@ -148,8 +149,15 @@ void SocketRWCommon::readAction() { close(); } else if (amount < 0) { - perror("Error while reading!"); - close(); + if (tlsActive) { + if (gnutls_error_is_fatal(amount)) { + printf("Error while reading [gnutls %d]!\n", amount); + close(); + } + } else { + perror("Error while reading!"); + close(); + } } } @@ -173,8 +181,15 @@ void SocketRWCommon::writeAction() { } else if (amount == 0) printf("Sent 0!\n"); else if (amount < 0) { - perror("Error while sending!"); - close(); + if (tlsActive) { + if (gnutls_error_is_fatal(amount)) { + printf("Error while sending [gnutls %d]!\n", amount); + close(); + } + } else { + perror("Error while sending!"); + close(); + } } } @@ -373,7 +388,8 @@ void Client::handlePacket(Packet::Type type, char *data, int size) { printf("[fd=%d] Client authenticating\n", sock); if (!isNullSessionKey(reqKey)) { - printf("[fd=%d] Trying to resume session...\n", sock); + printf("[fd=%d] Trying to resume session...", sock); + printf("(last they received = %d, last we sent = %d)\n", lastReceivedByClient, nextPacketID - 1); Client *other = findClientWithKey(reqKey); printf("[fd=%d] Got client %p\n", sock, other); @@ -548,10 +564,13 @@ Server::~Server() { void Server::handleLine(char *line, int size) { - for (int i = 0; i < clientCount; i++) { - clients[i]->outputBuf.append(line, size); - clients[i]->outputBuf.append("\n", 1); - } + printf("[%d] { %s }\n", size, line); + + Buffer pkt; + pkt.writeStr(line, size); + for (int i = 0; i < clientCount; i++) + if (clients[i]->authState == Client::AS_AUTHED) + clients[i]->sendPacket(Packet::B2C_STATUS, pkt); } void Server::processReadBuffer() { // Try to process as many lines as we can @@ -573,7 +592,7 @@ void Server::processReadBuffer() { } // If we managed to handle anything, lop it off the buffer - inputBuf.trimFromStart(pos); + inputBuf.trimFromStart(lineBegin); } @@ -830,7 +849,7 @@ int main(int argc, char **argv) { int numFDs = select(maxFD+1, &readSet, &writeSet, NULL, &timeout); now = time(NULL); - printf("[%lu select:%d]\n", now, numFDs); + //printf("[%lu select:%d]\n", now, numFDs); for (int i = 0; i < clientCount; i++) { diff --git a/python_client.py b/python_client.py index 8237988..24c8a3e 100644 --- a/python_client.py +++ b/python_client.py @@ -72,7 +72,7 @@ def reader(): lastReceivedPacketID = pid clearCachedPackets(lastReceivedByServer) - packetdata = data[pos:pos+size] + packetdata = readbuf[pos:pos+size] print('0x%x : %d bytes : %s' % (type, size, packetdata)) if type == 0x8001: @@ -80,7 +80,7 @@ def reader(): authed = True elif type == 0x8003: authed = True - pid = struct.unpack('