Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include "compat.h"
19 #include <endian.h>
20 #include <inttypes.h>
21 #include <string.h>
23 #include "9pclib.h"
24 #include "kamid.h"
25 #include "log.h"
26 #include "utils.h"
28 uint16_t iota_tag;
30 struct evbuffer *evb;
32 void
33 write_hdr(uint32_t len, uint8_t type, uint16_t tag)
34 {
35 len += HEADERSIZE;
37 log_debug("enqueuing a packet; len=%"PRIu32" type=%d[%s] tag=%d",
38 len, type, pp_msg_type(type), tag);
40 len = htole32(len);
41 /* type is one byte, no endiannes issues */
42 tag = htole16(tag);
44 evbuffer_add(evb, &len, sizeof(len));
45 evbuffer_add(evb, &type, sizeof(type));
46 evbuffer_add(evb, &tag, sizeof(tag));
47 }
49 void
50 write_hdr_auto(uint32_t len, uint8_t type)
51 {
52 if (++iota_tag == NOTAG)
53 ++iota_tag;
54 write_hdr(len, type, iota_tag);
55 }
57 void
58 write_str(uint16_t len, const char *str)
59 {
60 uint16_t l = len;
62 len = htole16(len);
63 evbuffer_add(evb, &len, sizeof(len));
64 evbuffer_add(evb, str, l);
65 }
67 void
68 write_str_auto(const char *str)
69 {
70 write_str(strlen(str), str);
71 }
73 void
74 write_32(uint32_t fid)
75 {
76 fid = htole32(fid);
77 evbuffer_add(evb, &fid, sizeof(fid));
78 }
80 void
81 write_16(uint16_t tag)
82 {
83 tag = htole16(tag);
84 evbuffer_add(evb, &tag, sizeof(tag));
85 }
89 void
90 tversion(const char *v, uint32_t msize)
91 {
92 uint32_t len;
93 uint16_t sl;
95 sl = strlen(v);
97 /* msize[4] version[s] */
98 len = sizeof(msize) + sizeof(sl) + sl;
99 write_hdr(len, Tversion, NOTAG);
100 write_32(msize);
101 write_str(sl, v);
104 void
105 tattach(uint32_t fid, uint32_t afid, const char *uname, const char *aname)
107 uint32_t len;
108 uint16_t ul, al;
110 ul = strlen(uname);
111 al = strlen(aname);
113 /* fid[4] afid[4] uname[s] aname[s] */
114 len = sizeof(fid) + sizeof(afid) + sizeof(ul) + ul
115 + sizeof(al) + al;
116 write_hdr_auto(len, Tattach);
117 write_fid(fid);
118 write_fid(NOFID);
119 write_str(ul, uname);
120 write_str(al, aname);
123 void
124 tclunk(uint32_t fid)
126 uint32_t len;
128 /* fid[4] */
129 len = sizeof(fid);
130 write_hdr_auto(len, Tclunk);
131 write_fid(fid);
134 void
135 tflush(uint16_t oldtag)
137 uint32_t len;
139 /* oldtag[2] */
140 len = sizeof(oldtag);
141 write_hdr_auto(len, Tflush);
142 write_tag(oldtag);
145 void
146 twalk(uint32_t fid, uint32_t newfid, const char **wnames, size_t nwname)
148 size_t i;
149 uint32_t len;
151 /* fid[4] newfid[4] nwname[2] nwname*(wname[s]) */
152 len = sizeof(fid) + sizeof(newfid) + 2;
153 for (i = 0; i < nwname; ++i)
154 len += 2 + strlen(wnames[i]);
156 write_hdr_auto(len, Twalk);
157 write_fid(fid);
158 write_fid(newfid);
159 write_16(nwname);
160 for (i = 0; i < nwname; ++i)
161 write_str_auto(wnames[i]);