6 #include "inspircd_config.h"
7 #include "configreader.h"
18 /* $ModDesc: Provides zlib link support for servers */
19 /* $LinkerFlags: -lz */
23 enum izip_status { IZIP_WAITFIRST, IZIP_OPEN, IZIP_CLOSED };
25 const unsigned int CHUNK = 16384;
27 /** Represents an ZIP user's extra data
29 class izip_session : public classbase
32 z_stream c_stream; /* compression stream */
33 z_stream d_stream; /* decompress stream */
40 class ModuleZLib : public Module
42 izip_session sessions[MAX_DESCRIPTORS];
46 ModuleZLib(InspIRCd* Me)
49 ServerInstance->PublishInterface("InspSocketHook", this);
56 virtual Version GetVersion()
58 return Version(1, 1, 0, 0, VF_VENDOR, API_VERSION);
61 void Implements(char* List)
63 List[I_OnRawSocketConnect] = List[I_OnRawSocketAccept] = List[I_OnRawSocketClose] = List[I_OnRawSocketRead] = List[I_OnRawSocketWrite] = 1;
64 List[I_OnRequest] = 1;
67 virtual char* OnRequest(Request* request)
69 ISHRequest* ISR = (ISHRequest*)request;
70 if (strcmp("IS_NAME", request->GetId()) == 0)
74 else if (strcmp("IS_HOOK", request->GetId()) == 0)
76 return ServerInstance->Config->AddIOHook((Module*)this, (InspSocket*)ISR->Sock) ? (char*)"OK" : NULL;
78 else if (strcmp("IS_UNHOOK", request->GetId()) == 0)
80 return ServerInstance->Config->DelIOHook((InspSocket*)ISR->Sock) ? (char*)"OK" : NULL;
82 else if (strcmp("IS_HSDONE", request->GetId()) == 0)
86 else if (strcmp("IS_ATTACH", request->GetId()) == 0)
94 virtual void OnRawSocketAccept(int fd, const std::string &ip, int localport)
96 izip_session* session = &sessions[fd];
98 /* allocate deflate state */
100 session->status = IZIP_WAITFIRST;
102 session->need_bytes = 0;
104 session->c_stream.zalloc = (alloc_func)0;
105 session->c_stream.zfree = (free_func)0;
106 session->c_stream.opaque = (voidpf)0;
108 session->d_stream.zalloc = (alloc_func)0;
109 session->d_stream.zfree = (free_func)0;
110 session->d_stream.opaque = (voidpf)0;
114 virtual void OnRawSocketConnect(int fd)
116 OnRawSocketAccept(fd, "", 0);
119 virtual void OnRawSocketClose(int fd)
121 CloseSession(&sessions[fd]);
124 virtual int OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult)
126 izip_session* session = &sessions[fd];
128 if (session->status == IZIP_CLOSED)
133 if (session->need_bytes)
135 size = session->need_bytes;
139 if (read(fd, &size, sizeof(size)) != sizeof(size))
144 ServerInstance->Log(DEBUG,"Size of frame to read: %d%s", size, session->need_bytes ? " (remainder of last frame)" : "");
146 unsigned char compr[size+1+session->need_bytes];
148 readresult = read(fd, compr + session->need_bytes, size);
150 if (readresult == size)
152 if(session->status == IZIP_WAITFIRST)
154 session->status = IZIP_OPEN;
157 /* Reassemble first part of last frame */
158 if (session->need_bytes)
160 for (size_t i = 0; i < session->inbuf.length(); i++)
161 compr[i] = session->inbuf[i];
164 session->d_stream.next_in = (Bytef*)compr;
165 session->d_stream.avail_in = 0;
166 session->d_stream.next_out = (Bytef*)buffer;
167 if (inflateInit(&session->d_stream) != Z_OK)
169 session->status = IZIP_OPEN;
171 while ((session->d_stream.total_out < count) && (session->d_stream.total_in < (unsigned int)readresult))
173 session->d_stream.avail_in = session->d_stream.avail_out = 1; /* force small buffers */
174 if (inflate(&session->d_stream, Z_NO_FLUSH) == Z_STREAM_END)
178 inflateEnd(&session->d_stream);
180 readresult = session->d_stream.total_out;
182 buffer[readresult] = 0;
183 session->need_bytes = 0;
187 /* We need to buffer here */
188 ServerInstance->Log(DEBUG,"Didnt read whole frame, got %d bytes of %d!", readresult, size);
189 session->need_bytes = ((readresult > -1) ? (size - readresult) : (size));
192 /* Do it this way because it needs to be binary safe */
193 for (int i = 0; i < readresult; i++)
194 session->inbuf += compr[i];
198 return (readresult > 0);
201 virtual int OnRawSocketWrite(int fd, const char* buffer, int count)
206 ServerInstance->Log(DEBUG,"Nothing to do!");
210 unsigned char compr[count*2+4];
212 izip_session* session = &sessions[fd];
214 if(session->status == IZIP_WAITFIRST)
216 session->status = IZIP_OPEN;
219 if (deflateInit(&session->c_stream, Z_BEST_COMPRESSION) != Z_OK)
221 ServerInstance->Log(DEBUG,"Deflate init failed");
224 if(session->status != IZIP_OPEN)
226 ServerInstance->Log(DEBUG,"State not open!");
227 CloseSession(session);
231 session->c_stream.next_in = (Bytef*)buffer;
232 session->c_stream.next_out = compr+4;
234 while ((session->c_stream.total_in < (unsigned int)count) && (session->c_stream.total_out < (unsigned int)count*2))
236 session->c_stream.avail_in = session->c_stream.avail_out = 1; /* force small buffers */
237 if (deflate(&session->c_stream, Z_NO_FLUSH) != Z_OK)
239 ServerInstance->Log(DEBUG,"Couldnt deflate!");
240 CloseSession(session);
244 /* Finish the stream, still forcing small buffers: */
247 session->c_stream.avail_out = 1;
248 if (deflate(&session->c_stream, Z_FINISH) == Z_STREAM_END)
252 int x = htonl(session->c_stream.total_out);
253 /** XXX: We memcpy it onto the start of the buffer like this to save ourselves a write().
254 * A memcpy of 4 or so bytes is less expensive and gives the tcp stack more chance of
255 * assembling the frame size into the same packet as the compressed frame.
257 memcpy(compr, &x, sizeof(x));
258 write(fd, compr, session->c_stream.total_out+4);
260 deflateEnd(&session->c_stream);
265 void CloseSession(izip_session* session)
267 session->status = IZIP_CLOSED;
272 class ModuleZLibFactory : public ModuleFactory
283 virtual Module * CreateModule(InspIRCd* Me)
285 return new ModuleZLib(Me);
290 extern "C" void * init_module( void )
292 return new ModuleZLibFactory;