e4ef0dc9df71d787583674f817786ccdd9a3fafa
[ircu2.10.12-pk.git] / ircd / packet.c
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 #include "sys.h"
22 #include "h.h"
23 #include "struct.h"
24 #include "s_misc.h"
25 #include "s_bsd.h"
26 #include "ircd.h"
27 #include "msg.h"
28 #include "parse.h"
29 #include "send.h"
30 #include "packet.h"
31 #include "s_serv.h"
32
33 #include <assert.h>
34
35 RCSTAG_CC("$Id$");
36
37 /*
38  * dopacket
39  *
40  *    cptr - pointer to client structure for which the buffer data
41  *           applies.
42  *    buffer - pointer to the buffer containing the newly read data
43  *    length - number of valid bytes of data in the buffer
44  *
45  *  Note:
46  *    It is implicitly assumed that dopacket is called only
47  *    with cptr of "local" variation, which contains all the
48  *    necessary fields (buffer etc..)
49  */
50 int dopacket(aClient *cptr, char *buffer, int length)
51 {
52   Reg1 char *ch1;
53   Reg2 char *ch2;
54   register char *cptrbuf;
55   aClient *acpt = cptr->acpt;
56
57   cptrbuf = cptr->buffer;
58   me.receiveB += length;        /* Update bytes received */
59   cptr->receiveB += length;
60   if (cptr->receiveB > 1023)
61   {
62     cptr->receiveK += (cptr->receiveB >> 10);
63     cptr->receiveB &= 0x03ff;   /* 2^10 = 1024, 3ff = 1023 */
64   }
65   if (acpt != &me)
66   {
67     acpt->receiveB += length;
68     if (acpt->receiveB > 1023)
69     {
70       acpt->receiveK += (acpt->receiveB >> 10);
71       acpt->receiveB &= 0x03ff;
72     }
73   }
74   else if (me.receiveB > 1023)
75   {
76     me.receiveK += (me.receiveB >> 10);
77     me.receiveB &= 0x03ff;
78   }
79   ch1 = cptrbuf + cptr->count;
80   ch2 = buffer;
81   while (--length >= 0)
82   {
83     register char g;
84     g = (*ch1 = *ch2++);
85     /*
86      * Yuck.  Stuck.  To make sure we stay backward compatible,
87      * we must assume that either CR or LF terminates the message
88      * and not CR-LF.  By allowing CR or LF (alone) into the body
89      * of messages, backward compatibility is lost and major
90      * problems will arise. - Avalon
91      */
92     if (g < '\16' && (g == '\n' || g == '\r'))
93     {
94       if (ch1 == cptrbuf)
95         continue;               /* Skip extra LF/CR's */
96       *ch1 = '\0';
97       me.receiveM += 1;         /* Update messages received */
98       cptr->receiveM += 1;
99       if (cptr->acpt != &me)
100         cptr->acpt->receiveM += 1;
101       if (IsServer(cptr))
102       {
103         if (parse_server(cptr, cptr->buffer, ch1) == CPTR_KILLED)
104           return CPTR_KILLED;
105       }
106       else if (parse_client(cptr, cptr->buffer, ch1) == CPTR_KILLED)
107         return CPTR_KILLED;
108       /*
109        *  Socket is dead so exit
110        */
111       if (IsDead(cptr))
112         return exit_client(cptr, cptr, &me, LastDeadComment(cptr));
113       ch1 = cptrbuf;
114     }
115     else if (ch1 < cptrbuf + (sizeof(cptr->buffer) - 1))
116       ch1++;                    /* There is always room for the null */
117   }
118   cptr->count = ch1 - cptr->buffer;
119   return 0;
120 }
121
122 /*
123  * client_dopacket - handle client messages
124  */
125 int client_dopacket(aClient *cptr, size_t length)
126 {
127   assert(0 != cptr);
128
129   me.receiveB += length;        /* Update bytes received */
130   cptr->receiveB += length;
131
132   if (cptr->receiveB > 1023)
133   {
134     cptr->receiveK += (cptr->receiveB >> 10);
135     cptr->receiveB &= 0x03ff;   /* 2^10 = 1024, 3ff = 1023 */
136   }
137   if (me.receiveB > 1023)
138   {
139     me.receiveK += (me.receiveB >> 10);
140     me.receiveB &= 0x03ff;
141   }
142   cptr->count = 0;
143
144   ++me.receiveM;                /* Update messages received */
145   ++cptr->receiveM;
146
147   if (CPTR_KILLED == parse_client(cptr, cptr->buffer, cptr->buffer + length))
148     return CPTR_KILLED;
149   else if (IsDead(cptr))
150     return exit_client(cptr, cptr, &me, LastDeadComment(cptr));
151
152   return 0;
153 }