ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/packet.c
(Generate patch)

Comparing:
ircd-hybrid/src/packet.c (file contents), Revision 1028 by michael, Sun Nov 8 13:03:38 2009 UTC vs.
ircd-hybrid/trunk/src/packet.c (file contents), Revision 3453 by michael, Thu May 1 19:31:11 2014 UTC

# Line 1 | Line 1
1   /*
2 < *  ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3 < *  packet.c: Packet handlers.
2 > *  ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3   *
4 < *  Copyright (C) 2002 by the past and present ircd coders, and others.
4 > *  Copyright (c) 1997-2014 ircd-hybrid development team
5   *
6   *  This program is free software; you can redistribute it and/or modify
7   *  it under the terms of the GNU General Public License as published by
# Line 18 | Line 17
17   *  along with this program; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
19   *  USA
21 *
22 *  $Id$
20   */
21 +
22 + /*! \file packet.c
23 + * \brief Packet handlers.
24 + * \version $Id$
25 + */
26 +
27   #include "stdinc.h"
28   #include "list.h"
29   #include "s_bsd.h"
30 < #include "s_conf.h"
31 < #include "s_serv.h"
30 > #include "conf.h"
31 > #include "server.h"
32   #include "client.h"
30 #include "common.h"
33   #include "ircd.h"
34   #include "parse.h"
35   #include "fdlist.h"
36   #include "packet.h"
37   #include "irc_string.h"
38   #include "memory.h"
37 #include "hook.h"
39   #include "send.h"
40 < #include "irc_getnameinfo.h"
40 > #include "misc.h"
41  
42   #define READBUF_SIZE 16384
43  
43 struct Callback *iorecv_cb = NULL;
44 struct Callback *iorecvctrl_cb = NULL;
45
44   static char readBuf[READBUF_SIZE];
45   static void client_dopacket(struct Client *, char *, size_t);
46  
# Line 56 | Line 54 | static void client_dopacket(struct Clien
54   static int
55   extract_one_line(struct dbuf_queue *qptr, char *buffer)
56   {
59  struct dbuf_block *block;
57    int line_bytes = 0, empty_bytes = 0, phase = 0;
58 <  unsigned int idx;
59 <
63 <  char c;
64 <  dlink_node *ptr;
58 >  unsigned int idx = 0;
59 >  dlink_node *ptr = NULL;
60  
61    /*
62     * Phase 0: "empty" characters before the line
# Line 77 | Line 72 | extract_one_line(struct dbuf_queue *qptr
72     */
73    DLINK_FOREACH(ptr, qptr->blocks.head)
74    {
75 <    block = ptr->data;
75 >    struct dbuf_block *block = ptr->data;
76  
77 <    for (idx = 0; idx < block->size; idx++)
77 >    if (ptr == qptr->blocks.head)
78 >      idx = qptr->pos;
79 >    else
80 >      idx = 0;
81 >
82 >    for (; idx < block->size; ++idx)
83      {
84 <      c = block->data[idx];
84 >      char c = block->data[idx];
85 >
86        if (IsEol(c) || (c == ' ' && phase != 1))
87        {
88 <        empty_bytes++;
88 >        ++empty_bytes;
89 >
90          if (phase == 1)
91            phase = 2;
92        }
# Line 114 | Line 116 | extract_one_line(struct dbuf_queue *qptr
116    dbuf_delete(qptr, line_bytes + empty_bytes);
117    return IRCD_MIN(line_bytes, IRCD_BUFSIZE - 2);
118   }
117
119   /*
120   * parse_client_queued - parse client queued messages
121   */
122   static void
123   parse_client_queued(struct Client *client_p)
124 < {
124 > {
125    int dolen = 0;
126    int checkflood = 1;
127    struct LocalUser *lclient_p = client_p->localClient;
# Line 129 | Line 130 | parse_client_queued(struct Client *clien
130    {
131      int i = 0;
132  
133 <    for(;;)
133 >    while (1)
134      {
135        if (IsDefunct(client_p))
136 <        return;
136 >        return;
137  
138        /* rate unknown clients at MAX_FLOOD per loop */
139        if (i >= MAX_FLOOD)
# Line 140 | Line 141 | parse_client_queued(struct Client *clien
141  
142        dolen = extract_one_line(&lclient_p->buf_recvq, readBuf);
143        if (dolen == 0)
144 <        break;
144 >        break;
145  
146        client_dopacket(client_p, readBuf, dolen);
147        i++;
# Line 148 | Line 149 | parse_client_queued(struct Client *clien
149        /* if they've dropped out of the unknown state, break and move
150         * to the parsing for their appropriate status.  --fl
151         */
152 <      if(!IsUnknown(client_p))
152 >      if (!IsUnknown(client_p))
153          break;
154      }
155    }
# Line 159 | Line 160 | parse_client_queued(struct Client *clien
160      {
161        if (IsDefunct(client_p))
162          return;
163 <      if ((dolen = extract_one_line(&lclient_p->buf_recvq,
163 <                                    readBuf)) == 0)
163 >      if ((dolen = extract_one_line(&lclient_p->buf_recvq, readBuf)) == 0)
164          break;
165 +
166        client_dopacket(client_p, readBuf, dolen);
167      }
168    }
169    else if (IsClient(client_p))
170    {
171 <    if (ConfigFileEntry.no_oper_flood && (IsOper(client_p) || IsCanFlood(client_p)))
171 >    if (ConfigFileEntry.no_oper_flood && (HasUMode(client_p, UMODE_OPER) || IsCanFlood(client_p)))
172      {
173        if (ConfigFileEntry.true_no_oper_flood)
174          checkflood = -1;
# Line 180 | Line 181 | parse_client_queued(struct Client *clien
181       * messages in this loop, we simply drop out of the loop prematurely.
182       *   -- adrian
183       */
184 <    for (;;)
184 >    while (1)
185      {
186        if (IsDefunct(client_p))
187 <        break;
187 >        break;
188  
189        /* This flood protection works as follows:
190         *
# Line 200 | Line 201 | parse_client_queued(struct Client *clien
201         */
202        if (checkflood > 0)
203        {
204 <        if(lclient_p->sent_parsed >= lclient_p->allow_read)
204 >        if (lclient_p->sent_parsed >= lclient_p->allow_read)
205            break;
206        }
207 <      
207 >
208        /* allow opers 4 times the amount of messages as users. why 4?
209         * why not. :) --fl_
210         */
211 <      else if (lclient_p->sent_parsed >= (4 * lclient_p->allow_read) &&
211 <               checkflood != -1)
211 >      else if (lclient_p->sent_parsed >= (4 * lclient_p->allow_read) && checkflood != -1)
212          break;
213  
214        dolen = extract_one_line(&lclient_p->buf_recvq, readBuf);
# Line 250 | Line 250 | flood_recalc(fde_t *fd, void *data)
250   {
251    struct Client *client_p = data;
252    struct LocalUser *lclient_p = client_p->localClient;
253 <
253 >
254    /* allow a bursting client their allocation per second, allow
255     * a client whos flooding an extra 2 per second
256     */
# Line 258 | Line 258 | flood_recalc(fde_t *fd, void *data)
258      lclient_p->sent_parsed -= 2;
259    else
260      lclient_p->sent_parsed = 0;
261 <  
261 >
262    if (lclient_p->sent_parsed < 0)
263      lclient_p->sent_parsed = 0;
264 <  
264 >
265    parse_client_queued(client_p);
266 <  
266 >
267    /* And now, try flushing .. */
268    if (!IsDead(client_p))
269    {
# Line 273 | Line 273 | flood_recalc(fde_t *fd, void *data)
273   }
274  
275   /*
276 * read_ctrl_packet - Read a 'packet' of data from a servlink control
277 *                    link and process it.
278 */
279 void
280 read_ctrl_packet(fde_t *fd, void *data)
281 {
282  struct Client *server = data;
283  struct LocalUser *lserver = server->localClient;
284  struct SlinkRpl *reply;
285  int length = 0;
286  unsigned char tmp[2];
287  unsigned char *len = tmp;
288  struct SlinkRplDef *replydef;
289
290  assert(lserver != NULL);
291    
292  reply = &lserver->slinkrpl;
293
294  if (IsDefunct(server))
295    return;
296
297  if (!reply->command)
298  {
299    reply->gotdatalen = 0;
300    reply->readdata = 0;
301    reply->data = NULL;
302
303    length = recv(fd->fd, tmp, 1, 0);
304
305    if (length <= 0)
306    {
307      if ((length == -1) && ignoreErrno(errno))
308        goto nodata;
309      dead_link_on_read(server, length);
310      return;
311    }
312    reply->command = tmp[0];
313  }
314
315  for (replydef = slinkrpltab; replydef->handler; replydef++)
316  {
317    if (replydef->replyid == (unsigned int)reply->command)
318      break;
319  }
320
321  /* we should be able to trust a local slink process...
322   * and if it sends an invalid command, that's a bug.. */
323  assert(replydef->handler);
324
325  if ((replydef->flags & SLINKRPL_FLAG_DATA) && (reply->gotdatalen < 2))
326  {
327    /* we need a datalen u16 which we don't have yet... */
328    length = recv(fd->fd, len, (2 - reply->gotdatalen), 0);
329    if (length <= 0)
330    {
331      if ((length == -1) && ignoreErrno(errno))
332        goto nodata;
333      dead_link_on_read(server, length);
334      return;
335    }
336
337    if (reply->gotdatalen == 0)
338    {
339      reply->datalen = *len << 8;
340      reply->gotdatalen++;
341      length--;
342      len++;
343    }
344    if (length && (reply->gotdatalen == 1))
345    {
346      reply->datalen |= *len;
347      reply->gotdatalen++;
348      if (reply->datalen > 0)
349        reply->data = MyMalloc(reply->datalen);
350    }
351
352    if (reply->gotdatalen < 2)
353      return; /* wait for more data */
354  }
355
356  if (reply->readdata < reply->datalen) /* try to get any remaining data */
357  {
358    length = recv(fd->fd, (reply->data + reply->readdata),
359                  (reply->datalen - reply->readdata), 0);
360    if (length <= 0)
361    {
362      if ((length == -1) && ignoreErrno(errno))
363        goto nodata;
364      dead_link_on_read(server, length);
365      return;
366    }
367
368    reply->readdata += length;
369    if (reply->readdata < reply->datalen)
370      return; /* wait for more data */
371  }
372
373  execute_callback(iorecvctrl_cb, server, reply->command);
374
375  /* we now have the command and any data, pass it off to the handler */
376  (*replydef->handler)(reply->command, reply->datalen, reply->data, server);
377
378  /* reset SlinkRpl */                      
379  if (reply->datalen > 0)
380    MyFree(reply->data);
381  reply->command = 0;
382
383  if (IsDead(server))
384    return;
385
386 nodata:
387  /* If we get here, we need to register for another COMM_SELECT_READ */
388  comm_setselect(fd, COMM_SELECT_READ, read_ctrl_packet, server, 0);
389 }
390
391 /*
392 * iorecv_default - append a packet to the recvq dbuf
393 */
394 void *
395 iorecv_default(va_list args)
396 {
397  struct Client *client_p = va_arg(args, struct Client *);
398  int length = va_arg(args, int);
399  char *buf = va_arg(args, char *);
400
401  dbuf_put(&client_p->localClient->buf_recvq, buf, length);
402  return NULL;
403 }
404
405 /*
276   * read_packet - Read a 'packet' of data from a connection and process it.
277   */
278   void
# Line 419 | Line 289 | read_packet(fde_t *fd, void *data)
289     * I personally think it makes the code too hairy to make sane.
290     *     -- adrian
291     */
292 <  do {
292 >  do
293 >  {
294   #ifdef HAVE_LIBCRYPTO
295      if (fd->ssl)
296      {
297 <      length = SSL_read(fd->ssl, readBuf, READBUF_SIZE);
297 >      length = SSL_read(fd->ssl, readBuf, sizeof(readBuf));
298  
299        /* translate openssl error codes, sigh */
300        if (length < 0)
301          switch (SSL_get_error(fd->ssl, length))
302 <        {
302 >        {
303            case SSL_ERROR_WANT_WRITE:
304 <            fd->flags.pending_read = 1;
305 <            SetSendqBlocked(client_p);
306 <            comm_setselect(fd, COMM_SELECT_WRITE, (PF *) sendq_unblocked,
307 <                           client_p, 0);
437 <            return;
438 <          case SSL_ERROR_WANT_READ:
439 <            errno = EWOULDBLOCK;
304 >            comm_setselect(fd, COMM_SELECT_WRITE, (PF *)sendq_unblocked, client_p, 0);
305 >            return;
306 >          case SSL_ERROR_WANT_READ:
307 >              errno = EWOULDBLOCK;
308            case SSL_ERROR_SYSCALL:
309 <            break;
309 >              break;
310            case SSL_ERROR_SSL:
311              if (errno == EAGAIN)
312                break;
313            default:
314 <            length = errno = 0;
315 <        }
314 >            length = errno = 0;
315 >        }
316      }
317      else
318   #endif
319      {
320 <      length = recv(fd->fd, readBuf, READBUF_SIZE, 0);
320 >      length = recv(fd->fd, readBuf, sizeof(readBuf), 0);
321      }
322  
323      if (length <= 0)
# Line 465 | Line 333 | read_packet(fde_t *fd, void *data)
333        return;
334      }
335  
336 <    execute_callback(iorecv_cb, client_p, length, readBuf);
336 >    dbuf_put(&client_p->localClient->buf_recvq, readBuf, length);
337 >
338 >    if (client_p->localClient->lasttime < CurrentTime)
339 >      client_p->localClient->lasttime = CurrentTime;
340 >    if (client_p->localClient->lasttime > client_p->localClient->since)
341 >      client_p->localClient->since = CurrentTime;
342  
470    if (client_p->lasttime < CurrentTime)
471      client_p->lasttime = CurrentTime;
472    if (client_p->lasttime > client_p->since)
473      client_p->since = CurrentTime;
343      ClearPingSent(client_p);
344  
345      /* Attempt to parse what we have */
# Line 480 | Line 349 | read_packet(fde_t *fd, void *data)
349        return;
350  
351      /* Check to make sure we're not flooding */
483    /* TBD - ConfigFileEntry.client_flood should be a size_t */
352      if (!(IsServer(client_p) || IsHandshake(client_p) || IsConnecting(client_p))
353          && (dbuf_length(&client_p->localClient->buf_recvq) >
354 <            (unsigned int)ConfigFileEntry.client_flood))
354 >            get_recvq(&client_p->localClient->confs)))
355      {
356 <      if (!(ConfigFileEntry.no_oper_flood && IsOper(client_p)))
356 >      if (!(ConfigFileEntry.no_oper_flood && HasUMode(client_p, UMODE_OPER)))
357        {
358 <        exit_client(client_p, client_p, "Excess Flood");
358 >        exit_client(client_p, "Excess Flood");
359          return;
360        }
361      }
# Line 517 | Line 385 | read_packet(fde_t *fd, void *data)
385   static void
386   client_dopacket(struct Client *client_p, char *buffer, size_t length)
387   {
388 <  /*
388 >  /*
389     * Update messages received
390     */
391    ++me.localClient->recv.messages;
392    ++client_p->localClient->recv.messages;
393  
394 <  /*
394 >  /*
395     * Update bytes received
396     */
397    client_p->localClient->recv.bytes += length;

Diff Legend

Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)