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 9d26dce7 2021-12-15 op write_64(uint64_t x)
76 9d26dce7 2021-12-15 op x = htole64(x);
77 9d26dce7 2021-12-15 op evbuffer_add(evb, &x, sizeof(x));
81 4e83b30f 2021-12-14 op write_32(uint32_t fid)
83 4e83b30f 2021-12-14 op fid = htole32(fid);
84 4e83b30f 2021-12-14 op evbuffer_add(evb, &fid, sizeof(fid));
88 4e83b30f 2021-12-14 op write_16(uint16_t tag)
90 4e83b30f 2021-12-14 op tag = htole16(tag);
91 4e83b30f 2021-12-14 op evbuffer_add(evb, &tag, sizeof(tag));
95 9d26dce7 2021-12-15 op write_8(uint8_t x)
97 9d26dce7 2021-12-15 op evbuffer_add(evb, &x, sizeof(x));
103 4e83b30f 2021-12-14 op tversion(const char *v, uint32_t msize)
105 4e83b30f 2021-12-14 op uint32_t len;
108 4e83b30f 2021-12-14 op sl = strlen(v);
110 4e83b30f 2021-12-14 op /* msize[4] version[s] */
111 4e83b30f 2021-12-14 op len = sizeof(msize) + sizeof(sl) + sl;
112 4e83b30f 2021-12-14 op write_hdr(len, Tversion, NOTAG);
113 4e83b30f 2021-12-14 op write_32(msize);
114 4e83b30f 2021-12-14 op write_str(sl, v);
118 4e83b30f 2021-12-14 op tattach(uint32_t fid, uint32_t afid, const char *uname, const char *aname)
120 4e83b30f 2021-12-14 op uint32_t len;
121 4e83b30f 2021-12-14 op uint16_t ul, al;
123 4e83b30f 2021-12-14 op ul = strlen(uname);
124 4e83b30f 2021-12-14 op al = strlen(aname);
126 4e83b30f 2021-12-14 op /* fid[4] afid[4] uname[s] aname[s] */
127 4e83b30f 2021-12-14 op len = sizeof(fid) + sizeof(afid) + sizeof(ul) + ul
128 4e83b30f 2021-12-14 op + sizeof(al) + al;
129 4e83b30f 2021-12-14 op write_hdr_auto(len, Tattach);
130 4e83b30f 2021-12-14 op write_fid(fid);
131 febaaada 2021-12-16 op write_fid(afid);
132 4e83b30f 2021-12-14 op write_str(ul, uname);
133 4e83b30f 2021-12-14 op write_str(al, aname);
137 4e83b30f 2021-12-14 op tclunk(uint32_t fid)
139 4e83b30f 2021-12-14 op uint32_t len;
142 4e83b30f 2021-12-14 op len = sizeof(fid);
143 4e83b30f 2021-12-14 op write_hdr_auto(len, Tclunk);
144 4e83b30f 2021-12-14 op write_fid(fid);
148 4e83b30f 2021-12-14 op tflush(uint16_t oldtag)
150 4e83b30f 2021-12-14 op uint32_t len;
152 4e83b30f 2021-12-14 op /* oldtag[2] */
153 4e83b30f 2021-12-14 op len = sizeof(oldtag);
154 4e83b30f 2021-12-14 op write_hdr_auto(len, Tflush);
155 4e83b30f 2021-12-14 op write_tag(oldtag);
159 4e83b30f 2021-12-14 op twalk(uint32_t fid, uint32_t newfid, const char **wnames, size_t nwname)
162 4e83b30f 2021-12-14 op uint32_t len;
164 4e83b30f 2021-12-14 op /* fid[4] newfid[4] nwname[2] nwname*(wname[s]) */
165 4e83b30f 2021-12-14 op len = sizeof(fid) + sizeof(newfid) + 2;
166 4e83b30f 2021-12-14 op for (i = 0; i < nwname; ++i)
167 4e83b30f 2021-12-14 op len += 2 + strlen(wnames[i]);
169 4e83b30f 2021-12-14 op write_hdr_auto(len, Twalk);
170 4e83b30f 2021-12-14 op write_fid(fid);
171 4e83b30f 2021-12-14 op write_fid(newfid);
172 4e83b30f 2021-12-14 op write_16(nwname);
173 4e83b30f 2021-12-14 op for (i = 0; i < nwname; ++i)
174 4e83b30f 2021-12-14 op write_str_auto(wnames[i]);
178 9d26dce7 2021-12-15 op topen(uint32_t fid, uint8_t mode)
180 9d26dce7 2021-12-15 op uint32_t len;
182 9d26dce7 2021-12-15 op /* fid[4] mode[1] */
183 9d26dce7 2021-12-15 op len = sizeof(fid) + sizeof(mode);
184 9d26dce7 2021-12-15 op write_hdr_auto(len, Topen);
185 9d26dce7 2021-12-15 op write_fid(fid);
186 9d26dce7 2021-12-15 op write_8(mode);
190 9d26dce7 2021-12-15 op tread(uint32_t fid, uint64_t off, uint32_t count)
192 9d26dce7 2021-12-15 op uint32_t len;
194 9d26dce7 2021-12-15 op /* fid[4] off[8] count[4] */
195 9d26dce7 2021-12-15 op len = sizeof(fid) + sizeof(off) + sizeof(count);
196 9d26dce7 2021-12-15 op write_hdr_auto(len, Tread);
197 9d26dce7 2021-12-15 op write_fid(fid);
198 9d26dce7 2021-12-15 op write_off(off);
199 9d26dce7 2021-12-15 op write_32(count);