]>
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
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>
31 static rb_bh
*rb_linebuf_heap
;
33 static int bufline_count
= 0;
35 #ifndef LINEBUF_HEAP_SIZE
36 #define LINEBUF_HEAP_SIZE 2048
42 * Initialise the linebuf mechanism
46 rb_linebuf_init(size_t heap_size
)
48 rb_linebuf_heap
= rb_bh_create(sizeof(buf_line_t
), heap_size
, "librb_linebuf_heap");
52 rb_linebuf_allocate(void)
55 t
= rb_bh_alloc(rb_linebuf_heap
);
61 rb_linebuf_free(buf_line_t
* p
)
63 rb_bh_free(rb_linebuf_heap
, p
);
69 * Create a new line, and link it to the given linebuf.
70 * It will be initially empty.
73 rb_linebuf_new_line(buf_head_t
* bufhead
)
78 bufline
= rb_linebuf_allocate();
84 node
= rb_make_rb_dlink_node();
86 /* Stick it at the end of the buf list */
87 rb_dlinkAddTail(bufline
, node
, &bufhead
->list
);
90 /* And finally, update the allocated size */
99 * rb_linebuf_done_line
101 * We've finished with the given line, so deallocate it
104 rb_linebuf_done_line(buf_head_t
* bufhead
, buf_line_t
* bufline
, rb_dlink_node
*node
)
106 /* Remove it from the linked list */
107 rb_dlinkDestroy(node
, &bufhead
->list
);
109 /* Update the allocated size */
111 bufhead
->len
-= bufline
->len
;
112 lrb_assert(bufhead
->len
>= 0);
116 lrb_assert(bufline
->refcount
>= 0);
118 if(bufline
->refcount
== 0)
120 /* and finally, deallocate the buf */
122 lrb_assert(bufline_count
>= 0);
123 rb_linebuf_free(bufline
);
129 * skip to end of line or the crlfs, return the number of bytes ..
132 rb_linebuf_skip_crlf(char *ch
, int len
)
136 /* First, skip until the first non-CRLF */
137 for(; len
; len
--, ch
++)
145 /* Then, skip until the last CRLF */
146 for(; len
; len
--, ch
++)
148 if((*ch
!= '\r') && (*ch
!= '\n'))
151 lrb_assert(orig_len
> len
);
152 return (orig_len
- len
);
160 * Initialise the new buffer
163 rb_linebuf_newbuf(buf_head_t
* bufhead
)
165 /* not much to do right now :) */
166 memset(bufhead
, 0, sizeof(buf_head_t
));
172 * Flush all the lines associated with this buffer
175 rb_linebuf_donebuf(buf_head_t
* bufhead
)
177 while(bufhead
->list
.head
!= NULL
)
179 rb_linebuf_done_line(bufhead
, (buf_line_t
*) bufhead
->list
.head
->data
,
185 * rb_linebuf_copy_line
187 * Okay..this functions comments made absolutely no sense.
189 * Basically what we do is this. Find the first chunk of text
190 * and then scan for a CRLF. If we didn't find it, but we didn't
191 * overflow our buffer..we wait for some more data.
192 * If we found a CRLF, we replace them with a \0 character.
193 * If we overflowed, we copy the most our buffer can handle, terminate
194 * it with a \0 and return.
196 * The return value is the amount of data we consumed. This could
197 * be different than the size of the linebuffer, as when we discard
198 * the overflow, we don't want to process it again.
200 * This still sucks in my opinion, but it seems to work.
205 rb_linebuf_copy_line(buf_head_t
* bufhead
, buf_line_t
* bufline
, char *data
, int len
)
207 int cpylen
= 0; /* how many bytes we've copied */
208 char *ch
= data
; /* Pointer to where we are in the read data */
209 char *bufch
= bufline
->buf
+ bufline
->len
;
210 int clen
= 0; /* how many bytes we've processed,
211 and don't ever want to see again.. */
213 /* If its full or terminated, ignore it */
216 lrb_assert(bufline
->len
< BUF_DATA_SIZE
);
217 if(bufline
->terminated
== 1)
220 clen
= cpylen
= rb_linebuf_skip_crlf(ch
, len
);
224 /* This is the ~overflow case..This doesn't happen often.. */
225 if(cpylen
> (BUF_DATA_SIZE
- bufline
->len
- 1))
227 memcpy(bufch
, ch
, (BUF_DATA_SIZE
- bufline
->len
- 1));
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
;
501 * Similar to rb_linebuf_put, but designed for use by send.c.
503 * prefixfmt is used as a format for the varargs, and is inserted first.
504 * Then format/va_args is appended to the buffer.
507 rb_linebuf_putmsg(buf_head_t
* bufhead
, const char *format
, va_list * va_args
,
508 const char *prefixfmt
, ...)
514 /* make sure the previous line is terminated */
516 if(bufhead
->list
.tail
)
518 bufline
= bufhead
->list
.tail
->data
;
519 lrb_assert(bufline
->terminated
);
522 /* Create a new line */
523 bufline
= rb_linebuf_new_line(bufhead
);
525 if(prefixfmt
!= NULL
)
527 va_start(prefix_args
, prefixfmt
);
528 len
= rb_vsnprintf(bufline
->buf
, BUF_DATA_SIZE
, prefixfmt
, prefix_args
);
534 len
+= rb_vsnprintf((bufline
->buf
+ len
), (BUF_DATA_SIZE
- len
), format
, *va_args
);
537 bufline
->terminated
= 1;
539 /* Truncate the data if required */
540 if(rb_unlikely(len
> 510))
543 bufline
->buf
[len
++] = '\r';
544 bufline
->buf
[len
++] = '\n';
546 else if(rb_unlikely(len
== 0))
548 bufline
->buf
[len
++] = '\r';
549 bufline
->buf
[len
++] = '\n';
550 bufline
->buf
[len
] = '\0';
554 /* Chop trailing CRLF's .. */
555 while((bufline
->buf
[len
] == '\r') || (bufline
->buf
[len
] == '\n')
556 || (bufline
->buf
[len
] == '\0'))
561 bufline
->buf
[++len
] = '\r';
562 bufline
->buf
[++len
] = '\n';
563 bufline
->buf
[++len
] = '\0';
571 rb_linebuf_putbuf(buf_head_t
* bufhead
, const char *buffer
)
576 /* make sure the previous line is terminated */
578 if(bufhead
->list
.tail
)
580 bufline
= bufhead
->list
.tail
->data
;
581 lrb_assert(bufline
->terminated
);
584 /* Create a new line */
585 bufline
= rb_linebuf_new_line(bufhead
);
587 if(rb_unlikely(buffer
!= NULL
))
588 len
= rb_strlcpy(bufline
->buf
, buffer
, BUF_DATA_SIZE
);
590 bufline
->terminated
= 1;
592 /* Truncate the data if required */
593 if(rb_unlikely(len
> 510))
596 bufline
->buf
[len
++] = '\r';
597 bufline
->buf
[len
++] = '\n';
599 else if(rb_unlikely(len
== 0))
601 bufline
->buf
[len
++] = '\r';
602 bufline
->buf
[len
++] = '\n';
603 bufline
->buf
[len
] = '\0';
607 /* Chop trailing CRLF's .. */
608 while((bufline
->buf
[len
] == '\r') || (bufline
->buf
[len
] == '\n')
609 || (bufline
->buf
[len
] == '\0'))
614 bufline
->buf
[++len
] = '\r';
615 bufline
->buf
[++len
] = '\n';
616 bufline
->buf
[++len
] = '\0';
626 rb_linebuf_put(buf_head_t
* bufhead
, const char *format
, ...)
632 /* make sure the previous line is terminated */
634 if(bufhead
->list
.tail
)
636 bufline
= bufhead
->list
.tail
->data
;
637 lrb_assert(bufline
->terminated
);
640 /* Create a new line */
641 bufline
= rb_linebuf_new_line(bufhead
);
643 if(rb_unlikely(format
!= NULL
))
645 va_start(args
, format
);
646 len
= rb_vsnprintf(bufline
->buf
, BUF_DATA_SIZE
, format
, args
);
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';
688 * Flush data to the buffer. It tries to write as much data as possible
689 * to the given socket. Any return values are passed straight through.
690 * If there is no data in the socket, EWOULDBLOCK is set as an errno
691 * rather than returning 0 (which would map to an EOF..)
693 * Notes: XXX We *should* have a clue here when a non-full buffer is arrived.
694 * and tag it so that we don't re-schedule another write until
698 rb_linebuf_flush(rb_fde_t
*F
, buf_head_t
* bufhead
)
704 * autoconf checks for this..but really just want to use it if we have a
705 * native version even if libircd provides a fake version...
713 static struct rb_iovec vec
[RB_UIO_MAXIOV
];
715 memset(vec
, 0, sizeof(vec
));
716 /* Check we actually have a first buffer */
717 if(bufhead
->list
.head
== NULL
)
719 /* nope, so we return none .. */
724 ptr
= bufhead
->list
.head
;
727 if(!bufline
->terminated
)
734 vec
[x
].iov_base
= bufline
->buf
+ bufhead
->writeofs
;
735 vec
[x
++].iov_len
= bufline
->len
- bufhead
->writeofs
;
744 if(!bufline
->terminated
)
747 vec
[x
].iov_base
= bufline
->buf
;
748 vec
[x
].iov_len
= bufline
->len
;
752 while(++x
< RB_UIO_MAXIOV
);
760 xret
= retval
= rb_writev(F
, vec
, x
);
764 ptr
= bufhead
->list
.head
;
766 for(y
= 0; y
< x
; y
++)
770 if(xret
>= bufline
->len
- bufhead
->writeofs
)
772 xret
-= bufline
->len
- bufhead
->writeofs
;
774 rb_linebuf_done_line(bufhead
, bufline
, bufhead
->list
.head
);
775 bufhead
->writeofs
= 0;
779 bufhead
->writeofs
+= xret
;
788 /* this is the non-writev case */
790 /* Check we actually have a first buffer */
791 if(bufhead
->list
.head
== NULL
)
793 /* nope, so we return none .. */
798 bufline
= bufhead
->list
.head
->data
;
800 /* And that its actually full .. */
801 if(!bufline
->terminated
)
807 /* Now, try writing data */
808 retval
= rb_write(F
, bufline
->buf
+ bufhead
->writeofs
, bufline
->len
- bufhead
->writeofs
);
813 /* we've got data, so update the write offset */
814 bufhead
->writeofs
+= retval
;
816 /* if we've written everything *and* the CRLF, deallocate and update
818 if(bufhead
->writeofs
== bufline
->len
)
820 bufhead
->writeofs
= 0;
821 lrb_assert(bufhead
->len
>= 0);
822 rb_linebuf_done_line(bufhead
, bufline
, bufhead
->list
.head
);
825 /* Return line length */
832 * count linebufs for stats z
836 rb_count_rb_linebuf_memory(size_t *count
, size_t *rb_linebuf_memory_used
)
838 rb_bh_usage(rb_linebuf_heap
, count
, NULL
, rb_linebuf_memory_used
, NULL
);