… | |
… | |
123 | len += vsprintf ((char *)buf + len, format, ap); |
123 | len += vsprintf ((char *)buf + len, format, ap); |
124 | |
124 | |
125 | va_end (ap); |
125 | va_end (ap); |
126 | } |
126 | } |
127 | |
127 | |
128 | /* Basically does the reverse of SockList_AddInt, but on |
128 | /****************************************************************************** |
129 | * strings instead. Same for the GetShort, but for 16 bits. |
|
|
130 | */ |
129 | * |
|
|
130 | * Start of read routines. |
|
|
131 | * |
|
|
132 | ******************************************************************************/ |
|
|
133 | |
131 | int |
134 | int |
132 | GetInt_String (unsigned char *data) |
135 | NewSocket::read_packet () |
133 | { |
136 | { |
134 | return ((data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3]); |
137 | for (;;) |
135 | } |
|
|
136 | |
|
|
137 | short |
|
|
138 | GetShort_String (unsigned char *data) |
|
|
139 | { |
|
|
140 | return ((data[0] << 8) + data[1]); |
|
|
141 | } |
|
|
142 | |
|
|
143 | /****************************************************************************** |
|
|
144 | * |
|
|
145 | * Start of read routines. |
|
|
146 | * |
|
|
147 | ******************************************************************************/ |
|
|
148 | |
|
|
149 | /** |
|
|
150 | * This reads from fd and puts the data in sl. We return true if we think |
|
|
151 | * we have a full packet, 0 if we have a partial packet. The only processing |
|
|
152 | * we do is remove the intial size value. len (As passed) is the size of the |
|
|
153 | * buffer allocated in the socklist. We make the assumption the buffer is |
|
|
154 | * at least 2 bytes long. |
|
|
155 | */ |
|
|
156 | |
|
|
157 | int |
|
|
158 | SockList_ReadPacket (int fd, SockList *sl, int len) |
|
|
159 | { |
|
|
160 | int stat, toread; |
|
|
161 | |
|
|
162 | /* Sanity check - shouldn't happen */ |
|
|
163 | if (sl->len < 0) |
|
|
164 | { |
|
|
165 | abort (); |
|
|
166 | } |
138 | { |
167 | /* We already have a partial packet */ |
139 | if (inbuf_len >= 2) |
168 | if (sl->len < 2) |
|
|
169 | { |
|
|
170 | do |
|
|
171 | { |
|
|
172 | stat = read (fd, sl->buf + sl->len, 2 - sl->len); |
|
|
173 | } |
140 | { |
174 | while ((stat == -1) && (errno == EINTR)); |
141 | unsigned int pkt_len = (inbuf [0] << 8) | inbuf [1]; |
175 | |
142 | |
176 | if (stat < 0) |
143 | if (inbuf_len >= 2 + pkt_len) |
|
|
144 | return pkt_len + 2; |
177 | { |
145 | } |
178 | /* In non blocking mode, EAGAIN is set when there is no |
146 | |
179 | * data available. |
147 | int amount = sizeof (inbuf) - inbuf_len; |
180 | */ |
148 | |
|
|
149 | if (amount <= 0) |
|
|
150 | { |
|
|
151 | LOG (llevError, "packet too large");//TODO |
|
|
152 | return -1; |
|
|
153 | } |
|
|
154 | |
|
|
155 | amount = read (fd, inbuf + inbuf_len, amount); |
|
|
156 | |
|
|
157 | if (!amount) |
|
|
158 | { |
|
|
159 | status = Ns_Dead; |
|
|
160 | return -1; |
|
|
161 | } |
|
|
162 | else if (amount < 0) |
|
|
163 | { |
181 | if (errno != EAGAIN && errno != EWOULDBLOCK) |
164 | if (errno != EAGAIN && errno != EINTR) |
182 | { |
165 | { |
183 | LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno)); |
166 | LOG (llevError, "read error: %s", strerror (errno)); |
|
|
167 | return -1; |
184 | } |
168 | } |
185 | return 0; /*Error */ |
|
|
186 | } |
|
|
187 | if (stat == 0) |
|
|
188 | return -1; |
|
|
189 | sl->len += stat; |
|
|
190 | #ifdef CS_LOGSTATS |
|
|
191 | cst_tot.ibytes += stat; |
|
|
192 | cst_lst.ibytes += stat; |
|
|
193 | #endif |
|
|
194 | if (stat < 2) |
|
|
195 | return 0; /* Still don't have a full packet */ |
|
|
196 | } |
|
|
197 | /* Figure out how much more data we need to read. Add 2 from the |
|
|
198 | * end of this - size header information is not included. |
|
|
199 | */ |
|
|
200 | toread = 2 + (sl->buf[0] << 8) + sl->buf[1] - sl->len; |
|
|
201 | if ((toread + sl->len) >= len) |
|
|
202 | { |
|
|
203 | LOG (llevError, "SockList_ReadPacket: Want to read more bytes than will fit in buffer (%d>=%d).\n", toread + sl->len, len); |
|
|
204 | /* Quick hack in case for 'oldsocketmode' input. If we are |
|
|
205 | * closing the socket anyways, then reading this extra 100 bytes |
|
|
206 | * shouldn't hurt. |
|
|
207 | */ |
|
|
208 | read (fd, sl->buf + 2, 100); |
|
|
209 | |
169 | |
210 | /* return error so the socket is closed */ |
|
|
211 | return -1; |
|
|
212 | } |
|
|
213 | do |
|
|
214 | { |
|
|
215 | do |
|
|
216 | { |
|
|
217 | stat = read (fd, sl->buf + sl->len, toread); |
|
|
218 | } |
|
|
219 | while ((stat < 0) && (errno == EINTR)); |
|
|
220 | if (stat < 0) |
|
|
221 | { |
|
|
222 | |
|
|
223 | if (errno != EAGAIN && errno != EWOULDBLOCK) |
|
|
224 | { |
|
|
225 | LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno)); |
|
|
226 | } |
|
|
227 | return 0; /*Error */ |
|
|
228 | } |
|
|
229 | if (stat == 0) |
|
|
230 | return -1; |
|
|
231 | sl->len += stat; |
|
|
232 | #ifdef CS_LOGSTATS |
|
|
233 | cst_tot.ibytes += stat; |
|
|
234 | cst_lst.ibytes += stat; |
|
|
235 | #endif |
|
|
236 | toread -= stat; |
|
|
237 | if (toread == 0) |
|
|
238 | return 1; |
|
|
239 | if (toread < 0) |
|
|
240 | { |
|
|
241 | LOG (llevError, "SockList_ReadPacket: Read more bytes than desired.\n"); |
|
|
242 | return 1; |
170 | return 0; |
243 | } |
171 | } |
|
|
172 | |
|
|
173 | inbuf_len += amount; |
|
|
174 | |
|
|
175 | cst_tot.ibytes += amount; |
|
|
176 | cst_lst.ibytes += amount; |
244 | } |
177 | } |
245 | while (toread > 0); |
178 | } |
246 | return 0; |
179 | |
|
|
180 | void |
|
|
181 | NewSocket::skip_packet (int len) |
|
|
182 | { |
|
|
183 | inbuf_len -= len; |
|
|
184 | memmove (inbuf, inbuf + len, inbuf_len); |
247 | } |
185 | } |
248 | |
186 | |
249 | /******************************************************************************* |
187 | /******************************************************************************* |
250 | * |
188 | * |
251 | * Start of write related routines. |
189 | * Start of write related routines. |
… | |
… | |
258 | * ns is the socket we are adding the data to, buf is the start of the |
196 | * ns is the socket we are adding the data to, buf is the start of the |
259 | * data, and len is the number of bytes to add. |
197 | * data, and len is the number of bytes to add. |
260 | */ |
198 | */ |
261 | |
199 | |
262 | static void |
200 | static void |
263 | add_to_buffer (NewSocket * ns, char *buf, int len) |
201 | add_to_buffer (NewSocket *ns, char *buf, int len) |
264 | { |
202 | { |
265 | int avail, end; |
203 | int avail, end; |
266 | |
204 | |
267 | if ((len + ns->outputbuffer.len) > SOCKETBUFSIZE) |
205 | if ((len + ns->outputbuffer.len) > SOCKETBUFSIZE) |
268 | { |
206 | { |