Back to index

natlog  0.91.0
Public Member Functions | Private Member Functions | Private Attributes
Conntrack Class Reference

#include <conntrack.h>

Inheritance diagram for Conntrack:
Inheritance graph
[legend]
Collaboration diagram for Conntrack:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 Conntrack (std::ostream &stdMsg)
 ~Conntrack ()
void run (std::ostream &parent)

Private Member Functions

void log (ConntrackRecord::Record const &record, std::string const &endSeconds, std::string endMicroSecs)
virtual void signaled (size_t signum) override

Private Attributes

ConntrackRecord d_connections
Optionsd_options
FBB::Process d_conntrack
std::ostream & d_stdMsg

Detailed Description

Definition at line 14 of file conntrack.h.


Constructor & Destructor Documentation

Conntrack::Conntrack ( std::ostream &  stdMsg)

Definition at line 3 of file conntrack1.cc.

:
    d_options(Options::instance()),
    d_conntrack(Process::COUT | Process::CERR, d_options.conntrackPath() + 
                                " -p tcp -E -n -o timestamp -e NEW,DESTROY"),
    d_stdMsg(stdMsg)
{
    if (access("/proc/net/ip_conntrack", R_OK) != 0)
        throw Errno(1, "[Fatal] ") << "Cannot read /proc/net/ip_conntrack";

    imsg << "Calling `" << d_options.conntrackPath() + 
            " -p tcp -E -n -o timestamp -e NEW,DESTROY'" << endl;
}

Definition at line 3 of file destructor.cc.

{
    string endSeconds = X2a(time(0));

    d_stdMsg << "terminating" << endl;

    for (auto &rec: d_connections)
    {
        if (rec)
            log(*rec, endSeconds, "0");
    }
}

Here is the call graph for this function:


Member Function Documentation

void Conntrack::log ( ConntrackRecord::Record const &  record,
std::string const &  endSeconds,
std::string  endMicroSecs 
) [private]

Definition at line 3 of file log.cc.

{
    endMicroSecs.insert(0, 6 - endMicroSecs.length(), '0');

    d_stdMsg << "from " << 
                ShowSeconds(record.seconds) << ':' << record.musecs << 
            " until " << 
                ShowSeconds(endSeconds) << ':' << endMicroSecs << 
                                        ShowSeconds::utcMarker() << ": " <<
            record.sourceIP << ':' << record.sourcePort << 
               " (via: " << record.viaIP << ':' << record.viaPort << ") "
        "to " << record.destIP << ':' << record.destPort << endl;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void Conntrack::run ( std::ostream &  parent)

Definition at line 8 of file run.cc.

{
    Signal::instance().add(SIGTERM, *this);

    Pattern pat(
    //     1        2      3
    "\\[(\\d+)\\.(\\d+).*(NEW|DESTROY).*"   // time: [1338899277.41469 ]
    //      4             5
    "src=(\\S+)\\s+dst=(\\S+)\\s+"          // source to nat,  dest
    //        6               7
    "sport=(\\d+)\\s+dport=(\\d+).*"        // source from,    dest port
    //      8               
    "dst=(\\S+).*"                          // natted source 
    //        9
    "dport=(\\d+)");                        // natted sport


    parent << 0 << endl;                    // all OK

    d_stdMsg << "starting: using " << d_options.conntrackPath() << endl;
    d_conntrack.start();

    string line;
    while (getline(d_conntrack, line))
    {
        imsg << "LINE: " << line << endl;

        if (pat << line)
        {
            string key(pat[8] + pat[9]);

            if (pat[3] == "NEW")
                d_connections.add(key, pat);
            else 
            {
                size_t idx = d_connections.find(key);
                if (idx == numeric_limits<size_t>::max())
                    wmsg << "UNAVAILABLE: " << line << endl;
                else
                {
                    log(*d_connections[idx], pat[1], pat[2]);
                    d_connections.erase(idx);  // erase processed element
                }
            }
        }
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void Conntrack::signaled ( size_t  signum) [override, private, virtual]

Implements SignalHandler.

Definition at line 3 of file signaled.cc.

{
    d_conntrack.stop();
}

Member Data Documentation

Definition at line 16 of file conntrack.h.

FBB::Process Conntrack::d_conntrack [private]

Definition at line 18 of file conntrack.h.

Definition at line 17 of file conntrack.h.

std::ostream& Conntrack::d_stdMsg [private]

Definition at line 19 of file conntrack.h.


The documentation for this class was generated from the following files: