2 fb1a36c0 2022-01-09 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 fb1a36c0 2022-01-09 op * Permission to use, copy, modify, and distribute this software for any
5 fb1a36c0 2022-01-09 op * purpose with or without fee is hereby granted, provided that the above
6 fb1a36c0 2022-01-09 op * copyright notice and this permission notice appear in all copies.
8 fb1a36c0 2022-01-09 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 fb1a36c0 2022-01-09 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 fb1a36c0 2022-01-09 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 fb1a36c0 2022-01-09 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 fb1a36c0 2022-01-09 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 fb1a36c0 2022-01-09 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 fb1a36c0 2022-01-09 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 bbcba3ed 2022-01-10 op #include "compat.h"
19 fb1a36c0 2022-01-09 op #include <endian.h>
20 fb1a36c0 2022-01-09 op #include <inttypes.h>
21 fb1a36c0 2022-01-09 op #include <string.h>
23 fb1a36c0 2022-01-09 op #include "9pclib.h"
24 fb1a36c0 2022-01-09 op #include "kami.h"
25 fb1a36c0 2022-01-09 op #include "log.h"
26 fb1a36c0 2022-01-09 op #include "utils.h"
28 fb1a36c0 2022-01-09 op uint16_t iota_tag;
30 fb1a36c0 2022-01-09 op struct evbuffer *evb;
33 fb1a36c0 2022-01-09 op write_hdr(uint32_t len, uint8_t type, uint16_t tag)
35 fb1a36c0 2022-01-09 op len += HEADERSIZE;
37 fb1a36c0 2022-01-09 op log_debug("enqueuing a packet; len=%"PRIu32" type=%d[%s] tag=%d",
38 fb1a36c0 2022-01-09 op len, type, pp_msg_type(type), tag);
40 fb1a36c0 2022-01-09 op len = htole32(len);
41 fb1a36c0 2022-01-09 op /* type is one byte, no endiannes issues */
42 fb1a36c0 2022-01-09 op tag = htole16(tag);
44 fb1a36c0 2022-01-09 op evbuffer_add(evb, &len, sizeof(len));
45 fb1a36c0 2022-01-09 op evbuffer_add(evb, &type, sizeof(type));
46 fb1a36c0 2022-01-09 op evbuffer_add(evb, &tag, sizeof(tag));
50 fb1a36c0 2022-01-09 op write_hdr_auto(uint32_t len, uint8_t type)
52 fb1a36c0 2022-01-09 op if (++iota_tag == NOTAG)
54 fb1a36c0 2022-01-09 op write_hdr(len, type, iota_tag);
58 fb1a36c0 2022-01-09 op write_str(uint16_t len, const char *str)
60 fb1a36c0 2022-01-09 op uint16_t l = len;
62 fb1a36c0 2022-01-09 op len = htole16(len);
63 fb1a36c0 2022-01-09 op evbuffer_add(evb, &len, sizeof(len));
64 fb1a36c0 2022-01-09 op evbuffer_add(evb, str, l);
68 fb1a36c0 2022-01-09 op write_str_auto(const char *str)
70 fb1a36c0 2022-01-09 op write_str(strlen(str), str);
74 fb1a36c0 2022-01-09 op write_buf(const void *d, uint32_t len)
76 fb1a36c0 2022-01-09 op write_32(len);
77 fb1a36c0 2022-01-09 op evbuffer_add(evb, d, len);
81 fb1a36c0 2022-01-09 op write_64(uint64_t x)
83 fb1a36c0 2022-01-09 op x = htole64(x);
84 fb1a36c0 2022-01-09 op evbuffer_add(evb, &x, sizeof(x));
88 fb1a36c0 2022-01-09 op write_32(uint32_t fid)
90 fb1a36c0 2022-01-09 op fid = htole32(fid);
91 fb1a36c0 2022-01-09 op evbuffer_add(evb, &fid, sizeof(fid));
95 fb1a36c0 2022-01-09 op write_16(uint16_t tag)
97 fb1a36c0 2022-01-09 op tag = htole16(tag);
98 fb1a36c0 2022-01-09 op evbuffer_add(evb, &tag, sizeof(tag));
102 fb1a36c0 2022-01-09 op write_8(uint8_t x)
104 fb1a36c0 2022-01-09 op evbuffer_add(evb, &x, sizeof(x));
110 fb1a36c0 2022-01-09 op tversion(const char *v, uint32_t msize)
112 fb1a36c0 2022-01-09 op uint32_t len;
115 fb1a36c0 2022-01-09 op sl = strlen(v);
117 fb1a36c0 2022-01-09 op /* msize[4] version[s] */
118 fb1a36c0 2022-01-09 op len = sizeof(msize) + sizeof(sl) + sl;
119 fb1a36c0 2022-01-09 op write_hdr(len, Tversion, NOTAG);
120 fb1a36c0 2022-01-09 op write_32(msize);
121 fb1a36c0 2022-01-09 op write_str(sl, v);
125 fb1a36c0 2022-01-09 op tattach(uint32_t fid, uint32_t afid, const char *uname, const char *aname)
127 fb1a36c0 2022-01-09 op uint32_t len;
128 fb1a36c0 2022-01-09 op uint16_t ul, al;
130 fb1a36c0 2022-01-09 op ul = strlen(uname);
131 fb1a36c0 2022-01-09 op al = strlen(aname);
133 fb1a36c0 2022-01-09 op /* fid[4] afid[4] uname[s] aname[s] */
134 fb1a36c0 2022-01-09 op len = sizeof(fid) + sizeof(afid) + sizeof(ul) + ul
135 fb1a36c0 2022-01-09 op + sizeof(al) + al;
136 fb1a36c0 2022-01-09 op write_hdr_auto(len, Tattach);
137 fb1a36c0 2022-01-09 op write_fid(fid);
138 fb1a36c0 2022-01-09 op write_fid(afid);
139 fb1a36c0 2022-01-09 op write_str(ul, uname);
140 fb1a36c0 2022-01-09 op write_str(al, aname);
144 fb1a36c0 2022-01-09 op tclunk(uint32_t fid)
146 fb1a36c0 2022-01-09 op uint32_t len;
149 fb1a36c0 2022-01-09 op len = sizeof(fid);
150 fb1a36c0 2022-01-09 op write_hdr_auto(len, Tclunk);
151 fb1a36c0 2022-01-09 op write_fid(fid);
155 fb1a36c0 2022-01-09 op tflush(uint16_t oldtag)
157 fb1a36c0 2022-01-09 op uint32_t len;
159 fb1a36c0 2022-01-09 op /* oldtag[2] */
160 fb1a36c0 2022-01-09 op len = sizeof(oldtag);
161 fb1a36c0 2022-01-09 op write_hdr_auto(len, Tflush);
162 fb1a36c0 2022-01-09 op write_tag(oldtag);
166 fb1a36c0 2022-01-09 op twalk(uint32_t fid, uint32_t newfid, const char **wnames, size_t nwname)
169 fb1a36c0 2022-01-09 op uint32_t len;
171 fb1a36c0 2022-01-09 op /* fid[4] newfid[4] nwname[2] nwname*(wname[s]) */
172 fb1a36c0 2022-01-09 op len = sizeof(fid) + sizeof(newfid) + 2;
173 fb1a36c0 2022-01-09 op for (i = 0; i < nwname; ++i)
174 fb1a36c0 2022-01-09 op len += 2 + strlen(wnames[i]);
176 fb1a36c0 2022-01-09 op write_hdr_auto(len, Twalk);
177 fb1a36c0 2022-01-09 op write_fid(fid);
178 fb1a36c0 2022-01-09 op write_fid(newfid);
179 fb1a36c0 2022-01-09 op write_16(nwname);
180 fb1a36c0 2022-01-09 op for (i = 0; i < nwname; ++i)
181 fb1a36c0 2022-01-09 op write_str_auto(wnames[i]);
185 fb1a36c0 2022-01-09 op topen(uint32_t fid, uint8_t mode)
187 fb1a36c0 2022-01-09 op uint32_t len;
189 fb1a36c0 2022-01-09 op /* fid[4] mode[1] */
190 fb1a36c0 2022-01-09 op len = sizeof(fid) + sizeof(mode);
191 fb1a36c0 2022-01-09 op write_hdr_auto(len, Topen);
192 fb1a36c0 2022-01-09 op write_fid(fid);
193 fb1a36c0 2022-01-09 op write_8(mode);
197 fb1a36c0 2022-01-09 op tcreate(uint32_t fid, const char *name, uint32_t perm, uint8_t mode)
199 fb1a36c0 2022-01-09 op uint32_t len;
202 fb1a36c0 2022-01-09 op /* fid[4] name[s] perm[4] mode[1] */
203 fb1a36c0 2022-01-09 op nl = strlen(name);
204 fb1a36c0 2022-01-09 op len = sizeof(fid) + sizeof(nl) + nl + sizeof(perm) + sizeof(mode);
205 fb1a36c0 2022-01-09 op write_hdr_auto(len, Tcreate);
206 fb1a36c0 2022-01-09 op write_fid(fid);
207 fb1a36c0 2022-01-09 op write_str(nl, name);
208 fb1a36c0 2022-01-09 op write_32(perm);
209 fb1a36c0 2022-01-09 op write_8(mode);
213 fb1a36c0 2022-01-09 op tread(uint32_t fid, uint64_t off, uint32_t count)
215 fb1a36c0 2022-01-09 op uint32_t len;
217 fb1a36c0 2022-01-09 op /* fid[4] off[8] count[4] */
218 fb1a36c0 2022-01-09 op len = sizeof(fid) + sizeof(off) + sizeof(count);
219 fb1a36c0 2022-01-09 op write_hdr_auto(len, Tread);
220 fb1a36c0 2022-01-09 op write_fid(fid);
221 fb1a36c0 2022-01-09 op write_off(off);
222 fb1a36c0 2022-01-09 op write_32(count);
226 fb1a36c0 2022-01-09 op twrite(uint32_t fid, uint64_t off, const void *data, uint32_t count)
228 fb1a36c0 2022-01-09 op uint32_t len;
230 fb1a36c0 2022-01-09 op /* fid[4] off[8] count[4] data[count] */
231 fb1a36c0 2022-01-09 op len = sizeof(fid) + sizeof(off) + sizeof(count) + count;
232 fb1a36c0 2022-01-09 op write_hdr_auto(len, Twrite);
233 fb1a36c0 2022-01-09 op write_fid(fid);
234 fb1a36c0 2022-01-09 op write_off(off);
235 fb1a36c0 2022-01-09 op write_buf(data, count);
239 fb1a36c0 2022-01-09 op tstat(uint32_t fid)
242 fb1a36c0 2022-01-09 op write_hdr_auto(sizeof(fid), Tstat);
243 fb1a36c0 2022-01-09 op write_fid(fid);
247 fb1a36c0 2022-01-09 op tremove(uint32_t fid)
250 fb1a36c0 2022-01-09 op write_hdr_auto(sizeof(fid), Tremove);
251 fb1a36c0 2022-01-09 op write_fid(fid);