]>
jfr.im git - irc/quakenet/snircd.git/blob - ircd/packet.c
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.
22 * @version $Id: packet.c,v 1.14 2004/12/11 05:14:04 klmitch Exp $
29 #include "ircd_chattr.h"
36 /* #include <assert.h> -- Now using assert in ircd_log.h */
38 /** Add a certain number of bytes to a client's received statistics.
39 * @param[in,out] cptr Client to update.
40 * @param[in] length Number of newly received bytes to add.
42 static void update_bytes_received(struct Client
* cptr
, unsigned int length
)
44 cli_receiveB(&me
) += length
; /* Update bytes received */
45 cli_receiveB(cptr
) += length
;
48 /** Add one message to a client's received statistics.
49 * @param[in,out] cptr Client to update.
51 static void update_messages_received(struct Client
* cptr
)
53 ++(cli_receiveM(&me
));
54 ++(cli_receiveM(cptr
));
57 /** Handle received data from a directly connected server.
58 * @param[in] cptr Peer server that sent us data.
59 * @param[in] buffer Input buffer.
60 * @param[in] length Number of bytes in input buffer.
61 * @return 1 on success or CPTR_KILLED if the client is squit.
63 int server_dopacket(struct Client
* cptr
, const char* buffer
, int length
)
71 update_bytes_received(cptr
, length
);
73 client_buffer
= cli_buffer(cptr
);
74 endp
= client_buffer
+ cli_count(cptr
);
77 while (length
-- > 0) {
80 * Yuck. Stuck. To make sure we stay backward compatible,
81 * we must assume that either CR or LF terminates the message
82 * and not CR-LF. By allowing CR or LF (alone) into the body
83 * of messages, backward compatibility is lost and major
84 * problems will arise. - Avalon
87 if (endp
== client_buffer
)
88 continue; /* Skip extra LF/CR's */
91 update_messages_received(cptr
);
93 if (parse_server(cptr
, cli_buffer(cptr
), endp
) == CPTR_KILLED
)
96 * Socket is dead so exit
99 return exit_client(cptr
, cptr
, &me
, cli_info(cptr
));
100 endp
= client_buffer
;
102 else if (endp
< client_buffer
+ BUFSIZE
)
103 ++endp
; /* There is always room for the null */
105 cli_count(cptr
) = endp
- cli_buffer(cptr
);
109 /** Handle received data from a new (unregistered) connection.
110 * @param[in] cptr Unregistered connection that sent us data.
111 * @param[in] buffer Input buffer.
112 * @param[in] length Number of bytes in input buffer.
113 * @return 1 on success or CPTR_KILLED if the client is squit.
115 int connect_dopacket(struct Client
*cptr
, const char *buffer
, int length
)
123 update_bytes_received(cptr
, length
);
125 client_buffer
= cli_buffer(cptr
);
126 endp
= client_buffer
+ cli_count(cptr
);
133 * Yuck. Stuck. To make sure we stay backward compatible,
134 * we must assume that either CR or LF terminates the message
135 * and not CR-LF. By allowing CR or LF (alone) into the body
136 * of messages, backward compatibility is lost and major
137 * problems will arise. - Avalon
141 /* Skip extra LF/CR's */
142 if (endp
== client_buffer
)
146 update_messages_received(cptr
);
148 if (parse_client(cptr
, cli_buffer(cptr
), endp
) == CPTR_KILLED
)
150 /* Socket is dead so exit */
152 return exit_client(cptr
, cptr
, &me
, cli_info(cptr
));
153 else if (IsServer(cptr
))
156 return server_dopacket(cptr
, src
, length
);
158 endp
= client_buffer
;
160 else if (endp
< client_buffer
+ BUFSIZE
)
161 /* There is always room for the null */
164 cli_count(cptr
) = endp
- cli_buffer(cptr
);
168 /** Handle received data from a local client.
169 * @param[in] cptr Local client that sent us data.
170 * @param[in] length Total number of bytes in client's input buffer.
171 * @return 1 on success or CPTR_KILLED if the client is squit.
173 int client_dopacket(struct Client
*cptr
, unsigned int length
)
177 update_bytes_received(cptr
, length
);
178 update_messages_received(cptr
);
180 if (CPTR_KILLED
== parse_client(cptr
, cli_buffer(cptr
), cli_buffer(cptr
) + length
))
182 else if (IsDead(cptr
))
183 return exit_client(cptr
, cptr
, &me
, cli_info(cptr
));