diff options
-rw-r--r-- | buffer.h | 25 | ||||
-rwxr-xr-x | build.sh | 2 | ||||
-rw-r--r-- | core.cpp | 43 | ||||
-rw-r--r-- | python_client.py | 7 |
4 files changed, 61 insertions, 16 deletions
@@ -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 */ @@ -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 @@ -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('<I', packetdata) + pid = struct.unpack('<I', packetdata)[0] clearCachedPackets(pid) try: for packet in packetCache: @@ -88,7 +88,8 @@ def reader(): except: pass elif type == 1: - print(packetdata.decode('utf-8')) + strlen = struct.unpack_from('<I', packetdata, 0)[0] + print(packetdata[4:4+strlen].decode('utf-8')) pos += size |