diff --git a/auxil/bifcl b/auxil/bifcl index 2affd6a4fd..1607cc50dd 160000 --- a/auxil/bifcl +++ b/auxil/bifcl @@ -1 +1 @@ -Subproject commit 2affd6a4fd7b2543a533c481334828d3a47890d6 +Subproject commit 1607cc50ddde62ac13c0f1d6539a96bb7182ca4e diff --git a/src/Anon.cc b/src/Anon.cc index d86df56759..d2365e46ca 100644 --- a/src/Anon.cc +++ b/src/Anon.cc @@ -20,7 +20,7 @@ AnonymizeIPAddr* zeek::detail::ip_anonymizer[NUM_ADDR_ANONYMIZATION_METHODS] = { static uint32_t rand32() { - return ((zeek::util::random_number() & 0xffff) << 16) | (zeek::util::random_number() & 0xffff); + return ((zeek::util::detail::random_number() & 0xffff) << 16) | (zeek::util::detail::random_number() & 0xffff); } // From tcpdpriv. @@ -104,7 +104,7 @@ ipaddr32_t AnonymizeIPAddr_RandomMD5::anonymize(ipaddr32_t input) uint8_t digest[16]; ipaddr32_t output = 0; - zeek::util::hmac_md5(sizeof(input), (u_char*)(&input), digest); + zeek::util::detail::hmac_md5(sizeof(input), (u_char*)(&input), digest); for ( int i = 0; i < 4; ++i ) output = (output << 8) | digest[i]; @@ -132,7 +132,7 @@ ipaddr32_t AnonymizeIPAddr_PrefixMD5::anonymize(ipaddr32_t input) prefix.prefix = htonl((input & ~(prefix_mask>>i)) | (1<<(31-i))); // HK(PAD(x_0 ... x_{i-1})). - zeek::util::hmac_md5(sizeof(prefix), (u_char*) &prefix, digest); + zeek::util::detail::hmac_md5(sizeof(prefix), (u_char*) &prefix, digest); // f_{i-1} = LSB(HK(PAD(x_0 ... x_{i-1}))). ipaddr32_t bit_mask = (digest[0] & 1) << (31-i); diff --git a/src/Conn.cc b/src/Conn.cc index deb32b1097..4c97420bdb 100644 --- a/src/Conn.cc +++ b/src/Conn.cc @@ -512,13 +512,13 @@ void Connection::ConnectionEvent(zeek::EventHandlerPtr f, zeek::analyzer::Analyz return; // "this" is passed as a cookie for the event - zeek::event_mgr.Enqueue(f, std::move(args), SOURCE_LOCAL, a ? a->GetID() : 0, this); + zeek::event_mgr.Enqueue(f, std::move(args), zeek::util::detail::SOURCE_LOCAL, a ? a->GetID() : 0, this); } void Connection::ConnectionEventFast(zeek::EventHandlerPtr f, zeek::analyzer::Analyzer* a, val_list vl) { // "this" is passed as a cookie for the event - zeek::event_mgr.Enqueue(f, zeek::val_list_to_args(vl), SOURCE_LOCAL, + zeek::event_mgr.Enqueue(f, zeek::val_list_to_args(vl), zeek::util::detail::SOURCE_LOCAL, a ? a->GetID() : 0, this); } @@ -535,7 +535,7 @@ void Connection::EnqueueEvent(zeek::EventHandlerPtr f, zeek::analyzer::Analyzer* zeek::Args args) { // "this" is passed as a cookie for the event - zeek::event_mgr.Enqueue(f, std::move(args), zeek::util::SOURCE_LOCAL, a ? a->GetID() : 0, this); + zeek::event_mgr.Enqueue(f, std::move(args), zeek::util::detail::SOURCE_LOCAL, a ? a->GetID() : 0, this); } void Connection::Weird(const char* name, const char* addl) diff --git a/src/DebugLogger.cc b/src/DebugLogger.cc index c89adb19bb..1924ccc372 100644 --- a/src/DebugLogger.cc +++ b/src/DebugLogger.cc @@ -49,7 +49,7 @@ void DebugLogger::OpenDebugLog(const char* filename) { if ( filename ) { - filename = zeek::util::log_file_name(filename); + filename = zeek::util::detail::log_file_name(filename); file = fopen(filename, "w"); if ( ! file ) diff --git a/src/Dict.cc b/src/Dict.cc index 626967beee..c22e5504d2 100644 --- a/src/Dict.cc +++ b/src/Dict.cc @@ -410,14 +410,14 @@ size_t Dictionary::MemoryAllocation() const size_t size = padded_sizeof(*this); if ( table ) { - size += pad_size(Capacity() * sizeof(detail::DictEntry)); + size += zeek::util::pad_size(Capacity() * sizeof(detail::DictEntry)); for ( int i = Capacity()-1; i>=0; i-- ) if ( ! table[i].Empty() && table[i].key_size > 8 ) - size += pad_size(table[i].key_size); + size += zeek::util::pad_size(table[i].key_size); } if ( order ) - size += padded_sizeof(std::vector) + pad_size(sizeof(detail::DictEntry) * order->capacity()); + size += padded_sizeof(std::vector) + zeek::util::pad_size(sizeof(detail::DictEntry) * order->capacity()); return size; } @@ -497,7 +497,7 @@ void Dictionary::Dump(int level) const { if ( table[i].Empty() ) continue; - key_size += pad_size(table[i].key_size); + key_size += zeek::util::pad_size(table[i].key_size); if ( ! table[i].value ) continue; } diff --git a/src/Event.cc b/src/Event.cc index 1a1ef98acc..46852ba650 100644 --- a/src/Event.cc +++ b/src/Event.cc @@ -19,7 +19,8 @@ zeek::EventMgr& mgr = zeek::event_mgr; namespace zeek { Event::Event(EventHandlerPtr arg_handler, zeek::Args arg_args, - zeek::util::SourceID arg_src, zeek::analyzer::ID arg_aid, Obj* arg_obj) + zeek::util::detail::SourceID arg_src, zeek::analyzer::ID arg_aid, + Obj* arg_obj) : handler(arg_handler), args(std::move(arg_args)), src(arg_src), @@ -48,7 +49,7 @@ void Event::Describe(ODesc* d) const void Event::Dispatch(bool no_remote) { - if ( src == zeek::util::SOURCE_BROKER ) + if ( src == zeek::util::detail::SOURCE_BROKER ) no_remote = true; if ( handler->ErrorHandler() ) @@ -75,7 +76,7 @@ void Event::Dispatch(bool no_remote) EventMgr::EventMgr() { head = tail = nullptr; - current_src = zeek::util::SOURCE_LOCAL; + current_src = zeek::util::detail::SOURCE_LOCAL; current_aid = 0; src_val = nullptr; draining = false; @@ -94,14 +95,14 @@ EventMgr::~EventMgr() } void EventMgr::QueueEventFast(const EventHandlerPtr &h, val_list vl, - SourceID src, analyzer::ID aid, zeek::detail::TimerMgr* mgr, + zeek::util::detail::SourceID src, analyzer::ID aid, zeek::detail::TimerMgr* mgr, Obj* obj) { QueueEvent(new Event(h, zeek::val_list_to_args(vl), src, aid, obj)); } void EventMgr::QueueEvent(const EventHandlerPtr &h, val_list vl, - SourceID src, analyzer::ID aid, + zeek::util::detail::SourceID src, analyzer::ID aid, zeek::detail::TimerMgr* mgr, Obj* obj) { auto args = zeek::val_list_to_args(vl); @@ -111,7 +112,7 @@ void EventMgr::QueueEvent(const EventHandlerPtr &h, val_list vl, } void EventMgr::QueueEvent(const EventHandlerPtr &h, val_list* vl, - SourceID src, analyzer::ID aid, + zeek::util::detail::SourceID src, analyzer::ID aid, zeek::detail::TimerMgr* mgr, Obj* obj) { auto args = zeek::val_list_to_args(*vl); @@ -122,7 +123,8 @@ void EventMgr::QueueEvent(const EventHandlerPtr &h, val_list* vl, } void EventMgr::Enqueue(const EventHandlerPtr& h, zeek::Args vl, - zeek::util::SourceID src, zeek::analyzer::ID aid, Obj* obj) + zeek::util::detail::SourceID src, + zeek::analyzer::ID aid, Obj* obj) { QueueEvent(new Event(h, std::move(vl), src, aid, obj)); } diff --git a/src/Event.h b/src/Event.h index 392c2f6147..ce28cc63ce 100644 --- a/src/Event.h +++ b/src/Event.h @@ -19,13 +19,13 @@ namespace zeek { class Event final : public zeek::Obj { public: Event(EventHandlerPtr handler, zeek::Args args, - zeek::util::SourceID src = zeek::util::SOURCE_LOCAL, zeek::analyzer::ID aid = 0, + zeek::util::detail::SourceID src = zeek::util::detail::SOURCE_LOCAL, zeek::analyzer::ID aid = 0, zeek::Obj* obj = nullptr); void SetNext(Event* n) { next_event = n; } Event* NextEvent() const { return next_event; } - zeek::util::SourceID Source() const { return src; } + zeek::util::detail::SourceID Source() const { return src; } zeek::analyzer::ID Analyzer() const { return aid; } EventHandlerPtr Handler() const { return handler; } const zeek::Args& Args() const { return args; } @@ -41,7 +41,7 @@ protected: EventHandlerPtr handler; zeek::Args args; - zeek::util::SourceID src; + zeek::util::detail::SourceID src; zeek::analyzer::ID aid; zeek::Obj* obj; Event* next_event; @@ -62,7 +62,7 @@ public: // arguments when there's no handlers to consume them). [[deprecated("Remove in v4.1. Use Enqueue() instead.")]] void QueueEventFast(const EventHandlerPtr &h, val_list vl, - zeek::util::SourceID src = zeek::util::SOURCE_LOCAL, zeek::analyzer::ID aid = 0, + zeek::util::detail::SourceID src = zeek::util::detail::SOURCE_LOCAL, zeek::analyzer::ID aid = 0, zeek::detail::TimerMgr* mgr = nullptr, zeek::Obj* obj = nullptr); // Queues an event if there's an event handler (or remote consumer). This @@ -73,7 +73,7 @@ public: // existence check. [[deprecated("Remove in v4.1. Use Enqueue() instead.")]] void QueueEvent(const EventHandlerPtr &h, val_list vl, - zeek::util::SourceID src = zeek::util::SOURCE_LOCAL, zeek::analyzer::ID aid = 0, + zeek::util::detail::SourceID src = zeek::util::detail::SOURCE_LOCAL, zeek::analyzer::ID aid = 0, zeek::detail::TimerMgr* mgr = nullptr, zeek::Obj* obj = nullptr); // Same as QueueEvent, except taking the event's argument list via a @@ -82,7 +82,7 @@ public: // each of its elements. [[deprecated("Remove in v4.1. Use Enqueue() instead.")]] void QueueEvent(const EventHandlerPtr &h, val_list* vl, - zeek::util::SourceID src = zeek::util::SOURCE_LOCAL, zeek::analyzer::ID aid = 0, + zeek::util::detail::SourceID src = zeek::util::detail::SOURCE_LOCAL, zeek::analyzer::ID aid = 0, zeek::detail::TimerMgr* mgr = nullptr, zeek::Obj* obj = nullptr); /** @@ -98,7 +98,7 @@ public: * reference to until dispatching the event. */ void Enqueue(const EventHandlerPtr& h, zeek::Args vl, - zeek::util::SourceID src = zeek::util::SOURCE_LOCAL, zeek::analyzer::ID aid = 0, + zeek::util::detail::SourceID src = zeek::util::detail::SOURCE_LOCAL, zeek::analyzer::ID aid = 0, zeek::Obj* obj = nullptr); /** @@ -119,7 +119,7 @@ public: bool HasEvents() const { return head != nullptr; } // Returns the source ID of last raised event. - zeek::util::SourceID CurrentSource() const { return current_src; } + zeek::util::detail::SourceID CurrentSource() const { return current_src; } // Returns the ID of the analyzer which raised the last event, or 0 if // non-analyzer event. @@ -143,7 +143,7 @@ protected: Event* head; Event* tail; - zeek::util::SourceID current_src; + zeek::util::detail::SourceID current_src; zeek::analyzer::ID current_aid; zeek::RecordVal* src_val; bool draining; diff --git a/src/File.cc b/src/File.cc index 4d675863d8..d015d50ed9 100644 --- a/src/File.cc +++ b/src/File.cc @@ -282,7 +282,7 @@ zeek::RecordVal* File::Rotate() static auto rotate_info = zeek::id::find_type("rotate_info"); auto* info = new zeek::RecordVal(rotate_info); - FILE* newf = zeek::util::rotate_file(name, info); + FILE* newf = zeek::util::detail::rotate_file(name, info); if ( ! newf ) { diff --git a/src/Hash.h b/src/Hash.h index 2ac2b422ff..b2b237a420 100644 --- a/src/Hash.h +++ b/src/Hash.h @@ -199,7 +199,7 @@ private: inline static uint8_t shared_hmac_md5_key[16]; inline static bool seeds_initialized = false; - friend void zeek::util::hmac_md5(size_t size, const unsigned char* bytes, unsigned char digest[16]); + friend void zeek::util::detail::hmac_md5(size_t size, const unsigned char* bytes, unsigned char digest[16]); friend BifReturnVal zeek::BifFunc::md5_hmac_bif(zeek::detail::Frame* frame, const zeek::Args*); }; diff --git a/src/ID.cc b/src/ID.cc index f924fe04ea..46dcd50efd 100644 --- a/src/ID.cc +++ b/src/ID.cc @@ -222,8 +222,8 @@ void ID::SetVal(ExprPtr ev, InitClass c) if ( ! val ) { - Error(fmt("%s initializer applied to ID without value", - c == INIT_EXTRA ? "+=" : "-="), this); + Error(zeek::util::fmt("%s initializer applied to ID without value", + c == INIT_EXTRA ? "+=" : "-="), this); return; } diff --git a/src/Net.cc b/src/Net.cc index 108bdaa4f8..186117cd9d 100644 --- a/src/Net.cc +++ b/src/Net.cc @@ -234,7 +234,7 @@ void net_packet_dispatch(double t, const zeek::Packet* pkt, zeek::iosource::PktS if ( load_freq == 0 ) load_freq = uint32_t(0xffffffff) / uint32_t(load_sample_freq); - if ( uint32_t(zeek::util::random_number() & 0xffffffff) < load_freq ) + if ( uint32_t(zeek::util::detail::random_number() & 0xffffffff) < load_freq ) { // Drain the queued timer events so they're not // charged against this sample. @@ -263,7 +263,7 @@ void net_packet_dispatch(double t, const zeek::Packet* pkt, zeek::iosource::PktS void net_run() { - zeek::util::set_processing_status("RUNNING", "net_run"); + zeek::util::detail::set_processing_status("RUNNING", "net_run"); std::vector ready; ready.reserve(zeek::iosource_mgr->TotalSize()); @@ -364,7 +364,7 @@ void net_get_final_stats() void net_finish(int drain_events) { - zeek::util::set_processing_status("TERMINATING", "net_finish"); + zeek::util::detail::set_processing_status("TERMINATING", "net_finish"); if ( drain_events ) { @@ -389,7 +389,7 @@ void net_finish(int drain_events) void net_delete() { - zeek::util::set_processing_status("TERMINATING", "net_delete"); + zeek::util::detail::set_processing_status("TERMINATING", "net_delete"); delete zeek::sessions; diff --git a/src/Options.cc b/src/Options.cc index 9c1f22d4db..c35b9753bb 100644 --- a/src/Options.cc +++ b/src/Options.cc @@ -431,7 +431,7 @@ zeek::Options zeek::parse_cmdline(int argc, char** argv) if ( path->empty() ) return; - *path = zeek::util::normalize_path(*path); + *path = zeek::util::detail::normalize_path(*path); if ( (*path)[0] == '/' || (*path)[0] == '~' ) // Absolute path diff --git a/src/PacketFilter.cc b/src/PacketFilter.cc index bc7ef14db0..3e00f43d02 100644 --- a/src/PacketFilter.cc +++ b/src/PacketFilter.cc @@ -20,7 +20,7 @@ void PacketFilter::AddSrc(const zeek::IPAddr& src, uint32_t tcp_flags, double pr { Filter* f = new Filter; f->tcp_flags = tcp_flags; - f->probability = probability * static_cast(zeek::util::max_random()); + f->probability = probability * static_cast(zeek::util::detail::max_random()); auto prev = static_cast(src_filter.Insert(src, 128, f)); delete prev; } @@ -29,7 +29,7 @@ void PacketFilter::AddSrc(zeek::Val* src, uint32_t tcp_flags, double probability { Filter* f = new Filter; f->tcp_flags = tcp_flags; - f->probability = probability * static_cast(zeek::util::max_random()); + f->probability = probability * static_cast(zeek::util::detail::max_random()); auto prev = static_cast(src_filter.Insert(src, f)); delete prev; } @@ -38,7 +38,7 @@ void PacketFilter::AddDst(const zeek::IPAddr& dst, uint32_t tcp_flags, double pr { Filter* f = new Filter; f->tcp_flags = tcp_flags; - f->probability = probability * static_cast(zeek::util::max_random()); + f->probability = probability * static_cast(zeek::util::detail::max_random()); auto prev = static_cast(dst_filter.Insert(dst, 128, f)); delete prev; } @@ -47,7 +47,7 @@ void PacketFilter::AddDst(zeek::Val* dst, uint32_t tcp_flags, double probability { Filter* f = new Filter; f->tcp_flags = tcp_flags; - f->probability = probability * static_cast(zeek::util::max_random()); + f->probability = probability * static_cast(zeek::util::detail::max_random()); auto prev = static_cast(dst_filter.Insert(dst, f)); delete prev; } @@ -115,7 +115,7 @@ bool PacketFilter::MatchFilter(const Filter& f, const zeek::IP_Hdr& ip, return false; } - return zeek::util::random_number() < f.probability; + return zeek::util::detail::random_number() < f.probability; } } // namespace zeek::detail diff --git a/src/Reporter.cc b/src/Reporter.cc index 5306df72a0..9d5dc9f7b2 100644 --- a/src/Reporter.cc +++ b/src/Reporter.cc @@ -126,7 +126,7 @@ void Reporter::FatalError(const char* fmt, ...) va_end(ap); - zeek::util::set_processing_status("TERMINATED", "fatal_error"); + zeek::util::detail::set_processing_status("TERMINATED", "fatal_error"); fflush(stderr); fflush(stdout); _exit(1); @@ -142,7 +142,7 @@ void Reporter::FatalErrorWithCore(const char* fmt, ...) va_end(ap); - zeek::util::set_processing_status("TERMINATED", "fatal_error"); + zeek::util::detail::set_processing_status("TERMINATED", "fatal_error"); abort(); } @@ -195,7 +195,7 @@ void Reporter::InternalError(const char* fmt, ...) va_end(ap); - zeek::util::set_processing_status("TERMINATED", "internal_error"); + zeek::util::detail::set_processing_status("TERMINATED", "internal_error"); abort(); } diff --git a/src/ScriptCoverageManager.cc b/src/ScriptCoverageManager.cc index bd15556944..c153ac546f 100644 --- a/src/ScriptCoverageManager.cc +++ b/src/ScriptCoverageManager.cc @@ -96,7 +96,7 @@ bool ScriptCoverageManager::WriteStats() zeek::util::SafeDirname dirname{bf}; - if ( ! zeek::util::ensure_intermediate_dirs(dirname.result.data()) ) + if ( ! zeek::util::detail::ensure_intermediate_dirs(dirname.result.data()) ) { zeek::reporter->Error("Failed to open ZEEK_PROFILER_FILE destination '%s' for writing", bf); return false; diff --git a/src/Stmt.cc b/src/Stmt.cc index af792b4491..4c5fe7555c 100644 --- a/src/Stmt.cc +++ b/src/Stmt.cc @@ -143,7 +143,7 @@ void Stmt::AccessStats(ODesc* d) const if ( d->IncludeStats() ) { d->Add("(@"); - d->Add(last_access ? zeek::util::fmt_access_time(last_access) : ""); + d->Add(last_access ? zeek::util::detail::fmt_access_time(last_access) : ""); d->Add(" #"); d->Add(access_count); d->Add(")"); diff --git a/src/Val.cc b/src/Val.cc index f54b3eb06e..4925f84b32 100644 --- a/src/Val.cc +++ b/src/Val.cc @@ -2445,7 +2445,7 @@ void TableVal::Describe(ODesc* d) const if ( d->IsReadable() && ! d->IsShort() && d->IncludeStats() ) { d->Add(" @"); - d->Add(zeek::util::fmt_access_time(v->ExpireAccessTime())); + d->Add(zeek::util::detail::fmt_access_time(v->ExpireAccessTime())); } } diff --git a/src/broker/Manager.cc b/src/broker/Manager.cc index cb60344ec0..7019c6d013 100644 --- a/src/broker/Manager.cc +++ b/src/broker/Manager.cc @@ -1216,7 +1216,7 @@ void Manager::ProcessEvent(const broker::topic& topic, broker::zeek::Event ev) } if ( vl.size() == args.size() ) - zeek::event_mgr.Enqueue(handler, std::move(vl), zeek::util::SOURCE_BROKER); + zeek::event_mgr.Enqueue(handler, std::move(vl), zeek::util::detail::SOURCE_BROKER); } bool Manager::ProcessLogCreate(broker::zeek::LogCreate lc) diff --git a/src/input/Manager.cc b/src/input/Manager.cc index e1421332db..2089da05b3 100644 --- a/src/input/Manager.cc +++ b/src/input/Manager.cc @@ -1828,7 +1828,7 @@ void Manager::SendEvent(zeek::EventHandlerPtr ev, const int numvals, ...) const va_end(lP); if ( ev ) - zeek::event_mgr.Enqueue(ev, std::move(vl), zeek::util::SOURCE_LOCAL); + zeek::event_mgr.Enqueue(ev, std::move(vl), zeek::util::detail::SOURCE_LOCAL); } void Manager::SendEvent(zeek::EventHandlerPtr ev, list events) const @@ -1845,7 +1845,7 @@ void Manager::SendEvent(zeek::EventHandlerPtr ev, list events) const vl.emplace_back(zeek::AdoptRef{}, *i); if ( ev ) - zeek::event_mgr.Enqueue(ev, std::move(vl), zeek::util::SOURCE_LOCAL); + zeek::event_mgr.Enqueue(ev, std::move(vl), zeek::util::detail::SOURCE_LOCAL); } // Convert a bro list value to a bro record value. diff --git a/src/logging/Manager.cc b/src/logging/Manager.cc index f2463860b2..f71006afe8 100644 --- a/src/logging/Manager.cc +++ b/src/logging/Manager.cc @@ -1215,7 +1215,7 @@ WriterFrontend* Manager::CreateWriter(zeek::EnumVal* id, zeek::EnumVal* writer, static auto base_time = log_rotate_base_time->AsString()->CheckString(); winfo->info->rotation_interval = winfo->interval; - winfo->info->rotation_base = zeek::util::parse_rotate_base_time(base_time); + winfo->info->rotation_base = zeek::util::detail::parse_rotate_base_time(base_time); winfo->writer = new WriterFrontend(*winfo->info, id, writer, local, remote); winfo->writer->Init(num_fields, fields); @@ -1474,9 +1474,9 @@ void Manager::InstallRotationTimer(WriterInfo* winfo) static auto log_rotate_base_time = zeek::id::find_val("log_rotate_base_time"); static auto base_time = log_rotate_base_time->AsString()->CheckString(); - double base = zeek::util::parse_rotate_base_time(base_time); + double base = zeek::util::detail::parse_rotate_base_time(base_time); double delta_t = - zeek::util::calc_next_rotate(zeek::net::network_time, rotation_interval, base); + zeek::util::detail::calc_next_rotate(zeek::net::network_time, rotation_interval, base); winfo->rotation_timer = new RotationTimer(zeek::net::network_time + delta_t, winfo, true); @@ -1522,7 +1522,7 @@ std::string Manager::FormatRotationPath(zeek::EnumValPtr writer, auto prefix = rp_val->GetField(1)->AsString()->CheckString(); auto dir = dir_val->AsString()->CheckString(); - if ( ! zeek::util::streq(dir, "") && ! zeek::util::ensure_intermediate_dirs(dir) ) + if ( ! zeek::util::streq(dir, "") && ! zeek::util::detail::ensure_intermediate_dirs(dir) ) { zeek::reporter->Error("Failed to create dir '%s' returned by " "Log::rotation_format_func for path %.*s: %s", diff --git a/src/plugin/Manager.cc b/src/plugin/Manager.cc index 3bdb5ed7f3..d77fd0622e 100644 --- a/src/plugin/Manager.cc +++ b/src/plugin/Manager.cc @@ -180,47 +180,47 @@ bool Manager::ActivateDynamicPluginInternal(const std::string& name, bool ok_if_ if ( zeek::util::is_dir(scripts) ) { DBG_LOG(zeek::DBG_PLUGINS, " Adding %s to ZEEKPATH", scripts.c_str()); - zeek::util::add_to_zeek_path(scripts); + zeek::util::detail::add_to_zeek_path(scripts); } string init; // First load {scripts}/__preload__.zeek automatically. - for (const string& ext : zeek::util::script_extensions) + for (const string& ext : zeek::util::detail::script_extensions) { init = dir + "scripts/__preload__" + ext; if ( zeek::util::is_file(init) ) { DBG_LOG(zeek::DBG_PLUGINS, " Loading %s", init.c_str()); - zeek::util::warn_if_legacy_script(init); + zeek::util::detail::warn_if_legacy_script(init); scripts_to_load.push_back(init); break; } } // Load {bif,scripts}/__load__.zeek automatically. - for (const string& ext : zeek::util::script_extensions) + for (const string& ext : zeek::util::detail::script_extensions) { init = dir + "lib/bif/__load__" + ext; if ( zeek::util::is_file(init) ) { DBG_LOG(zeek::DBG_PLUGINS, " Loading %s", init.c_str()); - zeek::util::warn_if_legacy_script(init); + zeek::util::detail::warn_if_legacy_script(init); scripts_to_load.push_back(init); break; } } - for (const string& ext : zeek::util::script_extensions) + for (const string& ext : zeek::util::detail::script_extensions) { init = dir + "scripts/__load__" + ext; if ( zeek::util::is_file(init) ) { DBG_LOG(zeek::DBG_PLUGINS, " Loading %s", init.c_str()); - zeek::util::warn_if_legacy_script(init); + zeek::util::detail::warn_if_legacy_script(init); scripts_to_load.push_back(init); break; } @@ -259,7 +259,7 @@ bool Manager::ActivateDynamicPluginInternal(const std::string& name, bool ok_if_ DBG_LOG(zeek::DBG_PLUGINS, " InitialzingComponents"); current_plugin->InitializeComponents(); - plugins_by_path.insert(std::make_pair(zeek::util::normalize_path(dir), current_plugin)); + plugins_by_path.insert(std::make_pair(zeek::util::detail::normalize_path(dir), current_plugin)); // We execute the pre-script initialization here; this in // fact could be *during* script initialization if we got @@ -346,7 +346,7 @@ void Manager::RegisterPlugin(Plugin *plugin) if ( current_dir && current_sopath ) // A dynamic plugin, record its location. - plugin->SetPluginLocation(zeek::util::normalize_path(current_dir), current_sopath); + plugin->SetPluginLocation(zeek::util::detail::normalize_path(current_dir), current_sopath); current_plugin = plugin; } @@ -483,7 +483,7 @@ Manager::bif_init_func_map* Manager::BifFilesInternal() Plugin* Manager::LookupPluginByPath(std::string_view _path) { - auto path = zeek::util::normalize_path(_path); + auto path = zeek::util::detail::normalize_path(_path); if ( zeek::util::is_file(path) ) path = zeek::util::SafeDirname(path).result; diff --git a/src/probabilistic/Hasher.cc b/src/probabilistic/Hasher.cc index e3dd574912..06062a479c 100644 --- a/src/probabilistic/Hasher.cc +++ b/src/probabilistic/Hasher.cc @@ -33,7 +33,7 @@ Hasher::seed_t Hasher::MakeSeed(const void* data, size_t size) else { - unsigned int first_seed = zeek::util::initial_seed(); + unsigned int first_seed = zeek::util::detail::initial_seed(); zeek::detail::hash_update(ctx, &first_seed, sizeof(first_seed)); } @@ -119,7 +119,7 @@ DefaultHasher::DefaultHasher(size_t k, Hasher::seed_t seed) for ( size_t i = 1; i <= k; ++i ) { seed_t s = Seed(); - s.h[0] += zeek::util::prng(i); + s.h[0] += zeek::util::detail::prng(i); hash_functions.push_back(UHF(s)); } } @@ -149,7 +149,7 @@ bool DefaultHasher::Equals(const Hasher* other) const } DoubleHasher::DoubleHasher(size_t k, seed_t seed) - : Hasher(k, seed), h1(seed + zeek::util::prng(1)), h2(seed + zeek::util::prng(2)) + : Hasher(k, seed), h1(seed + zeek::util::detail::prng(1)), h2(seed + zeek::util::detail::prng(2)) { } diff --git a/src/re-scan.l b/src/re-scan.l index 9becd2fab0..17ffd1879a 100644 --- a/src/re-scan.l +++ b/src/re-scan.l @@ -212,7 +212,7 @@ CCL_EXPR ("[:"[[:alpha:]]+":]") {ESCSEQ} { const char* esc_text = yytext + 1; - yylval.int_val = zeek::util::expand_escape(esc_text); + yylval.int_val = zeek::util::detail::expand_escape(esc_text); if ( YY_START == SC_FIRST_CCL ) BEGIN(SC_CCL); diff --git a/src/rule-scan.l b/src/rule-scan.l index 6b61844079..ff4c6b0dfa 100644 --- a/src/rule-scan.l +++ b/src/rule-scan.l @@ -43,13 +43,13 @@ PID {PIDCOMPONENT}(::{PIDCOMPONENT})* } {IP6} { - rules_lval.prefixval = new zeek::IPPrefix(zeek::IPAddr(zeek::util::extract_ip(yytext)), 128, true); + rules_lval.prefixval = new zeek::IPPrefix(zeek::IPAddr(zeek::util::detail::extract_ip(yytext)), 128, true); return TOK_IP6; } {IP6}{OWS}"/"{OWS}{D} { int len = 0; - std::string ip = zeek::util::extract_ip_and_len(yytext, &len); + std::string ip = zeek::util::detail::extract_ip_and_len(yytext, &len); rules_lval.prefixval = new zeek::IPPrefix(zeek::IPAddr(ip), len, true); return TOK_IP6; } diff --git a/src/scan.l b/src/scan.l index f7ed9fc305..3ae0c29672 100644 --- a/src/scan.l +++ b/src/scan.l @@ -175,12 +175,12 @@ ESCSEQ (\\([^\n]|[0-7]+|x[[:xdigit:]]+)) /* IPv6 literal constant patterns */ {IP6} { - RET_CONST(new zeek::AddrVal(zeek::util::extract_ip(yytext))) + RET_CONST(new zeek::AddrVal(zeek::util::detail::extract_ip(yytext))) } {IP6}{OWS}"/"{OWS}{D} { int len = 0; - std::string ip = zeek::util::extract_ip_and_len(yytext, &len); + std::string ip = zeek::util::detail::extract_ip_and_len(yytext, &len); RET_CONST(new zeek::SubNetVal(zeek::IPPrefix(zeek::IPAddr(ip), len, true))) } @@ -189,7 +189,7 @@ ESCSEQ (\\([^\n]|[0-7]+|x[[:xdigit:]]+)) ({D}"."){3}{D}{OWS}"/"{OWS}{D} { int len = 0; - std::string ip = zeek::util::extract_ip_and_len(yytext, &len); + std::string ip = zeek::util::detail::extract_ip_and_len(yytext, &len); RET_CONST(new zeek::SubNetVal(zeek::IPPrefix(zeek::IPAddr(ip), len))) } @@ -516,7 +516,7 @@ F RET_CONST(zeek::val_mgr->False()->Ref()) if ( *text == '\\' ) { ++text; // skip '\' - s[i++] = zeek::util::expand_escape(text); + s[i++] = zeek::util::detail::expand_escape(text); --text; // point to end of sequence } else @@ -614,7 +614,7 @@ static int load_files(const char* orig_file) zeek::reporter->FatalError("can't find %s", orig_file); if ( zeek::util::is_dir(file_path.c_str()) ) - f = zeek::util::open_package(file_path); + f = zeek::util::detail::open_package(file_path); else f = zeek::util::open_file(file_path); @@ -903,8 +903,8 @@ int yywrap() if ( ! zeek_script_prefixes[i][0] ) continue; - std::string canon = zeek::util::without_zeekpath_component(it->name); - std::string flat = zeek::util::flatten_script_name(canon, zeek_script_prefixes[i]); + std::string canon = zeek::util::detail::without_zeekpath_component(it->name); + std::string flat = zeek::util::detail::flatten_script_name(canon, zeek_script_prefixes[i]); std::string path = find_relative_script_file(flat); if ( ! path.empty() ) diff --git a/src/supervisor/Supervisor.cc b/src/supervisor/Supervisor.cc index 4fa08dbd31..cf66bad118 100644 --- a/src/supervisor/Supervisor.cc +++ b/src/supervisor/Supervisor.cc @@ -624,7 +624,7 @@ size_t Supervisor::ProcessMessages() Stem::Stem(State ss) : parent_pid(ss.parent_pid), signal_flare(new zeek::detail::Flare()), pipe(std::move(ss.pipe)) { - zeek::util::set_thread_name("zeek.stem"); + zeek::util::detail::set_thread_name("zeek.stem"); pipe->Swap(); stem = this; setsignal(SIGCHLD, stem_signal_handler); @@ -840,7 +840,7 @@ std::variant Stem::Spawn(SupervisorNode* node) { setsignal(SIGCHLD, SIG_DFL); setsignal(SIGTERM, SIG_DFL); - zeek::util::set_thread_name(zeek::util::fmt("zeek.%s", node->Name().data())); + zeek::util::detail::set_thread_name(zeek::util::fmt("zeek.%s", node->Name().data())); SupervisedNode rval; rval.config = node->config; rval.parent_pid = ppid; @@ -1610,7 +1610,7 @@ std::string Supervisor::Create(const Supervisor::NodeConfig& node) if ( node.directory ) { - auto res = zeek::util::ensure_intermediate_dirs(node.directory->data()); + auto res = zeek::util::detail::ensure_intermediate_dirs(node.directory->data()); if ( ! res ) return zeek::util::fmt("failed to create working directory %s\n", diff --git a/src/threading/BasicThread.cc b/src/threading/BasicThread.cc index 45b8120ad4..dcac59ead5 100644 --- a/src/threading/BasicThread.cc +++ b/src/threading/BasicThread.cc @@ -47,7 +47,7 @@ void BasicThread::SetName(const char* arg_name) void BasicThread::SetOSName(const char* arg_name) { static_assert(std::is_same::value, "libstdc++ doesn't use pthread_t"); - zeek::util::set_thread_name(arg_name, thread.native_handle()); + zeek::util::detail::set_thread_name(arg_name, thread.native_handle()); } const char* BasicThread::Fmt(const char* format, ...) diff --git a/src/threading/Manager.cc b/src/threading/Manager.cc index e0e325cb1d..95f9826f09 100644 --- a/src/threading/Manager.cc +++ b/src/threading/Manager.cc @@ -182,7 +182,7 @@ bool Manager::SendEvent(MsgThread* thread, const std::string& name, const int nu if ( convert_error ) return false; else if ( handler ) - zeek::event_mgr.Enqueue(handler, std::move(vl), zeek::util::SOURCE_LOCAL); + zeek::event_mgr.Enqueue(handler, std::move(vl), zeek::util::detail::SOURCE_LOCAL); return true; } diff --git a/src/util.cc b/src/util.cc index d29c6fb10b..e8333f4957 100644 --- a/src/util.cc +++ b/src/util.cc @@ -72,7 +72,10 @@ using namespace std; extern const char* proc_status_file; -namespace zeek::util { +static bool can_read(const string& path) + { + return access(path.c_str(), R_OK) == 0; + } static bool starts_with(std::string_view s, std::string_view beginning) { @@ -104,6 +107,11 @@ TEST_CASE("util ends_with") CHECK(ends_with("abcde", "abcedf") == false); } +static string zeek_path_value; + +namespace zeek::util { +namespace detail { + TEST_CASE("util extract_ip") { CHECK(extract_ip("[1.2.3.4]") == "1.2.3.4"); @@ -166,6 +174,863 @@ std::string extract_ip_and_len(const std::string& i, int* len) return extract_ip(i.substr(0, pos)); } + +static constexpr int parse_octal_digit(char ch) noexcept + { + if ( ch >= '0' && ch <= '7' ) + return ch - '0'; + else + return -1; + } + +static constexpr int parse_hex_digit(char ch) noexcept + { + if ( ch >= '0' && ch <= '9' ) + return ch - '0'; + else if ( ch >= 'a' && ch <= 'f' ) + return 10 + ch - 'a'; + else if ( ch >= 'A' && ch <= 'F' ) + return 10 + ch - 'A'; + else + return -1; + } + +int expand_escape(const char*& s) + { + switch ( *(s++) ) { + case 'b': return '\b'; + case 'f': return '\f'; + case 'n': return '\n'; + case 'r': return '\r'; + case 't': return '\t'; + case 'a': return '\a'; + case 'v': return '\v'; + + case '0': case '1': case '2': case '3': case '4': + case '5': case '6': case '7': + { // \{1,3} + --s; // put back the first octal digit + const char* start = s; + + // require at least one octal digit and parse at most three + + int result = parse_octal_digit(*s++); + + if ( result < 0 ) + { + zeek::reporter->Error("bad octal escape: %s", start); + return 0; + } + + // second digit? + int digit = parse_octal_digit(*s); + + if ( digit >= 0 ) + { + result = (result << 3) | digit; + ++s; + + // third digit? + digit = parse_octal_digit(*s); + + if ( digit >= 0 ) + { + result = (result << 3) | digit; + ++s; + } + } + + return result; + } + + case 'x': + { /* \x */ + const char* start = s; + + // Look at most 2 characters, so that "\x0ddir" -> "^Mdir". + + int result = parse_hex_digit(*s++); + + if ( result < 0 ) + { + zeek::reporter->Error("bad hexadecimal escape: %s", start); + return 0; + } + + // second digit? + int digit = parse_hex_digit(*s); + + if ( digit >= 0 ) + { + result = (result << 4) | digit; + ++s; + } + + return result; + } + + default: + return s[-1]; + } + } + +const char* fmt_access_time(double t) + { + static char buf[256]; + time_t time = (time_t) t; + struct tm ts; + + if ( ! localtime_r(&time, &ts) ) + { + zeek::reporter->InternalError("unable to get time"); + } + + strftime(buf, sizeof(buf), "%d/%m-%H:%M", &ts); + return buf; + } + +bool ensure_intermediate_dirs(const char* dirname) + { + if ( ! dirname || strlen(dirname) == 0 ) + return false; + + bool absolute = dirname[0] == '/'; + string path = normalize_path(dirname); + + const auto path_components = tokenize_string(path, '/'); + + string current_dir; + + for ( size_t i = 0; i < path_components.size(); ++i ) + { + if ( i > 0 || absolute ) + current_dir += "/"; + + current_dir += path_components[i]; + + if ( ! ensure_dir(current_dir.c_str()) ) + return false; + } + + return true; + } + +bool ensure_dir(const char *dirname) + { + if ( mkdir(dirname, 0700) == 0 ) + return true; + + auto mkdir_errno = errno; + struct stat st; + + if ( stat(dirname, &st) == -1 ) + { + // Show the original failure reason for mkdir() since nothing's there + // or we can't even tell what is now. + zeek::reporter->Warning("can't create directory %s: %s", + dirname, strerror(mkdir_errno)); + return false; + } + + if ( S_ISDIR(st.st_mode) ) + return true; + + zeek::reporter->Warning("%s exists but is not a directory", dirname); + return false; + } + +void hmac_md5(size_t size, const unsigned char* bytes, unsigned char digest[16]) + { + if ( ! zeek::detail::KeyedHash::seeds_initialized ) + zeek::reporter->InternalError("HMAC-MD5 invoked before the HMAC key is set"); + + zeek::detail::internal_md5(bytes, size, digest); + + for ( int i = 0; i < 16; ++i ) + digest[i] ^= zeek::detail::KeyedHash::shared_hmac_md5_key[i]; + + zeek::detail::internal_md5(digest, 16, digest); + } + +static bool read_random_seeds(const char* read_file, uint32_t* seed, + std::array& buf) + { + FILE* f = nullptr; + + if ( ! (f = fopen(read_file, "r")) ) + { + zeek::reporter->Warning("Could not open seed file '%s': %s", + read_file, strerror(errno)); + return false; + } + + // Read seed for srandom(). + if ( fscanf(f, "%u", seed) != 1 ) + { + fclose(f); + return false; + } + + // Read seeds for hmac-md5/siphash/highwayhash. + for ( auto &v : buf ) + { + int tmp; + if ( fscanf(f, "%u", &tmp) != 1 ) + { + fclose(f); + return false; + } + + v = tmp; + } + + fclose(f); + return true; + } + +static bool write_random_seeds(const char* write_file, uint32_t seed, + std::array& buf) + { + FILE* f = nullptr; + + if ( ! (f = fopen(write_file, "w+")) ) + { + zeek::reporter->Warning("Could not create seed file '%s': %s", + write_file, strerror(errno)); + return false; + } + + fprintf(f, "%u\n", seed); + + for ( const auto &v: buf ) + fprintf(f, "%u\n", v); + + fclose(f); + return true; + } + +static bool zeek_rand_determistic = false; +static long int zeek_rand_state = 0; +static bool first_seed_saved = false; +static unsigned int first_seed = 0; + +static void zeek_srandom(unsigned int seed, bool deterministic) + { + zeek_rand_state = seed == 0 ? 1 : seed; + zeek_rand_determistic = deterministic; + + srandom(seed); + } + +void seed_random(unsigned int seed) + { + if ( zeek_rand_determistic ) + zeek_rand_state = seed == 0 ? 1 : seed; + else + srandom(seed); + } + +void init_random_seed(const char* read_file, const char* write_file, + bool use_empty_seeds) + { + std::array buf = {}; + size_t pos = 0; // accumulates entropy + bool seeds_done = false; + uint32_t seed = 0; + + if ( read_file ) + { + if ( ! read_random_seeds(read_file, &seed, buf) ) + zeek::reporter->FatalError("Could not load seeds from file '%s'.\n", + read_file); + else + seeds_done = true; + } + else if ( use_empty_seeds ) + seeds_done = true; + + if ( ! seeds_done ) + { +#ifdef HAVE_GETRANDOM + // getrandom() guarantees reads up to 256 bytes are always successful, + assert(sizeof(buf) < 256); + auto nbytes = getrandom(buf.data(), sizeof(buf), 0); + assert(nbytes == sizeof(buf)); + pos += nbytes / sizeof(uint32_t); +#else + // Gather up some entropy. + gettimeofday((struct timeval *)(buf.data() + pos), 0); + pos += sizeof(struct timeval) / sizeof(uint32_t); + + // use urandom. For reasons see e.g. http://www.2uo.de/myths-about-urandom/ +#if defined(O_NONBLOCK) + int fd = open("/dev/urandom", O_RDONLY | O_NONBLOCK); +#elif defined(O_NDELAY) + int fd = open("/dev/urandom", O_RDONLY | O_NDELAY); +#else + int fd = open("/dev/urandom", O_RDONLY); +#endif + + if ( fd >= 0 ) + { + int amt = read(fd, buf.data() + pos, + sizeof(uint32_t) * (zeek::detail::KeyedHash::SEED_INIT_SIZE - pos)); + safe_close(fd); + + if ( amt > 0 ) + pos += amt / sizeof(uint32_t); + else + // Clear errno, which can be set on some + // systems due to a lack of entropy. + errno = 0; + } +#endif + + if ( pos < zeek::detail::KeyedHash::SEED_INIT_SIZE ) + zeek::reporter->FatalError("Could not read enough random data. Wanted %d, got %zu", + zeek::detail::KeyedHash::SEED_INIT_SIZE, pos); + + if ( ! seed ) + { + for ( size_t i = 0; i < pos; ++i ) + { + seed ^= buf[i]; + seed = (seed << 1) | (seed >> 31); + } + } + else + seeds_done = true; + } + + zeek_srandom(seed, seeds_done); + + if ( ! first_seed_saved ) + { + first_seed = seed; + first_seed_saved = true; + } + + if ( ! zeek::detail::KeyedHash::IsInitialized() ) + zeek::detail::KeyedHash::InitializeSeeds(buf); + + if ( write_file && ! write_random_seeds(write_file, seed, buf) ) + zeek::reporter->Error("Could not write seeds to file '%s'.\n", + write_file); + } + +unsigned int initial_seed() + { + return first_seed; + } + +bool have_random_seed() + { + return zeek_rand_determistic; + } + +constexpr uint32_t zeek_prng_mod = 2147483647; +constexpr uint32_t zeek_prng_max = zeek_prng_mod - 1; + +long int max_random() + { + return zeek_rand_determistic ? zeek_prng_max : RAND_MAX; + } + +long int prng(long int state) + { + // Use our own simple linear congruence PRNG to make sure we are + // predictable across platforms. (Lehmer RNG, Schrage's method) + // Note: the choice of "long int" storage type for the state is mostly + // for parity with the possible return values of random(). + constexpr uint32_t m = zeek_prng_mod; + constexpr uint32_t a = 16807; + constexpr uint32_t q = m / a; + constexpr uint32_t r = m % a; + + uint32_t rem = state % q; + uint32_t div = state / q; + int32_t s = a * rem; + int32_t t = r * div; + int32_t res = s - t; + + if ( res < 0 ) + res += m; + + return res; + } + +long int random_number() + { + if ( ! zeek_rand_determistic ) + return random(); // Use system PRNG. + + zeek_rand_state = detail::prng(zeek_rand_state); + + return zeek_rand_state; + } + +// Returns a 64-bit random string. +uint64_t rand64bit() + { + uint64_t base = 0; + int i; + + for ( i = 1; i <= 4; ++i ) + base = (base<<16) | detail::random_number(); + return base; + } + +const array script_extensions = {".zeek", ".bro"}; + +void warn_if_legacy_script(std::string_view filename) + { + if ( ends_with(filename, ".bro") ) + { + std::string x(filename); + zeek::reporter->Warning("Loading script '%s' with legacy extension, support for '.bro' will be removed in Zeek v4.1", x.c_str()); + } + } + +TEST_CASE("util is_package_loader") + { + CHECK(is_package_loader("/some/path/__load__.zeek") == true); + CHECK(is_package_loader("/some/path/notload.zeek") == false); + } + +bool is_package_loader(const string& path) + { + string filename(std::move(SafeBasename(path).result)); + + for ( const string& ext : script_extensions ) + { + if ( filename == "__load__" + ext ) + { + warn_if_legacy_script(filename); + return true; + } + } + + return false; + } + +void add_to_zeek_path(const string& dir) + { + // Make sure path is initialized. + zeek_path(); + + zeek_path_value += string(":") + dir; + } + +FILE* open_package(string& path, const string& mode) + { + string arg_path = path; + path.append("/__load__"); + + for ( const string& ext : script_extensions ) + { + string p = path + ext; + if ( can_read(p) ) + { + warn_if_legacy_script(path); + path.append(ext); + return open_file(path, mode); + } + } + + path.append(script_extensions[0]); + string package_loader = "__load__" + script_extensions[0]; + zeek::reporter->Error("Failed to open package '%s': missing '%s' file", + arg_path.c_str(), package_loader.c_str()); + return nullptr; + } + +void SafePathOp::CheckValid(const char* op_result, const char* path, + bool error_aborts) + { + if ( op_result ) + { + result = op_result; + error = false; + } + else + { + if ( error_aborts ) + zeek::reporter->InternalError("Path operation failed on %s: %s", + path ? path : "", strerror(errno)); + else + error = true; + } + } + +TEST_CASE("util flatten_script_name") + { + CHECK(flatten_script_name("script", "some/path") == "some.path.script"); + CHECK(flatten_script_name("other/path/__load__.zeek", "some/path") == "some.path.other.path"); + CHECK(flatten_script_name("path/to/script", "") == "path.to.script"); + } + +string flatten_script_name(const string& name, const string& prefix) + { + string rval = prefix; + + if ( ! rval.empty() ) + rval.append("."); + + if ( is_package_loader(name) ) + rval.append(SafeDirname(name).result); + else + rval.append(name); + + size_t i; + + while ( (i = rval.find('/')) != string::npos ) + rval[i] = '.'; + + return rval; + } + +TEST_CASE("util normalize_path") + { + CHECK(normalize_path("/1/2/3") == "/1/2/3"); + CHECK(normalize_path("/1/./2/3") == "/1/2/3"); + CHECK(normalize_path("/1/2/../3") == "/1/3"); + CHECK(normalize_path("1/2/3/") == "1/2/3"); + CHECK(normalize_path("1/2//3///") == "1/2/3"); + CHECK(normalize_path("~/zeek/testing") == "~/zeek/testing"); + CHECK(normalize_path("~jon/zeek/testing") == "~jon/zeek/testing"); + CHECK(normalize_path("~jon/./zeek/testing") == "~jon/zeek/testing"); + CHECK(normalize_path("~/zeek/testing/../././.") == "~/zeek"); + CHECK(normalize_path("./zeek") == "./zeek"); + CHECK(normalize_path("../zeek") == "../zeek"); + CHECK(normalize_path("../zeek/testing/..") == "../zeek"); + CHECK(normalize_path("./zeek/..") == "."); + CHECK(normalize_path("./zeek/../..") == ".."); + CHECK(normalize_path("./zeek/../../..") == "../.."); + CHECK(normalize_path("./..") == ".."); + CHECK(normalize_path("../..") == "../.."); + CHECK(normalize_path("/..") == "/.."); + CHECK(normalize_path("~/..") == "~/.."); + CHECK(normalize_path("/../..") == "/../.."); + CHECK(normalize_path("~/../..") == "~/../.."); + CHECK(normalize_path("zeek/..") == ""); + CHECK(normalize_path("zeek/../..") == ".."); + } + +string normalize_path(std::string_view path) + { + if ( path.find("/.") == std::string_view::npos && + path.find("//") == std::string_view::npos ) + { + // no need to normalize anything + if ( path.size() > 1 && path.back() == '/' ) + path.remove_suffix(1); + return std::string(path); + } + + size_t n; + vector final_components; + string new_path; + new_path.reserve(path.size()); + + if ( ! path.empty() && path[0] == '/' ) + new_path = "/"; + + const auto components = tokenize_string(path, '/'); + final_components.reserve(components.size()); + + for ( auto it = components.begin(); it != components.end(); ++it ) + { + if ( *it == "" ) continue; + if ( *it == "." && it != components.begin() ) continue; + + final_components.push_back(*it); + + if ( *it == ".." ) + { + auto cur_idx = final_components.size() - 1; + + if ( cur_idx != 0 ) + { + auto last_idx = cur_idx - 1; + auto& last_component = final_components[last_idx]; + + if ( last_component == "/" || last_component == "~" || + last_component == ".." ) + continue; + + if ( last_component == "." ) + { + last_component = ".."; + final_components.pop_back(); + } + else + { + final_components.pop_back(); + final_components.pop_back(); + } + } + } + } + + for ( auto it = final_components.begin(); it != final_components.end(); ++it ) + { + new_path.append(*it); + new_path.append("/"); + } + + if ( new_path.size() > 1 && new_path[new_path.size() - 1] == '/' ) + new_path.erase(new_path.size() - 1); + + return new_path; + } + +string without_zeekpath_component(std::string_view path) + { + string rval = normalize_path(path); + + const auto paths = tokenize_string(zeek_path(), ':'); + + for ( size_t i = 0; i < paths.size(); ++i ) + { + string common = normalize_path(paths[i]); + + if ( rval.find(common) != 0 ) + continue; + + // Found the containing directory. + std::string_view v(rval); + v.remove_prefix(common.size()); + + // Remove leading path separators. + while ( !v.empty() && v.front() == '/' ) + v.remove_prefix(1); + + return std::string(v); + } + + return rval; + } + +std::string get_exe_path(const std::string& invocation) + { + if ( invocation.empty() ) + return ""; + + if ( invocation[0] == '/' || invocation[0] == '~' ) + // Absolute path + return invocation; + + if ( invocation.find('/') != std::string::npos ) + { + // Relative path + char cwd[PATH_MAX]; + + if ( ! getcwd(cwd, sizeof(cwd)) ) + { + fprintf(stderr, "failed to get current directory: %s\n", + strerror(errno)); + exit(1); + } + + return std::string(cwd) + "/" + invocation; + } + + auto path = getenv("PATH"); + + if ( ! path ) + return ""; + + return find_file(invocation, path); + } + +FILE* rotate_file(const char* name, zeek::RecordVal* rotate_info) + { + // Build file names. + const int buflen = strlen(name) + 128; + + auto newname_buf = std::make_unique(buflen); + auto tmpname_buf = std::make_unique(buflen + 4); + auto newname = newname_buf.get(); + auto tmpname = tmpname_buf.get(); + + snprintf(newname, buflen, "%s.%d.%.06f.tmp", + name, getpid(), zeek::net::network_time); + newname[buflen-1] = '\0'; + strcpy(tmpname, newname); + strcat(tmpname, ".tmp"); + + // First open the new file using a temporary name. + FILE* newf = fopen(tmpname, "w"); + if ( ! newf ) + { + zeek::reporter->Error("rotate_file: can't open %s: %s", tmpname, strerror(errno)); + return nullptr; + } + + // Then move old file to ".." and make sure + // it really gets created. + struct stat dummy; + if ( link(name, newname) < 0 || stat(newname, &dummy) < 0 ) + { + zeek::reporter->Error("rotate_file: can't move %s to %s: %s", name, newname, strerror(errno)); + fclose(newf); + unlink(newname); + unlink(tmpname); + return nullptr; + } + + // Close current file, and move the tmp to its place. + if ( unlink(name) < 0 || link(tmpname, name) < 0 || unlink(tmpname) < 0 ) + { + zeek::reporter->Error("rotate_file: can't move %s to %s: %s", tmpname, name, strerror(errno)); + exit(1); // hard to fix, but shouldn't happen anyway... + } + + // Init rotate_info. + if ( rotate_info ) + { + rotate_info->Assign(0, name); + rotate_info->Assign(1, newname); + rotate_info->Assign(2, zeek::net::network_time); + rotate_info->Assign(3, zeek::net::network_time); + } + + return newf; + } + +const char* log_file_name(const char* tag) + { + const char* env = zeekenv("ZEEK_LOG_SUFFIX"); + return fmt("%s.%s", tag, (env ? env : "log")); + } + +double parse_rotate_base_time(const char* rotate_base_time) + { + double base = -1; + + if ( rotate_base_time && rotate_base_time[0] != '\0' ) + { + struct tm t; + if ( ! strptime(rotate_base_time, "%H:%M", &t) ) + zeek::reporter->Error("calc_next_rotate(): can't parse rotation base time"); + else + base = t.tm_min * 60 + t.tm_hour * 60 * 60; + } + + return base; + } + +double calc_next_rotate(double current, double interval, double base) + { + if ( ! interval ) + { + zeek::reporter->Error("calc_next_rotate(): interval is zero, falling back to 24hrs"); + interval = 86400; + } + + // Calculate start of day. + time_t teatime = time_t(current); + + struct tm t; + if ( ! localtime_r(&teatime, &t) ) + { + zeek::reporter->Error("calc_next_rotate(): failure processing current time (%.6f)", current); + + // fall back to the method used if no base time is given + base = -1; + } + + if ( base < 0 ) + // No base time given. To get nice timestamps, we round + // the time up to the next multiple of the rotation interval. + return floor(current / interval) * interval + + interval - current; + + t.tm_hour = t.tm_min = t.tm_sec = 0; + double startofday = mktime(&t); + + // current < startofday + base + i * interval <= current + interval + return startofday + base + + ceil((current - startofday - base) / interval) * interval - + current; + } + +void terminate_processing() + { + if ( ! zeek::net::terminating ) + raise(SIGTERM); + } + +void set_processing_status(const char* status, const char* reason) + { + if ( ! proc_status_file ) + return; + + // This function can be called from a signal context, so we have to + // make sure to only call reentrant functions and to restore errno + // afterwards. + + int old_errno = errno; + + int fd = open(proc_status_file, O_CREAT | O_WRONLY | O_TRUNC, 0700); + + if ( fd < 0 ) + { + char buf[256]; + zeek_strerror_r(errno, buf, sizeof(buf)); + if ( zeek::reporter ) + zeek::reporter->Error("Failed to open process status file '%s': %s", + proc_status_file, buf); + else + fprintf(stderr, "Failed to open process status file '%s': %s\n", + proc_status_file, buf); + errno = old_errno; + return; + } + + auto write_str = [](int fd, const char* s) + { + int len = strlen(s); + while ( len ) + { + int n = write(fd, s, len); + + if ( n < 0 && errno != EINTR && errno != EAGAIN ) + // Ignore errors, as they're too difficult to + // safely report here. + break; + + s += n; + len -= n; + } + }; + + write_str(fd, status); + write_str(fd, " ["); + write_str(fd, reason); + write_str(fd, "]\n"); + safe_close(fd); + + errno = old_errno; + } + +void set_thread_name(const char* name, pthread_t tid) + { +#ifdef HAVE_LINUX + prctl(PR_SET_NAME, name, 0, 0, 0); +#endif + +#ifdef __APPLE__ + pthread_setname_np(name); +#endif + +#ifdef __FreeBSD__ + pthread_set_name_np(tid, name); +#endif + } + +} // namespace detail + TEST_CASE("util get_unescaped_string") { CHECK(get_unescaped_string("abcde") == "abcde"); @@ -304,105 +1169,6 @@ int streq(const char* s1, const char* s2) return ! strcmp(s1, s2); } -static constexpr int parse_octal_digit(char ch) noexcept - { - if ( ch >= '0' && ch <= '7' ) - return ch - '0'; - else - return -1; - } - -static constexpr int parse_hex_digit(char ch) noexcept - { - if ( ch >= '0' && ch <= '9' ) - return ch - '0'; - else if ( ch >= 'a' && ch <= 'f' ) - return 10 + ch - 'a'; - else if ( ch >= 'A' && ch <= 'F' ) - return 10 + ch - 'A'; - else - return -1; - } - -int expand_escape(const char*& s) - { - switch ( *(s++) ) { - case 'b': return '\b'; - case 'f': return '\f'; - case 'n': return '\n'; - case 'r': return '\r'; - case 't': return '\t'; - case 'a': return '\a'; - case 'v': return '\v'; - - case '0': case '1': case '2': case '3': case '4': - case '5': case '6': case '7': - { // \{1,3} - --s; // put back the first octal digit - const char* start = s; - - // require at least one octal digit and parse at most three - - int result = parse_octal_digit(*s++); - - if ( result < 0 ) - { - zeek::reporter->Error("bad octal escape: %s", start); - return 0; - } - - // second digit? - int digit = parse_octal_digit(*s); - - if ( digit >= 0 ) - { - result = (result << 3) | digit; - ++s; - - // third digit? - digit = parse_octal_digit(*s); - - if ( digit >= 0 ) - { - result = (result << 3) | digit; - ++s; - } - } - - return result; - } - - case 'x': - { /* \x */ - const char* start = s; - - // Look at most 2 characters, so that "\x0ddir" -> "^Mdir". - - int result = parse_hex_digit(*s++); - - if ( result < 0 ) - { - zeek::reporter->Error("bad hexadecimal escape: %s", start); - return 0; - } - - // second digit? - int digit = parse_hex_digit(*s); - - if ( digit >= 0 ) - { - result = (result << 4) | digit; - ++s; - } - - return result; - } - - default: - return s[-1]; - } - } - char* skip_whitespace(char* s) { while ( *s == ' ' || *s == '\t' ) @@ -862,71 +1628,6 @@ const char* fmt(const char* format, ...) return rval; } -const char* fmt_access_time(double t) - { - static char buf[256]; - time_t time = (time_t) t; - struct tm ts; - - if ( ! localtime_r(&time, &ts) ) - { - zeek::reporter->InternalError("unable to get time"); - } - - strftime(buf, sizeof(buf), "%d/%m-%H:%M", &ts); - return buf; - } - -bool ensure_intermediate_dirs(const char* dirname) - { - if ( ! dirname || strlen(dirname) == 0 ) - return false; - - bool absolute = dirname[0] == '/'; - string path = normalize_path(dirname); - - const auto path_components = tokenize_string(path, '/'); - - string current_dir; - - for ( size_t i = 0; i < path_components.size(); ++i ) - { - if ( i > 0 || absolute ) - current_dir += "/"; - - current_dir += path_components[i]; - - if ( ! ensure_dir(current_dir.c_str()) ) - return false; - } - - return true; - } - -bool ensure_dir(const char *dirname) - { - if ( mkdir(dirname, 0700) == 0 ) - return true; - - auto mkdir_errno = errno; - struct stat st; - - if ( stat(dirname, &st) == -1 ) - { - // Show the original failure reason for mkdir() since nothing's there - // or we can't even tell what is now. - zeek::reporter->Warning("can't create directory %s: %s", - dirname, strerror(mkdir_errno)); - return false; - } - - if ( S_ISDIR(st.st_mode) ) - return true; - - zeek::reporter->Warning("%s exists but is not a directory", dirname); - return false; - } - bool is_dir(const std::string& path) { struct stat st; @@ -999,247 +1700,6 @@ std::string strstrip(std::string s) return s; } -void hmac_md5(size_t size, const unsigned char* bytes, unsigned char digest[16]) - { - if ( ! zeek::detail::KeyedHash::seeds_initialized ) - zeek::reporter->InternalError("HMAC-MD5 invoked before the HMAC key is set"); - - zeek::detail::internal_md5(bytes, size, digest); - - for ( int i = 0; i < 16; ++i ) - digest[i] ^= zeek::detail::KeyedHash::shared_hmac_md5_key[i]; - - zeek::detail::internal_md5(digest, 16, digest); - } - -static bool read_random_seeds(const char* read_file, uint32_t* seed, - std::array& buf) - { - FILE* f = nullptr; - - if ( ! (f = fopen(read_file, "r")) ) - { - zeek::reporter->Warning("Could not open seed file '%s': %s", - read_file, strerror(errno)); - return false; - } - - // Read seed for srandom(). - if ( fscanf(f, "%u", seed) != 1 ) - { - fclose(f); - return false; - } - - // Read seeds for hmac-md5/siphash/highwayhash. - for ( auto &v : buf ) - { - int tmp; - if ( fscanf(f, "%u", &tmp) != 1 ) - { - fclose(f); - return false; - } - - v = tmp; - } - - fclose(f); - return true; - } - -static bool write_random_seeds(const char* write_file, uint32_t seed, - std::array& buf) - { - FILE* f = nullptr; - - if ( ! (f = fopen(write_file, "w+")) ) - { - zeek::reporter->Warning("Could not create seed file '%s': %s", - write_file, strerror(errno)); - return false; - } - - fprintf(f, "%u\n", seed); - - for ( const auto &v: buf ) - fprintf(f, "%u\n", v); - - fclose(f); - return true; - } - -static bool zeek_rand_determistic = false; -static long int zeek_rand_state = 0; -static bool first_seed_saved = false; -static unsigned int first_seed = 0; - -static void zeek_srandom(unsigned int seed, bool deterministic) - { - zeek_rand_state = seed == 0 ? 1 : seed; - zeek_rand_determistic = deterministic; - - srandom(seed); - } - -void seed_random(unsigned int seed) - { - if ( zeek_rand_determistic ) - zeek_rand_state = seed == 0 ? 1 : seed; - else - srandom(seed); - } - -void init_random_seed(const char* read_file, const char* write_file, - bool use_empty_seeds) - { - std::array buf = {}; - size_t pos = 0; // accumulates entropy - bool seeds_done = false; - uint32_t seed = 0; - - if ( read_file ) - { - if ( ! read_random_seeds(read_file, &seed, buf) ) - zeek::reporter->FatalError("Could not load seeds from file '%s'.\n", - read_file); - else - seeds_done = true; - } - else if ( use_empty_seeds ) - seeds_done = true; - - if ( ! seeds_done ) - { -#ifdef HAVE_GETRANDOM - // getrandom() guarantees reads up to 256 bytes are always successful, - assert(sizeof(buf) < 256); - auto nbytes = getrandom(buf.data(), sizeof(buf), 0); - assert(nbytes == sizeof(buf)); - pos += nbytes / sizeof(uint32_t); -#else - // Gather up some entropy. - gettimeofday((struct timeval *)(buf.data() + pos), 0); - pos += sizeof(struct timeval) / sizeof(uint32_t); - - // use urandom. For reasons see e.g. http://www.2uo.de/myths-about-urandom/ -#if defined(O_NONBLOCK) - int fd = open("/dev/urandom", O_RDONLY | O_NONBLOCK); -#elif defined(O_NDELAY) - int fd = open("/dev/urandom", O_RDONLY | O_NDELAY); -#else - int fd = open("/dev/urandom", O_RDONLY); -#endif - - if ( fd >= 0 ) - { - int amt = read(fd, buf.data() + pos, - sizeof(uint32_t) * (zeek::detail::KeyedHash::SEED_INIT_SIZE - pos)); - safe_close(fd); - - if ( amt > 0 ) - pos += amt / sizeof(uint32_t); - else - // Clear errno, which can be set on some - // systems due to a lack of entropy. - errno = 0; - } -#endif - - if ( pos < zeek::detail::KeyedHash::SEED_INIT_SIZE ) - zeek::reporter->FatalError("Could not read enough random data. Wanted %d, got %zu", - zeek::detail::KeyedHash::SEED_INIT_SIZE, pos); - - if ( ! seed ) - { - for ( size_t i = 0; i < pos; ++i ) - { - seed ^= buf[i]; - seed = (seed << 1) | (seed >> 31); - } - } - else - seeds_done = true; - } - - zeek_srandom(seed, seeds_done); - - if ( ! first_seed_saved ) - { - first_seed = seed; - first_seed_saved = true; - } - - if ( ! zeek::detail::KeyedHash::IsInitialized() ) - zeek::detail::KeyedHash::InitializeSeeds(buf); - - if ( write_file && ! write_random_seeds(write_file, seed, buf) ) - zeek::reporter->Error("Could not write seeds to file '%s'.\n", - write_file); - } - -unsigned int initial_seed() - { - return first_seed; - } - -bool have_random_seed() - { - return zeek_rand_determistic; - } - -constexpr uint32_t zeek_prng_mod = 2147483647; -constexpr uint32_t zeek_prng_max = zeek_prng_mod - 1; - -long int max_random() - { - return zeek_rand_determistic ? zeek_prng_max : RAND_MAX; - } - -long int prng(long int state) - { - // Use our own simple linear congruence PRNG to make sure we are - // predictable across platforms. (Lehmer RNG, Schrage's method) - // Note: the choice of "long int" storage type for the state is mostly - // for parity with the possible return values of random(). - constexpr uint32_t m = zeek_prng_mod; - constexpr uint32_t a = 16807; - constexpr uint32_t q = m / a; - constexpr uint32_t r = m % a; - - uint32_t rem = state % q; - uint32_t div = state / q; - int32_t s = a * rem; - int32_t t = r * div; - int32_t res = s - t; - - if ( res < 0 ) - res += m; - - return res; - } - -long int random_number() - { - if ( ! zeek_rand_determistic ) - return random(); // Use system PRNG. - - zeek_rand_state = zeek::util::prng(zeek_rand_state); - - return zeek_rand_state; - } - -// Returns a 64-bit random string. -uint64_t rand64bit() - { - uint64_t base = 0; - int i; - - for ( i = 1; i <= 4; ++i ) - base = (base<<16) | zeek::util::random_number(); - return base; - } - int int_list_cmp(const void* v1, const void* v2) { std::intptr_t i1 = *(std::intptr_t*) v1; @@ -1253,8 +1713,6 @@ int int_list_cmp(const void* v1, const void* v2) return 1; } -static string zeek_path_value; - const std::string& zeek_path() { if ( zeek_path_value.empty() ) @@ -1270,14 +1728,6 @@ const std::string& zeek_path() return zeek_path_value; } -extern void add_to_zeek_path(const string& dir) - { - // Make sure path is initialized. - zeek_path(); - - zeek_path_value += string(":") + dir; - } - const char* zeek_plugin_path() { const char* path = zeekenv("ZEEK_PLUGIN_PATH"); @@ -1312,39 +1762,6 @@ string zeek_prefixes() return rval; } -TEST_CASE("util is_package_loader") - { - CHECK(is_package_loader("/some/path/__load__.zeek") == true); - CHECK(is_package_loader("/some/path/notload.zeek") == false); - } - -const array script_extensions = {".zeek", ".bro"}; - -void warn_if_legacy_script(std::string_view filename) - { - if ( ends_with(filename, ".bro") ) - { - std::string x(filename); - zeek::reporter->Warning("Loading script '%s' with legacy extension, support for '.bro' will be removed in Zeek v4.1", x.c_str()); - } - } - -bool is_package_loader(const string& path) - { - string filename(std::move(SafeBasename(path).result)); - - for ( const string& ext : script_extensions ) - { - if ( filename == "__load__" + ext ) - { - warn_if_legacy_script(filename); - return true; - } - } - - return false; - } - FILE* open_file(const string& path, const string& mode) { if ( path.empty() ) @@ -1362,34 +1779,6 @@ FILE* open_file(const string& path, const string& mode) return rval; } -static bool can_read(const string& path) - { - return access(path.c_str(), R_OK) == 0; - } - -FILE* open_package(string& path, const string& mode) - { - string arg_path = path; - path.append("/__load__"); - - for ( const string& ext : script_extensions ) - { - string p = path + ext; - if ( can_read(p) ) - { - warn_if_legacy_script(path); - path.append(ext); - return open_file(path, mode); - } - } - - path.append(script_extensions[0]); - string package_loader = "__load__" + script_extensions[0]; - zeek::reporter->Error("Failed to open package '%s': missing '%s' file", - arg_path.c_str(), package_loader.c_str()); - return nullptr; - } - TEST_CASE("util path ops") { SUBCASE("SafeDirname") @@ -1416,24 +1805,6 @@ TEST_CASE("util path ops") } } -void SafePathOp::CheckValid(const char* op_result, const char* path, - bool error_aborts) - { - if ( op_result ) - { - result = op_result; - error = false; - } - else - { - if ( error_aborts ) - zeek::reporter->InternalError("Path operation failed on %s: %s", - path ? path : "", strerror(errno)); - else - error = true; - } - } - SafeDirname::SafeDirname(const char* path, bool error_aborts) : SafePathOp() { @@ -1498,33 +1869,6 @@ string implode_string_vector(const std::vector& v, return rval; } -TEST_CASE("util flatten_script_name") - { - CHECK(flatten_script_name("script", "some/path") == "some.path.script"); - CHECK(flatten_script_name("other/path/__load__.zeek", "some/path") == "some.path.other.path"); - CHECK(flatten_script_name("path/to/script", "") == "path.to.script"); - } - -string flatten_script_name(const string& name, const string& prefix) - { - string rval = prefix; - - if ( ! rval.empty() ) - rval.append("."); - - if ( is_package_loader(name) ) - rval.append(SafeDirname(name).result); - else - rval.append(name); - - size_t i; - - while ( (i = rval.find('/')) != string::npos ) - rval[i] = '.'; - - return rval; - } - TEST_CASE("util tokenize_string") { auto v = tokenize_string("/this/is/a/path", "/", nullptr); @@ -1591,128 +1935,6 @@ vector tokenize_string(std::string_view input, const char deli return rval; } -TEST_CASE("util normalize_path") - { - CHECK(normalize_path("/1/2/3") == "/1/2/3"); - CHECK(normalize_path("/1/./2/3") == "/1/2/3"); - CHECK(normalize_path("/1/2/../3") == "/1/3"); - CHECK(normalize_path("1/2/3/") == "1/2/3"); - CHECK(normalize_path("1/2//3///") == "1/2/3"); - CHECK(normalize_path("~/zeek/testing") == "~/zeek/testing"); - CHECK(normalize_path("~jon/zeek/testing") == "~jon/zeek/testing"); - CHECK(normalize_path("~jon/./zeek/testing") == "~jon/zeek/testing"); - CHECK(normalize_path("~/zeek/testing/../././.") == "~/zeek"); - CHECK(normalize_path("./zeek") == "./zeek"); - CHECK(normalize_path("../zeek") == "../zeek"); - CHECK(normalize_path("../zeek/testing/..") == "../zeek"); - CHECK(normalize_path("./zeek/..") == "."); - CHECK(normalize_path("./zeek/../..") == ".."); - CHECK(normalize_path("./zeek/../../..") == "../.."); - CHECK(normalize_path("./..") == ".."); - CHECK(normalize_path("../..") == "../.."); - CHECK(normalize_path("/..") == "/.."); - CHECK(normalize_path("~/..") == "~/.."); - CHECK(normalize_path("/../..") == "/../.."); - CHECK(normalize_path("~/../..") == "~/../.."); - CHECK(normalize_path("zeek/..") == ""); - CHECK(normalize_path("zeek/../..") == ".."); - } - -string normalize_path(std::string_view path) - { - if ( path.find("/.") == std::string_view::npos && - path.find("//") == std::string_view::npos ) - { - // no need to normalize anything - if ( path.size() > 1 && path.back() == '/' ) - path.remove_suffix(1); - return std::string(path); - } - - size_t n; - vector final_components; - string new_path; - new_path.reserve(path.size()); - - if ( ! path.empty() && path[0] == '/' ) - new_path = "/"; - - const auto components = tokenize_string(path, '/'); - final_components.reserve(components.size()); - - for ( auto it = components.begin(); it != components.end(); ++it ) - { - if ( *it == "" ) continue; - if ( *it == "." && it != components.begin() ) continue; - - final_components.push_back(*it); - - if ( *it == ".." ) - { - auto cur_idx = final_components.size() - 1; - - if ( cur_idx != 0 ) - { - auto last_idx = cur_idx - 1; - auto& last_component = final_components[last_idx]; - - if ( last_component == "/" || last_component == "~" || - last_component == ".." ) - continue; - - if ( last_component == "." ) - { - last_component = ".."; - final_components.pop_back(); - } - else - { - final_components.pop_back(); - final_components.pop_back(); - } - } - } - } - - for ( auto it = final_components.begin(); it != final_components.end(); ++it ) - { - new_path.append(*it); - new_path.append("/"); - } - - if ( new_path.size() > 1 && new_path[new_path.size() - 1] == '/' ) - new_path.erase(new_path.size() - 1); - - return new_path; - } - -string without_zeekpath_component(std::string_view path) - { - string rval = normalize_path(path); - - const auto paths = tokenize_string(zeek_path(), ':'); - - for ( size_t i = 0; i < paths.size(); ++i ) - { - string common = normalize_path(paths[i]); - - if ( rval.find(common) != 0 ) - continue; - - // Found the containing directory. - std::string_view v(rval); - v.remove_prefix(common.size()); - - // Remove leading path separators. - while ( !v.empty() && v.front() == '/' ) - v.remove_prefix(1); - - return std::string(v); - } - - return rval; - } - static string find_file_in_path(const string& filename, const string& path, const vector& opt_ext) { @@ -1747,38 +1969,6 @@ static string find_file_in_path(const string& filename, const string& path, return string(); } -std::string get_exe_path(const std::string& invocation) - { - if ( invocation.empty() ) - return ""; - - if ( invocation[0] == '/' || invocation[0] == '~' ) - // Absolute path - return invocation; - - if ( invocation.find('/') != std::string::npos ) - { - // Relative path - char cwd[PATH_MAX]; - - if ( ! getcwd(cwd, sizeof(cwd)) ) - { - fprintf(stderr, "failed to get current directory: %s\n", - strerror(errno)); - exit(1); - } - - return std::string(cwd) + "/" + invocation; - } - - auto path = getenv("PATH"); - - if ( ! path ) - return ""; - - return find_file(invocation, path); - } - string find_file(const string& filename, const string& path_set, const string& opt_ext) { @@ -1805,7 +1995,7 @@ string find_script_file(const string& filename, const string& path_set) vector paths; tokenize_string(path_set, ":", &paths); - vector ext(script_extensions.begin(), script_extensions.end()); + vector ext(detail::script_extensions.begin(), detail::script_extensions.end()); for ( size_t n = 0; n < paths.size(); ++n ) { @@ -1813,14 +2003,14 @@ string find_script_file(const string& filename, const string& path_set) if ( ! f.empty() ) { - warn_if_legacy_script(f); + detail::warn_if_legacy_script(f); return f; } } if ( ends_with(filename, ".bro") ) { - warn_if_legacy_script(filename); + detail::warn_if_legacy_script(filename); // We were looking for a file explicitly ending in .bro and didn't // find it, so fall back to one ending in .zeek, if it exists. @@ -1831,180 +2021,8 @@ string find_script_file(const string& filename, const string& path_set) return string(); } -FILE* rotate_file(const char* name, zeek::RecordVal* rotate_info) - { - // Build file names. - const int buflen = strlen(name) + 128; - - auto newname_buf = std::make_unique(buflen); - auto tmpname_buf = std::make_unique(buflen + 4); - auto newname = newname_buf.get(); - auto tmpname = tmpname_buf.get(); - - snprintf(newname, buflen, "%s.%d.%.06f.tmp", - name, getpid(), zeek::net::network_time); - newname[buflen-1] = '\0'; - strcpy(tmpname, newname); - strcat(tmpname, ".tmp"); - - // First open the new file using a temporary name. - FILE* newf = fopen(tmpname, "w"); - if ( ! newf ) - { - zeek::reporter->Error("rotate_file: can't open %s: %s", tmpname, strerror(errno)); - return nullptr; - } - - // Then move old file to ".." and make sure - // it really gets created. - struct stat dummy; - if ( link(name, newname) < 0 || stat(newname, &dummy) < 0 ) - { - zeek::reporter->Error("rotate_file: can't move %s to %s: %s", name, newname, strerror(errno)); - fclose(newf); - unlink(newname); - unlink(tmpname); - return nullptr; - } - - // Close current file, and move the tmp to its place. - if ( unlink(name) < 0 || link(tmpname, name) < 0 || unlink(tmpname) < 0 ) - { - zeek::reporter->Error("rotate_file: can't move %s to %s: %s", tmpname, name, strerror(errno)); - exit(1); // hard to fix, but shouldn't happen anyway... - } - - // Init rotate_info. - if ( rotate_info ) - { - rotate_info->Assign(0, name); - rotate_info->Assign(1, newname); - rotate_info->Assign(2, zeek::net::network_time); - rotate_info->Assign(3, zeek::net::network_time); - } - - return newf; - } - -const char* log_file_name(const char* tag) - { - const char* env = zeekenv("ZEEK_LOG_SUFFIX"); - return fmt("%s.%s", tag, (env ? env : "log")); - } - -double parse_rotate_base_time(const char* rotate_base_time) - { - double base = -1; - - if ( rotate_base_time && rotate_base_time[0] != '\0' ) - { - struct tm t; - if ( ! strptime(rotate_base_time, "%H:%M", &t) ) - zeek::reporter->Error("calc_next_rotate(): can't parse rotation base time"); - else - base = t.tm_min * 60 + t.tm_hour * 60 * 60; - } - - return base; - } - -double calc_next_rotate(double current, double interval, double base) - { - if ( ! interval ) - { - zeek::reporter->Error("calc_next_rotate(): interval is zero, falling back to 24hrs"); - interval = 86400; - } - - // Calculate start of day. - time_t teatime = time_t(current); - - struct tm t; - if ( ! localtime_r(&teatime, &t) ) - { - zeek::reporter->Error("calc_next_rotate(): failure processing current time (%.6f)", current); - - // fall back to the method used if no base time is given - base = -1; - } - - if ( base < 0 ) - // No base time given. To get nice timestamps, we round - // the time up to the next multiple of the rotation interval. - return floor(current / interval) * interval - + interval - current; - - t.tm_hour = t.tm_min = t.tm_sec = 0; - double startofday = mktime(&t); - - // current < startofday + base + i * interval <= current + interval - return startofday + base + - ceil((current - startofday - base) / interval) * interval - - current; - } - - RETSIGTYPE sig_handler(int signo); -void terminate_processing() - { - if ( ! zeek::net::terminating ) - raise(SIGTERM); - } - -void set_processing_status(const char* status, const char* reason) - { - if ( ! proc_status_file ) - return; - - // This function can be called from a signal context, so we have to - // make sure to only call reentrant functions and to restore errno - // afterwards. - - int old_errno = errno; - - int fd = open(proc_status_file, O_CREAT | O_WRONLY | O_TRUNC, 0700); - - if ( fd < 0 ) - { - char buf[256]; - zeek_strerror_r(errno, buf, sizeof(buf)); - if ( zeek::reporter ) - zeek::reporter->Error("Failed to open process status file '%s': %s", - proc_status_file, buf); - else - fprintf(stderr, "Failed to open process status file '%s': %s\n", - proc_status_file, buf); - errno = old_errno; - return; - } - - auto write_str = [](int fd, const char* s) - { - int len = strlen(s); - while ( len ) - { - int n = write(fd, s, len); - - if ( n < 0 && errno != EINTR && errno != EAGAIN ) - // Ignore errors, as they're too difficult to - // safely report here. - break; - - s += n; - len -= n; - } - }; - - write_str(fd, status); - write_str(fd, " ["); - write_str(fd, reason); - write_str(fd, "]\n"); - safe_close(fd); - - errno = old_errno; - } - double current_time(bool real) { struct timeval tv; @@ -2085,7 +2103,7 @@ uint64_t calculate_unique_id(size_t pool) if ( uid_pool[pool].needs_init ) { // This is the first time we need a UID for this pool. - if ( ! have_random_seed() ) + if ( ! detail::have_random_seed() ) { // If we don't need deterministic output (as // indicated by a set seed), we calculate the @@ -2105,7 +2123,7 @@ uint64_t calculate_unique_id(size_t pool) gettimeofday(&unique.time, 0); unique.pool = (uint64_t) pool; unique.pid = getpid(); - unique.rnd = static_cast(zeek::util::random_number()); + unique.rnd = static_cast(detail::random_number()); uid_instance = zeek::detail::HashKey::HashBytes(&unique, sizeof(unique)); ++uid_instance; // Now it's larger than zero. @@ -2509,34 +2527,19 @@ string json_escape_utf8(const string& val) return result; } -void set_thread_name(const char* name, pthread_t tid) - { -#ifdef HAVE_LINUX - prctl(PR_SET_NAME, name, 0, 0, 0); -#endif - -#ifdef __APPLE__ - pthread_setname_np(name); -#endif - -#ifdef __FreeBSD__ - pthread_set_name_np(tid, name); -#endif - } - } // namespace zeek::util // Remove in v4.1. double& network_time = zeek::net::network_time; unsigned int bro_prng(unsigned int state) - { return zeek::util::prng(state); } + { return zeek::util::detail::prng(state); } long int bro_random() - { return zeek::util::random_number(); } + { return zeek::util::detail::random_number(); } void bro_srandom(unsigned int seed) - { zeek::util::seed_random(seed); } + { zeek::util::detail::seed_random(seed); } zeek::ODesc* get_escaped_string(zeek::ODesc* d, const char* str, size_t len, bool escape_all) { return zeek::util::get_escaped_string(d, str, len, escape_all); } @@ -2580,7 +2583,7 @@ std::string implode_string_vector(const std::vector& v, { return zeek::util::implode_string_vector(v, delim); } std::string flatten_script_name(const std::string& name, const std::string& prefix) - { return zeek::util::flatten_script_name(name, prefix); } + { return zeek::util::detail::flatten_script_name(name, prefix); } std::string find_file(const std::string& filename, const std::string& path_set, const std::string& opt_ext) @@ -2588,7 +2591,7 @@ std::string find_file(const std::string& filename, const std::string& path_set, FILE* open_file(const std::string& path, const std::string& mode) { return zeek::util::open_file(path, mode); } FILE* open_package(std::string& path, const std::string& mode) - { return zeek::util::open_package(path, mode); } + { return zeek::util::detail::open_package(path, mode); } double current_time(bool real) { return zeek::util::current_time(real); } @@ -2598,12 +2601,12 @@ uint64_t calculate_unique_id() uint64_t calculate_unique_id(const size_t pool) { return zeek::util::calculate_unique_id(pool); } -const array& script_extensions = zeek::util::script_extensions; +const array& script_extensions = zeek::util::detail::script_extensions; namespace zeek { void set_thread_name(const char* name, pthread_t tid) - { zeek::util::set_thread_name(name, tid); } + { zeek::util::detail::set_thread_name(name, tid); } } // namespace zeek diff --git a/src/util.h b/src/util.h index bc19b35b44..dcde78e014 100644 --- a/src/util.h +++ b/src/util.h @@ -50,7 +50,6 @@ extern HeapLeakChecker* heap_checker; #endif -#include #include #ifdef HAVE_LINUX @@ -140,96 +139,21 @@ inline int safe_vsnprintf(char* str, size_t size, const char* format, va_list al extern "C" void out_of_memory(const char* where); namespace zeek::util { - -template -void delete_each(T* t) - { - typedef typename T::iterator iterator; - for ( iterator it = t->begin(); it != t->end(); ++it ) - delete *it; - } +namespace detail { std::string extract_ip(const std::string& i); std::string extract_ip_and_len(const std::string& i, int* len); -inline void bytetohex(unsigned char byte, char* hex_out) - { - static constexpr char hex_chars[] = "0123456789abcdef"; - hex_out[0] = hex_chars[(byte & 0xf0) >> 4]; - hex_out[1] = hex_chars[byte & 0x0f]; - } - -std::string get_unescaped_string(const std::string& str); - -zeek::ODesc* get_escaped_string(zeek::ODesc* d, const char* str, size_t len, - bool escape_all); -std::string get_escaped_string(const char* str, size_t len, bool escape_all); - -inline std::string get_escaped_string(const std::string& str, bool escape_all) - { - return get_escaped_string(str.data(), str.length(), escape_all); - } - -std::vector* tokenize_string(std::string_view input, - std::string_view delim, - std::vector* rval = nullptr, int limit = 0); - -std::vector tokenize_string(std::string_view input, const char delim) noexcept; - -extern char* copy_string(const char* s); -extern int streq(const char* s1, const char* s2); - // Returns the character corresponding to the given escape sequence (s points // just past the '\'), and updates s to point just beyond the last character // of the sequence. extern int expand_escape(const char*& s); -extern char* skip_whitespace(char* s); -extern const char* skip_whitespace(const char* s); -extern char* skip_whitespace(char* s, char* end_of_s); -extern const char* skip_whitespace(const char* s, const char* end_of_s); -extern char* skip_digits(char* s); -extern char* get_word(char*& s); -extern void get_word(int length, const char* s, int& pwlen, const char*& pw); -extern void to_upper(char* s); -extern std::string to_upper(const std::string& s); -extern int decode_hex(char ch); -extern unsigned char encode_hex(int h); -extern const char* strpbrk_n(size_t len, const char* s, const char* charset); -template int atoi_n(int len, const char* s, const char** end, int base, T& result); -extern char* uitoa_n(uint64_t value, char* str, int n, int base, const char* prefix=nullptr); -int strstr_n(const int big_len, const unsigned char* big, - const int little_len, const unsigned char* little); -extern int fputs(int len, const char* s, FILE* fp); -extern bool is_printable(const char* s, int len); - -// Return a lower-cased version of the string. -extern std::string strtolower(const std::string& s); - -extern const char* fmt_bytes(const char* data, int len); - -// Note: returns a pointer into a shared buffer. -extern const char* vfmt(const char* format, va_list args); -// Note: returns a pointer into a shared buffer. -extern const char* fmt(const char* format, ...) - __attribute__((format (printf, 1, 2))); extern const char* fmt_access_time(double time); extern bool ensure_intermediate_dirs(const char* dirname); extern bool ensure_dir(const char *dirname); -// Returns true if path exists and is a directory. -bool is_dir(const std::string& path); - -// Returns true if path exists and is a file. -bool is_file(const std::string& path); - -// Replaces all occurences of *o* in *s* with *n*. -extern std::string strreplace(const std::string& s, const std::string& o, const std::string& n); - -// Remove all leading and trailing white space from string. -extern std::string strstrip(std::string s); - extern void hmac_md5(size_t size, const unsigned char* bytes, unsigned char digest[16]); @@ -251,6 +175,46 @@ extern bool have_random_seed(); extern uint64_t rand64bit(); +/** + * A platform-independent PRNG implementation. Note that this is not + * necessarily a "statistically sound" implementation as the main purpose is + * not for production use, but rather for regression testing. + * @param state The value used to generate the next random number. + * @return A new random value generated from *state* and that can passed + * back into subsequent calls to generate further random numbers. + */ +long int prng(long int state); + +/** + * Wrapper for system random() in the default case, but when running in + * deterministic mode, uses the platform-independent zeek::prng() + * to obtain consistent results since implementations of rand() may vary. + * @return A value in the range [0, zeek::max_random()]. + */ +long int random_number(); + +/** + * @return The maximum value that can be returned from zeek::random_number(). + * When not using deterministic-mode, this is always equivalent to RAND_MAX. + */ +long int max_random(); + +/** + * Wrapper for system srandom() in the default case, but when running in + * deterministic mode, updates the state used for calling zeek::prng() + * inside of zeek::random_number(). + * @param seed Value to use for initializing the PRNG. + */ +void seed_random(unsigned int seed); + +/** + * Set the process/thread name. May not be supported on all OSs. + * @param name new name for the process/thread. OS limitations typically + * truncate the name to 15 bytes maximum. + * @param tid handle of thread whose name shall change + */ +void set_thread_name(const char* name, pthread_t tid = pthread_self()); + // Each event source that may generate events gets an internally unique ID. // This is always LOCAL for a local Bro. For remote event sources, it gets // assigned by the RemoteSerializer. @@ -261,23 +225,14 @@ extern uint64_t rand64bit(); using SourceID = std::uintptr_t; #define PRI_SOURCE_ID PRI_PTR_COMPAT_UINT -static const SourceID SOURCE_LOCAL = 0; +constexpr SourceID SOURCE_LOCAL = 0; // TODO: This is a temporary marker to flag events coming in via Broker. // Those are remote events but we don't have any further peer informationa // available for them (as the old communication code would have). Once we // remove RemoteSerializer, we can turn the SourceID into a simple boolean // indicating whether it's a local or remote event. -static const SourceID SOURCE_BROKER = 0xffffffff; - -extern void pinpoint(); -extern int int_list_cmp(const void* v1, const void* v2); - -extern const std::string& zeek_path(); -extern const char* zeek_magic_path(); -extern const char* zeek_plugin_path(); -extern const char* zeek_plugin_activate(); -extern std::string zeek_prefixes(); +constexpr SourceID SOURCE_BROKER = 0xffffffff; extern const std::array script_extensions; @@ -288,7 +243,6 @@ bool is_package_loader(const std::string& path); extern void add_to_zeek_path(const std::string& dir); - /** * Wrapper class for functions like dirname(3) or basename(3) that won't * modify the path argument and may optionally abort execution on error. @@ -309,31 +263,6 @@ protected: }; -class SafeDirname : public SafePathOp { -public: - - explicit SafeDirname(const char* path, bool error_aborts = true); - explicit SafeDirname(const std::string& path, bool error_aborts = true); - -private: - - void DoFunc(const std::string& path, bool error_aborts = true); -}; - -class SafeBasename : public SafePathOp { -public: - - explicit SafeBasename(const char* path, bool error_aborts = true); - explicit SafeBasename(const std::string& path, bool error_aborts = true); - -private: - - void DoFunc(const std::string& path, bool error_aborts = true); -}; - -std::string implode_string_vector(const std::vector& v, - const std::string& delim = "\n"); - /** * Flatten a script name by replacing '/' path separators with '.'. * @param file A path to a Zeek script. If it is a __load__.zeek, that part @@ -367,6 +296,161 @@ std::string without_zeekpath_component(std::string_view path); */ std::string get_exe_path(const std::string& invocation); +/** Opens a Zeek script package. + * @param path Location of a Zeek script package (a directory). Will be changed + * to the path of the package's loader script. + * @param mode An fopen(3) mode. + * @return The return value of fopen(3) on the loader script or null if one + * doesn't exist. + */ +FILE* open_package(std::string& path, const std::string& mode = "r"); + +// This mimics the script-level function with the same name. +const char* log_file_name(const char* tag); + +// Terminates processing gracefully, similar to pressing CTRL-C. +void terminate_processing(); + +// Sets the current status of the Zeek process to the given string. +// If the option --status-file has been set, this is written into +// the the corresponding file. Otherwise, the function is a no-op. +void set_processing_status(const char* status, const char* reason); + +// Renames the given file to a new temporary name, and opens a new file with +// the original name. Returns new file or NULL on error. Inits rotate_info if +// given (open time is set network time). +extern FILE* rotate_file(const char* name, zeek::RecordVal* rotate_info); + +// Parse a time string of the form "HH:MM" (as used for the rotation base +// time) into a double representing the number of seconds. Returns -1 if the +// string cannot be parsed. The function's result is intended to be used with +// calc_next_rotate(). +// +// This function is not thread-safe. +double parse_rotate_base_time(const char* rotate_base_time); + +// Calculate the duration until the next time a file is to be rotated, based +// on the given rotate_interval and rotate_base_time. 'current' the the +// current time to be used as base, 'rotate_interval' the rotation interval, +// and 'base' the value returned by parse_rotate_base_time(). For the latter, +// if the function returned -1, that's fine, calc_next_rotate() handles that. +// +// This function is thread-safe. +double calc_next_rotate(double current, double rotate_interval, double base); + +} // namespace detail + +template +void delete_each(T* t) + { + typedef typename T::iterator iterator; + for ( iterator it = t->begin(); it != t->end(); ++it ) + delete *it; + } + +inline void bytetohex(unsigned char byte, char* hex_out) + { + static constexpr char hex_chars[] = "0123456789abcdef"; + hex_out[0] = hex_chars[(byte & 0xf0) >> 4]; + hex_out[1] = hex_chars[byte & 0x0f]; + } + +std::string get_unescaped_string(const std::string& str); + +zeek::ODesc* get_escaped_string(zeek::ODesc* d, const char* str, size_t len, + bool escape_all); +std::string get_escaped_string(const char* str, size_t len, bool escape_all); + +inline std::string get_escaped_string(const std::string& str, bool escape_all) + { + return get_escaped_string(str.data(), str.length(), escape_all); + } + +std::vector* tokenize_string(std::string_view input, + std::string_view delim, + std::vector* rval = nullptr, int limit = 0); + +std::vector tokenize_string(std::string_view input, const char delim) noexcept; + +extern char* copy_string(const char* s); +extern int streq(const char* s1, const char* s2); + +extern char* skip_whitespace(char* s); +extern const char* skip_whitespace(const char* s); +extern char* skip_whitespace(char* s, char* end_of_s); +extern const char* skip_whitespace(const char* s, const char* end_of_s); +extern char* skip_digits(char* s); +extern char* get_word(char*& s); +extern void get_word(int length, const char* s, int& pwlen, const char*& pw); +extern void to_upper(char* s); +extern std::string to_upper(const std::string& s); +extern int decode_hex(char ch); +extern unsigned char encode_hex(int h); +template int atoi_n(int len, const char* s, const char** end, int base, T& result); +extern char* uitoa_n(uint64_t value, char* str, int n, int base, const char* prefix=nullptr); +extern const char* strpbrk_n(size_t len, const char* s, const char* charset); +int strstr_n(const int big_len, const unsigned char* big, + const int little_len, const unsigned char* little); + +// Replaces all occurences of *o* in *s* with *n*. +extern std::string strreplace(const std::string& s, const std::string& o, const std::string& n); + +// Remove all leading and trailing white space from string. +extern std::string strstrip(std::string s); + +// Return a lower-cased version of the string. +extern std::string strtolower(const std::string& s); + +extern int fputs(int len, const char* s, FILE* fp); +extern bool is_printable(const char* s, int len); + +extern const char* fmt_bytes(const char* data, int len); + +// Note: returns a pointer into a shared buffer. +extern const char* vfmt(const char* format, va_list args); +// Note: returns a pointer into a shared buffer. +extern const char* fmt(const char* format, ...) + __attribute__((format (printf, 1, 2))); + +// Returns true if path exists and is a directory. +bool is_dir(const std::string& path); + +// Returns true if path exists and is a file. +bool is_file(const std::string& path); + +extern int int_list_cmp(const void* v1, const void* v2); + +extern const std::string& zeek_path(); +extern const char* zeek_magic_path(); +extern const char* zeek_plugin_path(); +extern const char* zeek_plugin_activate(); +extern std::string zeek_prefixes(); + +class SafeDirname : public detail::SafePathOp { +public: + + explicit SafeDirname(const char* path, bool error_aborts = true); + explicit SafeDirname(const std::string& path, bool error_aborts = true); + +private: + + void DoFunc(const std::string& path, bool error_aborts = true); +}; + +class SafeBasename : public detail::SafePathOp { +public: + + explicit SafeBasename(const char* path, bool error_aborts = true); + explicit SafeBasename(const std::string& path, bool error_aborts = true); + +private: + + void DoFunc(const std::string& path, bool error_aborts = true); +}; + +std::string implode_string_vector(const std::vector& v, + const std::string& delim = "\n"); + /** * Locate a file within a given search path. * @param filename Name of a file to find. @@ -388,48 +472,6 @@ std::string find_script_file(const std::string& filename, const std::string& pat // Wrapper around fopen(3). Emits an error when failing to open. FILE* open_file(const std::string& path, const std::string& mode = "r"); -/** Opens a Zeek script package. - * @param path Location of a Zeek script package (a directory). Will be changed - * to the path of the package's loader script. - * @param mode An fopen(3) mode. - * @return The return value of fopen(3) on the loader script or null if one - * doesn't exist. - */ -FILE* open_package(std::string& path, const std::string& mode = "r"); - -// Renames the given file to a new temporary name, and opens a new file with -// the original name. Returns new file or NULL on error. Inits rotate_info if -// given (open time is set network time). -extern FILE* rotate_file(const char* name, zeek::RecordVal* rotate_info); - -// This mimics the script-level function with the same name. -const char* log_file_name(const char* tag); - -// Parse a time string of the form "HH:MM" (as used for the rotation base -// time) into a double representing the number of seconds. Returns -1 if the -// string cannot be parsed. The function's result is intended to be used with -// calc_next_rotate(). -// -// This function is not thread-safe. -double parse_rotate_base_time(const char* rotate_base_time); - -// Calculate the duration until the next time a file is to be rotated, based -// on the given rotate_interval and rotate_base_time. 'current' the the -// current time to be used as base, 'rotate_interval' the rotation interval, -// and 'base' the value returned by parse_rotate_base_time(). For the latter, -// if the function returned -1, that's fine, calc_next_rotate() handles that. -// -// This function is thread-safe. -double calc_next_rotate(double current, double rotate_interval, double base); - -// Terminates processing gracefully, similar to pressing CTRL-C. -void terminate_processing(); - -// Sets the current status of the Zeek process to the given string. -// If the option --status-file has been set, this is written into -// the the corresponding file. Otherwise, the function is a no-op. -void set_processing_status(const char* status, const char* reason); - // Returns the current time. // (In pseudo-realtime mode this is faked to be the start time of the // trace plus the time interval Zeek has been running. To avoid this, @@ -452,17 +494,6 @@ extern int time_compare(struct timeval* tv_a, struct timeval* tv_b); extern uint64_t calculate_unique_id(); extern uint64_t calculate_unique_id(const size_t pool); -// For now, don't use hash_maps - they're not fully portable. -#if 0 -// Use for hash_map's string keys. -struct eqstr { - bool operator()(const char* s1, const char* s2) const - { - return strcmp(s1, s2) == 0; - } -}; -#endif - // Use for map's string keys. struct ltstr { bool operator()(const char* s1, const char* s2) const @@ -471,8 +502,6 @@ struct ltstr { } }; -// Versions of realloc/malloc which abort() on out of memory - constexpr size_t pad_size(size_t size) { // We emulate glibc here (values measured on Linux i386). @@ -503,6 +532,8 @@ extern void safe_close(int fd); // Versions of realloc/malloc which abort() on out of memory +// Versions of realloc/malloc which abort() on out of memory + inline void* safe_realloc(void* ptr, size_t size) { ptr = realloc(ptr, size); @@ -571,46 +602,6 @@ char* zeekenv(const char* name); */ std::string json_escape_utf8(const std::string& val); -/** - * Set the process/thread name. May not be supported on all OSs. - * @param name new name for the process/thread. OS limitations typically - * truncate the name to 15 bytes maximum. - * @param tid handle of thread whose name shall change - */ -void set_thread_name(const char* name, pthread_t tid = pthread_self()); - -/** - * A platform-independent PRNG implementation. Note that this is not - * necessarily a "statistically sound" implementation as the main purpose is - * not for production use, but rather for regression testing. - * @param state The value used to generate the next random number. - * @return A new random value generated from *state* and that can passed - * back into subsequent calls to generate further random numbers. - */ -long int prng(long int state); - -/** - * Wrapper for system random() in the default case, but when running in - * deterministic mode, uses the platform-independent zeek::prng() - * to obtain consistent results since implementations of rand() may vary. - * @return A value in the range [0, zeek::max_random()]. - */ -long int random_number(); - -/** - * @return The maximum value that can be returned from zeek::random_number(). - * When not using deterministic-mode, this is always equivalent to RAND_MAX. - */ -long int max_random(); - -/** - * Wrapper for system srandom() in the default case, but when running in - * deterministic mode, updates the state used for calling zeek::prng() - * inside of zeek::random_number(). - * @param seed Value to use for initializing the PRNG. - */ -void seed_random(unsigned int seed); - } // namespace zeek::util // A simple linear congruence PRNG. It takes its state as argument and @@ -632,8 +623,8 @@ template [[ deprecated("Remove in v4.1. Use zeek::util::delete_each.")]] void delete_each(T* t) { zeek::util::delete_each(t); } -constexpr auto extract_ip [[deprecated("Remove in v4.1. Use zeek::util::extract_ip.")]] = zeek::util::extract_ip; -constexpr auto extract_ip_and_len [[deprecated("Remove in v4.1. Use zeek::util::extract_ip_and_len.")]] = zeek::util::extract_ip_and_len; +constexpr auto extract_ip [[deprecated("Remove in v4.1. Use zeek::util::detail::extract_ip.")]] = zeek::util::detail::extract_ip; +constexpr auto extract_ip_and_len [[deprecated("Remove in v4.1. Use zeek::util::detail::extract_ip_and_len.")]] = zeek::util::detail::extract_ip_and_len; constexpr auto bytetohex [[deprecated("Remove in v4.1. Use zeek::util::bytetohex.")]] = zeek::util::bytetohex; constexpr auto get_unescaped_string [[deprecated("Remove in v4.1. Use zeek::util::get_unescaped_string.")]] = zeek::util::get_unescaped_string; @@ -652,7 +643,7 @@ std::vector tokenize_string(std::string_view input, const char constexpr auto copy_string [[deprecated("Remove in v4.1. Use zeek::util::copy_string.")]] = zeek::util::copy_string; constexpr auto streq [[deprecated("Remove in v4.1. Use zeek::util::streq.")]] = zeek::util::streq; -constexpr auto expand_escape [[deprecated("Remove in v4.1. Use zeek::util::expand_escape.")]] = zeek::util::expand_escape; +constexpr auto expand_escape [[deprecated("Remove in v4.1. Use zeek::util::detail::expand_escape.")]] = zeek::util::detail::expand_escape; constexpr auto skip_digits [[deprecated("Remove in v4.1. Use zeek::util::skip_digits.")]] = zeek::util::skip_digits; [[deprecated("Remove in v4.1. Use zeek::util::skip_whitespace.")]] @@ -677,6 +668,8 @@ constexpr auto decode_hex [[deprecated("Remove in v4.1. Use zeek::util::decode_h constexpr auto encode_hex [[deprecated("Remove in v4.1. Use zeek::util::encode_hex.")]] = zeek::util::encode_hex; constexpr auto strpbrk_n [[deprecated("Remove in v4.1. Use zeek::util::strpbrk_n.")]] = zeek::util::strpbrk_n; constexpr auto strstr_n [[deprecated("Remove in v4.1. Use zeek::util::strstr_n.")]] = zeek::util::strstr_n; +constexpr auto strreplace [[deprecated("Remove in v4.1. Use zeek::util::strreplace.")]] = zeek::util::strreplace; +constexpr auto strstrip [[deprecated("Remove in v4.1. Use zeek::util::strstrip.")]] = zeek::util::strstrip; template [[deprecated("Remove in v4.1. Use zeek::util::atoi_n.")]] @@ -694,24 +687,21 @@ constexpr auto strtolower [[deprecated("Remove in v4.1. Use zeek::util::strtolow constexpr auto fmt_bytes [[deprecated("Remove in v4.1. Use zeek::util::fmt_bytes.")]] = zeek::util::fmt_bytes; constexpr auto vfmt [[deprecated("Remove in v4.1. Use zeek::util::vfmt.")]] = zeek::util::vfmt; constexpr auto fmt [[deprecated("Remove in v4.1. Use zeek::util::fmt.")]] = zeek::util::fmt; -constexpr auto fmt_access_time [[deprecated("Remove in v4.1. Use zeek::util::fmt_access_time.")]] = zeek::util::fmt_access_time; -constexpr auto ensure_intermediate_dirs [[deprecated("Remove in v4.1. Use zeek::util::ensure_intermediate_dirs.")]] = zeek::util::ensure_intermediate_dirs; -constexpr auto ensure_dir [[deprecated("Remove in v4.1. Use zeek::util::ensure_dir.")]] = zeek::util::ensure_dir; +constexpr auto fmt_access_time [[deprecated("Remove in v4.1. Use zeek::util::detail::fmt_access_time.")]] = zeek::util::detail::fmt_access_time; +constexpr auto ensure_intermediate_dirs [[deprecated("Remove in v4.1. Use zeek::util::detail::ensure_intermediate_dirs.")]] = zeek::util::detail::ensure_intermediate_dirs; +constexpr auto ensure_dir [[deprecated("Remove in v4.1. Use zeek::util::detail::ensure_dir.")]] = zeek::util::detail::ensure_dir; constexpr auto is_dir [[deprecated("Remove in v4.1. Use zeek::util::is_dir.")]] = zeek::util::is_dir; constexpr auto is_file [[deprecated("Remove in v4.1. Use zeek::util::is_file.")]] = zeek::util::is_file; -constexpr auto strreplace [[deprecated("Remove in v4.1. Use zeek::util::strreplace.")]] = zeek::util::strreplace; -constexpr auto strstrip [[deprecated("Remove in v4.1. Use zeek::util::strstrip.")]] = zeek::util::strstrip; -constexpr auto hmac_md5 [[deprecated("Remove in v4.1. Use zeek::util::hmac_md5.")]] = zeek::util::hmac_md5; -constexpr auto init_random_seed [[deprecated("Remove in v4.1. Use zeek::util::init_random_seed.")]] = zeek::util::init_random_seed; -constexpr auto initial_seed [[deprecated("Remove in v4.1. Use zeek::util::initial_seed.")]] = zeek::util::initial_seed; -constexpr auto have_random_seed [[deprecated("Remove in v4.1. Use zeek::util::have_random_seed.")]] = zeek::util::have_random_seed; -constexpr auto rand64bit [[deprecated("Remove in v4.1. Use zeek::util::rand64bit.")]] = zeek::util::rand64bit; +constexpr auto hmac_md5 [[deprecated("Remove in v4.1. Use zeek::util::detail::hmac_md5.")]] = zeek::util::detail::hmac_md5; +constexpr auto init_random_seed [[deprecated("Remove in v4.1. Use zeek::util::detail::init_random_seed.")]] = zeek::util::detail::init_random_seed; +constexpr auto initial_seed [[deprecated("Remove in v4.1. Use zeek::util::detail::initial_seed.")]] = zeek::util::detail::initial_seed; +constexpr auto have_random_seed [[deprecated("Remove in v4.1. Use zeek::util::detail::have_random_seed.")]] = zeek::util::detail::have_random_seed; +constexpr auto rand64bit [[deprecated("Remove in v4.1. Use zeek::util::detail::rand64bit.")]] = zeek::util::detail::rand64bit; -using SourceID [[deprecated("Remove in v4.1. Use zeek::util::SourceID.")]] = zeek::util::SourceID; -static const zeek::util::SourceID SOURCE_LOCAL [[deprecated("Remove in v4.1. Use zeek::util::SOURCE_LOCAL.")]] = zeek::util::SOURCE_LOCAL; -static const zeek::util::SourceID SOURCE_BROKER [[deprecated("Remove in v4.1. Use zeek::util::SOURCE_BROKER.")]] = zeek::util::SOURCE_BROKER; +using SourceID [[deprecated("Remove in v4.1. Use zeek::util::detail::SourceID.")]] = zeek::util::detail::SourceID; +static const zeek::util::detail::SourceID SOURCE_LOCAL [[deprecated("Remove in v4.1. Use zeek::util::detail::SOURCE_LOCAL.")]] = zeek::util::detail::SOURCE_LOCAL; +static const zeek::util::detail::SourceID SOURCE_BROKER [[deprecated("Remove in v4.1. Use zeek::util::detail::SOURCE_BROKER.")]] = zeek::util::detail::SOURCE_BROKER; -constexpr auto pinpoint [[deprecated("Remove in v4.1. Use zeek::util::pinpoint.")]] = zeek::util::pinpoint; constexpr auto int_list_cmp [[deprecated("Remove in v4.1. Use zeek::util::int_list_cmp.")]] = zeek::util::int_list_cmp; constexpr auto bro_path [[deprecated("Remove in v4.1. Use zeek::util::zeek_path.")]] = zeek::util::zeek_path; constexpr auto bro_magic_path [[deprecated("Remove in v4.1. Use zeek::util::zeek_magic_path.")]] = zeek::util::zeek_magic_path; @@ -719,13 +709,13 @@ constexpr auto bro_plugin_path [[deprecated("Remove in v4.1. Use zeek::util::zee constexpr auto bro_plugin_activate [[deprecated("Remove in v4.1. Use zeek::util::zeek_plugin_activate.")]] = zeek::util::zeek_plugin_activate; constexpr auto bro_prefixes [[deprecated("Remove in v4.1. Use zeek::util::zeek_prefixes.")]] = zeek::util::zeek_prefixes; -extern const std::array& script_extensions [[deprecated("Remove in v4.1. Use zeek::util::script_extensions.")]]; +extern const std::array& script_extensions [[deprecated("Remove in v4.1. Use zeek::util::detail::script_extensions.")]]; -constexpr auto warn_if_legacy_script [[deprecated("Remove in v4.1. Use zeek::util::warn_if_legacy_script.")]] = zeek::util::warn_if_legacy_script; -constexpr auto is_package_loader [[deprecated("Remove in v4.1. Use zeek::util::is_package_loader.")]] = zeek::util::is_package_loader; -constexpr auto add_to_bro_path [[deprecated("Remove in v4.1. Use zeek::util::add_to_zeek_path.")]] = zeek::util::add_to_zeek_path; +constexpr auto warn_if_legacy_script [[deprecated("Remove in v4.1. Use zeek::util::detail::warn_if_legacy_script.")]] = zeek::util::detail::warn_if_legacy_script; +constexpr auto is_package_loader [[deprecated("Remove in v4.1. Use zeek::util::detail::is_package_loader.")]] = zeek::util::detail::is_package_loader; +constexpr auto add_to_bro_path [[deprecated("Remove in v4.1. Use zeek::util::detail::add_to_zeek_path.")]] = zeek::util::detail::add_to_zeek_path; -using SafePathOp [[deprecated("Remove in v4.1. Use zeek::util::SafePathOp.")]] = zeek::util::SafePathOp; +using SafePathOp [[deprecated("Remove in v4.1. Use zeek::util::detail::SafePathOp.")]] = zeek::util::detail::SafePathOp; using SafeDirname [[deprecated("Remove in v4.1. Use zeek::util::SafeDirname.")]] = zeek::util::SafeDirname; using SafeBasename [[deprecated("Remove in v4.1. Use zeek::util::SafeBasename.")]] = zeek::util::SafeBasename; @@ -736,9 +726,9 @@ std::string implode_string_vector(const std::vector& v, std::string flatten_script_name(const std::string& name, const std::string& prefix = ""); -constexpr auto normalize_path [[deprecated("Remove in v4.1. Use zeek::util::normalize_path.")]] = zeek::util::normalize_path; -constexpr auto without_bropath_component [[deprecated("Remove in v4.1. Use zeek::util::without_zeekpath_component.")]] = zeek::util::without_zeekpath_component; -constexpr auto get_exe_path [[deprecated("Remove in v4.1. Use zeek::util::get_exe_path.")]] = zeek::util::get_exe_path; +constexpr auto normalize_path [[deprecated("Remove in v4.1. Use zeek::util::detail::normalize_path.")]] = zeek::util::detail::normalize_path; +constexpr auto without_bropath_component [[deprecated("Remove in v4.1. Use zeek::util::detail::without_zeekpath_component.")]] = zeek::util::detail::without_zeekpath_component; +constexpr auto get_exe_path [[deprecated("Remove in v4.1. Use zeek::util::detail::get_exe_path.")]] = zeek::util::detail::get_exe_path; constexpr auto find_script_file [[deprecated("Remove in v4.1. Use zeek::util::find_script_file.")]] = zeek::util::find_script_file; [[deprecated("Remove in v4.1. Use zeek::util::find_file.")]] @@ -749,12 +739,12 @@ FILE* open_file(const std::string& path, const std::string& mode = "r"); [[deprecated("Remove in v4.1. Use zeek::util::open_package.")]] FILE* open_package(std::string& path, const std::string& mode = "r"); -constexpr auto rotate_file [[deprecated("Remove in v4.1. Use zeek::util::rotate_file.")]] = zeek::util::rotate_file; -constexpr auto log_file_name [[deprecated("Remove in v4.1. Use zeek::util::log_file_name.")]] = zeek::util::log_file_name; -constexpr auto parse_rotate_base_time [[deprecated("Remove in v4.1. Use zeek::util::parse_rotate_base_time.")]] = zeek::util::parse_rotate_base_time; -constexpr auto calc_next_rotate [[deprecated("Remove in v4.1. Use zeek::util::calc_next_rotate.")]] = zeek::util::calc_next_rotate; -constexpr auto terminate_processing [[deprecated("Remove in v4.1. Use zeek::util::terminate_processing.")]] = zeek::util::terminate_processing; -constexpr auto set_processing_status [[deprecated("Remove in v4.1. Use zeek::util::set_processing_status.")]] = zeek::util::set_processing_status; +constexpr auto rotate_file [[deprecated("Remove in v4.1. Use zeek::util::detail::rotate_file.")]] = zeek::util::detail::rotate_file; +constexpr auto log_file_name [[deprecated("Remove in v4.1. Use zeek::util::detail::log_file_name.")]] = zeek::util::detail::log_file_name; +constexpr auto parse_rotate_base_time [[deprecated("Remove in v4.1. Use zeek::util::detail::parse_rotate_base_time.")]] = zeek::util::detail::parse_rotate_base_time; +constexpr auto calc_next_rotate [[deprecated("Remove in v4.1. Use zeek::util::detail::calc_next_rotate.")]] = zeek::util::detail::calc_next_rotate; +constexpr auto terminate_processing [[deprecated("Remove in v4.1. Use zeek::util::detail::terminate_processing.")]] = zeek::util::detail::terminate_processing; +constexpr auto set_processing_status [[deprecated("Remove in v4.1. Use zeek::util::detail::set_processing_status.")]] = zeek::util::detail::set_processing_status; [[deprecated("Remove in v4.1. Use zeek::util::current_time.")]] extern double current_time(bool real=false); @@ -786,8 +776,8 @@ namespace zeek { [[deprecated("Remove in v4.1. Use zeek::util::set_thread_name.")]] void set_thread_name(const char* name, pthread_t tid = pthread_self()); - constexpr auto prng [[deprecated("Remove in v4.1. Use zeek::util::prng.")]] = zeek::util::prng; - constexpr auto random_number [[deprecated("Remove in v4.1. Use zeek::util::random_number.")]] = zeek::util::random_number; - constexpr auto max_random [[deprecated("Remove in v4.1. Use zeek::util::max_random.")]] = zeek::util::max_random; - constexpr auto seed_random [[deprecated("Remove in v4.1. Use zeek::util::seed_random.")]] = zeek::util::seed_random; + constexpr auto prng [[deprecated("Remove in v4.1. Use zeek::util::detail::prng.")]] = zeek::util::detail::prng; + constexpr auto random_number [[deprecated("Remove in v4.1. Use zeek::util::detail::random_number.")]] = zeek::util::detail::random_number; + constexpr auto max_random [[deprecated("Remove in v4.1. Use zeek::util::detail::max_random.")]] = zeek::util::detail::max_random; + constexpr auto seed_random [[deprecated("Remove in v4.1. Use zeek::util::detail::seed_random.")]] = zeek::util::detail::seed_random; } diff --git a/src/zeek-setup.cc b/src/zeek-setup.cc index 70d0a119ed..4c38755a37 100644 --- a/src/zeek-setup.cc +++ b/src/zeek-setup.cc @@ -233,7 +233,7 @@ bool show_plugins(int level) void done_with_network() { - zeek::util::set_processing_status("TERMINATING", "done_with_network"); + zeek::util::detail::set_processing_status("TERMINATING", "done_with_network"); // Cancel any pending alarms (watchdog, in particular). (void) alarm(0); @@ -280,7 +280,7 @@ void done_with_network() void terminate_bro() { - zeek::util::set_processing_status("TERMINATING", "terminate_bro"); + zeek::util::detail::set_processing_status("TERMINATING", "terminate_bro"); zeek::net::terminating = true; @@ -342,7 +342,7 @@ namespace zeek::net::detail { void zeek_terminate_loop(const char* reason) { - zeek::util::set_processing_status("TERMINATING", reason); + zeek::util::detail::set_processing_status("TERMINATING", reason); zeek::reporter->Info("%s", reason); net_get_final_stats(); @@ -364,7 +364,7 @@ void zeek_terminate_loop(const char* reason) RETSIGTYPE sig_handler(int signo) { - zeek::util::set_processing_status("TERMINATING", "sig_handler"); + zeek::util::detail::set_processing_status("TERMINATING", "sig_handler"); signal_val = signo; if ( ! zeek::net::terminating ) @@ -375,7 +375,7 @@ RETSIGTYPE sig_handler(int signo) static void atexit_handler() { - zeek::util::set_processing_status("TERMINATED", "atexit"); + zeek::util::detail::set_processing_status("TERMINATED", "atexit"); } static void bro_new_handler() @@ -407,7 +407,7 @@ zeek::detail::SetupResult zeek::detail::setup(int argc, char** argv, ZEEK_LSAN_DISABLE(); std::set_new_handler(bro_new_handler); - auto zeek_exe_path = zeek::util::get_exe_path(argv[0]); + auto zeek_exe_path = zeek::util::detail::get_exe_path(argv[0]); if ( zeek_exe_path.empty() ) { @@ -487,7 +487,7 @@ zeek::detail::SetupResult zeek::detail::setup(int argc, char** argv, proc_status_file = zeek::util::copy_string(options.process_status_file->data()); atexit(atexit_handler); - zeek::util::set_processing_status("INITIALIZING", "main"); + zeek::util::detail::set_processing_status("INITIALIZING", "main"); zeek::net::zeek_start_time = zeek::util::current_time(true); @@ -521,9 +521,9 @@ zeek::detail::SetupResult zeek::detail::setup(int argc, char** argv, if ( options.random_seed_input_file ) seed_load_file = options.random_seed_input_file->data(); - zeek::util::init_random_seed((seed_load_file && *seed_load_file ? seed_load_file : nullptr), - options.random_seed_output_file ? options.random_seed_output_file->data() : nullptr, - options.deterministic_mode); + zeek::util::detail::init_random_seed((seed_load_file && *seed_load_file ? seed_load_file : nullptr), + options.random_seed_output_file ? options.random_seed_output_file->data() : nullptr, + options.deterministic_mode); // DEBUG_MSG("HMAC key: %s\n", md5_digest_print(shared_hmac_md5_key)); init_hash_function(); diff --git a/src/zeek.bif b/src/zeek.bif index 6d9cc79a22..3a8fefd20d 100644 --- a/src/zeek.bif +++ b/src/zeek.bif @@ -391,7 +391,7 @@ function terminate%(%): bool if ( zeek::net::terminating ) return zeek::val_mgr->False(); - zeek::util::terminate_processing(); + zeek::util::detail::terminate_processing(); return zeek::val_mgr->True(); %} @@ -930,7 +930,7 @@ function hrw_weight%(key_digest: count, site_id: count%): count ## provided by the OS. function rand%(max: count%): count %{ - auto result = bro_uint_t(double(max) * double(zeek::util::random_number()) / (zeek::util::max_random() + 1.0)); + auto result = bro_uint_t(double(max) * double(zeek::util::detail::random_number()) / (zeek::util::detail::max_random() + 1.0)); return zeek::val_mgr->Count(result); %} @@ -946,7 +946,7 @@ function rand%(max: count%): count ## provided by the OS. function srand%(seed: count%): any %{ - zeek::util::seed_random(seed); + zeek::util::detail::seed_random(seed); return nullptr; %} @@ -4712,7 +4712,7 @@ function rotate_file_by_name%(f: string%): rotate_info addl_pkt_dumper->Close(); } - FILE* file = zeek::util::rotate_file(f->CheckString(), info.get()); + FILE* file = zeek::util::detail::rotate_file(f->CheckString(), info.get()); if ( ! file ) { // Record indicating error. @@ -4750,8 +4750,8 @@ function calc_next_rotate%(i: interval%) : interval static auto log_rotate_base_time = zeek::id::find_val("log_rotate_base_time"); static auto base_time = log_rotate_base_time->AsString()->CheckString(); - double base = zeek::util::parse_rotate_base_time(base_time); - return zeek::make_intrusive(zeek::util::calc_next_rotate(zeek::net::network_time, i, base)); + double base = zeek::util::detail::parse_rotate_base_time(base_time); + return zeek::make_intrusive(zeek::util::detail::calc_next_rotate(zeek::net::network_time, i, base)); %} ## Returns the size of a given file. @@ -4999,7 +4999,7 @@ function uninstall_dst_net_filter%(snet: subnet%) : bool ## Returns: True if the last raised event came from a remote peer. function is_remote_event%(%) : bool %{ - return zeek::val_mgr->Bool(zeek::event_mgr.CurrentSource() != zeek::util::SOURCE_LOCAL); + return zeek::val_mgr->Bool(zeek::event_mgr.CurrentSource() != zeek::util::detail::SOURCE_LOCAL); %} ## Stops Zeek's packet processing. This function is used to synchronize @@ -5169,5 +5169,5 @@ function to_json%(val: any, only_loggable: bool &default=F, field_escape_pattern ## Returns: a compressed version of the input path. function compress_path%(dir: string%): string %{ - return zeek::make_intrusive(zeek::util::normalize_path(dir->ToStdString())); + return zeek::make_intrusive(zeek::util::detail::normalize_path(dir->ToStdString())); %} diff --git a/src/zeekygen/Manager.cc b/src/zeekygen/Manager.cc index 348f787a77..8971679725 100644 --- a/src/zeekygen/Manager.cc +++ b/src/zeekygen/Manager.cc @@ -58,12 +58,12 @@ static string NormalizeScriptPath(const string& path) { if ( auto p = zeek::plugin_mgr->LookupPluginByPath(path) ) { - auto rval = zeek::util::normalize_path(path); + auto rval = zeek::util::detail::normalize_path(path); auto prefix = zeek::util::SafeBasename(p->PluginDirectory()).result; return prefix + "/" + rval.substr(p->PluginDirectory().size() + 1); } - return zeek::util::without_zeekpath_component(path); + return zeek::util::detail::without_zeekpath_component(path); } Manager::Manager(const string& arg_config, const string& bro_command) diff --git a/src/zeekygen/ScriptInfo.cc b/src/zeekygen/ScriptInfo.cc index 4850c27598..c4a53b5582 100644 --- a/src/zeekygen/ScriptInfo.cc +++ b/src/zeekygen/ScriptInfo.cc @@ -162,7 +162,7 @@ static string make_redef_details(const string& heading, char underline, ScriptInfo::ScriptInfo(const string& arg_name, const string& arg_path) : Info(), name(arg_name), path(arg_path), - is_pkg_loader(zeek::util::is_package_loader(name)), + is_pkg_loader(zeek::util::detail::is_package_loader(name)), dependencies(), module_usages(), comments(), id_info(), redef_options(), constants(), state_vars(), types(), events(), hooks(), functions(), redefs() @@ -369,7 +369,7 @@ time_t ScriptInfo::DoGetModificationTime() const if ( ! info ) { - for (const string& ext : zeek::util::script_extensions) + for (const string& ext : zeek::util::detail::script_extensions) { string pkg_name = *it + "/__load__" + ext; info = zeek::detail::zeekygen_mgr->GetScriptInfo(pkg_name); diff --git a/src/zeekygen/Target.cc b/src/zeekygen/Target.cc index ab2f90a01d..d2393dfa11 100644 --- a/src/zeekygen/Target.cc +++ b/src/zeekygen/Target.cc @@ -196,7 +196,7 @@ TargetFile::TargetFile(const string& arg_name) { string dir = zeek::util::SafeDirname(name).result; - if ( ! zeek::util::ensure_intermediate_dirs(dir.c_str()) ) + if ( ! zeek::util::detail::ensure_intermediate_dirs(dir.c_str()) ) zeek::reporter->FatalError("Zeekygen failed to make dir %s", dir.c_str()); } @@ -413,7 +413,7 @@ void ScriptTarget::DoFindDependencies(const vector& infos) for ( size_t i = 0; i < script_deps.size(); ++i ) { - if ( zeek::util::is_package_loader(script_deps[i]->Name()) ) + if ( zeek::util::detail::is_package_loader(script_deps[i]->Name()) ) { string pkg_dir = zeek::util::SafeDirname(script_deps[i]->Name()).result; string target_file = Name() + pkg_dir + "/index.rst";