ntp_debug.c
Go to the documentation of this file.
1 /**
2  * @file ntp_debug.c
3  * @brief Data logging functions for debugging purpose (NTP)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.4
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL NTP_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "ntp/ntp_debug.h"
37 #include "debug.h"
38 
39 //Check TCP/IP stack configuration
40 #if (NTP_TRACE_LEVEL >= TRACE_LEVEL_DEBUG)
41 
42 //Leap indicators
44 {
45  {NTP_LI_NO_WARNING, "no warning"},
46  {NTP_LI_LAST_MIN_HAS_61_SECS, "last minute has 61 seconds"},
47  {NTP_LI_LAST_MIN_HAS_59_SECS, "last minute has 59 seconds"},
48  {NTP_LI_ALARM_CONDITION, "alarm condition"}
49 };
50 
51 //NTP version numbers
53 {
54  {NTP_VERSION_3, "NTPv3"},
55  {NTP_VERSION_4, "NTPv4"}
56 };
57 
58 //Protocol modes
60 {
61  {NTP_MODE_SYMMETRIC_ACTIVE, "symmetric active"},
62  {NTP_MODE_SYMMETRIC_PASSIVE, "symmetric passive"},
63  {NTP_MODE_CLIENT, "client"},
64  {NTP_MODE_SERVER, "server"},
65  {NTP_MODE_BROADCAST, "broadcast"}
66 };
67 
68 //Stratum
70 {
71  {NTP_STRATUM_KISS_OF_DEATH, "kiss-of-death message"},
72  {NTP_STRATUM_PRIMARY, "primary"},
73  {NTP_STRATUM_SECONDARY_2, "secondary"},
74  {NTP_STRATUM_SECONDARY_3, "secondary"},
75  {NTP_STRATUM_SECONDARY_4, "secondary"},
76  {NTP_STRATUM_SECONDARY_5, "secondary"},
77  {NTP_STRATUM_SECONDARY_6, "secondary"},
78  {NTP_STRATUM_SECONDARY_7, "secondary"},
79  {NTP_STRATUM_SECONDARY_8, "secondary"},
80  {NTP_STRATUM_SECONDARY_9, "secondary"},
81  {NTP_STRATUM_SECONDARY_10, "secondary"},
82  {NTP_STRATUM_SECONDARY_11, "secondary"},
83  {NTP_STRATUM_SECONDARY_12, "secondary"},
84  {NTP_STRATUM_SECONDARY_13, "secondary"},
85  {NTP_STRATUM_SECONDARY_14, "secondary"},
86  {NTP_STRATUM_SECONDARY_15, "secondary"}
87 };
88 
89 //Extensions field types
91 {
92  {NTP_EXTENSION_TYPE_NO_OPERATION_REQ, "No-Operation Request"},
93  {NTP_EXTENSION_TYPE_UNIQUE_ID, "Unique Identifier"},
94  {NTP_EXTENSION_TYPE_NTS_COOKIE, "NTS Cookie"},
95  {NTP_EXTENSION_TYPE_NTS_COOKIE_PLACEHOLDER, "NTS Cookie Placeholder"},
96  {NTP_EXTENSION_TYPE_NTS_AEAD, "NTS Authenticator and Encrypted Extension Fields"},
97  {NTP_EXTENSION_TYPE_NO_OPERATION_RESP, "No-Operation Response"},
98  {NTP_EXTENSION_TYPE_NO_OPERATION_ERROR_RESP, "No-Operation Error Response"}
99 };
100 
101 
102 /**
103  * @brief Dump NTP packet for debugging purpose
104  * @param[in] packet Pointer to the NTP packet to dump
105  * @param[in] length Length of the packet, in bytes
106  **/
107 
108 void ntpDumpPacket(const NtpHeader *packet, size_t length)
109 {
110  uint32_t value;
111  const char_t *name;
112  const NtpHeader *header;
113 
114  //Malformed packet?
115  if(length < sizeof(NtpHeader))
116  return;
117 
118  //Point to the NTP packet header
119  header = (NtpHeader *) packet;
120 
121  //Convert the Leap Indicator field to string representation
124 
125  //Dump Leap Indicator field
126  TRACE_DEBUG(" Leap indicator = %" PRIu8 " (%s)\r\n", header->li, name);
127 
128  //Convert the Version Number field to string representation
129  name = ntpGetParamName(header->vn, ntpVersionList,
131 
132  //Dump Version Number field
133  TRACE_DEBUG(" Version Number = %" PRIu8 " (%s)\r\n", header->vn, name);
134 
135  //Convert the Mode field to string representation
137 
138  //Dump Mode field
139  TRACE_DEBUG(" Mode = %" PRIu8 " (%s)\r\n", header->mode, name);
140 
141  //The Stratum field is significant only in server messages
142  if(header->mode == NTP_MODE_SERVER)
143  {
144  //Convert the Startum field to string representation
145  name = ntpGetParamName(header->stratum, ntpStartumList,
147 
148  //Dump Stratum field
149  TRACE_DEBUG(" Stratum = %" PRIu8 " (%s)\r\n", header->stratum, name);
150  }
151  else
152  {
153  //Dump Stratum field
154  TRACE_DEBUG(" Stratum = %" PRIu8 "\r\n", header->stratum);
155  }
156 
157  //Dump other fields
158  TRACE_DEBUG(" Poll = %" PRIu8 " (%us)\r\n", header->poll, 1U << header->poll);
159  TRACE_DEBUG(" Precision = %" PRId8 "\r\n", header->precision);
160  TRACE_DEBUG(" Root Delay = %" PRIu32 "\r\n", ntohl(header->rootDelay));
161  TRACE_DEBUG(" Root Dispersion = %" PRIu32 "\r\n", ntohl(header->rootDispersion));
162 
163  //The Reference Identifier field is significant only in server messages,
164  //where for stratum 0 (kiss-of-death message) and 1 (primary server)
165  if(header->mode == NTP_MODE_SERVER &&
166  header->stratum <= NTP_STRATUM_PRIMARY)
167  {
168  //The value is a four-character ASCII string
169  value = htonl(header->referenceId);
170 
171  //Dump Reference Identifier field
172  TRACE_DEBUG(" Reference Identifier = '%c%c%c%c'\r\n",
173  (value >> 24) & 0xFF, (value >> 16) & 0xFF, (value >> 8) & 0xFF,
174  value & 0xFF);
175  }
176  else
177  {
178  //Dump Reference Identifier field
179  TRACE_DEBUG(" Reference Identifier = %" PRIu32 "\r\n",
180  header->referenceId);
181  }
182 
183  //Dump Reference Timestamp field
184  TRACE_DEBUG(" ReferenceTimestamp\r\n");
185  ntpDumpTimestamp(&header->referenceTimestamp);
186 
187  //Dump Originate Timestamp field
188  TRACE_DEBUG(" Originate Timestamp\r\n");
189  ntpDumpTimestamp(&header->originateTimestamp);
190 
191  //Dump Receive Timestamp field
192  TRACE_DEBUG(" Receive Timestamp\r\n");
193  ntpDumpTimestamp(&header->receiveTimestamp);
194 
195  //Dump Transmit Timestamp field
196  TRACE_DEBUG(" Transmit Timestamp\r\n");
197  ntpDumpTimestamp(&header->transmitTimestamp);
198 
199  //Get the length of the extension fields
200  length -= sizeof(NtpHeader);
201  //Parse extension fields
202  ntpDumpExtensions(header->extensions, length);
203 }
204 
205 
206 /**
207  * @brief Dump NTP extension fields
208  * @param[in] extensions Pointer to the extension fields
209  * @param[in] length Total length of the extension fields, in bytes
210  **/
211 
212 void ntpDumpExtensions(const uint8_t *extensions, size_t length)
213 {
214  size_t i;
215  size_t n;
216  const NtpExtension *extension;
217 
218  //Parse extension fields
219  for(i = 0; i < length; i += n)
220  {
221  //Malformed extension?
222  if(length < sizeof(NtpExtension))
223  break;
224 
225  //Point to the current extension
226  extension = (NtpExtension *) (extensions + i);
227  //Retrieve the length of the extension
228  n = ntohs(extension->length);
229 
230  //Malformed extension?
231  if(n < sizeof(NtpExtension) || (i + n) > length)
232  break;
233 
234  //Dump extension
235  ntpDumpExtension(extension, n);
236  }
237 }
238 
239 
240 /**
241  * @brief Dump NTP extension field
242  * @param[in] extension Pointer to the extension field
243  * @param[in] length Length of the extension field, in bytes
244  **/
245 
246 void ntpDumpExtension(const NtpExtension *extension, size_t length)
247 {
248  uint16_t type;
249  const char_t *name;
250 
251  //Malformed extension?
252  if(length < sizeof(NtpExtension))
253  return;
254 
255  //Get extension type
256  type = ntohs(extension->fieldType);
257 
258  //Convert the Type field to string representation
261 
262  //Display the name of the current extension
263  if(osStrcmp(name, "Unknown") != 0)
264  {
265  TRACE_DEBUG(" %s Extension (%" PRIu8 " bytes)\r\n", name, length);
266  }
267  else
268  {
269  TRACE_DEBUG(" Extension %" PRIu16 " (%" PRIu8 " bytes)\r\n", type,
270  length);
271  }
272 
273  //Check extension type
275  {
276  //Dump NTS Authenticator and Encrypted Extension Fields extension
278  }
279  else
280  {
281  //Retrieve the length of the extension value
282  length -= sizeof(NtpExtension);
283  //Dump extension value
284  TRACE_DEBUG_ARRAY(" ", extension->value, length);
285  }
286 }
287 
288 
289 /**
290  * @brief Dump NTS Authenticator and Encrypted Extension Fields extension
291  * @param[in] extension Pointer to the extension field
292  * @param[in] length Length of the extension field, in bytes
293  **/
294 
295 void ntpDumpNtsAeadExtension(const NtpNtsAeadExtension *extension, size_t length)
296 {
297  uint16_t nonceLen;
298  uint16_t ciphertextLen;
299 
300  //Malformed extension?
301  if(length < sizeof(NtpNtsAeadExtension))
302  return;
303 
304  //Retrieve the length of the nonce and ciphertext
305  nonceLen = ntohs(extension->nonceLength);
306  ciphertextLen = ntohs(extension->ciphertextLength);
307 
308  //Malformed extension?
309  if(length < (sizeof(NtpNtsAeadExtension) + nonceLen + ciphertextLen))
310  return;
311 
312  //Dump extension
313  TRACE_DEBUG(" Nonce Length = %" PRIu16 "\r\n", nonceLen);
314  TRACE_DEBUG(" Ciphertext Length = %" PRIu16 "\r\n", ciphertextLen);
315 
316  //Dump nonce
317  TRACE_DEBUG(" Nonce (%" PRIu16 " bytes)\r\n", nonceLen);
318  TRACE_DEBUG_ARRAY(" ", extension->nonce, nonceLen);
319 
320  //Dump ciphertext
321  TRACE_DEBUG(" Ciphertext (%" PRIu16 " bytes)\r\n", ciphertextLen);
322  TRACE_DEBUG_ARRAY(" ", extension->nonce + nonceLen, ciphertextLen);
323 }
324 
325 
326 /**
327  * @brief Dump NTP timestamp
328  * @param[in] timestamp Pointer to the NTP timestamp
329  **/
330 
331 void ntpDumpTimestamp(const NtpTimestamp *timestamp)
332 {
333  //Dump seconds
334  TRACE_DEBUG(" Seconds = %" PRIu32 "\r\n", ntohl(timestamp->seconds));
335  //Dump fraction field
336  TRACE_DEBUG(" Fraction = %" PRIu32 "\r\n", ntohl(timestamp->fraction));
337 }
338 
339 
340 /**
341  * @brief Convert a parameter to string representation
342  * @param[in] value Parameter value
343  * @param[in] paramList List of acceptable parameters
344  * @param[in] paramListLen Number of entries in the list
345  * @return NULL-terminated string describing the parameter
346  **/
347 
348 const char_t *ntpGetParamName(uint_t value, const NtpParamName *paramList,
349  size_t paramListLen)
350 {
351  uint_t i;
352 
353  //Default name for unknown values
354  static const char_t defaultName[] = "Unknown";
355 
356  //Loop through the list of acceptable parameters
357  for(i = 0; i < paramListLen; i++)
358  {
359  if(paramList[i].value == value)
360  return paramList[i].name;
361  }
362 
363  //Unknown value
364  return defaultName;
365 }
366 
367 #endif
const char_t * name
Definition: ntp_debug.h:52
@ NTP_STRATUM_SECONDARY_2
Definition: ntp_common.h:101
@ NTP_EXTENSION_TYPE_NTS_COOKIE
NTS Cookie.
Definition: ntp_common.h:155
void ntpDumpPacket(const NtpHeader *packet, size_t length)
Dump NTP packet for debugging purpose.
Definition: ntp_debug.c:108
uint8_t extensions[]
Definition: ntp_common.h:207
@ NTP_LI_NO_WARNING
Definition: ntp_common.h:59
NtpTimestamp
Definition: ntp_common.h:179
void ntpDumpTimestamp(const NtpTimestamp *timestamp)
Dump NTP timestamp.
Definition: ntp_debug.c:331
@ NTP_STRATUM_SECONDARY_13
Definition: ntp_common.h:112
@ NTP_MODE_SERVER
Definition: ntp_common.h:88
uint8_t type
Definition: coap_common.h:176
@ NTP_STRATUM_SECONDARY_6
Definition: ntp_common.h:105
@ NTP_STRATUM_SECONDARY_7
Definition: ntp_common.h:106
void ntpDumpExtensions(const uint8_t *extensions, size_t length)
Dump NTP extension fields.
Definition: ntp_debug.c:212
char_t name[]
#define osStrcmp(s1, s2)
Definition: os_port.h:171
NtpNtsAeadExtension
Definition: ntp_common.h:234
@ NTP_STRATUM_SECONDARY_8
Definition: ntp_common.h:107
const NtpParamName ntpStartumList[]
Definition: ntp_debug.c:69
@ NTP_EXTENSION_TYPE_NO_OPERATION_ERROR_RESP
No-Operation Error Response.
Definition: ntp_common.h:159
@ NTP_MODE_BROADCAST
Definition: ntp_common.h:89
@ NTP_STRATUM_SECONDARY_4
Definition: ntp_common.h:103
#define htonl(value)
Definition: cpu_endian.h:414
@ NTP_STRATUM_SECONDARY_9
Definition: ntp_common.h:108
@ NTP_EXTENSION_TYPE_NO_OPERATION_REQ
No-Operation Request.
Definition: ntp_common.h:153
Data logging functions for debugging purpose (NTP)
@ NTP_MODE_SYMMETRIC_PASSIVE
Definition: ntp_common.h:86
@ NTP_STRATUM_SECONDARY_12
Definition: ntp_common.h:111
@ NTP_STRATUM_SECONDARY_10
Definition: ntp_common.h:109
@ NTP_EXTENSION_TYPE_UNIQUE_ID
Unique Identifier.
Definition: ntp_common.h:154
const NtpParamName ntpLeapIndicatorList[]
Definition: ntp_debug.c:43
@ NTP_STRATUM_SECONDARY_15
Definition: ntp_common.h:114
@ NTP_VERSION_3
Definition: ntp_common.h:74
void ntpDumpNtsAeadExtension(const NtpNtsAeadExtension *extension, size_t length)
Dump NTS Authenticator and Encrypted Extension Fields extension.
Definition: ntp_debug.c:295
uint8_t length
Definition: tcp.h:368
@ NTP_LI_LAST_MIN_HAS_61_SECS
Definition: ntp_common.h:60
@ NTP_LI_ALARM_CONDITION
Definition: ntp_common.h:62
const NtpParamName ntpVersionList[]
Definition: ntp_debug.c:52
@ NTP_LI_LAST_MIN_HAS_59_SECS
Definition: ntp_common.h:61
#define ntohs(value)
Definition: cpu_endian.h:421
#define TRACE_DEBUG(...)
Definition: debug.h:107
char char_t
Definition: compiler_port.h:48
@ NTP_STRATUM_KISS_OF_DEATH
Definition: ntp_common.h:99
const NtpParamName ntpExtensionTypeList[]
Definition: ntp_debug.c:90
@ NTP_MODE_CLIENT
Definition: ntp_common.h:87
#define TRACE_DEBUG_ARRAY(p, a, n)
Definition: debug.h:108
@ NTP_STRATUM_SECONDARY_14
Definition: ntp_common.h:113
NtpHeader
Definition: ntp_common.h:208
uint8_t n
@ NTP_EXTENSION_TYPE_NO_OPERATION_RESP
No-Operation Response.
Definition: ntp_common.h:158
@ NTP_STRATUM_PRIMARY
Definition: ntp_common.h:100
uint8_t value[]
Definition: tcp.h:369
@ NTP_STRATUM_SECONDARY_11
Definition: ntp_common.h:110
@ NTP_STRATUM_SECONDARY_5
Definition: ntp_common.h:104
Parameter value/name binding.
Definition: ntp_debug.h:50
@ NTP_EXTENSION_TYPE_NTS_COOKIE_PLACEHOLDER
NTS Cookie Placeholder.
Definition: ntp_common.h:156
const NtpParamName ntpModeList[]
Definition: ntp_debug.c:59
void ntpDumpExtension(const NtpExtension *extension, size_t length)
Dump NTP extension field.
Definition: ntp_debug.c:246
@ NTP_STRATUM_SECONDARY_3
Definition: ntp_common.h:102
@ NTP_MODE_SYMMETRIC_ACTIVE
Definition: ntp_common.h:85
NtpExtension
Definition: ntp_common.h:220
unsigned int uint_t
Definition: compiler_port.h:50
TCP/IP stack core.
const char_t * ntpGetParamName(uint_t value, const NtpParamName *paramList, size_t paramListLen)
Convert a parameter to string representation.
Definition: ntp_debug.c:348
@ NTP_VERSION_4
Definition: ntp_common.h:75
#define ntohl(value)
Definition: cpu_endian.h:422
Debugging facilities.
@ NTP_EXTENSION_TYPE_NTS_AEAD
NTS Authenticator and Encrypted Extension Fields.
Definition: ntp_common.h:157
#define arraysize(a)
Definition: os_port.h:71