]>
Commit | Line | Data |
---|---|---|
189935b1 | 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: packet.c,v 1.14 2004/12/11 05:14:04 klmitch Exp $ | |
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 "ircd_log.h" | |
31 | #include "parse.h" | |
32 | #include "s_bsd.h" | |
33 | #include "s_misc.h" | |
34 | #include "send.h" | |
35 | ||
36 | /* #include <assert.h> -- Now using assert in ircd_log.h */ | |
37 | ||
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. | |
41 | */ | |
42 | static void update_bytes_received(struct Client* cptr, unsigned int length) | |
43 | { | |
44 | cli_receiveB(&me) += length; /* Update bytes received */ | |
45 | cli_receiveB(cptr) += length; | |
46 | } | |
47 | ||
48 | /** Add one message to a client's received statistics. | |
49 | * @param[in,out] cptr Client to update. | |
50 | */ | |
51 | static void update_messages_received(struct Client* cptr) | |
52 | { | |
53 | ++(cli_receiveM(&me)); | |
54 | ++(cli_receiveM(cptr)); | |
55 | } | |
56 | ||
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. | |
62 | */ | |
63 | int server_dopacket(struct Client* cptr, const char* buffer, int length) | |
64 | { | |
65 | const char* src; | |
66 | char* endp; | |
67 | char* client_buffer; | |
68 | ||
69 | assert(0 != cptr); | |
70 | ||
71 | update_bytes_received(cptr, length); | |
72 | ||
73 | client_buffer = cli_buffer(cptr); | |
74 | endp = client_buffer + cli_count(cptr); | |
75 | src = buffer; | |
76 | ||
77 | while (length-- > 0) { | |
78 | *endp = *src++; | |
79 | /* | |
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 | |
85 | */ | |
86 | if (IsEol(*endp)) { | |
87 | if (endp == client_buffer) | |
88 | continue; /* Skip extra LF/CR's */ | |
89 | *endp = '\0'; | |
90 | ||
91 | update_messages_received(cptr); | |
92 | ||
93 | if (parse_server(cptr, cli_buffer(cptr), endp) == CPTR_KILLED) | |
94 | return CPTR_KILLED; | |
95 | /* | |
96 | * Socket is dead so exit | |
97 | */ | |
98 | if (IsDead(cptr)) | |
99 | return exit_client(cptr, cptr, &me, cli_info(cptr)); | |
100 | endp = client_buffer; | |
101 | } | |
102 | else if (endp < client_buffer + BUFSIZE) | |
103 | ++endp; /* There is always room for the null */ | |
104 | } | |
105 | cli_count(cptr) = endp - cli_buffer(cptr); | |
106 | return 1; | |
107 | } | |
108 | ||
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. | |
114 | */ | |
115 | int connect_dopacket(struct Client *cptr, const char *buffer, int length) | |
116 | { | |
117 | const char* src; | |
118 | char* endp; | |
119 | char* client_buffer; | |
120 | ||
121 | assert(0 != cptr); | |
122 | ||
123 | update_bytes_received(cptr, length); | |
124 | ||
125 | client_buffer = cli_buffer(cptr); | |
126 | endp = client_buffer + cli_count(cptr); | |
127 | src = buffer; | |
128 | ||
129 | while (length-- > 0) | |
130 | { | |
131 | *endp = *src++; | |
132 | /* | |
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 | |
138 | */ | |
139 | if (IsEol(*endp)) | |
140 | { | |
141 | /* Skip extra LF/CR's */ | |
142 | if (endp == client_buffer) | |
143 | continue; | |
144 | *endp = '\0'; | |
145 | ||
146 | update_messages_received(cptr); | |
147 | ||
148 | if (parse_client(cptr, cli_buffer(cptr), endp) == CPTR_KILLED) | |
149 | return CPTR_KILLED; | |
150 | /* Socket is dead so exit */ | |
151 | if (IsDead(cptr)) | |
152 | return exit_client(cptr, cptr, &me, cli_info(cptr)); | |
153 | else if (IsServer(cptr)) | |
154 | { | |
155 | cli_count(cptr) = 0; | |
156 | return server_dopacket(cptr, src, length); | |
157 | } | |
158 | endp = client_buffer; | |
159 | } | |
160 | else if (endp < client_buffer + BUFSIZE) | |
161 | /* There is always room for the null */ | |
162 | ++endp; | |
163 | } | |
164 | cli_count(cptr) = endp - cli_buffer(cptr); | |
165 | return 1; | |
166 | } | |
167 | ||
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. | |
172 | */ | |
173 | int client_dopacket(struct Client *cptr, unsigned int length) | |
174 | { | |
175 | assert(0 != cptr); | |
176 | ||
177 | update_bytes_received(cptr, length); | |
178 | update_messages_received(cptr); | |
179 | ||
180 | if (CPTR_KILLED == parse_client(cptr, cli_buffer(cptr), cli_buffer(cptr) + length)) | |
181 | return CPTR_KILLED; | |
182 | else if (IsDead(cptr)) | |
183 | return exit_client(cptr, cptr, &me, cli_info(cptr)); | |
184 | ||
185 | return 1; | |
186 | } | |
187 | ||
188 |