libptpmgmt  1.3
libptpmgmt library that provides the functionality of linuxptp pmc
cfg.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: LGPL-3.0-or-later
2  SPDX-FileCopyrightText: Copyright © 2021 Erez Geva <ErezGeva2@gmail.com> */
3 
16 #ifndef __PTPMGMT_CFG_H
17 #define __PTPMGMT_CFG_H
18 
19 #ifdef __cplusplus
20 #include <map>
21 #include "bin.h"
22 
23 __PTPMGMT_NAMESPACE_BEGIN
24 
25 class ConfigFile;
26 class SaFile;
27 
28 #ifndef SWIG
33 class ConfigSection
34 {
35  protected:
36  enum {
37  val_base_val,
38  transportSpecific_val = val_base_val,
39  domainNumber_val,
40  udp6_scope_val,
41  udp_ttl_val,
42  socket_priority_val,
43  network_transport_val,
44  active_key_id_val,
45  spp_val,
46  allow_unauth_val,
47  str_base_val,
48  uds_address_val = str_base_val,
49  sa_file_val,
50  bin_base_val,
51  ptp_dst_mac_val = bin_base_val,
52  p2p_dst_mac_val,
53  last_val,
54  };
55  /* new values must be add to ranges[] */
56  struct range_t {
57  const char *name;
58  const char *defStr;
59  uint32_t defVal;
60  uint32_t min;
61  uint32_t max;
62  };
63  /* ranges and default value */
64  static const range_t ranges[];
65  /* String values */
66  std::string m_str_vals[bin_base_val - str_base_val];
67  /* Binaries values */
68  Binary m_bin_vals[last_val - bin_base_val];
69  /* integer values */
70  uint32_t m_vals[str_base_val - val_base_val];
71  /* Determine if a value is set in the configuration file.
72  * Relevant for non global sections. */
73  bool m_set[last_val];
74 
75  friend class ConfigFile;
76  void setGlobal();
77  bool set_val(char *line);
78 
79  public:
80  ConfigSection() : m_set{0} {} /* Must be public for map usage */
81 };
83 #endif /* SWIG */
84 
92 {
93  private:
94  std::map<std::string, ConfigSection> cfgSec;
95  ConfigSection *cfgGlobal; /* Not the owner, just a shortcut */
96  void clear_sections();
97 
98  uint32_t get_num(int idx, const std::string &section) const;
99  const std::string &get_str(int idx, const std::string &section) const;
100  const Binary &get_bin(int idx, const std::string &section) const;
101  bool is_global(int idx, const std::string &section) const;
102 
103  public:
104  ConfigFile() { clear_sections(); }
110  bool read_cfg(const std::string &file);
117  uint8_t transportSpecific(const std::string &section = "") const;
124  uint8_t domainNumber(const std::string &section = "") const;
131  uint8_t udp6_scope(const std::string &section = "") const;
138  uint8_t udp_ttl(const std::string &section = "") const;
145  uint8_t socket_priority(const std::string &section = "") const;
152  uint8_t network_transport(const std::string &section = "") const;
159  uint32_t active_key_id(const std::string &section = "") const;
166  uint8_t spp(const std::string &section = "") const;
173  uint8_t allow_unauth(const std::string &section = "") const;
180  bool haveSpp(const std::string &section = "") const;
187  const std::string &uds_address(const std::string &section = "") const;
194  const std::string &sa_file(const std::string &section = "") const;
201  const Binary &ptp_dst_mac(const std::string &section = "") const;
208  const Binary &p2p_dst_mac(const std::string &section = "") const;
209 };
210 
216 enum HMAC_t {
226 };
227 
234 class Spp
235 {
236  private:
237  #ifndef SWIG
238  struct key_t {
239  HMAC_t type;
240  Binary key;
241  size_t mac_size;
242  };
243  std::map<uint32_t, key_t> m_keys;
244  int16_t m_own_id;
245  protected:
247  bool set_val(char *line);
248  void set(long id) { m_own_id = (uint8_t)id; m_keys.clear(); }
249  friend class SaFile;
251  #endif
252  public:
262  bool addKey(uint32_t id, HMAC_t type, Binary &value, size_t digest,
263  bool replace = true);
269  bool have(uint32_t key) const { return key > 0 && m_keys.count(key) > 0; }
275  size_t mac_size(uint32_t key) const;
281  const Binary &key(uint32_t key) const;
286  size_t keys() const { return m_keys.size(); }
292  HMAC_t htype(uint32_t key) const;
297  uint8_t ownID() const { return m_own_id; }
302  Spp(uint8_t id) : m_own_id(id) {}
303  Spp() : m_own_id(-1) {}
304 };
305 
312 class SaFile
313 {
314  private:
315  std::map<uint8_t, Spp> m_spps;
316  public:
322  bool read_sa(const std::string &file);
332  bool read_sa(const ConfigFile &cfg, const std::string &section = "");
338  bool have(uint8_t spp) const { return m_spps.count(spp) > 0; }
345  bool have(uint8_t spp, uint32_t key) const {
346  return have(spp) && m_spps.at(spp).have(key);
347  }
353  const Spp &spp(uint8_t spp) const;
354 };
355 
356 __PTPMGMT_NAMESPACE_END
357 #else /* __cplusplus */
358 #include "c/cfg.h"
359 #endif /* __cplusplus */
360 
361 #endif /* __PTPMGMT_CFG_H */
Provide Binary class.
C interface to configuration class.
HMAC_t
Authentication key type.
Definition: cfg.h:216
@ HMAC_AES256
Definition: cfg.h:225
@ HMAC_SHA256
Definition: cfg.h:217
@ HMAC_AES128
Definition: cfg.h:221
Hold octets.
Definition: bin.h:28
Hold configuration parameters.
Definition: cfg.h:92
const std::string & uds_address(const std::string &section="") const
uint8_t socket_priority(const std::string &section="") const
bool read_cfg(const std::string &file)
uint8_t udp_ttl(const std::string &section="") const
uint8_t transportSpecific(const std::string &section="") const
uint8_t udp6_scope(const std::string &section="") const
uint32_t active_key_id(const std::string &section="") const
const Binary & ptp_dst_mac(const std::string &section="") const
uint8_t network_transport(const std::string &section="") const
bool haveSpp(const std::string &section="") const
const std::string & sa_file(const std::string &section="") const
uint8_t allow_unauth(const std::string &section="") const
uint8_t spp(const std::string &section="") const
const Binary & p2p_dst_mac(const std::string &section="") const
uint8_t domainNumber(const std::string &section="") const
authentication security association file
Definition: cfg.h:313
const Spp & spp(uint8_t spp) const
bool have(uint8_t spp, uint32_t key) const
Definition: cfg.h:345
bool have(uint8_t spp) const
Definition: cfg.h:338
bool read_sa(const std::string &file)
bool read_sa(const ConfigFile &cfg, const std::string &section="")
authentication security parameters
Definition: cfg.h:235
Spp(uint8_t id)
Definition: cfg.h:302
bool addKey(uint32_t id, HMAC_t type, Binary &value, size_t digest, bool replace=true)
uint8_t ownID() const
Definition: cfg.h:297
const Binary & key(uint32_t key) const
bool have(uint32_t key) const
Definition: cfg.h:269
HMAC_t htype(uint32_t key) const
size_t mac_size(uint32_t key) const
size_t keys() const
Definition: cfg.h:286