]>
Commit | Line | Data |
---|---|---|
b57f37fb WP |
1 | /* |
2 | * ircd-ratbox: A slightly useful ircd. | |
3 | * linebuf.c: Maintains linebuffers. | |
4 | * | |
5 | * Copyright (C) 2001-2002 Adrian Chadd <adrian@creative.net.au> | |
6 | * Copyright (C) 2002 Hybrid Development Team | |
7 | * Copyright (C) 2002-2005 ircd-ratbox development team | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, write to the Free Software | |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 | |
22 | * USA | |
23 | * | |
b57f37fb WP |
24 | */ |
25 | ||
26 | #include <libratbox_config.h> | |
27 | #include <ratbox_lib.h> | |
28 | #include <commio-int.h> | |
29 | ||
b57f37fb | 30 | static rb_bh *rb_linebuf_heap; |
b57f37fb WP |
31 | |
32 | static int bufline_count = 0; | |
33 | ||
34 | #ifndef LINEBUF_HEAP_SIZE | |
35 | #define LINEBUF_HEAP_SIZE 2048 | |
36 | #endif | |
37 | ||
38 | /* | |
39 | * rb_linebuf_init | |
40 | * | |
41 | * Initialise the linebuf mechanism | |
42 | */ | |
43 | ||
44 | void | |
45 | rb_linebuf_init(size_t heap_size) | |
46 | { | |
b57f37fb | 47 | rb_linebuf_heap = rb_bh_create(sizeof(buf_line_t), heap_size, "librb_linebuf_heap"); |
b57f37fb WP |
48 | } |
49 | ||
50 | static buf_line_t * | |
51 | rb_linebuf_allocate(void) | |
52 | { | |
53 | buf_line_t *t; | |
b57f37fb | 54 | t = rb_bh_alloc(rb_linebuf_heap); |
b57f37fb WP |
55 | return (t); |
56 | ||
57 | } | |
58 | ||
59 | static void | |
60 | rb_linebuf_free(buf_line_t * p) | |
61 | { | |
b57f37fb | 62 | rb_bh_free(rb_linebuf_heap, p); |
b57f37fb WP |
63 | } |
64 | ||
65 | /* | |
66 | * rb_linebuf_new_line | |
67 | * | |
68 | * Create a new line, and link it to the given linebuf. | |
69 | * It will be initially empty. | |
70 | */ | |
71 | static buf_line_t * | |
72 | rb_linebuf_new_line(buf_head_t * bufhead) | |
73 | { | |
74 | buf_line_t *bufline; | |
75 | rb_dlink_node *node; | |
76 | ||
77 | bufline = rb_linebuf_allocate(); | |
78 | if(bufline == NULL) | |
79 | return NULL; | |
80 | ++bufline_count; | |
81 | ||
82 | ||
83 | node = rb_make_rb_dlink_node(); | |
84 | ||
85 | /* Stick it at the end of the buf list */ | |
86 | rb_dlinkAddTail(bufline, node, &bufhead->list); | |
87 | bufline->refcount++; | |
88 | ||
89 | /* And finally, update the allocated size */ | |
90 | bufhead->alloclen++; | |
91 | bufhead->numlines++; | |
92 | ||
93 | return bufline; | |
94 | } | |
95 | ||
96 | ||
97 | /* | |
98 | * rb_linebuf_done_line | |
99 | * | |
100 | * We've finished with the given line, so deallocate it | |
101 | */ | |
102 | static void | |
94b4fbf9 | 103 | rb_linebuf_done_line(buf_head_t * bufhead, buf_line_t * bufline, rb_dlink_node *node) |
b57f37fb WP |
104 | { |
105 | /* Remove it from the linked list */ | |
106 | rb_dlinkDestroy(node, &bufhead->list); | |
107 | ||
108 | /* Update the allocated size */ | |
109 | bufhead->alloclen--; | |
110 | bufhead->len -= bufline->len; | |
111 | lrb_assert(bufhead->len >= 0); | |
112 | bufhead->numlines--; | |
113 | ||
114 | bufline->refcount--; | |
115 | lrb_assert(bufline->refcount >= 0); | |
116 | ||
117 | if(bufline->refcount == 0) | |
118 | { | |
119 | /* and finally, deallocate the buf */ | |
120 | --bufline_count; | |
121 | lrb_assert(bufline_count >= 0); | |
122 | rb_linebuf_free(bufline); | |
123 | } | |
124 | } | |
125 | ||
126 | ||
127 | /* | |
128 | * skip to end of line or the crlfs, return the number of bytes .. | |
129 | */ | |
130 | static inline int | |
131 | rb_linebuf_skip_crlf(char *ch, int len) | |
132 | { | |
133 | int orig_len = len; | |
134 | ||
135 | /* First, skip until the first non-CRLF */ | |
136 | for(; len; len--, ch++) | |
137 | { | |
138 | if(*ch == '\r') | |
139 | break; | |
140 | else if(*ch == '\n') | |
141 | break; | |
142 | } | |
143 | ||
144 | /* Then, skip until the last CRLF */ | |
145 | for(; len; len--, ch++) | |
146 | { | |
147 | if((*ch != '\r') && (*ch != '\n')) | |
148 | break; | |
149 | } | |
150 | lrb_assert(orig_len > len); | |
151 | return (orig_len - len); | |
152 | } | |
153 | ||
154 | ||
155 | ||
156 | /* | |
157 | * rb_linebuf_newbuf | |
158 | * | |
159 | * Initialise the new buffer | |
160 | */ | |
161 | void | |
162 | rb_linebuf_newbuf(buf_head_t * bufhead) | |
163 | { | |
164 | /* not much to do right now :) */ | |
165 | memset(bufhead, 0, sizeof(buf_head_t)); | |
166 | } | |
167 | ||
168 | /* | |
169 | * rb_linebuf_donebuf | |
170 | * | |
171 | * Flush all the lines associated with this buffer | |
172 | */ | |
173 | void | |
174 | rb_linebuf_donebuf(buf_head_t * bufhead) | |
175 | { | |
176 | while(bufhead->list.head != NULL) | |
177 | { | |
94b4fbf9 VY |
178 | rb_linebuf_done_line(bufhead, (buf_line_t *) bufhead->list.head->data, |
179 | bufhead->list.head); | |
b57f37fb WP |
180 | } |
181 | } | |
182 | ||
183 | /* | |
184 | * rb_linebuf_copy_line | |
185 | * | |
186 | * Okay..this functions comments made absolutely no sense. | |
187 | * | |
188 | * Basically what we do is this. Find the first chunk of text | |
189 | * and then scan for a CRLF. If we didn't find it, but we didn't | |
190 | * overflow our buffer..we wait for some more data. | |
191 | * If we found a CRLF, we replace them with a \0 character. | |
192 | * If we overflowed, we copy the most our buffer can handle, terminate | |
193 | * it with a \0 and return. | |
194 | * | |
195 | * The return value is the amount of data we consumed. This could | |
196 | * be different than the size of the linebuffer, as when we discard | |
197 | * the overflow, we don't want to process it again. | |
198 | * | |
199 | * This still sucks in my opinion, but it seems to work. | |
200 | * | |
201 | * -Aaron | |
202 | */ | |
203 | static int | |
204 | rb_linebuf_copy_line(buf_head_t * bufhead, buf_line_t * bufline, char *data, int len) | |
205 | { | |
206 | int cpylen = 0; /* how many bytes we've copied */ | |
207 | char *ch = data; /* Pointer to where we are in the read data */ | |
208 | char *bufch = bufline->buf + bufline->len; | |
209 | int clen = 0; /* how many bytes we've processed, | |
210 | and don't ever want to see again.. */ | |
211 | ||
212 | /* If its full or terminated, ignore it */ | |
213 | ||
214 | bufline->raw = 0; | |
215 | lrb_assert(bufline->len < BUF_DATA_SIZE); | |
216 | if(bufline->terminated == 1) | |
217 | return 0; | |
218 | ||
219 | clen = cpylen = rb_linebuf_skip_crlf(ch, len); | |
220 | if(clen == -1) | |
221 | return -1; | |
222 | ||
223 | /* This is the ~overflow case..This doesn't happen often.. */ | |
224 | if(cpylen > (BUF_DATA_SIZE - bufline->len - 1)) | |
225 | { | |
226 | memcpy(bufch, ch, (BUF_DATA_SIZE - bufline->len - 1)); | |
227 | bufline->buf[BUF_DATA_SIZE - 1] = '\0'; | |
228 | bufch = bufline->buf + BUF_DATA_SIZE - 2; | |
229 | while(cpylen && (*bufch == '\r' || *bufch == '\n')) | |
230 | { | |
231 | *bufch = '\0'; | |
232 | cpylen--; | |
233 | bufch--; | |
234 | } | |
235 | bufline->terminated = 1; | |
236 | bufline->len = BUF_DATA_SIZE - 1; | |
237 | bufhead->len += BUF_DATA_SIZE - 1; | |
238 | return clen; | |
239 | } | |
240 | ||
241 | memcpy(bufch, ch, cpylen); | |
242 | bufch += cpylen; | |
243 | *bufch = '\0'; | |
244 | bufch--; | |
245 | ||
246 | if(*bufch != '\r' && *bufch != '\n') | |
247 | { | |
248 | /* No linefeed, bail for the next time */ | |
249 | bufhead->len += cpylen; | |
250 | bufline->len += cpylen; | |
251 | bufline->terminated = 0; | |
252 | return clen; | |
253 | } | |
254 | ||
255 | /* Yank the CRLF off this, replace with a \0 */ | |
256 | while(cpylen && (*bufch == '\r' || *bufch == '\n')) | |
257 | { | |
258 | *bufch = '\0'; | |
259 | cpylen--; | |
260 | bufch--; | |
261 | } | |
262 | ||
263 | bufline->terminated = 1; | |
264 | bufhead->len += cpylen; | |
265 | bufline->len += cpylen; | |
266 | return clen; | |
267 | } | |
268 | ||
269 | /* | |
270 | * rb_linebuf_copy_raw | |
271 | * | |
272 | * Copy as much data as possible directly into a linebuf, | |
273 | * splitting at \r\n, but without altering any data. | |
274 | * | |
275 | */ | |
276 | static int | |
277 | rb_linebuf_copy_raw(buf_head_t * bufhead, buf_line_t * bufline, char *data, int len) | |
278 | { | |
279 | int cpylen = 0; /* how many bytes we've copied */ | |
280 | char *ch = data; /* Pointer to where we are in the read data */ | |
281 | char *bufch = bufline->buf + bufline->len; | |
282 | int clen = 0; /* how many bytes we've processed, | |
283 | and don't ever want to see again.. */ | |
284 | ||
285 | /* If its full or terminated, ignore it */ | |
286 | ||
287 | bufline->raw = 1; | |
288 | lrb_assert(bufline->len < BUF_DATA_SIZE); | |
289 | if(bufline->terminated == 1) | |
290 | return 0; | |
291 | ||
292 | clen = cpylen = rb_linebuf_skip_crlf(ch, len); | |
293 | if(clen == -1) | |
294 | return -1; | |
295 | ||
296 | /* This is the overflow case..This doesn't happen often.. */ | |
297 | if(cpylen > (BUF_DATA_SIZE - bufline->len - 1)) | |
298 | { | |
299 | clen = BUF_DATA_SIZE - bufline->len - 1; | |
300 | memcpy(bufch, ch, clen); | |
301 | bufline->buf[BUF_DATA_SIZE - 1] = '\0'; | |
302 | bufch = bufline->buf + BUF_DATA_SIZE - 2; | |
303 | bufline->terminated = 1; | |
304 | bufline->len = BUF_DATA_SIZE - 1; | |
305 | bufhead->len += BUF_DATA_SIZE - 1; | |
306 | return clen; | |
307 | } | |
308 | ||
309 | memcpy(bufch, ch, cpylen); | |
310 | bufch += cpylen; | |
311 | *bufch = '\0'; | |
312 | bufch--; | |
313 | ||
314 | if(*bufch != '\r' && *bufch != '\n') | |
315 | { | |
316 | /* No linefeed, bail for the next time */ | |
317 | bufhead->len += cpylen; | |
318 | bufline->len += cpylen; | |
319 | bufline->terminated = 0; | |
320 | return clen; | |
321 | } | |
322 | ||
323 | bufline->terminated = 1; | |
324 | bufhead->len += cpylen; | |
325 | bufline->len += cpylen; | |
326 | return clen; | |
327 | } | |
328 | ||
329 | ||
330 | /* | |
331 | * rb_linebuf_parse | |
332 | * | |
333 | * Take a given buffer and break out as many buffers as we can. | |
334 | * If we find a CRLF, we terminate that buffer and create a new one. | |
335 | * If we don't find a CRLF whilst parsing a buffer, we don't mark it | |
336 | * 'finished', so the next loop through we can continue appending .. | |
337 | * | |
338 | * A few notes here, which you'll need to understand before continuing. | |
339 | * | |
340 | * - right now I'm only dealing with single sized buffers. Later on, | |
341 | * I might consider chaining buffers together to get longer "lines" | |
342 | * but seriously, I don't see the advantage right now. | |
343 | * | |
344 | * - This *is* designed to turn into a reference-counter-protected setup | |
345 | * to dodge copious copies. | |
346 | */ | |
347 | int | |
348 | rb_linebuf_parse(buf_head_t * bufhead, char *data, int len, int raw) | |
349 | { | |
350 | buf_line_t *bufline; | |
351 | int cpylen; | |
352 | int linecnt = 0; | |
353 | ||
354 | /* First, if we have a partial buffer, try to squeze data into it */ | |
355 | if(bufhead->list.tail != NULL) | |
356 | { | |
357 | /* Check we're doing the partial buffer thing */ | |
358 | bufline = bufhead->list.tail->data; | |
b57f37fb WP |
359 | /* just try, the worst it could do is *reject* us .. */ |
360 | if(!raw) | |
361 | cpylen = rb_linebuf_copy_line(bufhead, bufline, data, len); | |
362 | else | |
363 | cpylen = rb_linebuf_copy_raw(bufhead, bufline, data, len); | |
364 | ||
365 | if(cpylen == -1) | |
366 | return -1; | |
367 | ||
368 | linecnt++; | |
369 | /* If we've copied the same as what we've got, quit now */ | |
370 | if(cpylen == len) | |
371 | return linecnt; /* all the data done so soon? */ | |
372 | ||
373 | /* Skip the data and update len .. */ | |
374 | len -= cpylen; | |
375 | lrb_assert(len >= 0); | |
376 | data += cpylen; | |
377 | } | |
378 | ||
379 | /* Next, the loop */ | |
380 | while(len > 0) | |
381 | { | |
382 | /* We obviously need a new buffer, so .. */ | |
383 | bufline = rb_linebuf_new_line(bufhead); | |
384 | ||
385 | /* And parse */ | |
386 | if(!raw) | |
387 | cpylen = rb_linebuf_copy_line(bufhead, bufline, data, len); | |
388 | else | |
389 | cpylen = rb_linebuf_copy_raw(bufhead, bufline, data, len); | |
390 | ||
391 | if(cpylen == -1) | |
392 | return -1; | |
393 | ||
394 | len -= cpylen; | |
395 | lrb_assert(len >= 0); | |
396 | data += cpylen; | |
397 | linecnt++; | |
398 | } | |
399 | return linecnt; | |
400 | } | |
401 | ||
402 | ||
403 | /* | |
404 | * rb_linebuf_get | |
405 | * | |
406 | * get the next buffer from our line. For the time being it will copy | |
407 | * data into the given buffer and free the underlying linebuf. | |
408 | */ | |
409 | int | |
410 | rb_linebuf_get(buf_head_t * bufhead, char *buf, int buflen, int partial, int raw) | |
411 | { | |
412 | buf_line_t *bufline; | |
413 | int cpylen; | |
414 | char *start, *ch; | |
415 | ||
416 | /* make sure we have a line */ | |
417 | if(bufhead->list.head == NULL) | |
418 | return 0; /* Obviously not.. hrm. */ | |
419 | ||
420 | bufline = bufhead->list.head->data; | |
421 | ||
422 | /* make sure that the buffer was actually *terminated */ | |
423 | if(!(partial || bufline->terminated)) | |
424 | return 0; /* Wait for more data! */ | |
425 | ||
426 | if(buflen < bufline->len) | |
427 | cpylen = buflen - 1; | |
428 | else | |
429 | cpylen = bufline->len; | |
430 | ||
431 | /* Copy it */ | |
432 | start = bufline->buf; | |
433 | ||
434 | /* if we left extraneous '\r\n' characters in the string, | |
435 | * and we don't want to read the raw data, clean up the string. | |
436 | */ | |
437 | if(bufline->raw && !raw) | |
438 | { | |
439 | /* skip leading EOL characters */ | |
440 | while(cpylen && (*start == '\r' || *start == '\n')) | |
441 | { | |
442 | start++; | |
443 | cpylen--; | |
444 | } | |
445 | /* skip trailing EOL characters */ | |
446 | ch = &start[cpylen - 1]; | |
447 | while(cpylen && (*ch == '\r' || *ch == '\n')) | |
448 | { | |
449 | ch--; | |
450 | cpylen--; | |
451 | } | |
452 | } | |
453 | ||
454 | memcpy(buf, start, cpylen); | |
455 | ||
456 | /* convert CR/LF to NULL */ | |
457 | if(!raw) | |
458 | buf[cpylen] = '\0'; | |
459 | ||
460 | lrb_assert(cpylen >= 0); | |
461 | ||
462 | /* Deallocate the line */ | |
463 | rb_linebuf_done_line(bufhead, bufline, bufhead->list.head); | |
464 | ||
465 | /* return how much we copied */ | |
466 | return cpylen; | |
467 | } | |
468 | ||
469 | /* | |
470 | * rb_linebuf_attach | |
471 | * | |
472 | * attach the lines in a buf_head_t to another buf_head_t | |
473 | * without copying the data (using refcounts). | |
474 | */ | |
475 | void | |
476 | rb_linebuf_attach(buf_head_t * bufhead, buf_head_t * new) | |
477 | { | |
478 | rb_dlink_node *ptr; | |
479 | buf_line_t *line; | |
480 | ||
481 | RB_DLINK_FOREACH(ptr, new->list.head) | |
482 | { | |
483 | line = ptr->data; | |
484 | rb_dlinkAddTailAlloc(line, &bufhead->list); | |
485 | ||
486 | /* Update the allocated size */ | |
487 | bufhead->alloclen++; | |
488 | bufhead->len += line->len; | |
489 | bufhead->numlines++; | |
490 | ||
491 | line->refcount++; | |
492 | } | |
493 | } | |
494 | ||
495 | ||
496 | ||
497 | /* | |
498 | * rb_linebuf_putmsg | |
499 | * | |
500 | * Similar to rb_linebuf_put, but designed for use by send.c. | |
501 | * | |
502 | * prefixfmt is used as a format for the varargs, and is inserted first. | |
503 | * Then format/va_args is appended to the buffer. | |
504 | */ | |
505 | void | |
94b4fbf9 VY |
506 | rb_linebuf_putmsg(buf_head_t * bufhead, const char *format, va_list * va_args, |
507 | const char *prefixfmt, ...) | |
b57f37fb WP |
508 | { |
509 | buf_line_t *bufline; | |
510 | int len = 0; | |
511 | va_list prefix_args; | |
512 | ||
513 | /* make sure the previous line is terminated */ | |
514 | #ifndef NDEBUG | |
515 | if(bufhead->list.tail) | |
516 | { | |
517 | bufline = bufhead->list.tail->data; | |
518 | lrb_assert(bufline->terminated); | |
519 | } | |
520 | #endif | |
521 | /* Create a new line */ | |
522 | bufline = rb_linebuf_new_line(bufhead); | |
523 | ||
524 | if(prefixfmt != NULL) | |
525 | { | |
526 | va_start(prefix_args, prefixfmt); | |
527 | len = rb_vsnprintf(bufline->buf, BUF_DATA_SIZE, prefixfmt, prefix_args); | |
528 | va_end(prefix_args); | |
529 | } | |
530 | ||
531 | if(va_args != NULL) | |
532 | { | |
533 | len += rb_vsnprintf((bufline->buf + len), (BUF_DATA_SIZE - len), format, *va_args); | |
534 | } | |
535 | ||
536 | bufline->terminated = 1; | |
537 | ||
538 | /* Truncate the data if required */ | |
033be687 | 539 | if(rb_unlikely(len > 510)) |
b57f37fb WP |
540 | { |
541 | len = 510; | |
542 | bufline->buf[len++] = '\r'; | |
543 | bufline->buf[len++] = '\n'; | |
544 | } | |
033be687 | 545 | else if(rb_unlikely(len == 0)) |
b57f37fb WP |
546 | { |
547 | bufline->buf[len++] = '\r'; | |
548 | bufline->buf[len++] = '\n'; | |
549 | bufline->buf[len] = '\0'; | |
550 | } | |
551 | else | |
552 | { | |
553 | /* Chop trailing CRLF's .. */ | |
94b4fbf9 VY |
554 | while((bufline->buf[len] == '\r') || (bufline->buf[len] == '\n') |
555 | || (bufline->buf[len] == '\0')) | |
b57f37fb WP |
556 | { |
557 | len--; | |
558 | } | |
559 | ||
560 | bufline->buf[++len] = '\r'; | |
561 | bufline->buf[++len] = '\n'; | |
562 | bufline->buf[++len] = '\0'; | |
563 | } | |
564 | ||
565 | bufline->len = len; | |
566 | bufhead->len += len; | |
567 | } | |
568 | ||
569 | void | |
94b4fbf9 | 570 | rb_linebuf_putbuf(buf_head_t * bufhead, const char *buffer) |
b57f37fb WP |
571 | { |
572 | buf_line_t *bufline; | |
573 | int len = 0; | |
574 | ||
575 | /* make sure the previous line is terminated */ | |
576 | #ifndef NDEBUG | |
577 | if(bufhead->list.tail) | |
578 | { | |
579 | bufline = bufhead->list.tail->data; | |
580 | lrb_assert(bufline->terminated); | |
581 | } | |
582 | #endif | |
583 | /* Create a new line */ | |
584 | bufline = rb_linebuf_new_line(bufhead); | |
585 | ||
033be687 | 586 | if(rb_unlikely(buffer != NULL)) |
b57f37fb WP |
587 | len = rb_strlcpy(bufline->buf, buffer, BUF_DATA_SIZE); |
588 | ||
589 | bufline->terminated = 1; | |
590 | ||
591 | /* Truncate the data if required */ | |
033be687 | 592 | if(rb_unlikely(len > 510)) |
b57f37fb WP |
593 | { |
594 | len = 510; | |
595 | bufline->buf[len++] = '\r'; | |
596 | bufline->buf[len++] = '\n'; | |
597 | } | |
033be687 | 598 | else if(rb_unlikely(len == 0)) |
b57f37fb WP |
599 | { |
600 | bufline->buf[len++] = '\r'; | |
601 | bufline->buf[len++] = '\n'; | |
602 | bufline->buf[len] = '\0'; | |
603 | } | |
604 | else | |
605 | { | |
606 | /* Chop trailing CRLF's .. */ | |
94b4fbf9 VY |
607 | while((bufline->buf[len] == '\r') || (bufline->buf[len] == '\n') |
608 | || (bufline->buf[len] == '\0')) | |
b57f37fb WP |
609 | { |
610 | len--; | |
611 | } | |
612 | ||
613 | bufline->buf[++len] = '\r'; | |
614 | bufline->buf[++len] = '\n'; | |
615 | bufline->buf[++len] = '\0'; | |
616 | } | |
617 | ||
618 | bufline->len = len; | |
619 | bufhead->len += len; | |
620 | ||
94b4fbf9 | 621 | |
b57f37fb WP |
622 | } |
623 | ||
624 | void | |
625 | rb_linebuf_put(buf_head_t * bufhead, const char *format, ...) | |
626 | { | |
627 | buf_line_t *bufline; | |
628 | int len = 0; | |
629 | va_list args; | |
630 | ||
631 | /* make sure the previous line is terminated */ | |
632 | #ifndef NDEBUG | |
633 | if(bufhead->list.tail) | |
634 | { | |
635 | bufline = bufhead->list.tail->data; | |
636 | lrb_assert(bufline->terminated); | |
637 | } | |
638 | #endif | |
639 | /* Create a new line */ | |
640 | bufline = rb_linebuf_new_line(bufhead); | |
641 | ||
033be687 | 642 | if(rb_unlikely(format != NULL)) |
b57f37fb WP |
643 | { |
644 | va_start(args, format); | |
645 | len = rb_vsnprintf(bufline->buf, BUF_DATA_SIZE, format, args); | |
646 | va_end(args); | |
647 | } | |
648 | ||
649 | bufline->terminated = 1; | |
650 | ||
651 | /* Truncate the data if required */ | |
033be687 | 652 | if(rb_unlikely(len > 510)) |
b57f37fb WP |
653 | { |
654 | len = 510; | |
655 | bufline->buf[len++] = '\r'; | |
656 | bufline->buf[len++] = '\n'; | |
657 | } | |
033be687 | 658 | else if(rb_unlikely(len == 0)) |
b57f37fb WP |
659 | { |
660 | bufline->buf[len++] = '\r'; | |
661 | bufline->buf[len++] = '\n'; | |
662 | bufline->buf[len] = '\0'; | |
663 | } | |
664 | else | |
665 | { | |
666 | /* Chop trailing CRLF's .. */ | |
94b4fbf9 VY |
667 | while((bufline->buf[len] == '\r') || (bufline->buf[len] == '\n') |
668 | || (bufline->buf[len] == '\0')) | |
b57f37fb WP |
669 | { |
670 | len--; | |
671 | } | |
672 | ||
673 | bufline->buf[++len] = '\r'; | |
674 | bufline->buf[++len] = '\n'; | |
675 | bufline->buf[++len] = '\0'; | |
676 | } | |
677 | ||
678 | bufline->len = len; | |
679 | bufhead->len += len; | |
680 | } | |
681 | ||
682 | ||
683 | ||
684 | /* | |
685 | * rb_linebuf_flush | |
686 | * | |
687 | * Flush data to the buffer. It tries to write as much data as possible | |
688 | * to the given socket. Any return values are passed straight through. | |
689 | * If there is no data in the socket, EWOULDBLOCK is set as an errno | |
690 | * rather than returning 0 (which would map to an EOF..) | |
691 | * | |
692 | * Notes: XXX We *should* have a clue here when a non-full buffer is arrived. | |
693 | * and tag it so that we don't re-schedule another write until | |
694 | * we have a CRLF. | |
695 | */ | |
696 | int | |
697 | rb_linebuf_flush(rb_fde_t *F, buf_head_t * bufhead) | |
698 | { | |
699 | buf_line_t *bufline; | |
700 | int retval; | |
701 | ||
702 | /* | |
703 | * autoconf checks for this..but really just want to use it if we have a | |
704 | * native version even if libircd provides a fake version... | |
705 | */ | |
706 | #ifdef HAVE_WRITEV | |
707 | if(!rb_fd_ssl(F)) | |
708 | { | |
709 | rb_dlink_node *ptr; | |
710 | int x = 0, y; | |
711 | int xret; | |
712 | static struct rb_iovec vec[RB_UIO_MAXIOV]; | |
713 | ||
714 | memset(vec, 0, sizeof(vec)); | |
715 | /* Check we actually have a first buffer */ | |
716 | if(bufhead->list.head == NULL) | |
717 | { | |
718 | /* nope, so we return none .. */ | |
719 | errno = EWOULDBLOCK; | |
720 | return -1; | |
721 | } | |
722 | ||
723 | ptr = bufhead->list.head; | |
94b4fbf9 | 724 | |
b57f37fb WP |
725 | bufline = ptr->data; |
726 | if(!bufline->terminated) | |
727 | { | |
728 | errno = EWOULDBLOCK; | |
729 | return -1; | |
730 | ||
731 | } | |
732 | ||
dcb90e0d JT |
733 | vec[x].iov_base = bufline->buf + bufhead->writeofs; |
734 | vec[x++].iov_len = bufline->len - bufhead->writeofs; | |
735 | ptr = ptr->next; | |
b57f37fb WP |
736 | |
737 | do | |
738 | { | |
739 | if(ptr == NULL) | |
740 | break; | |
741 | ||
742 | bufline = ptr->data; | |
743 | if(!bufline->terminated) | |
744 | break; | |
94b4fbf9 | 745 | |
b57f37fb WP |
746 | vec[x].iov_base = bufline->buf; |
747 | vec[x].iov_len = bufline->len; | |
748 | ptr = ptr->next; | |
749 | ||
94b4fbf9 VY |
750 | } |
751 | while(++x < RB_UIO_MAXIOV); | |
b57f37fb WP |
752 | |
753 | if(x == 0) | |
754 | { | |
755 | errno = EWOULDBLOCK; | |
756 | return -1; | |
757 | } | |
758 | ||
759 | xret = retval = rb_writev(F, vec, x); | |
760 | if(retval <= 0) | |
761 | return retval; | |
762 | ||
763 | ptr = bufhead->list.head; | |
764 | ||
765 | for(y = 0; y < x; y++) | |
766 | { | |
767 | bufline = ptr->data; | |
768 | ||
dcb90e0d | 769 | if(xret >= bufline->len - bufhead->writeofs) |
b57f37fb | 770 | { |
dcb90e0d | 771 | xret -= bufline->len - bufhead->writeofs; |
b57f37fb WP |
772 | ptr = ptr->next; |
773 | rb_linebuf_done_line(bufhead, bufline, bufhead->list.head); | |
dcb90e0d | 774 | bufhead->writeofs = 0; |
b57f37fb | 775 | } |
94b4fbf9 | 776 | else |
b57f37fb | 777 | { |
dcb90e0d | 778 | bufhead->writeofs += xret; |
b57f37fb WP |
779 | break; |
780 | } | |
b57f37fb WP |
781 | } |
782 | ||
783 | return retval; | |
784 | } | |
94b4fbf9 VY |
785 | #endif |
786 | ||
787 | /* this is the non-writev case */ | |
b57f37fb | 788 | |
b57f37fb WP |
789 | /* Check we actually have a first buffer */ |
790 | if(bufhead->list.head == NULL) | |
791 | { | |
792 | /* nope, so we return none .. */ | |
793 | errno = EWOULDBLOCK; | |
794 | return -1; | |
795 | } | |
796 | ||
797 | bufline = bufhead->list.head->data; | |
798 | ||
799 | /* And that its actually full .. */ | |
800 | if(!bufline->terminated) | |
801 | { | |
802 | errno = EWOULDBLOCK; | |
803 | return -1; | |
804 | } | |
805 | ||
b57f37fb WP |
806 | /* Now, try writing data */ |
807 | retval = rb_write(F, bufline->buf + bufhead->writeofs, bufline->len - bufhead->writeofs); | |
808 | ||
809 | if(retval <= 0) | |
810 | return retval; | |
811 | ||
812 | /* we've got data, so update the write offset */ | |
813 | bufhead->writeofs += retval; | |
814 | ||
815 | /* if we've written everything *and* the CRLF, deallocate and update | |
816 | bufhead */ | |
817 | if(bufhead->writeofs == bufline->len) | |
818 | { | |
819 | bufhead->writeofs = 0; | |
820 | lrb_assert(bufhead->len >= 0); | |
821 | rb_linebuf_done_line(bufhead, bufline, bufhead->list.head); | |
822 | } | |
823 | ||
824 | /* Return line length */ | |
825 | return retval; | |
826 | } | |
827 | ||
828 | ||
829 | ||
830 | /* | |
831 | * count linebufs for stats z | |
832 | */ | |
833 | ||
834 | void | |
94b4fbf9 | 835 | rb_count_rb_linebuf_memory(size_t *count, size_t *rb_linebuf_memory_used) |
b57f37fb | 836 | { |
b57f37fb | 837 | rb_bh_usage(rb_linebuf_heap, count, NULL, rb_linebuf_memory_used, NULL); |
b57f37fb | 838 | } |