OpenVPN 3 Core Library
Loading...
Searching...
No Matches
logperiod.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12#ifndef OPENVPN_LOG_LOGPERIOD_H
13#define OPENVPN_LOG_LOGPERIOD_H
14
15#include <sys/time.h> // for time_t
16
17#include <string>
18#include <sstream>
19#include <iomanip> // for setfill and setw
20
23#include <openvpn/time/time.hpp>
25
26namespace openvpn {
28{
29 public:
30 OPENVPN_EXCEPTION(log_period_error);
31
39
41 : start_(0),
42 end_(0),
44 {
45 }
46
47 LogPeriod(const Period period, const time_t base)
48 {
49 period_ = period;
50 const olong p = period_sec(period_);
51 start_ = period_base(period_, base);
52 end_ = start_ + p;
53 }
54
55 LogPeriod(const LogPeriod &other, const int index)
56 {
57 period_ = other.period_;
58 const olong p = period_sec(period_);
59 start_ = other.start_ + p * olong(index);
60 end_ = start_ + p;
61 }
62
63 bool is_current(const time_t now) const
64 {
65 const olong onow = olong(now);
66 return onow >= start_ && onow < end_;
67 }
68
69 bool defined() const
70 {
71 return period_ != UNDEF;
72 }
73
74 time_t expires_in(const time_t now)
75 {
76 const olong onow = olong(now);
77 if (onow < end_)
78 return end_ - onow;
79 else
80 return 0;
81 }
82
83 std::string to_string_verbose() const
84 {
85 return date_time(start_) + " -> " + date_time(end_);
86 }
87
88 std::string to_string() const
89 {
90 std::ostringstream os;
91 struct tm lt;
92 const time_t time = time_t(start_);
93 if (!localtime_r(&time, &lt))
94 throw log_period_error("to_string localtime_r");
95 os << std::setfill('0');
96 os << std::setw(4) << (lt.tm_year + 1900) << '.' << std::setw(2) << (lt.tm_mon + 1) << '.' << std::setw(2) << lt.tm_mday;
97 if (period_ == HOURLY || period_ == BY_MINUTE)
98 os << '-' << std::setw(2) << lt.tm_hour << ':' << std::setw(2) << lt.tm_min;
99 return os.str();
100 }
101
102 static Period period_from_string(const std::string &str)
103 {
104 if (str == "daily")
105 return DAILY;
106 else if (str == "hourly")
107 return HOURLY;
108 else if (str == "by_minute")
109 return BY_MINUTE;
110 else
111 throw log_period_error("unknown period: " + str);
112 }
113
114 private:
115 static olong period_sec(const Period p)
116 {
117 switch (p)
118 {
119 case DAILY:
120 return 86400;
121 case HOURLY:
122 return 3600;
123 case BY_MINUTE:
124 return 60;
125 default:
126 throw log_period_error("undefined period");
127 }
128 }
129
130 static olong period_base(const Period p, const time_t time)
131 {
132 struct tm lt;
133 if (!localtime_r(&time, &lt))
134 throw log_period_error("period_base localtime_r");
135 switch (p)
136 {
137 case DAILY:
138 lt.tm_hour = 0;
139 lt.tm_min = 0;
140 lt.tm_sec = 0;
141 break;
142 case HOURLY:
143 lt.tm_min = 0;
144 lt.tm_sec = 0;
145 break;
146 case BY_MINUTE:
147 lt.tm_sec = 0;
148 break;
149 default:
150 throw log_period_error("unknown period");
151 }
152 const time_t ret = mktime(&lt);
153 if (ret == -1)
154 throw log_period_error("mktime");
155 return olong(ret);
156 }
157
158 private:
162};
163} // namespace openvpn
164
165#endif
LogPeriod(const Period period, const time_t base)
Definition logperiod.hpp:47
LogPeriod(const LogPeriod &other, const int index)
Definition logperiod.hpp:55
static olong period_sec(const Period p)
OPENVPN_EXCEPTION(log_period_error)
bool defined() const
Definition logperiod.hpp:69
std::string to_string() const
Definition logperiod.hpp:88
static Period period_from_string(const std::string &str)
std::string to_string_verbose() const
Definition logperiod.hpp:83
static olong period_base(const Period p, const time_t time)
time_t expires_in(const time_t now)
Definition logperiod.hpp:74
bool is_current(const time_t now) const
Definition logperiod.hpp:63
long olong
Definition olong.hpp:23
std::string date_time()
Definition timestr.hpp:139
os<< "Session Name: "<< tbc-> session_name<< '\n';os<< "Layer: "<< tbc-> layer str()<< '\n'
std::string ret
std::ostringstream os