Back to index

natlog  0.91.0
Public Member Functions | Private Types | Private Member Functions | Private Attributes
NatFork Class Reference

#include <natfork.h>

Collaboration diagram for NatFork:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 NatFork ()
void run ()

Private Types

enum  Mode { CONNTRACK, PCAP }

Private Member Functions

void setupStdMsg ()
void checkSyslogParam (char const *label, std::string const &actual, std::string const &err)
void setupDaemonMsg ()
void setupNonDaemonMsg ()
void handleChildStatus ()
void conntrackMode ()
virtual void parentProcess () override
virtual void childProcess () override

Private Attributes

Optionsd_options
std::unique_ptr
< FBB::SyslogStream > 
d_syslog
FBB::MultiStreambuf d_multiStreambuf
std::ostream d_stdMsg
Mode d_mode
FBB::Pipe d_pipe

Detailed Description

Definition at line 19 of file natfork.h.


Member Enumeration Documentation

enum NatFork::Mode [private]
Enumerator:
CONNTRACK 
PCAP 

Definition at line 21 of file natfork.h.

    {
        CONNTRACK,
        PCAP
    };

Constructor & Destructor Documentation

Definition at line 3 of file natfork1.cc.

:
    d_options(Options::instance()),
    d_stdMsg(&d_multiStreambuf)
{
    setupStdMsg();

    if (d_options.daemon())
        setupDaemonMsg();
    else
        setupNonDaemonMsg();


    if (d_options.timeSpecError().length() == 0)
        imsg << "Using time type `" << d_options.timeTxt() << '\'' << endl;
    else
        wmsg << "Time specifcation `" << d_options.timeSpecError() <<
                "' not supported. Using `" << d_options.timeTxt() << 
                "' instead" << endl;
}

Here is the call graph for this function:


Member Function Documentation

void NatFork::checkSyslogParam ( char const *  label,
std::string const &  actual,
std::string const &  err 
) [private]

Definition at line 3 of file checksyslogparam.cc.

{
    if (err.length())
        d_stdMsg << "[Warning] Using Syslog " << label << '`' <<
                actual << "', ignoring unsupported " << label << '`' << endl;
}

Here is the caller graph for this function:

void NatFork::childProcess ( ) [override, private, virtual]

Definition at line 3 of file childprocess.cc.

{
    ShowSeconds::setFormat();

    if (d_options.daemon())
        prepareDaemon();

    OFdStream out(d_pipe.writeFd());        // the message to the parent

    try
    {
        if (d_mode == CONNTRACK)
        {
            Conntrack conntrack(d_stdMsg);
            conntrack.run(out);
        }
        else 
        {
            Devices devices(d_stdMsg);
            devices.run(out);
        }
    }
    catch (Errno const &err)            // any errors at Conntrack or Devices
    {                                   // are thrown as Errno exceptions
        if (not d_options.daemon())
            throw;                      // rethrow the exception

        d_stdMsg << err.why() << endl;
        out << 1 << endl;               // The daemon can't start:
                                        // inform via the pipe
    }

        // when the child process ends it throws away its own pid file:
    ifstream pidFile(d_options.pidFile());
    pid_t pid;
    if (pidFile >> pid && pid == getpid())
    {
        pidFile.close();
        unlink(d_options.pidFile().c_str());
    }

    throw Options::OK;              // ends the program or the child process
}

Here is the call graph for this function:

Here is the caller graph for this function:

void NatFork::conntrackMode ( ) [private]

Definition at line 3 of file conntrackmode.cc.

{
    Options &options = Options::instance();

    char const *conntrack = options.conntrackPath().c_str();

    if (access(conntrack, R_OK | X_OK) != 0)
    {
        d_stdMsg << "[Fatal] Can't execute " << conntrack << endl;
        throw Options::FAILED;
    }
    d_mode = CONNTRACK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void NatFork::handleChildStatus ( ) [private]

Definition at line 3 of file handlechildstatus.cc.

{
    d_pipe.readFrom(STDIN_FILENO);

    int status;
    cin >> status;

    if (status != 0)
        throw Options::FAILED;
}

Here is the caller graph for this function:

void NatFork::parentProcess ( ) [override, private, virtual]

Definition at line 6 of file parentprocess.cc.

{
    handleChildStatus();

        // the daemon started OK, now write its pid-file:
    ofstream pidFile(Options::instance().pidFile());
    pidFile << pid() << endl;

    throw Options::OK;
}

Here is the call graph for this function:

void NatFork::run ( )

Definition at line 3 of file run.cc.

{
    if (string(d_options[0]) == "conntrack")
        conntrackMode();
    else if (d_options.nArgs() == 2)
        d_mode = PCAP;
    else
    {
        d_stdMsg << "[Fatal] invalid argument(s): `";
        for (size_t idx = 0, end = d_options.nArgs(); idx != end; ++idx)
            d_stdMsg << d_options[idx] << (idx + 1 == end ? '\'' : ' ');
        d_stdMsg << endl;

        throw Options::FAILED;
    }

    if (d_options.daemon())
        fork();
    else
        childProcess();
}

Here is the call graph for this function:

Here is the caller graph for this function:

void NatFork::setupDaemonMsg ( ) [private]

Definition at line 3 of file setupdaemonmsg.cc.

{
    if (not d_options.syslog())
    {
        imsg.off();
        wmsg.off();

        return;
    }

                                // verbose msg via syslog, if syslog is active
    if (d_options.verbose())
        imsg.reset(*d_syslog);
    else
        imsg.off();

                                // warning msg via syslog, if syslog is active
    if (d_options.warnings())
        wmsg.reset(*d_syslog);
    else
        wmsg.off();
}

Here is the caller graph for this function:

void NatFork::setupNonDaemonMsg ( ) [private]

Definition at line 3 of file setupnondaemonmsg.cc.

{
    if (not d_options.verbose())
        imsg.off();

    if (not d_options.warnings())
        wmsg.off();
}

Here is the caller graph for this function:

void NatFork::setupStdMsg ( ) [private]

Definition at line 3 of file setupstdmsg.cc.

{
    if (d_options.stdout())             // if --stdout, then messages also go
        d_multiStreambuf.insert(cout);  // to stdout

    if (d_options.syslog())             // always set up syslog
    {
        d_syslog.reset(
            new SyslogStream(
                d_options.syslogTag().c_str(), d_options.syslogPriority(), 
                d_options.syslogFacility()
            )
        );

        d_multiStreambuf.insert(*d_syslog);
    }

    checkSyslogParam("facility", d_options.facility(), 
                                 d_options.syslogFacilityError());

    checkSyslogParam("priority", d_options.priority(), 
                                 d_options.syslogPriorityError());
}

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Mode NatFork::d_mode [private]

Definition at line 34 of file natfork.h.

FBB::MultiStreambuf NatFork::d_multiStreambuf [private]

Definition at line 31 of file natfork.h.

Definition at line 27 of file natfork.h.

FBB::Pipe NatFork::d_pipe [private]

Definition at line 35 of file natfork.h.

std::ostream NatFork::d_stdMsg [private]

Definition at line 32 of file natfork.h.

std::unique_ptr<FBB::SyslogStream> NatFork::d_syslog [private]

Definition at line 29 of file natfork.h.


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