6 #include "inspircd_config.h"
7 #include "configreader.h"
16 #include "transport.h"
18 /* $ModDesc: Provides zlib link support for servers */
19 /* $LinkerFlags: -lz */
20 /* $ModDep: transport.h */
23 * Compressed data is transmitted across the link in the following format:
26 * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
28 * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
30 * Where: n is the size of a frame, in network byte order, 4 bytes.
31 * Z0 through Zn are Zlib compressed data, n bytes in length.
33 * If the module fails to read the entire frame, then it will buffer
34 * the portion of the last frame it received, then attempt to read
35 * the next part of the frame next time a write notification arrives.
37 * ZLIB_BEST_COMPRESSION (9) is used for all sending of data with
38 * a flush after each frame. A frame may contain multiple lines
39 * and should be treated as raw binary data.
45 enum izip_status { IZIP_OPEN, IZIP_CLOSED };
47 const unsigned int CHUNK = 128 * 1024;
49 class CountedBuffer : public classbase
51 std::deque<unsigned char> buffer; /* Current buffer contents */
52 unsigned int amount_expected; /* Amount of data expected */
59 void AddData(unsigned char* data, int data_length)
61 SI->Log(DEBUG,"AddData, %d bytes to add", data_length);
62 for (int i = 0; i < data_length; i++)
63 buffer.push_back(data[i]);
65 this->NextFrameSize();
70 if ((!amount_expected) && (buffer.size() >= 4))
72 SI->Log(DEBUG,"We dont yet have an expected amount");
73 /* We have enough to read an int */
75 for (int i = 0; i < 4; i++)
77 sz[i] = buffer.front();
81 amount_expected = ntohl(*size);
82 SI->Log(DEBUG,"Expected amount is %d", amount_expected);
86 int GetFrame(unsigned char* frame, int maxsize)
90 SI->Log(DEBUG,"Were expecting a frame of size %d", amount_expected);
91 /* We know how much we're expecting...
92 * Do we have enough yet?
94 if (buffer.size() >= amount_expected)
97 for (unsigned int i = 0; i < amount_expected; i++, j++)
99 frame[i] = buffer.front();
109 /* Not enough for a frame yet, COME AGAIN! */
114 /** Represents an ZIP user's extra data
116 class izip_session : public classbase
119 z_stream c_stream; /* compression stream */
120 z_stream d_stream; /* decompress stream */
123 CountedBuffer* inbuf;
126 class ModuleZLib : public Module
128 izip_session sessions[MAX_DESCRIPTORS];
129 float total_out_compressed;
130 float total_in_compressed;
131 float total_out_uncompressed;
132 float total_in_uncompressed;
136 ModuleZLib(InspIRCd* Me)
139 ServerInstance->PublishInterface("InspSocketHook", this);
141 total_out_compressed = total_in_compressed = 0;
142 total_out_uncompressed = total_out_uncompressed = 0;
147 virtual ~ModuleZLib()
151 virtual Version GetVersion()
153 return Version(1, 1, 0, 0, VF_VENDOR, API_VERSION);
156 void Implements(char* List)
158 List[I_OnRawSocketConnect] = List[I_OnRawSocketAccept] = List[I_OnRawSocketClose] = List[I_OnRawSocketRead] = List[I_OnRawSocketWrite] = 1;
159 List[I_OnStats] = List[I_OnRequest] = 1;
162 virtual char* OnRequest(Request* request)
164 ISHRequest* ISR = (ISHRequest*)request;
165 if (strcmp("IS_NAME", request->GetId()) == 0)
169 else if (strcmp("IS_HOOK", request->GetId()) == 0)
174 ret = ServerInstance->Config->AddIOHook((Module*)this, (InspSocket*)ISR->Sock) ? (char*)"OK" : NULL;
176 catch (ModuleException& e)
182 else if (strcmp("IS_UNHOOK", request->GetId()) == 0)
184 return ServerInstance->Config->DelIOHook((InspSocket*)ISR->Sock) ? (char*)"OK" : NULL;
186 else if (strcmp("IS_HSDONE", request->GetId()) == 0)
190 else if (strcmp("IS_ATTACH", request->GetId()) == 0)
197 virtual int OnStats(char symbol, userrec* user, string_list &results)
201 std::string sn = ServerInstance->Config->ServerName;
203 float outbound_r = 100 - ((total_out_compressed / (total_out_uncompressed + 0.001)) * 100);
204 float inbound_r = 100 - ((total_in_compressed / (total_in_uncompressed + 0.001)) * 100);
206 float total_compressed = total_in_compressed + total_out_compressed;
207 float total_uncompressed = total_in_uncompressed + total_out_uncompressed;
209 float total_r = 100 - ((total_compressed / (total_uncompressed + 0.001)) * 100);
211 char outbound_ratio[MAXBUF], inbound_ratio[MAXBUF], combined_ratio[MAXBUF];
213 sprintf(outbound_ratio, "%3.2f%%", outbound_r);
214 sprintf(inbound_ratio, "%3.2f%%", inbound_r);
215 sprintf(combined_ratio, "%3.2f%%", total_r);
217 results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS outbound_compressed = "+ConvToStr(total_out_compressed));
218 results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS inbound_compressed = "+ConvToStr(total_in_compressed));
219 results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS outbound_uncompressed = "+ConvToStr(total_out_uncompressed));
220 results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS inbound_uncompressed = "+ConvToStr(total_in_uncompressed));
221 results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS outbound_ratio = "+outbound_ratio);
222 results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS inbound_ratio = "+inbound_ratio);
223 results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS combined_ratio = "+combined_ratio);
230 virtual void OnRawSocketAccept(int fd, const std::string &ip, int localport)
232 izip_session* session = &sessions[fd];
234 /* allocate deflate state */
236 session->status = IZIP_OPEN;
238 session->inbuf = new CountedBuffer();
239 ServerInstance->Log(DEBUG,"session->inbuf ALLOC = %d, %08x", fd, session->inbuf);
241 session->c_stream.zalloc = (alloc_func)0;
242 session->c_stream.zfree = (free_func)0;
243 session->c_stream.opaque = (voidpf)0;
245 session->d_stream.zalloc = (alloc_func)0;
246 session->d_stream.zfree = (free_func)0;
247 session->d_stream.opaque = (voidpf)0;
250 virtual void OnRawSocketConnect(int fd)
252 OnRawSocketAccept(fd, "", 0);
255 virtual void OnRawSocketClose(int fd)
257 CloseSession(&sessions[fd]);
260 virtual int OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult)
262 izip_session* session = &sessions[fd];
264 if (session->status == IZIP_CLOSED)
267 unsigned char compr[CHUNK + 1];
268 unsigned int total_decomp = 0;
270 readresult = read(fd, compr, CHUNK);
274 session->inbuf->AddData(compr, readresult);
277 while ((size = session->inbuf->GetFrame(compr, CHUNK)) != 0)
279 session->d_stream.next_in = (Bytef*)compr;
280 session->d_stream.avail_in = 0;
281 session->d_stream.next_out = (Bytef*)(buffer + total_decomp);
282 if (inflateInit(&session->d_stream) != Z_OK)
285 while ((session->d_stream.total_out < count) && (session->d_stream.total_in < (unsigned int)size))
287 session->d_stream.avail_in = session->d_stream.avail_out = 1;
288 if (inflate(&session->d_stream, Z_NO_FLUSH) == Z_STREAM_END)
292 inflateEnd(&session->d_stream);
294 total_in_compressed += readresult;
295 readresult = session->d_stream.total_out;
296 total_in_uncompressed += session->d_stream.total_out;
298 total_decomp += session->d_stream.total_out;
301 buffer[total_decomp] = 0;
303 ServerInstance->Log(DEBUG,"Complete buffer: '%s' size=%d", buffer, total_decomp);
305 return (readresult > 0);
308 virtual int OnRawSocketWrite(int fd, const char* buffer, int count)
310 ServerInstance->Log(DEBUG,"Compressing %d bytes", count);
312 izip_session* session = &sessions[fd];
317 ServerInstance->Log(DEBUG,"Nothing to do!");
321 if(session->status != IZIP_OPEN)
323 CloseSession(session);
327 unsigned char compr[CHUNK];
329 if (deflateInit(&session->c_stream, Z_BEST_COMPRESSION) != Z_OK)
331 ServerInstance->Log(DEBUG,"Deflate init failed");
334 session->c_stream.next_in = (Bytef*)buffer;
335 session->c_stream.next_out = compr+4;
337 while ((session->c_stream.total_in < (unsigned int)count) && (session->c_stream.total_out < CHUNK))
339 session->c_stream.avail_in = session->c_stream.avail_out = 1; /* force small buffers */
340 if (deflate(&session->c_stream, Z_NO_FLUSH) != Z_OK)
342 ServerInstance->Log(DEBUG,"Couldnt deflate!");
343 CloseSession(session);
347 /* Finish the stream, still forcing small buffers: */
350 session->c_stream.avail_out = 1;
351 if (deflate(&session->c_stream, Z_FINISH) == Z_STREAM_END)
355 deflateEnd(&session->c_stream);
357 total_out_uncompressed += ocount;
358 total_out_compressed += session->c_stream.total_out;
360 int x = htonl(session->c_stream.total_out);
361 /** XXX: We memcpy it onto the start of the buffer like this to save ourselves a write().
362 * A memcpy of 4 or so bytes is less expensive and gives the tcp stack more chance of
363 * assembling the frame size into the same packet as the compressed frame.
365 memcpy(compr, &x, sizeof(x));
366 write(fd, compr, session->c_stream.total_out+4);
368 ServerInstance->Log(DEBUG,"Sending frame of size %d", x);
373 void CloseSession(izip_session* session)
375 if (session->status = IZIP_OPEN)
377 session->status = IZIP_CLOSED;
378 delete session->inbuf;
384 class ModuleZLibFactory : public ModuleFactory
395 virtual Module * CreateModule(InspIRCd* Me)
397 return new ModuleZLib(Me);
402 extern "C" void * init_module( void )
404 return new ModuleZLibFactory;