summaryrefslogtreecommitdiff
path: root/usr.sbin/puffs/mount_9p/ninebuf.c
blob: daf2e9327d59fc447a2291073be86fb0177f3258 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*      $NetBSD: ninebuf.c,v 1.9 2019/10/28 02:59:25 ozaki-r Exp $	*/

/*
 * Copyright (c) 2006, 2007  Antti Kantee.  All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: ninebuf.c,v 1.9 2019/10/28 02:59:25 ozaki-r Exp $");
#endif /* !lint */

#include <sys/types.h>
#include <sys/time.h>
#include <sys/vnode.h>
#include <sys/socket.h>

#include <err.h>
#include <errno.h>
#include <stdlib.h>
#include <util.h>
#include <unistd.h>

#include "ninepuffs.h"

#define CHECK(v) if (!(v)) abort()

uint8_t
p9pbuf_get_type(struct puffs_framebuf *pb)
{
	uint8_t val;

	puffs_framebuf_getdata_atoff(pb, 4, &val, 1);
	return val;
}

uint16_t
p9pbuf_get_tag(struct puffs_framebuf *pb)
{
	uint16_t val;

	puffs_framebuf_getdata_atoff(pb, 5, &val, 2);
	return le16toh(val);
}

static uint32_t
p9pbuf_get_len(struct puffs_framebuf *pb)
{
	uint32_t val;

	puffs_framebuf_getdata_atoff(pb, 0, &val, 4);
	return le32toh(val);
}

#define CUROFF(pb) (puffs_framebuf_telloff(pb))
int
p9pbuf_read(struct puffs_usermount *pu, struct puffs_framebuf *pb,
	int fd, int *done)
{
	void *win;
	ssize_t n;
	size_t howmuch, winlen;
	int lenstate;

 the_next_level:
	if ((lenstate = (CUROFF(pb) < 4)))
		howmuch = 4 - CUROFF(pb);
	else
		howmuch = p9pbuf_get_len(pb) - CUROFF(pb);

	if (puffs_framebuf_reserve_space(pb, howmuch) == -1)
		return errno;

	while (howmuch) {
		winlen = howmuch;
		if (puffs_framebuf_getwindow(pb, CUROFF(pb), &win, &winlen)==-1)
			return errno;
		n = read(fd, win, winlen);
		switch (n) {
		case 0:
			return ECONNRESET;
		case -1:
			if (errno == EAGAIN)
				return 0;
			return errno;
		default:
			howmuch -= n;
			puffs_framebuf_seekset(pb, CUROFF(pb) + n);
			break;
		}
	}

	if (!lenstate) {
		puffs_framebuf_seekset(pb, 7);
		*done = 1;
		return 0;
	} else
		goto the_next_level;
}

int
p9pbuf_write(struct puffs_usermount *pu, struct puffs_framebuf *pb,
	int fd, int *done)
{
	void *win;
	ssize_t n;
	size_t winlen, howmuch;

	if (CUROFF(pb) == 0) {
		uint32_t len;

		len = htole32(puffs_framebuf_tellsize(pb));
		puffs_framebuf_putdata_atoff(pb, 0, &len, 4);
	}

	howmuch = puffs_framebuf_tellsize(pb) - CUROFF(pb);
	while (howmuch) {
		winlen = howmuch;
		if (puffs_framebuf_getwindow(pb, CUROFF(pb), &win, &winlen)==-1)
			return errno;
		n = write(fd, win, winlen);
		switch (n) {
		case 0:
			return ECONNRESET;
		case -1:
			if (errno == EAGAIN)
				return 0;
			return errno;
		default:
			howmuch -= n;
			puffs_framebuf_seekset(pb, CUROFF(pb) + n);
			break;
		}
	}

	*done = 1;
	return 0;
}
#undef CUROFF

int
p9pbuf_cmp(struct puffs_usermount *pu,
	struct puffs_framebuf *c1, struct puffs_framebuf *c2, int *notresp)
{

	return p9pbuf_get_tag(c1) != p9pbuf_get_tag(c2);
}

struct puffs_framebuf *
p9pbuf_makeout()
{
	struct puffs_framebuf *pb;

	pb = puffs_framebuf_make();
	puffs_framebuf_seekset(pb, 4);
	return pb;
}

void
p9pbuf_recycleout(struct puffs_framebuf *pb)
{

	puffs_framebuf_recycle(pb);
	puffs_framebuf_seekset(pb, 4);
}

void
p9pbuf_put_1(struct puffs_framebuf *pb, uint8_t val)
{
	int rv;

	rv = puffs_framebuf_putdata(pb, &val, 1);
	CHECK(rv == 0);
}

void
p9pbuf_put_2(struct puffs_framebuf *pb, uint16_t val)
{
	int rv;

	HTOLE16(val);
	rv = puffs_framebuf_putdata(pb, &val, 2);
	CHECK(rv == 0);
}

void
p9pbuf_put_4(struct puffs_framebuf *pb, uint32_t val)
{
	int rv;

	HTOLE32(val);
	rv = puffs_framebuf_putdata(pb, &val, 4);
	CHECK(rv == 0);
}

void
p9pbuf_put_8(struct puffs_framebuf *pb, uint64_t val)
{
	int rv;

	HTOLE64(val);
	rv = puffs_framebuf_putdata(pb, &val, 8);
	CHECK(rv == 0);
}

void
p9pbuf_put_data(struct puffs_framebuf *pb, const void *data, uint16_t dlen)
{
	int rv;

	p9pbuf_put_2(pb, dlen);
	rv = puffs_framebuf_putdata(pb, data, dlen);
	CHECK(rv == 0);
}

void
p9pbuf_put_str(struct puffs_framebuf *pb, const char *str)
{

	p9pbuf_put_data(pb, str, strlen(str));
}

void
p9pbuf_write_data(struct puffs_framebuf *pb, uint8_t *data, uint32_t dlen)
{
	int rv;

	rv = puffs_framebuf_putdata(pb, data, dlen);
	CHECK(rv == 0);
}

#define ERETURN(rv) return ((rv) == -1 ? errno : 0)

int
p9pbuf_get_1(struct puffs_framebuf *pb, uint8_t *val)
{

	ERETURN(puffs_framebuf_getdata(pb, val, 1));
}

int
p9pbuf_get_2(struct puffs_framebuf *pb, uint16_t *val)
{
	int rv;
	 
	rv = puffs_framebuf_getdata(pb, val, 2);
	LE16TOH(*val);

	ERETURN(rv);
}

int
p9pbuf_get_4(struct puffs_framebuf *pb, uint32_t *val)
{
	int rv;
	 
	rv = puffs_framebuf_getdata(pb, val, 4);
	LE32TOH(*val);

	ERETURN(rv);
}

int
p9pbuf_get_8(struct puffs_framebuf *pb, uint64_t *val)
{
	int rv;
	 
	rv = puffs_framebuf_getdata(pb, val, 8);
	LE64TOH(*val);

	ERETURN(rv);
}

int
p9pbuf_get_data(struct puffs_framebuf *pb, uint8_t **dp, uint16_t *dlenp)
{
        uint8_t *data;              
	uint16_t len;
	int rv;

	rv = p9pbuf_get_2(pb, &len);
	if (rv)
		return errno;

        if (puffs_framebuf_remaining(pb) < len)
                return EPROTO;
 
	if (dp) {
		data = emalloc(len+1);   
		rv = puffs_framebuf_getdata(pb, data, len);
		if (rv) {
			free(data);
			return errno;
		}
		data[len] = '\0';
		*dp = data;
	} else
		puffs_framebuf_seekset(pb, puffs_framebuf_telloff(pb)+len);

	if (dlenp)
		*dlenp = len;

	return 0;
}

int
p9pbuf_read_data(struct puffs_framebuf *pb, uint8_t *buf, uint32_t dlen)
{

	ERETURN(puffs_framebuf_getdata(pb, buf, dlen));
}

int
p9pbuf_get_str(struct puffs_framebuf *pb, char **dp, uint16_t *dlenp)
{

	return p9pbuf_get_data(pb, (uint8_t **)dp, dlenp);
}