libDwmCredence-0.1.32
DwmCredenceUtils.hh
Go to the documentation of this file.
1//===========================================================================
2// @(#) $DwmPath$
3//===========================================================================
4// Copyright (c) Daniel W. McRobb 2022, 2023
5// All rights reserved.
6//
7// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions
9// are met:
10//
11// 1. Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13// 2. Redistributions in binary form must reproduce the above copyright
14// notice, this list of conditions and the following disclaimer in the
15// documentation and/or other materials provided with the distribution.
16// 3. The names of the authors and copyright holders may not be used to
17// endorse or promote products derived from this software without
18// specific prior written permission.
19//
20// IN NO EVENT SHALL DANIEL W. MCROBB BE LIABLE TO ANY PARTY FOR
21// DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
22// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE,
23// EVEN IF DANIEL W. MCROBB HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
24// DAMAGE.
25//
26// THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND
27// DANIEL W. MCROBB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
28// UPDATES, ENHANCEMENTS, OR MODIFICATIONS. DANIEL W. MCROBB MAKES NO
29// REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER
30// IMPLIED OR EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31// WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE,
32// OR THAT THE USE OF THIS SOFTWARE WILL NOT INFRINGE ANY PATENT,
33// TRADEMARK OR OTHER RIGHTS.
34//===========================================================================
35
36//---------------------------------------------------------------------------
40//---------------------------------------------------------------------------
41
42#ifndef _DWMCREDENCEUTILS_HH_
43#define _DWMCREDENCEUTILS_HH_
44
45extern "C" {
46 #include <sodium.h>
47}
48
49#include <chrono>
50#include <cstdint>
51#include <string>
52#include <boost/asio.hpp>
53#include <boost/version.hpp>
54
55namespace Dwm {
56
57 namespace Credence {
58
59 //------------------------------------------------------------------------
61 //------------------------------------------------------------------------
62 class Utils
63 {
64 public:
65 using Clock = std::chrono::system_clock;
66 using TimePoint = std::chrono::time_point<Clock>;
67#if BOOST_VERSION >= 107400
68 using BoostTcpSocket =
69 boost::asio::basic_socket<boost::asio::ip::tcp,
70 boost::asio::any_io_executor>;
71 using BoostUnixSocket =
72 boost::asio::basic_socket<boost::asio::local::stream_protocol,
73 boost::asio::any_io_executor>;
74#else
75 using BoostTcpSocket =
76 boost::asio::basic_socket<boost::asio::ip::tcp,
77 boost::asio::executor>;
78 using BoostUnixSocket =
79 boost::asio::basic_socket<boost::asio::local::stream_protocol,
80 boost::asio::executor>;
81#endif
82
83 //----------------------------------------------------------------------
87 //----------------------------------------------------------------------
88 static ssize_t BytesReady(BoostTcpSocket & sck);
89 static ssize_t BytesReady(BoostUnixSocket & sck);
90
91 //----------------------------------------------------------------------
96 //----------------------------------------------------------------------
97 static bool WaitUntilBytesReady(BoostTcpSocket & sck,
98 uint32_t numBytes, TimePoint endTime);
99 static bool WaitUntilBytesReady(BoostUnixSocket & sck,
100 uint32_t numBytes, TimePoint endTime);
101
102 //----------------------------------------------------------------------
107 //----------------------------------------------------------------------
108 static bool WaitForBytesReady(BoostTcpSocket & sck, uint32_t numBytes,
109 std::chrono::milliseconds timeout);
110 static bool WaitForBytesReady(BoostUnixSocket & sck, uint32_t numBytes,
111 std::chrono::milliseconds timeout);
112
113 //----------------------------------------------------------------------
115 //----------------------------------------------------------------------
116 static ssize_t
117 ReadLengthRestrictedString(boost::asio::ip::tcp::socket & sck,
118 std::string & s, uint64_t maxLen);
119 static ssize_t
120 ReadLengthRestrictedString(boost::asio::local::stream_protocol::socket & sck,
121 std::string & s, uint64_t maxLen);
122
123 //----------------------------------------------------------------------
125 //----------------------------------------------------------------------
126 static std::istream & ReadLengthRestrictedString(std::istream & is,
127 std::string & s,
128 uint64_t maxLen);
129
130 //----------------------------------------------------------------------
132 //----------------------------------------------------------------------
133 static ssize_t
134 ReadLengthRestrictedString(boost::asio::ip::tcp::socket & sck,
135 std::string & s, uint64_t maxLen,
136 TimePoint endTime);
137 static ssize_t
138 ReadLengthRestrictedString(boost::asio::local::stream_protocol::socket & sck,
139 std::string & s, uint64_t maxLen,
140 TimePoint endTime);
141
142 //----------------------------------------------------------------------
144 //----------------------------------------------------------------------
145 static std::string Bin2Base64(const std::string & s);
146
147 //----------------------------------------------------------------------
150 //----------------------------------------------------------------------
151 static std::string Base642Bin(const std::string & s);
152
153 //----------------------------------------------------------------------
155 //----------------------------------------------------------------------
156 static std::string UserHomeDirectory();
157
158 //----------------------------------------------------------------------
160 //----------------------------------------------------------------------
161 static std::string UserName();
162
163 //----------------------------------------------------------------------
165 //----------------------------------------------------------------------
166 static std::string HostName();
167
168 static bool ScalarMult(const std::string & sk, const std::string & pk,
169 std::string & q);
170
171 //----------------------------------------------------------------------
174 //----------------------------------------------------------------------
175 static std::string
176 EndPointString(const boost::asio::ip::tcp::endpoint & endPoint);
177 };
178
179 } // namespace Credence
180
181} // namespace Dwm
182
183#endif // _DWMCREDENCEUTILS_HH_