libptpmgmt  1.4
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] = { false };
74 
75  friend class ConfigFile;
76  void setGlobal();
77  bool set_val(char *line);
78 };
80 #endif /* SWIG */
81 
89 {
90  private:
91  std::map<std::string, ConfigSection> cfgSec;
92  ConfigSection *cfgGlobal; /* Not the owner, just a shortcut */
93  void clear_sections();
94 
95  uint32_t get_num(int idx, const std::string &section) const;
96  const std::string &get_str(int idx, const std::string &section) const;
97  const Binary &get_bin(int idx, const std::string &section) const;
98  bool is_global(int idx, const std::string &section) const;
99 
100  public:
101  ConfigFile() { clear_sections(); }
107  bool read_cfg(const std::string &file);
114  uint8_t transportSpecific(const std::string &section = "") const;
121  uint8_t domainNumber(const std::string &section = "") const;
128  uint8_t udp6_scope(const std::string &section = "") const;
135  uint8_t udp_ttl(const std::string &section = "") const;
142  uint8_t socket_priority(const std::string &section = "") const;
149  uint8_t network_transport(const std::string &section = "") const;
156  uint32_t active_key_id(const std::string &section = "") const;
163  uint8_t spp(const std::string &section = "") const;
170  uint8_t allow_unauth(const std::string &section = "") const;
177  bool haveSpp(const std::string &section = "") const;
184  const std::string &uds_address(const std::string &section = "") const;
191  const std::string &sa_file(const std::string &section = "") const;
198  const Binary &ptp_dst_mac(const std::string &section = "") const;
205  const Binary &p2p_dst_mac(const std::string &section = "") const;
206 };
207 
213 enum HMAC_t {
223 };
224 
231 class Spp
232 {
233  private:
234  #ifndef SWIG
235  struct key_t {
236  HMAC_t type = HMAC_SHA256;
237  Binary key;
238  size_t mac_size = 0;
239  void operator()(HMAC_t _t, Binary _k, size_t _m) {
240  type = _t;
241  key = _k;
242  mac_size = _m;
243  }
244  };
245  std::map<uint32_t, key_t> m_keys;
246  int8_t m_own_id;
247  protected:
249  bool set_val(char *line);
250  void set(long id) { m_own_id = (uint8_t)id; m_keys.clear(); }
251  friend class SaFile;
253  #endif
254  public:
264  bool addKey(uint32_t id, HMAC_t type, Binary &value, size_t digest,
265  bool replace = true);
271  bool have(uint32_t key) const { return key > 0 && m_keys.count(key) > 0; }
277  size_t mac_size(uint32_t key) const;
283  const Binary &key(uint32_t key) const;
288  size_t keys() const { return m_keys.size(); }
294  HMAC_t htype(uint32_t key) const;
299  uint8_t ownID() const { return m_own_id; }
304  Spp(uint8_t id) : m_own_id(id) {}
305  Spp() : m_own_id(-1) {}
306 };
307 
314 class SaFile
315 {
316  private:
317  std::map<uint8_t, Spp> m_spps;
318  public:
324  bool read_sa(const std::string &file);
334  bool read_sa(const ConfigFile &cfg, const std::string &section = "");
340  bool have(uint8_t spp) const { return m_spps.count(spp) > 0; }
347  bool have(uint8_t spp, uint32_t key) const {
348  return have(spp) && m_spps.at(spp).have(key);
349  }
355  const Spp &spp(uint8_t spp) const;
356 };
357 
358 __PTPMGMT_NAMESPACE_END
359 #else /* __cplusplus */
360 #include "c/cfg.h"
361 #endif /* __cplusplus */
362 
363 #endif /* __PTPMGMT_CFG_H */
Provide Binary class.
C interface to configuration class.
HMAC_t
Authentication key type.
Definition: cfg.h:213
@ HMAC_AES256
Definition: cfg.h:222
@ HMAC_SHA256
Definition: cfg.h:214
@ HMAC_AES128
Definition: cfg.h:218
Hold octets.
Definition: bin.h:28
Hold configuration parameters.
Definition: cfg.h:89
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:315
const Spp & spp(uint8_t spp) const
bool have(uint8_t spp, uint32_t key) const
Definition: cfg.h:347
bool have(uint8_t spp) const
Definition: cfg.h:340
bool read_sa(const std::string &file)
bool read_sa(const ConfigFile &cfg, const std::string &section="")
authentication security parameters
Definition: cfg.h:232
Spp(uint8_t id)
Definition: cfg.h:304
bool addKey(uint32_t id, HMAC_t type, Binary &value, size_t digest, bool replace=true)
uint8_t ownID() const
Definition: cfg.h:299
const Binary & key(uint32_t key) const
bool have(uint32_t key) const
Definition: cfg.h:271
HMAC_t htype(uint32_t key) const
size_t mac_size(uint32_t key) const
size_t keys() const
Definition: cfg.h:288