ntp_client/formats/
mod.rs

1use std::fmt;
2
3pub mod timestamp;
4use strum::*;
5
6#[repr(u8)]
7#[derive(Debug, PartialEq, FromRepr, Clone)]
8pub enum LeapIndicator {
9    NoWarning = 0,
10    AddOne = 1,
11    SubOne = 2,
12    Unknown = 3,
13}
14
15impl Default for LeapIndicator {
16    fn default() -> LeapIndicator {
17        LeapIndicator::NoWarning
18    }
19}
20
21#[repr(u8)]
22#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, FromRepr, Clone)]
23pub enum Version {
24    Ver1 = 1,
25    Ver2 = 2,
26    Ver3 = 3,
27    Ver4 = 4,
28}
29
30impl Default for Version {
31    fn default() -> Version {
32        Version::Ver2
33    }
34}
35
36#[repr(u8)]
37#[derive(Debug, PartialEq, FromRepr, Clone)]
38pub enum Mode {
39    Reserved = 0,
40    SymmetricActive = 1,
41    SymmetricPassive = 2,
42    Client = 3,
43    Server = 4,
44    Broadcase = 5,
45    NTPControlMessage = 6,
46    ReservedForPrivateUse = 7,
47}
48
49impl Default for Mode {
50    fn default() -> Mode {
51        Mode::Reserved
52    }
53}
54
55#[derive(Debug, PartialEq, Eq, PartialOrd, Default, Ord,Clone)]
56pub struct Stratum {
57    value: u8,
58}
59
60impl Stratum {
61    #[inline]
62    pub fn new(val: u8) -> Stratum {
63        Stratum { value: val }
64    }
65    #[inline]
66    pub fn unspecified(&self) -> bool {
67        self.value == 0
68    }
69    #[inline]
70    pub fn invalid(&self) -> bool {
71        self.value == 0
72    }
73    #[inline]
74    pub fn primary(&self) -> bool {
75        self.value == 1
76    }
77    #[inline]
78    pub fn secondary(&self) -> bool {
79        2 <= self.value && self.value <= 15
80    }
81    #[inline]
82    pub fn unsynchronized(&self) -> bool {
83        self.value == 16
84    }
85    #[inline]
86    pub fn reserved(&self) -> bool {
87        self.value >= 17
88    }
89    #[inline]
90    pub fn get_value(&self) -> u8 {
91        self.value
92    }
93}
94
95#[repr(u32)]
96#[derive(Debug, PartialEq, Copy, Clone, FromRepr)]
97pub enum ReferenceIdentifier {
98    Primary(PrimarySource),
99    Secondary(u32),
100}
101
102impl Default for ReferenceIdentifier {
103    fn default() -> ReferenceIdentifier {
104        ReferenceIdentifier::Primary(Default::default())
105    }
106}
107
108impl From<ReferenceIdentifier> for u32 {
109    fn from(ri: ReferenceIdentifier) -> u32 {
110        match ri {
111            ReferenceIdentifier::Primary(s) => s as u32,
112            ReferenceIdentifier::Secondary(s) => s as u32,
113        }
114    }
115}
116
117impl fmt::Display for ReferenceIdentifier {
118    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
119        match *self {
120            ReferenceIdentifier::Primary(id) => write!(f, "{}", id),
121            ReferenceIdentifier::Secondary(id) => {
122                let fields: Vec<u8> = vec![
123                    (id >> 24 & 0xff) as u8,
124                    (id >> 16 & 0xff) as u8,
125                    (id >> 8 & 0xff) as u8,
126                    (id & 0xff) as u8,
127                ];
128                write!(f, "{}.{}.{}.{}", fields[0], fields[1], fields[2], fields[3])
129            }
130        }
131    }
132}
133
134/// ascii chars packed into a u32 for matching a raw buffer
135#[repr(u32)]
136#[derive(Debug, PartialEq, Copy, Clone, FromRepr)]
137pub enum PrimarySource {
138    GOES = 1196377427,
139    GPS = 1196446464,
140    CDMA = 1128549697,
141    GAL = 1195461632,
142    PPS = 1347441408,
143    IRIG = 1230129479,
144    WWVB = 1465341506,
145    DCF = 1145259520,
146    HBG = 1212303104,
147    MSF = 1297303040,
148    JJY = 1246386432,
149    LORC = 1280266819,
150    TDF = 1413760512,
151    CHU = 1128813824,
152    WWV = 1465341440,
153    WWVH = 1465341512,
154    NIST = 1313428308,
155    ACTS = 1094931539,
156    USNO = 1431522895,
157    PTB = 1347699200,
158    GOOG = 1196379975,
159    LOCL = 1280262988,
160    CESM = 1128616781,
161    RBDM = 1380074573,
162    OMEG = 1330464071,
163    DCN = 1145261568,
164    TSP = 1414746112,
165    DTS = 1146376960,
166    ATOM = 1096044365,
167    VLF = 1447839232,
168    OPPS = 1330663507,
169    FREE = 1179796805,
170    INIT = 1229867348,
171    NULL = 0000000000,
172}
173
174impl Default for PrimarySource {
175    fn default() -> PrimarySource {
176        PrimarySource::NULL
177    }
178}
179
180impl fmt::Display for PrimarySource {
181    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
182        use self::PrimarySource::*;
183        let desc = match *self {
184            GOES => "GOES: Geosynchronous Orbit Environment Satellite",
185            GPS => "GPS: Global Position System",
186            CDMA => "CDMA: Code Division Multiple Access",
187            GAL => "GAL: Galileo Positioning System",
188            PPS => "PPS: Generic pulse-per-second",
189            IRIG => "IRIG: Inter-Range Instrumentation Group",
190            WWVB => "WWVB: LF Radio WWVB Ft. Collins, CO 60 kHz",
191            DCF => "DCF: LF Radio DCF77 Mainflingen, DE 77.5 kHz",
192            HBG => "HBG: LF Radio HBG Prangins, HB 75 kHz",
193            MSF => "MSF: LF Radio MSF Anthorn, UK 60 kHz",
194            JJY => "JJY: LF Radio JJY Fukushima, JP 40 kHz, Saga, JP 60 kHz",
195            LORC => "LORC: MF Radio LORAN C station, 100 kHz",
196            TDF => "TDF: MF Radio Allouis, FR 162 kHz",
197            CHU => "CHU: HF Radio CHU Ottawa, Ontario",
198            WWV => "WWV: HF Radio WWV Ft. Collins, CO",
199            WWVH => "WWVH: HF Radio WWVH Kauai, HI",
200            NIST => "NIST: NIST telephone modem",
201            ACTS => "ACTS: ACTS telephone modem",
202            USNO => "USNO: USNO telephone modem",
203            PTB => "PTB: European telephone modem",
204            GOOG => "GOOG: Google Public NTP",
205            LOCL => "LOCL: Not Yet Described",
206            CESM => "CESM: Not Yet Described",
207            RBDM => "RBDM: Not Yet Described",
208            OMEG => "OMEG: Not Yet Described",
209            DCN => "DCN: Not Yet Described",
210            TSP => "TSP: Not Yet Described",
211            DTS => "DTS: Not Yet Described",
212            ATOM => "ATOM: Not Yet Described",
213            VLF => "VLF: Not Yet Described",
214            OPPS => "OPPS: Not Yet Described",
215            FREE => "FREE: Not Yet Described",
216            INIT => "INIT: Not Yet Described",
217            NULL => "NULL: Null Value",
218        };
219        write!(f, "{}", desc)
220    }
221}