home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / ircd4652.zip / ircd-df-4.6.5-os2 / src / packet.c < prev    next >
C/C++ Source or Header  |  1997-12-28  |  4KB  |  145 lines

  1. /************************************************************************
  2.  *   IRC - Internet Relay Chat, common/packet.c
  3.  *   Copyright (C) 1990  Jarkko Oikarinen and
  4.  *                       University of Oulu, Computing Center
  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
  8.  *   the Free Software Foundation; either version 1, or (at your option)
  9.  *   any later version.
  10.  *
  11.  *   This program is distributed in the hope that it will be useful,
  12.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *   GNU General Public License for more details.
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. #ifndef lint
  22. static  char sccsid[] = "@(#)packet.c    2.12 1/30/94 (C) 1988 University of Oulu, \
  23. Computing Center and Jarkko Oikarinen";
  24. #endif
  25.  
  26. #include "struct.h"
  27. #include "common.h"
  28. #include "sys.h"
  29. #include "msg.h"
  30. #include "h.h"
  31.  
  32. /*
  33.  * inittoken
  34.  * Cheat here, blah. Build the lookup tables from msgtab's,
  35.  * call them msgmap's. Called in main() with other inits.
  36.  * Yes, I know this is not the right module, but I said I cheat ;)
  37.  */
  38. void    inittoken(void)
  39. {
  40.     Reg1    int    loopy;
  41.     Reg2    int    final;
  42.  
  43.     /* Find the zero-entry */
  44.     for (final = 0; msgtab[final].cmd; final++)
  45.         ;
  46.     /* Point all entries to it */
  47.     for (loopy = 0; loopy<256; loopy++)
  48.         msgmap[loopy] = &msgtab[final];
  49.     /* Build references to existing commands */
  50.     for (loopy = 0; msgtab[loopy].cmd; loopy++)
  51.         msgmap[msgtab[loopy].token[0]] = &msgtab[loopy];
  52. }
  53.  
  54. /*
  55. ** dopacket
  56. **    cptr - pointer to client structure for which the buffer data
  57. **           applies.
  58. **    buffer - pointr to the buffer containing the newly read data
  59. **    length - number of valid bytes of data in the buffer
  60. **
  61. ** Note:
  62. **    It is implicitly assumed that dopacket is called only
  63. **    with cptr of "local" variation, which contains all the
  64. **    necessary fields (buffer etc..)
  65. */
  66. int    dopacket(cptr, buffer, length)
  67. Reg3    aClient *cptr;
  68. char    *buffer;
  69. Reg4    int    length;
  70. {
  71.     register char    *ch1;
  72.     register char    *ch2;    
  73.     aClient    *acpt = cptr->acpt;
  74.  
  75.     me.receiveB += length; /* Update bytes received */
  76.     cptr->receiveB += length;
  77.     if (cptr->receiveB > 1023)
  78.         {
  79.         cptr->receiveK += (cptr->receiveB >> 10);
  80.         cptr->receiveB &= 0x03ff;    /* 2^10 = 1024, 3ff = 1023 */
  81.         }
  82.     if (acpt != &me)
  83.         {
  84.         acpt->receiveB += length;
  85.         if (acpt->receiveB > 1023)
  86.             {
  87.             acpt->receiveK += (acpt->receiveB >> 10);
  88.             acpt->receiveB &= 0x03ff;
  89.             }
  90.         }
  91.     else if (me.receiveB > 1023)
  92.         {
  93.         me.receiveK += (me.receiveB >> 10);
  94.         me.receiveB &= 0x03ff;
  95.         }
  96.     ch1 = cptr->buffer + cptr->count;
  97.     ch2 = buffer;
  98.     while (--length >= 0)
  99.         {
  100.         register char g=(*ch1 = *ch2++);
  101.         /*
  102.          * Yuck.  Stuck.  To make sure we stay backward compatible,
  103.          * we must assume that either CR or LF terminates the message
  104.          * and not CR-LF.  By allowing CR or LF (alone) into the body
  105.          * of messages, backward compatibility is lost and major
  106.          * problems will arise. - Avalon
  107.          */
  108.         if (g<'\16' && (g == '\n' || g == '\r'))
  109.             {
  110.             if (ch1 == cptr->buffer)
  111.                 continue; /* Skip extra LF/CR's */
  112.             *ch1 = '\0';
  113.             me.receiveM += 1; /* Update messages received */
  114.             cptr->receiveM += 1;
  115.             if (cptr->acpt != &me)
  116.                 cptr->acpt->receiveM += 1;
  117.             cptr->count = 0; /* ...just in case parse returns with
  118.                      ** FLUSH_BUFFER without removing the
  119.                      ** structure pointed by cptr... --msa
  120.                      */
  121.             if (parse(cptr, cptr->buffer, ch1, msgtab) ==
  122.                 FLUSH_BUFFER)
  123.                 /*
  124.                 ** FLUSH_BUFFER means actually that cptr
  125.                 ** structure *does* not exist anymore!!! --msa
  126.                 */
  127.                 return FLUSH_BUFFER;
  128. #ifndef CLIENT_COMPILE
  129.             /*
  130.             ** Socket is dead so exit (which always returns with
  131.             ** FLUSH_BUFFER here).  - avalon
  132.             */
  133.             if (cptr->flags & FLAGS_DEADSOCKET)
  134.                 return exit_client(cptr, cptr, &me,
  135.                            "Dead Socket");
  136. #endif
  137.             ch1 = cptr->buffer;
  138.             }
  139.         else if (ch1 < cptr->buffer + (sizeof(cptr->buffer)-1))
  140.             ch1++; /* There is always room for the null */
  141.         }
  142.     cptr->count = ch1 - cptr->buffer;
  143.     return 0;
  144. }
  145.