]>
jfr.im git - irc/rqf/shadowircd.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
26 #include <libratbox_config.h>
27 #include <ratbox_lib.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 memcpy(bufch
, ch
, (BUF_DATA_SIZE
- bufline
->len
- 1));
227 bufline
->buf
[BUF_DATA_SIZE
- 1] = '\0';
228 bufch
= bufline
->buf
+ BUF_DATA_SIZE
- 2;
229 while(cpylen
&& (*bufch
== '\r' || *bufch
== '\n'))
235 bufline
->terminated
= 1;
236 bufline
->len
= BUF_DATA_SIZE
- 1;
237 bufhead
->len
+= BUF_DATA_SIZE
- 1;
241 memcpy(bufch
, ch
, cpylen
);
246 if(*bufch
!= '\r' && *bufch
!= '\n')
248 /* No linefeed, bail for the next time */
249 bufhead
->len
+= cpylen
;
250 bufline
->len
+= cpylen
;
251 bufline
->terminated
= 0;
255 /* Yank the CRLF off this, replace with a \0 */
256 while(cpylen
&& (*bufch
== '\r' || *bufch
== '\n'))
263 bufline
->terminated
= 1;
264 bufhead
->len
+= cpylen
;
265 bufline
->len
+= cpylen
;
270 * rb_linebuf_copy_raw
272 * Copy as much data as possible directly into a linebuf,
273 * splitting at \r\n, but without altering any data.
277 rb_linebuf_copy_raw(buf_head_t
* bufhead
, buf_line_t
* bufline
, char *data
, int len
)
279 int cpylen
= 0; /* how many bytes we've copied */
280 char *ch
= data
; /* Pointer to where we are in the read data */
281 char *bufch
= bufline
->buf
+ bufline
->len
;
282 int clen
= 0; /* how many bytes we've processed,
283 and don't ever want to see again.. */
285 /* If its full or terminated, ignore it */
288 lrb_assert(bufline
->len
< BUF_DATA_SIZE
);
289 if(bufline
->terminated
== 1)
292 clen
= cpylen
= rb_linebuf_skip_crlf(ch
, len
);
296 /* This is the overflow case..This doesn't happen often.. */
297 if(cpylen
> (BUF_DATA_SIZE
- bufline
->len
- 1))
299 clen
= BUF_DATA_SIZE
- bufline
->len
- 1;
300 memcpy(bufch
, ch
, clen
);
301 bufline
->buf
[BUF_DATA_SIZE
- 1] = '\0';
302 bufch
= bufline
->buf
+ BUF_DATA_SIZE
- 2;
303 bufline
->terminated
= 1;
304 bufline
->len
= BUF_DATA_SIZE
- 1;
305 bufhead
->len
+= BUF_DATA_SIZE
- 1;
309 memcpy(bufch
, ch
, cpylen
);
314 if(*bufch
!= '\r' && *bufch
!= '\n')
316 /* No linefeed, bail for the next time */
317 bufhead
->len
+= cpylen
;
318 bufline
->len
+= cpylen
;
319 bufline
->terminated
= 0;
323 bufline
->terminated
= 1;
324 bufhead
->len
+= cpylen
;
325 bufline
->len
+= cpylen
;
333 * Take a given buffer and break out as many buffers as we can.
334 * If we find a CRLF, we terminate that buffer and create a new one.
335 * If we don't find a CRLF whilst parsing a buffer, we don't mark it
336 * 'finished', so the next loop through we can continue appending ..
338 * A few notes here, which you'll need to understand before continuing.
340 * - right now I'm only dealing with single sized buffers. Later on,
341 * I might consider chaining buffers together to get longer "lines"
342 * but seriously, I don't see the advantage right now.
344 * - This *is* designed to turn into a reference-counter-protected setup
345 * to dodge copious copies.
348 rb_linebuf_parse(buf_head_t
* bufhead
, char *data
, int len
, int raw
)
354 /* First, if we have a partial buffer, try to squeze data into it */
355 if(bufhead
->list
.tail
!= NULL
)
357 /* Check we're doing the partial buffer thing */
358 bufline
= bufhead
->list
.tail
->data
;
359 /* just try, the worst it could do is *reject* us .. */
361 cpylen
= rb_linebuf_copy_line(bufhead
, bufline
, data
, len
);
363 cpylen
= rb_linebuf_copy_raw(bufhead
, bufline
, data
, len
);
369 /* If we've copied the same as what we've got, quit now */
371 return linecnt
; /* all the data done so soon? */
373 /* Skip the data and update len .. */
375 lrb_assert(len
>= 0);
382 /* We obviously need a new buffer, so .. */
383 bufline
= rb_linebuf_new_line(bufhead
);
387 cpylen
= rb_linebuf_copy_line(bufhead
, bufline
, data
, len
);
389 cpylen
= rb_linebuf_copy_raw(bufhead
, bufline
, data
, len
);
395 lrb_assert(len
>= 0);
406 * get the next buffer from our line. For the time being it will copy
407 * data into the given buffer and free the underlying linebuf.
410 rb_linebuf_get(buf_head_t
* bufhead
, char *buf
, int buflen
, int partial
, int raw
)
416 /* make sure we have a line */
417 if(bufhead
->list
.head
== NULL
)
418 return 0; /* Obviously not.. hrm. */
420 bufline
= bufhead
->list
.head
->data
;
422 /* make sure that the buffer was actually *terminated */
423 if(!(partial
|| bufline
->terminated
))
424 return 0; /* Wait for more data! */
426 if(buflen
< bufline
->len
)
429 cpylen
= bufline
->len
;
432 start
= bufline
->buf
;
434 /* if we left extraneous '\r\n' characters in the string,
435 * and we don't want to read the raw data, clean up the string.
437 if(bufline
->raw
&& !raw
)
439 /* skip leading EOL characters */
440 while(cpylen
&& (*start
== '\r' || *start
== '\n'))
445 /* skip trailing EOL characters */
446 ch
= &start
[cpylen
- 1];
447 while(cpylen
&& (*ch
== '\r' || *ch
== '\n'))
454 memcpy(buf
, start
, cpylen
);
456 /* convert CR/LF to NULL */
460 lrb_assert(cpylen
>= 0);
462 /* Deallocate the line */
463 rb_linebuf_done_line(bufhead
, bufline
, bufhead
->list
.head
);
465 /* return how much we copied */
472 * attach the lines in a buf_head_t to another buf_head_t
473 * without copying the data (using refcounts).
476 rb_linebuf_attach(buf_head_t
* bufhead
, buf_head_t
* new)
481 RB_DLINK_FOREACH(ptr
, new->list
.head
)
484 rb_dlinkAddTailAlloc(line
, &bufhead
->list
);
486 /* Update the allocated size */
488 bufhead
->len
+= line
->len
;
500 * Similar to rb_linebuf_put, but designed for use by send.c.
502 * prefixfmt is used as a format for the varargs, and is inserted first.
503 * Then format/va_args is appended to the buffer.
506 rb_linebuf_putmsg(buf_head_t
* bufhead
, const char *format
, va_list * va_args
,
507 const char *prefixfmt
, ...)
513 /* make sure the previous line is terminated */
515 if(bufhead
->list
.tail
)
517 bufline
= bufhead
->list
.tail
->data
;
518 lrb_assert(bufline
->terminated
);
521 /* Create a new line */
522 bufline
= rb_linebuf_new_line(bufhead
);
524 if(prefixfmt
!= NULL
)
526 va_start(prefix_args
, prefixfmt
);
527 len
= rb_vsnprintf(bufline
->buf
, BUF_DATA_SIZE
, prefixfmt
, prefix_args
);
533 len
+= rb_vsnprintf((bufline
->buf
+ len
), (BUF_DATA_SIZE
- len
), format
, *va_args
);
536 bufline
->terminated
= 1;
538 /* Truncate the data if required */
539 if(rb_unlikely(len
> 510))
542 bufline
->buf
[len
++] = '\r';
543 bufline
->buf
[len
++] = '\n';
545 else if(rb_unlikely(len
== 0))
547 bufline
->buf
[len
++] = '\r';
548 bufline
->buf
[len
++] = '\n';
549 bufline
->buf
[len
] = '\0';
553 /* Chop trailing CRLF's .. */
554 while((bufline
->buf
[len
] == '\r') || (bufline
->buf
[len
] == '\n')
555 || (bufline
->buf
[len
] == '\0'))
560 bufline
->buf
[++len
] = '\r';
561 bufline
->buf
[++len
] = '\n';
562 bufline
->buf
[++len
] = '\0';
570 rb_linebuf_putbuf(buf_head_t
* bufhead
, const char *buffer
)
575 /* make sure the previous line is terminated */
577 if(bufhead
->list
.tail
)
579 bufline
= bufhead
->list
.tail
->data
;
580 lrb_assert(bufline
->terminated
);
583 /* Create a new line */
584 bufline
= rb_linebuf_new_line(bufhead
);
586 if(rb_unlikely(buffer
!= NULL
))
587 len
= rb_strlcpy(bufline
->buf
, buffer
, BUF_DATA_SIZE
);
589 bufline
->terminated
= 1;
591 /* Truncate the data if required */
592 if(rb_unlikely(len
> 510))
595 bufline
->buf
[len
++] = '\r';
596 bufline
->buf
[len
++] = '\n';
598 else if(rb_unlikely(len
== 0))
600 bufline
->buf
[len
++] = '\r';
601 bufline
->buf
[len
++] = '\n';
602 bufline
->buf
[len
] = '\0';
606 /* Chop trailing CRLF's .. */
607 while((bufline
->buf
[len
] == '\r') || (bufline
->buf
[len
] == '\n')
608 || (bufline
->buf
[len
] == '\0'))
613 bufline
->buf
[++len
] = '\r';
614 bufline
->buf
[++len
] = '\n';
615 bufline
->buf
[++len
] = '\0';
625 rb_linebuf_put(buf_head_t
* bufhead
, const char *format
, ...)
631 /* make sure the previous line is terminated */
633 if(bufhead
->list
.tail
)
635 bufline
= bufhead
->list
.tail
->data
;
636 lrb_assert(bufline
->terminated
);
639 /* Create a new line */
640 bufline
= rb_linebuf_new_line(bufhead
);
642 if(rb_unlikely(format
!= NULL
))
644 va_start(args
, format
);
645 len
= rb_vsnprintf(bufline
->buf
, BUF_DATA_SIZE
, format
, args
);
649 bufline
->terminated
= 1;
651 /* Truncate the data if required */
652 if(rb_unlikely(len
> 510))
655 bufline
->buf
[len
++] = '\r';
656 bufline
->buf
[len
++] = '\n';
658 else if(rb_unlikely(len
== 0))
660 bufline
->buf
[len
++] = '\r';
661 bufline
->buf
[len
++] = '\n';
662 bufline
->buf
[len
] = '\0';
666 /* Chop trailing CRLF's .. */
667 while((bufline
->buf
[len
] == '\r') || (bufline
->buf
[len
] == '\n')
668 || (bufline
->buf
[len
] == '\0'))
673 bufline
->buf
[++len
] = '\r';
674 bufline
->buf
[++len
] = '\n';
675 bufline
->buf
[++len
] = '\0';
687 * Flush data to the buffer. It tries to write as much data as possible
688 * to the given socket. Any return values are passed straight through.
689 * If there is no data in the socket, EWOULDBLOCK is set as an errno
690 * rather than returning 0 (which would map to an EOF..)
692 * Notes: XXX We *should* have a clue here when a non-full buffer is arrived.
693 * and tag it so that we don't re-schedule another write until
697 rb_linebuf_flush(rb_fde_t
*F
, buf_head_t
* bufhead
)
703 * autoconf checks for this..but really just want to use it if we have a
704 * native version even if libircd provides a fake version...
712 static struct rb_iovec vec
[RB_UIO_MAXIOV
];
714 memset(vec
, 0, sizeof(vec
));
715 /* Check we actually have a first buffer */
716 if(bufhead
->list
.head
== NULL
)
718 /* nope, so we return none .. */
723 ptr
= bufhead
->list
.head
;
726 if(!bufline
->terminated
)
733 vec
[x
].iov_base
= bufline
->buf
+ bufhead
->writeofs
;
734 vec
[x
++].iov_len
= bufline
->len
- bufhead
->writeofs
;
743 if(!bufline
->terminated
)
746 vec
[x
].iov_base
= bufline
->buf
;
747 vec
[x
].iov_len
= bufline
->len
;
751 while(++x
< RB_UIO_MAXIOV
);
759 xret
= retval
= rb_writev(F
, vec
, x
);
763 ptr
= bufhead
->list
.head
;
765 for(y
= 0; y
< x
; y
++)
769 if(xret
>= bufline
->len
- bufhead
->writeofs
)
771 xret
-= bufline
->len
- bufhead
->writeofs
;
773 rb_linebuf_done_line(bufhead
, bufline
, bufhead
->list
.head
);
774 bufhead
->writeofs
= 0;
778 bufhead
->writeofs
+= xret
;
787 /* this is the non-writev case */
789 /* Check we actually have a first buffer */
790 if(bufhead
->list
.head
== NULL
)
792 /* nope, so we return none .. */
797 bufline
= bufhead
->list
.head
->data
;
799 /* And that its actually full .. */
800 if(!bufline
->terminated
)
806 /* Now, try writing data */
807 retval
= rb_write(F
, bufline
->buf
+ bufhead
->writeofs
, bufline
->len
- bufhead
->writeofs
);
812 /* we've got data, so update the write offset */
813 bufhead
->writeofs
+= retval
;
815 /* if we've written everything *and* the CRLF, deallocate and update
817 if(bufhead
->writeofs
== bufline
->len
)
819 bufhead
->writeofs
= 0;
820 lrb_assert(bufhead
->len
>= 0);
821 rb_linebuf_done_line(bufhead
, bufline
, bufhead
->list
.head
);
824 /* Return line length */
831 * count linebufs for stats z
835 rb_count_rb_linebuf_memory(size_t *count
, size_t *rb_linebuf_memory_used
)
837 rb_bh_usage(rb_linebuf_heap
, count
, NULL
, rb_linebuf_memory_used
, NULL
);