1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
use crate::glob::GlobSet;
use arcstr::ArcStr;
use bytes::{Buf, BufMut, Bytes};
use smallvec::SmallVec;
use netidx_core::{
    chars::Chars,
    pack::{
        len_wrapped_decode, len_wrapped_encode, len_wrapped_len, Pack, PackError, Z64,
    },
    path::Path,
    pool::Pooled,
};
use netidx_derive::Pack;
use std::{
    cmp::{Eq, PartialEq},
    hash::{Hash, Hasher},
    net::SocketAddr,
    result,
};

type Error = PackError;
pub type Result<T> = result::Result<T, Error>;

#[derive(Clone, Debug, Copy, PartialEq, Eq, Pack)]
pub enum HashMethod {
    Sha3_512,
}

#[derive(Clone, Debug, Copy, PartialEq, Pack)]
pub struct AuthChallenge {
    pub hash_method: HashMethod,
    pub challenge: u128,
}

#[derive(Clone, Debug, PartialEq, Eq, Pack)]
pub enum AuthRead {
    Anonymous,
    Krb5,
    Local,
    Tls,
}

#[derive(Clone, Debug, PartialEq, Eq, Pack)]
pub enum AuthWrite {
    Anonymous,
    Reuse,
    Krb5 { spn: Chars },
    Local,
    Tls { name: Chars },
}

#[derive(Clone, Debug, PartialEq, Eq, Pack)]
pub struct ClientHelloWrite {
    pub write_addr: SocketAddr,
    pub auth: AuthWrite,
}

#[derive(Clone, Debug, PartialEq, Eq, Pack)]
pub enum ClientHello {
    /// Instruct the resolver server that this connection will not
    /// publish paths.
    ReadOnly(AuthRead),
    /// Instruct the resolver server that this connection will
    /// only publish paths. All published paths will use the
    /// specified address `write_addr`, and the publisher must
    /// send a heartbeat at least every `ttl` seconds or the
    /// resolver server will purge all paths published by
    /// `write_addr`.
    WriteOnly(ClientHelloWrite),
}

#[derive(Clone, Debug, PartialEq, Eq, Pack)]
pub struct ServerHelloWrite {
    pub ttl: u64,
    pub ttl_expired: bool,
    pub auth: AuthWrite,
    pub resolver_id: SocketAddr,
}

#[derive(Clone, Debug, PartialEq, Eq, Pack)]
pub struct Secret(pub u128);

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ReadyForOwnershipCheck;

impl Pack for ReadyForOwnershipCheck {
    fn encoded_len(&self) -> usize {
        len_wrapped_len(1)
    }

    fn encode(&self, buf: &mut impl BufMut) -> Result<()> {
        len_wrapped_encode(buf, self, |buf| Ok(buf.put_u8(0)))
    }

    fn decode(buf: &mut impl Buf) -> Result<Self> {
        len_wrapped_decode(buf, |buf| match <u8 as Pack>::decode(buf)? {
            0 => Ok(ReadyForOwnershipCheck),
            _ => Err(PackError::UnknownTag),
        })
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Pack)]
pub enum ToRead {
    /// Resolve path to addresses/ports
    Resolve(Path),
    /// List the paths published under the specified root path
    List(Path),
    /// Describe the table rooted at the specified path
    Table(Path),
    /// List paths matching the specified glob set.
    ListMatching(GlobSet),
    /// Get the change nr for the specified path
    GetChangeNr(Path),
}

#[derive(Clone, Debug, PartialEq, Eq, Pack)]
pub enum Auth {
    Anonymous,
    Local { path: Chars },
    Krb5 { spn: Chars },
    Tls { name: Chars },
}

atomic_id!(PublisherId);

#[derive(Clone, Debug, PartialEq, Eq, Pack)]
pub enum TargetAuth {
    Anonymous,
    Local,
    Krb5 { spn: Chars },
    Tls { name: Chars },
}

impl TargetAuth {
    pub fn is_anonymous(&self) -> bool {
        match self {
            Self::Anonymous => true,
            Self::Krb5 { .. } | Self::Local | Self::Tls { .. } => false,
        }
    }
}

impl TryFrom<AuthWrite> for TargetAuth {
    type Error = anyhow::Error;

    fn try_from(v: AuthWrite) -> result::Result<Self, Self::Error> {
        match v {
            AuthWrite::Anonymous => Ok(Self::Anonymous),
            AuthWrite::Local => Ok(Self::Local),
            AuthWrite::Krb5 { spn } => Ok(Self::Krb5 { spn }),
            AuthWrite::Reuse => bail!("no session to reuse"),
            AuthWrite::Tls { name } => Ok(Self::Tls { name }),
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Pack)]
pub struct UserInfo {
    pub name: ArcStr,
    pub primary_group: ArcStr,
    pub groups: SmallVec<[ArcStr; 16]>,
    pub resolver: SocketAddr,
    pub token: Bytes,
}

#[derive(Clone, Debug, PartialEq, Eq, Pack)]
pub struct Publisher {
    pub resolver: SocketAddr,
    pub id: PublisherId,
    pub addr: SocketAddr,
    pub hash_method: HashMethod,
    pub target_auth: TargetAuth,
    #[pack(default)]
    pub user_info: Option<UserInfo>,
}

#[derive(Clone, Debug, PartialEq, Eq, Pack)]
pub struct PublisherRef {
    pub id: PublisherId,
    pub token: Bytes,
}

#[derive(Clone, Debug, PartialEq, Eq, Pack)]
pub struct Resolved {
    pub resolver: SocketAddr,
    pub publishers: Pooled<Vec<PublisherRef>>,
    pub timestamp: u64,
    pub flags: u32,
    pub permissions: u32,
}

#[derive(Clone, Debug, Pack)]
pub struct Referral {
    pub path: Path,
    pub ttl: Option<u16>,
    pub addrs: Pooled<Vec<(SocketAddr, Auth)>>,
}

impl Hash for Referral {
    fn hash<H: Hasher>(&self, state: &mut H) {
        for (addr, _) in &*self.addrs {
            Hash::hash(&addr, state)
        }
    }
}

impl PartialEq for Referral {
    fn eq(&self, other: &Referral) -> bool {
        self.addrs.iter().zip(other.addrs.iter()).all(|(l, r)| l == r)
    }
}

impl Eq for Referral {}

#[derive(Clone, Debug, PartialEq, Eq, Pack)]
pub struct Table {
    pub rows: Pooled<Vec<Path>>,
    pub cols: Pooled<Vec<(Path, Z64)>>,
}

#[derive(Clone, Debug, PartialEq, Eq, Pack)]
pub struct ListMatching {
    pub matched: Pooled<Vec<Pooled<Vec<Path>>>>,
    pub referrals: Pooled<Vec<Referral>>,
}

#[derive(Clone, Debug, PartialEq, Eq, Pack)]
pub struct GetChangeNr {
    pub change_number: Z64,
    pub resolver: SocketAddr,
    pub referrals: Pooled<Vec<Referral>>,
}

#[derive(Clone, Debug, PartialEq, Eq, Pack)]
pub enum FromRead {
    Publisher(Publisher),
    Resolved(Resolved),
    List(Pooled<Vec<Path>>),
    Table(Table),
    Referral(Referral),
    Denied,
    Error(Chars),
    ListMatching(ListMatching),
    GetChangeNr(GetChangeNr),
}

#[derive(Clone, Debug, PartialEq, Eq, Hash, Pack)]
pub enum ToWrite {
    /// Publish the path
    Publish(Path),
    /// Add a default publisher to path
    PublishDefault(Path),
    /// Stop publishing the path
    Unpublish(Path),
    /// Clear all values you've published
    Clear,
    /// Tell the resolver that we are still alive
    Heartbeat,
    /// Publish the path and set associated flags
    PublishWithFlags(Path, u32),
    /// Add a default publisher to path and set associated flags
    PublishDefaultWithFlags(Path, u32),
    /// Unpublish a default publisher
    UnpublishDefault(Path),
}

#[derive(Clone, Debug, PartialEq, Eq, Hash, Pack)]
pub enum FromWrite {
    Published,
    Unpublished,
    Referral(Referral),
    Denied,
    Error(Chars),
}