]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/inspsocket.cpp
Improve speed of SSL sendq processing
[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);
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, true))
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 > 0)
182                 {
183                         recvq.append(ReadBuffer, n);
184                         OnDataReady();
185                 }
186                 else if (n == 0)
187                 {
188                         error = "Connection closed";
189                 }
190                 else if (errno != EAGAIN && errno != EINTR)
191                 {
192                         error = strerror(errno);
193                 }
194         }
195 }
196
197 void StreamSocket::DoWrite()
198 {
199         if (sendq.empty())
200                 return;
201
202         if (IOHook)
203         {
204                 int rv = -1;
205                 try
206                 {
207                         if (sendq.size() > 1 && sendq[0].length() < 1024)
208                         {
209                                 // Avoid multiple repeated SSL encryption invocations
210                                 // This adds a single copy of the queue, but avoids
211                                 // much more overhead in terms of system calls invoked
212                                 // by the IOHook.
213                                 //
214                                 // The length limit of 1024 is to prevent merging strings
215                                 // more than once when writes begin to block.
216                                 std::string tmp;
217                                 tmp.reserve(sendq_len);
218                                 for(unsigned int i=0; i < sendq.size(); i++)
219                                         tmp.append(sendq[i]);
220                                 sendq.clear();
221                                 sendq.push_back(tmp);
222                         }
223                         while (!sendq.empty())
224                         {
225                                 std::string& front = sendq.front();
226                                 int itemlen = front.length();
227                                 rv = IOHook->OnStreamSocketWrite(this, front);
228                                 if (rv > 0)
229                                 {
230                                         // consumed the entire string, and is ready for more
231                                         sendq_len -= itemlen;
232                                         sendq.pop_front();
233                                 }
234                                 else if (rv == 0)
235                                 {
236                                         // socket has blocked. Stop trying to send data.
237                                         // IOHook has requested unblock notification from the socketengine
238
239                                         // Since it is possible that a partial write took place, adjust sendq_len
240                                         sendq_len = sendq_len - itemlen + front.length();
241                                         return;
242                                 }
243                                 else
244                                 {
245                                         SetError("Write Error"); // will not overwrite a better error message
246                                         return;
247                                 }
248                         }
249                 }
250                 catch (CoreException& modexcept)
251                 {
252                         ServerInstance->Logs->Log("SOCKET", DEBUG,"%s threw an exception: %s",
253                                 modexcept.GetSource(), modexcept.GetReason());
254                 }
255         }
256         else
257         {
258                 // Prepare a writev() call to write all buffers efficiently
259                 int bufcount = sendq.size();
260                 
261                 // cap the number of buffers at IOV_MAX
262                 if (bufcount > IOV_MAX)
263                         bufcount = IOV_MAX;
264
265                 iovec* iovecs = new iovec[bufcount];
266                 for(int i=0; i < bufcount; i++)
267                 {
268                         iovecs[i].iov_base = const_cast<char*>(sendq[i].data());
269                         iovecs[i].iov_len = sendq[i].length();
270                 }
271                 int rv = writev(fd, iovecs, bufcount);
272                 delete[] iovecs;
273                 if (rv == (int)sendq_len)
274                 {
275                         // it's our lucky day, everything got written out. Fast cleanup.
276                         sendq_len = 0;
277                         sendq.clear();
278                 }
279                 else if (rv > 0)
280                 {
281                         // Partial write. Clean out strings from the sendq
282                         sendq_len -= rv;
283                         while (rv > 0 && !sendq.empty())
284                         {
285                                 std::string& front = sendq.front();
286                                 if (front.length() < (size_t)rv)
287                                 {
288                                         // this string got fully written out
289                                         rv -= front.length();
290                                         sendq.pop_front();
291                                 }
292                                 else
293                                 {
294                                         // stopped in the middle of this string
295                                         front = front.substr(rv);
296                                         rv = 0;
297                                 }
298                         }
299                 }
300                 else if (rv == 0)
301                 {
302                         error = "Connection closed";
303                 }
304                 else if (errno != EAGAIN && errno != EINTR)
305                 {
306                         error = strerror(errno);
307                 }
308                 if (sendq_len && error.empty())
309                         ServerInstance->SE->WantWrite(this);
310         }
311 }
312
313 void StreamSocket::WriteData(const std::string &data)
314 {
315         if (fd < 0)
316         {
317                 ServerInstance->Logs->Log("SOCKET", DEBUG, "Attempt to write data to dead socket: %s",
318                         data.c_str());
319                 return;
320         }
321         bool newWrite = sendq.empty() && !data.empty();
322
323         /* Append the data to the back of the queue ready for writing */
324         sendq.push_back(data);
325         sendq_len += data.length();
326
327         if (newWrite)
328         {
329                 // TODO perhaps we should try writing first, before asking SE about writes?
330                 // DoWrite();
331                 ServerInstance->SE->WantWrite(this);
332         }
333 }
334
335 void SocketTimeout::Tick(time_t)
336 {
337         ServerInstance->Logs->Log("SOCKET", DEBUG,"SocketTimeout::Tick");
338
339         if (ServerInstance->SE->GetRef(this->sfd) != this->sock)
340                 return;
341
342         if (this->sock->state == I_CONNECTING)
343         {
344                 // for connecting sockets, the timeout can occur
345                 // which causes termination of the connection after
346                 // the given number of seconds without a successful
347                 // connection.
348                 this->sock->OnTimeout();
349                 this->sock->OnError(I_ERR_TIMEOUT);
350
351                 /* NOTE: We must set this AFTER DelFd, as we added
352                  * this socket whilst writeable. This means that we
353                  * must DELETE the socket whilst writeable too!
354                  */
355                 this->sock->state = I_ERROR;
356
357                 ServerInstance->GlobalCulls.AddItem(sock);
358         }
359
360         this->sock->Timeout = NULL;
361 }
362
363 void BufferedSocket::OnConnected() { }
364 void BufferedSocket::OnTimeout() { return; }
365
366 void BufferedSocket::DoWrite()
367 {
368         if (state == I_CONNECTING)
369         {
370                 state = I_CONNECTED;
371                 this->OnConnected();
372                 if (GetIOHook())
373                         GetIOHook()->OnStreamSocketConnect(this);
374         }
375         this->StreamSocket::DoWrite();
376 }
377
378 BufferedSocket::~BufferedSocket()
379 {
380         this->Close();
381         if (Timeout)
382         {
383                 ServerInstance->Timers->DelTimer(Timeout);
384                 Timeout = NULL;
385         }
386 }
387
388 void StreamSocket::HandleEvent(EventType et, int errornum)
389 {
390         BufferedSocketError errcode = I_ERR_OTHER;
391         switch (et)
392         {
393                 case EVENT_ERROR:
394                 {
395                         SetError(strerror(errornum));
396                         switch (errornum)
397                         {
398                                 case ETIMEDOUT:
399                                         errcode = I_ERR_TIMEOUT;
400                                         break;
401                                 case ECONNREFUSED:
402                                 case 0:
403                                         errcode = I_ERR_CONNECT;
404                                         break;
405                                 case EADDRINUSE:
406                                         errcode = I_ERR_BIND;
407                                         break;
408                                 case EPIPE:
409                                 case EIO:
410                                         errcode = I_ERR_WRITE;
411                                         break;
412                         }
413                         break;
414                 }
415                 case EVENT_READ:
416                 {
417                         DoRead();
418                         break;
419                 }
420                 case EVENT_WRITE:
421                 {
422                         DoWrite();
423                         break;
424                 }
425         }
426         if (!error.empty())
427         {
428                 ServerInstance->Logs->Log("SOCKET", DEBUG, "Error on FD %d - '%s'", fd, error.c_str());
429                 OnError(errcode);
430                 ServerInstance->GlobalCulls.AddItem(this);
431         }
432 }
433