]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/inspsocket.cpp
Remove individual read buffers inside of BufferedSocket, use the shared netbuffer...
[user/henk/code/inspircd.git] / src / inspsocket.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $Core: libIRCDinspsocket */
15
16 #include "socket.h"
17 #include "inspstring.h"
18 #include "socketengine.h"
19 #include "inspircd.h"
20
21 using irc::sockets::OpenTCPSocket;
22
23 bool BufferedSocket::Readable()
24 {
25         return ((this->state != I_CONNECTING) && (this->WaitingForWriteEvent == false));
26 }
27
28 BufferedSocket::BufferedSocket(InspIRCd* SI)
29 {
30         this->Timeout = NULL;
31         this->state = I_DISCONNECTED;
32         this->fd = -1;
33         this->WaitingForWriteEvent = false;
34         this->Instance = SI;
35         this->IsIOHooked = false;
36 }
37
38 BufferedSocket::BufferedSocket(InspIRCd* SI, int newfd, const char* ip)
39 {
40         this->Timeout = NULL;
41         this->fd = newfd;
42         this->state = I_CONNECTED;
43         strlcpy(this->IP,ip,MAXBUF);
44         this->WaitingForWriteEvent = false;
45         this->Instance = SI;
46         this->IsIOHooked = false;
47         if (this->fd > -1)
48                 this->Instance->SE->AddFd(this);
49 }
50
51 BufferedSocket::BufferedSocket(InspIRCd* SI, const std::string &ipaddr, int aport, bool listening, unsigned long maxtime, const std::string &connectbindip)
52 {
53         this->cbindip = connectbindip;
54         this->fd = -1;
55         this->Instance = SI;
56         strlcpy(host,ipaddr.c_str(),MAXBUF);
57         this->WaitingForWriteEvent = false;
58         this->IsIOHooked = false;
59         this->Timeout = NULL;
60         if (listening)
61         {
62                 if ((this->fd = OpenTCPSocket(host)) == ERROR)
63                 {
64                         this->fd = -1;
65                         this->state = I_ERROR;
66                         this->OnError(I_ERR_SOCKET);
67                         return;
68                 }
69                 else
70                 {
71                         if (!SI->BindSocket(this->fd,aport,(char*)ipaddr.c_str()))
72                         {
73                                 this->Close();
74                                 this->fd = -1;
75                                 this->state = I_ERROR;
76                                 this->OnError(I_ERR_BIND);
77                                 this->ClosePending = true;
78                                 return;
79                         }
80                         else
81                         {
82                                 this->state = I_LISTENING;
83                                 this->port = aport;
84                                 if (this->fd > -1)
85                                 {
86                                         if (!this->Instance->SE->AddFd(this))
87                                         {
88                                                 this->Close();
89                                                 this->state = I_ERROR;
90                                                 this->OnError(I_ERR_NOMOREFDS);
91                                         }
92                                 }
93                                 return;
94                         }
95                 }
96         }
97         else
98         {
99                 strlcpy(this->host,ipaddr.c_str(),MAXBUF);
100                 this->port = aport;
101
102                 bool ipvalid = true;
103 #ifdef IPV6
104                 if (strchr(host,':'))
105                 {
106                         in6_addr n;
107                         if (inet_pton(AF_INET6, host, &n) < 1)
108                                 ipvalid = false;
109                 }
110                 else
111 #endif
112                 {
113                         in_addr n;
114                         if (inet_aton(host,&n) < 1)
115                                 ipvalid = false;
116                 }
117                 if (!ipvalid)
118                 {
119                         this->Instance->Logs->Log("SOCKET", DEBUG,"BUG: Hostname passed to BufferedSocket, rather than an IP address!");
120                         this->OnError(I_ERR_CONNECT);
121                         this->Close();
122                         this->fd = -1;
123                         this->state = I_ERROR;
124                         return;
125                 }
126                 else
127                 {
128                         strlcpy(this->IP,host,MAXBUF);
129                         timeout_val = maxtime;
130                         if (!this->DoConnect())
131                         {
132                                 this->OnError(I_ERR_CONNECT);
133                                 this->Close();
134                                 this->fd = -1;
135                                 this->state = I_ERROR;
136                                 return;
137                         }
138                 }
139         }
140 }
141
142 void BufferedSocket::WantWrite()
143 {
144         this->Instance->SE->WantWrite(this);
145         this->WaitingForWriteEvent = true;
146 }
147
148 void BufferedSocket::SetQueues(int nfd)
149 {
150         // attempt to increase socket sendq and recvq as high as its possible
151         int sendbuf = 32768;
152         int recvbuf = 32768;
153         if(setsockopt(nfd,SOL_SOCKET,SO_SNDBUF,(const char *)&sendbuf,sizeof(sendbuf)) || setsockopt(nfd,SOL_SOCKET,SO_RCVBUF,(const char *)&recvbuf,sizeof(sendbuf)))
154         {
155                 //this->Instance->Log(DEFAULT, "Could not increase SO_SNDBUF/SO_RCVBUF for socket %u", GetFd());
156                 ; // do nothing. I'm a little sick of people trying to interpret this message as a result of why their incorrect setups don't work.
157         }
158 }
159
160 /* Most irc servers require you to specify the ip you want to bind to.
161  * If you dont specify an IP, they rather dumbly bind to the first IP
162  * of the box (e.g. INADDR_ANY). In InspIRCd, we scan thought the IP
163  * addresses we've bound server ports to, and we try and bind our outbound
164  * connections to the first usable non-loopback and non-any IP we find.
165  * This is easier to configure when you have a lot of links and a lot
166  * of servers to configure.
167  */
168 bool BufferedSocket::BindAddr(const std::string &ip)
169 {
170         ConfigReader Conf(this->Instance);
171         socklen_t size = sizeof(sockaddr_in);
172 #ifdef IPV6
173         bool v6 = false;
174         /* Are we looking for a binding to fit an ipv6 host? */
175         if ((ip.empty()) || (ip.find(':') != std::string::npos))
176                 v6 = true;
177 #endif
178         int j = 0;
179         while (j < Conf.Enumerate("bind") || (!ip.empty()))
180         {
181                 std::string sIP = ip.empty() ? Conf.ReadValue("bind","address",j) : ip;
182                 if (!ip.empty() || Conf.ReadValue("bind","type",j) == "servers")
183                 {
184                         if (!ip.empty() || ((sIP != "*") && (sIP != "127.0.0.1") && (!sIP.empty()) && (sIP != "::1")))
185                         {
186                                 /* The [2] is required because we may write a sockaddr_in6 here, and sockaddr_in6 is larger than sockaddr, where sockaddr_in4 is not. */
187                                 sockaddr* s = new sockaddr[2];
188 #ifdef IPV6
189                                 if (v6)
190                                 {
191                                         in6_addr n;
192                                         if (inet_pton(AF_INET6, sIP.c_str(), &n) > 0)
193                                         {
194                                                 memcpy(&((sockaddr_in6*)s)->sin6_addr, &n, sizeof(sockaddr_in6));
195                                                 ((sockaddr_in6*)s)->sin6_port = 0;
196                                                 ((sockaddr_in6*)s)->sin6_family = AF_INET6;
197                                                 size = sizeof(sockaddr_in6);
198                                         }
199                                         else
200                                         {
201                                                 delete[] s;
202                                                 j++;
203                                                 continue;
204                                         }
205                                 }
206                                 else
207 #endif
208                                 {
209                                         in_addr n;
210                                         if (inet_aton(sIP.c_str(), &n) > 0)
211                                         {
212                                                 ((sockaddr_in*)s)->sin_addr = n;
213                                                 ((sockaddr_in*)s)->sin_port = 0;
214                                                 ((sockaddr_in*)s)->sin_family = AF_INET;
215                                         }
216                                         else
217                                         {
218                                                 delete[] s;
219                                                 j++;
220                                                 continue;
221                                         }
222                                 }
223
224                                 if (Instance->SE->Bind(this->fd, s, size) < 0)
225                                 {
226                                         this->state = I_ERROR;
227                                         this->OnError(I_ERR_BIND);
228                                         this->fd = -1;
229                                         delete[] s;
230                                         return false;
231                                 }
232
233                                 delete[] s;
234                                 return true;
235                         }
236                 }
237                 j++;
238         }
239         Instance->Logs->Log("SOCKET", DEBUG,"nothing in the config to bind()!");
240         return true;
241 }
242
243 bool BufferedSocket::DoConnect()
244 {
245         /* The [2] is required because we may write a sockaddr_in6 here, and sockaddr_in6 is larger than sockaddr, where sockaddr_in4 is not. */
246         sockaddr* addr = new sockaddr[2];
247         socklen_t size = sizeof(sockaddr_in);
248 #ifdef IPV6
249         bool v6 = false;
250         if ((!*this->host) || strchr(this->host, ':'))
251                 v6 = true;
252
253         if (v6)
254         {
255                 this->fd = socket(AF_INET6, SOCK_STREAM, 0);
256                 if ((this->fd > -1) && ((strstr(this->IP,"::ffff:") != (char*)&this->IP) && (strstr(this->IP,"::FFFF:") != (char*)&this->IP)))
257                 {
258                         if (!this->BindAddr(this->cbindip))
259                         {
260                                 delete[] addr;
261                                 return false;
262                         }
263                 }
264         }
265         else
266 #endif
267         {
268                 this->fd = socket(AF_INET, SOCK_STREAM, 0);
269                 if (this->fd > -1)
270                 {
271                         if (!this->BindAddr(this->cbindip))
272                         {
273                                 delete[] addr;
274                                 return false;
275                         }
276                 }
277         }
278
279         if (this->fd == -1)
280         {
281                 this->state = I_ERROR;
282                 this->OnError(I_ERR_SOCKET);
283                 delete[] addr;
284                 return false;
285         }
286
287 #ifdef IPV6
288         if (v6)
289         {
290                 in6_addr addy;
291                 if (inet_pton(AF_INET6, this->host, &addy) > 0)
292                 {
293                         ((sockaddr_in6*)addr)->sin6_family = AF_INET6;
294                         memcpy(&((sockaddr_in6*)addr)->sin6_addr, &addy, sizeof(addy));
295                         ((sockaddr_in6*)addr)->sin6_port = htons(this->port);
296                         size = sizeof(sockaddr_in6);
297                 }
298         }
299         else
300 #endif
301         {
302                 in_addr addy;
303                 if (inet_aton(this->host, &addy) > 0)
304                 {
305                         ((sockaddr_in*)addr)->sin_family = AF_INET;
306                         ((sockaddr_in*)addr)->sin_addr = addy;
307                         ((sockaddr_in*)addr)->sin_port = htons(this->port);
308                 }
309         }
310
311         Instance->SE->NonBlocking(this->fd);
312
313 #ifdef WIN32
314         /* UGH for the LOVE OF ZOMBIE JESUS SOMEONE FIX THIS!!!!!!!!!!! */
315         Instance->SE->Blocking(this->fd);
316 #endif
317
318         if (Instance->SE->Connect(this, (sockaddr*)addr, size) == -1)
319         {
320                 if (errno != EINPROGRESS)
321                 {
322                         this->OnError(I_ERR_CONNECT);
323                         this->Close();
324                         this->state = I_ERROR;
325                         return false;
326                 }
327
328                 this->Timeout = new SocketTimeout(this->GetFd(), this->Instance, this, timeout_val, this->Instance->Time());
329                 this->Instance->Timers->AddTimer(this->Timeout);
330         }
331 #ifdef WIN32
332         /* CRAQ SMOKING STUFF TO BE FIXED */
333         Instance->SE->NonBlocking(this->fd);
334 #endif
335         this->state = I_CONNECTING;
336         if (this->fd > -1)
337         {
338                 if (!this->Instance->SE->AddFd(this))
339                 {
340                         this->OnError(I_ERR_NOMOREFDS);
341                         this->Close();
342                         this->state = I_ERROR;
343                         return false;
344                 }
345                 this->SetQueues(this->fd);
346         }
347
348         Instance->Logs->Log("SOCKET", DEBUG,"BufferedSocket::DoConnect success");
349         return true;
350 }
351
352
353 void BufferedSocket::Close()
354 {
355         /* Save this, so we dont lose it,
356          * otherise on failure, error messages
357          * might be inaccurate.
358          */
359         int save = errno;
360         if (this->fd > -1)
361         {
362                 if (this->IsIOHooked && Instance->Config->GetIOHook(this))
363                 {
364                         try
365                         {
366                                 if (this->state != I_LISTENING)
367                                         Instance->Config->GetIOHook(this)->OnRawSocketClose(this->fd);
368                         }
369                         catch (CoreException& modexcept)
370                         {
371                                 Instance->Logs->Log("SOCKET", DEFAULT,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
372                         }
373                 }
374                 Instance->SE->Shutdown(this, 2);
375                 if (Instance->SE->Close(this) != -1)
376                         this->OnClose();
377
378                 if (Instance->SocketCull.find(this) == Instance->SocketCull.end())
379                         Instance->SocketCull[this] = this;
380         }
381         errno = save;
382 }
383
384 std::string BufferedSocket::GetIP()
385 {
386         return this->IP;
387 }
388
389 const char* BufferedSocket::Read()
390 {
391         if (!Instance->SE->BoundsCheckFd(this))
392                 return NULL;
393
394         int n = 0;
395         char *ReadBuffer = Instance->GetReadBuffer();
396
397         if (this->IsIOHooked)
398         {
399                 int result2 = 0;
400                 int MOD_RESULT = 0;
401                 try
402                 {
403                         MOD_RESULT = Instance->Config->GetIOHook(this)->OnRawSocketRead(this->fd, ReadBuffer, sizeof(ReadBuffer) - 1,result2);
404                 }
405                 catch (CoreException& modexcept)
406                 {
407                         Instance->Logs->Log("SOCKET", DEFAULT,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
408                 }
409                 if (MOD_RESULT < 0)
410                 {
411                         n = -1;
412                         errno = EAGAIN;
413                 }
414                 else
415                 {
416                         n = result2;
417                 }
418         }
419         else
420         {
421                 n = recv(this->fd, ReadBuffer, sizeof(ReadBuffer) - 1, 0);
422         }
423
424         /*
425          * This used to do some silly bounds checking instead of just passing bufsize - 1 to recv.
426          * Not only does that make absolutely no sense, but it could potentially result in a read buffer's worth
427          * of data being thrown into the bit bucket for no good reason, which is just *stupid*.. do things correctly now.
428          * --w00t (july 2, 2008)
429          */
430         if (n > 0)
431         {
432                 ReadBuffer[n] = 0;
433                 return ReadBuffer;
434         }
435         else
436         {
437                 int err = errno;
438                 if (err == EAGAIN)
439                         return "";
440                 else
441                         return NULL;
442         }
443 }
444
445 /*
446  * This function formerly tried to flush write buffer each call.
447  * While admirable in attempting to get the data out to wherever
448  * it is going, on a full socket, it's just going to syscall write() and
449  * EAGAIN constantly, instead of waiting in the SE to know if it can write
450  * which will chew a bit of CPU.
451  *
452  * So, now this function returns void (take note) and just adds to the sendq.
453  *
454  * It'll get written at a determinate point when the socketengine tells us it can write.
455  *              -- w00t (april 1, 2008)
456  */
457 void BufferedSocket::Write(const std::string &data)
458 {
459         /* Append the data to the back of the queue ready for writing */
460         outbuffer.push_back(data);
461
462         /* Mark ourselves as wanting write */
463         this->Instance->SE->WantWrite(this);
464 }
465
466 bool BufferedSocket::FlushWriteBuffer()
467 {
468         errno = 0;
469         if ((this->fd > -1) && (this->state == I_CONNECTED))
470         {
471                 if (this->IsIOHooked)
472                 {
473                         while (outbuffer.size() && (errno != EAGAIN))
474                         {
475                                 try
476                                 {
477                                         /* XXX: The lack of buffering here is NOT a bug, modules implementing this interface have to
478                                          * implement their own buffering mechanisms
479                                          */
480                                         Instance->Config->GetIOHook(this)->OnRawSocketWrite(this->fd, outbuffer[0].c_str(), outbuffer[0].length());
481                                         outbuffer.pop_front();
482                                 }
483                                 catch (CoreException& modexcept)
484                                 {
485                                         Instance->Logs->Log("SOCKET", DEBUG,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
486                                         return true;
487                                 }
488                         }
489                 }
490                 else
491                 {
492                         /* If we have multiple lines, try to send them all,
493                          * not just the first one -- Brain
494                          */
495                         while (outbuffer.size() && (errno != EAGAIN))
496                         {
497                                 /* Send a line */
498                                 int result = Instance->SE->Send(this, outbuffer[0].c_str(), outbuffer[0].length(), 0);
499
500                                 if (result > 0)
501                                 {
502                                         if ((unsigned int)result >= outbuffer[0].length())
503                                         {
504                                                 /* The whole block was written (usually a line)
505                                                  * Pop the block off the front of the queue,
506                                                  * dont set errno, because we are clear of errors
507                                                  * and want to try and write the next block too.
508                                                  */
509                                                 outbuffer.pop_front();
510                                         }
511                                         else
512                                         {
513                                                 std::string temp = outbuffer[0].substr(result);
514                                                 outbuffer[0] = temp;
515                                                 /* We didnt get the whole line out. arses.
516                                                  * Try again next time, i guess. Set errno,
517                                                  * because we shouldnt be writing any more now,
518                                                  * until the socketengine says its safe to do so.
519                                                  */
520                                                 errno = EAGAIN;
521                                         }
522                                 }
523                                 else if (result == 0)
524                                 {
525                                         this->Instance->SE->DelFd(this);
526                                         this->Close();
527                                         return true;
528                                 }
529                                 else if ((result == -1) && (errno != EAGAIN))
530                                 {
531                                         this->OnError(I_ERR_WRITE);
532                                         this->state = I_ERROR;
533                                         this->Instance->SE->DelFd(this);
534                                         this->Close();
535                                         return true;
536                                 }
537                         }
538                 }
539         }
540
541         if ((errno == EAGAIN) && (fd > -1))
542         {
543                 this->Instance->SE->WantWrite(this);
544         }
545
546         return (fd < 0);
547 }
548
549 void SocketTimeout::Tick(time_t)
550 {
551         ServerInstance->Logs->Log("SOCKET", DEBUG,"SocketTimeout::Tick");
552
553         if (ServerInstance->SE->GetRef(this->sfd) != this->sock)
554                 return;
555
556         if (this->sock->state == I_CONNECTING)
557         {
558                 // for non-listening sockets, the timeout can occur
559                 // which causes termination of the connection after
560                 // the given number of seconds without a successful
561                 // connection.
562                 this->sock->OnTimeout();
563                 this->sock->OnError(I_ERR_TIMEOUT);
564                 this->sock->timeout = true;
565
566                 /* NOTE: We must set this AFTER DelFd, as we added
567                  * this socket whilst writeable. This means that we
568                  * must DELETE the socket whilst writeable too!
569                  */
570                 this->sock->state = I_ERROR;
571
572                 if (ServerInstance->SocketCull.find(this->sock) == ServerInstance->SocketCull.end())
573                         ServerInstance->SocketCull[this->sock] = this->sock;
574         }
575
576         this->sock->Timeout = NULL;
577 }
578
579 bool BufferedSocket::Poll()
580 {
581         int incoming = -1;
582
583 #ifndef WINDOWS
584         if (!Instance->SE->BoundsCheckFd(this))
585                 return false;
586 #endif
587
588         if (Instance->SE->GetRef(this->fd) != this)
589                 return false;
590
591         switch (this->state)
592         {
593                 case I_CONNECTING:
594                         /* Our socket was in write-state, so delete it and re-add it
595                          * in read-state.
596                          */
597 #ifndef WINDOWS
598                         if (this->fd > -1)
599                         {
600                                 this->Instance->SE->DelFd(this);
601                                 if (!this->Instance->SE->AddFd(this))
602                                         return false;
603                         }
604 #endif
605                         this->SetState(I_CONNECTED);
606
607                         if (Instance->Config->GetIOHook(this))
608                         {
609                                 Instance->Logs->Log("SOCKET",DEBUG,"Hook for raw connect");
610                                 try
611                                 {
612                                         Instance->Config->GetIOHook(this)->OnRawSocketConnect(this->fd);
613                                 }
614                                 catch (CoreException& modexcept)
615                                 {
616                                         Instance->Logs->Log("SOCKET",DEBUG,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
617                                 }
618                         }
619                         return this->OnConnected();
620                 break;
621                 case I_LISTENING:
622                 {
623                         /* The [2] is required because we may write a sockaddr_in6 here, and sockaddr_in6 is larger than sockaddr, where sockaddr_in4 is not. */
624                         sockaddr* client = new sockaddr[2];
625                         length = sizeof (sockaddr_in);
626                         std::string recvip;
627 #ifdef IPV6
628                         if ((!*this->host) || strchr(this->host, ':'))
629                                 length = sizeof(sockaddr_in6);
630 #endif
631                         incoming = Instance->SE->Accept(this, client, &length);
632 #ifdef IPV6
633                         if ((!*this->host) || strchr(this->host, ':'))
634                         {
635                                 char buf[1024];
636                                 recvip = inet_ntop(AF_INET6, &((sockaddr_in6*)client)->sin6_addr, buf, sizeof(buf));
637                         }
638                         else
639 #endif
640                         {
641                                 // FIX: we were doing this for IPv6 connections as well, which was fucking recvip..
642                                 // Add brackets to make this a bit clearer. -- w00t (Jan 15, 2008)
643                                 recvip = inet_ntoa(((sockaddr_in*)client)->sin_addr);
644                         }
645
646                         Instance->SE->NonBlocking(incoming);
647
648                         this->OnIncomingConnection(incoming, (char*)recvip.c_str());
649
650                         if (this->IsIOHooked)
651                         {
652                                 try
653                                 {
654                                         Instance->Config->GetIOHook(this)->OnRawSocketAccept(incoming, recvip.c_str(), this->port);
655                                 }
656                                 catch (CoreException& modexcept)
657                                 {
658                                         Instance->Logs->Log("SOCKET",DEBUG,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
659                                 }
660                         }
661
662                         this->SetQueues(incoming);
663
664                         delete[] client;
665                         return true;
666                 }
667                 break;
668                 case I_CONNECTED:
669                         /* Process the read event */
670                         return this->OnDataReady();
671                 break;
672                 default:
673                 break;
674         }
675         return true;
676 }
677
678 void BufferedSocket::SetState(BufferedSocketState s)
679 {
680         this->state = s;
681 }
682
683 BufferedSocketState BufferedSocket::GetState()
684 {
685         return this->state;
686 }
687
688 int BufferedSocket::GetFd()
689 {
690         return this->fd;
691 }
692
693 bool BufferedSocket::OnConnected() { return true; }
694 void BufferedSocket::OnError(BufferedSocketError) { return; }
695 int BufferedSocket::OnDisconnect() { return 0; }
696 int BufferedSocket::OnIncomingConnection(int, char*) { return 0; }
697 bool BufferedSocket::OnDataReady() { return true; }
698 bool BufferedSocket::OnWriteReady() { return true; }
699 void BufferedSocket::OnTimeout() { return; }
700 void BufferedSocket::OnClose() { return; }
701
702 BufferedSocket::~BufferedSocket()
703 {
704         this->Close();
705         if (Timeout)
706         {
707                 Instance->Timers->DelTimer(Timeout);
708                 Timeout = NULL;
709         }
710 }
711
712 void BufferedSocket::HandleEvent(EventType et, int errornum)
713 {
714         switch (et)
715         {
716                 case EVENT_ERROR:
717                         switch (errornum)
718                         {
719                                 case ETIMEDOUT:
720                                         this->OnError(I_ERR_TIMEOUT);
721                                 break;
722                                 case ECONNREFUSED:
723                                 case 0:
724                                         this->OnError(this->state == I_CONNECTING ? I_ERR_CONNECT : I_ERR_WRITE);
725                                 break;
726                                 case EADDRINUSE:
727                                         this->OnError(I_ERR_BIND);
728                                 break;
729                                 case EPIPE:
730                                 case EIO:
731                                         this->OnError(I_ERR_WRITE);
732                                 break;
733                         }
734                         if (this->Instance->SocketCull.find(this) == this->Instance->SocketCull.end())
735                                 this->Instance->SocketCull[this] = this;
736                         return;
737                 break;
738                 case EVENT_READ:
739                         if (!this->Poll())
740                         {
741                                 if (this->Instance->SocketCull.find(this) == this->Instance->SocketCull.end())
742                                         this->Instance->SocketCull[this] = this;
743                                 return;
744                         }
745                 break;
746                 case EVENT_WRITE:
747                         if (this->WaitingForWriteEvent)
748                         {
749                                 this->WaitingForWriteEvent = false;
750                                 if (!this->OnWriteReady())
751                                 {
752                                         if (this->Instance->SocketCull.find(this) == this->Instance->SocketCull.end())
753                                                 this->Instance->SocketCull[this] = this;
754                                         return;
755                                 }
756                         }
757                         if (this->state == I_CONNECTING)
758                         {
759                                 /* This might look wrong as if we should be actually calling
760                                  * with EVENT_WRITE, but trust me it is correct. There are some
761                                  * writeability-state things in the read code, because of how
762                                  * BufferedSocket used to work regarding write buffering in previous
763                                  * versions of InspIRCd. - Brain
764                                  */
765                                 this->HandleEvent(EVENT_READ);
766                                 return;
767                         }
768                         else
769                         {
770                                 if (this->FlushWriteBuffer())
771                                 {
772                                         if (this->Instance->SocketCull.find(this) == this->Instance->SocketCull.end())
773                                                 this->Instance->SocketCull[this] = this;
774                                         return;
775                                 }
776                         }
777                 break;
778         }
779 }
780