1 //-------------------------------------------------------------------------- 2 // Copyright (C) 2014-2021 Cisco and/or its affiliates. All rights reserved. 3 // Copyright (C) 2002-2013 Sourcefire, Inc. 4 // Copyright (C) 2002 Martin Roesch <roesch@sourcefire.com> 5 // 6 // This program is free software; you can redistribute it and/or modify it 7 // under the terms of the GNU General Public License Version 2 as published 8 // by the Free Software Foundation. You may not use, modify or distribute 9 // this program under any other version of the GNU General Public 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 // General Public License for more details. 15 // 16 // You should have received a copy of the GNU General Public License along 17 // with this program; if not, write to the Free Software Foundation, Inc., 18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 19 //-------------------------------------------------------------------------- 20 21 #ifndef MESSAGES_H 22 #define MESSAGES_H 23 24 #include <arpa/inet.h> 25 #include <cstdio> 26 #include <string> 27 #include <ctime> 28 29 #include "main/snort_types.h" 30 31 #define LOG_DIV "--------------------------------------------------" 32 33 #ifndef __GNUC__ 34 #define __attribute__(x) /*NOTHING*/ 35 #endif 36 37 #define STD_BUF 1024 38 39 enum WarningGroup 40 { 41 WARN_DAQ, WARN_CONF, WARN_CONF_STRICT, WARN_VARS, 42 WARN_SYMBOLS, WARN_SCRIPTS, WARN_HOSTS, WARN_RULES, 43 WARN_FLOWBITS, WARN_PLUGINS, 44 #ifdef PIGLET 45 WARN_PIGLET, 46 #endif 47 WARN_MAX 48 }; 49 50 void reset_parse_errors(); 51 unsigned get_parse_errors(); 52 unsigned get_parse_warnings(); 53 void reset_reload_errors(); 54 unsigned get_reload_errors(); 55 std::string& get_reload_errors_description(); 56 57 namespace snort 58 { 59 SO_PUBLIC void ParseWarning(WarningGroup, const char*, ...) __attribute__((format (printf, 2, 3))); 60 SO_PUBLIC void ParseError(const char*, ...) __attribute__((format (printf, 1, 2))); 61 SO_PUBLIC void ReloadError(const char*, ...) __attribute__((format (printf, 1, 2))); 62 [[noreturn]] SO_PUBLIC void ParseAbort(const char*, ...) __attribute__((format (printf, 1, 2))); 63 64 SO_PUBLIC void LogMessage(const char*, ...) __attribute__((format (printf, 1, 2))); 65 SO_PUBLIC void LogMessage(FILE*, const char*, ...) __attribute__((format (printf, 2, 3))); 66 SO_PUBLIC void WarningMessage(const char*, ...) __attribute__((format (printf, 1, 2))); 67 SO_PUBLIC void ErrorMessage(const char*, ...) __attribute__((format (printf, 1, 2))); 68 69 class SO_PUBLIC ConfigLogger final 70 { 71 public: 72 ConfigLogger() = delete; 73 static void log_option(const char* caption); 74 static bool log_flag(const char* caption, bool flag, bool subopt = false); 75 static void log_limit(const char* caption, int val, int unlim, bool subopt = false); 76 static void log_limit(const char* caption, int val, int unlim, int disable, bool subopt = false); 77 static void log_limit(const char* caption, int64_t val, int64_t unlim, bool subopt = false); 78 static void log_value(const char* caption, int n, const char* descr, bool subopt = false); 79 static void log_value(const char* caption, int32_t n, bool subopt = false); 80 static void log_value(const char* caption, uint32_t n, bool subopt = false); 81 static void log_value(const char* caption, int64_t n, bool subopt = false); 82 static void log_value(const char* caption, uint64_t n, bool subopt = false); 83 static void log_value(const char* caption, double n, bool subopt = false); 84 static void log_value(const char* caption, const char* str, bool subopt = false); 85 static void log_list(const char* caption, const char* list, const char* prefix = " ", bool subopt = false); 86 static void log_list(const char* list); 87 private: 88 static constexpr int indention = 25; 89 static constexpr int max_line_len = 75; 90 }; 91 92 // FIXIT-RC do not call FatalError() during runtime 93 [[noreturn]] SO_PUBLIC void FatalError(const char*, ...) __attribute__((format (printf, 1, 2))); 94 95 NORETURN_ASSERT void log_safec_error(const char*, void*, int); 96 97 class Dumper 98 { 99 public: 100 Dumper(const char* s = nullptr, unsigned n = 3) 101 { 102 max = n; idx = 0; 103 if ( s ) 104 LogMessage("%s\n", s); 105 } 106 ~Dumper()107 ~Dumper() 108 { 109 if ( idx % max ) 110 LogMessage("\n"); 111 } 112 113 void dump(const char* s, unsigned v = 0) 114 { 115 const char* eol = !(++idx % max) ? "\n" : ""; 116 LogMessage(" %18.18s(v%u)%s", s, v, eol); 117 } 118 dump(const char * s,const char * t)119 void dump(const char* s, const char* t) 120 { 121 LogMessage("%s::%s\n", s, t); 122 } 123 124 private: 125 unsigned max; 126 unsigned idx; 127 }; 128 } 129 130 #endif 131 132