2 * IRC - Internet Relay Chat, common/packet.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
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)
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.
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.
21 * @brief Input packet handling functions.
29 #include "ircd_chattr.h"
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.
41 static void update_bytes_received(struct Client* cptr, unsigned int length)
43 cli_receiveB(&me) += length; /* Update bytes received */
44 cli_receiveB(cptr) += length;
47 /** Add one message to a client's received statistics.
48 * @param[in,out] cptr Client to update.
50 static void update_messages_received(struct Client* cptr)
52 ++(cli_receiveM(&me));
53 ++(cli_receiveM(cptr));
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.
62 int server_dopacket(struct Client* cptr, const char* buffer, int length)
70 update_bytes_received(cptr, length);
72 client_buffer = cli_buffer(cptr);
73 endp = client_buffer + cli_count(cptr);
76 while (length-- > 0) {
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
86 if (endp == client_buffer)
87 continue; /* Skip extra LF/CR's */
90 update_messages_received(cptr);
92 if (parse_server(cptr, cli_buffer(cptr), endp) == CPTR_KILLED)
95 * Socket is dead so exit
98 return exit_client(cptr, cptr, &me, cli_info(cptr));
101 else if (endp < client_buffer + BUFSIZE)
102 ++endp; /* There is always room for the null */
104 cli_count(cptr) = endp - cli_buffer(cptr);
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.
114 int connect_dopacket(struct Client *cptr, const char *buffer, int length)
122 update_bytes_received(cptr, length);
124 client_buffer = cli_buffer(cptr);
125 endp = client_buffer + cli_count(cptr);
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
140 /* Skip extra LF/CR's */
141 if (endp == client_buffer)
145 update_messages_received(cptr);
147 if (parse_client(cptr, cli_buffer(cptr), endp) == CPTR_KILLED)
149 /* Socket is dead so exit */
151 return exit_client(cptr, cptr, &me, cli_info(cptr));
152 else if (IsServer(cptr))
155 return server_dopacket(cptr, src, length);
157 endp = client_buffer;
159 else if (endp < client_buffer + BUFSIZE)
160 /* There is always room for the null */
163 cli_count(cptr) = endp - cli_buffer(cptr);
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.
172 int client_dopacket(struct Client *cptr, unsigned int length)
176 update_bytes_received(cptr, length);
177 update_messages_received(cptr);
179 if (CPTR_KILLED == parse_client(cptr, cli_buffer(cptr), cli_buffer(cptr) + length))
181 else if (IsDead(cptr))
182 return exit_client(cptr, cptr, &me, cli_info(cptr));