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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
//! Protocol Data Unit module
//!
//! This module comprises multiple data structures representing possible
//! protocol data units (PDUs) according to
//! the standard message exchange mechanisms,
//! as well as readers and writers of PDUs from arbitrary data sources.
pub mod reader;
pub mod writer;

use std::fmt::Display;

pub use reader::read_pdu;
pub use writer::write_pdu;

/// Message component for a proposed presentation context.
#[derive(Clone, Eq, PartialEq, PartialOrd, Hash, Debug)]
pub struct PresentationContextProposed {
    /// the presentation context identifier
    pub id: u8,
    /// the expected abstract syntax UID
    /// (commonly referrering to the expected SOP class)
    pub abstract_syntax: String,
    /// a list of transfer syntax UIDs to support in this interaction
    pub transfer_syntaxes: Vec<String>,
}

#[derive(Clone, Eq, PartialEq, PartialOrd, Hash, Debug)]
pub struct PresentationContextResult {
    pub id: u8,
    pub reason: PresentationContextResultReason,
    pub transfer_syntax: String,
}

#[derive(Clone, Eq, PartialEq, PartialOrd, Hash, Debug)]
pub enum PresentationContextResultReason {
    Acceptance = 0,
    UserRejection = 1,
    NoReason = 2,
    AbstractSyntaxNotSupported = 3,
    TransferSyntaxesNotSupported = 4,
}

impl PresentationContextResultReason {
    fn from(reason: u8) -> Option<PresentationContextResultReason> {
        let result = match reason {
            0 => PresentationContextResultReason::Acceptance,
            1 => PresentationContextResultReason::UserRejection,
            2 => PresentationContextResultReason::NoReason,
            3 => PresentationContextResultReason::AbstractSyntaxNotSupported,
            4 => PresentationContextResultReason::TransferSyntaxesNotSupported,
            _ => {
                return None;
            }
        };

        Some(result)
    }
}

impl Display for PresentationContextResultReason {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let msg = match self {
            PresentationContextResultReason::Acceptance => "acceptance",
            PresentationContextResultReason::UserRejection => "user rejection",
            PresentationContextResultReason::NoReason => "no reason",
            PresentationContextResultReason::AbstractSyntaxNotSupported => {
                "abstract syntax not supported"
            }
            PresentationContextResultReason::TransferSyntaxesNotSupported => {
                "transfer syntaxes not supported"
            }
        };
        f.write_str(msg)
    }
}

#[derive(Clone, Eq, PartialEq, PartialOrd, Hash, Debug)]
pub enum AssociationRJResult {
    Permanent = 1,
    Transient = 2,
}

impl AssociationRJResult {
    fn from(value: u8) -> Option<AssociationRJResult> {
        match value {
            1 => Some(AssociationRJResult::Permanent),
            2 => Some(AssociationRJResult::Transient),
            _ => None,
        }
    }
}

#[derive(Clone, Eq, PartialEq, PartialOrd, Hash, Debug)]
pub enum AssociationRJSource {
    ServiceUser(AssociationRJServiceUserReason),
    ServiceProviderASCE(AssociationRJServiceProviderASCEReason),
    ServiceProviderPresentation(AssociationRJServiceProviderPresentationReason),
}

impl AssociationRJSource {
    fn from(source: u8, reason: u8) -> Option<AssociationRJSource> {
        let result = match (source, reason) {
            (1, 1) => {
                AssociationRJSource::ServiceUser(AssociationRJServiceUserReason::NoReasonGiven)
            }
            (1, 2) => AssociationRJSource::ServiceUser(
                AssociationRJServiceUserReason::ApplicationContextNameNotSupported,
            ),
            (1, 3) => AssociationRJSource::ServiceUser(
                AssociationRJServiceUserReason::CallingAETitleNotRecognized,
            ),
            (1, x) if x == 4 || x == 5 || x == 6 => {
                AssociationRJSource::ServiceUser(AssociationRJServiceUserReason::Reserved(x))
            }
            (1, 7) => AssociationRJSource::ServiceUser(
                AssociationRJServiceUserReason::CalledAETitleNotRecognized,
            ),
            //(1, 8) | (1, 9) | (1, 10) => {
            (1, x) if x == 8 || x == 9 || x == 10 => {
                AssociationRJSource::ServiceUser(AssociationRJServiceUserReason::Reserved(x))
            }
            (1, _) => {
                return None;
            }
            (2, 1) => AssociationRJSource::ServiceProviderASCE(
                AssociationRJServiceProviderASCEReason::NoReasonGiven,
            ),
            (2, 2) => AssociationRJSource::ServiceProviderASCE(
                AssociationRJServiceProviderASCEReason::ProtocolVersionNotSupported,
            ),
            (2, _) => {
                return None;
            }
            (3, 0) => AssociationRJSource::ServiceProviderPresentation(
                AssociationRJServiceProviderPresentationReason::Reserved(0),
            ),
            (3, 1) => AssociationRJSource::ServiceProviderPresentation(
                AssociationRJServiceProviderPresentationReason::TemporaryCongestion,
            ),
            (3, 2) => AssociationRJSource::ServiceProviderPresentation(
                AssociationRJServiceProviderPresentationReason::LocalLimitExceeded,
            ),
            (3, x) if x == 3 || x == 4 || x == 5 || x == 6 || x == 7 => {
                AssociationRJSource::ServiceProviderPresentation(
                    AssociationRJServiceProviderPresentationReason::Reserved(x),
                )
            }
            _ => {
                return None;
            }
        };
        Some(result)
    }
}

impl Display for AssociationRJSource {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            AssociationRJSource::ServiceUser(r) => Display::fmt(r, f),
            AssociationRJSource::ServiceProviderASCE(r) => Display::fmt(r, f),
            AssociationRJSource::ServiceProviderPresentation(r) => Display::fmt(r, f),
        }
    }
}

#[derive(Clone, Eq, PartialEq, PartialOrd, Hash, Debug)]
pub enum AssociationRJServiceUserReason {
    NoReasonGiven,
    ApplicationContextNameNotSupported,
    CallingAETitleNotRecognized,
    CalledAETitleNotRecognized,
    Reserved(u8),
}

impl Display for AssociationRJServiceUserReason {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            AssociationRJServiceUserReason::NoReasonGiven => f.write_str("no reason given"),
            AssociationRJServiceUserReason::ApplicationContextNameNotSupported => {
                f.write_str("application context name not supported")
            }
            AssociationRJServiceUserReason::CallingAETitleNotRecognized => {
                f.write_str("calling AE title not recognized")
            }
            AssociationRJServiceUserReason::CalledAETitleNotRecognized => {
                f.write_str("called AE title not recognized")
            }
            AssociationRJServiceUserReason::Reserved(code) => write!(f, "reserved code {}", code),
        }
    }
}

#[derive(Clone, Eq, PartialEq, PartialOrd, Hash, Debug)]
pub enum AssociationRJServiceProviderASCEReason {
    NoReasonGiven,
    ProtocolVersionNotSupported,
}

impl Display for AssociationRJServiceProviderASCEReason {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            AssociationRJServiceProviderASCEReason::NoReasonGiven => f.write_str("no reason given"),
            AssociationRJServiceProviderASCEReason::ProtocolVersionNotSupported => {
                f.write_str("protocol version not supported")
            }
        }
    }
}

#[derive(Clone, Eq, PartialEq, PartialOrd, Hash, Debug)]
pub enum AssociationRJServiceProviderPresentationReason {
    TemporaryCongestion,
    LocalLimitExceeded,
    Reserved(u8),
}

impl Display for AssociationRJServiceProviderPresentationReason {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            AssociationRJServiceProviderPresentationReason::TemporaryCongestion => {
                f.write_str("temporary congestion")
            }
            AssociationRJServiceProviderPresentationReason::LocalLimitExceeded => {
                f.write_str("local limit exceeded")
            }
            AssociationRJServiceProviderPresentationReason::Reserved(code) => {
                write!(f, "reserved code {}", code)
            }
        }
    }
}

#[derive(Clone, Eq, PartialEq, PartialOrd, Hash, Debug)]
pub struct PDataValue {
    pub presentation_context_id: u8,
    pub value_type: PDataValueType,
    pub is_last: bool,
    pub data: Vec<u8>,
}

#[derive(Clone, Eq, PartialEq, PartialOrd, Hash, Debug)]
pub enum PDataValueType {
    Command,
    Data,
}

#[derive(Clone, Eq, PartialEq, PartialOrd, Hash, Debug)]
pub enum AbortRQSource {
    ServiceUser,
    ServiceProvider(AbortRQServiceProviderReason),
    Reserved,
}

impl AbortRQSource {
    fn from(source: u8, reason: u8) -> Option<AbortRQSource> {
        let result = match (source, reason) {
            (0, _) => AbortRQSource::ServiceUser,
            (1, _) => AbortRQSource::Reserved,
            (2, 0) => {
                AbortRQSource::ServiceProvider(AbortRQServiceProviderReason::ReasonNotSpecified)
            }
            (2, 1) => AbortRQSource::ServiceProvider(AbortRQServiceProviderReason::UnrecognizedPdu),
            (2, 2) => AbortRQSource::ServiceProvider(AbortRQServiceProviderReason::UnexpectedPdu),
            (2, 3) => AbortRQSource::ServiceProvider(AbortRQServiceProviderReason::Reserved),
            (2, 4) => AbortRQSource::ServiceProvider(
                AbortRQServiceProviderReason::UnrecognizedPduParameter,
            ),
            (2, 5) => {
                AbortRQSource::ServiceProvider(AbortRQServiceProviderReason::UnexpectedPduParameter)
            }
            (2, 6) => {
                AbortRQSource::ServiceProvider(AbortRQServiceProviderReason::InvalidPduParameter)
            }
            (_, _) => {
                return None;
            }
        };

        Some(result)
    }
}

/// An enumeration of supported A-ABORT PDU provider reasons.
#[derive(Clone, Eq, PartialEq, PartialOrd, Hash, Debug)]
pub enum AbortRQServiceProviderReason {
    /// Reason Not Specified
    ReasonNotSpecified,
    /// Unrecognized PDU
    UnrecognizedPdu,
    /// Unexpected PDU
    UnexpectedPdu,
    /// Reserved
    Reserved,
    /// Unrecognized PDU parameter
    UnrecognizedPduParameter,
    /// Unexpected PDU parameter
    UnexpectedPduParameter,
    /// Invalid PDU parameter
    InvalidPduParameter,
}

impl Display for AbortRQServiceProviderReason {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let msg = match self {
            AbortRQServiceProviderReason::ReasonNotSpecified => "reason not specified",
            AbortRQServiceProviderReason::UnrecognizedPdu => "unrecognized PDU",
            AbortRQServiceProviderReason::UnexpectedPdu => "unexpected PDU",
            AbortRQServiceProviderReason::Reserved => "reserved code",
            AbortRQServiceProviderReason::UnrecognizedPduParameter => "unrecognized PDU parameter",
            AbortRQServiceProviderReason::UnexpectedPduParameter => "unexpected PDU parameter",
            AbortRQServiceProviderReason::InvalidPduParameter => "invalid PDU parameter",
        };
        f.write_str(msg)
    }
}

#[derive(Clone, Eq, PartialEq, PartialOrd, Hash, Debug)]
pub enum PduVariableItem {
    Unknown(u8),
    ApplicationContext(String),
    PresentationContextProposed(PresentationContextProposed),
    PresentationContextResult(PresentationContextResult),
    UserVariables(Vec<UserVariableItem>),
}

#[derive(Clone, Eq, PartialEq, PartialOrd, Hash, Debug)]
pub enum UserVariableItem {
    Unknown(u8, Vec<u8>),
    MaxLength(u32),
    ImplementationClassUID(String),
    ImplementationVersionName(String),
    SopClassExtendedNegotiationSubItem(String, Vec<u8>),
    UserIdentityItem(UserIdentity),
}

#[derive(Clone, Eq, PartialEq, PartialOrd, Hash, Debug)]
pub struct UserIdentity {
    positive_response_requested: bool,
    identity_type: UserIdentityType,
    primary_field: Vec<u8>,
    secondary_field: Vec<u8>,
}
impl UserIdentity {
    pub fn new(
        positive_response_requested: bool,
        identity_type: UserIdentityType,
        primary_field: Vec<u8>,
        secondary_field: Vec<u8>,
    ) -> Self {
        UserIdentity {
            positive_response_requested,
            identity_type,
            primary_field,
            secondary_field,
        }
    }

    pub fn positive_response_requested(&self) -> bool {
        self.positive_response_requested
    }

    pub fn identity_type(&self) -> UserIdentityType {
        self.identity_type.clone()
    }

    pub fn primary_field(&self) -> Vec<u8> {
        self.primary_field.clone()
    }

    pub fn secondary_field(&self) -> Vec<u8> {
        self.secondary_field.clone()
    }
}

#[derive(Clone, Eq, PartialEq, PartialOrd, Hash, Debug)]
#[non_exhaustive]
pub enum UserIdentityType {
    Username,
    UsernamePassword,
    KerberosServiceTicket,
    SamlAssertion,
    Jwt,
}
impl UserIdentityType {
    fn from(user_identity_type: u8) -> Option<Self> {
        match user_identity_type {
            1 => Some(Self::Username),
            2 => Some(Self::UsernamePassword),
            3 => Some(Self::KerberosServiceTicket),
            4 => Some(Self::SamlAssertion),
            5 => Some(Self::Jwt),
            _ => None,
        }
    }

    fn to_u8(&self) -> u8 {
        match self {
            Self::Username => 1,
            Self::UsernamePassword => 2,
            Self::KerberosServiceTicket => 3,
            Self::SamlAssertion => 4,
            Self::Jwt => 5,
        }
    }
}

/// An in-memory representation of a full Protocol Data Unit (PDU).
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Hash)]
pub enum Pdu {
    /// Unrecognized PDU type
    Unknown { pdu_type: u8, data: Vec<u8> },
    /// Association request (A-ASSOCIATION-RQ)
    AssociationRQ(AssociationRQ),
    /// Association acknowledgement (A-ASSOCIATION-AC)
    AssociationAC(AssociationAC),
    /// Association rejection (A-ASSOCIATION-RJ)
    AssociationRJ(AssociationRJ),
    /// P-Data
    PData { data: Vec<PDataValue> },
    /// Association release request (A-RELEASE-RQ)
    ReleaseRQ,
    /// Association release reply (A-RELEASE-RP)
    ReleaseRP,
    /// Association abort request (A-ABORT-RQ)
    AbortRQ { source: AbortRQSource },
}

impl Pdu {
    /// Provide a short description of the PDU.
    pub fn short_description(&self) -> impl std::fmt::Display + '_ {
        PduShortDescription(self)
    }
}

struct PduShortDescription<'a>(&'a Pdu);

impl std::fmt::Display for PduShortDescription<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self.0 {
            Pdu::Unknown { pdu_type, data } => {
                write!(
                    f,
                    "Unknown {{pdu_type: {}, data: {} bytes }}",
                    pdu_type,
                    data.len()
                )
            }
            Pdu::AssociationRQ { .. }
            | Pdu::AssociationAC { .. }
            | Pdu::AssociationRJ { .. }
            | Pdu::ReleaseRQ
            | Pdu::ReleaseRP
            | Pdu::AbortRQ { .. } => std::fmt::Debug::fmt(self.0, f),
            Pdu::PData { data } => {
                if data.len() == 1 {
                    write!(
                        f,
                        "PData [({:?}, {} bytes)]",
                        data[0].value_type,
                        data[0].data.len()
                    )
                } else if data.len() == 2 {
                    write!(
                        f,
                        "PData [({:?}, {} bytes), ({:?}, {} bytes)]",
                        data[0].value_type,
                        data[0].data.len(),
                        data[1].value_type,
                        data[1].data.len(),
                    )
                } else {
                    write!(f, "PData [{} p-data values]", data.len())
                }
            }
        }
    }
}

/// An in-memory representation of an association request
#[derive(Debug, Clone, Eq, Hash, PartialEq, PartialOrd)]
pub struct AssociationRQ {
    pub protocol_version: u16,
    pub calling_ae_title: String,
    pub called_ae_title: String,
    pub application_context_name: String,
    pub presentation_contexts: Vec<PresentationContextProposed>,
    pub user_variables: Vec<UserVariableItem>,
}

impl From<AssociationRQ> for Pdu {
    fn from(value: AssociationRQ) -> Self {
        Pdu::AssociationRQ(value)
    }
}

/// An in-memory representation of an association acknowledgement
#[derive(Debug, Clone, Eq, Hash, PartialEq, PartialOrd)]
pub struct AssociationAC {
    pub protocol_version: u16,
    pub calling_ae_title: String,
    pub called_ae_title: String,
    pub application_context_name: String,
    pub presentation_contexts: Vec<PresentationContextResult>,
    pub user_variables: Vec<UserVariableItem>,
}

impl From<AssociationAC> for Pdu {
    fn from(value: AssociationAC) -> Self {
        Pdu::AssociationAC(value)
    }
}

/// An in-memory representation of an association rejection.
#[derive(Debug, Clone, Eq, Hash, PartialEq, PartialOrd)]
pub struct AssociationRJ {
    pub result: AssociationRJResult,
    pub source: AssociationRJSource,
}

impl From<AssociationRJ> for Pdu {
    fn from(value: AssociationRJ) -> Self {
        Pdu::AssociationRJ(value)
    }
}

#[cfg(test)]
mod tests {
    use crate::pdu::{PDataValue, PDataValueType};

    use super::Pdu;

    #[test]
    fn pdu_short_description() {
        let pdu = Pdu::AbortRQ {
            source: super::AbortRQSource::ServiceUser,
        };
        assert_eq!(
            &pdu.short_description().to_string(),
            "AbortRQ { source: ServiceUser }",
        );

        let pdu = Pdu::PData {
            data: vec![PDataValue {
                is_last: true,
                presentation_context_id: 2,
                value_type: PDataValueType::Data,
                data: vec![0x55; 384],
            }],
        };
        assert_eq!(
            &pdu.short_description().to_string(),
            "PData [(Data, 384 bytes)]",
        );
    }
}