1 /* $NetBSD: ninebuf.c,v 1.9 2019/10/28 02:59:25 ozaki-r Exp $ */
2
3 /*
4 * Copyright (c) 2006, 2007 Antti Kantee. All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 #ifndef lint
30 __RCSID("$NetBSD: ninebuf.c,v 1.9 2019/10/28 02:59:25 ozaki-r Exp $");
31 #endif /* !lint */
32
33 #include <sys/types.h>
34 #include <sys/time.h>
35 #include <sys/vnode.h>
36 #include <sys/socket.h>
37
38 #include <err.h>
39 #include <errno.h>
40 #include <stdlib.h>
41 #include <util.h>
42 #include <unistd.h>
43
44 #include "ninepuffs.h"
45
46 #define CHECK(v) if (!(v)) abort()
47
48 uint8_t
p9pbuf_get_type(struct puffs_framebuf * pb)49 p9pbuf_get_type(struct puffs_framebuf *pb)
50 {
51 uint8_t val;
52
53 puffs_framebuf_getdata_atoff(pb, 4, &val, 1);
54 return val;
55 }
56
57 uint16_t
p9pbuf_get_tag(struct puffs_framebuf * pb)58 p9pbuf_get_tag(struct puffs_framebuf *pb)
59 {
60 uint16_t val;
61
62 puffs_framebuf_getdata_atoff(pb, 5, &val, 2);
63 return le16toh(val);
64 }
65
66 static uint32_t
p9pbuf_get_len(struct puffs_framebuf * pb)67 p9pbuf_get_len(struct puffs_framebuf *pb)
68 {
69 uint32_t val;
70
71 puffs_framebuf_getdata_atoff(pb, 0, &val, 4);
72 return le32toh(val);
73 }
74
75 #define CUROFF(pb) (puffs_framebuf_telloff(pb))
76 int
p9pbuf_read(struct puffs_usermount * pu,struct puffs_framebuf * pb,int fd,int * done)77 p9pbuf_read(struct puffs_usermount *pu, struct puffs_framebuf *pb,
78 int fd, int *done)
79 {
80 void *win;
81 ssize_t n;
82 size_t howmuch, winlen;
83 int lenstate;
84
85 the_next_level:
86 if ((lenstate = (CUROFF(pb) < 4)))
87 howmuch = 4 - CUROFF(pb);
88 else
89 howmuch = p9pbuf_get_len(pb) - CUROFF(pb);
90
91 if (puffs_framebuf_reserve_space(pb, howmuch) == -1)
92 return errno;
93
94 while (howmuch) {
95 winlen = howmuch;
96 if (puffs_framebuf_getwindow(pb, CUROFF(pb), &win, &winlen)==-1)
97 return errno;
98 n = read(fd, win, winlen);
99 switch (n) {
100 case 0:
101 return ECONNRESET;
102 case -1:
103 if (errno == EAGAIN)
104 return 0;
105 return errno;
106 default:
107 howmuch -= n;
108 puffs_framebuf_seekset(pb, CUROFF(pb) + n);
109 break;
110 }
111 }
112
113 if (!lenstate) {
114 puffs_framebuf_seekset(pb, 7);
115 *done = 1;
116 return 0;
117 } else
118 goto the_next_level;
119 }
120
121 int
p9pbuf_write(struct puffs_usermount * pu,struct puffs_framebuf * pb,int fd,int * done)122 p9pbuf_write(struct puffs_usermount *pu, struct puffs_framebuf *pb,
123 int fd, int *done)
124 {
125 void *win;
126 ssize_t n;
127 size_t winlen, howmuch;
128
129 if (CUROFF(pb) == 0) {
130 uint32_t len;
131
132 len = htole32(puffs_framebuf_tellsize(pb));
133 puffs_framebuf_putdata_atoff(pb, 0, &len, 4);
134 }
135
136 howmuch = puffs_framebuf_tellsize(pb) - CUROFF(pb);
137 while (howmuch) {
138 winlen = howmuch;
139 if (puffs_framebuf_getwindow(pb, CUROFF(pb), &win, &winlen)==-1)
140 return errno;
141 n = write(fd, win, winlen);
142 switch (n) {
143 case 0:
144 return ECONNRESET;
145 case -1:
146 if (errno == EAGAIN)
147 return 0;
148 return errno;
149 default:
150 howmuch -= n;
151 puffs_framebuf_seekset(pb, CUROFF(pb) + n);
152 break;
153 }
154 }
155
156 *done = 1;
157 return 0;
158 }
159 #undef CUROFF
160
161 int
p9pbuf_cmp(struct puffs_usermount * pu,struct puffs_framebuf * c1,struct puffs_framebuf * c2,int * notresp)162 p9pbuf_cmp(struct puffs_usermount *pu,
163 struct puffs_framebuf *c1, struct puffs_framebuf *c2, int *notresp)
164 {
165
166 return p9pbuf_get_tag(c1) != p9pbuf_get_tag(c2);
167 }
168
169 struct puffs_framebuf *
p9pbuf_makeout()170 p9pbuf_makeout()
171 {
172 struct puffs_framebuf *pb;
173
174 pb = puffs_framebuf_make();
175 puffs_framebuf_seekset(pb, 4);
176 return pb;
177 }
178
179 void
p9pbuf_recycleout(struct puffs_framebuf * pb)180 p9pbuf_recycleout(struct puffs_framebuf *pb)
181 {
182
183 puffs_framebuf_recycle(pb);
184 puffs_framebuf_seekset(pb, 4);
185 }
186
187 void
p9pbuf_put_1(struct puffs_framebuf * pb,uint8_t val)188 p9pbuf_put_1(struct puffs_framebuf *pb, uint8_t val)
189 {
190 int rv;
191
192 rv = puffs_framebuf_putdata(pb, &val, 1);
193 CHECK(rv == 0);
194 }
195
196 void
p9pbuf_put_2(struct puffs_framebuf * pb,uint16_t val)197 p9pbuf_put_2(struct puffs_framebuf *pb, uint16_t val)
198 {
199 int rv;
200
201 HTOLE16(val);
202 rv = puffs_framebuf_putdata(pb, &val, 2);
203 CHECK(rv == 0);
204 }
205
206 void
p9pbuf_put_4(struct puffs_framebuf * pb,uint32_t val)207 p9pbuf_put_4(struct puffs_framebuf *pb, uint32_t val)
208 {
209 int rv;
210
211 HTOLE32(val);
212 rv = puffs_framebuf_putdata(pb, &val, 4);
213 CHECK(rv == 0);
214 }
215
216 void
p9pbuf_put_8(struct puffs_framebuf * pb,uint64_t val)217 p9pbuf_put_8(struct puffs_framebuf *pb, uint64_t val)
218 {
219 int rv;
220
221 HTOLE64(val);
222 rv = puffs_framebuf_putdata(pb, &val, 8);
223 CHECK(rv == 0);
224 }
225
226 void
p9pbuf_put_data(struct puffs_framebuf * pb,const void * data,uint16_t dlen)227 p9pbuf_put_data(struct puffs_framebuf *pb, const void *data, uint16_t dlen)
228 {
229 int rv;
230
231 p9pbuf_put_2(pb, dlen);
232 rv = puffs_framebuf_putdata(pb, data, dlen);
233 CHECK(rv == 0);
234 }
235
236 void
p9pbuf_put_str(struct puffs_framebuf * pb,const char * str)237 p9pbuf_put_str(struct puffs_framebuf *pb, const char *str)
238 {
239
240 p9pbuf_put_data(pb, str, strlen(str));
241 }
242
243 void
p9pbuf_write_data(struct puffs_framebuf * pb,uint8_t * data,uint32_t dlen)244 p9pbuf_write_data(struct puffs_framebuf *pb, uint8_t *data, uint32_t dlen)
245 {
246 int rv;
247
248 rv = puffs_framebuf_putdata(pb, data, dlen);
249 CHECK(rv == 0);
250 }
251
252 #define ERETURN(rv) return ((rv) == -1 ? errno : 0)
253
254 int
p9pbuf_get_1(struct puffs_framebuf * pb,uint8_t * val)255 p9pbuf_get_1(struct puffs_framebuf *pb, uint8_t *val)
256 {
257
258 ERETURN(puffs_framebuf_getdata(pb, val, 1));
259 }
260
261 int
p9pbuf_get_2(struct puffs_framebuf * pb,uint16_t * val)262 p9pbuf_get_2(struct puffs_framebuf *pb, uint16_t *val)
263 {
264 int rv;
265
266 rv = puffs_framebuf_getdata(pb, val, 2);
267 LE16TOH(*val);
268
269 ERETURN(rv);
270 }
271
272 int
p9pbuf_get_4(struct puffs_framebuf * pb,uint32_t * val)273 p9pbuf_get_4(struct puffs_framebuf *pb, uint32_t *val)
274 {
275 int rv;
276
277 rv = puffs_framebuf_getdata(pb, val, 4);
278 LE32TOH(*val);
279
280 ERETURN(rv);
281 }
282
283 int
p9pbuf_get_8(struct puffs_framebuf * pb,uint64_t * val)284 p9pbuf_get_8(struct puffs_framebuf *pb, uint64_t *val)
285 {
286 int rv;
287
288 rv = puffs_framebuf_getdata(pb, val, 8);
289 LE64TOH(*val);
290
291 ERETURN(rv);
292 }
293
294 int
p9pbuf_get_data(struct puffs_framebuf * pb,uint8_t ** dp,uint16_t * dlenp)295 p9pbuf_get_data(struct puffs_framebuf *pb, uint8_t **dp, uint16_t *dlenp)
296 {
297 uint8_t *data;
298 uint16_t len;
299 int rv;
300
301 rv = p9pbuf_get_2(pb, &len);
302 if (rv)
303 return errno;
304
305 if (puffs_framebuf_remaining(pb) < len)
306 return EPROTO;
307
308 if (dp) {
309 data = emalloc(len+1);
310 rv = puffs_framebuf_getdata(pb, data, len);
311 if (rv) {
312 free(data);
313 return errno;
314 }
315 data[len] = '\0';
316 *dp = data;
317 } else
318 puffs_framebuf_seekset(pb, puffs_framebuf_telloff(pb)+len);
319
320 if (dlenp)
321 *dlenp = len;
322
323 return 0;
324 }
325
326 int
p9pbuf_read_data(struct puffs_framebuf * pb,uint8_t * buf,uint32_t dlen)327 p9pbuf_read_data(struct puffs_framebuf *pb, uint8_t *buf, uint32_t dlen)
328 {
329
330 ERETURN(puffs_framebuf_getdata(pb, buf, dlen));
331 }
332
333 int
p9pbuf_get_str(struct puffs_framebuf * pb,char ** dp,uint16_t * dlenp)334 p9pbuf_get_str(struct puffs_framebuf *pb, char **dp, uint16_t *dlenp)
335 {
336
337 return p9pbuf_get_data(pb, (uint8_t **)dp, dlenp);
338 }
339