Author: Bleep <tomh@inxpress.net>
[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  * $Id$
21  */
22 #include "packet.h"
23 #include "client.h"
24 #include "ircd.h"
25 #include "ircd_chattr.h"
26 #include "parse.h"
27 #include "s_bsd.h"
28 #include "s_misc.h"
29 #include "send.h"
30
31 #include <assert.h>
32
33 static void update_bytes_received(struct Client* cptr, size_t length)
34 {
35   me.receiveB    += length;     /* Update bytes received */
36   cptr->receiveB += length;
37
38   if (cptr->receiveB > 1023) {
39     cptr->receiveK += (cptr->receiveB >> 10);
40     cptr->receiveB &= 0x03ff;   /* 2^10 = 1024, 3ff = 1023 */
41   }
42   if (me.receiveB > 1023) {
43     me.receiveK += (me.receiveB >> 10);
44     me.receiveB &= 0x03ff;
45   }
46 }
47
48 static void update_messages_received(struct Client* cptr)
49 {
50   ++me.receiveM;
51   ++cptr->receiveM;
52 }
53
54 /*
55  * dopacket
56  *
57  *    cptr - pointer to client structure for which the buffer data
58  *           applies.
59  *    buffer - pointer to the buffer containing the newly read data
60  *    length - number of valid bytes of data in the buffer
61  *
62  *  Note:
63  *    It is implicitly assumed that dopacket is called only
64  *    with cptr of "local" variation, which contains all the
65  *    necessary fields (buffer etc..)
66  */
67 int server_dopacket(struct Client* cptr, const char* buffer, int length)
68 {
69   const char* src;
70   char*       endp;
71   char*       client_buffer;
72   
73   assert(0 != cptr);
74
75   update_bytes_received(cptr, length);
76
77   client_buffer = cptr->buffer;
78   endp = client_buffer + cptr->count;
79   src = buffer;
80
81   while (length-- > 0) {
82     *endp = *src++;
83     /*
84      * Yuck.  Stuck.  To make sure we stay backward compatible,
85      * we must assume that either CR or LF terminates the message
86      * and not CR-LF.  By allowing CR or LF (alone) into the body
87      * of messages, backward compatibility is lost and major
88      * problems will arise. - Avalon
89      */
90     if (IsEol(*endp)) {
91       if (endp == client_buffer)
92         continue;               /* Skip extra LF/CR's */
93       *endp = '\0';
94
95       update_messages_received(cptr);
96
97       if (parse_server(cptr, cptr->buffer, endp) == CPTR_KILLED)
98         return CPTR_KILLED;
99       /*
100        *  Socket is dead so exit
101        */
102       if (IsDead(cptr))
103         return exit_client(cptr, cptr, &me, LastDeadComment(cptr));
104       endp = client_buffer;
105     }
106     else if (endp < client_buffer + BUFSIZE)
107       ++endp;                   /* There is always room for the null */
108   }
109   cptr->count = endp - cptr->buffer;
110   return 1;
111 }
112
113 /*
114  * client_dopacket - handle client messages
115  */
116 int client_dopacket(struct Client *cptr, size_t length)
117 {
118   assert(0 != cptr);
119
120   update_bytes_received(cptr, length);
121   update_messages_received(cptr);
122
123   if (CPTR_KILLED == parse_client(cptr, cptr->buffer, cptr->buffer + length))
124     return CPTR_KILLED;
125   else if (IsDead(cptr))
126     return exit_client(cptr, cptr, &me, LastDeadComment(cptr));
127
128   return 1;
129 }
130
131