]>
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
)
76 bufline
= rb_linebuf_allocate();
81 /* Stick it at the end of the buf list */
82 rb_dlinkAddTailAlloc(bufline
, &bufhead
->list
);
85 /* And finally, update the allocated size */
94 * rb_linebuf_done_line
96 * We've finished with the given line, so deallocate it
99 rb_linebuf_done_line(buf_head_t
* bufhead
, buf_line_t
* bufline
, rb_dlink_node
*node
)
101 /* Remove it from the linked list */
102 rb_dlinkDestroy(node
, &bufhead
->list
);
104 /* Update the allocated size */
106 bufhead
->len
-= bufline
->len
;
107 lrb_assert(bufhead
->len
>= 0);
111 lrb_assert(bufline
->refcount
>= 0);
113 if(bufline
->refcount
== 0)
115 /* and finally, deallocate the buf */
117 lrb_assert(bufline_count
>= 0);
118 rb_linebuf_free(bufline
);
124 * skip to end of line or the crlfs, return the number of bytes ..
127 rb_linebuf_skip_crlf(char *ch
, int len
)
131 /* First, skip until the first non-CRLF */
132 for(; len
; len
--, ch
++)
140 /* Then, skip until the last CRLF */
141 for(; len
; len
--, ch
++)
143 if((*ch
!= '\r') && (*ch
!= '\n'))
146 lrb_assert(orig_len
> len
);
147 return (orig_len
- len
);
155 * Initialise the new buffer
158 rb_linebuf_newbuf(buf_head_t
* bufhead
)
160 /* not much to do right now :) */
161 memset(bufhead
, 0, sizeof(buf_head_t
));
167 * Flush all the lines associated with this buffer
170 rb_linebuf_donebuf(buf_head_t
* bufhead
)
172 while(bufhead
->list
.head
!= NULL
)
174 rb_linebuf_done_line(bufhead
, (buf_line_t
*) bufhead
->list
.head
->data
,
180 * rb_linebuf_copy_line
182 * Okay..this functions comments made absolutely no sense.
184 * Basically what we do is this. Find the first chunk of text
185 * and then scan for a CRLF. If we didn't find it, but we didn't
186 * overflow our buffer..we wait for some more data.
187 * If we found a CRLF, we replace them with a \0 character.
188 * If we overflowed, we copy the most our buffer can handle, terminate
189 * it with a \0 and return.
191 * The return value is the amount of data we consumed. This could
192 * be different than the size of the linebuffer, as when we discard
193 * the overflow, we don't want to process it again.
195 * This still sucks in my opinion, but it seems to work.
200 rb_linebuf_copy_line(buf_head_t
* bufhead
, buf_line_t
* bufline
, char *data
, int len
)
202 int cpylen
= 0; /* how many bytes we've copied */
203 char *ch
= data
; /* Pointer to where we are in the read data */
204 char *bufch
= bufline
->buf
+ bufline
->len
;
205 int clen
= 0; /* how many bytes we've processed,
206 and don't ever want to see again.. */
208 /* If its full or terminated, ignore it */
211 lrb_assert(bufline
->len
< BUF_DATA_SIZE
);
212 if(bufline
->terminated
== 1)
215 clen
= cpylen
= rb_linebuf_skip_crlf(ch
, len
);
219 /* This is the ~overflow case..This doesn't happen often.. */
220 if(cpylen
> (BUF_DATA_SIZE
- bufline
->len
- 1))
222 cpylen
= BUF_DATA_SIZE
- bufline
->len
- 1;
223 memcpy(bufch
, ch
, cpylen
);
224 bufline
->buf
[BUF_DATA_SIZE
- 1] = '\0';
225 bufch
= bufline
->buf
+ BUF_DATA_SIZE
- 2;
226 while(cpylen
&& (*bufch
== '\r' || *bufch
== '\n'))
232 bufline
->terminated
= 1;
233 bufline
->len
= BUF_DATA_SIZE
- 1;
234 bufhead
->len
+= BUF_DATA_SIZE
- 1;
238 memcpy(bufch
, ch
, cpylen
);
243 if(*bufch
!= '\r' && *bufch
!= '\n')
245 /* No linefeed, bail for the next time */
246 bufhead
->len
+= cpylen
;
247 bufline
->len
+= cpylen
;
248 bufline
->terminated
= 0;
252 /* Yank the CRLF off this, replace with a \0 */
253 while(cpylen
&& (*bufch
== '\r' || *bufch
== '\n'))
260 bufline
->terminated
= 1;
261 bufhead
->len
+= cpylen
;
262 bufline
->len
+= cpylen
;
267 * rb_linebuf_copy_raw
269 * Copy as much data as possible directly into a linebuf,
270 * splitting at \r\n, but without altering any data.
274 rb_linebuf_copy_raw(buf_head_t
* bufhead
, buf_line_t
* bufline
, char *data
, int len
)
276 int cpylen
= 0; /* how many bytes we've copied */
277 char *ch
= data
; /* Pointer to where we are in the read data */
278 char *bufch
= bufline
->buf
+ bufline
->len
;
279 int clen
= 0; /* how many bytes we've processed,
280 and don't ever want to see again.. */
282 /* If its full or terminated, ignore it */
285 lrb_assert(bufline
->len
< BUF_DATA_SIZE
);
286 if(bufline
->terminated
== 1)
289 clen
= cpylen
= rb_linebuf_skip_crlf(ch
, len
);
293 /* This is the overflow case..This doesn't happen often.. */
294 if(cpylen
> (BUF_DATA_SIZE
- bufline
->len
- 1))
296 clen
= BUF_DATA_SIZE
- bufline
->len
- 1;
297 memcpy(bufch
, ch
, clen
);
298 bufline
->buf
[BUF_DATA_SIZE
- 1] = '\0';
299 bufch
= bufline
->buf
+ BUF_DATA_SIZE
- 2;
300 bufline
->terminated
= 1;
301 bufline
->len
= BUF_DATA_SIZE
- 1;
302 bufhead
->len
+= BUF_DATA_SIZE
- 1;
306 memcpy(bufch
, ch
, cpylen
);
311 if(*bufch
!= '\r' && *bufch
!= '\n')
313 /* No linefeed, bail for the next time */
314 bufhead
->len
+= cpylen
;
315 bufline
->len
+= cpylen
;
316 bufline
->terminated
= 0;
320 bufline
->terminated
= 1;
321 bufhead
->len
+= cpylen
;
322 bufline
->len
+= cpylen
;
330 * Take a given buffer and break out as many buffers as we can.
331 * If we find a CRLF, we terminate that buffer and create a new one.
332 * If we don't find a CRLF whilst parsing a buffer, we don't mark it
333 * 'finished', so the next loop through we can continue appending ..
335 * A few notes here, which you'll need to understand before continuing.
337 * - right now I'm only dealing with single sized buffers. Later on,
338 * I might consider chaining buffers together to get longer "lines"
339 * but seriously, I don't see the advantage right now.
341 * - This *is* designed to turn into a reference-counter-protected setup
342 * to dodge copious copies.
345 rb_linebuf_parse(buf_head_t
* bufhead
, char *data
, int len
, int raw
)
351 /* First, if we have a partial buffer, try to squeze data into it */
352 if(bufhead
->list
.tail
!= NULL
)
354 /* Check we're doing the partial buffer thing */
355 bufline
= bufhead
->list
.tail
->data
;
356 /* just try, the worst it could do is *reject* us .. */
358 cpylen
= rb_linebuf_copy_line(bufhead
, bufline
, data
, len
);
360 cpylen
= rb_linebuf_copy_raw(bufhead
, bufline
, data
, len
);
366 /* If we've copied the same as what we've got, quit now */
368 return linecnt
; /* all the data done so soon? */
370 /* Skip the data and update len .. */
372 lrb_assert(len
>= 0);
379 /* We obviously need a new buffer, so .. */
380 bufline
= rb_linebuf_new_line(bufhead
);
384 cpylen
= rb_linebuf_copy_line(bufhead
, bufline
, data
, len
);
386 cpylen
= rb_linebuf_copy_raw(bufhead
, bufline
, data
, len
);
392 lrb_assert(len
>= 0);
403 * get the next buffer from our line. For the time being it will copy
404 * data into the given buffer and free the underlying linebuf.
407 rb_linebuf_get(buf_head_t
* bufhead
, char *buf
, int buflen
, int partial
, int raw
)
413 /* make sure we have a line */
414 if(bufhead
->list
.head
== NULL
)
415 return 0; /* Obviously not.. hrm. */
417 bufline
= bufhead
->list
.head
->data
;
419 /* make sure that the buffer was actually *terminated */
420 if(!(partial
|| bufline
->terminated
))
421 return 0; /* Wait for more data! */
423 if(buflen
< bufline
->len
)
426 cpylen
= bufline
->len
;
429 start
= bufline
->buf
;
431 /* if we left extraneous '\r\n' characters in the string,
432 * and we don't want to read the raw data, clean up the string.
434 if(bufline
->raw
&& !raw
)
436 /* skip leading EOL characters */
437 while(cpylen
&& (*start
== '\r' || *start
== '\n'))
442 /* skip trailing EOL characters */
443 ch
= &start
[cpylen
- 1];
444 while(cpylen
&& (*ch
== '\r' || *ch
== '\n'))
451 memcpy(buf
, start
, cpylen
);
453 /* convert CR/LF to NULL */
457 lrb_assert(cpylen
>= 0);
459 /* Deallocate the line */
460 rb_linebuf_done_line(bufhead
, bufline
, bufhead
->list
.head
);
462 /* return how much we copied */
469 * attach the lines in a buf_head_t to another buf_head_t
470 * without copying the data (using refcounts).
473 rb_linebuf_attach(buf_head_t
* bufhead
, buf_head_t
* new)
478 RB_DLINK_FOREACH(ptr
, new->list
.head
)
481 rb_dlinkAddTailAlloc(line
, &bufhead
->list
);
483 /* Update the allocated size */
485 bufhead
->len
+= line
->len
;
495 * Similar to rb_linebuf_put, but designed for use by send.c.
497 * prefixfmt is used as a format for the varargs, and is inserted first.
498 * Then format/va_args is appended to the buffer.
501 rb_linebuf_putmsg(buf_head_t
* bufhead
, const char *format
, va_list * va_args
,
502 const char *prefixfmt
, ...)
508 /* make sure the previous line is terminated */
510 if(bufhead
->list
.tail
)
512 bufline
= bufhead
->list
.tail
->data
;
513 lrb_assert(bufline
->terminated
);
516 /* Create a new line */
517 bufline
= rb_linebuf_new_line(bufhead
);
519 if(prefixfmt
!= NULL
)
521 va_start(prefix_args
, prefixfmt
);
522 len
= vsnprintf(bufline
->buf
, BUF_DATA_SIZE
, prefixfmt
, prefix_args
);
528 len
+= vsnprintf((bufline
->buf
+ len
), (BUF_DATA_SIZE
- len
), format
, *va_args
);
531 bufline
->terminated
= 1;
533 /* Truncate the data if required */
534 if(rb_unlikely(len
> 510))
537 bufline
->buf
[len
++] = '\r';
538 bufline
->buf
[len
++] = '\n';
540 else if(rb_unlikely(len
== 0))
542 bufline
->buf
[len
++] = '\r';
543 bufline
->buf
[len
++] = '\n';
544 bufline
->buf
[len
] = '\0';
548 /* Chop trailing CRLF's .. */
549 while((bufline
->buf
[len
] == '\r') || (bufline
->buf
[len
] == '\n')
550 || (bufline
->buf
[len
] == '\0'))
555 bufline
->buf
[++len
] = '\r';
556 bufline
->buf
[++len
] = '\n';
557 bufline
->buf
[++len
] = '\0';
565 * rb_linebuf_putprefix
567 * Similar to rb_linebuf_put, but designed for use by send.c.
569 * prefix is inserted first, then format/va_args is appended to the buffer.
572 rb_linebuf_putprefix(buf_head_t
* bufhead
, const char *format
, va_list * va_args
,
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
);
590 len
= rb_strlcpy(bufline
->buf
, prefix
, BUF_DATA_SIZE
);
594 len
+= vsnprintf((bufline
->buf
+ len
), (BUF_DATA_SIZE
- len
), format
, *va_args
);
597 bufline
->terminated
= 1;
599 /* Truncate the data if required */
600 if(rb_unlikely(len
> 510))
603 bufline
->buf
[len
++] = '\r';
604 bufline
->buf
[len
++] = '\n';
606 else if(rb_unlikely(len
== 0))
608 bufline
->buf
[len
++] = '\r';
609 bufline
->buf
[len
++] = '\n';
610 bufline
->buf
[len
] = '\0';
614 /* Chop trailing CRLF's .. */
615 while((bufline
->buf
[len
] == '\r') || (bufline
->buf
[len
] == '\n')
616 || (bufline
->buf
[len
] == '\0'))
621 bufline
->buf
[++len
] = '\r';
622 bufline
->buf
[++len
] = '\n';
623 bufline
->buf
[++len
] = '\0';
631 rb_linebuf_putbuf(buf_head_t
* bufhead
, const char *buffer
)
636 /* make sure the previous line is terminated */
638 if(bufhead
->list
.tail
)
640 bufline
= bufhead
->list
.tail
->data
;
641 lrb_assert(bufline
->terminated
);
644 /* Create a new line */
645 bufline
= rb_linebuf_new_line(bufhead
);
647 if(rb_unlikely(buffer
!= NULL
))
648 len
= rb_strlcpy(bufline
->buf
, buffer
, BUF_DATA_SIZE
);
650 bufline
->terminated
= 1;
652 /* Truncate the data if required */
653 if(rb_unlikely(len
> 510))
656 bufline
->buf
[len
++] = '\r';
657 bufline
->buf
[len
++] = '\n';
659 else if(rb_unlikely(len
== 0))
661 bufline
->buf
[len
++] = '\r';
662 bufline
->buf
[len
++] = '\n';
663 bufline
->buf
[len
] = '\0';
667 /* Chop trailing CRLF's .. */
668 while((bufline
->buf
[len
] == '\r') || (bufline
->buf
[len
] == '\n')
669 || (bufline
->buf
[len
] == '\0'))
674 bufline
->buf
[++len
] = '\r';
675 bufline
->buf
[++len
] = '\n';
676 bufline
->buf
[++len
] = '\0';
686 rb_linebuf_put(buf_head_t
* bufhead
, const char *format
, ...)
692 /* make sure the previous line is terminated */
694 if(bufhead
->list
.tail
)
696 bufline
= bufhead
->list
.tail
->data
;
697 lrb_assert(bufline
->terminated
);
700 /* Create a new line */
701 bufline
= rb_linebuf_new_line(bufhead
);
703 if(rb_unlikely(format
!= NULL
))
705 va_start(args
, format
);
706 len
= vsnprintf(bufline
->buf
, BUF_DATA_SIZE
, format
, args
);
710 bufline
->terminated
= 1;
712 /* Truncate the data if required */
713 if(rb_unlikely(len
> 510))
716 bufline
->buf
[len
++] = '\r';
717 bufline
->buf
[len
++] = '\n';
719 else if(rb_unlikely(len
== 0))
721 bufline
->buf
[len
++] = '\r';
722 bufline
->buf
[len
++] = '\n';
723 bufline
->buf
[len
] = '\0';
727 /* Chop trailing CRLF's .. */
728 while((bufline
->buf
[len
] == '\r') || (bufline
->buf
[len
] == '\n')
729 || (bufline
->buf
[len
] == '\0'))
734 bufline
->buf
[++len
] = '\r';
735 bufline
->buf
[++len
] = '\n';
736 bufline
->buf
[++len
] = '\0';
748 * Flush data to the buffer. It tries to write as much data as possible
749 * to the given socket. Any return values are passed straight through.
750 * If there is no data in the socket, EWOULDBLOCK is set as an errno
751 * rather than returning 0 (which would map to an EOF..)
753 * Notes: XXX We *should* have a clue here when a non-full buffer is arrived.
754 * and tag it so that we don't re-schedule another write until
758 rb_linebuf_flush(rb_fde_t
*F
, buf_head_t
* bufhead
)
764 * autoconf checks for this..but really just want to use it if we have a
765 * native version even if libircd provides a fake version...
773 static struct rb_iovec vec
[RB_UIO_MAXIOV
];
775 memset(vec
, 0, sizeof(vec
));
776 /* Check we actually have a first buffer */
777 if(bufhead
->list
.head
== NULL
)
779 /* nope, so we return none .. */
784 ptr
= bufhead
->list
.head
;
787 if(!bufline
->terminated
)
794 vec
[x
].iov_base
= bufline
->buf
+ bufhead
->writeofs
;
795 vec
[x
++].iov_len
= bufline
->len
- bufhead
->writeofs
;
804 if(!bufline
->terminated
)
807 vec
[x
].iov_base
= bufline
->buf
;
808 vec
[x
].iov_len
= bufline
->len
;
812 while(++x
< RB_UIO_MAXIOV
);
820 xret
= retval
= rb_writev(F
, vec
, x
);
824 ptr
= bufhead
->list
.head
;
826 for(y
= 0; y
< x
; y
++)
830 if(xret
>= bufline
->len
- bufhead
->writeofs
)
832 xret
-= bufline
->len
- bufhead
->writeofs
;
834 rb_linebuf_done_line(bufhead
, bufline
, bufhead
->list
.head
);
835 bufhead
->writeofs
= 0;
839 bufhead
->writeofs
+= xret
;
848 /* this is the non-writev case */
850 /* Check we actually have a first buffer */
851 if(bufhead
->list
.head
== NULL
)
853 /* nope, so we return none .. */
858 bufline
= bufhead
->list
.head
->data
;
860 /* And that its actually full .. */
861 if(!bufline
->terminated
)
867 /* Now, try writing data */
868 retval
= rb_write(F
, bufline
->buf
+ bufhead
->writeofs
, bufline
->len
- bufhead
->writeofs
);
873 /* we've got data, so update the write offset */
874 bufhead
->writeofs
+= retval
;
876 /* if we've written everything *and* the CRLF, deallocate and update
878 if(bufhead
->writeofs
== bufline
->len
)
880 bufhead
->writeofs
= 0;
881 lrb_assert(bufhead
->len
>= 0);
882 rb_linebuf_done_line(bufhead
, bufline
, bufhead
->list
.head
);
885 /* Return line length */
892 * count linebufs for stats z
896 rb_count_rb_linebuf_memory(size_t *count
, size_t *rb_linebuf_memory_used
)
898 rb_bh_usage(rb_linebuf_heap
, count
, NULL
, rb_linebuf_memory_used
, NULL
);