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_WAITFIRST, IZIP_OPEN, IZIP_CLOSED };
46 const unsigned int CHUNK = 16384;
48 /** Represents an ZIP user's extra data
50 class izip_session : public classbase
53 z_stream c_stream; /* compression stream */
54 z_stream d_stream; /* decompress stream */
61 class ModuleZLib : public Module
63 izip_session sessions[MAX_DESCRIPTORS];
64 float total_out_compressed;
65 float total_in_compressed;
66 float total_out_uncompressed;
67 float total_in_uncompressed;
71 ModuleZLib(InspIRCd* Me)
74 ServerInstance->PublishInterface("InspSocketHook", this);
76 total_out_compressed = total_in_compressed = 0;
77 total_out_uncompressed = total_out_uncompressed = 0;
84 virtual Version GetVersion()
86 return Version(1, 1, 0, 0, VF_VENDOR, API_VERSION);
89 void Implements(char* List)
91 List[I_OnRawSocketConnect] = List[I_OnRawSocketAccept] = List[I_OnRawSocketClose] = List[I_OnRawSocketRead] = List[I_OnRawSocketWrite] = 1;
92 List[I_OnStats] = List[I_OnRequest] = 1;
95 virtual char* OnRequest(Request* request)
97 ISHRequest* ISR = (ISHRequest*)request;
98 if (strcmp("IS_NAME", request->GetId()) == 0)
102 else if (strcmp("IS_HOOK", request->GetId()) == 0)
104 return ServerInstance->Config->AddIOHook((Module*)this, (InspSocket*)ISR->Sock) ? (char*)"OK" : NULL;
106 else if (strcmp("IS_UNHOOK", request->GetId()) == 0)
108 return ServerInstance->Config->DelIOHook((InspSocket*)ISR->Sock) ? (char*)"OK" : NULL;
110 else if (strcmp("IS_HSDONE", request->GetId()) == 0)
114 else if (strcmp("IS_ATTACH", request->GetId()) == 0)
121 virtual int OnStats(char symbol, userrec* user, string_list &results)
125 std::string sn = ServerInstance->Config->ServerName;
127 float outbound_r = 100 - ((total_out_compressed / (total_out_uncompressed + 0.001)) * 100);
128 float inbound_r = 100 - ((total_in_compressed / (total_in_uncompressed + 0.001)) * 100);
130 float total_compressed = total_in_compressed + total_out_compressed;
131 float total_uncompressed = total_in_uncompressed + total_out_uncompressed;
133 float total_r = 100 - ((total_compressed / (total_uncompressed + 0.001)) * 100);
135 char outbound_ratio[MAXBUF], inbound_ratio[MAXBUF], combined_ratio[MAXBUF];
137 sprintf(outbound_ratio, "%3.2f%%", outbound_r);
138 sprintf(inbound_ratio, "%3.2f%%", inbound_r);
139 sprintf(combined_ratio, "%3.2f%%", total_r);
141 results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS outbound_compressed = "+ConvToStr(total_out_compressed));
142 results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS inbound_compressed = "+ConvToStr(total_in_compressed));
143 results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS outbound_uncompressed = "+ConvToStr(total_out_uncompressed));
144 results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS inbound_uncompressed = "+ConvToStr(total_in_uncompressed));
145 results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS outbound_ratio = "+outbound_ratio);
146 results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS inbound_ratio = "+inbound_ratio);
147 results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS combined_ratio = "+combined_ratio);
154 virtual void OnRawSocketAccept(int fd, const std::string &ip, int localport)
156 izip_session* session = &sessions[fd];
158 /* allocate deflate state */
160 session->status = IZIP_WAITFIRST;
162 session->need_bytes = 0;
164 session->c_stream.zalloc = (alloc_func)0;
165 session->c_stream.zfree = (free_func)0;
166 session->c_stream.opaque = (voidpf)0;
168 session->d_stream.zalloc = (alloc_func)0;
169 session->d_stream.zfree = (free_func)0;
170 session->d_stream.opaque = (voidpf)0;
174 virtual void OnRawSocketConnect(int fd)
176 OnRawSocketAccept(fd, "", 0);
179 virtual void OnRawSocketClose(int fd)
181 CloseSession(&sessions[fd]);
184 virtual int OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult)
186 izip_session* session = &sessions[fd];
188 if (session->status == IZIP_CLOSED)
193 if (session->need_bytes)
195 size = session->need_bytes;
199 if (read(fd, &size, sizeof(size)) != sizeof(size))
204 ServerInstance->Log(DEBUG,"Size of frame to read: %d%s", size, session->need_bytes ? " (remainder of last frame)" : "");
206 unsigned char compr[size+1+session->need_bytes];
208 readresult = read(fd, compr + session->need_bytes, size);
210 if (readresult == size)
212 if(session->status == IZIP_WAITFIRST)
214 session->status = IZIP_OPEN;
217 /* Reassemble first part of last frame */
218 if (session->need_bytes)
220 for (size_t i = 0; i < session->inbuf.length(); i++)
221 compr[i] = session->inbuf[i];
224 session->d_stream.next_in = (Bytef*)compr;
225 session->d_stream.avail_in = 0;
226 session->d_stream.next_out = (Bytef*)buffer;
227 if (inflateInit(&session->d_stream) != Z_OK)
229 session->status = IZIP_OPEN;
231 while ((session->d_stream.total_out < count) && (session->d_stream.total_in < (unsigned int)readresult))
233 session->d_stream.avail_in = session->d_stream.avail_out = 1; /* force small buffers */
234 if (inflate(&session->d_stream, Z_NO_FLUSH) == Z_STREAM_END)
238 inflateEnd(&session->d_stream);
240 total_in_compressed += readresult;
241 readresult = session->d_stream.total_out;
242 total_in_uncompressed += session->d_stream.total_out;
244 buffer[readresult] = 0;
245 session->need_bytes = 0;
249 /* We need to buffer here */
250 ServerInstance->Log(DEBUG,"Didnt read whole frame, got %d bytes of %d!", readresult, size);
251 session->need_bytes = ((readresult > -1) ? (size - readresult) : (size));
254 /* Do it this way because it needs to be binary safe */
255 for (int i = 0; i < readresult; i++)
256 session->inbuf += compr[i];
260 return (readresult > 0);
263 virtual int OnRawSocketWrite(int fd, const char* buffer, int count)
268 ServerInstance->Log(DEBUG,"Nothing to do!");
272 unsigned char compr[count*2+4];
274 izip_session* session = &sessions[fd];
276 if(session->status == IZIP_WAITFIRST)
278 session->status = IZIP_OPEN;
281 if (deflateInit(&session->c_stream, Z_BEST_COMPRESSION) != Z_OK)
283 ServerInstance->Log(DEBUG,"Deflate init failed");
286 if(session->status != IZIP_OPEN)
288 ServerInstance->Log(DEBUG,"State not open!");
289 CloseSession(session);
293 session->c_stream.next_in = (Bytef*)buffer;
294 session->c_stream.next_out = compr+4;
296 while ((session->c_stream.total_in < (unsigned int)count) && (session->c_stream.total_out < (unsigned int)count*2))
298 session->c_stream.avail_in = session->c_stream.avail_out = 1; /* force small buffers */
299 if (deflate(&session->c_stream, Z_NO_FLUSH) != Z_OK)
301 ServerInstance->Log(DEBUG,"Couldnt deflate!");
302 CloseSession(session);
306 /* Finish the stream, still forcing small buffers: */
309 session->c_stream.avail_out = 1;
310 if (deflate(&session->c_stream, Z_FINISH) == Z_STREAM_END)
314 deflateEnd(&session->c_stream);
316 total_out_uncompressed += ocount;
317 total_out_compressed += session->c_stream.total_out;
319 int x = htonl(session->c_stream.total_out);
320 /** XXX: We memcpy it onto the start of the buffer like this to save ourselves a write().
321 * A memcpy of 4 or so bytes is less expensive and gives the tcp stack more chance of
322 * assembling the frame size into the same packet as the compressed frame.
324 memcpy(compr, &x, sizeof(x));
325 write(fd, compr, session->c_stream.total_out+4);
330 void CloseSession(izip_session* session)
332 session->status = IZIP_CLOSED;
337 class ModuleZLibFactory : public ModuleFactory
348 virtual Module * CreateModule(InspIRCd* Me)
350 return new ModuleZLib(Me);
355 extern "C" void * init_module( void )
357 return new ModuleZLibFactory;