]> git.ipfire.org Git - thirdparty/squid.git/blame - src/ssl/context_storage.cc
Source Format Enforcement (#745)
[thirdparty/squid.git] / src / ssl / context_storage.cc
CommitLineData
bbc27441 1/*
77b1029d 2 * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
bbc27441
AJ
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
f7f3304a 9#include "squid.h"
7e10ac87 10#include "base/PackableStream.h"
602d9612
A
11#include "mgr/Registration.h"
12#include "ssl/context_storage.h"
95d2589c 13#include "Store.h"
074d6a40 14
95d2589c 15#include <limits>
24b30fdc
EQ
16#if USE_OPENSSL
17#include "compat/openssl.h"
cb4f4424 18#if HAVE_OPENSSL_SSL_H
a011edee
FC
19#include <openssl/ssl.h>
20#endif
24b30fdc 21#endif
95d2589c 22
eb898410 23Ssl::CertificateStorageAction::CertificateStorageAction(const Mgr::Command::Pointer &aCmd)
f53969cc 24 : Mgr::Action(aCmd)
95d2589c
CT
25{}
26
27Ssl::CertificateStorageAction::Pointer
eb898410 28Ssl::CertificateStorageAction::Create(const Mgr::Command::Pointer &aCmd)
95d2589c 29{
eb898410 30 return new CertificateStorageAction(aCmd);
95d2589c
CT
31}
32
33void Ssl::CertificateStorageAction::dump (StoreEntry *sentry)
34{
7e10ac87 35 PackableStream stream(*sentry);
95d2589c
CT
36 const char delimiter = '\t';
37 const char endString = '\n';
38 // Page title.
39 stream << "Cached ssl certificates statistic.\n";
40 // Title of statistic table.
41 stream << "Port" << delimiter << "Max mem(KB)" << delimiter << "Cert number" << delimiter << "KB/cert" << delimiter << "Mem used(KB)" << delimiter << "Mem free(KB)" << endString;
42
43 // Add info for each port.
d7ae3534 44 for (std::map<Ip::Address, LocalContextStorage *>::iterator i = TheGlobalContextStorage.storage.begin(); i != TheGlobalContextStorage.storage.end(); ++i) {
95d2589c
CT
45 stream << i->first << delimiter;
46 LocalContextStorage & ssl_store_policy(*(i->second));
72247610 47 const auto memoryPerEntry = ssl_store_policy.entries() ?
70ac5b29 48 ssl_store_policy.memoryUsed() / ssl_store_policy.entries() : 0;
14798e73
CT
49 stream << ssl_store_policy.memLimit() / 1024 << delimiter;
50 stream << ssl_store_policy.entries() << delimiter;
72247610
AJ
51 stream << memoryPerEntry / 1024 << delimiter;
52 stream << ssl_store_policy.memoryUsed() / 1024 << delimiter;
14798e73 53 stream << ssl_store_policy.freeMem() / 1024 << endString;
95d2589c
CT
54 }
55 stream << endString;
56 stream.flush();
57}
58
95d2589c
CT
59///////////////////////////////////////////////////////
60
61Ssl::GlobalContextStorage::GlobalContextStorage()
f53969cc 62 : reconfiguring(true)
95d2589c
CT
63{
64 RegisterAction("cached_ssl_cert", "Statistic of cached generated ssl certificates", &CertificateStorageAction::Create, 0, 1);
65}
66
67Ssl::GlobalContextStorage::~GlobalContextStorage()
68{
d7ae3534 69 for (std::map<Ip::Address, LocalContextStorage *>::iterator i = storage.begin(); i != storage.end(); ++i) {
95d2589c
CT
70 delete i->second;
71 }
72}
73
74void Ssl::GlobalContextStorage::addLocalStorage(Ip::Address const & address, size_t size_of_store)
75{
76 assert(reconfiguring);
77 configureStorage.insert(std::pair<Ip::Address, size_t>(address, size_of_store));
78}
79
9873e378 80Ssl::LocalContextStorage *Ssl::GlobalContextStorage::getLocalStorage(Ip::Address const & address)
95d2589c
CT
81{
82 reconfigureFinish();
83 std::map<Ip::Address, LocalContextStorage *>::iterator i = storage.find(address);
9873e378
CT
84
85 if (i == storage.end())
86 return NULL;
87 else
88 return i->second;
95d2589c
CT
89}
90
91void Ssl::GlobalContextStorage::reconfigureStart()
92{
9873e378 93 configureStorage.clear();
95d2589c
CT
94 reconfiguring = true;
95}
96
97void Ssl::GlobalContextStorage::reconfigureFinish()
98{
99 if (reconfiguring) {
100 reconfiguring = false;
101
102 // remove or change old local storages.
b02a2ff8 103 for (std::map<Ip::Address, LocalContextStorage *>::iterator i = storage.begin(); i != storage.end();) {
95d2589c 104 std::map<Ip::Address, size_t>::iterator conf_i = configureStorage.find(i->first);
9873e378 105 if (conf_i == configureStorage.end() || conf_i->second <= 0) {
ea886ed6 106 delete i->second;
b02a2ff8 107 storage.erase(i++);
95d2589c 108 } else {
14798e73 109 i->second->setMemLimit(conf_i->second);
b02a2ff8 110 ++i;
95d2589c
CT
111 }
112 }
113
114 // add new local storages.
a38ec4b1 115 for (std::map<Ip::Address, size_t>::iterator conf_i = configureStorage.begin(); conf_i != configureStorage.end(); ++conf_i ) {
9873e378 116 if (storage.find(conf_i->first) == storage.end() && conf_i->second > 0) {
72247610 117 storage.insert(std::pair<Ip::Address, LocalContextStorage *>(conf_i->first, new LocalContextStorage(conf_i->second)));
95d2589c
CT
118 }
119 }
120 }
121}
122
123Ssl::GlobalContextStorage Ssl::TheGlobalContextStorage;
f53969cc 124