1use std::io::{Cursor, Read};
15
16pub const TVERSION: u8 = 100;
21pub const RVERSION: u8 = 101;
22pub const TATTACH: u8 = 104;
23pub const RATTACH: u8 = 105;
24pub const RLERROR: u8 = 7;
25pub const TWALK: u8 = 110;
26pub const RWALK: u8 = 111;
27pub const TLOPEN: u8 = 12;
28pub const RLOPEN: u8 = 13;
29pub const TREAD: u8 = 116;
30pub const RREAD: u8 = 117;
31pub const TWRITE: u8 = 118;
32pub const RWRITE: u8 = 119;
33pub const TCLUNK: u8 = 120;
34pub const RCLUNK: u8 = 121;
35pub const TGETATTR: u8 = 24;
36pub const RGETATTR: u8 = 25;
37pub const TREADDIR: u8 = 40;
38pub const RREADDIR: u8 = 41;
39
40#[derive(Clone, Debug, Default)]
45pub struct Qid {
46 pub qtype: u8,
47 pub version: u32,
48 pub path: u64,
49}
50
51impl Qid {
52 pub fn read_from(r: &mut Cursor<&[u8]>) -> anyhow::Result<Self> {
53 Ok(Qid {
54 qtype: read_u8(r)?,
55 version: read_u32(r)?,
56 path: read_u64(r)?,
57 })
58 }
59
60 pub fn write_to(&self, w: &mut Vec<u8>) {
61 w.push(self.qtype);
62 w.extend_from_slice(&self.version.to_le_bytes());
63 w.extend_from_slice(&self.path.to_le_bytes());
64 }
65
66 pub fn is_dir(&self) -> bool {
67 self.qtype & 0x80 != 0
68 }
69}
70
71fn read_u8(r: &mut Cursor<&[u8]>) -> anyhow::Result<u8> {
76 let mut buf = [0u8; 1];
77 r.read_exact(&mut buf)?;
78 Ok(buf[0])
79}
80
81fn read_u16(r: &mut Cursor<&[u8]>) -> anyhow::Result<u16> {
82 let mut buf = [0u8; 2];
83 r.read_exact(&mut buf)?;
84 Ok(u16::from_le_bytes(buf))
85}
86
87fn read_u32(r: &mut Cursor<&[u8]>) -> anyhow::Result<u32> {
88 let mut buf = [0u8; 4];
89 r.read_exact(&mut buf)?;
90 Ok(u32::from_le_bytes(buf))
91}
92
93fn read_u64(r: &mut Cursor<&[u8]>) -> anyhow::Result<u64> {
94 let mut buf = [0u8; 8];
95 r.read_exact(&mut buf)?;
96 Ok(u64::from_le_bytes(buf))
97}
98
99fn read_string(r: &mut Cursor<&[u8]>) -> anyhow::Result<String> {
100 let len = read_u16(r)? as usize;
101 let mut buf = vec![0u8; len];
102 r.read_exact(&mut buf)?;
103 Ok(String::from_utf8_lossy(&buf).to_string())
104}
105
106fn read_data(r: &mut Cursor<&[u8]>) -> anyhow::Result<Vec<u8>> {
107 let len = read_u32(r)? as usize;
108 let mut buf = vec![0u8; len];
109 r.read_exact(&mut buf)?;
110 Ok(buf)
111}
112
113fn write_string(w: &mut Vec<u8>, s: &str) {
114 w.extend_from_slice(&(s.len() as u16).to_le_bytes());
115 w.extend_from_slice(s.as_bytes());
116}
117
118#[allow(dead_code)]
119fn write_data(w: &mut Vec<u8>, data: &[u8]) {
120 w.extend_from_slice(&(data.len() as u32).to_le_bytes());
121 w.extend_from_slice(data);
122}
123
124pub fn encode_tmessage(tag: u16, msg_type: u8, body: &[u8]) -> Vec<u8> {
130 let size = 4 + 1 + 2 + body.len();
131 let mut buf = Vec::with_capacity(size);
132 buf.extend_from_slice(&(size as u32).to_le_bytes());
133 buf.push(msg_type);
134 buf.extend_from_slice(&tag.to_le_bytes());
135 buf.extend_from_slice(body);
136 buf
137}
138
139pub fn tversion(tag: u16, msize: u32, version: &str) -> Vec<u8> {
140 let mut body = Vec::new();
141 body.extend_from_slice(&msize.to_le_bytes());
142 write_string(&mut body, version);
143 encode_tmessage(tag, TVERSION, &body)
144}
145
146pub fn tattach(tag: u16, fid: u32, afid: u32, uname: &str, aname: &str) -> Vec<u8> {
147 let mut body = Vec::new();
148 body.extend_from_slice(&fid.to_le_bytes());
149 body.extend_from_slice(&afid.to_le_bytes());
150 write_string(&mut body, uname);
151 write_string(&mut body, aname);
152 body.extend_from_slice(&0u32.to_le_bytes());
154 encode_tmessage(tag, TATTACH, &body)
155}
156
157pub fn twalk(tag: u16, fid: u32, newfid: u32, wnames: &[&str]) -> Vec<u8> {
158 let mut body = Vec::new();
159 body.extend_from_slice(&fid.to_le_bytes());
160 body.extend_from_slice(&newfid.to_le_bytes());
161 body.extend_from_slice(&(wnames.len() as u16).to_le_bytes());
162 for name in wnames {
163 write_string(&mut body, name);
164 }
165 encode_tmessage(tag, TWALK, &body)
166}
167
168pub fn tlopen(tag: u16, fid: u32, flags: u32) -> Vec<u8> {
169 let mut body = Vec::new();
170 body.extend_from_slice(&fid.to_le_bytes());
171 body.extend_from_slice(&flags.to_le_bytes());
172 encode_tmessage(tag, TLOPEN, &body)
173}
174
175pub fn tread(tag: u16, fid: u32, offset: u64, count: u32) -> Vec<u8> {
176 let mut body = Vec::new();
177 body.extend_from_slice(&fid.to_le_bytes());
178 body.extend_from_slice(&offset.to_le_bytes());
179 body.extend_from_slice(&count.to_le_bytes());
180 encode_tmessage(tag, TREAD, &body)
181}
182
183pub fn twrite(tag: u16, fid: u32, offset: u64, data: &[u8]) -> Vec<u8> {
184 let mut body = Vec::new();
185 body.extend_from_slice(&fid.to_le_bytes());
186 body.extend_from_slice(&offset.to_le_bytes());
187 body.extend_from_slice(&(data.len() as u32).to_le_bytes());
188 body.extend_from_slice(data);
189 encode_tmessage(tag, TWRITE, &body)
190}
191
192pub fn tclunk(tag: u16, fid: u32) -> Vec<u8> {
193 encode_tmessage(tag, TCLUNK, &fid.to_le_bytes())
194}
195
196pub fn tgetattr(tag: u16, fid: u32, request_mask: u64) -> Vec<u8> {
197 let mut body = Vec::new();
198 body.extend_from_slice(&fid.to_le_bytes());
199 body.extend_from_slice(&request_mask.to_le_bytes());
200 encode_tmessage(tag, TGETATTR, &body)
201}
202
203pub fn treaddir(tag: u16, fid: u32, offset: u64, count: u32) -> Vec<u8> {
204 let mut body = Vec::new();
205 body.extend_from_slice(&fid.to_le_bytes());
206 body.extend_from_slice(&offset.to_le_bytes());
207 body.extend_from_slice(&count.to_le_bytes());
208 encode_tmessage(tag, TREADDIR, &body)
209}
210
211#[derive(Debug)]
221pub enum Request {
222 Version {
223 msize: u32,
224 version: String,
225 },
226 Attach {
227 fid: u32,
228 afid: u32,
229 uname: String,
230 aname: String,
231 },
232 Flush {
233 oldtag: u16,
234 },
235 Walk {
236 fid: u32,
237 newfid: u32,
238 wnames: Vec<String>,
239 },
240 Lopen {
241 fid: u32,
242 flags: u32,
243 },
244 Read {
245 fid: u32,
246 offset: u64,
247 count: u32,
248 },
249 Write {
250 fid: u32,
251 offset: u64,
252 data: Vec<u8>,
253 },
254 Clunk {
255 fid: u32,
256 },
257 Getattr {
258 fid: u32,
259 request_mask: u64,
260 },
261 Readdir {
262 fid: u32,
263 offset: u64,
264 count: u32,
265 },
266}
267
268pub fn parse_request(buf: &[u8]) -> anyhow::Result<(u16, Request)> {
273 if buf.len() < 7 {
274 anyhow::bail!("9P request too short: {} bytes", buf.len());
275 }
276
277 let mut r = Cursor::new(buf);
278 let _size = read_u32(&mut r)?;
279 let msg_type = read_u8(&mut r)?;
280 let tag = read_u16(&mut r)?;
281
282 let request = match msg_type {
283 TVERSION => {
284 let msize = read_u32(&mut r)?;
285 let version = read_string(&mut r)?;
286 Request::Version { msize, version }
287 }
288 TATTACH => {
289 let fid = read_u32(&mut r)?;
290 let afid = read_u32(&mut r)?;
291 let uname = read_string(&mut r)?;
292 let aname = read_string(&mut r)?;
293 let _n_uname = read_u32(&mut r)?;
295 Request::Attach {
296 fid,
297 afid,
298 uname,
299 aname,
300 }
301 }
302 TFLUSH => {
303 let oldtag = read_u16(&mut r)?;
304 Request::Flush { oldtag }
305 }
306 TWALK => {
307 let fid = read_u32(&mut r)?;
308 let newfid = read_u32(&mut r)?;
309 let nwname = read_u16(&mut r)?;
310 let mut wnames = Vec::with_capacity(nwname as usize);
311 for _ in 0..nwname {
312 wnames.push(read_string(&mut r)?);
313 }
314 Request::Walk {
315 fid,
316 newfid,
317 wnames,
318 }
319 }
320 TLOPEN => {
321 let fid = read_u32(&mut r)?;
322 let flags = read_u32(&mut r)?;
323 Request::Lopen { fid, flags }
324 }
325 TREAD => {
326 let fid = read_u32(&mut r)?;
327 let offset = read_u64(&mut r)?;
328 let count = read_u32(&mut r)?;
329 Request::Read { fid, offset, count }
330 }
331 TWRITE => {
332 let fid = read_u32(&mut r)?;
333 let offset = read_u64(&mut r)?;
334 let data = read_data(&mut r)?;
335 Request::Write { fid, offset, data }
336 }
337 TCLUNK => {
338 let fid = read_u32(&mut r)?;
339 Request::Clunk { fid }
340 }
341 TGETATTR => {
342 let fid = read_u32(&mut r)?;
343 let request_mask = read_u64(&mut r)?;
344 Request::Getattr { fid, request_mask }
345 }
346 TREADDIR => {
347 let fid = read_u32(&mut r)?;
348 let offset = read_u64(&mut r)?;
349 let count = read_u32(&mut r)?;
350 Request::Readdir { fid, offset, count }
351 }
352 _ => anyhow::bail!("unknown 9P request type: {msg_type}"),
353 };
354
355 Ok((tag, request))
356}
357
358pub const TFLUSH: u8 = 108;
360pub const RFLUSH: u8 = 109;
361
362pub fn encode_response(tag: u16, response: &Response) -> Vec<u8> {
372 match response {
373 Response::Error { ecode } => rlerror(tag, *ecode),
374 Response::Version { msize, version } => rversion(tag, *msize, version),
375 Response::Attach { qid } => rattach(tag, qid),
376 Response::Walk { qids } => rwalk(tag, qids),
377 Response::Lopen { qid, iounit } => rlopen(tag, qid, *iounit),
378 Response::Read { data } => rread(tag, data),
379 Response::Write { count } => rwrite(tag, *count),
380 Response::Clunk => rclunk(tag),
381 Response::Getattr {
382 qid,
383 mode,
384 size,
385 mtime_sec,
386 } => rgetattr(tag, qid, *mode, *size, *mtime_sec),
387 Response::Readdir { data } => rreaddir(tag, data),
393 }
394}
395
396fn encode_rmessage(tag: u16, msg_type: u8, body: &[u8]) -> Vec<u8> {
397 encode_tmessage(tag, msg_type, body)
399}
400
401pub fn rlerror(tag: u16, ecode: u32) -> Vec<u8> {
402 encode_rmessage(tag, RLERROR, &ecode.to_le_bytes())
403}
404
405pub fn rversion(tag: u16, msize: u32, version: &str) -> Vec<u8> {
406 let mut body = Vec::new();
407 body.extend_from_slice(&msize.to_le_bytes());
408 write_string(&mut body, version);
409 encode_rmessage(tag, RVERSION, &body)
410}
411
412pub fn rattach(tag: u16, qid: &Qid) -> Vec<u8> {
413 let mut body = Vec::new();
414 qid.write_to(&mut body);
415 encode_rmessage(tag, RATTACH, &body)
416}
417
418pub fn rwalk(tag: u16, qids: &[Qid]) -> Vec<u8> {
419 let mut body = Vec::new();
420 body.extend_from_slice(&(qids.len() as u16).to_le_bytes());
421 for q in qids {
422 q.write_to(&mut body);
423 }
424 encode_rmessage(tag, RWALK, &body)
425}
426
427pub fn rlopen(tag: u16, qid: &Qid, iounit: u32) -> Vec<u8> {
428 let mut body = Vec::new();
429 qid.write_to(&mut body);
430 body.extend_from_slice(&iounit.to_le_bytes());
431 encode_rmessage(tag, RLOPEN, &body)
432}
433
434pub fn rread(tag: u16, data: &[u8]) -> Vec<u8> {
435 let mut body = Vec::new();
436 body.extend_from_slice(&(data.len() as u32).to_le_bytes());
437 body.extend_from_slice(data);
438 encode_rmessage(tag, RREAD, &body)
439}
440
441pub fn rreaddir(tag: u16, data: &[u8]) -> Vec<u8> {
445 let mut body = Vec::new();
446 body.extend_from_slice(&(data.len() as u32).to_le_bytes());
447 body.extend_from_slice(data);
448 encode_rmessage(tag, RREADDIR, &body)
449}
450
451pub fn rwrite(tag: u16, count: u32) -> Vec<u8> {
452 encode_rmessage(tag, RWRITE, &count.to_le_bytes())
453}
454
455pub fn rclunk(tag: u16) -> Vec<u8> {
456 encode_rmessage(tag, RCLUNK, &[])
457}
458
459pub fn rflush(tag: u16) -> Vec<u8> {
460 encode_rmessage(tag, RFLUSH, &[])
461}
462
463#[allow(clippy::too_many_lines)]
466pub fn rgetattr(tag: u16, qid: &Qid, mode: u32, size: u64, mtime_sec: u64) -> Vec<u8> {
467 let valid: u64 = 0x7ff;
471 let mut body = Vec::with_capacity(153);
472 body.extend_from_slice(&valid.to_le_bytes());
473 qid.write_to(&mut body);
474 body.extend_from_slice(&mode.to_le_bytes());
475 body.extend_from_slice(&0u32.to_le_bytes()); body.extend_from_slice(&0u32.to_le_bytes()); body.extend_from_slice(&1u64.to_le_bytes()); body.extend_from_slice(&0u64.to_le_bytes()); body.extend_from_slice(&size.to_le_bytes());
480 body.extend_from_slice(&4096u64.to_le_bytes()); body.extend_from_slice(&0u64.to_le_bytes()); body.extend_from_slice(&mtime_sec.to_le_bytes()); body.extend_from_slice(&0u64.to_le_bytes()); body.extend_from_slice(&mtime_sec.to_le_bytes()); body.extend_from_slice(&0u64.to_le_bytes()); body.extend_from_slice(&mtime_sec.to_le_bytes()); body.extend_from_slice(&0u64.to_le_bytes()); body.extend_from_slice(&0u64.to_le_bytes()); body.extend_from_slice(&0u64.to_le_bytes()); body.extend_from_slice(&0u64.to_le_bytes()); body.extend_from_slice(&0u64.to_le_bytes()); encode_rmessage(tag, RGETATTR, &body)
493}
494
495#[derive(Debug)]
501pub enum Response {
502 Version {
503 msize: u32,
504 version: String,
505 },
506 Attach {
507 qid: Qid,
508 },
509 Walk {
510 qids: Vec<Qid>,
511 },
512 Lopen {
513 qid: Qid,
514 iounit: u32,
515 },
516 Read {
517 data: Vec<u8>,
518 },
519 Write {
520 count: u32,
521 },
522 Clunk,
523 Getattr {
524 qid: Qid,
525 mode: u32,
526 size: u64,
527 mtime_sec: u64,
528 },
529 Readdir {
530 data: Vec<u8>,
531 },
532 Error {
533 ecode: u32,
534 },
535}
536
537pub fn parse_response(buf: &[u8]) -> anyhow::Result<(u16, Response)> {
539 if buf.len() < 7 {
540 anyhow::bail!("9P response too short: {} bytes", buf.len());
541 }
542
543 let mut r = Cursor::new(buf);
544 let _size = read_u32(&mut r)?;
545 let msg_type = read_u8(&mut r)?;
546 let tag = read_u16(&mut r)?;
547
548 let response = match msg_type {
549 RLERROR => {
550 let ecode = read_u32(&mut r)?;
551 Response::Error { ecode }
552 }
553 RVERSION => {
554 let msize = read_u32(&mut r)?;
555 let version = read_string(&mut r)?;
556 Response::Version { msize, version }
557 }
558 RATTACH => {
559 let qid = Qid::read_from(&mut r)?;
560 Response::Attach { qid }
561 }
562 RWALK => {
563 let nwqid = read_u16(&mut r)?;
564 let mut qids = Vec::with_capacity(nwqid as usize);
565 for _ in 0..nwqid {
566 qids.push(Qid::read_from(&mut r)?);
567 }
568 Response::Walk { qids }
569 }
570 RLOPEN => {
571 let qid = Qid::read_from(&mut r)?;
572 let iounit = read_u32(&mut r)?;
573 Response::Lopen { qid, iounit }
574 }
575 RREAD => {
576 let data = read_data(&mut r)?;
577 Response::Read { data }
578 }
579 RWRITE => {
580 let count = read_u32(&mut r)?;
581 Response::Write { count }
582 }
583 RCLUNK => Response::Clunk,
584 RGETATTR => {
585 let _valid = read_u64(&mut r)?;
586 let qid = Qid::read_from(&mut r)?;
587 let mode = read_u32(&mut r)?;
588 let _uid = read_u32(&mut r)?;
589 let _gid = read_u32(&mut r)?;
590 let _nlink = read_u64(&mut r)?;
591 let _rdev = read_u64(&mut r)?;
592 let size = read_u64(&mut r)?;
593 let _blksize = read_u64(&mut r)?;
594 let _blocks = read_u64(&mut r)?;
595 let _atime_sec = read_u64(&mut r)?;
596 let _atime_nsec = read_u64(&mut r)?;
597 let mtime_sec = read_u64(&mut r)?;
598 let _mtime_nsec = read_u64(&mut r)?;
599 let _ctime_sec = read_u64(&mut r)?;
600 let _ctime_nsec = read_u64(&mut r)?;
601 let _btime_sec = read_u64(&mut r)?;
602 let _btime_nsec = read_u64(&mut r)?;
603 let _gen = read_u64(&mut r)?;
604 let _data_version = read_u64(&mut r)?;
605 Response::Getattr {
606 qid,
607 mode,
608 size,
609 mtime_sec,
610 }
611 }
612 RREADDIR => {
613 let data = read_data(&mut r)?;
614 Response::Readdir { data }
615 }
616 _ => anyhow::bail!("unknown 9P response type: {msg_type}"),
617 };
618
619 Ok((tag, response))
620}
621
622#[derive(Debug)]
628pub struct DirEntryP9 {
629 pub qid: Qid,
630 pub offset: u64,
631 pub dtype: u8,
632 pub name: String,
633}
634
635#[derive(Debug, Clone)]
642pub struct ReaddirEntry {
643 pub qid: Qid,
644 pub name: String,
645}
646
647pub fn encode_readdir_page(entries: &[ReaddirEntry], offset: u64, count: u32) -> Vec<u8> {
666 let budget = count as usize;
667 let mut out = Vec::new();
668 for (i, e) in entries.iter().enumerate() {
669 let cookie = i as u64 + 1;
670 if cookie <= offset {
671 continue;
672 }
673 let mut rec = Vec::new();
674 e.qid.write_to(&mut rec); rec.extend_from_slice(&cookie.to_le_bytes()); rec.push(e.qid.qtype); write_string(&mut rec, &e.name); if !out.is_empty() && out.len() + rec.len() > budget {
679 break;
680 }
681 out.extend_from_slice(&rec);
682 }
683 out
684}
685
686pub fn parse_readdir_entries(data: &[u8]) -> anyhow::Result<Vec<DirEntryP9>> {
688 let mut entries = Vec::new();
689 let mut r = Cursor::new(data);
690
691 while (r.position() as usize) < data.len() {
692 let qid = Qid::read_from(&mut r)?;
693 let offset = read_u64(&mut r)?;
694 let dtype = read_u8(&mut r)?;
695 let name = read_string(&mut r)?;
696 entries.push(DirEntryP9 {
697 qid,
698 offset,
699 dtype,
700 name,
701 });
702 }
703
704 Ok(entries)
705}