]>
jfr.im git - solanum.git/blob - libratbox/src/linebuf.c
2 * ircd-ratbox: A slightly useful ircd.
3 * linebuf.c: Maintains linebuffers.
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
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.
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.
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
24 * $Id: linebuf.c 26092 2008-09-19 15:13:52Z androsyn $
27 #include <libratbox_config.h>
28 #include <ratbox_lib.h>
29 #include <commio-int.h>
32 static rb_bh
*rb_linebuf_heap
;
35 static int bufline_count
= 0;
37 #ifndef LINEBUF_HEAP_SIZE
38 #define LINEBUF_HEAP_SIZE 2048
44 * Initialise the linebuf mechanism
48 rb_linebuf_init(size_t heap_size
)
50 rb_linebuf_heap
= rb_bh_create(sizeof(buf_line_t
), heap_size
, "librb_linebuf_heap");
54 rb_linebuf_allocate(void)
57 t
= rb_bh_alloc(rb_linebuf_heap
);
63 rb_linebuf_free(buf_line_t
* p
)
65 rb_bh_free(rb_linebuf_heap
, p
);
71 * Create a new line, and link it to the given linebuf.
72 * It will be initially empty.
75 rb_linebuf_new_line(buf_head_t
* bufhead
)
80 bufline
= rb_linebuf_allocate();
86 node
= rb_make_rb_dlink_node();
88 /* Stick it at the end of the buf list */
89 rb_dlinkAddTail(bufline
, node
, &bufhead
->list
);
92 /* And finally, update the allocated size */
101 * rb_linebuf_done_line
103 * We've finished with the given line, so deallocate it
106 rb_linebuf_done_line(buf_head_t
* bufhead
, buf_line_t
* bufline
, rb_dlink_node
*node
)
108 /* Remove it from the linked list */
109 rb_dlinkDestroy(node
, &bufhead
->list
);
111 /* Update the allocated size */
113 bufhead
->len
-= bufline
->len
;
114 lrb_assert(bufhead
->len
>= 0);
118 lrb_assert(bufline
->refcount
>= 0);
120 if(bufline
->refcount
== 0)
122 /* and finally, deallocate the buf */
124 lrb_assert(bufline_count
>= 0);
125 rb_linebuf_free(bufline
);
131 * skip to end of line or the crlfs, return the number of bytes ..
134 rb_linebuf_skip_crlf(char *ch
, int len
)
138 /* First, skip until the first non-CRLF */
139 for(; len
; len
--, ch
++)
147 /* Then, skip until the last CRLF */
148 for(; len
; len
--, ch
++)
150 if((*ch
!= '\r') && (*ch
!= '\n'))
153 lrb_assert(orig_len
> len
);
154 return (orig_len
- len
);
162 * Initialise the new buffer
165 rb_linebuf_newbuf(buf_head_t
* bufhead
)
167 /* not much to do right now :) */
168 memset(bufhead
, 0, sizeof(buf_head_t
));
174 * Flush all the lines associated with this buffer
177 rb_linebuf_donebuf(buf_head_t
* bufhead
)
179 while(bufhead
->list
.head
!= NULL
)
181 rb_linebuf_done_line(bufhead
, (buf_line_t
*) bufhead
->list
.head
->data
,
187 * rb_linebuf_copy_line
189 * Okay..this functions comments made absolutely no sense.
191 * Basically what we do is this. Find the first chunk of text
192 * and then scan for a CRLF. If we didn't find it, but we didn't
193 * overflow our buffer..we wait for some more data.
194 * If we found a CRLF, we replace them with a \0 character.
195 * If we overflowed, we copy the most our buffer can handle, terminate
196 * it with a \0 and return.
198 * The return value is the amount of data we consumed. This could
199 * be different than the size of the linebuffer, as when we discard
200 * the overflow, we don't want to process it again.
202 * This still sucks in my opinion, but it seems to work.
207 rb_linebuf_copy_line(buf_head_t
* bufhead
, buf_line_t
* bufline
, char *data
, int len
)
209 int cpylen
= 0; /* how many bytes we've copied */
210 char *ch
= data
; /* Pointer to where we are in the read data */
211 char *bufch
= bufline
->buf
+ bufline
->len
;
212 int clen
= 0; /* how many bytes we've processed,
213 and don't ever want to see again.. */
215 /* If its full or terminated, ignore it */
218 lrb_assert(bufline
->len
< BUF_DATA_SIZE
);
219 if(bufline
->terminated
== 1)
222 clen
= cpylen
= rb_linebuf_skip_crlf(ch
, len
);
226 /* This is the ~overflow case..This doesn't happen often.. */
227 if(cpylen
> (BUF_DATA_SIZE
- bufline
->len
- 1))
229 memcpy(bufch
, ch
, (BUF_DATA_SIZE
- bufline
->len
- 1));
230 bufline
->buf
[BUF_DATA_SIZE
- 1] = '\0';
231 bufch
= bufline
->buf
+ BUF_DATA_SIZE
- 2;
232 while(cpylen
&& (*bufch
== '\r' || *bufch
== '\n'))
238 bufline
->terminated
= 1;
239 bufline
->len
= BUF_DATA_SIZE
- 1;
240 bufhead
->len
+= BUF_DATA_SIZE
- 1;
244 memcpy(bufch
, ch
, cpylen
);
249 if(*bufch
!= '\r' && *bufch
!= '\n')
251 /* No linefeed, bail for the next time */
252 bufhead
->len
+= cpylen
;
253 bufline
->len
+= cpylen
;
254 bufline
->terminated
= 0;
258 /* Yank the CRLF off this, replace with a \0 */
259 while(cpylen
&& (*bufch
== '\r' || *bufch
== '\n'))
266 bufline
->terminated
= 1;
267 bufhead
->len
+= cpylen
;
268 bufline
->len
+= cpylen
;
273 * rb_linebuf_copy_raw
275 * Copy as much data as possible directly into a linebuf,
276 * splitting at \r\n, but without altering any data.
280 rb_linebuf_copy_raw(buf_head_t
* bufhead
, buf_line_t
* bufline
, char *data
, int len
)
282 int cpylen
= 0; /* how many bytes we've copied */
283 char *ch
= data
; /* Pointer to where we are in the read data */
284 char *bufch
= bufline
->buf
+ bufline
->len
;
285 int clen
= 0; /* how many bytes we've processed,
286 and don't ever want to see again.. */
288 /* If its full or terminated, ignore it */
291 lrb_assert(bufline
->len
< BUF_DATA_SIZE
);
292 if(bufline
->terminated
== 1)
295 clen
= cpylen
= rb_linebuf_skip_crlf(ch
, len
);
299 /* This is the overflow case..This doesn't happen often.. */
300 if(cpylen
> (BUF_DATA_SIZE
- bufline
->len
- 1))
302 clen
= BUF_DATA_SIZE
- bufline
->len
- 1;
303 memcpy(bufch
, ch
, clen
);
304 bufline
->buf
[BUF_DATA_SIZE
- 1] = '\0';
305 bufch
= bufline
->buf
+ BUF_DATA_SIZE
- 2;
306 bufline
->terminated
= 1;
307 bufline
->len
= BUF_DATA_SIZE
- 1;
308 bufhead
->len
+= BUF_DATA_SIZE
- 1;
312 memcpy(bufch
, ch
, cpylen
);
317 if(*bufch
!= '\r' && *bufch
!= '\n')
319 /* No linefeed, bail for the next time */
320 bufhead
->len
+= cpylen
;
321 bufline
->len
+= cpylen
;
322 bufline
->terminated
= 0;
326 bufline
->terminated
= 1;
327 bufhead
->len
+= cpylen
;
328 bufline
->len
+= cpylen
;
336 * Take a given buffer and break out as many buffers as we can.
337 * If we find a CRLF, we terminate that buffer and create a new one.
338 * If we don't find a CRLF whilst parsing a buffer, we don't mark it
339 * 'finished', so the next loop through we can continue appending ..
341 * A few notes here, which you'll need to understand before continuing.
343 * - right now I'm only dealing with single sized buffers. Later on,
344 * I might consider chaining buffers together to get longer "lines"
345 * but seriously, I don't see the advantage right now.
347 * - This *is* designed to turn into a reference-counter-protected setup
348 * to dodge copious copies.
351 rb_linebuf_parse(buf_head_t
* bufhead
, char *data
, int len
, int raw
)
357 /* First, if we have a partial buffer, try to squeze data into it */
358 if(bufhead
->list
.tail
!= NULL
)
360 /* Check we're doing the partial buffer thing */
361 bufline
= bufhead
->list
.tail
->data
;
362 /* just try, the worst it could do is *reject* us .. */
364 cpylen
= rb_linebuf_copy_line(bufhead
, bufline
, data
, len
);
366 cpylen
= rb_linebuf_copy_raw(bufhead
, bufline
, data
, len
);
372 /* If we've copied the same as what we've got, quit now */
374 return linecnt
; /* all the data done so soon? */
376 /* Skip the data and update len .. */
378 lrb_assert(len
>= 0);
385 /* We obviously need a new buffer, so .. */
386 bufline
= rb_linebuf_new_line(bufhead
);
390 cpylen
= rb_linebuf_copy_line(bufhead
, bufline
, data
, len
);
392 cpylen
= rb_linebuf_copy_raw(bufhead
, bufline
, data
, len
);
398 lrb_assert(len
>= 0);
409 * get the next buffer from our line. For the time being it will copy
410 * data into the given buffer and free the underlying linebuf.
413 rb_linebuf_get(buf_head_t
* bufhead
, char *buf
, int buflen
, int partial
, int raw
)
419 /* make sure we have a line */
420 if(bufhead
->list
.head
== NULL
)
421 return 0; /* Obviously not.. hrm. */
423 bufline
= bufhead
->list
.head
->data
;
425 /* make sure that the buffer was actually *terminated */
426 if(!(partial
|| bufline
->terminated
))
427 return 0; /* Wait for more data! */
429 if(buflen
< bufline
->len
)
432 cpylen
= bufline
->len
;
435 start
= bufline
->buf
;
437 /* if we left extraneous '\r\n' characters in the string,
438 * and we don't want to read the raw data, clean up the string.
440 if(bufline
->raw
&& !raw
)
442 /* skip leading EOL characters */
443 while(cpylen
&& (*start
== '\r' || *start
== '\n'))
448 /* skip trailing EOL characters */
449 ch
= &start
[cpylen
- 1];
450 while(cpylen
&& (*ch
== '\r' || *ch
== '\n'))
457 memcpy(buf
, start
, cpylen
);
459 /* convert CR/LF to NULL */
463 lrb_assert(cpylen
>= 0);
465 /* Deallocate the line */
466 rb_linebuf_done_line(bufhead
, bufline
, bufhead
->list
.head
);
468 /* return how much we copied */
475 * attach the lines in a buf_head_t to another buf_head_t
476 * without copying the data (using refcounts).
479 rb_linebuf_attach(buf_head_t
* bufhead
, buf_head_t
* new)
484 RB_DLINK_FOREACH(ptr
, new->list
.head
)
487 rb_dlinkAddTailAlloc(line
, &bufhead
->list
);
489 /* Update the allocated size */
491 bufhead
->len
+= line
->len
;
503 * Similar to rb_linebuf_put, but designed for use by send.c.
505 * prefixfmt is used as a format for the varargs, and is inserted first.
506 * Then format/va_args is appended to the buffer.
509 rb_linebuf_putmsg(buf_head_t
* bufhead
, const char *format
, va_list * va_args
,
510 const char *prefixfmt
, ...)
516 /* make sure the previous line is terminated */
518 if(bufhead
->list
.tail
)
520 bufline
= bufhead
->list
.tail
->data
;
521 lrb_assert(bufline
->terminated
);
524 /* Create a new line */
525 bufline
= rb_linebuf_new_line(bufhead
);
527 if(prefixfmt
!= NULL
)
529 va_start(prefix_args
, prefixfmt
);
530 len
= rb_vsnprintf(bufline
->buf
, BUF_DATA_SIZE
, prefixfmt
, prefix_args
);
536 len
+= rb_vsnprintf((bufline
->buf
+ len
), (BUF_DATA_SIZE
- len
), format
, *va_args
);
539 bufline
->terminated
= 1;
541 /* Truncate the data if required */
542 if(rb_unlikely(len
> 510))
545 bufline
->buf
[len
++] = '\r';
546 bufline
->buf
[len
++] = '\n';
548 else if(rb_unlikely(len
== 0))
550 bufline
->buf
[len
++] = '\r';
551 bufline
->buf
[len
++] = '\n';
552 bufline
->buf
[len
] = '\0';
556 /* Chop trailing CRLF's .. */
557 while((bufline
->buf
[len
] == '\r') || (bufline
->buf
[len
] == '\n')
558 || (bufline
->buf
[len
] == '\0'))
563 bufline
->buf
[++len
] = '\r';
564 bufline
->buf
[++len
] = '\n';
565 bufline
->buf
[++len
] = '\0';
573 rb_linebuf_putbuf(buf_head_t
* bufhead
, const char *buffer
)
578 /* make sure the previous line is terminated */
580 if(bufhead
->list
.tail
)
582 bufline
= bufhead
->list
.tail
->data
;
583 lrb_assert(bufline
->terminated
);
586 /* Create a new line */
587 bufline
= rb_linebuf_new_line(bufhead
);
589 if(rb_unlikely(buffer
!= NULL
))
590 len
= rb_strlcpy(bufline
->buf
, buffer
, BUF_DATA_SIZE
);
592 bufline
->terminated
= 1;
594 /* Truncate the data if required */
595 if(rb_unlikely(len
> 510))
598 bufline
->buf
[len
++] = '\r';
599 bufline
->buf
[len
++] = '\n';
601 else if(rb_unlikely(len
== 0))
603 bufline
->buf
[len
++] = '\r';
604 bufline
->buf
[len
++] = '\n';
605 bufline
->buf
[len
] = '\0';
609 /* Chop trailing CRLF's .. */
610 while((bufline
->buf
[len
] == '\r') || (bufline
->buf
[len
] == '\n')
611 || (bufline
->buf
[len
] == '\0'))
616 bufline
->buf
[++len
] = '\r';
617 bufline
->buf
[++len
] = '\n';
618 bufline
->buf
[++len
] = '\0';
628 rb_linebuf_put(buf_head_t
* bufhead
, const char *format
, ...)
634 /* make sure the previous line is terminated */
636 if(bufhead
->list
.tail
)
638 bufline
= bufhead
->list
.tail
->data
;
639 lrb_assert(bufline
->terminated
);
642 /* Create a new line */
643 bufline
= rb_linebuf_new_line(bufhead
);
645 if(rb_unlikely(format
!= NULL
))
647 va_start(args
, format
);
648 len
= rb_vsnprintf(bufline
->buf
, BUF_DATA_SIZE
, format
, args
);
652 bufline
->terminated
= 1;
654 /* Truncate the data if required */
655 if(rb_unlikely(len
> 510))
658 bufline
->buf
[len
++] = '\r';
659 bufline
->buf
[len
++] = '\n';
661 else if(rb_unlikely(len
== 0))
663 bufline
->buf
[len
++] = '\r';
664 bufline
->buf
[len
++] = '\n';
665 bufline
->buf
[len
] = '\0';
669 /* Chop trailing CRLF's .. */
670 while((bufline
->buf
[len
] == '\r') || (bufline
->buf
[len
] == '\n')
671 || (bufline
->buf
[len
] == '\0'))
676 bufline
->buf
[++len
] = '\r';
677 bufline
->buf
[++len
] = '\n';
678 bufline
->buf
[++len
] = '\0';
690 * Flush data to the buffer. It tries to write as much data as possible
691 * to the given socket. Any return values are passed straight through.
692 * If there is no data in the socket, EWOULDBLOCK is set as an errno
693 * rather than returning 0 (which would map to an EOF..)
695 * Notes: XXX We *should* have a clue here when a non-full buffer is arrived.
696 * and tag it so that we don't re-schedule another write until
700 rb_linebuf_flush(rb_fde_t
*F
, buf_head_t
* bufhead
)
706 * autoconf checks for this..but really just want to use it if we have a
707 * native version even if libircd provides a fake version...
715 static struct rb_iovec vec
[RB_UIO_MAXIOV
];
717 memset(vec
, 0, sizeof(vec
));
718 /* Check we actually have a first buffer */
719 if(bufhead
->list
.head
== NULL
)
721 /* nope, so we return none .. */
726 ptr
= bufhead
->list
.head
;
729 if(!bufline
->terminated
)
736 vec
[x
].iov_base
= bufline
->buf
+ bufhead
->writeofs
;
737 vec
[x
++].iov_len
= bufline
->len
- bufhead
->writeofs
;
746 if(!bufline
->terminated
)
749 vec
[x
].iov_base
= bufline
->buf
;
750 vec
[x
].iov_len
= bufline
->len
;
754 while(++x
< RB_UIO_MAXIOV
);
762 xret
= retval
= rb_writev(F
, vec
, x
);
766 ptr
= bufhead
->list
.head
;
768 for(y
= 0; y
< x
; y
++)
772 if(xret
>= bufline
->len
- bufhead
->writeofs
)
774 xret
-= bufline
->len
- bufhead
->writeofs
;
776 rb_linebuf_done_line(bufhead
, bufline
, bufhead
->list
.head
);
777 bufhead
->writeofs
= 0;
781 bufhead
->writeofs
+= xret
;
790 /* this is the non-writev case */
792 /* Check we actually have a first buffer */
793 if(bufhead
->list
.head
== NULL
)
795 /* nope, so we return none .. */
800 bufline
= bufhead
->list
.head
->data
;
802 /* And that its actually full .. */
803 if(!bufline
->terminated
)
809 /* Now, try writing data */
810 retval
= rb_write(F
, bufline
->buf
+ bufhead
->writeofs
, bufline
->len
- bufhead
->writeofs
);
815 /* we've got data, so update the write offset */
816 bufhead
->writeofs
+= retval
;
818 /* if we've written everything *and* the CRLF, deallocate and update
820 if(bufhead
->writeofs
== bufline
->len
)
822 bufhead
->writeofs
= 0;
823 lrb_assert(bufhead
->len
>= 0);
824 rb_linebuf_done_line(bufhead
, bufline
, bufhead
->list
.head
);
827 /* Return line length */
834 * count linebufs for stats z
838 rb_count_rb_linebuf_memory(size_t *count
, size_t *rb_linebuf_memory_used
)
840 rb_bh_usage(rb_linebuf_heap
, count
, NULL
, rb_linebuf_memory_used
, NULL
);