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