- Now server connections really work, and don't lose part of the burst
[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  * $Id$
21  */
22 #include "config.h"
23
24 #include "packet.h"
25 #include "client.h"
26 #include "ircd.h"
27 #include "ircd_chattr.h"
28 #include "parse.h"
29 #include "s_bsd.h"
30 #include "s_misc.h"
31 #include "send.h"
32
33 #include <assert.h>
34
35 static void update_bytes_received(struct Client* cptr, unsigned int length)
36 {
37   cli_receiveB(&me)  += length;     /* Update bytes received */
38   cli_receiveB(cptr) += length;
39
40   if (cli_receiveB(cptr) > 1023) {
41     cli_receiveK(cptr) += (cli_receiveB(cptr) >> 10);
42     cli_receiveB(cptr) &= 0x03ff;   /* 2^10 = 1024, 3ff = 1023 */
43   }
44   if (cli_receiveB(&me) > 1023) {
45     cli_receiveK(&me) += (cli_receiveB(&me) >> 10);
46     cli_receiveB(&me) &= 0x03ff;
47   }
48 }
49
50 static void update_messages_received(struct Client* cptr)
51 {
52   ++(cli_receiveM(&me));
53   ++(cli_receiveM(cptr));
54 }
55
56 /*
57  * dopacket
58  *
59  *    cptr - pointer to client structure for which the buffer data
60  *           applies.
61  *    buffer - pointer to the buffer containing the newly read data
62  *    length - number of valid bytes of data in the buffer
63  *
64  *  Note:
65  *    It is implicitly assumed that dopacket is called only
66  *    with cptr of "local" variation, which contains all the
67  *    necessary fields (buffer etc..)
68  */
69 int server_dopacket(struct Client* cptr, const char* buffer, int length)
70 {
71   const char* src;
72   char*       endp;
73   char*       client_buffer;
74   
75   assert(0 != cptr);
76
77   update_bytes_received(cptr, length);
78
79   client_buffer = cli_buffer(cptr);
80   endp = client_buffer + cli_count(cptr);
81   src = buffer;
82
83   while (length-- > 0) {
84     *endp = *src++;
85     /*
86      * Yuck.  Stuck.  To make sure we stay backward compatible,
87      * we must assume that either CR or LF terminates the message
88      * and not CR-LF.  By allowing CR or LF (alone) into the body
89      * of messages, backward compatibility is lost and major
90      * problems will arise. - Avalon
91      */
92     if (IsEol(*endp)) {
93       if (endp == client_buffer)
94         continue;               /* Skip extra LF/CR's */
95       *endp = '\0';
96
97       update_messages_received(cptr);
98
99       if (parse_server(cptr, cli_buffer(cptr), endp) == CPTR_KILLED)
100         return CPTR_KILLED;
101       /*
102        *  Socket is dead so exit
103        */
104       if (IsDead(cptr))
105         return exit_client(cptr, cptr, &me, cli_info(cptr));
106       endp = client_buffer;
107     }
108     else if (endp < client_buffer + BUFSIZE)
109       ++endp;                   /* There is always room for the null */
110   }
111   cli_count(cptr) = endp - cli_buffer(cptr);
112   return 1;
113 }
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 /*
169  * client_dopacket - handle client messages
170  */
171 int client_dopacket(struct Client *cptr, unsigned int length)
172 {
173   assert(0 != cptr);
174
175   update_bytes_received(cptr, length);
176   update_messages_received(cptr);
177
178   if (CPTR_KILLED == parse_client(cptr, cli_buffer(cptr), cli_buffer(cptr) + length))
179     return CPTR_KILLED;
180   else if (IsDead(cptr))
181     return exit_client(cptr, cptr, &me, cli_info(cptr));
182
183   return 1;
184 }
185
186