1
#![doc(
2
    html_logo_url = "https://raw.githubusercontent.com/sevki/jetstream/main/logo/JetStream.png"
3
)]
4
#![doc(
5
    html_favicon_url = "https://raw.githubusercontent.com/sevki/jetstream/main/logo/JetStream.png"
6
)]
7
#![cfg_attr(docsrs, feature(doc_cfg))]
8
// Copyright (c) 2024, Sevki <s@sevki.io>
9
// Copyright 2018 The ChromiumOS Authors
10
// Use of this source code is governed by a BSD-style license that can be
11
// found in the LICENSE file.
12

            
13
pub mod messages;
14
pub mod ninep_2000_l;
15
pub mod server;
16
use jetstream_libc as libc;
17

            
18
use std::io;
19

            
20
pub use self::messages::*;
21

            
22
pub fn error_to_rmessage(err: &io::Error) -> Rlerror {
23
    let errno = if let Some(errno) = err.raw_os_error() {
24
        errno
25
    } else {
26
        // Make a best-effort guess based on the kind.
27
        match err.kind() {
28
            io::ErrorKind::NotFound => libc::ENOENT,
29
            io::ErrorKind::PermissionDenied => libc::EPERM,
30
            io::ErrorKind::ConnectionRefused => libc::ECONNREFUSED,
31
            io::ErrorKind::ConnectionReset => libc::ECONNRESET,
32
            io::ErrorKind::ConnectionAborted => libc::ECONNABORTED,
33
            io::ErrorKind::NotConnected => libc::ENOTCONN,
34
            io::ErrorKind::AddrInUse => libc::EADDRINUSE,
35
            io::ErrorKind::AddrNotAvailable => libc::EADDRNOTAVAIL,
36
            io::ErrorKind::BrokenPipe => libc::EPIPE,
37
            io::ErrorKind::AlreadyExists => libc::EEXIST,
38
            io::ErrorKind::WouldBlock => libc::EWOULDBLOCK,
39
            io::ErrorKind::InvalidInput => libc::EINVAL,
40
            io::ErrorKind::InvalidData => libc::EINVAL,
41
            io::ErrorKind::TimedOut => libc::ETIMEDOUT,
42
            io::ErrorKind::WriteZero => libc::EIO,
43
            io::ErrorKind::Interrupted => libc::EINTR,
44
            io::ErrorKind::Other => libc::EIO,
45
            io::ErrorKind::UnexpectedEof => libc::EIO,
46
            _ => libc::EIO,
47
        }
48
    };
49

            
50
    Rlerror {
51
        ecode: errno as u32,
52
    }
53
}
54

            
55
// Tlopen and Tlcreate flags.  Taken from "include/net/9p/9p.h" in the linux tree.
56
pub const P9_RDONLY: u32 = 0o00000000;
57
pub const P9_WRONLY: u32 = 0o00000001;
58
pub const P9_RDWR: u32 = 0o00000002;
59
pub const P9_NOACCESS: u32 = 0o00000003;
60
pub const P9_CREATE: u32 = 0o00000100;
61
pub const P9_EXCL: u32 = 0o00000200;
62
pub const P9_NOCTTY: u32 = 0o00000400;
63
pub const P9_TRUNC: u32 = 0o00001000;
64
pub const P9_APPEND: u32 = 0o00002000;
65
pub const P9_NONBLOCK: u32 = 0o00004000;
66
pub const P9_DSYNC: u32 = 0o00010000;
67
pub const P9_FASYNC: u32 = 0o00020000;
68
pub const P9_DIRECT: u32 = 0o00040000;
69
pub const P9_LARGEFILE: u32 = 0o00100000;
70
pub const P9_DIRECTORY: u32 = 0o00200000;
71
pub const P9_NOFOLLOW: u32 = 0o00400000;
72
pub const P9_NOATIME: u32 = 0o01000000;
73
pub const _P9_CLOEXEC: u32 = 0o02000000;
74
pub const P9_SYNC: u32 = 0o04000000;
75

            
76
// Mapping from 9P flags to libc flags.
77
pub const MAPPED_FLAGS: [(u32, i32); 16] = [
78
    (P9_WRONLY, libc::O_WRONLY),
79
    (P9_RDWR, libc::O_RDWR),
80
    (P9_CREATE, libc::O_CREAT),
81
    (P9_EXCL, libc::O_EXCL),
82
    (P9_NOCTTY, libc::O_NOCTTY),
83
    (P9_TRUNC, libc::O_TRUNC),
84
    (P9_APPEND, libc::O_APPEND),
85
    (P9_NONBLOCK, libc::O_NONBLOCK),
86
    (P9_DSYNC, libc::O_DSYNC),
87
    (P9_FASYNC, 0), // Unsupported
88
    (P9_DIRECT, libc::O_DIRECT),
89
    (P9_LARGEFILE, libc::O_LARGEFILE),
90
    (P9_DIRECTORY, libc::O_DIRECTORY),
91
    (P9_NOFOLLOW, libc::O_NOFOLLOW),
92
    (P9_NOATIME, libc::O_NOATIME),
93
    (P9_SYNC, libc::O_SYNC),
94
];
95

            
96
// 9P Qid types.  Taken from "include/net/9p/9p.h" in the linux tree.
97
pub const P9_QTDIR: u8 = 0x80;
98
pub const _P9_QTAPPEND: u8 = 0x40;
99
pub const _P9_QTEXCL: u8 = 0x20;
100
pub const _P9_QTMOUNT: u8 = 0x10;
101
pub const _P9_QTAUTH: u8 = 0x08;
102
pub const _P9_QTTMP: u8 = 0x04;
103
pub const P9_QTSYMLINK: u8 = 0x02;
104
pub const _P9_QTLINK: u8 = 0x01;
105
pub const P9_QTFILE: u8 = 0x00;
106

            
107
// Bitmask values for the getattr request.
108
pub const _P9_GETATTR_MODE: u64 = 0x00000001;
109
pub const _P9_GETATTR_NLINK: u64 = 0x00000002;
110
pub const _P9_GETATTR_UID: u64 = 0x00000004;
111
pub const _P9_GETATTR_GID: u64 = 0x00000008;
112
pub const _P9_GETATTR_RDEV: u64 = 0x00000010;
113
pub const _P9_GETATTR_ATIME: u64 = 0x00000020;
114
pub const _P9_GETATTR_MTIME: u64 = 0x00000040;
115
pub const _P9_GETATTR_CTIME: u64 = 0x00000080;
116
pub const _P9_GETATTR_INO: u64 = 0x00000100;
117
pub const _P9_GETATTR_SIZE: u64 = 0x00000200;
118
pub const _P9_GETATTR_BLOCKS: u64 = 0x00000400;
119

            
120
pub const _P9_GETATTR_BTIME: u64 = 0x00000800;
121
pub const _P9_GETATTR_GEN: u64 = 0x00001000;
122
pub const _P9_GETATTR_DATA_VERSION: u64 = 0x00002000;
123

            
124
pub const P9_GETATTR_BASIC: u64 = 0x000007ff; // Mask for fields up to BLOCKS
125
pub const _P9_GETATTR_ALL: u64 = 0x00003fff; // Mask for All fields above
126

            
127
// Bitmask values for the setattr request.
128
pub const P9_SETATTR_MODE: u32 = 0x00000001;
129
pub const P9_SETATTR_UID: u32 = 0x00000002;
130
pub const P9_SETATTR_GID: u32 = 0x00000004;
131
pub const P9_SETATTR_SIZE: u32 = 0x00000008;
132
pub const P9_SETATTR_ATIME: u32 = 0x00000010;
133
pub const P9_SETATTR_MTIME: u32 = 0x00000020;
134
pub const P9_SETATTR_CTIME: u32 = 0x00000040;
135
pub const P9_SETATTR_ATIME_SET: u32 = 0x00000080;
136
pub const P9_SETATTR_MTIME_SET: u32 = 0x00000100;
137

            
138
// 9p lock constants. Taken from "include/net/9p/9p.h" in the linux kernel.
139
pub const _P9_LOCK_TYPE_RDLCK: u8 = 0;
140
pub const _P9_LOCK_TYPE_WRLCK: u8 = 1;
141
pub const P9_LOCK_TYPE_UNLCK: u8 = 2;
142
pub const _P9_LOCK_FLAGS_BLOCK: u8 = 1;
143
pub const _P9_LOCK_FLAGS_RECLAIM: u8 = 2;
144
pub const P9_LOCK_SUCCESS: u8 = 0;
145
pub const _P9_LOCK_BLOCKED: u8 = 1;
146
pub const _P9_LOCK_ERROR: u8 = 2;
147
pub const _P9_LOCK_GRACE: u8 = 3;
148

            
149
// Minimum and maximum message size that we'll expect from the client.
150
pub const MIN_MESSAGE_SIZE: u32 = 256;
151
pub const MAX_MESSAGE_SIZE: u32 = 64 * 1024 + 24; // 64 KiB of payload plus some extra for the header
152

            
153
pub const DEFAULT_MSIZE: u32 = 8192;