]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/inspsocket.cpp
2e8d6fd68411c4c62437a7f126ba64d2a77e542c
[user/henk/code/inspircd.git] / src / inspsocket.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 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 #include "socket.h"
15 #include "configreader.h"
16 #include "inspstring.h"
17 #include "socketengine.h"
18 #include "inspircd.h"
19
20 using irc::sockets::OpenTCPSocket;
21
22 bool InspSocket::Readable()
23 {
24         return ((this->state != I_CONNECTING) && (this->WaitingForWriteEvent == false));
25 }
26
27 InspSocket::InspSocket(InspIRCd* SI)
28 {
29         this->Timeout = NULL;
30         this->state = I_DISCONNECTED;
31         this->fd = -1;
32         this->WaitingForWriteEvent = false;
33         this->Instance = SI;
34         this->IsIOHooked = false;
35 }
36
37 InspSocket::InspSocket(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         this->IsIOHooked = false;
46         if (this->fd > -1)
47                 this->Instance->SE->AddFd(this);
48 }
49
50 InspSocket::InspSocket(InspIRCd* SI, const std::string &ipaddr, int aport, bool listening, unsigned long maxtime, const std::string &connectbindip)
51 {
52         this->cbindip = connectbindip;
53         this->fd = -1;
54         this->Instance = SI;
55         strlcpy(host,ipaddr.c_str(),MAXBUF);
56         this->WaitingForWriteEvent = false;
57         this->IsIOHooked = false;
58         this->Timeout = NULL;
59         if (listening)
60         {
61                 if ((this->fd = OpenTCPSocket(host)) == ERROR)
62                 {
63                         this->fd = -1;
64                         this->state = I_ERROR;
65                         this->OnError(I_ERR_SOCKET);
66                         return;
67                 }
68                 else
69                 {
70                         if (!SI->BindSocket(this->fd,aport,(char*)ipaddr.c_str()))
71                         {
72                                 this->Close();
73                                 this->fd = -1;
74                                 this->state = I_ERROR;
75                                 this->OnError(I_ERR_BIND);
76                                 this->ClosePending = true;
77                                 return;
78                         }
79                         else
80                         {
81                                 this->state = I_LISTENING;
82                                 this->port = aport;
83                                 if (this->fd > -1)
84                                 {
85                                         if (!this->Instance->SE->AddFd(this))
86                                         {
87                                                 this->Close();
88                                                 this->state = I_ERROR;
89                                                 this->OnError(I_ERR_NOMOREFDS);
90                                         }
91                                 }
92                                 return;
93                         }
94                 }
95         }
96         else
97         {
98                 strlcpy(this->host,ipaddr.c_str(),MAXBUF);
99                 this->port = aport;
100
101                 bool ipvalid = true;
102 #ifdef IPV6
103                 if (strchr(host,':'))
104                 {
105                         in6_addr n;
106                         if (inet_pton(AF_INET6, host, &n) < 1)
107                                 ipvalid = false;
108                 }
109                 else
110                 {
111                         in_addr n;
112                         if (inet_aton(host,&n) < 1)
113                                 ipvalid = false;
114                 }
115 #else
116                 in_addr n;
117                 if (inet_aton(host,&n) < 1)
118                         ipvalid = false;
119 #endif
120                 if (!ipvalid)
121                 {
122                         this->Instance->Log(DEBUG,"BUG: Hostname passed to InspSocket, rather than an IP address!");
123                         this->OnError(I_ERR_CONNECT);
124                         this->Close();
125                         this->fd = -1;
126                         this->state = I_ERROR;
127                         return;
128                 }
129                 else
130                 {
131                         strlcpy(this->IP,host,MAXBUF);
132                         timeout_val = maxtime;
133                         if (!this->DoConnect())
134                         {
135                                 this->OnError(I_ERR_CONNECT);
136                                 this->Close();
137                                 this->fd = -1;
138                                 this->state = I_ERROR;
139                                 return;
140                         }
141                 }
142         }
143 }
144
145 void InspSocket::WantWrite()
146 {
147         this->Instance->SE->WantWrite(this);
148         this->WaitingForWriteEvent = true;
149 }
150
151 void InspSocket::SetQueues(int nfd)
152 {
153         // attempt to increase socket sendq and recvq as high as its possible
154         int sendbuf = 32768;
155         int recvbuf = 32768;
156         setsockopt(nfd,SOL_SOCKET,SO_SNDBUF,(const char *)&sendbuf,sizeof(sendbuf));
157         setsockopt(nfd,SOL_SOCKET,SO_RCVBUF,(const char *)&recvbuf,sizeof(sendbuf));
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 InspSocket::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 IP = ip.empty() ? Conf.ReadValue("bind","address",j) : ip;
182                 if (!ip.empty() || Conf.ReadValue("bind","type",j) == "servers")
183                 {
184                         if (!ip.empty() || ((IP != "*") && (IP != "127.0.0.1") && (IP != "") && (IP != "::1")))
185                         {
186                                 sockaddr* s = new sockaddr[2];
187 #ifdef IPV6
188                                 if (v6)
189                                 {
190                                         in6_addr n;
191                                         if (inet_pton(AF_INET6, IP.c_str(), &n) > 0)
192                                         {
193                                                 memcpy(&((sockaddr_in6*)s)->sin6_addr, &n, sizeof(n));
194                                                 ((sockaddr_in6*)s)->sin6_port = 0;
195                                                 ((sockaddr_in6*)s)->sin6_family = AF_INET6;
196                                                 size = sizeof(sockaddr_in6);
197                                         }
198                                         else
199                                         {
200                                                 delete[] s;
201                                                 j++;
202                                                 continue;
203                                         }
204                                 }
205                                 else
206                                 {
207                                         in_addr n;
208                                         if (inet_aton(IP.c_str(), &n) > 0)
209                                         {
210                                                 ((sockaddr_in*)s)->sin_addr = n;
211                                                 ((sockaddr_in*)s)->sin_port = 0;
212                                                 ((sockaddr_in*)s)->sin_family = AF_INET;
213                                         }
214                                         else
215                                         {
216                                                 delete[] s;
217                                                 j++;
218                                                 continue;
219                                         }
220                                 }
221 #else
222                                 in_addr n;
223                                 if (insp_aton(IP.c_str(), &n) > 0)
224                                 {
225                                         ((sockaddr_in*)s)->sin_addr = n;
226                                         ((sockaddr_in*)s)->sin_port = 0;
227                                         ((sockaddr_in*)s)->sin_family = AF_INET;
228                                 }
229                                 else
230                                 {
231                                         delete[] s;
232                                         j++;
233                                         continue;
234                                 }
235 #endif
236
237                                 if (bind(this->fd, s, size) < 0)
238                                 {
239                                         this->state = I_ERROR;
240                                         this->OnError(I_ERR_BIND);
241                                         this->fd = -1;
242                                         delete[] s;
243                                         return false;
244                                 }
245
246                                 delete[] s;
247                                 return true;
248                         }
249                 }
250                 j++;
251         }
252         return true;
253 }
254
255 bool InspSocket::DoConnect()
256 {
257         sockaddr* addr = new sockaddr[2];
258         socklen_t size = sizeof(sockaddr_in);
259 #ifdef IPV6
260         bool v6 = false;
261         if ((!*this->host) || strchr(this->host, ':'))
262                 v6 = true;
263
264         if (v6)
265         {
266                 this->fd = socket(AF_INET6, SOCK_STREAM, 0);
267                 if ((this->fd > -1) && ((strstr(this->IP,"::ffff:") != (char*)&this->IP) && (strstr(this->IP,"::FFFF:") != (char*)&this->IP)))
268                 {
269                         if (!this->BindAddr(this->cbindip))
270                         {
271                                 delete[] addr;
272                                 return false;
273                         }
274                 }
275         }
276         else
277         {
278                 this->fd = socket(AF_INET, SOCK_STREAM, 0);
279                 if (this->fd > -1)
280                 {
281                         if (!this->BindAddr(this->cbindip))
282                         {
283                                 delete[] addr;
284                                 return false;
285                         }
286                 }
287         }
288 #else
289         this->fd = socket(AF_INET, SOCK_STREAM, 0);
290         if (this->fd > -1)
291         {
292                 if (!this->BindAddr(this->cbindip))
293                 {
294                         delete[] addr;
295                         return false;
296                 }
297         }
298 #endif
299
300         if (this->fd == -1)
301         {
302                 this->state = I_ERROR;
303                 this->OnError(I_ERR_SOCKET);
304                 delete[] addr;
305                 return false;
306         }
307
308 #ifdef IPV6
309         if (v6)
310         {
311                 in6_addr addy;
312                 if (inet_pton(AF_INET6, this->host, &addy) > 0)
313                 {
314                         ((sockaddr_in6*)addr)->sin6_family = AF_INET6;
315                         memcpy(&((sockaddr_in6*)addr)->sin6_addr, &addy, sizeof(addy));
316                         ((sockaddr_in6*)addr)->sin6_port = htons(this->port);
317                         size = sizeof(sockaddr_in6);
318                 }
319         }
320         else
321         {
322                 in_addr addy;
323                 if (inet_aton(this->host, &addy) > 0)
324                 {
325                         ((sockaddr_in*)addr)->sin_family = AF_INET;
326                         ((sockaddr_in*)addr)->sin_addr = addy;
327                         ((sockaddr_in*)addr)->sin_port = htons(this->port);
328                 }
329         }
330 #else
331         in_addr addy;
332         if (inet_aton(this->host, &addy) > 0)
333         {
334                 ((sockaddr_in*)addr)->sin_family = AF_INET;
335                 ((sockaddr_in*)addr)->sin_addr = addy;
336                 ((sockaddr_in*)addr)->sin_port = htons(this->port);
337         }
338 #endif
339 #ifndef WIN32
340         int flags = fcntl(this->fd, F_GETFL, 0);
341         fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
342 #else
343         unsigned long flags = 0;
344         ioctlsocket(this->fd, FIONBIO, &flags);
345 #endif
346         if (connect(this->fd, (sockaddr*)addr, size) == -1)
347         {
348                 if (errno != EINPROGRESS)
349                 {
350                         this->OnError(I_ERR_CONNECT);
351                         this->Close();
352                         this->state = I_ERROR;
353                         return false;
354                 }
355
356                 this->Timeout = new SocketTimeout(this->GetFd(), this->Instance, this, timeout_val, this->Instance->Time());
357                 this->Instance->Timers->AddTimer(this->Timeout);
358         }
359         this->state = I_CONNECTING;
360         if (this->fd > -1)
361         {
362                 if (!this->Instance->SE->AddFd(this))
363                 {
364                         this->OnError(I_ERR_NOMOREFDS);
365                         this->Close();
366                         this->state = I_ERROR;
367                         return false;
368                 }
369                 this->SetQueues(this->fd);
370         }
371         return true;
372 }
373
374
375 void InspSocket::Close()
376 {
377         /* Save this, so we dont lose it,
378          * otherise on failure, error messages
379          * might be inaccurate.
380          */
381         int save = errno;
382         if (this->fd > -1)
383         {
384                 if (this->IsIOHooked && Instance->Config->GetIOHook(this))
385                 {
386                         try
387                         {
388                                 Instance->Config->GetIOHook(this)->OnRawSocketClose(this->fd);
389                         }
390                         catch (CoreException& modexcept)
391                         {
392                                 Instance->Log(DEFAULT,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
393                         }
394                 }
395                 this->OnClose();
396                 shutdown(this->fd,2);
397                 close(this->fd);
398         }
399         errno = save;
400 }
401
402 std::string InspSocket::GetIP()
403 {
404         return this->IP;
405 }
406
407 char* InspSocket::Read()
408 {
409 #ifdef WINDOWS
410         if ((fd < 0) || (m_internalFd > MAX_DESCRIPTORS))
411 #else
412         if ((fd < 0) || (fd > MAX_DESCRIPTORS))
413 #endif
414                 return NULL;
415
416         int n = 0;
417
418         if (this->IsIOHooked)
419         {
420                 int result2 = 0;
421                 int MOD_RESULT = 0;
422                 try
423                 {
424                         MOD_RESULT = Instance->Config->GetIOHook(this)->OnRawSocketRead(this->fd,this->ibuf,sizeof(this->ibuf),result2);
425                 }
426                 catch (CoreException& modexcept)
427                 {
428                         Instance->Log(DEFAULT,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
429                 }
430                 if (MOD_RESULT < 0)
431                 {
432                         n = -1;
433                         errno = EAGAIN;
434                 }
435                 else
436                 {
437                         n = result2;
438                 }
439         }
440         else
441         {
442                 n = recv(this->fd,this->ibuf,sizeof(this->ibuf),0);
443         }
444
445         if ((n > 0) && (n <= (int)sizeof(this->ibuf)))
446         {
447                 ibuf[n] = 0;
448                 return ibuf;
449         }
450         else
451         {
452                 int err = errno;
453                 if (err == EAGAIN)
454                         return "";
455                 else
456                         return NULL;
457         }
458 }
459
460 void InspSocket::MarkAsClosed()
461 {
462 }
463
464 // There are two possible outcomes to this function.
465 // It will either write all of the data, or an undefined amount.
466 // If an undefined amount is written the connection has failed
467 // and should be aborted.
468 int InspSocket::Write(const std::string &data)
469 {
470         /* Try and append the data to the back of the queue, and send it on its way
471          */
472         outbuffer.push_back(data);
473         this->Instance->SE->WantWrite(this);
474         return (!this->FlushWriteBuffer());
475 }
476
477 bool InspSocket::FlushWriteBuffer()
478 {
479         errno = 0;
480         if ((this->fd > -1) && (this->state == I_CONNECTED))
481         {
482                 if (this->IsIOHooked)
483                 {
484                         while (outbuffer.size() && (errno != EAGAIN))
485                         {
486                                 try
487                                 {
488                                         int result = Instance->Config->GetIOHook(this)->OnRawSocketWrite(this->fd, outbuffer[0].c_str(), outbuffer[0].length());
489                                         if (result > 0)
490                                         {
491                                                 if ((unsigned int)result >= outbuffer[0].length())
492                                                 {
493                                                         outbuffer.pop_front();
494                                                 }
495                                                 else
496                                                 {
497                                                         std::string temp = outbuffer[0].substr(result);
498                                                         outbuffer[0] = temp;
499                                                         errno = EAGAIN;
500                                                 }
501                                         }
502                                         else if (((result == -1) && (errno != EAGAIN)) || (result == 0))
503                                         {
504                                                 this->OnError(I_ERR_WRITE);
505                                                 this->state = I_ERROR;
506                                                 this->Instance->SE->DelFd(this);
507                                                 this->Close();
508                                                 return true;
509                                         }
510                                 }
511                                 catch (CoreException& modexcept)
512                                 {
513                                         Instance->Log(DEBUG,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
514                                         return true;
515                                 }
516                         }
517                 }
518                 else
519                 {
520                         /* If we have multiple lines, try to send them all,
521                          * not just the first one -- Brain
522                          */
523                         while (outbuffer.size() && (errno != EAGAIN))
524                         {
525                                 /* Send a line */
526 #ifndef WIN32
527                                 int result = write(this->fd,outbuffer[0].c_str(),outbuffer[0].length());
528 #else
529                                 int result = send(this->fd,outbuffer[0].c_str(),outbuffer[0].length(), 0);
530 #endif
531                                 if (result > 0)
532                                 {
533                                         if ((unsigned int)result >= outbuffer[0].length())
534                                         {
535                                                 /* The whole block was written (usually a line)
536                                                  * Pop the block off the front of the queue,
537                                                  * dont set errno, because we are clear of errors
538                                                  * and want to try and write the next block too.
539                                                  */
540                                                 outbuffer.pop_front();
541                                         }
542                                         else
543                                         {
544                                                 std::string temp = outbuffer[0].substr(result);
545                                                 outbuffer[0] = temp;
546                                                 /* We didnt get the whole line out. arses.
547                                                  * Try again next time, i guess. Set errno,
548                                                  * because we shouldnt be writing any more now,
549                                                  * until the socketengine says its safe to do so.
550                                                  */
551                                                 errno = EAGAIN;
552                                         }
553                                 }
554                                 else if ((result == -1) && (errno != EAGAIN))
555                                 {
556                                         this->OnError(I_ERR_WRITE);
557                                         this->state = I_ERROR;
558                                         this->Instance->SE->DelFd(this);
559                                         this->Close();
560                                         return true;
561                                 }
562                         }
563                 }
564         }
565
566         if ((errno == EAGAIN) && (fd > -1))
567         {
568                 this->Instance->SE->WantWrite(this);
569         }
570
571         return (fd < 0);
572 }
573
574 void SocketTimeout::Tick(time_t now)
575 {
576         if (ServerInstance->SE->GetRef(this->sfd) != this->sock)
577                 return;
578
579         if (this->sock->state == I_CONNECTING)
580         {
581                 // for non-listening sockets, the timeout can occur
582                 // which causes termination of the connection after
583                 // the given number of seconds without a successful
584                 // connection.
585                 this->sock->OnTimeout();
586                 this->sock->OnError(I_ERR_TIMEOUT);
587                 this->sock->timeout = true;
588
589                 /* NOTE: We must set this AFTER DelFd, as we added
590                  * this socket whilst writeable. This means that we
591                  * must DELETE the socket whilst writeable too!
592                  */
593                 this->sock->state = I_ERROR;
594
595                 if (ServerInstance->SocketCull.find(this->sock) == ServerInstance->SocketCull.end())
596                         ServerInstance->SocketCull[this->sock] = this->sock;
597         }
598
599         this->sock->Timeout = NULL;
600 }
601
602 bool InspSocket::Poll()
603 {
604 #ifdef WINDOWS
605         if(Instance->SE->GetRef(this->fd) != this)
606                 return false;
607         int incoming = -1;
608 #else
609         if (this->Instance->SE->GetRef(this->fd) != this)
610                 return false;
611
612         int incoming = -1;
613
614         if ((fd < 0) || (fd > MAX_DESCRIPTORS))
615                 return false;
616 #endif
617         switch (this->state)
618         {
619                 case I_CONNECTING:
620                         /* Our socket was in write-state, so delete it and re-add it
621                          * in read-state.
622                          */
623 #ifndef WINDOWS
624                         if (this->fd > -1)
625                         {
626                                 this->Instance->SE->DelFd(this);
627                                 this->SetState(I_CONNECTED);
628                                 if (!this->Instance->SE->AddFd(this))
629                                         return false;
630                         }
631 #else
632                         this->SetState(I_CONNECTED);
633 #endif
634                         if (Instance->Config->GetIOHook(this))
635                         {
636                                 try
637                                 {
638                                         Instance->Config->GetIOHook(this)->OnRawSocketConnect(this->fd);
639                                 }
640                                 catch (CoreException& modexcept)
641                                 {
642                                         Instance->Log(DEBUG,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
643                                 }
644                         }
645                         return this->OnConnected();
646                 break;
647                 case I_LISTENING:
648                 {
649                         sockaddr* client = new sockaddr[2];
650                         length = sizeof (sockaddr_in);
651                         std::string recvip;
652 #ifdef IPV6
653                         if ((!*this->host) || strchr(this->host, ':'))
654                                 length = sizeof(sockaddr_in6);
655 #endif
656                         incoming = _accept (this->fd, client, &length);
657 #ifdef IPV6
658                         if ((!*this->host) || strchr(this->host, ':'))
659                         {
660                                 char buf[1024];
661                                 recvip = inet_ntop(AF_INET6, &((sockaddr_in6*)client)->sin6_addr, buf, sizeof(buf));
662                         }
663                         else
664                         {
665                                 recvip = inet_ntoa(((sockaddr_in*)client)->sin_addr);
666                         }
667 #else
668                         recvip = inet_ntoa(((sockaddr_in*)client)->sin_addr);
669 #endif
670                         this->OnIncomingConnection(incoming, (char*)recvip.c_str());
671
672                         if (this->IsIOHooked)
673                         {
674                                 try
675                                 {
676                                         Instance->Config->GetIOHook(this)->OnRawSocketAccept(incoming, recvip.c_str(), this->port);
677                                 }
678                                 catch (CoreException& modexcept)
679                                 {
680                                         Instance->Log(DEBUG,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
681                                 }
682                         }
683
684                         this->SetQueues(incoming);
685
686                         delete[] client;
687                         return true;
688                 }
689                 break;
690                 case I_CONNECTED:
691                         /* Process the read event */
692                         return this->OnDataReady();
693                 break;
694                 default:
695                 break;
696         }
697         return true;
698 }
699
700 void InspSocket::SetState(InspSocketState s)
701 {
702         this->state = s;
703 }
704
705 InspSocketState InspSocket::GetState()
706 {
707         return this->state;
708 }
709
710 int InspSocket::GetFd()
711 {
712         return this->fd;
713 }
714
715 bool InspSocket::OnConnected() { return true; }
716 void InspSocket::OnError(InspSocketError e) { return; }
717 int InspSocket::OnDisconnect() { return 0; }
718 int InspSocket::OnIncomingConnection(int newfd, char* ip) { return 0; }
719 bool InspSocket::OnDataReady() { return true; }
720 bool InspSocket::OnWriteReady() { return true; }
721 void InspSocket::OnTimeout() { return; }
722 void InspSocket::OnClose() { return; }
723
724 InspSocket::~InspSocket()
725 {
726         this->Close();
727         if (Timeout)
728         {
729                 Instance->Timers->DelTimer(Timeout);
730                 Timeout = NULL;
731         }
732 }
733
734 void InspSocket::HandleEvent(EventType et, int errornum)
735 {
736         switch (et)
737         {
738                 case EVENT_ERROR:
739                         switch (errornum)
740                         {
741                                 case ETIMEDOUT:
742                                         this->OnError(I_ERR_TIMEOUT);
743                                 break;
744                                 case ECONNREFUSED:
745                                 case 0:
746                                         this->OnError(this->state == I_CONNECTING ? I_ERR_CONNECT : I_ERR_WRITE);
747                                 break;
748                                 case EADDRINUSE:
749                                         this->OnError(I_ERR_BIND);
750                                 break;
751                                 case EPIPE:
752                                 case EIO:
753                                         this->OnError(I_ERR_WRITE);
754                                 break;
755                         }
756                         if (this->Instance->SocketCull.find(this) == this->Instance->SocketCull.end())
757                                 this->Instance->SocketCull[this] = this;
758                         return;
759                 break;
760                 case EVENT_READ:
761                         if (!this->Poll())
762                         {
763                                 if (this->Instance->SocketCull.find(this) == this->Instance->SocketCull.end())
764                                         this->Instance->SocketCull[this] = this;
765                                 return;
766                         }
767                 break;
768                 case EVENT_WRITE:
769                         if (this->WaitingForWriteEvent)
770                         {
771                                 this->WaitingForWriteEvent = false;
772                                 if (!this->OnWriteReady())
773                                 {
774                                         if (this->Instance->SocketCull.find(this) == this->Instance->SocketCull.end())
775                                                 this->Instance->SocketCull[this] = this;
776                                         return;
777                                 }
778                         }
779                         if (this->state == I_CONNECTING)
780                         {
781                                 /* This might look wrong as if we should be actually calling
782                                  * with EVENT_WRITE, but trust me it is correct. There are some
783                                  * writeability-state things in the read code, because of how
784                                  * InspSocket used to work regarding write buffering in previous
785                                  * versions of InspIRCd. - Brain
786                                  */
787                                 this->HandleEvent(EVENT_READ);
788                                 return;
789                         }
790                         else
791                         {
792                                 if (this->FlushWriteBuffer())
793                                 {
794                                         if (this->Instance->SocketCull.find(this) == this->Instance->SocketCull.end())
795                                                 this->Instance->SocketCull[this] = this;
796                                         return;
797                                 }
798                         }
799                 break;
800         }
801 }
802