textmessage_receive.c (1005B)
1 /* ISC license. */ 2 3 #include <sys/uio.h> 4 #include <stdint.h> 5 #include <errno.h> 6 #include <skalibs/uint32.h> 7 #include <skalibs/allreadwrite.h> 8 #include <skalibs/buffer.h> 9 #include <skalibs/stralloc.h> 10 #include <skalibs/textmessage.h> 11 12 int textmessage_receive (textmessage_receiver *tr, struct iovec *v) 13 { 14 if (tr->indata.len == tr->wanted) 15 { 16 uint32_t u ; 17 char pack[4] ; 18 if (buffer_len(&tr->in) < 4) 19 { 20 ssize_t r = sanitize_read(buffer_fill(&tr->in)) ; 21 if (r <= 0) return r ; 22 if (buffer_len(&tr->in) < 4) return (errno = EWOULDBLOCK, 0) ; 23 } 24 buffer_getnofill(&tr->in, pack, 4) ; 25 uint32_unpack_big(pack, &u) ; 26 if (u > tr->max) return (errno = EMSGSIZE, -1) ; 27 if (!stralloc_ready(&tr->indata, u)) return -1 ; 28 tr->wanted = u ; 29 tr->indata.len = 0 ; 30 } 31 32 { 33 int r = buffer_getall(&tr->in, tr->indata.s, tr->wanted, &tr->indata.len) ; 34 if (r <= 0) return r ; 35 } 36 37 v->iov_base = tr->indata.s ; 38 v->iov_len = tr->indata.len ; 39 return 1 ; 40 }