1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2019-2019 Bareos GmbH & Co. KG
5 
6    This program is Free Software; you can redistribute it and/or
7    modify it under the terms of version three of the GNU Affero General Public
8    License as published by the Free Software Foundation and included
9    in the file LICENSE.
10 
11    This program is distributed in the hope that it will be useful, but
12    WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14    Affero General Public License for more details.
15 
16    You should have received a copy of the GNU Affero General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19    02110-1301, USA.
20 */
21 
22 #ifndef BAREOS_LIB_BNET_NETWORK_DUMP_PRIVATE_H_
23 #define BAREOS_LIB_BNET_NETWORK_DUMP_PRIVATE_H_
24 
25 #include "lib/bareos_resource.h"
26 
27 #include <string>
28 #include <memory>
29 #include <set>
30 #include <fstream>
31 
32 class QualifiedResourceNameTypeConverter;
33 class BStringList;
34 
35 class BnetDumpPrivate {
36  public:
37   BnetDumpPrivate() = default;
38   ~BnetDumpPrivate() = default;
39 
40   static bool SetFilename(const char* filename);
41 
42   void DumpToFile(const char* ptr, int nbytes);
43   void SaveAndSendMessageIfNoDestinationDefined(const char* ptr, int nbytes);
44   void OpenFile();
45   void CloseFile();
46 
47   static std::string filename_;
48   static bool plantuml_mode_;
49   static int stack_level_amount_;
50 
51   std::string own_qualified_name_;
52   std::string destination_qualified_name_;
53 
54   std::ofstream output_file_;
55 
56  private:
57   void CreateAndWriteStacktraceToBuffer();
58   void CreateAndWriteMessageToBuffer(const char* ptr, int nbytes);
59   std::string CreateDataString(int signal, const char* ptr, int nbytes) const;
60   std::string CreateFormatStringForNetworkMessage(int signal) const;
61   bool SuppressMessageIfRcodeIsInExcludeList() const;
62   bool IsExcludedRcode(const BStringList& l) const;
63 
64   static int stack_level_start_;
65   static std::size_t max_data_dump_bytes_;
66   static std::set<std::string> exclude_rcodes_;
67 
68   std::string output_buffer_;
69 
70   std::vector<std::vector<char>> temporary_buffer_for_initial_messages_;
71 
72   enum class State
73   {
74     kWaitForDestinationName,
75     kRunNormal
76   };
77   State state_ = State::kWaitForDestinationName;
78 };
79 
80 #endif  // BAREOS_LIB_BNET_NETWORK_DUMP_PRIVATE_H_
81