Skip to content

Commit

Permalink
Clean up utils header file to be self-contained
Browse files Browse the repository at this point in the history
Currently, utils.h has a lot of implicit dependencies on other stl
header files as well as assuming the std namespace is available.

Clean it up so it can be included on its own (say, in
falcosecurity/falco#162).
  • Loading branch information
mstemm committed Dec 8, 2016
1 parent c3ebaa7 commit a70090a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 32 deletions.
1 change: 0 additions & 1 deletion userspace/libsinsp/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ along with sysdig. If not, see <http://www.gnu.org/licenses/>.
#include "filterchecks.h"
#include "chisel.h"
#include "protodecoder.h"
#include "json/json.h"
#include "uri.h"
#ifndef _WIN32
#include "curl/curl.h"
Expand Down
66 changes: 35 additions & 31 deletions userspace/libsinsp/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ along with sysdig. If not, see <http://www.gnu.org/licenses/>.

#pragma once

#include <list>
#include <algorithm>
#include <locale>
#include <sstream>

#include <scap.h>
#include "json/json.h"

class sinsp_evttables;
typedef union _sinsp_sockinfo sinsp_sockinfo;
typedef union _ipv4tuple ipv4tuple;
Expand Down Expand Up @@ -80,12 +84,12 @@ class sinsp_utils
//
// Given a string, scan the event list and find the longest argument that the input string contains
//
static const struct ppm_param_info* find_longest_matching_evt_param(string name);
static const struct ppm_param_info* find_longest_matching_evt_param(std::string name);

//
// Get the list of filtercheck fields
//
static void get_filtercheck_fields_info(vector<const filter_check_info*>* list);
static void get_filtercheck_fields_info(std::vector<const filter_check_info*>* list);

static uint64_t get_current_time_ns();

Expand Down Expand Up @@ -116,7 +120,7 @@ struct g_invalidchar
}
};

inline void sanitize_string(string &str)
inline void sanitize_string(std::string &str)
{
// It turns out with -O3 (release flags) using erase and
// remove_if is slighly faster than the inline version that
Expand Down Expand Up @@ -149,33 +153,33 @@ SINSP_PUBLIC int gettimeofday(struct timeval *tv, struct timezone2 *tz);
///////////////////////////////////////////////////////////////////////////////
// gethostname wrapper
///////////////////////////////////////////////////////////////////////////////
string sinsp_gethostname();
std::string sinsp_gethostname();

///////////////////////////////////////////////////////////////////////////////
// tuples to string
///////////////////////////////////////////////////////////////////////////////

// each of these functions uses values in network byte order

string ipv4tuple_to_string(ipv4tuple* tuple, bool resolve);
string ipv6tuple_to_string(_ipv6tuple* tuple, bool resolve);
string ipv4serveraddr_to_string(ipv4serverinfo* addr, bool resolve);
string ipv6serveraddr_to_string(ipv6serverinfo* addr, bool resolve);
std::string ipv4tuple_to_string(ipv4tuple* tuple, bool resolve);
std::string ipv6tuple_to_string(_ipv6tuple* tuple, bool resolve);
std::string ipv4serveraddr_to_string(ipv4serverinfo* addr, bool resolve);
std::string ipv6serveraddr_to_string(ipv6serverinfo* addr, bool resolve);

// `l4proto` should be of type scap_l4_proto, but since it's an enum sometimes
// is used as int and we would have to cast
// `port` must be saved with network byte order
// `l4proto` could be neither TCP nor UDP, in this case any protocol will be
// matched
string port_to_string(uint16_t port, uint8_t l4proto, bool resolve);
std::string port_to_string(uint16_t port, uint8_t l4proto, bool resolve);

///////////////////////////////////////////////////////////////////////////////
// String helpers
///////////////////////////////////////////////////////////////////////////////
vector<string> sinsp_split(const string& s, char delim);
std::vector<std::string> sinsp_split(const std::string& s, char delim);

template<typename It>
string sinsp_join(It begin, It end, char delim)
std::string sinsp_join(It begin, It end, char delim)
{
if(begin == end)
{
Expand All @@ -191,31 +195,31 @@ string sinsp_join(It begin, It end, char delim)
return ss.str();
}

string& ltrim(string& s);
string& rtrim(string& s);
string& trim(string& s);
string& replace_in_place(string& s, const string& search, const string& replacement);
string replace(const string& str, const string& search, const string& replacement);
std::string& ltrim(std::string& s);
std::string& rtrim(std::string& s);
std::string& trim(std::string& s);
std::string& replace_in_place(std::string& s, const std::string& search, const std::string& replacement);
std::string replace(const std::string& str, const std::string& search, const std::string& replacement);

///////////////////////////////////////////////////////////////////////////////
// number parser
///////////////////////////////////////////////////////////////////////////////
class sinsp_numparser
{
public:
static uint8_t parseu8(const string& str);
static int8_t parsed8(const string& str);
static uint16_t parseu16(const string& str);
static int16_t parsed16(const string& str);
static uint32_t parseu32(const string& str);
static int32_t parsed32(const string& str);
static uint64_t parseu64(const string& str);
static int64_t parsed64(const string& str);

static bool tryparseu32(const string& str, uint32_t* res);
static bool tryparsed32(const string& str, int32_t* res);
static bool tryparseu64(const string& str, uint64_t* res);
static bool tryparsed64(const string& str, int64_t* res);
static uint8_t parseu8(const std::string& str);
static int8_t parsed8(const std::string& str);
static uint16_t parseu16(const std::string& str);
static int16_t parsed16(const std::string& str);
static uint32_t parseu32(const std::string& str);
static int32_t parsed32(const std::string& str);
static uint64_t parseu64(const std::string& str);
static int64_t parsed64(const std::string& str);

static bool tryparseu32(const std::string& str, uint32_t* res);
static bool tryparsed32(const std::string& str, int32_t* res);
static bool tryparseu64(const std::string& str, uint64_t* res);
static bool tryparsed64(const std::string& str, int64_t* res);

static bool tryparseu32_fast(const char* str, uint32_t strlen, uint32_t* res);
static bool tryparsed32_fast(const char* str, uint32_t strlen, int32_t* res);
Expand Down Expand Up @@ -285,8 +289,8 @@ class simple_lifo_queue
}

private:
list<OBJ*> m_avail_list;
list<OBJ*> m_full_list;
std::list<OBJ*> m_avail_list;
std::list<OBJ*> m_full_list;
};

///////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit a70090a

Please sign in to comment.