Author: Bleep <tomh@inxpress.net>
[ircu2.10.12-pk.git] / ircd / packet.c
index e4ef0dc9df71d787583674f817786ccdd9a3fafa..e167f7960703a524c2f61a2490da8ee666432eb5 100644 (file)
  * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id$
  */
-
-#include "sys.h"
-#include "h.h"
-#include "struct.h"
-#include "s_misc.h"
-#include "s_bsd.h"
+#include "packet.h"
+#include "client.h"
 #include "ircd.h"
-#include "msg.h"
+#include "ircd_chattr.h"
 #include "parse.h"
+#include "s_bsd.h"
+#include "s_misc.h"
 #include "send.h"
-#include "packet.h"
-#include "s_serv.h"
 
 #include <assert.h>
 
-RCSTAG_CC("$Id$");
+static void update_bytes_received(struct Client* cptr, size_t length)
+{
+  me.receiveB    += length;     /* Update bytes received */
+  cptr->receiveB += length;
+
+  if (cptr->receiveB > 1023) {
+    cptr->receiveK += (cptr->receiveB >> 10);
+    cptr->receiveB &= 0x03ff;   /* 2^10 = 1024, 3ff = 1023 */
+  }
+  if (me.receiveB > 1023) {
+    me.receiveK += (me.receiveB >> 10);
+    me.receiveB &= 0x03ff;
+  }
+}
+
+static void update_messages_received(struct Client* cptr)
+{
+  ++me.receiveM;
+  ++cptr->receiveM;
+}
 
 /*
  * dopacket
@@ -47,41 +64,22 @@ RCSTAG_CC("$Id$");
  *    with cptr of "local" variation, which contains all the
  *    necessary fields (buffer etc..)
  */
-int dopacket(aClient *cptr, char *buffer, int length)
+int server_dopacket(struct Client* cptr, const char* buffer, int length)
 {
-  Reg1 char *ch1;
-  Reg2 char *ch2;
-  register char *cptrbuf;
-  aClient *acpt = cptr->acpt;
+  const char* src;
+  char*       endp;
+  char*       client_buffer;
+  
+  assert(0 != cptr);
 
-  cptrbuf = cptr->buffer;
-  me.receiveB += length;       /* Update bytes received */
-  cptr->receiveB += length;
-  if (cptr->receiveB > 1023)
-  {
-    cptr->receiveK += (cptr->receiveB >> 10);
-    cptr->receiveB &= 0x03ff;  /* 2^10 = 1024, 3ff = 1023 */
-  }
-  if (acpt != &me)
-  {
-    acpt->receiveB += length;
-    if (acpt->receiveB > 1023)
-    {
-      acpt->receiveK += (acpt->receiveB >> 10);
-      acpt->receiveB &= 0x03ff;
-    }
-  }
-  else if (me.receiveB > 1023)
-  {
-    me.receiveK += (me.receiveB >> 10);
-    me.receiveB &= 0x03ff;
-  }
-  ch1 = cptrbuf + cptr->count;
-  ch2 = buffer;
-  while (--length >= 0)
-  {
-    register char g;
-    g = (*ch1 = *ch2++);
+  update_bytes_received(cptr, length);
+
+  client_buffer = cptr->buffer;
+  endp = client_buffer + cptr->count;
+  src = buffer;
+
+  while (length-- > 0) {
+    *endp = *src++;
     /*
      * Yuck.  Stuck.  To make sure we stay backward compatible,
      * we must assume that either CR or LF terminates the message
@@ -89,65 +87,45 @@ int dopacket(aClient *cptr, char *buffer, int length)
      * of messages, backward compatibility is lost and major
      * problems will arise. - Avalon
      */
-    if (g < '\16' && (g == '\n' || g == '\r'))
-    {
-      if (ch1 == cptrbuf)
-       continue;               /* Skip extra LF/CR's */
-      *ch1 = '\0';
-      me.receiveM += 1;                /* Update messages received */
-      cptr->receiveM += 1;
-      if (cptr->acpt != &me)
-       cptr->acpt->receiveM += 1;
-      if (IsServer(cptr))
-      {
-       if (parse_server(cptr, cptr->buffer, ch1) == CPTR_KILLED)
-         return CPTR_KILLED;
-      }
-      else if (parse_client(cptr, cptr->buffer, ch1) == CPTR_KILLED)
-       return CPTR_KILLED;
+    if (IsEol(*endp)) {
+      if (endp == client_buffer)
+        continue;               /* Skip extra LF/CR's */
+      *endp = '\0';
+
+      update_messages_received(cptr);
+
+      if (parse_server(cptr, cptr->buffer, endp) == CPTR_KILLED)
+        return CPTR_KILLED;
       /*
        *  Socket is dead so exit
        */
       if (IsDead(cptr))
-       return exit_client(cptr, cptr, &me, LastDeadComment(cptr));
-      ch1 = cptrbuf;
+        return exit_client(cptr, cptr, &me, LastDeadComment(cptr));
+      endp = client_buffer;
     }
-    else if (ch1 < cptrbuf + (sizeof(cptr->buffer) - 1))
-      ch1++;                   /* There is always room for the null */
+    else if (endp < client_buffer + BUFSIZE)
+      ++endp;                   /* There is always room for the null */
   }
-  cptr->count = ch1 - cptr->buffer;
-  return 0;
+  cptr->count = endp - cptr->buffer;
+  return 1;
 }
 
 /*
  * client_dopacket - handle client messages
  */
-int client_dopacket(aClient *cptr, size_t length)
+int client_dopacket(struct Client *cptr, size_t length)
 {
   assert(0 != cptr);
 
-  me.receiveB += length;       /* Update bytes received */
-  cptr->receiveB += length;
-
-  if (cptr->receiveB > 1023)
-  {
-    cptr->receiveK += (cptr->receiveB >> 10);
-    cptr->receiveB &= 0x03ff;  /* 2^10 = 1024, 3ff = 1023 */
-  }
-  if (me.receiveB > 1023)
-  {
-    me.receiveK += (me.receiveB >> 10);
-    me.receiveB &= 0x03ff;
-  }
-  cptr->count = 0;
-
-  ++me.receiveM;               /* Update messages received */
-  ++cptr->receiveM;
+  update_bytes_received(cptr, length);
+  update_messages_received(cptr);
 
   if (CPTR_KILLED == parse_client(cptr, cptr->buffer, cptr->buffer + length))
     return CPTR_KILLED;
   else if (IsDead(cptr))
     return exit_client(cptr, cptr, &me, LastDeadComment(cptr));
 
-  return 0;
+  return 1;
 }
+
+