* | Inspire Internet Relay Chat Daemon |
* +------------------------------------+
*
- * InspIRCd: (C) 2002-2008 InspIRCd Development Team
- * See: http://www.inspircd.org/wiki/index.php/Credits
+ * InspIRCd: (C) 2002-2009 InspIRCd Development Team
+ * See: http://wiki.inspircd.org/Credits
*
* This program is free but copyrighted software; see
* the file COPYING for details.
#include "inspircd.h"
#include <zlib.h>
-#include "users.h"
-#include "channels.h"
-#include "modules.h"
-#include "socket.h"
-#include "hashcomp.h"
-#include "transport.h"
-
#include <iostream>
/* $ModDesc: Provides zlib link support for servers */
/* $LinkerFlags: -lz */
-/* $ModDep: transport.h */
/*
* ZLIB_BEST_COMPRESSION (9) is used for all sending of data with
/* Status of a connection */
enum izip_status { IZIP_CLOSED = 0, IZIP_OPEN };
-/* Maximum transfer size per read operation */
-const unsigned int CHUNK = 128 * 1024;
-
/** Represents an zipped connections extra data
*/
-class izip_session : public classbase
+class izip_session
{
public:
z_stream c_stream; /* compression stream */
float total_out_uncompressed;
float total_in_uncompressed;
- /* Used for reading data from the wire.
- * We need a smaller buffer than CHUNK, else we could read more data
- * from the wire than we can pass to the socket code.
- */
- char *read_buffer;
- unsigned int read_buffer_size;
+ /* Used for reading data from the wire and compressing data to. */
+ char *net_buffer;
+ unsigned int net_buffer_size;
public:
- ModuleZLib(InspIRCd* Me)
- : Module::Module(Me)
+ ModuleZLib()
{
- ServerInstance->Modules->PublishInterface("BufferedSocketHook", this);
-
sessions = new izip_session[ServerInstance->SE->GetMaxFds()];
for (int i = 0; i < ServerInstance->SE->GetMaxFds(); i++)
sessions[i].status = IZIP_CLOSED;
total_out_compressed = total_in_compressed = 0;
total_out_uncompressed = total_in_uncompressed = 0;
- Implementation eventlist[] = { I_OnRawSocketConnect, I_OnRawSocketAccept, I_OnRawSocketClose, I_OnRawSocketRead, I_OnRawSocketWrite, I_OnStats, I_OnRequest };
- ServerInstance->Modules->Attach(eventlist, this, 7);
+ Implementation eventlist[] = { I_OnStats };
+ ServerInstance->Modules->Attach(eventlist, this, 1);
- read_buffer_size = ServerInstance->Config->NetBufferSize / 4;
- read_buffer = new char[read_buffer_size];
+ // Allocate a buffer which is used for reading and writing data
+ net_buffer_size = ServerInstance->Config->NetBufferSize;
+ net_buffer = new char[net_buffer_size];
}
- virtual ~ModuleZLib()
+ ~ModuleZLib()
{
- ServerInstance->Modules->UnpublishInterface("BufferedSocketHook", this);
delete[] sessions;
- delete[] read_buffer;
+ delete[] net_buffer;
}
- virtual Version GetVersion()
+ Version GetVersion()
{
- return Version("$Id$", VF_VENDOR, API_VERSION);
- }
-
-
- /* Handle BufferedSocketHook API requests */
- virtual const char* OnRequest(Request* request)
- {
- 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 */
- const char* ret = "OK";
- try
- {
- ret = ServerInstance->Config->AddIOHook((Module*)this, (BufferedSocket*)ISR->Sock) ? "OK" : NULL;
- }
- catch (ModuleException& e)
- {
- return NULL;
- }
- return ret;
- }
- else if (strcmp("IS_UNHOOK", request->GetId()) == 0)
- {
- /* Detach from an inspsocket */
- return ServerInstance->Config->DelIOHook((BufferedSocket*)ISR->Sock) ? "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;
+ return Version("Provides zlib link support for servers", VF_VENDOR);
}
/* Handle stats z (misc stats) */
- virtual int OnStats(char symbol, User* user, string_list &results)
+ ModResult OnStats(char symbol, User* user, string_list &results)
{
if (symbol == 'z')
{
results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS inbound_compressed = "+ConvToStr(total_in_compressed));
results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS outbound_uncompressed = "+ConvToStr(total_out_uncompressed));
results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS inbound_uncompressed = "+ConvToStr(total_in_uncompressed));
- results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS outbound_ratio = "+outbound_ratio);
- results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS inbound_ratio = "+inbound_ratio);
- results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS combined_ratio = "+combined_ratio);
- return 0;
+ results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS percentage_of_original_outbound_traffic = "+outbound_ratio);
+ results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS percentage_of_orignal_inbound_traffic = "+inbound_ratio);
+ results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS total_size_of_original_traffic = "+combined_ratio);
+ return MOD_RES_PASSTHRU;
}
- return 0;
+ return MOD_RES_PASSTHRU;
}
- virtual void OnRawSocketConnect(int fd)
+ void OnStreamSocketConnect(StreamSocket* user)
{
- if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds() - 1))
- return;
+ OnStreamSocketAccept(user, 0, 0);
+ }
- izip_session* session = &sessions[fd];
+ void OnRawSocketAccept(StreamSocket* user, irc::sockets::sockaddrs*, irc::sockets::sockaddrs*)
+ {
+ int fd = user->GetFd();
- /* allocate state and buffers */
- session->status = IZIP_OPEN;
+ izip_session* session = &sessions[fd];
/* Just in case... */
session->outbuf.clear();
session->status = IZIP_CLOSED;
return;
}
- }
- virtual void OnRawSocketAccept(int fd, const std::string &ip, int localport)
- {
- /* Nothing special needs doing here compared to connect() */
- OnRawSocketConnect(fd);
+ /* Just in case, do this last */
+ session->status = IZIP_OPEN;
}
- virtual void OnRawSocketClose(int fd)
+ void OnStreamSocketClose(StreamSocket* user)
{
+ int fd = user->GetFd();
CloseSession(&sessions[fd]);
}
- virtual int OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult)
+ int OnStreamSocketRead(StreamSocket* user, std::string& recvq)
{
+ int fd = user->GetFd();
/* Find the sockets session */
izip_session* session = &sessions[fd];
if (session->status == IZIP_CLOSED)
- return 0;
+ return -1;
- /* Read read_buffer_size bytes at a time to the buffer (usually 128k) */
- readresult = read(fd, read_buffer, read_buffer_size);
+ if (session->inbuf.empty())
+ {
+ /* Read read_buffer_size bytes at a time to the buffer (usually 2.5k) */
+ int readresult = read(fd, net_buffer, net_buffer_size);
- /* Did we get anything? */
- if (readresult <= 0)
- return 0;
+ if (readresult < 0)
+ {
+ if (errno == EINTR || errno == EAGAIN)
+ return 0;
+ }
+ if (readresult <= 0)
+ return -1;
- total_in_compressed += readresult;
+ total_in_compressed += readresult;
+
+ /* Copy the compressed data into our input buffer */
+ session->inbuf.append(net_buffer, readresult);
+ }
- /* Copy the compressed data into out input buffer */
- session->inbuf.append(read_buffer, readresult);
size_t in_len = session->inbuf.length();
+ char* buffer = ServerInstance->GetReadBuffer();
+ int count = ServerInstance->Config->NetBufferSize;
/* Prepare decompression */
session->d_stream.next_in = (Bytef *)session->inbuf.c_str();
}
if (ret != Z_OK)
{
- readresult = 0;
- return 0;
+ return -1;
}
/* Update the inbut buffer */
total_in_uncompressed += uncompressed_length;
/* Null-terminate the buffer -- this doesnt harm binary data */
- buffer[uncompressed_length] = 0;
-
- /* Set the read size to the correct total size */
- readresult = uncompressed_length;
-
+ recvq.append(buffer, uncompressed_length);
return 1;
}
- virtual int OnRawSocketWrite(int fd, const char* buffer, int count)
+ int OnStreamSocketWrite(StreamSocket* user, std::string& sendq)
{
+ int fd = user->GetFd();
izip_session* session = &sessions[fd];
- if (!count) /* Nothing to do! */
- return 0;
-
if(session->status != IZIP_OPEN)
/* Seriously, wtf? */
- return 0;
+ return -1;
- unsigned char compr[CHUNK];
int ret;
/* This loop is really only supposed to run once, but in case 'compr'
do
{
/* Prepare compression */
- session->c_stream.next_in = (Bytef*)buffer + offset;
- session->c_stream.avail_in = count - offset;
+ session->c_stream.next_in = (Bytef*)sendq.data() + offset;
+ session->c_stream.avail_in = sendq.length() - offset;
- session->c_stream.next_out = compr;
- session->c_stream.avail_out = CHUNK;
+ session->c_stream.next_out = (Bytef*)net_buffer;
+ session->c_stream.avail_out = net_buffer_size;
/* Compress the text */
ret = deflate(&session->c_stream, Z_SYNC_FLUSH);
return 0;
/* Space before - space after stuff was added to this */
- unsigned int compressed = CHUNK - session->c_stream.avail_out;
- unsigned int uncompressed = count - session->c_stream.avail_in;
+ unsigned int compressed = net_buffer_size - session->c_stream.avail_out;
+ unsigned int uncompressed = sendq.length() - session->c_stream.avail_in;
/* Make it skip the data which was compressed already */
offset += uncompressed;
total_out_compressed += compressed;
/* Add compressed to the output buffer */
- session->outbuf.append((const char*)compr, compressed);
+ session->outbuf.append((const char*)net_buffer, compressed);
} while (session->c_stream.avail_in != 0);
/* Lets see how much we can send out */
else
{
session->outbuf.clear();
- /* TODO pass errors down? */
- return 0;
+ return -1;
}
}
- /* ALL LIES the lot of it, we havent really written
- * this amount, but the layer above doesnt need to know.
- */
- return count;
+ return 1;
}
void Error(izip_session* session, const std::string &text)