]>
jfr.im git - solanum.git/blob - librb/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
26 #include <librb_config.h>
28 #include <commio-int.h>
30 static rb_bh
*rb_linebuf_heap
;
32 static int bufline_count
= 0;
34 #ifndef LINEBUF_HEAP_SIZE
35 #define LINEBUF_HEAP_SIZE 2048
41 * Initialise the linebuf mechanism
45 rb_linebuf_init(size_t heap_size
)
47 rb_linebuf_heap
= rb_bh_create(sizeof(buf_line_t
), heap_size
, "librb_linebuf_heap");
51 rb_linebuf_allocate(void)
54 t
= rb_bh_alloc(rb_linebuf_heap
);
60 rb_linebuf_free(buf_line_t
* p
)
62 rb_bh_free(rb_linebuf_heap
, p
);
68 * Create a new line, and link it to the given linebuf.
69 * It will be initially empty.
72 rb_linebuf_new_line(buf_head_t
* bufhead
)
77 bufline
= rb_linebuf_allocate();
83 node
= rb_make_rb_dlink_node();
85 /* Stick it at the end of the buf list */
86 rb_dlinkAddTail(bufline
, node
, &bufhead
->list
);
89 /* And finally, update the allocated size */
98 * rb_linebuf_done_line
100 * We've finished with the given line, so deallocate it
103 rb_linebuf_done_line(buf_head_t
* bufhead
, buf_line_t
* bufline
, rb_dlink_node
*node
)
105 /* Remove it from the linked list */
106 rb_dlinkDestroy(node
, &bufhead
->list
);
108 /* Update the allocated size */
110 bufhead
->len
-= bufline
->len
;
111 lrb_assert(bufhead
->len
>= 0);
115 lrb_assert(bufline
->refcount
>= 0);
117 if(bufline
->refcount
== 0)
119 /* and finally, deallocate the buf */
121 lrb_assert(bufline_count
>= 0);
122 rb_linebuf_free(bufline
);
128 * skip to end of line or the crlfs, return the number of bytes ..
131 rb_linebuf_skip_crlf(char *ch
, int len
)
135 /* First, skip until the first non-CRLF */
136 for(; len
; len
--, ch
++)
144 /* Then, skip until the last CRLF */
145 for(; len
; len
--, ch
++)
147 if((*ch
!= '\r') && (*ch
!= '\n'))
150 lrb_assert(orig_len
> len
);
151 return (orig_len
- len
);
159 * Initialise the new buffer
162 rb_linebuf_newbuf(buf_head_t
* bufhead
)
164 /* not much to do right now :) */
165 memset(bufhead
, 0, sizeof(buf_head_t
));
171 * Flush all the lines associated with this buffer
174 rb_linebuf_donebuf(buf_head_t
* bufhead
)
176 while(bufhead
->list
.head
!= NULL
)
178 rb_linebuf_done_line(bufhead
, (buf_line_t
*) bufhead
->list
.head
->data
,
184 * rb_linebuf_copy_line
186 * Okay..this functions comments made absolutely no sense.
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.
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.
199 * This still sucks in my opinion, but it seems to work.
204 rb_linebuf_copy_line(buf_head_t
* bufhead
, buf_line_t
* bufline
, char *data
, int len
)
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.. */
212 /* If its full or terminated, ignore it */
215 lrb_assert(bufline
->len
< BUF_DATA_SIZE
);
216 if(bufline
->terminated
== 1)
219 clen
= cpylen
= rb_linebuf_skip_crlf(ch
, len
);
223 /* This is the ~overflow case..This doesn't happen often.. */
224 if(cpylen
> (BUF_DATA_SIZE
- bufline
->len
- 1))
226 cpylen
= BUF_DATA_SIZE
- bufline
->len
- 1;
227 memcpy(bufch
, ch
, cpylen
);
228 bufline
->buf
[BUF_DATA_SIZE
- 1] = '\0';
229 bufch
= bufline
->buf
+ BUF_DATA_SIZE
- 2;
230 while(cpylen
&& (*bufch
== '\r' || *bufch
== '\n'))
236 bufline
->terminated
= 1;
237 bufline
->len
= BUF_DATA_SIZE
- 1;
238 bufhead
->len
+= BUF_DATA_SIZE
- 1;
242 memcpy(bufch
, ch
, cpylen
);
247 if(*bufch
!= '\r' && *bufch
!= '\n')
249 /* No linefeed, bail for the next time */
250 bufhead
->len
+= cpylen
;
251 bufline
->len
+= cpylen
;
252 bufline
->terminated
= 0;
256 /* Yank the CRLF off this, replace with a \0 */
257 while(cpylen
&& (*bufch
== '\r' || *bufch
== '\n'))
264 bufline
->terminated
= 1;
265 bufhead
->len
+= cpylen
;
266 bufline
->len
+= cpylen
;
271 * rb_linebuf_copy_raw
273 * Copy as much data as possible directly into a linebuf,
274 * splitting at \r\n, but without altering any data.
278 rb_linebuf_copy_raw(buf_head_t
* bufhead
, buf_line_t
* bufline
, char *data
, int len
)
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.. */
286 /* If its full or terminated, ignore it */
289 lrb_assert(bufline
->len
< BUF_DATA_SIZE
);
290 if(bufline
->terminated
== 1)
293 clen
= cpylen
= rb_linebuf_skip_crlf(ch
, len
);
297 /* This is the overflow case..This doesn't happen often.. */
298 if(cpylen
> (BUF_DATA_SIZE
- bufline
->len
- 1))
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;
310 memcpy(bufch
, ch
, cpylen
);
315 if(*bufch
!= '\r' && *bufch
!= '\n')
317 /* No linefeed, bail for the next time */
318 bufhead
->len
+= cpylen
;
319 bufline
->len
+= cpylen
;
320 bufline
->terminated
= 0;
324 bufline
->terminated
= 1;
325 bufhead
->len
+= cpylen
;
326 bufline
->len
+= cpylen
;
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 ..
339 * A few notes here, which you'll need to understand before continuing.
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.
345 * - This *is* designed to turn into a reference-counter-protected setup
346 * to dodge copious copies.
349 rb_linebuf_parse(buf_head_t
* bufhead
, char *data
, int len
, int raw
)
355 /* First, if we have a partial buffer, try to squeze data into it */
356 if(bufhead
->list
.tail
!= NULL
)
358 /* Check we're doing the partial buffer thing */
359 bufline
= bufhead
->list
.tail
->data
;
360 /* just try, the worst it could do is *reject* us .. */
362 cpylen
= rb_linebuf_copy_line(bufhead
, bufline
, data
, len
);
364 cpylen
= rb_linebuf_copy_raw(bufhead
, bufline
, data
, len
);
370 /* If we've copied the same as what we've got, quit now */
372 return linecnt
; /* all the data done so soon? */
374 /* Skip the data and update len .. */
376 lrb_assert(len
>= 0);
383 /* We obviously need a new buffer, so .. */
384 bufline
= rb_linebuf_new_line(bufhead
);
388 cpylen
= rb_linebuf_copy_line(bufhead
, bufline
, data
, len
);
390 cpylen
= rb_linebuf_copy_raw(bufhead
, bufline
, data
, len
);
396 lrb_assert(len
>= 0);
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.
411 rb_linebuf_get(buf_head_t
* bufhead
, char *buf
, int buflen
, int partial
, int raw
)
417 /* make sure we have a line */
418 if(bufhead
->list
.head
== NULL
)
419 return 0; /* Obviously not.. hrm. */
421 bufline
= bufhead
->list
.head
->data
;
423 /* make sure that the buffer was actually *terminated */
424 if(!(partial
|| bufline
->terminated
))
425 return 0; /* Wait for more data! */
427 if(buflen
< bufline
->len
)
430 cpylen
= bufline
->len
;
433 start
= bufline
->buf
;
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.
438 if(bufline
->raw
&& !raw
)
440 /* skip leading EOL characters */
441 while(cpylen
&& (*start
== '\r' || *start
== '\n'))
446 /* skip trailing EOL characters */
447 ch
= &start
[cpylen
- 1];
448 while(cpylen
&& (*ch
== '\r' || *ch
== '\n'))
455 memcpy(buf
, start
, cpylen
);
457 /* convert CR/LF to NULL */
461 lrb_assert(cpylen
>= 0);
463 /* Deallocate the line */
464 rb_linebuf_done_line(bufhead
, bufline
, bufhead
->list
.head
);
466 /* return how much we copied */
473 * attach the lines in a buf_head_t to another buf_head_t
474 * without copying the data (using refcounts).
477 rb_linebuf_attach(buf_head_t
* bufhead
, buf_head_t
* new)
482 RB_DLINK_FOREACH(ptr
, new->list
.head
)
485 rb_dlinkAddTailAlloc(line
, &bufhead
->list
);
487 /* Update the allocated size */
489 bufhead
->len
+= line
->len
;
499 * Similar to rb_linebuf_put, but designed for use by send.c.
501 * prefixfmt is used as a format for the varargs, and is inserted first.
502 * Then format/va_args is appended to the buffer.
505 rb_linebuf_putmsg(buf_head_t
* bufhead
, const char *format
, va_list * va_args
,
506 const char *prefixfmt
, ...)
512 /* make sure the previous line is terminated */
514 if(bufhead
->list
.tail
)
516 bufline
= bufhead
->list
.tail
->data
;
517 lrb_assert(bufline
->terminated
);
520 /* Create a new line */
521 bufline
= rb_linebuf_new_line(bufhead
);
523 if(prefixfmt
!= NULL
)
525 va_start(prefix_args
, prefixfmt
);
526 len
= vsnprintf(bufline
->buf
, BUF_DATA_SIZE
, prefixfmt
, prefix_args
);
532 len
+= vsnprintf((bufline
->buf
+ len
), (BUF_DATA_SIZE
- len
), format
, *va_args
);
535 bufline
->terminated
= 1;
537 /* Truncate the data if required */
538 if(rb_unlikely(len
> 510))
541 bufline
->buf
[len
++] = '\r';
542 bufline
->buf
[len
++] = '\n';
544 else if(rb_unlikely(len
== 0))
546 bufline
->buf
[len
++] = '\r';
547 bufline
->buf
[len
++] = '\n';
548 bufline
->buf
[len
] = '\0';
552 /* Chop trailing CRLF's .. */
553 while((bufline
->buf
[len
] == '\r') || (bufline
->buf
[len
] == '\n')
554 || (bufline
->buf
[len
] == '\0'))
559 bufline
->buf
[++len
] = '\r';
560 bufline
->buf
[++len
] = '\n';
561 bufline
->buf
[++len
] = '\0';
569 * rb_linebuf_putprefix
571 * Similar to rb_linebuf_put, but designed for use by send.c.
573 * prefix is inserted first, then format/va_args is appended to the buffer.
576 rb_linebuf_putprefix(buf_head_t
* bufhead
, const char *format
, va_list * va_args
,
582 /* make sure the previous line is terminated */
584 if(bufhead
->list
.tail
)
586 bufline
= bufhead
->list
.tail
->data
;
587 lrb_assert(bufline
->terminated
);
590 /* Create a new line */
591 bufline
= rb_linebuf_new_line(bufhead
);
594 len
= rb_strlcpy(bufline
->buf
, prefix
, BUF_DATA_SIZE
);
598 len
+= vsnprintf((bufline
->buf
+ len
), (BUF_DATA_SIZE
- len
), format
, *va_args
);
601 bufline
->terminated
= 1;
603 /* Truncate the data if required */
604 if(rb_unlikely(len
> 510))
607 bufline
->buf
[len
++] = '\r';
608 bufline
->buf
[len
++] = '\n';
610 else if(rb_unlikely(len
== 0))
612 bufline
->buf
[len
++] = '\r';
613 bufline
->buf
[len
++] = '\n';
614 bufline
->buf
[len
] = '\0';
618 /* Chop trailing CRLF's .. */
619 while((bufline
->buf
[len
] == '\r') || (bufline
->buf
[len
] == '\n')
620 || (bufline
->buf
[len
] == '\0'))
625 bufline
->buf
[++len
] = '\r';
626 bufline
->buf
[++len
] = '\n';
627 bufline
->buf
[++len
] = '\0';
635 rb_linebuf_putbuf(buf_head_t
* bufhead
, const char *buffer
)
640 /* make sure the previous line is terminated */
642 if(bufhead
->list
.tail
)
644 bufline
= bufhead
->list
.tail
->data
;
645 lrb_assert(bufline
->terminated
);
648 /* Create a new line */
649 bufline
= rb_linebuf_new_line(bufhead
);
651 if(rb_unlikely(buffer
!= NULL
))
652 len
= rb_strlcpy(bufline
->buf
, buffer
, BUF_DATA_SIZE
);
654 bufline
->terminated
= 1;
656 /* Truncate the data if required */
657 if(rb_unlikely(len
> 510))
660 bufline
->buf
[len
++] = '\r';
661 bufline
->buf
[len
++] = '\n';
663 else if(rb_unlikely(len
== 0))
665 bufline
->buf
[len
++] = '\r';
666 bufline
->buf
[len
++] = '\n';
667 bufline
->buf
[len
] = '\0';
671 /* Chop trailing CRLF's .. */
672 while((bufline
->buf
[len
] == '\r') || (bufline
->buf
[len
] == '\n')
673 || (bufline
->buf
[len
] == '\0'))
678 bufline
->buf
[++len
] = '\r';
679 bufline
->buf
[++len
] = '\n';
680 bufline
->buf
[++len
] = '\0';
690 rb_linebuf_put(buf_head_t
* bufhead
, const char *format
, ...)
696 /* make sure the previous line is terminated */
698 if(bufhead
->list
.tail
)
700 bufline
= bufhead
->list
.tail
->data
;
701 lrb_assert(bufline
->terminated
);
704 /* Create a new line */
705 bufline
= rb_linebuf_new_line(bufhead
);
707 if(rb_unlikely(format
!= NULL
))
709 va_start(args
, format
);
710 len
= vsnprintf(bufline
->buf
, BUF_DATA_SIZE
, format
, args
);
714 bufline
->terminated
= 1;
716 /* Truncate the data if required */
717 if(rb_unlikely(len
> 510))
720 bufline
->buf
[len
++] = '\r';
721 bufline
->buf
[len
++] = '\n';
723 else if(rb_unlikely(len
== 0))
725 bufline
->buf
[len
++] = '\r';
726 bufline
->buf
[len
++] = '\n';
727 bufline
->buf
[len
] = '\0';
731 /* Chop trailing CRLF's .. */
732 while((bufline
->buf
[len
] == '\r') || (bufline
->buf
[len
] == '\n')
733 || (bufline
->buf
[len
] == '\0'))
738 bufline
->buf
[++len
] = '\r';
739 bufline
->buf
[++len
] = '\n';
740 bufline
->buf
[++len
] = '\0';
752 * Flush data to the buffer. It tries to write as much data as possible
753 * to the given socket. Any return values are passed straight through.
754 * If there is no data in the socket, EWOULDBLOCK is set as an errno
755 * rather than returning 0 (which would map to an EOF..)
757 * Notes: XXX We *should* have a clue here when a non-full buffer is arrived.
758 * and tag it so that we don't re-schedule another write until
762 rb_linebuf_flush(rb_fde_t
*F
, buf_head_t
* bufhead
)
768 * autoconf checks for this..but really just want to use it if we have a
769 * native version even if libircd provides a fake version...
777 static struct rb_iovec vec
[RB_UIO_MAXIOV
];
779 memset(vec
, 0, sizeof(vec
));
780 /* Check we actually have a first buffer */
781 if(bufhead
->list
.head
== NULL
)
783 /* nope, so we return none .. */
788 ptr
= bufhead
->list
.head
;
791 if(!bufline
->terminated
)
798 vec
[x
].iov_base
= bufline
->buf
+ bufhead
->writeofs
;
799 vec
[x
++].iov_len
= bufline
->len
- bufhead
->writeofs
;
808 if(!bufline
->terminated
)
811 vec
[x
].iov_base
= bufline
->buf
;
812 vec
[x
].iov_len
= bufline
->len
;
816 while(++x
< RB_UIO_MAXIOV
);
824 xret
= retval
= rb_writev(F
, vec
, x
);
828 ptr
= bufhead
->list
.head
;
830 for(y
= 0; y
< x
; y
++)
834 if(xret
>= bufline
->len
- bufhead
->writeofs
)
836 xret
-= bufline
->len
- bufhead
->writeofs
;
838 rb_linebuf_done_line(bufhead
, bufline
, bufhead
->list
.head
);
839 bufhead
->writeofs
= 0;
843 bufhead
->writeofs
+= xret
;
852 /* this is the non-writev case */
854 /* Check we actually have a first buffer */
855 if(bufhead
->list
.head
== NULL
)
857 /* nope, so we return none .. */
862 bufline
= bufhead
->list
.head
->data
;
864 /* And that its actually full .. */
865 if(!bufline
->terminated
)
871 /* Now, try writing data */
872 retval
= rb_write(F
, bufline
->buf
+ bufhead
->writeofs
, bufline
->len
- bufhead
->writeofs
);
877 /* we've got data, so update the write offset */
878 bufhead
->writeofs
+= retval
;
880 /* if we've written everything *and* the CRLF, deallocate and update
882 if(bufhead
->writeofs
== bufline
->len
)
884 bufhead
->writeofs
= 0;
885 lrb_assert(bufhead
->len
>= 0);
886 rb_linebuf_done_line(bufhead
, bufline
, bufhead
->list
.head
);
889 /* Return line length */
896 * count linebufs for stats z
900 rb_count_rb_linebuf_memory(size_t *count
, size_t *rb_linebuf_memory_used
)
902 rb_bh_usage(rb_linebuf_heap
, count
, NULL
, rb_linebuf_memory_used
, NULL
);