]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/inspsocket.cpp
Add FD_WANT_SINGLE_WRITE to efficiently replace FD_WANT_POLL_WRITE
[user/henk/code/inspircd.git] / src / inspsocket.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "socket.h"
16 #include "inspstring.h"
17 #include "socketengine.h"
18
19 BufferedSocket::BufferedSocket()
20 {
21         Timeout = NULL;
22         state = I_ERROR;
23 }
24
25 BufferedSocket::BufferedSocket(int newfd)
26 {
27         Timeout = NULL;
28         this->fd = newfd;
29         this->state = I_CONNECTED;
30         if (fd > -1)
31                 ServerInstance->SE->AddFd(this, FD_WANT_FAST_READ | FD_WANT_EDGE_WRITE);
32 }
33
34 void BufferedSocket::DoConnect(const std::string &ipaddr, int aport, unsigned long maxtime, const std::string &connectbindip)
35 {
36         BufferedSocketError err = BeginConnect(ipaddr, aport, maxtime, connectbindip);
37         if (err != I_ERR_NONE)
38         {
39                 state = I_ERROR;
40                 SetError(strerror(errno));
41                 OnError(err);
42         }
43 }
44
45 BufferedSocketError BufferedSocket::BeginConnect(const std::string &ipaddr, int aport, unsigned long maxtime, const std::string &connectbindip)
46 {
47         irc::sockets::sockaddrs addr, bind;
48         if (!irc::sockets::aptosa(ipaddr.c_str(), aport, &addr))
49         {
50                 ServerInstance->Logs->Log("SOCKET", DEBUG, "BUG: Hostname passed to BufferedSocket, rather than an IP address!");
51                 return I_ERR_CONNECT;
52         }
53
54         bind.sa.sa_family = 0;
55         if (!connectbindip.empty())
56         {
57                 if (!irc::sockets::aptosa(connectbindip.c_str(), 0, &bind))
58                 {
59                         return I_ERR_BIND;
60                 }
61         }
62
63         return BeginConnect(addr, bind, maxtime);
64 }
65
66 static void IncreaseOSBuffers(int fd)
67 {
68         // attempt to increase socket sendq and recvq as high as its possible
69         int sendbuf = 32768;
70         int recvbuf = 32768;
71         setsockopt(fd,SOL_SOCKET,SO_SNDBUF,(const char *)&sendbuf,sizeof(sendbuf));
72         setsockopt(fd,SOL_SOCKET,SO_RCVBUF,(const char *)&recvbuf,sizeof(recvbuf));
73         // on failure, 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.
74 }
75
76 BufferedSocketError BufferedSocket::BeginConnect(const irc::sockets::sockaddrs& dest, const irc::sockets::sockaddrs& bind, unsigned long timeout)
77 {
78         if (fd < 0)
79                 fd = socket(dest.sa.sa_family, SOCK_STREAM, 0);
80
81         if (fd < 0)
82                 return I_ERR_SOCKET;
83
84         if (bind.sa.sa_family != 0)
85         {
86                 if (ServerInstance->SE->Bind(fd, &bind.sa, sa_size(bind)) < 0)
87                         return I_ERR_BIND;
88         }
89
90         ServerInstance->SE->NonBlocking(fd);
91
92         if (ServerInstance->SE->Connect(this, &dest.sa, sa_size(dest)) == -1)
93         {
94                 if (errno != EINPROGRESS)
95                         return I_ERR_CONNECT;
96         }
97
98         this->state = I_CONNECTING;
99
100         if (!ServerInstance->SE->AddFd(this, FD_WANT_NO_READ | FD_WANT_SINGLE_WRITE))
101                 return I_ERR_NOMOREFDS;
102
103         this->Timeout = new SocketTimeout(this->GetFd(), this, timeout, ServerInstance->Time());
104         ServerInstance->Timers->AddTimer(this->Timeout);
105
106         IncreaseOSBuffers(fd);
107
108         ServerInstance->Logs->Log("SOCKET", DEBUG,"BufferedSocket::DoConnect success");
109         return I_ERR_NONE;
110 }
111
112 void StreamSocket::Close()
113 {
114         /* Save this, so we dont lose it,
115          * otherise on failure, error messages
116          * might be inaccurate.
117          */
118         int save = errno;
119         if (this->fd > -1)
120         {
121                 if (IOHook)
122                 {
123                         try
124                         {
125                                 IOHook->OnStreamSocketClose(this);
126                         }
127                         catch (CoreException& modexcept)
128                         {
129                                 ServerInstance->Logs->Log("SOCKET", DEFAULT,"%s threw an exception: %s",
130                                         modexcept.GetSource(), modexcept.GetReason());
131                         }
132                 }
133                 ServerInstance->SE->Shutdown(this, 2);
134                 ServerInstance->SE->DelFd(this);
135                 ServerInstance->SE->Close(this);
136                 fd = -1;
137         }
138         errno = save;
139 }
140
141 void StreamSocket::cull()
142 {
143         Close();
144 }
145
146 bool StreamSocket::GetNextLine(std::string& line, char delim)
147 {
148         std::string::size_type i = recvq.find(delim);
149         if (i == std::string::npos)
150                 return false;
151         line = recvq.substr(0, i - 1);
152         // TODO is this the most efficient way to split?
153         recvq = recvq.substr(i + 1);
154         return true;
155 }
156
157 void StreamSocket::DoRead()
158 {
159         if (IOHook)
160         {
161                 int rv = -1;
162                 try
163                 {
164                         rv = IOHook->OnStreamSocketRead(this, recvq);
165                 }
166                 catch (CoreException& modexcept)
167                 {
168                         ServerInstance->Logs->Log("SOCKET", DEFAULT, "%s threw an exception: %s",
169                                 modexcept.GetSource(), modexcept.GetReason());
170                         return;
171                 }
172                 if (rv > 0)
173                         OnDataReady();
174                 if (rv < 0)
175                         SetError("Read Error"); // will not overwrite a better error message
176         }
177         else
178         {
179                 char* ReadBuffer = ServerInstance->GetReadBuffer();
180                 int n = recv(fd, ReadBuffer, ServerInstance->Config->NetBufferSize, 0);
181                 if (n == ServerInstance->Config->NetBufferSize)
182                 {
183                         ServerInstance->SE->ChangeEventMask(this, FD_WANT_FAST_READ | FD_ADD_TRIAL_READ);
184                         recvq.append(ReadBuffer, n);
185                         OnDataReady();
186                 }
187                 else if (n > 0)
188                 {
189                         ServerInstance->SE->ChangeEventMask(this, FD_WANT_FAST_READ);
190                         recvq.append(ReadBuffer, n);
191                         OnDataReady();
192                 }
193                 else if (n == 0)
194                 {
195                         error = "Connection closed";
196                         ServerInstance->SE->ChangeEventMask(this, FD_WANT_NO_READ | FD_WANT_NO_WRITE);
197                 }
198                 else if (errno == EAGAIN)
199                 {
200                         ServerInstance->SE->ChangeEventMask(this, FD_WANT_FAST_READ | FD_READ_WILL_BLOCK);
201                 }
202                 else if (errno == EINTR)
203                 {
204                         ServerInstance->SE->ChangeEventMask(this, FD_WANT_FAST_READ | FD_ADD_TRIAL_READ);
205                 }
206                 else
207                 {
208                         error = strerror(errno);
209                         ServerInstance->SE->ChangeEventMask(this, FD_WANT_NO_READ | FD_WANT_NO_WRITE);
210                 }
211         }
212 }
213
214 void StreamSocket::DoWrite()
215 {
216         if (sendq.empty())
217                 return;
218
219         if (IOHook)
220         {
221                 int rv = -1;
222                 try
223                 {
224                         if (sendq.size() > 1 && sendq[0].length() < 1024)
225                         {
226                                 // Avoid multiple repeated SSL encryption invocations
227                                 // This adds a single copy of the queue, but avoids
228                                 // much more overhead in terms of system calls invoked
229                                 // by the IOHook.
230                                 //
231                                 // The length limit of 1024 is to prevent merging strings
232                                 // more than once when writes begin to block.
233                                 std::string tmp;
234                                 tmp.reserve(sendq_len);
235                                 for(unsigned int i=0; i < sendq.size(); i++)
236                                         tmp.append(sendq[i]);
237                                 sendq.clear();
238                                 sendq.push_back(tmp);
239                         }
240                         while (!sendq.empty())
241                         {
242                                 std::string& front = sendq.front();
243                                 int itemlen = front.length();
244                                 rv = IOHook->OnStreamSocketWrite(this, front);
245                                 if (rv > 0)
246                                 {
247                                         // consumed the entire string, and is ready for more
248                                         sendq_len -= itemlen;
249                                         sendq.pop_front();
250                                 }
251                                 else if (rv == 0)
252                                 {
253                                         // socket has blocked. Stop trying to send data.
254                                         // IOHook has requested unblock notification from the socketengine
255
256                                         // Since it is possible that a partial write took place, adjust sendq_len
257                                         sendq_len = sendq_len - itemlen + front.length();
258                                         return;
259                                 }
260                                 else
261                                 {
262                                         SetError("Write Error"); // will not overwrite a better error message
263                                         return;
264                                 }
265                         }
266                 }
267                 catch (CoreException& modexcept)
268                 {
269                         ServerInstance->Logs->Log("SOCKET", DEBUG,"%s threw an exception: %s",
270                                 modexcept.GetSource(), modexcept.GetReason());
271                 }
272         }
273         else
274         {
275                 bool again = true;
276                 while (again)
277                 {
278                         again = false;
279
280                         // Prepare a writev() call to write all buffers efficiently
281                         int bufcount = sendq.size();
282                 
283                         // cap the number of buffers at IOV_MAX
284                         if (bufcount > IOV_MAX)
285                         {
286                                 bufcount = IOV_MAX;
287                                 again = true;
288                         }
289
290                         iovec* iovecs = new iovec[bufcount];
291                         for(int i=0; i < bufcount; i++)
292                         {
293                                 iovecs[i].iov_base = const_cast<char*>(sendq[i].data());
294                                 iovecs[i].iov_len = sendq[i].length();
295                         }
296                         int rv = writev(fd, iovecs, bufcount);
297                         delete[] iovecs;
298
299                         if (rv == (int)sendq_len)
300                         {
301                                 // it's our lucky day, everything got written out. Fast cleanup.
302                                 // This won't ever happen if the number of buffers got capped.
303                                 sendq_len = 0;
304                                 sendq.clear();
305                         }
306                         else if (rv > 0)
307                         {
308                                 // Partial write. Clean out strings from the sendq
309                                 sendq_len -= rv;
310                                 while (rv > 0 && !sendq.empty())
311                                 {
312                                         std::string& front = sendq.front();
313                                         if (front.length() < (size_t)rv)
314                                         {
315                                                 // this string got fully written out
316                                                 rv -= front.length();
317                                                 sendq.pop_front();
318                                         }
319                                         else
320                                         {
321                                                 // stopped in the middle of this string
322                                                 front = front.substr(rv);
323                                                 rv = 0;
324                                         }
325                                 }
326                         }
327                         else if (rv == 0)
328                         {
329                                 error = "Connection closed";
330                         }
331                         else if (errno == EAGAIN)
332                         {
333                                 again = false;
334                         }
335                         else if (errno == EINTR)
336                         {
337                                 again = true;
338                         }
339                         else
340                         {
341                                 error = strerror(errno);
342                         }
343                 }
344                 if (!error.empty())
345                 {
346                         // error - kill all events
347                         ServerInstance->SE->ChangeEventMask(this, FD_WANT_NO_READ | FD_WANT_NO_WRITE);
348                 }
349                 else if (sendq_len)
350                 {
351                         // writes have blocked, we can use FAST_WRITE to find when they unblock
352                         ServerInstance->SE->ChangeEventMask(this, FD_WANT_FAST_WRITE | FD_WRITE_WILL_BLOCK);
353                 }
354                 else
355                 {
356                         // writes are done, we can use EDGE_WRITE to stop asking for write
357                         ServerInstance->SE->ChangeEventMask(this, FD_WANT_EDGE_WRITE);
358                 }
359         }
360 }
361
362 void StreamSocket::WriteData(const std::string &data)
363 {
364         if (fd < 0)
365         {
366                 ServerInstance->Logs->Log("SOCKET", DEBUG, "Attempt to write data to dead socket: %s",
367                         data.c_str());
368                 return;
369         }
370
371         /* Append the data to the back of the queue ready for writing */
372         sendq.push_back(data);
373         sendq_len += data.length();
374
375         ServerInstance->SE->ChangeEventMask(this, FD_ADD_TRIAL_WRITE);
376 }
377
378 void SocketTimeout::Tick(time_t)
379 {
380         ServerInstance->Logs->Log("SOCKET", DEBUG,"SocketTimeout::Tick");
381
382         if (ServerInstance->SE->GetRef(this->sfd) != this->sock)
383                 return;
384
385         if (this->sock->state == I_CONNECTING)
386         {
387                 // for connecting sockets, the timeout can occur
388                 // which causes termination of the connection after
389                 // the given number of seconds without a successful
390                 // connection.
391                 this->sock->OnTimeout();
392                 this->sock->OnError(I_ERR_TIMEOUT);
393
394                 /* NOTE: We must set this AFTER DelFd, as we added
395                  * this socket whilst writeable. This means that we
396                  * must DELETE the socket whilst writeable too!
397                  */
398                 this->sock->state = I_ERROR;
399
400                 ServerInstance->GlobalCulls.AddItem(sock);
401         }
402
403         this->sock->Timeout = NULL;
404 }
405
406 void BufferedSocket::OnConnected() { }
407 void BufferedSocket::OnTimeout() { return; }
408
409 void BufferedSocket::DoWrite()
410 {
411         if (state == I_CONNECTING)
412         {
413                 state = I_CONNECTED;
414                 this->OnConnected();
415                 if (GetIOHook())
416                         GetIOHook()->OnStreamSocketConnect(this);
417                 else
418                         ServerInstance->SE->ChangeEventMask(this, FD_WANT_FAST_READ | FD_WANT_EDGE_WRITE);
419         }
420         this->StreamSocket::DoWrite();
421 }
422
423 BufferedSocket::~BufferedSocket()
424 {
425         this->Close();
426         if (Timeout)
427         {
428                 ServerInstance->Timers->DelTimer(Timeout);
429                 Timeout = NULL;
430         }
431 }
432
433 void StreamSocket::HandleEvent(EventType et, int errornum)
434 {
435         BufferedSocketError errcode = I_ERR_OTHER;
436         switch (et)
437         {
438                 case EVENT_ERROR:
439                 {
440                         SetError(strerror(errornum));
441                         switch (errornum)
442                         {
443                                 case ETIMEDOUT:
444                                         errcode = I_ERR_TIMEOUT;
445                                         break;
446                                 case ECONNREFUSED:
447                                 case 0:
448                                         errcode = I_ERR_CONNECT;
449                                         break;
450                                 case EADDRINUSE:
451                                         errcode = I_ERR_BIND;
452                                         break;
453                                 case EPIPE:
454                                 case EIO:
455                                         errcode = I_ERR_WRITE;
456                                         break;
457                         }
458                         break;
459                 }
460                 case EVENT_READ:
461                 {
462                         DoRead();
463                         break;
464                 }
465                 case EVENT_WRITE:
466                 {
467                         DoWrite();
468                         break;
469                 }
470         }
471         if (!error.empty())
472         {
473                 ServerInstance->Logs->Log("SOCKET", DEBUG, "Error on FD %d - '%s'", fd, error.c_str());
474                 OnError(errcode);
475                 ServerInstance->GlobalCulls.AddItem(this);
476         }
477 }
478