Filter:   InfoImg
download packet.c
Language: C
License: GPL
Copyright: (C) 2002 by the past and present ircd coders, and others.
LOC: 352
Project Info
NeoStats
Server: SourceForge
Type: cvs
...stats\neostats\NeoIRCd\src\
   .cvsignore
   .depend
   adns.c
   balloc.c
   channel.c
   channel_mode.c
   class.c
   client.c
   cloak.c
   crypt.c
   descrip.mms
   dynlink.c
   event.c
   fdlist.c
   fileio.c
   getopt.c
   hash.c
   hook.c
   hostmask.c
   irc_string.c
   ircd.c
   ircd_parser.y
   ircd_signal.c
   ircdauth.c
   kdparse.c
   linebuf.c
   list.c
   listener.c
   m_error.c
   Makefile.in
   match.c
   md5.c
   memory.c
   messages.tab
   modules.c
   motd.c
   numeric.c
   packet.c
   parse.c
   restart.c
   resv.c
   rsa.c
   s_auth.c
   s_bsd.c
   s_bsd_devpoll.c
   s_bsd_kqueue.c
   s_bsd_poll.c
   s_bsd_select.c
   s_bsd_sigio.c
   s_conf.c
   s_debug.c
   s_gline.c
   s_log.c
   s_misc.c
   s_serv.c
   s_stats.c
   s_user.c
   scache.c
   send.c
   snprintf.c
   sprintf_irc.c
   ssl.c
   tools.c
   version.c.SH
   whowas.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/*
 *  NeoIRCd: NeoStats Group. Based on Hybird7
 *  packet.c: Packet handlers.
 *
 *  Copyright (C) 2002 by the past and present ircd coders, and others.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 *  USA
 *
 *  $Id: packet.c,v 1.10 2003/03/06 14:01:50 fishwaldo Exp $
 */
#include "stdinc.h"
#include "tools.h"
#include "s_bsd.h"
#include "s_conf.h"
#include "s_serv.h"
#include "client.h"
#include "common.h"
#include "ircd.h"
#include "list.h"
#include "parse.h"
#include "fdlist.h"
#include "packet.h"
#include "irc_string.h"
#include "memory.h"
#include "hook.h"
#include "send.h"

static char               readBuf[READBUF_SIZE];
static void client_dopacket(struct Client *client_p, char *buffer, size_t length);


/*
 * parse_client_queued - parse client queued messages
 */
static void
parse_client_queued(struct Client *client_p)
{ 
  int dolen = 0;
  int checkflood = 1;
  struct LocalUser *lclient_p = client_p->localClient;

  if(IsUnknown(client_p))
  {
    int i = 0;

    for(;;)
    {
      if (IsClosing(client_p))
	return;
      if (client_p->localClient == NULL)
	return;

      /* rate unknown clients at MAX_FLOOD per loop */
      if(i >= MAX_FLOOD)
        break;

	dolen = linebuf_get(&client_p->localClient->buf_recvq, readBuf,
			    READBUF_SIZE, LINEBUF_COMPLETE, LINEBUF_PARSED);

	if(dolen <= 0)
	  break;
                          
      if(!IsDefunct(client_p))
      {
        client_dopacket(client_p, readBuf, dolen);
        i++;

        /* if they've dropped out of the unknown state, break and move
         * to the parsing for their appropriate status.  --fl
         */
        if(!IsUnknown(client_p))
          break;
      }
      else if(MyConnect(client_p))
      {
        linebuf_donebuf(&client_p->localClient->buf_recvq);
        linebuf_donebuf(&client_p->localClient->buf_sendq);
        return;
      }
    }
  }

  if (IsServer(client_p) || IsConnecting(client_p) || IsHandshake(client_p))
  {
    if(IsDefunct(client_p))
      return;
    if(client_p->localClient == NULL)
      return;
    
    while ((dolen = linebuf_get(&client_p->localClient->buf_recvq,
                              readBuf, READBUF_SIZE, LINEBUF_COMPLETE,
                              LINEBUF_PARSED)) > 0)
    {
      if (!IsDefunct(client_p))
        client_dopacket(client_p, readBuf, dolen);
      else if(MyConnect(client_p))
      {
        linebuf_donebuf(&client_p->localClient->buf_recvq);
        linebuf_donebuf(&client_p->localClient->buf_sendq);
        return;
      }
      if (client_p->localClient == NULL)
	return; 
    }
  } 
  else if(IsClient(client_p))
  {

    if (ConfigFileEntry.no_oper_flood && (IsOper(client_p) || IsCanFlood(client_p)))
    {
      if (ConfigFileEntry.true_no_oper_flood)
        checkflood = -1;
      else
        checkflood = 0;
    }

    /*
     * Handle flood protection here - if we exceed our flood limit on
     * messages in this loop, we simply drop out of the loop prematurely.
     *   -- adrian
     */
    for(;;)
    {
      if (IsDefunct(client_p))
	break;

      /* This flood protection works as follows:
       *
       * A client is given allow_read lines to send to the server.  Every
       * time a line is parsed, sent_parsed is increased.  sent_parsed
       * is decreased by 1 every time flood_recalc is called.
       *
       * Thus a client can 'burst' allow_read lines to the server, any
       * excess lines will be parsed one per flood_recalc() call.
       *
       * Therefore a client will be penalised more if they keep flooding,
       * as sent_parsed will always hover around the allow_read limit
       * and no 'bursts' will be permitted.
       */
      if(checkflood > 0)
      {
        if(lclient_p->sent_parsed >= lclient_p->allow_read)
          break;
      }
      
      /* allow opers 4 times the amount of messages as users. why 4?
       * why not. :) --fl_
       */
      else if(lclient_p->sent_parsed >= (4 * lclient_p->allow_read) && checkflood != -1)
        break;
       
      if(client_p->localClient == NULL)
	break;

      dolen = linebuf_get(&client_p->localClient->buf_recvq, readBuf,
                          READBUF_SIZE, LINEBUF_COMPLETE, LINEBUF_PARSED);
			 
      if (!dolen)
        break;
       
      client_dopacket(client_p, readBuf, dolen);
      lclient_p->sent_parsed++;
    }
  }
}

/* flood_endgrace()
 *
 * marks the end of the clients grace period
 */
void
flood_endgrace(struct Client *client_p)
{
  SetFloodDone(client_p);

  /* Drop their flood limit back down */
  client_p->localClient->allow_read = MAX_FLOOD;

  /* sent_parsed could be way over MAX_FLOOD but under MAX_FLOOD_BURST,
   * so reset it.
   */
  client_p->localClient->sent_parsed = 0;
}
	    
/*
 * flood_recalc
 *
 * recalculate the number of allowed flood lines. this should be called
 * once a second on any given client. We then attempt to flush some data.
 */
void
flood_recalc(int fd, void *data)
{
  struct Client *client_p = data;
  struct LocalUser *lclient_p = client_p->localClient;
 
  /* This can happen in the event that the client detached. */
  if (!lclient_p)
    return;

  /* allow a bursting client their allocation per second, allow
   * a client whos flooding an extra 2 per second
   */
  if(IsFloodDone(client_p))
    lclient_p->sent_parsed -= 2;
  else
    lclient_p->sent_parsed = 0;
  
  if(lclient_p->sent_parsed < 0)
    lclient_p->sent_parsed = 0;
  
  if(--lclient_p->actually_read < 0)
    lclient_p->actually_read = 0;

  parse_client_queued(client_p);
  
  /* And now, try flushing .. */
  if (!IsDead(client_p))
  {
    /* and finally, reset the flood check */
    comm_setflush(fd, 1000, flood_recalc, client_p);
  }
}

/*
 * read_ctrl_packet - Read a 'packet' of data from a servlink control
 *                    link and process it.
 */
void
read_ctrl_packet(int fd, void *data)
{
  struct Client *server = data;
  struct LocalUser *lserver = server->localClient;
  struct SlinkRpl *reply;
  int length = 0;
  unsigned char tmp[2];
  unsigned char *len = tmp;
  struct SlinkRplDef *replydef;
#ifndef NDEBUG
  struct hook_io_data hdata;
#endif
 
 
  assert(lserver != NULL);
    
  reply = &lserver->slinkrpl;

  if(IsDefunct(server))
  {
    return;
  }

  if (!reply->command)
  {
    reply->gotdatalen = 0;
    reply->readdata = 0;
    reply->data = NULL;

    length = recv(fd, tmp, 1, 0);

    if (length <= 0)
    {
      if((length == -1) && ignoreErrno(errno))
        goto nodata;
      dead_link_on_read(server, length);
      return;
    }
    reply->command = tmp[0];
  }

  for(replydef = slinkrpltab; replydef->handler; replydef++)
  {
    if (replydef->replyid == reply->command)
      break;
  }

  /* we should be able to trust a local slink process...
   * and if it sends an invalid command, that's a bug.. */
  assert(replydef->handler);

  if ((replydef->flags & SLINKRPL_FLAG_DATA) && (reply->gotdatalen < 2))
  {
    /* we need a datalen u16 which we don't have yet... */
    length = recv(fd, len, (2 - reply->gotdatalen), 0);
    if (length <= 0)
    {
      if((length == -1) && ignoreErrno(errno))
        goto nodata;
      dead_link_on_read(server, length);
      return;
    }

    if (reply->gotdatalen == 0)
    {
      reply->datalen = *len << 8;
      reply->gotdatalen++;
      length--;
      len++;
    }
    if (length && (reply->gotdatalen == 1))
    {
      reply->datalen |= *len;
      reply->gotdatalen++;
      if (reply->datalen > 0)
        reply->data = MyMalloc(reply->datalen);
    }

    if (reply->gotdatalen < 2)
      return; /* wait for more data */
  }

  if (reply->readdata < reply->datalen) /* try to get any remaining data */
  {
    length = recv(fd, (reply->data + reply->readdata),
                  (reply->datalen - reply->readdata), 0);
    if (length <= 0)
    {
      if((length == -1) && ignoreErrno(errno))
        goto nodata;
      dead_link_on_read(server, length);
      return;
    }

    reply->readdata += length;
    if (reply->readdata < reply->datalen)
      return; /* wait for more data */
  }

#ifndef NDEBUG
  hdata.connection = server;
  hdata.len = reply->command;
  hdata.data = NULL;
  hook_call_event("iorecvctrl", &hdata);
#endif
  
  /* we now have the command and any data, pass it off to the handler */
  (*replydef->handler)(reply->command, reply->datalen, reply->data, server);

  /* reset SlinkRpl */                      
  if (reply->datalen > 0)
    MyFree(reply->data);
  reply->command = 0;

  if (IsDead(server))
    return;

nodata:
  /* If we get here, we need to register for another COMM_SELECT_READ */
  comm_setselect(fd, FDLIST_SERVER, COMM_SELECT_READ,
                 read_ctrl_packet, server, 0);
}
  
/*
 * read_packet - Read a 'packet' of data from a connection and process it.
 */
void
read_packet(int fd, void *data)
{
  struct Client *client_p = data;
  struct LocalUser *lclient_p = client_p->localClient;
  int length = 0;
  int lbuf_len;
  int fd_r; 
  int binary = 0;
#ifndef NDEBUG
  struct hook_io_data hdata;
#endif
  if(IsDefunct(client_p))
    return;
  
  assert(lclient_p != NULL);
  fd_r = client_p->localClient->fd;

#ifndef HAVE_SOCKETPAIR
  if (HasServlink(client_p))
  {
    assert(client_p->localClient->fd_r > -1);
    fd_r = client_p->localClient->fd_r;
  }
#endif

  /*
   * Read some data. We *used to* do anti-flood protection here, but
   * I personally think it makes the code too hairy to make sane.
   *     -- adrian
   */
   
#ifdef USE_SSL
  if (IsSSL(client_p)) {
  	length = safe_SSL_read(client_p, readBuf, READBUF_SIZE);
  	if (!IsSSLOK(client_p) && length > 0) {
		SetSSLOK(client_p);
  		start_auth(client_p);
  	} else if (length == 0) {
		comm_setselect(fd_r, FDLIST_SERVICE, COMM_SELECT_READ, read_packet, client_p, 0);
  		return;
  	}	
  	
  } else
#endif
  length = recv(fd_r, readBuf, READBUF_SIZE, 0);

/* THIS WAS <= 0, should it stay? */
  if (length <= 0)
  {
    if((length == -1) && ignoreErrno(errno))
    {
      comm_setselect(fd_r, FDLIST_IDLECLIENT, COMM_SELECT_READ,
      		read_packet, client_p, 0);
      return;
    }  	
    dead_link_on_read(client_p, length);
    return;
  }

#ifndef NDEBUG
  hdata.connection = client_p;
  hdata.data = readBuf;
  hdata.len = length;
  hook_call_event("iorecv", &hdata);
#endif
  
  if (client_p->lasttime < CurrentTime)
    client_p->lasttime = CurrentTime;
  if (client_p->lasttime > client_p->since)
    client_p->since = CurrentTime;
  ClearPingSent(client_p);

  /*
   * Before we even think of parsing what we just read, stick
   * it on the end of the receive queue and do it when its
   * turn comes around.
   */
  if (IsHandshake(client_p) || IsUnknown(client_p))
    binary = 1;

  lbuf_len = linebuf_parse(&client_p->localClient->buf_recvq,
                           readBuf, length, binary);

  lclient_p->actually_read += lbuf_len;
  
  /* Attempt to parse what we have */

  parse_client_queued(client_p);

  /* Check to make sure we're not flooding */
  if (IsPerson(client_p) &&
      (linebuf_alloclen(&client_p->localClient->buf_recvq) >
       ConfigFileEntry.client_flood))
  {
    if (!(ConfigFileEntry.no_oper_flood && IsOper(client_p)))
    {
      exit_client(client_p, client_p, client_p, "Excess Flood");
      return;
    }
  }

  /* server fd may have changed */
  fd_r = client_p->localClient->fd;
#ifndef HAVE_SOCKETPAIR
  if (HasServlink(client_p))
  {
    assert(client_p->localClient->fd_r > -1);
    fd_r = client_p->localClient->fd_r;
  }
#endif

  
  if (!IsDefunct(client_p))
  {
    /* If we get here, we need to register for another COMM_SELECT_READ */
    if (PARSE_AS_SERVER(client_p))
    {
      comm_setselect(fd_r, FDLIST_SERVER, COMM_SELECT_READ,
		     read_packet, client_p, 0);
    }
    else
    {
      comm_setselect(fd_r, FDLIST_IDLECLIENT, COMM_SELECT_READ,
		     read_packet, client_p, 0);
    }
  }
}

/*
 * client_dopacket - copy packet to client buf and parse it
 *      client_p - pointer to client structure for which the buffer data
 *             applies.
 *      buffer - pointr to the buffer containing the newly read data
 *      length - number of valid bytes of data in the buffer
 *
 * Note:
 *      It is implicitly assumed that dopacket is called only
 *      with client_p of "local" variation, which contains all the
 *      necessary fields (buffer etc..)
 */
void
client_dopacket(struct Client *client_p, char *buffer, size_t length)
{
  assert(client_p != NULL);
  assert(buffer != NULL);

  if(client_p == NULL || buffer == NULL)
    return;
  /* 
   * Update messages received
   */
  ++me.localClient->receiveM;
  ++client_p->localClient->receiveM;

  /* 
   * Update bytes received
   */
  client_p->localClient->receiveB += length;

  if (client_p->localClient->receiveB > 1023)
  {
    client_p->localClient->receiveK += (client_p->localClient->receiveB >> 10);
    client_p->localClient->receiveB &= 0x03ff; /* 2^10 = 1024, 3ff = 1023 */
  }

  me.localClient->receiveB += length;

  if (me.localClient->receiveB > 1023)
    {
      me.localClient->receiveK += (me.localClient->receiveB >> 10);
      me.localClient->receiveB &= 0x03ff;
    }

  parse(client_p, buffer, buffer + length);
}