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.
43 enum izip_status { IZIP_OPEN, IZIP_CLOSED };
45 const unsigned int CHUNK = 128 * 1024;
47 class CountedBuffer : public classbase
49 std::deque<unsigned char> buffer; /* Current buffer contents */
50 unsigned int amount_expected; /* Amount of data expected */
57 void AddData(unsigned char* data, int data_length)
59 for (int i = 0; i < data_length; i++)
60 buffer.push_back(data[i]);
62 this->NextFrameSize();
67 if ((!amount_expected) && (buffer.size() >= 4))
69 /* We have enough to read an int */
71 for (int i = 0; i < 4; i++)
73 sz[i] = buffer.front();
77 amount_expected = ntohl(*size);
81 int GetFrame(unsigned char* frame, int maxsize)
85 /* We know how much we're expecting...
86 * Do we have enough yet?
88 if (buffer.size() >= amount_expected)
91 for (unsigned int i = 0; i < amount_expected; i++, j++)
93 frame[i] = buffer.front();
103 /* Not enough for a frame yet, COME AGAIN! */
108 /** Represents an ZIP user's extra data
110 class izip_session : public classbase
113 z_stream c_stream; /* compression stream */
114 z_stream d_stream; /* decompress stream */
117 CountedBuffer* inbuf;
120 class ModuleZLib : public Module
122 izip_session sessions[MAX_DESCRIPTORS];
123 float total_out_compressed;
124 float total_in_compressed;
125 float total_out_uncompressed;
126 float total_in_uncompressed;
130 ModuleZLib(InspIRCd* Me)
133 ServerInstance->PublishInterface("InspSocketHook", this);
135 total_out_compressed = total_in_compressed = 0;
136 total_out_uncompressed = total_out_uncompressed = 0;
139 virtual ~ModuleZLib()
143 virtual Version GetVersion()
145 return Version(1, 1, 0, 0, VF_VENDOR, API_VERSION);
148 void Implements(char* List)
150 List[I_OnRawSocketConnect] = List[I_OnRawSocketAccept] = List[I_OnRawSocketClose] = List[I_OnRawSocketRead] = List[I_OnRawSocketWrite] = 1;
151 List[I_OnStats] = List[I_OnRequest] = 1;
154 virtual char* OnRequest(Request* request)
156 ISHRequest* ISR = (ISHRequest*)request;
157 if (strcmp("IS_NAME", request->GetId()) == 0)
161 else if (strcmp("IS_HOOK", request->GetId()) == 0)
166 ret = ServerInstance->Config->AddIOHook((Module*)this, (InspSocket*)ISR->Sock) ? (char*)"OK" : NULL;
168 catch (ModuleException& e)
174 else if (strcmp("IS_UNHOOK", request->GetId()) == 0)
176 return ServerInstance->Config->DelIOHook((InspSocket*)ISR->Sock) ? (char*)"OK" : NULL;
178 else if (strcmp("IS_HSDONE", request->GetId()) == 0)
182 else if (strcmp("IS_ATTACH", request->GetId()) == 0)
189 virtual int OnStats(char symbol, userrec* user, string_list &results)
193 std::string sn = ServerInstance->Config->ServerName;
195 float outbound_r = 100 - ((total_out_compressed / (total_out_uncompressed + 0.001)) * 100);
196 float inbound_r = 100 - ((total_in_compressed / (total_in_uncompressed + 0.001)) * 100);
198 float total_compressed = total_in_compressed + total_out_compressed;
199 float total_uncompressed = total_in_uncompressed + total_out_uncompressed;
201 float total_r = 100 - ((total_compressed / (total_uncompressed + 0.001)) * 100);
203 char outbound_ratio[MAXBUF], inbound_ratio[MAXBUF], combined_ratio[MAXBUF];
205 sprintf(outbound_ratio, "%3.2f%%", outbound_r);
206 sprintf(inbound_ratio, "%3.2f%%", inbound_r);
207 sprintf(combined_ratio, "%3.2f%%", total_r);
209 results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS outbound_compressed = "+ConvToStr(total_out_compressed));
210 results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS inbound_compressed = "+ConvToStr(total_in_compressed));
211 results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS outbound_uncompressed = "+ConvToStr(total_out_uncompressed));
212 results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS inbound_uncompressed = "+ConvToStr(total_in_uncompressed));
213 results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS outbound_ratio = "+outbound_ratio);
214 results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS inbound_ratio = "+inbound_ratio);
215 results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS combined_ratio = "+combined_ratio);
222 virtual void OnRawSocketAccept(int fd, const std::string &ip, int localport)
224 izip_session* session = &sessions[fd];
226 /* allocate deflate state */
228 session->status = IZIP_OPEN;
230 session->inbuf = new CountedBuffer();
231 ServerInstance->Log(DEBUG,"session->inbuf ALLOC = %d, %08x", fd, session->inbuf);
233 session->c_stream.zalloc = (alloc_func)0;
234 session->c_stream.zfree = (free_func)0;
235 session->c_stream.opaque = (voidpf)0;
237 session->d_stream.zalloc = (alloc_func)0;
238 session->d_stream.zfree = (free_func)0;
239 session->d_stream.opaque = (voidpf)0;
242 virtual void OnRawSocketConnect(int fd)
244 OnRawSocketAccept(fd, "", 0);
247 virtual void OnRawSocketClose(int fd)
249 CloseSession(&sessions[fd]);
252 virtual int OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult)
254 izip_session* session = &sessions[fd];
256 if (session->status == IZIP_CLOSED)
259 unsigned char compr[CHUNK + 1];
261 readresult = read(fd, compr, CHUNK);
265 session->inbuf->AddData(compr, readresult);
269 while ((size = session->inbuf->GetFrame(compr, CHUNK)) != 0)
271 unsigned char localbuf[count + 1];
273 session->d_stream.next_in = (Bytef*)compr;
274 session->d_stream.avail_in = 0;
275 session->d_stream.next_out = (Bytef*)localbuf;
276 if (inflateInit(&session->d_stream) != Z_OK)
279 while ((session->d_stream.total_out < count) && (session->d_stream.total_in < (unsigned int)size))
281 session->d_stream.avail_in = session->d_stream.avail_out = 1;
282 if (inflate(&session->d_stream, Z_NO_FLUSH) == Z_STREAM_END)
286 inflateEnd(&session->d_stream);
288 localbuf[session->d_stream.total_out] = 0;
289 str_out.append((const char*)localbuf);
290 total_in_compressed += readresult;
291 readresult = session->d_stream.total_out;
292 total_in_uncompressed += session->d_stream.total_out;
295 memcpy(buffer, str_out.data(), str_out.length() > count ? count : str_out.length());
296 readresult = str_out.length();
298 return (readresult > 0);
301 virtual int OnRawSocketWrite(int fd, const char* buffer, int count)
303 ServerInstance->Log(DEBUG,"Compressing %d bytes", count);
305 izip_session* session = &sessions[fd];
310 ServerInstance->Log(DEBUG,"Nothing to do!");
314 if(session->status != IZIP_OPEN)
316 CloseSession(session);
320 unsigned char compr[CHUNK];
322 if (deflateInit(&session->c_stream, Z_BEST_COMPRESSION) != Z_OK)
324 ServerInstance->Log(DEBUG,"Deflate init failed");
327 session->c_stream.next_in = (Bytef*)buffer;
328 session->c_stream.next_out = compr+4;
330 while ((session->c_stream.total_in < (unsigned int)count) && (session->c_stream.total_out < CHUNK))
332 session->c_stream.avail_in = session->c_stream.avail_out = 1; /* force small buffers */
333 if (deflate(&session->c_stream, Z_NO_FLUSH) != Z_OK)
335 ServerInstance->Log(DEBUG,"Couldnt deflate!");
336 CloseSession(session);
340 /* Finish the stream, still forcing small buffers: */
343 session->c_stream.avail_out = 1;
344 if (deflate(&session->c_stream, Z_FINISH) == Z_STREAM_END)
348 deflateEnd(&session->c_stream);
350 total_out_uncompressed += ocount;
351 total_out_compressed += session->c_stream.total_out;
353 int x = htonl(session->c_stream.total_out);
354 /** XXX: We memcpy it onto the start of the buffer like this to save ourselves a write().
355 * A memcpy of 4 or so bytes is less expensive and gives the tcp stack more chance of
356 * assembling the frame size into the same packet as the compressed frame.
358 memcpy(compr, &x, sizeof(x));
359 write(fd, compr, session->c_stream.total_out+4);
361 ServerInstance->Log(DEBUG,"Sending frame of size %d", ntohl(x));
366 void CloseSession(izip_session* session)
368 if (session->status = IZIP_OPEN)
370 session->status = IZIP_CLOSED;
371 delete session->inbuf;
377 class ModuleZLibFactory : public ModuleFactory
388 virtual Module * CreateModule(InspIRCd* Me)
390 return new ModuleZLib(Me);
395 extern "C" void * init_module( void )
397 return new ModuleZLibFactory;