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-8/src/packet.c (file contents), Revision 1243 by michael, Fri Sep 30 10:47:53 2011 UTC vs.
ircd-hybrid/trunk/src/packet.c (file contents), Revision 3113 by michael, Thu Mar 6 22:19:29 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"
30 > #include "conf.h"
31   #include "s_serv.h"
32   #include "client.h"
33   #include "ircd.h"
# Line 39 | Line 42
42  
43   #define READBUF_SIZE 16384
44  
42 struct Callback *iorecv_cb = NULL;
43 struct Callback *iorecvctrl_cb = NULL;
44
45   static char readBuf[READBUF_SIZE];
46   static void client_dopacket(struct Client *, char *, size_t);
47  
# Line 55 | Line 55 | static void client_dopacket(struct Clien
55   static int
56   extract_one_line(struct dbuf_queue *qptr, char *buffer)
57   {
58  struct dbuf_block *block;
58    int line_bytes = 0, empty_bytes = 0, phase = 0;
59 <  unsigned int idx;
60 <
62 <  char c;
63 <  dlink_node *ptr;
59 >  unsigned int idx = 0;
60 >  dlink_node *ptr = NULL;
61  
62    /*
63     * Phase 0: "empty" characters before the line
# Line 76 | Line 73 | extract_one_line(struct dbuf_queue *qptr
73     */
74    DLINK_FOREACH(ptr, qptr->blocks.head)
75    {
76 <    block = ptr->data;
76 >    struct dbuf_block *block = ptr->data;
77 >
78 >    if (ptr == qptr->blocks.head)
79 >      idx = qptr->pos;
80 >    else
81 >      idx = 0;
82  
83 <    for (idx = 0; idx < block->size; idx++)
83 >    for (; idx < block->size; ++idx)
84      {
85 <      c = block->data[idx];
85 >      char c = block->data[idx];
86 >
87        if (IsEol(c) || (c == ' ' && phase != 1))
88        {
89 <        empty_bytes++;
89 >        ++empty_bytes;
90          if (phase == 1)
91            phase = 2;
92        }
# Line 113 | 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   }
116
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 128 | Line 130 | parse_client_queued(struct Client *clien
130    {
131      int i = 0;
132  
133 <    for(;;)
133 >    for (; ;)
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 139 | 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 179 | 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 >    for (; ;)
185      {
186        if (IsDefunct(client_p))
187 <        break;
187 >        break;
188  
189        /* This flood protection works as follows:
190         *
# Line 202 | Line 204 | parse_client_queued(struct Client *clien
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         */
# Line 249 | Line 251 | flood_recalc(fde_t *fd, void *data)
251   {
252    struct Client *client_p = data;
253    struct LocalUser *lclient_p = client_p->localClient;
254 <
254 >
255    /* allow a bursting client their allocation per second, allow
256     * a client whos flooding an extra 2 per second
257     */
# Line 257 | Line 259 | flood_recalc(fde_t *fd, void *data)
259      lclient_p->sent_parsed -= 2;
260    else
261      lclient_p->sent_parsed = 0;
262 <  
262 >
263    if (lclient_p->sent_parsed < 0)
264      lclient_p->sent_parsed = 0;
265 <  
265 >
266    parse_client_queued(client_p);
267 <  
267 >
268    /* And now, try flushing .. */
269    if (!IsDead(client_p))
270    {
# Line 272 | Line 274 | flood_recalc(fde_t *fd, void *data)
274   }
275  
276   /*
275 * read_ctrl_packet - Read a 'packet' of data from a servlink control
276 *                    link and process it.
277 */
278 void
279 read_ctrl_packet(fde_t *fd, void *data)
280 {
281  struct Client *server = data;
282  struct LocalUser *lserver = server->localClient;
283  struct SlinkRpl *reply;
284  int length = 0;
285  unsigned char tmp[2];
286  unsigned char *len = tmp;
287  struct SlinkRplDef *replydef;
288
289  assert(lserver != NULL);
290    
291  reply = &lserver->slinkrpl;
292
293  if (IsDefunct(server))
294    return;
295
296  if (!reply->command)
297  {
298    reply->gotdatalen = 0;
299    reply->readdata = 0;
300    reply->data = NULL;
301
302    length = recv(fd->fd, tmp, 1, 0);
303
304    if (length <= 0)
305    {
306      if ((length == -1) && ignoreErrno(errno))
307        goto nodata;
308      dead_link_on_read(server, length);
309      return;
310    }
311    reply->command = tmp[0];
312  }
313
314  for (replydef = slinkrpltab; replydef->handler; replydef++)
315  {
316    if (replydef->replyid == (unsigned int)reply->command)
317      break;
318  }
319
320  /* we should be able to trust a local slink process...
321   * and if it sends an invalid command, that's a bug.. */
322  assert(replydef->handler);
323
324  if ((replydef->flags & SLINKRPL_FLAG_DATA) && (reply->gotdatalen < 2))
325  {
326    /* we need a datalen u16 which we don't have yet... */
327    length = recv(fd->fd, len, (2 - reply->gotdatalen), 0);
328    if (length <= 0)
329    {
330      if ((length == -1) && ignoreErrno(errno))
331        goto nodata;
332      dead_link_on_read(server, length);
333      return;
334    }
335
336    if (reply->gotdatalen == 0)
337    {
338      reply->datalen = *len << 8;
339      reply->gotdatalen++;
340      length--;
341      len++;
342    }
343    if (length && (reply->gotdatalen == 1))
344    {
345      reply->datalen |= *len;
346      reply->gotdatalen++;
347      if (reply->datalen > 0)
348        reply->data = MyMalloc(reply->datalen);
349    }
350
351    if (reply->gotdatalen < 2)
352      return; /* wait for more data */
353  }
354
355  if (reply->readdata < reply->datalen) /* try to get any remaining data */
356  {
357    length = recv(fd->fd, (reply->data + reply->readdata),
358                  (reply->datalen - reply->readdata), 0);
359    if (length <= 0)
360    {
361      if ((length == -1) && ignoreErrno(errno))
362        goto nodata;
363      dead_link_on_read(server, length);
364      return;
365    }
366
367    reply->readdata += length;
368    if (reply->readdata < reply->datalen)
369      return; /* wait for more data */
370  }
371
372  execute_callback(iorecvctrl_cb, server, reply->command);
373
374  /* we now have the command and any data, pass it off to the handler */
375  (*replydef->handler)(reply->command, reply->datalen, reply->data, server);
376
377  /* reset SlinkRpl */                      
378  if (reply->datalen > 0)
379    MyFree(reply->data);
380  reply->command = 0;
381
382  if (IsDead(server))
383    return;
384
385 nodata:
386  /* If we get here, we need to register for another COMM_SELECT_READ */
387  comm_setselect(fd, COMM_SELECT_READ, read_ctrl_packet, server, 0);
388 }
389
390 /*
391 * iorecv_default - append a packet to the recvq dbuf
392 */
393 void *
394 iorecv_default(va_list args)
395 {
396  struct Client *client_p = va_arg(args, struct Client *);
397  int length = va_arg(args, int);
398  char *buf = va_arg(args, char *);
399
400  dbuf_put(&client_p->localClient->buf_recvq, buf, length);
401  return NULL;
402 }
403
404 /*
277   * read_packet - Read a 'packet' of data from a connection and process it.
278   */
279   void
# Line 418 | Line 290 | read_packet(fde_t *fd, void *data)
290     * I personally think it makes the code too hairy to make sane.
291     *     -- adrian
292     */
293 <  do {
293 >  do
294 >  {
295   #ifdef HAVE_LIBCRYPTO
296      if (fd->ssl)
297      {
# Line 427 | Line 300 | read_packet(fde_t *fd, void *data)
300        /* translate openssl error codes, sigh */
301        if (length < 0)
302          switch (SSL_get_error(fd->ssl, length))
303 <        {
303 >        {
304            case SSL_ERROR_WANT_WRITE:
305 <            fd->flags.pending_read = 1;
306 <            SetSendqBlocked(client_p);
307 <            comm_setselect(fd, COMM_SELECT_WRITE, (PF *) sendq_unblocked,
308 <                           client_p, 0);
436 <            return;
437 <          case SSL_ERROR_WANT_READ:
438 <            errno = EWOULDBLOCK;
305 >            comm_setselect(fd, COMM_SELECT_WRITE, (PF *)sendq_unblocked, client_p, 0);
306 >            return;
307 >          case SSL_ERROR_WANT_READ:
308 >              errno = EWOULDBLOCK;
309            case SSL_ERROR_SYSCALL:
310 <            break;
310 >              break;
311            case SSL_ERROR_SSL:
312              if (errno == EAGAIN)
313                break;
314            default:
315 <            length = errno = 0;
316 <        }
315 >            length = errno = 0;
316 >        }
317      }
318      else
319   #endif
# Line 464 | Line 334 | read_packet(fde_t *fd, void *data)
334        return;
335      }
336  
337 <    execute_callback(iorecv_cb, client_p, length, readBuf);
337 >    dbuf_put(&client_p->localClient->buf_recvq, readBuf, length);
338  
339      if (client_p->localClient->lasttime < CurrentTime)
340        client_p->localClient->lasttime = CurrentTime;
# Line 479 | Line 349 | read_packet(fde_t *fd, void *data)
349        return;
350  
351      /* Check to make sure we're not flooding */
482    /* 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 && HasUMode(client_p, UMODE_OPER)))
357        {
# Line 516 | 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)