ntp_common.c
Go to the documentation of this file.
1 /**
2  * @file ntp_common.c
3  * @brief Definitions common to NTP client and server
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_common.h"
37 #include "debug.h"
38 
39 //Check TCP/IP stack configuration
40 #if (SNTP_CLIENT_SUPPORT == ENABLED || NTS_CLIENT_SUPPORT == ENABLED)
41 
42 
43 /**
44  * @brief Search a NTP packet for a given extension
45  * @param[in] extensions Pointer to the NTP extensions
46  * @param[in] length Length of the NTP extensions, in bytes
47  * @param[in] type Extension type
48  * @param[in] index Extension occurrence index
49  * @return If the specified extension is found, a pointer to the corresponding
50  * extension is returned. Otherwise NULL pointer is returned
51  **/
52 
53 const NtpExtension *ntpGetExtension(const uint8_t *extensions, size_t length,
54  uint16_t type, uint_t index)
55 {
56  uint_t k;
57  size_t i;
58  size_t n;
59  const NtpExtension *extension;
60 
61  //Initialize occurrence index
62  k = 0;
63 
64  //Parse extension fields
65  for(i = 0; i < length; i += n)
66  {
67  //Malformed extension?
68  if(length < sizeof(NtpExtension))
69  break;
70 
71  //Point to the current extension
72  extension = (NtpExtension *) (extensions + i);
73  //Retrieve the length of the extension
74  n = ntohs(extension->length);
75 
76  //Malformed extension?
77  if(n < sizeof(NtpExtension) || (i + n) > length)
78  break;
79 
80  //Matching extension type?
81  if(ntohs(extension->fieldType) == type)
82  {
83  //Matching occurrence found?
84  if(k++ == index)
85  {
86  return extension;
87  }
88  }
89  }
90 
91  //The specified extension type was not found
92  return NULL;
93 }
94 
95 #endif
uint8_t extensions[]
Definition: ntp_common.h:207
uint8_t type
Definition: coap_common.h:176
uint8_t length
Definition: tcp.h:368
const NtpExtension * ntpGetExtension(const uint8_t *extensions, size_t length, uint16_t type, uint_t index)
Search a NTP packet for a given extension.
Definition: ntp_common.c:53
#define ntohs(value)
Definition: cpu_endian.h:421
uint8_t n
Definitions common to NTP client and server.
NtpExtension
Definition: ntp_common.h:220
unsigned int uint_t
Definition: compiler_port.h:50
TCP/IP stack core.
Debugging facilities.