X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fmodules%2Fextra%2Fm_ziplink.cpp;h=ab40acb46556514faa9c7241622db6ca048523a3;hb=59bd18f2a0b43b71ee32124add9d40d1d3a54919;hp=dfad2d0e08f786ac36a5d748ef86824a2dc13c0e;hpb=6e287d0a402d3f348267436da77c229bb4203e3b;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/modules/extra/m_ziplink.cpp b/src/modules/extra/m_ziplink.cpp index dfad2d0e0..ab40acb46 100644 --- a/src/modules/extra/m_ziplink.cpp +++ b/src/modules/extra/m_ziplink.cpp @@ -2,21 +2,15 @@ * | Inspire Internet Relay Chat Daemon | * +------------------------------------+ * - * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev. - * E-mail: - * - * - * - * Written by Craig Edwards, Craig McLure, and others. + * InspIRCd: (C) 2002-2007 InspIRCd Development Team + * See: http://www.inspircd.org/wiki/index.php/Credits + * * This program is free but copyrighted software; see * the file COPYING for details. * * --------------------------------------------------- */ -#include -#include - #include "zlib.h" #include "inspircd_config.h" @@ -56,10 +50,18 @@ * */ +/* Status of a connection */ enum izip_status { IZIP_OPEN, IZIP_CLOSED }; +/* Maximum transfer size per read operation */ const unsigned int CHUNK = 128 * 1024; +/* This class manages a compressed chunk of data preceeded by + * a length count. + * + * It can handle having multiple chunks of data in the buffer + * at any time. + */ class CountedBuffer : public classbase { std::string buffer; /* Current buffer contents */ @@ -130,12 +132,12 @@ class CountedBuffer : public classbase class izip_session : public classbase { public: - z_stream c_stream; /* compression stream */ - z_stream d_stream; /* decompress stream */ - izip_status status; - int fd; - CountedBuffer* inbuf; - std::string outbuf; + z_stream c_stream; /* compression stream */ + z_stream d_stream; /* decompress stream */ + izip_status status; /* Connection status */ + int fd; /* File descriptor */ + CountedBuffer* inbuf; /* Holds input buffer */ + std::string outbuf; /* Holds output buffer */ }; class ModuleZLib : public Module @@ -161,6 +163,7 @@ class ModuleZLib : public Module virtual ~ModuleZLib() { + ServerInstance->UnpublishInterface("InspSocketHook", this); } virtual Version GetVersion() @@ -180,10 +183,12 @@ class ModuleZLib : public Module ISHRequest* ISR = (ISHRequest*)request; if (strcmp("IS_NAME", request->GetId()) == 0) { + /* Return name */ return "zip"; } else if (strcmp("IS_HOOK", request->GetId()) == 0) { + /* Attach to an inspsocket */ char* ret = "OK"; try { @@ -197,14 +202,21 @@ class ModuleZLib : public Module } else if (strcmp("IS_UNHOOK", request->GetId()) == 0) { + /* Detatch from an inspsocket */ return ServerInstance->Config->DelIOHook((InspSocket*)ISR->Sock) ? (char*)"OK" : NULL; } else if (strcmp("IS_HSDONE", request->GetId()) == 0) { + /* Check for completion of handshake + * (actually, this module doesnt handshake) + */ return "OK"; } else if (strcmp("IS_ATTACH", request->GetId()) == 0) { + /* Attach certificate data to the inspsocket + * (this module doesnt do that, either) + */ return NULL; } return NULL; @@ -300,6 +312,7 @@ class ModuleZLib : public Module { /* Add it to the frame queue */ session->inbuf->AddData(compr, readresult); + total_in_compressed += readresult; /* Parse all completed frames */ int size = 0; @@ -309,8 +322,9 @@ class ModuleZLib : public Module session->d_stream.avail_in = 0; session->d_stream.next_out = (Bytef*)(buffer + offset); + /* If we cant call this, well, we're boned. */ if (inflateInit(&session->d_stream) != Z_OK) - return -EBADF; + return 0; while ((session->d_stream.total_out < count) && (session->d_stream.total_in < (unsigned int)size)) { @@ -319,15 +333,19 @@ class ModuleZLib : public Module break; } + /* Stick a fork in me, i'm done */ inflateEnd(&session->d_stream); - total_in_compressed += readresult; + /* Update counters and offsets */ total_size += session->d_stream.total_out; total_in_uncompressed += session->d_stream.total_out; offset += session->d_stream.total_out; } + /* Null-terminate the buffer -- this doesnt harm binary data */ buffer[total_size] = 0; + + /* Set the read size to the correct total size */ readresult = total_size; } @@ -339,26 +357,32 @@ class ModuleZLib : public Module izip_session* session = &sessions[fd]; int ocount = count; - if (!count) + if (!count) /* Nothing to do! */ return 0; - + if(session->status != IZIP_OPEN) { + /* Seriously, wtf? */ CloseSession(session); return 0; } unsigned char compr[CHUNK + 4]; + /* Gentlemen, start your engines! */ if (deflateInit(&session->c_stream, Z_BEST_COMPRESSION) != Z_OK) { CloseSession(session); return 0; } + /* Set buffer sizes (we reserve 4 bytes at the start of the + * buffer for the length counters) + */ session->c_stream.next_in = (Bytef*)buffer; session->c_stream.next_out = compr + 4; + /* Compress the text */ while ((session->c_stream.total_in < (unsigned int)count) && (session->c_stream.total_out < CHUNK)) { session->c_stream.avail_in = session->c_stream.avail_out = 1; @@ -381,10 +405,15 @@ class ModuleZLib : public Module compr[2] = (session->c_stream.total_out >> 8); compr[3] = (session->c_stream.total_out & 0xFF); + /* Add compressed data plus leading length to the output buffer - + * Note, we may have incomplete half-sent frames in here. + */ session->outbuf.append((const char*)compr, session->c_stream.total_out + 4); + /* Lets see how much we can send out */ int ret = write(fd, session->outbuf.data(), session->outbuf.length()); + /* Check for errors, and advance the buffer if any was sent */ if (ret > 0) session->outbuf = session->outbuf.substr(ret); else if (ret < 1) @@ -414,7 +443,7 @@ class ModuleZLib : public Module void CloseSession(izip_session* session) { - if (session->status = IZIP_OPEN) + if (session->status == IZIP_OPEN) { session->status = IZIP_CLOSED; session->outbuf = "";