1 /* Stream/packet buffer API implementation
2 * Copyright (c) 2014-2015 Timo Teräs
4 * This file is free software: you may copy, redistribute and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
20 #define ERRNO_IO_RETRY(EN) (((EN) == EAGAIN) || ((EN) == EWOULDBLOCK) || ((EN) == EINTR))
22 struct zbuf *zbuf_alloc(size_t size)
26 zb = XMALLOC(MTYPE_STREAM_DATA, sizeof(*zb) + size);
30 zbuf_init(zb, zb+1, size, 0);
36 void zbuf_init(struct zbuf *zb, void *buf, size_t len, size_t datalen)
40 .end = (uint8_t *)buf + len,
42 .tail = (uint8_t *)buf + datalen,
46 void zbuf_free(struct zbuf *zb)
49 XFREE(MTYPE_STREAM_DATA, zb);
52 void zbuf_reset(struct zbuf *zb)
54 zb->head = zb->tail = zb->buf;
58 void zbuf_reset_head(struct zbuf *zb, void *ptr)
60 zassert((void*)zb->buf <= ptr && ptr <= (void*)zb->tail);
64 static void zbuf_remove_headroom(struct zbuf *zb)
66 ssize_t headroom = zbuf_headroom(zb);
69 memmove(zb->buf, zb->head, zbuf_used(zb));
74 ssize_t zbuf_read(struct zbuf *zb, int fd, size_t maxlen)
81 zbuf_remove_headroom(zb);
82 if (maxlen > zbuf_tailroom(zb))
83 maxlen = zbuf_tailroom(zb);
85 r = read(fd, zb->tail, maxlen);
86 if (r > 0) zb->tail += r;
87 else if (r == 0) r = -2;
88 else if (r < 0 && ERRNO_IO_RETRY(errno)) r = 0;
93 ssize_t zbuf_write(struct zbuf *zb, int fd)
100 r = write(fd, zb->head, zbuf_used(zb));
103 if (zb->head == zb->tail)
106 else if (r == 0) r = -2;
107 else if (r < 0 && ERRNO_IO_RETRY(errno)) r = 0;
112 ssize_t zbuf_recv(struct zbuf *zb, int fd)
119 zbuf_remove_headroom(zb);
120 r = recv(fd, zb->tail, zbuf_tailroom(zb), 0);
121 if (r > 0) zb->tail += r;
122 else if (r == 0) r = -2;
123 else if (r < 0 && ERRNO_IO_RETRY(errno)) r = 0;
127 ssize_t zbuf_send(struct zbuf *zb, int fd)
134 r = send(fd, zb->head, zbuf_used(zb), 0);
141 void *zbuf_may_pull_until(struct zbuf *zb, const char *sep, struct zbuf *msg)
143 size_t seplen = strlen(sep), len;
146 ptr = memmem(zb->head, zbuf_used(zb), sep, seplen);
147 if (!ptr) return NULL;
149 len = ptr - zb->head + seplen;
150 zbuf_init(msg, zbuf_pulln(zb, len), len, len);
154 void zbufq_init(struct zbuf_queue *zbq)
156 *zbq = (struct zbuf_queue) {
157 .queue_head = LIST_INITIALIZER(zbq->queue_head),
161 void zbufq_reset(struct zbuf_queue *zbq)
163 struct zbuf *buf, *bufn;
165 list_for_each_entry_safe(buf, bufn, &zbq->queue_head, queue_list) {
166 list_del(&buf->queue_list);
171 void zbufq_queue(struct zbuf_queue *zbq, struct zbuf *zb)
173 list_add_tail(&zb->queue_list, &zbq->queue_head);
176 int zbufq_write(struct zbuf_queue *zbq, int fd)
178 struct iovec iov[16];
179 struct zbuf *zb, *zbn;
183 list_for_each_entry_safe(zb, zbn, &zbq->queue_head, queue_list) {
184 iov[iovcnt++] = (struct iovec) {
185 .iov_base = zb->head,
186 .iov_len = zbuf_used(zb),
188 if (iovcnt >= ZEBRA_NUM_OF(iov))
192 r = writev(fd, iov, iovcnt);
196 list_for_each_entry_safe(zb, zbn, &zbq->queue_head, queue_list) {
197 if (r < (ssize_t)zbuf_used(zb)) {
203 list_del(&zb->queue_list);
210 void zbuf_copy(struct zbuf *zdst, struct zbuf *zsrc, size_t len)
215 dst = zbuf_pushn(zdst, len);
216 src = zbuf_pulln(zsrc, len);
217 if (!dst || !src) return;
218 memcpy(dst, src, len);