summaryrefslogtreecommitdiff
path: root/src/libunixonacid
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2017-12-21 11:18:28 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2017-12-21 11:18:28 +0000
commit2990ce9b390ec1e2bfa1c043c406878e8aff86dd (patch)
tree270e5e2bfa3cdb2fc795039fc45901d93290567e /src/libunixonacid
parent933e986a9207d2b61c5119e18603b44b924e7226 (diff)
downloadskalibs-2990ce9b390ec1e2bfa1c043c406878e8aff86dd.tar.xz
Big 2.6.3.0 reorganization
- Add libposixplz, update headers - Add memmem and friends - Add textmessage to libunixonacid - Update some sysdeps tests
Diffstat (limited to 'src/libunixonacid')
-rw-r--r--src/libunixonacid/textmessage_handle.c20
-rw-r--r--src/libunixonacid/textmessage_put.c21
-rw-r--r--src/libunixonacid/textmessage_putv.c22
-rw-r--r--src/libunixonacid/textmessage_receive.c40
-rw-r--r--src/libunixonacid/textmessage_receiver_0.c10
-rw-r--r--src/libunixonacid/textmessage_receiver_free.c10
-rw-r--r--src/libunixonacid/textmessage_receiver_hasmsginbuf.c21
-rw-r--r--src/libunixonacid/textmessage_receiver_init.c14
-rw-r--r--src/libunixonacid/textmessage_receiver_zero.c5
-rw-r--r--src/libunixonacid/textmessage_sender_1.c7
-rw-r--r--src/libunixonacid/textmessage_sender_flush.c9
-rw-r--r--src/libunixonacid/textmessage_sender_getfd.c9
-rw-r--r--src/libunixonacid/textmessage_sender_timed_flush.c15
-rw-r--r--src/libunixonacid/textmessage_sender_x.c7
-rw-r--r--src/libunixonacid/textmessage_sender_zero.c5
-rw-r--r--src/libunixonacid/textmessage_timed_handle.c29
-rw-r--r--src/libunixonacid/textmessage_timed_receive.c29
17 files changed, 273 insertions, 0 deletions
diff --git a/src/libunixonacid/textmessage_handle.c b/src/libunixonacid/textmessage_handle.c
new file mode 100644
index 0000000..bec62cb
--- /dev/null
+++ b/src/libunixonacid/textmessage_handle.c
@@ -0,0 +1,20 @@
+/* ISC license. */
+
+#include <sys/uio.h>
+#include <skalibs/textmessage.h>
+
+int textmessage_handle (textmessage_receiver_t *tr, textmessage_handler_func_t_ref f, void *p)
+{
+ unsigned int count = 0 ;
+ while (count < TEXTMESSAGE_MAXREADS || textmessage_receiver_hasmsginbuf(tr))
+ {
+ struct iovec v ;
+ int r = textmessage_receive(tr, &v) ;
+ if (r < 0) return -1 ;
+ if (!r) break ;
+ r = (*f)(&v, p) ;
+ if (r <= 0) return r-2 ;
+ count++ ;
+ }
+ return (int)count ;
+}
diff --git a/src/libunixonacid/textmessage_put.c b/src/libunixonacid/textmessage_put.c
new file mode 100644
index 0000000..a6eb6ca
--- /dev/null
+++ b/src/libunixonacid/textmessage_put.c
@@ -0,0 +1,21 @@
+/* ISC license. */
+
+#include <sys/uio.h>
+#include <errno.h>
+#include <stdint.h>
+#include <skalibs/uint32.h>
+#include <skalibs/bufalloc.h>
+#include <skalibs/textmessage.h>
+
+int textmessage_put (textmessage_sender_t *ts, char const *s, size_t len)
+{
+ char pack[4] ;
+ struct iovec v[2] =
+ {
+ { .iov_base = pack, .iov_len = 4 },
+ { .iov_base = (char *)s, .iov_len = len }
+ } ;
+ if (len > UINT32_MAX) return (errno = EINVAL, 0) ;
+ uint32_pack_big(pack, (uint32_t)len) ;
+ return bufalloc_putv(&ts->out, v, 2) ;
+}
diff --git a/src/libunixonacid/textmessage_putv.c b/src/libunixonacid/textmessage_putv.c
new file mode 100644
index 0000000..c144505
--- /dev/null
+++ b/src/libunixonacid/textmessage_putv.c
@@ -0,0 +1,22 @@
+/* ISC license. */
+
+#include <sys/uio.h>
+#include <errno.h>
+#include <stdint.h>
+#include <skalibs/uint32.h>
+#include <skalibs/bufalloc.h>
+#include <skalibs/siovec.h>
+#include <skalibs/textmessage.h>
+
+int textmessage_putv (textmessage_sender_t *ts, struct iovec const *v, unsigned int n)
+{
+ size_t len = siovec_len(v, n) ;
+ char pack[4] ;
+ struct iovec vv[n+1] ;
+ if (len > UINT32_MAX) return (errno = EINVAL, 0) ;
+ vv[0].iov_base = pack ;
+ vv[0].iov_len = 4 ;
+ for (unsigned int i = 0 ; i < n ; i++) vv[i+1] = v[i] ;
+ uint32_pack_big(pack, (uint32_t)len) ;
+ return bufalloc_putv(&ts->out, vv, n+1) ;
+}
diff --git a/src/libunixonacid/textmessage_receive.c b/src/libunixonacid/textmessage_receive.c
new file mode 100644
index 0000000..1411f9a
--- /dev/null
+++ b/src/libunixonacid/textmessage_receive.c
@@ -0,0 +1,40 @@
+/* ISC license. */
+
+#include <sys/uio.h>
+#include <stdint.h>
+#include <errno.h>
+#include <skalibs/uint32.h>
+#include <skalibs/allreadwrite.h>
+#include <skalibs/buffer.h>
+#include <skalibs/stralloc.h>
+#include <skalibs/textmessage.h>
+
+int textmessage_receive (textmessage_receiver_t *tr, struct iovec *v)
+{
+ if (tr->indata.len == tr->wanted)
+ {
+ uint32_t u ;
+ char pack[4] ;
+ if (buffer_len(&tr->in) < 4)
+ {
+ ssize_t r = sanitize_read(buffer_fill(&tr->in)) ;
+ if (r <= 0) return r ;
+ if (buffer_len(&tr->in) < 4) return (errno = EWOULDBLOCK, 0) ;
+ }
+ buffer_getnofill(&tr->in, pack, 4) ;
+ uint32_unpack_big(pack, &u) ;
+ if (u > tr->max) return (errno = EMSGSIZE, -1) ;
+ if (!stralloc_ready(&tr->indata, u)) return -1 ;
+ tr->wanted = u ;
+ tr->indata.len = 0 ;
+ }
+
+ {
+ int r = buffer_getall(&tr->in, tr->indata.s, tr->wanted, &tr->indata.len) ;
+ if (r <= 0) return r ;
+ }
+
+ v->iov_base = tr->indata.s ;
+ v->iov_len = tr->indata.len ;
+ return 1 ;
+}
diff --git a/src/libunixonacid/textmessage_receiver_0.c b/src/libunixonacid/textmessage_receiver_0.c
new file mode 100644
index 0000000..f21b396
--- /dev/null
+++ b/src/libunixonacid/textmessage_receiver_0.c
@@ -0,0 +1,10 @@
+/* ISC license. */
+
+/* MT-unsafe */
+
+#include <stdint.h>
+#include <skalibs/buffer.h>
+#include <skalibs/textmessage.h>
+
+static char buf[BUFFER_INSIZE] ;
+textmessage_receiver_t textmessage_receiver_0_ = TEXTMESSAGE_RECEIVER_INIT(0, buf, BUFFER_INSIZE, UINT32_MAX) ;
diff --git a/src/libunixonacid/textmessage_receiver_free.c b/src/libunixonacid/textmessage_receiver_free.c
new file mode 100644
index 0000000..037e23b
--- /dev/null
+++ b/src/libunixonacid/textmessage_receiver_free.c
@@ -0,0 +1,10 @@
+/* ISC license. */
+
+#include <skalibs/stralloc.h>
+#include <skalibs/textmessage.h>
+
+void textmessage_receiver_free (textmessage_receiver_t *ts)
+{
+ stralloc_free(&ts->indata) ;
+ *ts = textmessage_receiver_zero ;
+}
diff --git a/src/libunixonacid/textmessage_receiver_hasmsginbuf.c b/src/libunixonacid/textmessage_receiver_hasmsginbuf.c
new file mode 100644
index 0000000..d0cdcc3
--- /dev/null
+++ b/src/libunixonacid/textmessage_receiver_hasmsginbuf.c
@@ -0,0 +1,21 @@
+/* ISC license. */
+
+#include <stdint.h>
+#include <sys/uio.h>
+#include <skalibs/uint32.h>
+#include <skalibs/siovec.h>
+#include <skalibs/buffer.h>
+#include <skalibs/textmessage.h>
+
+int textmessage_receiver_hasmsginbuf (textmessage_receiver_t const *tr)
+{
+ size_t len = buffer_len(&tr->in) ;
+ uint32_t n ;
+ char pack[4] ;
+ struct iovec v[2] ;
+ if (len < 4) return 0 ;
+ buffer_rpeek(&tr->in, v) ;
+ siovec_gather(v, 2, pack, 4) ;
+ uint32_unpack_big(pack, &n) ;
+ return len - 4 >= n ;
+}
diff --git a/src/libunixonacid/textmessage_receiver_init.c b/src/libunixonacid/textmessage_receiver_init.c
new file mode 100644
index 0000000..8a29d68
--- /dev/null
+++ b/src/libunixonacid/textmessage_receiver_init.c
@@ -0,0 +1,14 @@
+/* ISC license. */
+
+#include <skalibs/buffer.h>
+#include <skalibs/stralloc.h>
+#include <skalibs/textmessage.h>
+
+int textmessage_receiver_init (textmessage_receiver_t *tr, int fd, char *buf, size_t buflen, uint32_t max)
+{
+ if (!buffer_init(&tr->in, &buffer_read, fd, buf, buflen)) return 0 ;
+ tr->indata = stralloc_zero ;
+ tr->wanted = 0 ;
+ tr->max = max ;
+ return 1 ;
+}
diff --git a/src/libunixonacid/textmessage_receiver_zero.c b/src/libunixonacid/textmessage_receiver_zero.c
new file mode 100644
index 0000000..1e852e5
--- /dev/null
+++ b/src/libunixonacid/textmessage_receiver_zero.c
@@ -0,0 +1,5 @@
+/* ISC license. */
+
+#include <skalibs/textmessage.h>
+
+textmessage_receiver_t const textmessage_receiver_zero = TEXTMESSAGE_RECEIVER_ZERO ;
diff --git a/src/libunixonacid/textmessage_sender_1.c b/src/libunixonacid/textmessage_sender_1.c
new file mode 100644
index 0000000..0be2ae9
--- /dev/null
+++ b/src/libunixonacid/textmessage_sender_1.c
@@ -0,0 +1,7 @@
+/* ISC license. */
+
+/* MT-unsafe */
+
+#include <skalibs/textmessage.h>
+
+textmessage_sender_t textmessage_sender_1_ = TEXTMESSAGE_SENDER_INIT(1) ;
diff --git a/src/libunixonacid/textmessage_sender_flush.c b/src/libunixonacid/textmessage_sender_flush.c
new file mode 100644
index 0000000..53bd407
--- /dev/null
+++ b/src/libunixonacid/textmessage_sender_flush.c
@@ -0,0 +1,9 @@
+/* ISC license. */
+
+#include <skalibs/bufalloc.h>
+#include <skalibs/textmessage.h>
+
+int textmessage_sender_flush (textmessage_sender_t *ts)
+{
+ return bufalloc_flush(&ts->out) ;
+}
diff --git a/src/libunixonacid/textmessage_sender_getfd.c b/src/libunixonacid/textmessage_sender_getfd.c
new file mode 100644
index 0000000..8a6ddf7
--- /dev/null
+++ b/src/libunixonacid/textmessage_sender_getfd.c
@@ -0,0 +1,9 @@
+/* ISC license. */
+
+#include <skalibs/bufalloc.h>
+#include <skalibs/textmessage.h>
+
+int textmessage_sender_getfd (textmessage_sender_t const *ts)
+{
+ return bufalloc_fd(&ts->out) ;
+}
diff --git a/src/libunixonacid/textmessage_sender_timed_flush.c b/src/libunixonacid/textmessage_sender_timed_flush.c
new file mode 100644
index 0000000..18eddd2
--- /dev/null
+++ b/src/libunixonacid/textmessage_sender_timed_flush.c
@@ -0,0 +1,15 @@
+/* ISC license. */
+
+#include <skalibs/functypes.h>
+#include <skalibs/unix-timed.h>
+#include <skalibs/textmessage.h>
+
+static int textmessage_sender_isnonempty (textmessage_sender_t *ts)
+{
+ return !textmessage_sender_isempty(ts) ;
+}
+
+int textmessage_sender_timed_flush (textmessage_sender_t *ts, tain_t const *deadline, tain_t *stamp)
+{
+ return timed_flush(ts, (initfunc_t_ref)&textmessage_sender_getfd, (initfunc_t_ref)&textmessage_sender_isnonempty, (initfunc_t_ref)&textmessage_sender_flush, deadline, stamp) ;
+}
diff --git a/src/libunixonacid/textmessage_sender_x.c b/src/libunixonacid/textmessage_sender_x.c
new file mode 100644
index 0000000..2f6c557
--- /dev/null
+++ b/src/libunixonacid/textmessage_sender_x.c
@@ -0,0 +1,7 @@
+/* ISC license. */
+
+/* MT-unsafe */
+
+#include <skalibs/textmessage.h>
+
+textmessage_sender_t textmessage_sender_x_ = TEXTMESSAGE_SENDER_ZERO ;
diff --git a/src/libunixonacid/textmessage_sender_zero.c b/src/libunixonacid/textmessage_sender_zero.c
new file mode 100644
index 0000000..fbb2921
--- /dev/null
+++ b/src/libunixonacid/textmessage_sender_zero.c
@@ -0,0 +1,5 @@
+/* ISC license. */
+
+#include <skalibs/textmessage.h>
+
+textmessage_sender_t const textmessage_sender_zero = TEXTMESSAGE_SENDER_ZERO ;
diff --git a/src/libunixonacid/textmessage_timed_handle.c b/src/libunixonacid/textmessage_timed_handle.c
new file mode 100644
index 0000000..f8cf2dd
--- /dev/null
+++ b/src/libunixonacid/textmessage_timed_handle.c
@@ -0,0 +1,29 @@
+/* ISC license. */
+
+#include <skalibs/functypes.h>
+#include <skalibs/unix-timed.h>
+#include <skalibs/textmessage.h>
+
+typedef struct textmessage_handler_blah_s textmessage_handler_blah_t, *textmessage_handler_blah_t_ref ;
+struct textmessage_handler_blah_s
+{
+ textmessage_receiver_t *tr ;
+ textmessage_handler_func_t_ref f ;
+ void *p ;
+} ;
+
+static int getfd (textmessage_handler_blah_t *blah)
+{
+ return textmessage_receiver_fd(blah->tr) ;
+}
+
+static ssize_t get (textmessage_handler_blah_t *blah)
+{
+ return textmessage_handle(blah->tr, blah->f, blah->p) ;
+}
+
+int textmessage_timed_handle (textmessage_receiver_t *tr, textmessage_handler_func_t_ref f, void *p, tain_t const *deadline, tain_t *stamp)
+{
+ textmessage_handler_blah_t blah = { .tr = tr, .f = f, .p = p } ;
+ return timed_get(&blah, (initfunc_t_ref)&getfd, (getfunc_t_ref)&get, deadline, stamp) ;
+}
diff --git a/src/libunixonacid/textmessage_timed_receive.c b/src/libunixonacid/textmessage_timed_receive.c
new file mode 100644
index 0000000..5c7a848
--- /dev/null
+++ b/src/libunixonacid/textmessage_timed_receive.c
@@ -0,0 +1,29 @@
+/* ISC license. */
+
+#include <sys/uio.h>
+#include <skalibs/functypes.h>
+#include <skalibs/unix-timed.h>
+#include <skalibs/textmessage.h>
+
+typedef struct textmessage_get_s textmessage_get_t, *textmessage_get_t_ref ;
+struct textmessage_get_s
+{
+ textmessage_receiver_t *tr ;
+ struct iovec *v ;
+} ;
+
+static int getfd (textmessage_get_t *g)
+{
+ return textmessage_receiver_fd(g->tr) ;
+}
+
+static ssize_t get (textmessage_get_t *g)
+{
+ return textmessage_receive(g->tr, g->v) ;
+}
+
+int textmessage_timed_receive (textmessage_receiver_t *tr, struct iovec *v, tain_t const *deadline, tain_t *stamp)
+{
+ textmessage_get_t g = { .tr = tr, .v = v } ;
+ return timed_get(&g, (initfunc_t_ref)&getfd, (getfunc_t_ref)&get, deadline, stamp) ;
+}