Aktualizr
C++ SOTA Client
utils.h
1 #ifndef UTILS_H_
2 #define UTILS_H_
3 
4 #include <boost/filesystem.hpp>
5 #include <memory>
6 #include <string>
7 
8 #include <curl/curl.h>
9 #include <netinet/in.h>
10 
11 #include "json/json.h"
12 
13 struct Utils {
14  static std::string fromBase64(std::string base64_string);
15  static std::string toBase64(const std::string &tob64);
16  static std::string stripQuotes(const std::string &value);
17  static std::string addQuotes(const std::string &value);
18  static std::string extractField(const std::string &in, unsigned int field_id);
19  static Json::Value parseJSON(const std::string &json_str);
20  static Json::Value parseJSONFile(const boost::filesystem::path &filename);
21  static std::string jsonToStr(const Json::Value &json);
22  static std::string jsonToCanonicalStr(const Json::Value &json);
23  static std::string genPrettyName();
24  static std::string readFile(const boost::filesystem::path &filename, bool trim = false);
25 
26  static void writeFile(const boost::filesystem::path &filename, const char *content, size_t size);
27  static void writeFile(const boost::filesystem::path &filename, const std::string &content,
28  bool create_directories = true);
29  static void writeFile(const boost::filesystem::path &filename, const Json::Value &content,
30  bool create_directories = true);
31  static void copyDir(const boost::filesystem::path &from, const boost::filesystem::path &to);
32  static std::string readFileFromArchive(std::istream &as, const std::string &filename, bool trim = false);
33  static void writeArchive(const std::map<std::string, std::string> &entries, std::ostream &as);
34  static void removeFileFromArchive(const boost::filesystem::path &archive_path, const std::string &filename);
35  static Json::Value getHardwareInfo();
36  static Json::Value getNetworkInfo();
37  static std::string getHostname();
38  static std::string randomUuid();
39  static sockaddr_storage ipGetSockaddr(int fd);
40  static std::string ipDisplayName(const sockaddr_storage &saddr);
41  static int ipPort(const sockaddr_storage &saddr);
42  static int shell(const std::string &command, std::string *output, bool include_stderr = false);
43  static boost::filesystem::path absolutePath(const boost::filesystem::path &root, const boost::filesystem::path &file);
44  static void createDirectories(const boost::filesystem::path &path, mode_t mode);
45  static bool createSecureDirectory(const boost::filesystem::path &path);
46  static std::string urlEncode(const std::string &input);
47  static CURL *curlDupHandleWrapper(CURL *curl_in, bool using_pkcs11);
48  static std::vector<boost::filesystem::path> getDirEntriesByExt(const boost::filesystem::path &dir_path,
49  const std::string &ext);
50  static void setStorageRootPath(const std::string &storage_root_path);
51  static boost::filesystem::path getStorageRootPath();
52 
53  static void setUserAgent(std::string user_agent);
54  static const char *getUserAgent();
55 
56  static void setCaPath(boost::filesystem::path path);
57  static const char *getCaPath();
58 
59  private:
60  static std::string storage_root_path_;
61  static std::string user_agent_;
62  static boost::filesystem::path ca_path_;
63 };
64 
65 /**
66  * RAII Temporary file creation
67  */
69  public:
70  explicit TemporaryFile(const std::string &hint = "file");
71  TemporaryFile(const TemporaryFile &) = delete;
72  TemporaryFile operator=(const TemporaryFile &) = delete;
73  ~TemporaryFile();
74  void PutContents(const std::string &contents) const;
75  boost::filesystem::path Path() const;
76  std::string PathString() const;
77 
78  private:
79  boost::filesystem::path tmp_name_;
80 };
81 
83  public:
84  explicit TemporaryDirectory(const std::string &hint = "dir");
85  TemporaryDirectory(const TemporaryDirectory &) = delete;
86  TemporaryDirectory operator=(TemporaryDirectory &) = delete;
88  boost::filesystem::path Path() const;
89  std::string PathString() const;
90  boost::filesystem::path operator/(const boost::filesystem::path &subdir) const;
91 
92  private:
93  boost::filesystem::path tmp_name_;
94 };
95 
96 // helper template for C (mostly openssl) data structured
97 // user should still take care about the order of destruction
98 // by instantiating StructGuard<> in a right order.
99 // BTW local variables are destructed in reverse order of instantiation
100 template <typename T>
101 using StructGuard = std::unique_ptr<T, void (*)(T *)>;
102 template <typename T>
103 using StructGuardInt = std::unique_ptr<T, int (*)(T *)>;
104 
105 class Socket {
106  public:
107  Socket();
108  Socket(int fd) : socket_fd_(fd) {}
109  virtual ~Socket();
110 
111  Socket(const Socket &) = delete;
112  Socket &operator=(const Socket &) = delete;
113 
114  int &operator*() { return socket_fd_; }
115  std::string toString() const;
116 
117  protected:
118  void bind(in_port_t port, bool reuse = true) const;
119 
120  protected:
121  int socket_fd_;
122 };
123 
124 class ConnectionSocket : public Socket {
125  public:
126  ConnectionSocket(const std::string &ip, in_port_t port, in_port_t bind_port = 0);
127  ~ConnectionSocket() override;
128 
129  public:
130  int connect();
131 
132  private:
133  struct sockaddr_in remote_sock_address_;
134 };
135 
136 class ListenSocket : public Socket {
137  public:
138  ListenSocket(in_port_t port);
139  in_port_t port() const { return _port; }
140 
141  private:
142  in_port_t _port;
143 };
144 
145 // wrapper for curl handles
147  public:
148  CurlEasyWrapper();
149  ~CurlEasyWrapper();
150  CURL *get() { return handle; }
151 
152  private:
153  CURL *handle;
154 };
155 
156 template <typename... T>
157 static void curlEasySetoptWrapper(CURL *curl_handle, CURLoption option, T &&... args) {
158  const CURLcode retval = curl_easy_setopt(curl_handle, option, std::forward<T>(args)...);
159  if (retval != 0U) {
160  throw std::runtime_error(std::string("curl_easy_setopt error: ") + curl_easy_strerror(retval));
161  }
162 }
163 
164 // this is reference implementation of make_unique which is not yet included to C++11
165 namespace std_ {
166 template <class T>
167 struct _Unique_if {
168  using _Single_object = std::unique_ptr<T>;
169 };
170 
171 template <class T>
172 struct _Unique_if<T[]> { // NOLINT: modernize-avoid-c-arrays
173  using _Unknown_bound = std::unique_ptr<T[]>; // NOLINT: modernize-avoid-c-arrays
174 };
175 
176 template <class T, size_t N>
177 struct _Unique_if<T[N]> { // NOLINT: modernize-avoid-c-arrays
178  using _Known_bound = void;
179 };
180 
181 template <class T, class... Args>
182 typename _Unique_if<T>::_Single_object make_unique(Args &&... args) {
183  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
184 }
185 
186 template <class T>
187 typename _Unique_if<T>::_Unknown_bound make_unique(size_t n) {
188  using U = typename std::remove_extent<T>::type;
189  return std::unique_ptr<T>(new U[n]());
190 }
191 
192 template <class T, class... Args>
193 typename _Unique_if<T>::_Known_bound make_unique(Args &&...) = delete;
194 } // namespace std_
195 
196 #endif // UTILS_H_
CurlEasyWrapper
Definition: utils.h:146
ConnectionSocket
Definition: utils.h:124
Utils
Definition: utils.h:13
std_::_Unique_if
Definition: utils.h:167
TemporaryDirectory
Definition: utils.h:82
Socket
Definition: utils.h:105
ListenSocket
Definition: utils.h:136
TemporaryFile
RAII Temporary file creation.
Definition: utils.h:68