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 32 by knight, Sun Oct 2 20:41:23 2005 UTC vs.
ircd-hybrid/trunk/src/packet.c (file contents), Revision 5421 by michael, Sun Jan 25 17:50:37 2015 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-2015 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 16 | Line 15
15   *
16   *  You should have received a copy of the GNU General Public License
17   *  along with this program; if not, write to the Free Software
18 < *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
18 > *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
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 "tools.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"
32 #include "list.h"
34   #include "parse.h"
35   #include "fdlist.h"
36   #include "packet.h"
37   #include "irc_string.h"
38   #include "memory.h"
38 #include "hook.h"
39   #include "send.h"
40 < #include "irc_getnameinfo.h"
40 > #include "misc.h"
41  
42   #define READBUF_SIZE 16384
43  
44 struct Callback *iorecv_cb = NULL;
45 struct Callback *iorecvctrl_cb = NULL;
46
44   static char readBuf[READBUF_SIZE];
45 < static void client_dopacket(struct Client *, char *, size_t);
45 >
46 >
47 > /*
48 > * client_dopacket - copy packet to client buf and parse it
49 > *      client_p - pointer to client structure for which the buffer data
50 > *             applies.
51 > *      buffer - pointr to the buffer containing the newly read data
52 > *      length - number of valid bytes of data in the buffer
53 > *
54 > * Note:
55 > *      It is implicitly assumed that client_dopacket() is called only
56 > *      with client_p of "local" variation, which contains all the
57 > *      necessary fields (buffer etc..)
58 > */
59 > static void
60 > client_dopacket(struct Client *client_p, char *buffer, size_t length)
61 > {
62 >  /* Update messages received */
63 >  ++me.connection->recv.messages;
64 >  ++client_p->connection->recv.messages;
65 >
66 >  /* Update bytes received */
67 >  client_p->connection->recv.bytes += length;
68 >  me.connection->recv.bytes += length;
69 >
70 >  parse(client_p, buffer, buffer + length);
71 > }
72  
73   /* extract_one_line()
74   *
# Line 57 | Line 80 | static void client_dopacket(struct Clien
80   static int
81   extract_one_line(struct dbuf_queue *qptr, char *buffer)
82   {
83 <  struct dbuf_block *block;
84 <  int line_bytes = 0, empty_bytes = 0, phase = 0;
62 <  unsigned int idx;
83 >  int line_bytes = 0, eol_bytes = 0;
84 >  dlink_node *node;
85  
86 <  char c;
65 <  dlink_node *ptr;
66 <
67 <  /*
68 <   * Phase 0: "empty" characters before the line
69 <   * Phase 1: copying the line
70 <   * Phase 2: "empty" characters after the line
71 <   *          (delete them as well and free some space in the dbuf)
72 <   *
73 <   * Empty characters are CR, LF and space (but, of course, not
74 <   * in the middle of a line). We try to remove as much of them as we can,
75 <   * since they simply eat server memory.
76 <   *
77 <   * --adx
78 <   */
79 <  DLINK_FOREACH(ptr, qptr->blocks.head)
86 >  DLINK_FOREACH(node, qptr->blocks.head)
87    {
88 <    block = ptr->data;
88 >    const struct dbuf_block *block = node->data;
89 >    unsigned int idx;
90 >
91 >    if (node == qptr->blocks.head)
92 >      idx = qptr->pos;
93 >    else
94 >      idx = 0;
95  
96 <    for (idx = 0; idx < block->size; idx++)
96 >    for (; idx < block->size; ++idx)
97      {
98 <      c = block->data[idx];
99 <      if (IsEol(c) || (c == ' ' && phase != 1))
100 <      {
88 <        empty_bytes++;
89 <        if (phase == 1)
90 <          phase = 2;
91 <      }
92 <      else switch (phase)
98 >      char c = block->data[idx];
99 >
100 >      if (IsEol(c))
101        {
102 <        case 0: phase = 1;
103 <        case 1: if (line_bytes++ < IRCD_BUFSIZE - 2)
104 <                  *buffer++ = c;
105 <                break;
106 <        case 2: *buffer = '\0';
99 <                dbuf_delete(qptr, line_bytes + empty_bytes);
100 <                return IRCD_MIN(line_bytes, IRCD_BUFSIZE - 2);
102 >        ++eol_bytes;
103 >
104 >        /* Allow 2 eol bytes per message */
105 >        if (eol_bytes == 2)
106 >          goto out;
107        }
108 +      else if (eol_bytes)
109 +        goto out;
110 +      else if (line_bytes++ < IRCD_BUFSIZE - 2)
111 +        *buffer++ = c;
112      }
113    }
114  
115 + out:
116 +
117    /*
118 <   * Now, if we haven't reached phase 2, ignore all line bytes
118 >   * Now, if we haven't found an EOL, ignore all line bytes
119     * that we have read, since this is a partial line case.
120     */
121 <  if (phase != 2)
110 <    line_bytes = 0;
111 <  else
121 >  if (eol_bytes)
122      *buffer = '\0';
123 +  else
124 +    line_bytes = 0;
125  
126    /* Remove what is now unnecessary */
127 <  dbuf_delete(qptr, line_bytes + empty_bytes);
127 >  dbuf_delete(qptr, line_bytes + eol_bytes);
128 >
129    return IRCD_MIN(line_bytes, IRCD_BUFSIZE - 2);
130   }
131  
# Line 121 | Line 134 | extract_one_line(struct dbuf_queue *qptr
134   */
135   static void
136   parse_client_queued(struct Client *client_p)
137 < {
137 > {
138    int dolen = 0;
126  int checkflood = 1;
127  struct LocalUser *lclient_p = client_p->localClient;
139  
140    if (IsUnknown(client_p))
141    {
142 <    int i = 0;
142 >    unsigned int i = 0;
143  
144 <    for(;;)
144 >    while (1)
145      {
146        if (IsDefunct(client_p))
147 <        return;
147 >        return;
148  
149 <      /* rate unknown clients at MAX_FLOOD per loop */
149 >      /* Rate unknown clients at MAX_FLOOD per loop */
150        if (i >= MAX_FLOOD)
151          break;
152  
153 <      dolen = extract_one_line(&lclient_p->buf_recvq, readBuf);
153 >      dolen = extract_one_line(&client_p->connection->buf_recvq, readBuf);
154 >
155        if (dolen == 0)
156 <        break;
156 >        break;
157  
158        client_dopacket(client_p, readBuf, dolen);
159 <      i++;
159 >      ++i;
160  
161 <      /* if they've dropped out of the unknown state, break and move
161 >      /*
162 >       * If they've dropped out of the unknown state, break and move
163         * to the parsing for their appropriate status.  --fl
164         */
165 <      if(!IsUnknown(client_p))
165 >      if (!IsUnknown(client_p))
166          break;
167      }
168    }
# Line 160 | Line 173 | parse_client_queued(struct Client *clien
173      {
174        if (IsDefunct(client_p))
175          return;
176 <      if ((dolen = extract_one_line(&lclient_p->buf_recvq,
177 <                                    readBuf)) == 0)
176 >
177 >      if ((dolen = extract_one_line(&client_p->connection->buf_recvq, readBuf)) == 0)
178          break;
179 +
180        client_dopacket(client_p, readBuf, dolen);
181      }
182    }
183 <  else if(IsClient(client_p))
183 >  else if (IsClient(client_p))
184    {
185 <    if (ConfigFileEntry.no_oper_flood && (IsOper(client_p) || IsCanFlood(client_p)))
186 <    {
187 <      if (ConfigFileEntry.true_no_oper_flood)
188 <        checkflood = -1;
189 <      else
190 <        checkflood = 0;
177 <    }
185 >    unsigned int checkflood = 1;
186 >
187 >    if (ConfigGeneral.no_oper_flood && HasUMode(client_p, UMODE_OPER))
188 >      checkflood = 0;
189 >    else if (IsCanFlood(client_p))
190 >      checkflood = 0;
191  
192      /*
193       * Handle flood protection here - if we exceed our flood limit on
194       * messages in this loop, we simply drop out of the loop prematurely.
195       *   -- adrian
196       */
197 <    for (;;)
197 >    while (1)
198      {
199        if (IsDefunct(client_p))
200 <        break;
200 >        break;
201  
202 <      /* This flood protection works as follows:
202 >      /*
203 >       * This flood protection works as follows:
204         *
205         * A client is given allow_read lines to send to the server.  Every
206         * time a line is parsed, sent_parsed is increased.  sent_parsed
# Line 199 | Line 213 | parse_client_queued(struct Client *clien
213         * as sent_parsed will always hover around the allow_read limit
214         * and no 'bursts' will be permitted.
215         */
216 <      if (checkflood > 0)
217 <      {
204 <        if(lclient_p->sent_parsed >= lclient_p->allow_read)
216 >      if (checkflood)
217 >        if (client_p->connection->sent_parsed >= client_p->connection->allow_read)
218            break;
206      }
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) &&
212               checkflood != -1)
213        break;
219  
220 <      dolen = extract_one_line(&lclient_p->buf_recvq, readBuf);
220 >      dolen = extract_one_line(&client_p->connection->buf_recvq, readBuf);
221 >
222        if (dolen == 0)
223          break;
224  
225        client_dopacket(client_p, readBuf, dolen);
226 <      lclient_p->sent_parsed++;
226 >      ++client_p->connection->sent_parsed;
227      }
228    }
229   }
# Line 232 | Line 238 | flood_endgrace(struct Client *client_p)
238    SetFloodDone(client_p);
239  
240    /* Drop their flood limit back down */
241 <  client_p->localClient->allow_read = MAX_FLOOD;
241 >  client_p->connection->allow_read = MAX_FLOOD;
242  
243 <  /* sent_parsed could be way over MAX_FLOOD but under MAX_FLOOD_BURST,
243 >  /*
244 >   * sent_parsed could be way over MAX_FLOOD but under MAX_FLOOD_BURST,
245     * so reset it.
246     */
247 <  client_p->localClient->sent_parsed = 0;
247 >  client_p->connection->sent_parsed = 0;
248   }
249  
250   /*
# Line 249 | Line 256 | flood_endgrace(struct Client *client_p)
256   void
257   flood_recalc(fde_t *fd, void *data)
258   {
259 <  struct Client *client_p = data;
260 <  struct LocalUser *lclient_p = client_p->localClient;
261 <
262 <  /* allow a bursting client their allocation per second, allow
259 >  struct Client *const client_p = data;
260 >
261 >  /*
262 >   * Allow a bursting client their allocation per second, allow
263     * a client whos flooding an extra 2 per second
264     */
265    if (IsFloodDone(client_p))
266 <    lclient_p->sent_parsed -= 2;
266 >    client_p->connection->sent_parsed -= 2;
267    else
268 <    lclient_p->sent_parsed = 0;
269 <  
270 <  if (lclient_p->sent_parsed < 0)
271 <    lclient_p->sent_parsed = 0;
272 <  
268 >    client_p->connection->sent_parsed = 0;
269 >
270 >  if (client_p->connection->sent_parsed < 0)
271 >    client_p->connection->sent_parsed = 0;
272 >
273    parse_client_queued(client_p);
274 <  
274 >
275    /* And now, try flushing .. */
276    if (!IsDead(client_p))
277    {
# Line 274 | Line 281 | flood_recalc(fde_t *fd, void *data)
281   }
282  
283   /*
277 * read_ctrl_packet - Read a 'packet' of data from a servlink control
278 *                    link and process it.
279 */
280 void
281 read_ctrl_packet(fde_t *fd, void *data)
282 {
283  struct Client *server = data;
284  struct LocalUser *lserver = server->localClient;
285  struct SlinkRpl *reply;
286  int length = 0;
287  unsigned char tmp[2];
288  unsigned char *len = tmp;
289  struct SlinkRplDef *replydef;
290
291  assert(lserver != NULL);
292    
293  reply = &lserver->slinkrpl;
294
295  if (IsDefunct(server))
296    return;
297
298  if (!reply->command)
299  {
300    reply->gotdatalen = 0;
301    reply->readdata = 0;
302    reply->data = NULL;
303
304    length = recv(fd->fd, tmp, 1, 0);
305
306    if (length <= 0)
307    {
308      if ((length == -1) && ignoreErrno(errno))
309        goto nodata;
310      dead_link_on_read(server, length);
311      return;
312    }
313    reply->command = tmp[0];
314  }
315
316  for (replydef = slinkrpltab; replydef->handler; replydef++)
317  {
318    if (replydef->replyid == (unsigned int)reply->command)
319      break;
320  }
321
322  /* we should be able to trust a local slink process...
323   * and if it sends an invalid command, that's a bug.. */
324  assert(replydef->handler);
325
326  if ((replydef->flags & SLINKRPL_FLAG_DATA) && (reply->gotdatalen < 2))
327  {
328    /* we need a datalen u16 which we don't have yet... */
329    length = recv(fd->fd, len, (2 - reply->gotdatalen), 0);
330    if (length <= 0)
331    {
332      if ((length == -1) && ignoreErrno(errno))
333        goto nodata;
334      dead_link_on_read(server, length);
335      return;
336    }
337
338    if (reply->gotdatalen == 0)
339    {
340      reply->datalen = *len << 8;
341      reply->gotdatalen++;
342      length--;
343      len++;
344    }
345    if (length && (reply->gotdatalen == 1))
346    {
347      reply->datalen |= *len;
348      reply->gotdatalen++;
349      if (reply->datalen > 0)
350        reply->data = MyMalloc(reply->datalen);
351    }
352
353    if (reply->gotdatalen < 2)
354      return; /* wait for more data */
355  }
356
357  if (reply->readdata < reply->datalen) /* try to get any remaining data */
358  {
359    length = recv(fd->fd, (reply->data + reply->readdata),
360                  (reply->datalen - reply->readdata), 0);
361    if (length <= 0)
362    {
363      if ((length == -1) && ignoreErrno(errno))
364        goto nodata;
365      dead_link_on_read(server, length);
366      return;
367    }
368
369    reply->readdata += length;
370    if (reply->readdata < reply->datalen)
371      return; /* wait for more data */
372  }
373
374  execute_callback(iorecvctrl_cb, server, reply->command);
375
376  /* we now have the command and any data, pass it off to the handler */
377  (*replydef->handler)(reply->command, reply->datalen, reply->data, server);
378
379  /* reset SlinkRpl */                      
380  if (reply->datalen > 0)
381    MyFree(reply->data);
382  reply->command = 0;
383
384  if (IsDead(server))
385    return;
386
387 nodata:
388  /* If we get here, we need to register for another COMM_SELECT_READ */
389  comm_setselect(fd, COMM_SELECT_READ, read_ctrl_packet, server, 0);
390 }
391
392 /*
284   * read_packet - Read a 'packet' of data from a connection and process it.
285   */
286   void
287   read_packet(fde_t *fd, void *data)
288   {
289 <  struct Client *client_p = data;
289 >  struct Client *const client_p = data;
290    int length = 0;
291  
292    if (IsDefunct(client_p))
# Line 406 | Line 297 | read_packet(fde_t *fd, void *data)
297     * I personally think it makes the code too hairy to make sane.
298     *     -- adrian
299     */
300 <  do {
300 >  do
301 >  {
302   #ifdef HAVE_LIBCRYPTO
303      if (fd->ssl)
304      {
305 <      length = SSL_read(fd->ssl, readBuf, READBUF_SIZE);
305 >      length = SSL_read(fd->ssl, readBuf, sizeof(readBuf));
306  
307        /* translate openssl error codes, sigh */
308        if (length < 0)
309          switch (SSL_get_error(fd->ssl, length))
310 <        {
310 >        {
311            case SSL_ERROR_WANT_WRITE:
312 <            fd->flags.pending_read = 1;
313 <            SetSendqBlocked(client_p);
314 <            comm_setselect(fd, COMM_SELECT_WRITE, (PF *) sendq_unblocked,
315 <                           client_p, 0);
424 <            return;
425 <          case SSL_ERROR_WANT_READ:
426 <            errno = EWOULDBLOCK;
312 >            comm_setselect(fd, COMM_SELECT_WRITE, sendq_unblocked, client_p, 0);
313 >            return;
314 >          case SSL_ERROR_WANT_READ:
315 >              errno = EWOULDBLOCK;
316            case SSL_ERROR_SYSCALL:
317 <            break;
317 >              break;
318 >          case SSL_ERROR_SSL:
319 >            if (errno == EAGAIN)
320 >              break;
321            default:
322 <            length = errno = 0;
323 <        }
322 >            length = errno = 0;
323 >        }
324      }
325      else
326   #endif
327      {
328 <      length = recv(fd->fd, readBuf, READBUF_SIZE, 0);
437 < #ifdef _WIN32
438 <      if (length < 0)
439 <        errno = WSAGetLastError();
440 < #endif
328 >      length = recv(fd->fd, readBuf, sizeof(readBuf), 0);
329      }
330  
331      if (length <= 0)
# Line 453 | Line 341 | read_packet(fde_t *fd, void *data)
341        return;
342      }
343  
344 <    execute_callback(iorecv_cb, client_p, length, readBuf);
344 >    dbuf_put(&client_p->connection->buf_recvq, readBuf, length);
345  
346 <    if (client_p->lasttime < CurrentTime)
347 <      client_p->lasttime = CurrentTime;
460 <    if (client_p->lasttime > client_p->since)
461 <      client_p->since = CurrentTime;
462 <    ClearPingSent(client_p);
346 >    if (client_p->connection->lasttime < CurrentTime)
347 >      client_p->connection->lasttime = CurrentTime;
348  
349 <    dbuf_put(&client_p->localClient->buf_recvq, readBuf, length);
349 >    if (client_p->connection->lasttime > client_p->connection->since)
350 >      client_p->connection->since = CurrentTime;
351 >
352 >    ClearPingSent(client_p);
353  
354      /* Attempt to parse what we have */
355      parse_client_queued(client_p);
# Line 470 | Line 358 | read_packet(fde_t *fd, void *data)
358        return;
359  
360      /* Check to make sure we're not flooding */
361 <    /* TBD - ConfigFileEntry.client_flood should be a size_t */
362 <    if (!(IsServer(client_p) || IsHandshake(client_p) || IsConnecting(client_p))
363 <        && (dbuf_length(&client_p->localClient->buf_recvq) >
476 <            (unsigned int)ConfigFileEntry.client_flood))
361 >    if (!(IsServer(client_p) || IsHandshake(client_p) || IsConnecting(client_p)) &&
362 >        (dbuf_length(&client_p->connection->buf_recvq) >
363 >         get_recvq(&client_p->connection->confs)))
364      {
365 <      if (!(ConfigFileEntry.no_oper_flood && IsOper(client_p)))
366 <      {
480 <        exit_client(client_p, client_p, "Excess Flood");
481 <        return;
482 <      }
365 >      exit_client(client_p, "Excess Flood");
366 >      return;
367      }
368    }
369   #ifdef HAVE_LIBCRYPTO
# Line 491 | Line 375 | read_packet(fde_t *fd, void *data)
375    /* If we get here, we need to register for another COMM_SELECT_READ */
376    comm_setselect(fd, COMM_SELECT_READ, read_packet, client_p, 0);
377   }
494
495 /*
496 * client_dopacket - copy packet to client buf and parse it
497 *      client_p - pointer to client structure for which the buffer data
498 *             applies.
499 *      buffer - pointr to the buffer containing the newly read data
500 *      length - number of valid bytes of data in the buffer
501 *
502 * Note:
503 *      It is implicitly assumed that dopacket is called only
504 *      with client_p of "local" variation, which contains all the
505 *      necessary fields (buffer etc..)
506 */
507 static void
508 client_dopacket(struct Client *client_p, char *buffer, size_t length)
509 {
510  /*
511   * Update messages received
512   */
513  ++me.localClient->recv.messages;
514  ++client_p->localClient->recv.messages;
515
516  /*
517   * Update bytes received
518   */
519  client_p->localClient->recv.bytes += length;
520  me.localClient->recv.bytes += length;
521
522  parse(client_p, buffer, buffer + length);
523 }

Comparing:
ircd-hybrid/src/packet.c (property svn:keywords), Revision 32 by knight, Sun Oct 2 20:41:23 2005 UTC vs.
ircd-hybrid/trunk/src/packet.c (property svn:keywords), Revision 5421 by michael, Sun Jan 25 17:50:37 2015 UTC

# Line 1 | Line 1
1 < Revision
1 > Id Revision

Diff Legend

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