Convert connection byte counters to 64-bit integers.
[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 /** @file
21  * @brief Input packet handling functions.
22  * @version $Id$
23  */
24 #include "config.h"
25
26 #include "packet.h"
27 #include "client.h"
28 #include "ircd.h"
29 #include "ircd_chattr.h"
30 #include "parse.h"
31 #include "s_bsd.h"
32 #include "s_misc.h"
33 #include "send.h"
34
35 #include <assert.h>
36
37 /** Add a certain number of bytes to a client's received statistics.
38  * @param[in,out] cptr Client to update.
39  * @param[in] length Number of newly received bytes to add.
40  */
41 static void update_bytes_received(struct Client* cptr, unsigned int length)
42 {
43   cli_receiveB(&me)  += length;     /* Update bytes received */
44   cli_receiveB(cptr) += length;
45 }
46
47 /** Add one message to a client's received statistics.
48  * @param[in,out] cptr Client to update.
49  */
50 static void update_messages_received(struct Client* cptr)
51 {
52   ++(cli_receiveM(&me));
53   ++(cli_receiveM(cptr));
54 }
55
56 /** Handle received data from a directly connected server.
57  * @param[in] cptr Peer server that sent us data.
58  * @param[in] buffer Input buffer.
59  * @param[in] length Number of bytes in input buffer.
60  * @return 1 on success or CPTR_KILLED if the client is squit.
61  */
62 int server_dopacket(struct Client* cptr, const char* buffer, int length)
63 {
64   const char* src;
65   char*       endp;
66   char*       client_buffer;
67
68   assert(0 != cptr);
69
70   update_bytes_received(cptr, length);
71
72   client_buffer = cli_buffer(cptr);
73   endp = client_buffer + cli_count(cptr);
74   src = buffer;
75
76   while (length-- > 0) {
77     *endp = *src++;
78     /*
79      * Yuck.  Stuck.  To make sure we stay backward compatible,
80      * we must assume that either CR or LF terminates the message
81      * and not CR-LF.  By allowing CR or LF (alone) into the body
82      * of messages, backward compatibility is lost and major
83      * problems will arise. - Avalon
84      */
85     if (IsEol(*endp)) {
86       if (endp == client_buffer)
87         continue;               /* Skip extra LF/CR's */
88       *endp = '\0';
89
90       update_messages_received(cptr);
91
92       if (parse_server(cptr, cli_buffer(cptr), endp) == CPTR_KILLED)
93         return CPTR_KILLED;
94       /*
95        *  Socket is dead so exit
96        */
97       if (IsDead(cptr))
98         return exit_client(cptr, cptr, &me, cli_info(cptr));
99       endp = client_buffer;
100     }
101     else if (endp < client_buffer + BUFSIZE)
102       ++endp;                   /* There is always room for the null */
103   }
104   cli_count(cptr) = endp - cli_buffer(cptr);
105   return 1;
106 }
107
108 /** Handle received data from a new (unregistered) connection.
109  * @param[in] cptr Unregistered connection that sent us data.
110  * @param[in] buffer Input buffer.
111  * @param[in] length Number of bytes in input buffer.
112  * @return 1 on success or CPTR_KILLED if the client is squit.
113  */
114 int connect_dopacket(struct Client *cptr, const char *buffer, int length)
115 {
116   const char* src;
117   char*       endp;
118   char*       client_buffer;
119
120   assert(0 != cptr);
121
122   update_bytes_received(cptr, length);
123
124   client_buffer = cli_buffer(cptr);
125   endp = client_buffer + cli_count(cptr);
126   src = buffer;
127
128   while (length-- > 0)
129   {
130     *endp = *src++;
131     /*
132      * Yuck.  Stuck.  To make sure we stay backward compatible,
133      * we must assume that either CR or LF terminates the message
134      * and not CR-LF.  By allowing CR or LF (alone) into the body
135      * of messages, backward compatibility is lost and major
136      * problems will arise. - Avalon
137      */
138     if (IsEol(*endp))
139     {
140       /* Skip extra LF/CR's */
141       if (endp == client_buffer)
142         continue;
143       *endp = '\0';
144
145       update_messages_received(cptr);
146
147       if (parse_client(cptr, cli_buffer(cptr), endp) == CPTR_KILLED)
148         return CPTR_KILLED;
149       /* Socket is dead so exit */
150       if (IsDead(cptr))
151         return exit_client(cptr, cptr, &me, cli_info(cptr));
152       else if (IsServer(cptr))
153       {
154         cli_count(cptr) = 0;
155         return server_dopacket(cptr, src, length);
156       }
157       endp = client_buffer;
158     }
159     else if (endp < client_buffer + BUFSIZE)
160       /* There is always room for the null */
161       ++endp;
162   }
163   cli_count(cptr) = endp - cli_buffer(cptr);
164   return 1;
165 }
166
167 /** Handle received data from a local client.
168  * @param[in] cptr Local client that sent us data.
169  * @param[in] length Total number of bytes in client's input buffer.
170  * @return 1 on success or CPTR_KILLED if the client is squit.
171  */
172 int client_dopacket(struct Client *cptr, unsigned int length)
173 {
174   assert(0 != cptr);
175
176   update_bytes_received(cptr, length);
177   update_messages_received(cptr);
178
179   if (CPTR_KILLED == parse_client(cptr, cli_buffer(cptr), cli_buffer(cptr) + length))
180     return CPTR_KILLED;
181   else if (IsDead(cptr))
182     return exit_client(cptr, cptr, &me, cli_info(cptr));
183
184   return 1;
185 }
186
187