| 1 |
/* |
| 2 |
* libmowgli: A collection of useful routines for programming. |
| 3 |
* linebuf.c: Line buffering for the event loop system |
| 4 |
* |
| 5 |
* Copyright (c) 2012 Elizabeth J. Myers. All rights reserved. |
| 6 |
* |
| 7 |
* Permission to use, copy, modify, and/or distribute this software for any |
| 8 |
* purpose with or without fee is hereby granted, provided that the above |
| 9 |
* copyright notice and this permission notice appear in all copies. |
| 10 |
* |
| 11 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 12 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 13 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 14 |
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
| 15 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 16 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 17 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 18 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 19 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 20 |
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 21 |
* POSSIBILITY OF SUCH DAMAGE. |
| 22 |
*/ |
| 23 |
|
| 24 |
#include "mowgli.h" |
| 25 |
|
| 26 |
static mowgli_heap_t *linebuf_heap = NULL; |
| 27 |
|
| 28 |
static void mowgli_linebuf_read_data(mowgli_eventloop_t *eventloop, mowgli_eventloop_io_t *io, mowgli_eventloop_io_dir_t dir, void *userdata); |
| 29 |
static void mowgli_linebuf_write_data(mowgli_eventloop_t *eventloop, mowgli_eventloop_io_t *io, mowgli_eventloop_io_dir_t dir, void *userdata); |
| 30 |
static void mowgli_linebuf_process(mowgli_linebuf_t *linebuf); |
| 31 |
static void mowgli_linebuf_do_shutdown(mowgli_linebuf_t *linebuf); |
| 32 |
|
| 33 |
static int mowgli_linebuf_error(mowgli_vio_t *vio); |
| 34 |
|
| 35 |
#ifdef NOTYET |
| 36 |
static mowgli_vio_evops_t linebuf_evops = |
| 37 |
{ |
| 38 |
.read_cb = mowgli_linebuf_read_data, |
| 39 |
.write_cb = mowgli_linebuf_write_data |
| 40 |
}; |
| 41 |
#endif |
| 42 |
|
| 43 |
mowgli_linebuf_t * |
| 44 |
mowgli_linebuf_create(mowgli_linebuf_readline_cb_t *cb, void *userdata) |
| 45 |
{ |
| 46 |
mowgli_linebuf_t *linebuf; |
| 47 |
|
| 48 |
if (linebuf_heap == NULL) |
| 49 |
linebuf_heap = mowgli_heap_create(sizeof(mowgli_linebuf_t), 16, BH_NOW); |
| 50 |
|
| 51 |
linebuf = mowgli_heap_alloc(linebuf_heap); |
| 52 |
|
| 53 |
/* Sane default */ |
| 54 |
mowgli_linebuf_delim(linebuf, "\r\n", "\r\n"); |
| 55 |
linebuf->readline_cb = cb; |
| 56 |
|
| 57 |
linebuf->flags = 0; |
| 58 |
|
| 59 |
linebuf->readbuf.buffer = NULL; |
| 60 |
linebuf->writebuf.buffer = NULL; |
| 61 |
mowgli_linebuf_setbuflen(&(linebuf->readbuf), 65536); |
| 62 |
mowgli_linebuf_setbuflen(&(linebuf->writebuf), 65536); |
| 63 |
|
| 64 |
linebuf->eventloop = NULL; |
| 65 |
|
| 66 |
linebuf->return_normal_strings = true; /* This is generally what you want, but beware of malicious \0's in input data! */ |
| 67 |
|
| 68 |
linebuf->userdata = userdata; |
| 69 |
|
| 70 |
linebuf->vio = mowgli_vio_create(linebuf); |
| 71 |
|
| 72 |
return linebuf; |
| 73 |
} |
| 74 |
|
| 75 |
/* Attach the linebuf instance to the eventloop -- socket must be created first with VIO instance! */ |
| 76 |
void |
| 77 |
mowgli_linebuf_attach_to_eventloop(mowgli_linebuf_t *linebuf, mowgli_eventloop_t *eventloop) |
| 78 |
{ |
| 79 |
return_if_fail(eventloop != NULL); |
| 80 |
return_if_fail(linebuf != NULL); |
| 81 |
return_if_fail(linebuf->vio != NULL); |
| 82 |
return_if_fail((linebuf->vio->flags & MOWGLI_VIO_FLAGS_ISCLOSED) == 0); |
| 83 |
|
| 84 |
#ifdef NOTYET |
| 85 |
mowgli_vio_eventloop_attach(linebuf->vio, eventloop, &linebuf_evops); |
| 86 |
#else |
| 87 |
mowgli_vio_eventloop_attach(linebuf->vio, eventloop, NULL); |
| 88 |
#endif |
| 89 |
mowgli_pollable_setselect(eventloop, linebuf->vio->io.e, MOWGLI_EVENTLOOP_IO_READ, mowgli_linebuf_read_data); |
| 90 |
mowgli_pollable_setselect(eventloop, linebuf->vio->io.e, MOWGLI_EVENTLOOP_IO_WRITE, mowgli_linebuf_write_data); |
| 91 |
|
| 92 |
linebuf->eventloop = eventloop; |
| 93 |
} |
| 94 |
|
| 95 |
/* Detach the linebuf instance from the eventloop */ |
| 96 |
void |
| 97 |
mowgli_linebuf_detach_from_eventloop(mowgli_linebuf_t *linebuf) |
| 98 |
{ |
| 99 |
return_if_fail(linebuf != NULL); |
| 100 |
return_if_fail(linebuf->eventloop != NULL); |
| 101 |
mowgli_pollable_setselect(linebuf->eventloop, linebuf->vio->io.e, MOWGLI_EVENTLOOP_IO_READ, NULL); |
| 102 |
mowgli_pollable_setselect(linebuf->eventloop, linebuf->vio->io.e, MOWGLI_EVENTLOOP_IO_WRITE, NULL); |
| 103 |
mowgli_vio_eventloop_detach(linebuf->vio); |
| 104 |
linebuf->eventloop = NULL; |
| 105 |
} |
| 106 |
|
| 107 |
void |
| 108 |
mowgli_linebuf_destroy(mowgli_linebuf_t *linebuf) |
| 109 |
{ |
| 110 |
if (linebuf->eventloop != NULL) |
| 111 |
mowgli_linebuf_detach_from_eventloop(linebuf); |
| 112 |
|
| 113 |
mowgli_vio_destroy(linebuf->vio); |
| 114 |
|
| 115 |
mowgli_free(linebuf->readbuf.buffer); |
| 116 |
mowgli_free(linebuf->writebuf.buffer); |
| 117 |
mowgli_heap_free(linebuf_heap, linebuf); |
| 118 |
} |
| 119 |
|
| 120 |
void |
| 121 |
mowgli_linebuf_setbuflen(mowgli_linebuf_buf_t *buffer, size_t buflen) |
| 122 |
{ |
| 123 |
return_if_fail(buffer != NULL); |
| 124 |
|
| 125 |
if (buffer->buffer == NULL) |
| 126 |
{ |
| 127 |
buffer->buffer = mowgli_alloc(buflen); |
| 128 |
} |
| 129 |
else |
| 130 |
{ |
| 131 |
char tmpbuf[buffer->maxbuflen]; |
| 132 |
|
| 133 |
if (buffer->buflen > 0) |
| 134 |
memcpy(tmpbuf, buffer->buffer, buffer->maxbuflen); /* Copy into tmp buffer */ |
| 135 |
|
| 136 |
/* Free old buffer and reallocate */ |
| 137 |
mowgli_free(buffer->buffer); |
| 138 |
buffer->buffer = mowgli_alloc(buflen); |
| 139 |
|
| 140 |
if (buffer->buflen > 0) |
| 141 |
/* Copy into new buffer using old buffer size */ |
| 142 |
memcpy(buffer->buffer, tmpbuf, buffer->maxbuflen); |
| 143 |
} |
| 144 |
|
| 145 |
buffer->maxbuflen = buflen; |
| 146 |
} |
| 147 |
|
| 148 |
void |
| 149 |
mowgli_linebuf_delim(mowgli_linebuf_t *linebuf, const char *delim, const char *endl) |
| 150 |
{ |
| 151 |
return_if_fail(linebuf != NULL); |
| 152 |
return_if_fail(delim != NULL && *delim != '\0'); |
| 153 |
return_if_fail(endl != NULL && *endl != '\0'); |
| 154 |
|
| 155 |
linebuf->delim = delim; |
| 156 |
linebuf->endl = endl; |
| 157 |
linebuf->endl_len = strlen(endl); |
| 158 |
} |
| 159 |
|
| 160 |
static void |
| 161 |
mowgli_linebuf_read_data(mowgli_eventloop_t *eventloop, mowgli_eventloop_io_t *io, mowgli_eventloop_io_dir_t dir, void *userdata) |
| 162 |
{ |
| 163 |
mowgli_linebuf_t *linebuf = (mowgli_linebuf_t *) userdata; |
| 164 |
mowgli_linebuf_buf_t *buffer = &(linebuf->readbuf); |
| 165 |
void *bufpos; |
| 166 |
size_t offset; |
| 167 |
int ret; |
| 168 |
|
| 169 |
if (buffer->maxbuflen - buffer->buflen == 0) |
| 170 |
{ |
| 171 |
linebuf->flags |= MOWGLI_LINEBUF_ERR_READBUF_FULL; |
| 172 |
mowgli_linebuf_error(linebuf->vio); |
| 173 |
return; |
| 174 |
} |
| 175 |
|
| 176 |
bufpos = buffer->buffer + buffer->buflen; |
| 177 |
offset = buffer->maxbuflen - buffer->buflen + 1; |
| 178 |
|
| 179 |
if ((ret = mowgli_vio_read(linebuf->vio, bufpos, offset)) <= 0) |
| 180 |
{ |
| 181 |
if (linebuf->vio->error.type == MOWGLI_VIO_ERR_NONE) |
| 182 |
return; |
| 183 |
|
| 184 |
/* Let's never come back here */ |
| 185 |
mowgli_pollable_setselect(eventloop, io, MOWGLI_EVENTLOOP_IO_READ, NULL); |
| 186 |
mowgli_linebuf_do_shutdown(linebuf); |
| 187 |
return; |
| 188 |
} |
| 189 |
|
| 190 |
/* Le sigh -- stupid edge-triggered interfaces */ |
| 191 |
if (mowgli_vio_hasflag(linebuf->vio, MOWGLI_VIO_FLAGS_NEEDREAD)) |
| 192 |
mowgli_pollable_setselect(eventloop, io, MOWGLI_EVENTLOOP_IO_READ, mowgli_linebuf_read_data); |
| 193 |
|
| 194 |
/* Do we want a write for SSL? */ |
| 195 |
if (mowgli_vio_hasflag(linebuf->vio, MOWGLI_VIO_FLAGS_NEEDWRITE)) |
| 196 |
mowgli_pollable_setselect(eventloop, io, MOWGLI_EVENTLOOP_IO_WRITE, mowgli_linebuf_write_data); |
| 197 |
|
| 198 |
buffer->buflen += ret; |
| 199 |
mowgli_linebuf_process(linebuf); |
| 200 |
} |
| 201 |
|
| 202 |
static void |
| 203 |
mowgli_linebuf_write_data(mowgli_eventloop_t *eventloop, mowgli_eventloop_io_t *io, mowgli_eventloop_io_dir_t dir, void *userdata) |
| 204 |
{ |
| 205 |
mowgli_linebuf_t *linebuf = (mowgli_linebuf_t *) userdata; |
| 206 |
mowgli_linebuf_buf_t *buffer = &(linebuf->writebuf); |
| 207 |
int ret; |
| 208 |
|
| 209 |
if ((ret = mowgli_vio_write(linebuf->vio, buffer->buffer, buffer->buflen)) <= 0) |
| 210 |
if (linebuf->vio->error.code != MOWGLI_VIO_ERR_NONE) |
| 211 |
/* If we have a genuine error, we shouldn't come back to this func |
| 212 |
* Otherwise we'll try again. */ |
| 213 |
if (ret != 0) |
| 214 |
{ |
| 215 |
mowgli_pollable_setselect(eventloop, io, MOWGLI_EVENTLOOP_IO_WRITE, NULL); |
| 216 |
mowgli_log("mowgli_vio_write returned error [%lu]: %s", linebuf->vio->error.code, linebuf->vio->error.string); |
| 217 |
return; |
| 218 |
} |
| 219 |
|
| 220 |
buffer->buflen -= ret; |
| 221 |
|
| 222 |
/* Anything else to write? */ |
| 223 |
if (buffer->buflen == 0) |
| 224 |
{ |
| 225 |
if (!mowgli_vio_hasflag(linebuf->vio, MOWGLI_VIO_FLAGS_NEEDWRITE)) |
| 226 |
mowgli_pollable_setselect(eventloop, io, MOWGLI_EVENTLOOP_IO_WRITE, NULL); |
| 227 |
|
| 228 |
if ((linebuf->flags & MOWGLI_LINEBUF_SHUTTING_DOWN) != 0) |
| 229 |
mowgli_linebuf_do_shutdown(linebuf); |
| 230 |
} |
| 231 |
else |
| 232 |
{ |
| 233 |
mowgli_pollable_setselect(eventloop, io, MOWGLI_EVENTLOOP_IO_WRITE, mowgli_linebuf_write_data); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
void |
| 238 |
mowgli_linebuf_writef(mowgli_linebuf_t *linebuf, const char *format, ...) |
| 239 |
{ |
| 240 |
char buf[linebuf->writebuf.maxbuflen]; |
| 241 |
size_t len; |
| 242 |
va_list va; |
| 243 |
|
| 244 |
va_start(va, format); |
| 245 |
len = vsnprintf(buf, linebuf->writebuf.maxbuflen - 1, format, va); |
| 246 |
va_end(va); |
| 247 |
|
| 248 |
mowgli_linebuf_write(linebuf, buf, len); |
| 249 |
} |
| 250 |
|
| 251 |
void |
| 252 |
mowgli_linebuf_write(mowgli_linebuf_t *linebuf, const char *data, int len) |
| 253 |
{ |
| 254 |
char *ptr = linebuf->writebuf.buffer + linebuf->writebuf.buflen; |
| 255 |
|
| 256 |
return_if_fail(len > 0); |
| 257 |
return_if_fail(data != NULL); |
| 258 |
|
| 259 |
if (linebuf->flags & MOWGLI_LINEBUF_SHUTTING_DOWN) |
| 260 |
return; |
| 261 |
|
| 262 |
if (linebuf->writebuf.buflen + len + linebuf->endl_len > linebuf->writebuf.maxbuflen) |
| 263 |
{ |
| 264 |
linebuf->flags |= MOWGLI_LINEBUF_ERR_WRITEBUF_FULL; |
| 265 |
mowgli_linebuf_error(linebuf->vio); |
| 266 |
return; |
| 267 |
} |
| 268 |
|
| 269 |
memcpy((void *) ptr, data, len); |
| 270 |
memcpy((void *) (ptr + len), linebuf->endl, linebuf->endl_len); |
| 271 |
|
| 272 |
linebuf->writebuf.buflen += len + linebuf->endl_len; |
| 273 |
|
| 274 |
/* Schedule our write */ |
| 275 |
mowgli_pollable_setselect(linebuf->eventloop, linebuf->vio->io.e, MOWGLI_EVENTLOOP_IO_WRITE, mowgli_linebuf_write_data); |
| 276 |
} |
| 277 |
|
| 278 |
void |
| 279 |
mowgli_linebuf_shut_down(mowgli_linebuf_t *linebuf) |
| 280 |
{ |
| 281 |
return_if_fail(linebuf != NULL); |
| 282 |
|
| 283 |
linebuf->flags |= MOWGLI_LINEBUF_SHUTTING_DOWN; |
| 284 |
|
| 285 |
if (linebuf->writebuf.buflen == 0) |
| 286 |
mowgli_linebuf_do_shutdown(linebuf); |
| 287 |
} |
| 288 |
|
| 289 |
static void |
| 290 |
mowgli_linebuf_process(mowgli_linebuf_t *linebuf) |
| 291 |
{ |
| 292 |
mowgli_linebuf_buf_t *buffer = &(linebuf->readbuf); |
| 293 |
|
| 294 |
char *line_start; |
| 295 |
char *cptr; |
| 296 |
size_t len = 0; |
| 297 |
int linecount = 0; |
| 298 |
|
| 299 |
line_start = cptr = buffer->buffer; |
| 300 |
|
| 301 |
/* Initalise */ |
| 302 |
linebuf->flags &= ~MOWGLI_LINEBUF_LINE_HASNULLCHAR; |
| 303 |
|
| 304 |
while (len < buffer->buflen) |
| 305 |
{ |
| 306 |
if (!strchr(linebuf->delim, *cptr)) |
| 307 |
{ |
| 308 |
if (*cptr == '\0') |
| 309 |
/* Warn about unexpected null chars in the string */ |
| 310 |
linebuf->flags |= MOWGLI_LINEBUF_LINE_HASNULLCHAR; |
| 311 |
|
| 312 |
cptr++; |
| 313 |
len++; |
| 314 |
continue; |
| 315 |
} |
| 316 |
|
| 317 |
linecount++; |
| 318 |
|
| 319 |
/* We now have a line */ |
| 320 |
if (linebuf->return_normal_strings) |
| 321 |
*cptr = '\0'; |
| 322 |
|
| 323 |
if ((linebuf->flags & MOWGLI_LINEBUF_SHUTTING_DOWN) == 0) |
| 324 |
linebuf->readline_cb(linebuf, line_start, cptr - line_start, linebuf->userdata); |
| 325 |
|
| 326 |
/* Next line starts here; begin scanning and set the start of it */ |
| 327 |
while (strchr(linebuf->delim, *cptr)) |
| 328 |
{ |
| 329 |
len++; |
| 330 |
cptr++; |
| 331 |
} |
| 332 |
|
| 333 |
line_start = cptr; |
| 334 |
|
| 335 |
/* Reset this for next line */ |
| 336 |
linebuf->flags &= ~MOWGLI_LINEBUF_LINE_HASNULLCHAR; |
| 337 |
} |
| 338 |
|
| 339 |
if ((linecount == 0) && (buffer->buflen == buffer->maxbuflen)) |
| 340 |
{ |
| 341 |
/* No more chars will fit in the buffer and we don't have a line |
| 342 |
* We're really screwed, let's trigger an error. */ |
| 343 |
linebuf->flags |= MOWGLI_LINEBUF_ERR_READBUF_FULL; |
| 344 |
mowgli_linebuf_error(linebuf->vio); |
| 345 |
return; |
| 346 |
} |
| 347 |
|
| 348 |
if (line_start != cptr) |
| 349 |
{ |
| 350 |
buffer->buflen = cptr - line_start; |
| 351 |
memmove(buffer->buffer, line_start, cptr - line_start); |
| 352 |
} |
| 353 |
else |
| 354 |
{ |
| 355 |
buffer->buflen = 0; |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
static void |
| 360 |
mowgli_linebuf_do_shutdown(mowgli_linebuf_t *linebuf) |
| 361 |
{ |
| 362 |
if (linebuf && linebuf->shutdown_cb) |
| 363 |
linebuf->shutdown_cb(linebuf, linebuf->userdata); |
| 364 |
} |
| 365 |
|
| 366 |
static int |
| 367 |
mowgli_linebuf_error(mowgli_vio_t *vio) |
| 368 |
{ |
| 369 |
mowgli_linebuf_t *linebuf = vio->userdata; |
| 370 |
mowgli_vio_error_t *error = &(linebuf->vio->error); |
| 371 |
|
| 372 |
if (linebuf->flags & MOWGLI_LINEBUF_ERR_READBUF_FULL) |
| 373 |
{ |
| 374 |
error->op = MOWGLI_VIO_ERR_OP_READ; |
| 375 |
error->type = MOWGLI_VIO_ERR_CUSTOM; |
| 376 |
mowgli_strlcpy(error->string, "Read buffer full", sizeof(error->string)); |
| 377 |
} |
| 378 |
else if (linebuf->flags & MOWGLI_LINEBUF_ERR_WRITEBUF_FULL) |
| 379 |
{ |
| 380 |
error->op = MOWGLI_VIO_ERR_OP_WRITE; |
| 381 |
error->type = MOWGLI_VIO_ERR_CUSTOM; |
| 382 |
mowgli_strlcpy(error->string, "Write buffer full", sizeof(error->string)); |
| 383 |
} |
| 384 |
|
| 385 |
/* Pass this up to higher callback */ |
| 386 |
return mowgli_vio_error(vio); |
| 387 |
} |