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