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