… | |
… | |
80 | #endif |
80 | #endif |
81 | } |
81 | } |
82 | |
82 | |
83 | /*********************************************************************** |
83 | /*********************************************************************** |
84 | * |
84 | * |
85 | * SockList functions/utilities |
85 | * packet functions/utilities |
86 | * |
86 | * |
87 | **********************************************************************/ |
87 | **********************************************************************/ |
88 | |
88 | |
89 | SockList &SockList::operator <<(const data &v) |
89 | packet &packet::operator <<(const data &v) |
90 | { |
90 | { |
91 | if (v.len) |
91 | if (v.len) |
92 | { |
92 | { |
93 | memcpy (buf + len, v.ptr, v.len); |
93 | memcpy (buf + len, v.ptr, v.len); |
94 | len += v.len; |
94 | len += v.len; |
95 | } |
95 | } |
96 | |
96 | |
97 | return *this; |
97 | return *this; |
98 | } |
98 | } |
99 | |
99 | |
100 | SockList &SockList::operator <<(const data8 &v) |
100 | packet &packet::operator <<(const data8 &v) |
101 | { |
101 | { |
102 | unsigned int len = min (v.len, 0x00FF); |
102 | unsigned int len = min (v.len, 0x00FF); |
103 | return *this << uint8 (len) << data (v.ptr, len); |
103 | return *this << uint8 (len) << data (v.ptr, len); |
104 | } |
104 | } |
105 | |
105 | |
106 | SockList &SockList::operator <<(const data16 &v) |
106 | packet &packet::operator <<(const data16 &v) |
107 | { |
107 | { |
108 | unsigned int len = min (v.len, 0xFFFF); |
108 | unsigned int len = min (v.len, 0xFFFF); |
109 | return *this << uint16 (len) << data (v.ptr, len); |
109 | return *this << uint16 (len) << data (v.ptr, len); |
110 | } |
110 | } |
111 | |
111 | |
112 | SockList &SockList::operator <<(const char *v) |
112 | packet &packet::operator <<(const char *v) |
113 | { |
113 | { |
114 | return *this << data (v, strlen (v ? v : 0)); |
114 | return *this << data (v, strlen (v ? v : 0)); |
115 | } |
115 | } |
116 | |
116 | |
117 | void |
117 | void |
118 | SockList::printf (const char *format, ...) |
118 | packet::printf (const char *format, ...) |
119 | { |
119 | { |
120 | va_list ap; |
120 | va_list ap; |
121 | va_start (ap, format); |
121 | va_start (ap, format); |
122 | |
122 | |
123 | len += vsprintf ((char *)buf + len, format, ap); |
123 | len += vsnprintf ((char *)buf + len, MAXSOCKBUF, 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 | { |
… | |
… | |
422 | cst_lst.obytes += amt; |
360 | cst_lst.obytes += amt; |
423 | #endif |
361 | #endif |
424 | } |
362 | } |
425 | } |
363 | } |
426 | |
364 | |
427 | |
|
|
428 | /** |
365 | /** |
429 | * Takes a string of data, and writes it out to the socket. A very handy |
366 | * Takes a string of data, and writes it out to the socket. A very handy |
430 | * shortcut function. |
367 | * shortcut function. |
431 | */ |
368 | */ |
432 | void |
|
|
433 | cs_write_string (NewSocket *ns, const char *buf, int len) |
|
|
434 | { |
|
|
435 | SockList sl; |
|
|
436 | |
369 | |
437 | sl.len = len; |
370 | void |
438 | sl.buf = (unsigned char *) buf; |
371 | NewSocket::send_packet (packet &sl) |
|
|
372 | { |
439 | Send_With_Handling (ns, &sl); |
373 | Send_With_Handling (this, &sl); |
440 | } |
374 | } |
441 | |
375 | |
|
|
376 | void |
|
|
377 | NewSocket::send_packet (const char *buf, int len) |
|
|
378 | { |
|
|
379 | packet sl; |
|
|
380 | |
|
|
381 | sl << data (buf, len); |
|
|
382 | send_packet (sl); |
|
|
383 | } |
|
|
384 | |
|
|
385 | void |
|
|
386 | NewSocket::send_packet (const char *buf) |
|
|
387 | { |
|
|
388 | send_packet (buf, strlen (buf)); |
|
|
389 | } |
442 | |
390 | |
443 | /** |
391 | /** |
444 | * Calls Write_To_Socket to send data to the client. |
392 | * Calls Write_To_Socket to send data to the client. |
445 | * |
393 | * |
446 | * The only difference in this function is that we take a SockList |
394 | * The only difference in this function is that we take a packet |
447 | *, and we prepend the length information. |
395 | *, and we prepend the length information. |
448 | */ |
396 | */ |
449 | void |
397 | void |
450 | Send_With_Handling (NewSocket *ns, SockList *msg) |
398 | Send_With_Handling (NewSocket *ns, packet *msg) |
451 | { |
399 | { |
452 | unsigned char sbuf[4]; |
400 | unsigned char sbuf[4]; |
453 | |
401 | |
454 | if (ns->status == Ns_Dead || !msg) |
402 | if (ns->status == Ns_Dead || !msg) |
455 | return; |
403 | return; |
… | |
… | |
466 | sbuf[1] = ((uint32) (msg->len)) & 0xFF; |
414 | sbuf[1] = ((uint32) (msg->len)) & 0xFF; |
467 | if (ns->status != Ns_Old) |
415 | if (ns->status != Ns_Old) |
468 | Write_To_Socket (ns, (char *) sbuf, 2); |
416 | Write_To_Socket (ns, (char *) sbuf, 2); |
469 | Write_To_Socket (ns, (char *) msg->buf, msg->len); |
417 | Write_To_Socket (ns, (char *) msg->buf, msg->len); |
470 | } |
418 | } |
471 | |
|
|
472 | /** |
|
|
473 | * Takes a string of data, and writes it out to the socket. A very handy |
|
|
474 | * shortcut function. |
|
|
475 | */ |
|
|
476 | void |
|
|
477 | Write_String_To_Socket (NewSocket *ns, char *buf, int len) |
|
|
478 | { |
|
|
479 | SockList sl; |
|
|
480 | |
|
|
481 | sl.len = len; |
|
|
482 | sl.buf = (unsigned char *) buf; |
|
|
483 | Send_With_Handling (ns, &sl); |
|
|
484 | } |
|
|
485 | |
|
|
486 | |
419 | |
487 | /****************************************************************************** |
420 | /****************************************************************************** |
488 | * |
421 | * |
489 | * statistics logging functions. |
422 | * statistics logging functions. |
490 | * |
423 | * |