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 */
152 CountedBuffer* inbuf;
155 class ModuleZLib : public Module
157 izip_session sessions[MAX_DESCRIPTORS];
158 float total_out_compressed;
159 float total_in_compressed;
160 float total_out_uncompressed;
161 float total_in_uncompressed;
165 ModuleZLib(InspIRCd* Me)
168 ServerInstance->PublishInterface("InspSocketHook", this);
170 total_out_compressed = total_in_compressed = 0;
171 total_out_uncompressed = total_out_uncompressed = 0;
174 virtual ~ModuleZLib()
178 virtual Version GetVersion()
180 return Version(1, 1, 0, 0, VF_VENDOR, API_VERSION);
183 void Implements(char* List)
185 List[I_OnRawSocketConnect] = List[I_OnRawSocketAccept] = List[I_OnRawSocketClose] = List[I_OnRawSocketRead] = List[I_OnRawSocketWrite] = 1;
186 List[I_OnStats] = List[I_OnRequest] = 1;
189 virtual char* OnRequest(Request* request)
191 ISHRequest* ISR = (ISHRequest*)request;
192 if (strcmp("IS_NAME", request->GetId()) == 0)
196 else if (strcmp("IS_HOOK", request->GetId()) == 0)
201 ret = ServerInstance->Config->AddIOHook((Module*)this, (InspSocket*)ISR->Sock) ? (char*)"OK" : NULL;
203 catch (ModuleException& e)
209 else if (strcmp("IS_UNHOOK", request->GetId()) == 0)
211 return ServerInstance->Config->DelIOHook((InspSocket*)ISR->Sock) ? (char*)"OK" : NULL;
213 else if (strcmp("IS_HSDONE", request->GetId()) == 0)
217 else if (strcmp("IS_ATTACH", request->GetId()) == 0)
224 virtual int OnStats(char symbol, userrec* user, string_list &results)
228 std::string sn = ServerInstance->Config->ServerName;
230 float outbound_r = 100 - ((total_out_compressed / (total_out_uncompressed + 0.001)) * 100);
231 float inbound_r = 100 - ((total_in_compressed / (total_in_uncompressed + 0.001)) * 100);
233 float total_compressed = total_in_compressed + total_out_compressed;
234 float total_uncompressed = total_in_uncompressed + total_out_uncompressed;
236 float total_r = 100 - ((total_compressed / (total_uncompressed + 0.001)) * 100);
238 char outbound_ratio[MAXBUF], inbound_ratio[MAXBUF], combined_ratio[MAXBUF];
240 sprintf(outbound_ratio, "%3.2f%%", outbound_r);
241 sprintf(inbound_ratio, "%3.2f%%", inbound_r);
242 sprintf(combined_ratio, "%3.2f%%", total_r);
244 results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS outbound_compressed = "+ConvToStr(total_out_compressed));
245 results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS inbound_compressed = "+ConvToStr(total_in_compressed));
246 results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS outbound_uncompressed = "+ConvToStr(total_out_uncompressed));
247 results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS inbound_uncompressed = "+ConvToStr(total_in_uncompressed));
248 results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS outbound_ratio = "+outbound_ratio);
249 results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS inbound_ratio = "+inbound_ratio);
250 results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS combined_ratio = "+combined_ratio);
257 virtual void OnRawSocketAccept(int fd, const std::string &ip, int localport)
259 izip_session* session = &sessions[fd];
261 /* allocate deflate state */
263 session->status = IZIP_OPEN;
265 session->inbuf = new CountedBuffer();
266 ServerInstance->Log(DEBUG,"session->inbuf ALLOC = %d, %08x", fd, session->inbuf);
268 session->c_stream.zalloc = (alloc_func)0;
269 session->c_stream.zfree = (free_func)0;
270 session->c_stream.opaque = (voidpf)0;
272 session->d_stream.zalloc = (alloc_func)0;
273 session->d_stream.zfree = (free_func)0;
274 session->d_stream.opaque = (voidpf)0;
277 virtual void OnRawSocketConnect(int fd)
279 OnRawSocketAccept(fd, "", 0);
282 virtual void OnRawSocketClose(int fd)
284 CloseSession(&sessions[fd]);
287 virtual int OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult)
289 izip_session* session = &sessions[fd];
291 if (session->status == IZIP_CLOSED)
294 unsigned char compr[CHUNK + 1];
296 readresult = read(fd, compr, CHUNK);
300 ServerInstance->Log(DEBUG,"session->inbuf PTR = %d, %08x", fd, session->inbuf);
302 session->inbuf->AddData(compr, readresult);
304 int size = session->inbuf->GetFrame(compr, CHUNK);
308 session->d_stream.next_in = (Bytef*)compr;
309 session->d_stream.avail_in = 0;
310 session->d_stream.next_out = (Bytef*)buffer;
311 if (inflateInit(&session->d_stream) != Z_OK)
314 while ((session->d_stream.total_out < count) && (session->d_stream.total_in < (unsigned int)size))
316 session->d_stream.avail_in = session->d_stream.avail_out = 1;
317 if (inflate(&session->d_stream, Z_NO_FLUSH) == Z_STREAM_END)
321 inflateEnd(&session->d_stream);
323 total_in_compressed += readresult;
324 readresult = session->d_stream.total_out;
325 total_in_uncompressed += session->d_stream.total_out;
327 buffer[session->d_stream.total_out] = 0;
329 ServerInstance->Log(DEBUG,"Decompressed %d bytes", session->d_stream.total_out);
332 return (readresult > 0);
335 virtual int OnRawSocketWrite(int fd, const char* buffer, int count)
337 ServerInstance->Log(DEBUG,"Compressing %d bytes", count);
339 izip_session* session = &sessions[fd];
344 ServerInstance->Log(DEBUG,"Nothing to do!");
348 if(session->status != IZIP_OPEN)
350 CloseSession(session);
354 unsigned char compr[count*2+4];
356 if (deflateInit(&session->c_stream, Z_BEST_COMPRESSION) != Z_OK)
358 ServerInstance->Log(DEBUG,"Deflate init failed");
361 session->c_stream.next_in = (Bytef*)buffer;
362 session->c_stream.next_out = compr+4;
364 while ((session->c_stream.total_in < (unsigned int)count) && (session->c_stream.total_out < (unsigned int)count*2))
366 session->c_stream.avail_in = session->c_stream.avail_out = 1; /* force small buffers */
367 if (deflate(&session->c_stream, Z_NO_FLUSH) != Z_OK)
369 ServerInstance->Log(DEBUG,"Couldnt deflate!");
370 CloseSession(session);
374 /* Finish the stream, still forcing small buffers: */
377 session->c_stream.avail_out = 1;
378 if (deflate(&session->c_stream, Z_FINISH) == Z_STREAM_END)
382 deflateEnd(&session->c_stream);
384 total_out_uncompressed += ocount;
385 total_out_compressed += session->c_stream.total_out;
387 int x = htonl(session->c_stream.total_out);
388 /** XXX: We memcpy it onto the start of the buffer like this to save ourselves a write().
389 * A memcpy of 4 or so bytes is less expensive and gives the tcp stack more chance of
390 * assembling the frame size into the same packet as the compressed frame.
392 memcpy(compr, &x, sizeof(x));
393 write(fd, compr, session->c_stream.total_out+4);
398 void CloseSession(izip_session* session)
400 if (session->status = IZIP_OPEN)
402 session->status = IZIP_CLOSED;
403 delete session->inbuf;
409 class ModuleZLibFactory : public ModuleFactory
420 virtual Module * CreateModule(InspIRCd* Me)
422 return new ModuleZLib(Me);
427 extern "C" void * init_module( void )
429 return new ModuleZLibFactory;