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