]> jfr.im git - solanum.git/blame - librb/src/linebuf.c
Remove ancient portability code (#361)
[solanum.git] / librb / src / linebuf.c
CommitLineData
db137867
AC
1/*
2 * ircd-ratbox: A slightly useful ircd.
3 * linebuf.c: Maintains linebuffers.
4 *
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
8 *
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.
13 *
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.
18 *
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
22 * USA
23 *
db137867
AC
24 */
25
fe037171
EM
26#include <librb_config.h>
27#include <rb_lib.h>
db137867
AC
28#include <commio-int.h>
29
db137867 30static rb_bh *rb_linebuf_heap;
db137867
AC
31
32static int bufline_count = 0;
33
db137867
AC
34/*
35 * rb_linebuf_init
36 *
37 * Initialise the linebuf mechanism
38 */
39
40void
41rb_linebuf_init(size_t heap_size)
42{
db137867 43 rb_linebuf_heap = rb_bh_create(sizeof(buf_line_t), heap_size, "librb_linebuf_heap");
db137867
AC
44}
45
46static buf_line_t *
47rb_linebuf_allocate(void)
48{
49 buf_line_t *t;
db137867 50 t = rb_bh_alloc(rb_linebuf_heap);
db137867
AC
51 return (t);
52
53}
54
55static void
56rb_linebuf_free(buf_line_t * p)
57{
db137867 58 rb_bh_free(rb_linebuf_heap, p);
db137867
AC
59}
60
61/*
62 * rb_linebuf_new_line
63 *
64 * Create a new line, and link it to the given linebuf.
65 * It will be initially empty.
66 */
67static buf_line_t *
68rb_linebuf_new_line(buf_head_t * bufhead)
69{
70 buf_line_t *bufline;
db137867
AC
71
72 bufline = rb_linebuf_allocate();
73 if(bufline == NULL)
74 return NULL;
75 ++bufline_count;
76
db137867 77 /* Stick it at the end of the buf list */
a2bfe0f8 78 rb_dlinkAddTailAlloc(bufline, &bufhead->list);
db137867
AC
79 bufline->refcount++;
80
81 /* And finally, update the allocated size */
82 bufhead->alloclen++;
83 bufhead->numlines++;
84
85 return bufline;
86}
87
88
89/*
90 * rb_linebuf_done_line
91 *
92 * We've finished with the given line, so deallocate it
93 */
94static void
3202e249 95rb_linebuf_done_line(buf_head_t * bufhead, buf_line_t * bufline, rb_dlink_node *node)
db137867
AC
96{
97 /* Remove it from the linked list */
98 rb_dlinkDestroy(node, &bufhead->list);
99
100 /* Update the allocated size */
101 bufhead->alloclen--;
102 bufhead->len -= bufline->len;
103 lrb_assert(bufhead->len >= 0);
104 bufhead->numlines--;
105
106 bufline->refcount--;
107 lrb_assert(bufline->refcount >= 0);
108
109 if(bufline->refcount == 0)
110 {
111 /* and finally, deallocate the buf */
112 --bufline_count;
113 lrb_assert(bufline_count >= 0);
114 rb_linebuf_free(bufline);
115 }
116}
117
118
119/*
120 * skip to end of line or the crlfs, return the number of bytes ..
121 */
122static inline int
123rb_linebuf_skip_crlf(char *ch, int len)
124{
125 int orig_len = len;
126
127 /* First, skip until the first non-CRLF */
128 for(; len; len--, ch++)
129 {
130 if(*ch == '\r')
131 break;
132 else if(*ch == '\n')
133 break;
134 }
135
136 /* Then, skip until the last CRLF */
137 for(; len; len--, ch++)
138 {
139 if((*ch != '\r') && (*ch != '\n'))
140 break;
141 }
142 lrb_assert(orig_len > len);
143 return (orig_len - len);
144}
145
146
147
148/*
149 * rb_linebuf_newbuf
150 *
151 * Initialise the new buffer
152 */
153void
154rb_linebuf_newbuf(buf_head_t * bufhead)
155{
156 /* not much to do right now :) */
157 memset(bufhead, 0, sizeof(buf_head_t));
158}
159
160/*
161 * rb_linebuf_donebuf
162 *
163 * Flush all the lines associated with this buffer
164 */
165void
166rb_linebuf_donebuf(buf_head_t * bufhead)
167{
168 while(bufhead->list.head != NULL)
169 {
3202e249
VY
170 rb_linebuf_done_line(bufhead, (buf_line_t *) bufhead->list.head->data,
171 bufhead->list.head);
db137867
AC
172 }
173}
174
175/*
176 * rb_linebuf_copy_line
177 *
178 * Okay..this functions comments made absolutely no sense.
55abcbb2 179 *
db137867
AC
180 * Basically what we do is this. Find the first chunk of text
181 * and then scan for a CRLF. If we didn't find it, but we didn't
182 * overflow our buffer..we wait for some more data.
183 * If we found a CRLF, we replace them with a \0 character.
184 * If we overflowed, we copy the most our buffer can handle, terminate
185 * it with a \0 and return.
186 *
187 * The return value is the amount of data we consumed. This could
188 * be different than the size of the linebuffer, as when we discard
189 * the overflow, we don't want to process it again.
190 *
191 * This still sucks in my opinion, but it seems to work.
192 *
193 * -Aaron
194 */
195static int
196rb_linebuf_copy_line(buf_head_t * bufhead, buf_line_t * bufline, char *data, int len)
197{
198 int cpylen = 0; /* how many bytes we've copied */
199 char *ch = data; /* Pointer to where we are in the read data */
200 char *bufch = bufline->buf + bufline->len;
201 int clen = 0; /* how many bytes we've processed,
202 and don't ever want to see again.. */
203
204 /* If its full or terminated, ignore it */
205
206 bufline->raw = 0;
7a06833f 207 lrb_assert(bufline->len <= LINEBUF_SIZE);
db137867
AC
208 if(bufline->terminated == 1)
209 return 0;
210
211 clen = cpylen = rb_linebuf_skip_crlf(ch, len);
212 if(clen == -1)
213 return -1;
214
215 /* This is the ~overflow case..This doesn't happen often.. */
7a06833f 216 if(cpylen > (LINEBUF_SIZE - bufline->len))
db137867 217 {
7a06833f 218 cpylen = LINEBUF_SIZE - bufline->len;
1c864688 219 memcpy(bufch, ch, cpylen);
7a06833f
SA
220 bufline->buf[LINEBUF_SIZE] = '\0';
221 bufch = bufline->buf + LINEBUF_SIZE - 1;
db137867
AC
222 while(cpylen && (*bufch == '\r' || *bufch == '\n'))
223 {
224 *bufch = '\0';
225 cpylen--;
226 bufch--;
227 }
228 bufline->terminated = 1;
7a06833f
SA
229 bufline->len = LINEBUF_SIZE;
230 bufhead->len += LINEBUF_SIZE;
db137867
AC
231 return clen;
232 }
233
234 memcpy(bufch, ch, cpylen);
235 bufch += cpylen;
236 *bufch = '\0';
237 bufch--;
238
239 if(*bufch != '\r' && *bufch != '\n')
240 {
241 /* No linefeed, bail for the next time */
242 bufhead->len += cpylen;
243 bufline->len += cpylen;
244 bufline->terminated = 0;
245 return clen;
246 }
247
248 /* Yank the CRLF off this, replace with a \0 */
249 while(cpylen && (*bufch == '\r' || *bufch == '\n'))
250 {
251 *bufch = '\0';
252 cpylen--;
253 bufch--;
254 }
255
256 bufline->terminated = 1;
257 bufhead->len += cpylen;
258 bufline->len += cpylen;
259 return clen;
260}
261
262/*
263 * rb_linebuf_copy_raw
264 *
265 * Copy as much data as possible directly into a linebuf,
266 * splitting at \r\n, but without altering any data.
267 *
268 */
269static int
270rb_linebuf_copy_raw(buf_head_t * bufhead, buf_line_t * bufline, char *data, int len)
271{
272 int cpylen = 0; /* how many bytes we've copied */
273 char *ch = data; /* Pointer to where we are in the read data */
274 char *bufch = bufline->buf + bufline->len;
275 int clen = 0; /* how many bytes we've processed,
276 and don't ever want to see again.. */
277
278 /* If its full or terminated, ignore it */
279
280 bufline->raw = 1;
7a06833f 281 lrb_assert(bufline->len <= LINEBUF_SIZE);
db137867
AC
282 if(bufline->terminated == 1)
283 return 0;
284
285 clen = cpylen = rb_linebuf_skip_crlf(ch, len);
286 if(clen == -1)
287 return -1;
288
289 /* This is the overflow case..This doesn't happen often.. */
7a06833f 290 if(cpylen > (LINEBUF_SIZE - bufline->len))
db137867 291 {
7a06833f 292 clen = LINEBUF_SIZE - bufline->len;
db137867 293 memcpy(bufch, ch, clen);
7a06833f 294 bufline->buf[LINEBUF_SIZE] = '\0';
db137867 295 bufline->terminated = 1;
7a06833f
SA
296 bufline->len = LINEBUF_SIZE;
297 bufhead->len += LINEBUF_SIZE;
db137867
AC
298 return clen;
299 }
300
301 memcpy(bufch, ch, cpylen);
302 bufch += cpylen;
303 *bufch = '\0';
304 bufch--;
305
306 if(*bufch != '\r' && *bufch != '\n')
307 {
308 /* No linefeed, bail for the next time */
309 bufhead->len += cpylen;
310 bufline->len += cpylen;
311 bufline->terminated = 0;
312 return clen;
313 }
314
315 bufline->terminated = 1;
316 bufhead->len += cpylen;
317 bufline->len += cpylen;
318 return clen;
319}
320
321
322/*
323 * rb_linebuf_parse
324 *
325 * Take a given buffer and break out as many buffers as we can.
326 * If we find a CRLF, we terminate that buffer and create a new one.
327 * If we don't find a CRLF whilst parsing a buffer, we don't mark it
328 * 'finished', so the next loop through we can continue appending ..
329 *
330 * A few notes here, which you'll need to understand before continuing.
331 *
332 * - right now I'm only dealing with single sized buffers. Later on,
333 * I might consider chaining buffers together to get longer "lines"
334 * but seriously, I don't see the advantage right now.
335 *
336 * - This *is* designed to turn into a reference-counter-protected setup
337 * to dodge copious copies.
338 */
339int
340rb_linebuf_parse(buf_head_t * bufhead, char *data, int len, int raw)
341{
342 buf_line_t *bufline;
343 int cpylen;
344 int linecnt = 0;
345
346 /* First, if we have a partial buffer, try to squeze data into it */
347 if(bufhead->list.tail != NULL)
348 {
349 /* Check we're doing the partial buffer thing */
350 bufline = bufhead->list.tail->data;
db137867
AC
351 /* just try, the worst it could do is *reject* us .. */
352 if(!raw)
353 cpylen = rb_linebuf_copy_line(bufhead, bufline, data, len);
354 else
355 cpylen = rb_linebuf_copy_raw(bufhead, bufline, data, len);
356
357 if(cpylen == -1)
358 return -1;
359
360 linecnt++;
361 /* If we've copied the same as what we've got, quit now */
362 if(cpylen == len)
363 return linecnt; /* all the data done so soon? */
364
365 /* Skip the data and update len .. */
366 len -= cpylen;
367 lrb_assert(len >= 0);
368 data += cpylen;
369 }
370
371 /* Next, the loop */
372 while(len > 0)
373 {
374 /* We obviously need a new buffer, so .. */
375 bufline = rb_linebuf_new_line(bufhead);
376
377 /* And parse */
378 if(!raw)
379 cpylen = rb_linebuf_copy_line(bufhead, bufline, data, len);
380 else
381 cpylen = rb_linebuf_copy_raw(bufhead, bufline, data, len);
382
383 if(cpylen == -1)
384 return -1;
385
386 len -= cpylen;
387 lrb_assert(len >= 0);
388 data += cpylen;
389 linecnt++;
390 }
391 return linecnt;
392}
393
394
395/*
396 * rb_linebuf_get
397 *
398 * get the next buffer from our line. For the time being it will copy
399 * data into the given buffer and free the underlying linebuf.
400 */
401int
402rb_linebuf_get(buf_head_t * bufhead, char *buf, int buflen, int partial, int raw)
403{
404 buf_line_t *bufline;
405 int cpylen;
406 char *start, *ch;
407
408 /* make sure we have a line */
409 if(bufhead->list.head == NULL)
410 return 0; /* Obviously not.. hrm. */
411
412 bufline = bufhead->list.head->data;
413
414 /* make sure that the buffer was actually *terminated */
415 if(!(partial || bufline->terminated))
416 return 0; /* Wait for more data! */
417
418 if(buflen < bufline->len)
419 cpylen = buflen - 1;
420 else
421 cpylen = bufline->len;
422
423 /* Copy it */
424 start = bufline->buf;
425
426 /* if we left extraneous '\r\n' characters in the string,
427 * and we don't want to read the raw data, clean up the string.
428 */
429 if(bufline->raw && !raw)
430 {
431 /* skip leading EOL characters */
432 while(cpylen && (*start == '\r' || *start == '\n'))
433 {
434 start++;
435 cpylen--;
436 }
437 /* skip trailing EOL characters */
438 ch = &start[cpylen - 1];
439 while(cpylen && (*ch == '\r' || *ch == '\n'))
440 {
441 ch--;
442 cpylen--;
443 }
444 }
445
446 memcpy(buf, start, cpylen);
447
448 /* convert CR/LF to NULL */
449 if(!raw)
450 buf[cpylen] = '\0';
451
452 lrb_assert(cpylen >= 0);
453
454 /* Deallocate the line */
455 rb_linebuf_done_line(bufhead, bufline, bufhead->list.head);
456
457 /* return how much we copied */
458 return cpylen;
459}
460
461/*
462 * rb_linebuf_attach
463 *
464 * attach the lines in a buf_head_t to another buf_head_t
465 * without copying the data (using refcounts).
466 */
467void
468rb_linebuf_attach(buf_head_t * bufhead, buf_head_t * new)
469{
470 rb_dlink_node *ptr;
471 buf_line_t *line;
472
473 RB_DLINK_FOREACH(ptr, new->list.head)
474 {
475 line = ptr->data;
476 rb_dlinkAddTailAlloc(line, &bufhead->list);
477
478 /* Update the allocated size */
479 bufhead->alloclen++;
480 bufhead->len += line->len;
481 bufhead->numlines++;
482
483 line->refcount++;
484 }
485}
486
db137867 487/*
4b1cce65 488 * rb_linebuf_put
db137867 489 *
4b1cce65 490 * linked list of strings are appended in order.
db137867 491 */
4b1cce65
SA
492void
493rb_linebuf_put(buf_head_t *bufhead, const rb_strf_t *strings)
db137867
AC
494{
495 buf_line_t *bufline;
4b1cce65
SA
496 size_t len = 0;
497 int ret;
db137867
AC
498
499 /* make sure the previous line is terminated */
7a06833f 500 if (bufhead->list.tail) {
db137867
AC
501 bufline = bufhead->list.tail->data;
502 lrb_assert(bufline->terminated);
503 }
9f46eae6 504
7a06833f 505 /* create a new line */
db137867
AC
506 bufline = rb_linebuf_new_line(bufhead);
507
4b1cce65
SA
508 ret = rb_fsnprint(bufline->buf, LINEBUF_SIZE + 1, strings);
509 if (ret > 0)
510 len += ret;
db137867 511
4b1cce65
SA
512 if (len > LINEBUF_SIZE)
513 len = LINEBUF_SIZE;
db137867 514
7a06833f
SA
515 /* add trailing CRLF */
516 bufline->buf[len++] = '\r';
517 bufline->buf[len++] = '\n';
518 bufline->buf[len] = '\0';
519
520 bufline->terminated = 1;
521
db137867
AC
522 bufline->len = len;
523 bufhead->len += len;
524}
525
db137867
AC
526/*
527 * rb_linebuf_flush
528 *
529 * Flush data to the buffer. It tries to write as much data as possible
530 * to the given socket. Any return values are passed straight through.
531 * If there is no data in the socket, EWOULDBLOCK is set as an errno
532 * rather than returning 0 (which would map to an EOF..)
533 *
534 * Notes: XXX We *should* have a clue here when a non-full buffer is arrived.
535 * and tag it so that we don't re-schedule another write until
536 * we have a CRLF.
537 */
538int
539rb_linebuf_flush(rb_fde_t *F, buf_head_t * bufhead)
540{
541 buf_line_t *bufline;
542 int retval;
543
55abcbb2
KB
544/*
545 * autoconf checks for this..but really just want to use it if we have a
db137867
AC
546 * native version even if libircd provides a fake version...
547 */
db137867
AC
548 if(!rb_fd_ssl(F))
549 {
550 rb_dlink_node *ptr;
551 int x = 0, y;
552 int xret;
553 static struct rb_iovec vec[RB_UIO_MAXIOV];
554
555 memset(vec, 0, sizeof(vec));
556 /* Check we actually have a first buffer */
557 if(bufhead->list.head == NULL)
558 {
559 /* nope, so we return none .. */
560 errno = EWOULDBLOCK;
561 return -1;
562 }
563
564 ptr = bufhead->list.head;
3202e249 565
db137867
AC
566 bufline = ptr->data;
567 if(!bufline->terminated)
568 {
569 errno = EWOULDBLOCK;
570 return -1;
571
572 }
573
39930c66
JT
574 vec[x].iov_base = bufline->buf + bufhead->writeofs;
575 vec[x++].iov_len = bufline->len - bufhead->writeofs;
576 ptr = ptr->next;
db137867
AC
577
578 do
579 {
580 if(ptr == NULL)
581 break;
582
583 bufline = ptr->data;
584 if(!bufline->terminated)
585 break;
3202e249 586
db137867
AC
587 vec[x].iov_base = bufline->buf;
588 vec[x].iov_len = bufline->len;
589 ptr = ptr->next;
590
3202e249
VY
591 }
592 while(++x < RB_UIO_MAXIOV);
db137867
AC
593
594 if(x == 0)
595 {
596 errno = EWOULDBLOCK;
597 return -1;
598 }
599
600 xret = retval = rb_writev(F, vec, x);
601 if(retval <= 0)
602 return retval;
603
604 ptr = bufhead->list.head;
605
606 for(y = 0; y < x; y++)
607 {
608 bufline = ptr->data;
609
39930c66 610 if(xret >= bufline->len - bufhead->writeofs)
db137867 611 {
39930c66 612 xret -= bufline->len - bufhead->writeofs;
db137867
AC
613 ptr = ptr->next;
614 rb_linebuf_done_line(bufhead, bufline, bufhead->list.head);
39930c66 615 bufhead->writeofs = 0;
db137867 616 }
3202e249 617 else
db137867 618 {
39930c66 619 bufhead->writeofs += xret;
db137867
AC
620 break;
621 }
db137867
AC
622 }
623
624 return retval;
625 }
3202e249
VY
626
627 /* this is the non-writev case */
db137867 628
db137867
AC
629 /* Check we actually have a first buffer */
630 if(bufhead->list.head == NULL)
631 {
632 /* nope, so we return none .. */
633 errno = EWOULDBLOCK;
634 return -1;
635 }
636
637 bufline = bufhead->list.head->data;
638
639 /* And that its actually full .. */
640 if(!bufline->terminated)
641 {
642 errno = EWOULDBLOCK;
643 return -1;
644 }
645
db137867
AC
646 /* Now, try writing data */
647 retval = rb_write(F, bufline->buf + bufhead->writeofs, bufline->len - bufhead->writeofs);
648
649 if(retval <= 0)
650 return retval;
651
652 /* we've got data, so update the write offset */
653 bufhead->writeofs += retval;
654
655 /* if we've written everything *and* the CRLF, deallocate and update
656 bufhead */
657 if(bufhead->writeofs == bufline->len)
658 {
659 bufhead->writeofs = 0;
660 lrb_assert(bufhead->len >= 0);
661 rb_linebuf_done_line(bufhead, bufline, bufhead->list.head);
662 }
663
664 /* Return line length */
665 return retval;
666}
667
668
669
670/*
671 * count linebufs for stats z
672 */
673
674void
3202e249 675rb_count_rb_linebuf_memory(size_t *count, size_t *rb_linebuf_memory_used)
db137867 676{
db137867 677 rb_bh_usage(rb_linebuf_heap, count, NULL, rb_linebuf_memory_used, NULL);
db137867 678}