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.
44 enum izip_status { IZIP_OPEN, IZIP_CLOSED };
46 const unsigned int CHUNK = 16384;
48 class CountedBuffer : public classbase
50 int bufptr; /* Current tail location */
51 unsigned char* buffer; /* Current buffer contents */
52 int bufsz; /* Current buffer size */
53 int amount_expected; /* Amount of data expected */
54 int amount_read; /* Amount of data read so far */
59 buffer = new unsigned char[bufsz + 1];
69 void AddData(unsigned char* data, int data_length)
71 if ((data_length + bufptr) > bufsz)
73 /* Buffer is too small, enlarge it and copy contents */
74 int old_bufsz = bufsz;
75 unsigned char* temp = buffer;
78 buffer = new unsigned char[bufsz + 1];
80 memcpy(buffer, temp, old_bufsz);
85 memcpy(buffer + bufptr, data, data_length);
86 bufptr += data_length;
87 amount_read += data_length;
89 if ((!amount_expected) && (amount_read >= 4))
91 /* We have enough to read an int */
92 int* size = (int*)buffer;
93 amount_expected = ntohl(*size);
97 int GetFrame(unsigned char* frame, int maxsize)
101 /* We know how much we're expecting...
102 * Do we have enough yet?
104 if ((amount_read - 4) >= amount_expected)
106 int amt_ex = amount_expected;
107 /* Yes, we have enough now */
108 memcpy(frame, buffer + 4, amount_expected > maxsize ? maxsize : amount_expected);
110 return (amt_ex > maxsize) ? maxsize : amt_ex;
113 /* Not enough for a frame yet, COME AGAIN! */
117 void RemoveFirstFrame()
119 unsigned char* temp = buffer;
121 bufsz -= (amount_expected + 4);
122 buffer = new unsigned char[bufsz + 1];
124 memcpy(buffer, temp + amount_expected, bufsz);
126 amount_read -= (amount_expected + 4);
128 if (amount_read >= 4)
130 /* We have enough to read an int */
131 int* size = (int*)buffer;
132 amount_expected = ntohl(*size);
143 /** Represents an ZIP user's extra data
145 class izip_session : public classbase
148 z_stream c_stream; /* compression stream */
149 z_stream d_stream; /* decompress stream */
153 CountedBuffer* inbuf;
156 class ModuleZLib : public Module
158 izip_session sessions[MAX_DESCRIPTORS];
159 float total_out_compressed;
160 float total_in_compressed;
161 float total_out_uncompressed;
162 float total_in_uncompressed;
166 ModuleZLib(InspIRCd* Me)
169 ServerInstance->PublishInterface("InspSocketHook", this);
171 total_out_compressed = total_in_compressed = 0;
172 total_out_uncompressed = total_out_uncompressed = 0;
175 virtual ~ModuleZLib()
179 virtual Version GetVersion()
181 return Version(1, 1, 0, 0, VF_VENDOR, API_VERSION);
184 void Implements(char* List)
186 List[I_OnRawSocketConnect] = List[I_OnRawSocketAccept] = List[I_OnRawSocketClose] = List[I_OnRawSocketRead] = List[I_OnRawSocketWrite] = 1;
187 List[I_OnStats] = List[I_OnRequest] = 1;
190 virtual char* OnRequest(Request* request)
192 ISHRequest* ISR = (ISHRequest*)request;
193 if (strcmp("IS_NAME", request->GetId()) == 0)
197 else if (strcmp("IS_HOOK", request->GetId()) == 0)
199 return ServerInstance->Config->AddIOHook((Module*)this, (InspSocket*)ISR->Sock) ? (char*)"OK" : NULL;
201 else if (strcmp("IS_UNHOOK", request->GetId()) == 0)
203 return ServerInstance->Config->DelIOHook((InspSocket*)ISR->Sock) ? (char*)"OK" : NULL;
205 else if (strcmp("IS_HSDONE", request->GetId()) == 0)
209 else if (strcmp("IS_ATTACH", request->GetId()) == 0)
216 virtual int OnStats(char symbol, userrec* user, string_list &results)
220 std::string sn = ServerInstance->Config->ServerName;
222 float outbound_r = 100 - ((total_out_compressed / (total_out_uncompressed + 0.001)) * 100);
223 float inbound_r = 100 - ((total_in_compressed / (total_in_uncompressed + 0.001)) * 100);
225 float total_compressed = total_in_compressed + total_out_compressed;
226 float total_uncompressed = total_in_uncompressed + total_out_uncompressed;
228 float total_r = 100 - ((total_compressed / (total_uncompressed + 0.001)) * 100);
230 char outbound_ratio[MAXBUF], inbound_ratio[MAXBUF], combined_ratio[MAXBUF];
232 sprintf(outbound_ratio, "%3.2f%%", outbound_r);
233 sprintf(inbound_ratio, "%3.2f%%", inbound_r);
234 sprintf(combined_ratio, "%3.2f%%", total_r);
236 results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS outbound_compressed = "+ConvToStr(total_out_compressed));
237 results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS inbound_compressed = "+ConvToStr(total_in_compressed));
238 results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS outbound_uncompressed = "+ConvToStr(total_out_uncompressed));
239 results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS inbound_uncompressed = "+ConvToStr(total_in_uncompressed));
240 results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS outbound_ratio = "+outbound_ratio);
241 results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS inbound_ratio = "+inbound_ratio);
242 results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS combined_ratio = "+combined_ratio);
249 virtual void OnRawSocketAccept(int fd, const std::string &ip, int localport)
251 izip_session* session = &sessions[fd];
253 /* allocate deflate state */
255 session->status = IZIP_OPEN;
257 session->inbuf = new CountedBuffer();
259 session->c_stream.zalloc = (alloc_func)0;
260 session->c_stream.zfree = (free_func)0;
261 session->c_stream.opaque = (voidpf)0;
263 session->d_stream.zalloc = (alloc_func)0;
264 session->d_stream.zfree = (free_func)0;
265 session->d_stream.opaque = (voidpf)0;
269 virtual void OnRawSocketConnect(int fd)
271 OnRawSocketAccept(fd, "", 0);
274 virtual void OnRawSocketClose(int fd)
276 CloseSession(&sessions[fd]);
279 virtual int OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult)
281 izip_session* session = &sessions[fd];
283 if (session->status == IZIP_CLOSED)
286 unsigned char compr[CHUNK + 1];
288 readresult = read(fd, compr, CHUNK);
292 session->inbuf->AddData(compr, readresult);
294 int size = session->inbuf->GetFrame(compr, CHUNK);
298 session->d_stream.next_in = (Bytef*)compr;
299 session->d_stream.avail_in = 0;
300 session->d_stream.next_out = (Bytef*)buffer;
301 if (inflateInit(&session->d_stream) != Z_OK)
304 while ((session->d_stream.total_out < count) && (session->d_stream.total_in < (unsigned int)size))
306 session->d_stream.avail_in = session->d_stream.avail_out = 1;
307 if (inflate(&session->d_stream, Z_NO_FLUSH) == Z_STREAM_END)
311 inflateEnd(&session->d_stream);
313 total_in_compressed += readresult;
314 readresult = session->d_stream.total_out;
315 total_in_uncompressed += session->d_stream.total_out;
317 buffer[session->d_stream.total_out] = 0;
320 return (readresult > 0);
323 virtual int OnRawSocketWrite(int fd, const char* buffer, int count)
325 izip_session* session = &sessions[fd];
330 ServerInstance->Log(DEBUG,"Nothing to do!");
334 if(session->status != IZIP_OPEN)
336 CloseSession(session);
340 unsigned char compr[count*2+4];
342 if (deflateInit(&session->c_stream, Z_BEST_COMPRESSION) != Z_OK)
344 ServerInstance->Log(DEBUG,"Deflate init failed");
347 session->c_stream.next_in = (Bytef*)buffer;
348 session->c_stream.next_out = compr+4;
350 while ((session->c_stream.total_in < (unsigned int)count) && (session->c_stream.total_out < (unsigned int)count*2))
352 session->c_stream.avail_in = session->c_stream.avail_out = 1; /* force small buffers */
353 if (deflate(&session->c_stream, Z_NO_FLUSH) != Z_OK)
355 ServerInstance->Log(DEBUG,"Couldnt deflate!");
356 CloseSession(session);
360 /* Finish the stream, still forcing small buffers: */
363 session->c_stream.avail_out = 1;
364 if (deflate(&session->c_stream, Z_FINISH) == Z_STREAM_END)
368 deflateEnd(&session->c_stream);
370 total_out_uncompressed += ocount;
371 total_out_compressed += session->c_stream.total_out;
373 int x = htonl(session->c_stream.total_out);
374 /** XXX: We memcpy it onto the start of the buffer like this to save ourselves a write().
375 * A memcpy of 4 or so bytes is less expensive and gives the tcp stack more chance of
376 * assembling the frame size into the same packet as the compressed frame.
378 memcpy(compr, &x, sizeof(x));
379 write(fd, compr, session->c_stream.total_out+4);
384 void CloseSession(izip_session* session)
386 if (session->status = IZIP_OPEN)
388 session->status = IZIP_CLOSED;
389 delete session->inbuf;
395 class ModuleZLibFactory : public ModuleFactory
406 virtual Module * CreateModule(InspIRCd* Me)
408 return new ModuleZLib(Me);
413 extern "C" void * init_module( void )
415 return new ModuleZLibFactory;