]> git.proxmox.com Git - mirror_frr.git/blob - nhrpd/zbuf.c
Merge branch 'stable/2.0'
[mirror_frr.git] / nhrpd / zbuf.c
1 /* Stream/packet buffer API implementation
2 * Copyright (c) 2014-2015 Timo Teräs
3 *
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.
8 */
9
10 #define _GNU_SOURCE
11 #include <string.h>
12 #include <unistd.h>
13 #include <errno.h>
14 #include "zassert.h"
15 #include "zbuf.h"
16 #include "memory.h"
17 #include "nhrpd.h"
18
19 #define ERRNO_IO_RETRY(EN) (((EN) == EAGAIN) || ((EN) == EWOULDBLOCK) || ((EN) == EINTR))
20
21 DEFINE_MTYPE_STATIC(NHRPD, ZBUF_DATA, "NHRPD zbuf data")
22
23 struct zbuf *zbuf_alloc(size_t size)
24 {
25 struct zbuf *zb;
26
27 zb = XMALLOC(MTYPE_ZBUF_DATA, sizeof(*zb) + size);
28 if (!zb)
29 return NULL;
30
31 zbuf_init(zb, zb+1, size, 0);
32 zb->allocated = 1;
33
34 return zb;
35 }
36
37 void zbuf_init(struct zbuf *zb, void *buf, size_t len, size_t datalen)
38 {
39 *zb = (struct zbuf) {
40 .buf = buf,
41 .end = (uint8_t *)buf + len,
42 .head = buf,
43 .tail = (uint8_t *)buf + datalen,
44 };
45 }
46
47 void zbuf_free(struct zbuf *zb)
48 {
49 if (zb->allocated)
50 XFREE(MTYPE_ZBUF_DATA, zb);
51 }
52
53 void zbuf_reset(struct zbuf *zb)
54 {
55 zb->head = zb->tail = zb->buf;
56 zb->error = 0;
57 }
58
59 void zbuf_reset_head(struct zbuf *zb, void *ptr)
60 {
61 zassert((void*)zb->buf <= ptr && ptr <= (void*)zb->tail);
62 zb->head = ptr;
63 }
64
65 static void zbuf_remove_headroom(struct zbuf *zb)
66 {
67 ssize_t headroom = zbuf_headroom(zb);
68 if (!headroom)
69 return;
70 memmove(zb->buf, zb->head, zbuf_used(zb));
71 zb->head -= headroom;
72 zb->tail -= headroom;
73 }
74
75 ssize_t zbuf_read(struct zbuf *zb, int fd, size_t maxlen)
76 {
77 ssize_t r;
78
79 if (zb->error)
80 return -3;
81
82 zbuf_remove_headroom(zb);
83 if (maxlen > zbuf_tailroom(zb))
84 maxlen = zbuf_tailroom(zb);
85
86 r = read(fd, zb->tail, maxlen);
87 if (r > 0) zb->tail += r;
88 else if (r == 0) r = -2;
89 else if (r < 0 && ERRNO_IO_RETRY(errno)) r = 0;
90
91 return r;
92 }
93
94 ssize_t zbuf_write(struct zbuf *zb, int fd)
95 {
96 ssize_t r;
97
98 if (zb->error)
99 return -3;
100
101 r = write(fd, zb->head, zbuf_used(zb));
102 if (r > 0) {
103 zb->head += r;
104 if (zb->head == zb->tail)
105 zbuf_reset(zb);
106 }
107 else if (r == 0) r = -2;
108 else if (r < 0 && ERRNO_IO_RETRY(errno)) r = 0;
109
110 return r;
111 }
112
113 ssize_t zbuf_recv(struct zbuf *zb, int fd)
114 {
115 ssize_t r;
116
117 if (zb->error)
118 return -3;
119
120 zbuf_remove_headroom(zb);
121 r = recv(fd, zb->tail, zbuf_tailroom(zb), 0);
122 if (r > 0) zb->tail += r;
123 else if (r == 0) r = -2;
124 else if (r < 0 && ERRNO_IO_RETRY(errno)) r = 0;
125 return r;
126 }
127
128 ssize_t zbuf_send(struct zbuf *zb, int fd)
129 {
130 ssize_t r;
131
132 if (zb->error)
133 return -3;
134
135 r = send(fd, zb->head, zbuf_used(zb), 0);
136 if (r >= 0)
137 zbuf_reset(zb);
138
139 return r;
140 }
141
142 void *zbuf_may_pull_until(struct zbuf *zb, const char *sep, struct zbuf *msg)
143 {
144 size_t seplen = strlen(sep), len;
145 uint8_t *ptr;
146
147 ptr = memmem(zb->head, zbuf_used(zb), sep, seplen);
148 if (!ptr) return NULL;
149
150 len = ptr - zb->head + seplen;
151 zbuf_init(msg, zbuf_pulln(zb, len), len, len);
152 return msg->head;
153 }
154
155 void zbufq_init(struct zbuf_queue *zbq)
156 {
157 *zbq = (struct zbuf_queue) {
158 .queue_head = LIST_INITIALIZER(zbq->queue_head),
159 };
160 }
161
162 void zbufq_reset(struct zbuf_queue *zbq)
163 {
164 struct zbuf *buf, *bufn;
165
166 list_for_each_entry_safe(buf, bufn, &zbq->queue_head, queue_list) {
167 list_del(&buf->queue_list);
168 zbuf_free(buf);
169 }
170 }
171
172 void zbufq_queue(struct zbuf_queue *zbq, struct zbuf *zb)
173 {
174 list_add_tail(&zb->queue_list, &zbq->queue_head);
175 }
176
177 int zbufq_write(struct zbuf_queue *zbq, int fd)
178 {
179 struct iovec iov[16];
180 struct zbuf *zb, *zbn;
181 ssize_t r;
182 size_t iovcnt = 0;
183
184 list_for_each_entry_safe(zb, zbn, &zbq->queue_head, queue_list) {
185 iov[iovcnt++] = (struct iovec) {
186 .iov_base = zb->head,
187 .iov_len = zbuf_used(zb),
188 };
189 if (iovcnt >= ZEBRA_NUM_OF(iov))
190 break;
191 }
192
193 r = writev(fd, iov, iovcnt);
194 if (r < 0)
195 return r;
196
197 list_for_each_entry_safe(zb, zbn, &zbq->queue_head, queue_list) {
198 if (r < (ssize_t)zbuf_used(zb)) {
199 zb->head += r;
200 return 1;
201 }
202
203 r -= zbuf_used(zb);
204 list_del(&zb->queue_list);
205 zbuf_free(zb);
206 }
207
208 return 0;
209 }
210
211 void zbuf_copy(struct zbuf *zdst, struct zbuf *zsrc, size_t len)
212 {
213 const void *src;
214 void *dst;
215
216 dst = zbuf_pushn(zdst, len);
217 src = zbuf_pulln(zsrc, len);
218 if (!dst || !src) return;
219 memcpy(dst, src, len);
220 }