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