Back to index

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

#include <pcap.h>

List of all members.

Public Member Functions

 Pcap (char const *device, bool promisc=false, size_t snapLen=1500, size_t timeOutMs=1000)
void filter (std::string const &filterExpr, bool optimize=true)
void loop (u_char *user, pcap_handler callback)
void stop ()
int shiftPacketBegin () const

Private Member Functions

void computeShift (char const *device)

Private Attributes

pcap_t * d_pcap
bpf_u_int32 d_net
int d_shift

Detailed Description

Definition at line 7 of file pcap.h.


Constructor & Destructor Documentation

Pcap::Pcap ( char const *  device,
bool  promisc = false,
size_t  snapLen = 1500,
size_t  timeOutMs = 1000 
)

Definition at line 3 of file pcap1.cc.

{
    computeShift(device);

    char errBuf[PCAP_ERRBUF_SIZE];

    d_pcap = pcap_open_live(device, snapLen, promisc, timeOutMs, errBuf);

    if (d_pcap == 0)
        throw Errno(1, "[Fatal] ") << errBuf;

    bpf_u_int32 netMask;        // The netmask of our sniffing device

    if (pcap_lookupnet(device, &d_net, &netMask, errBuf) == -1) 
        throw Errno(1, "[Fatal] ") << 
                        "Can't get network address of device " << device;
}

Here is the call graph for this function:


Member Function Documentation

void Pcap::computeShift ( char const *  device) [private]

Definition at line 3 of file computeshift.cc.

{
    // see the posting by user175104 on 
    // http://stackoverflow.com/questions/1779715/
    //              how-to-get-mac-address-of-your-machine-using-a-c-program 

    ifstream mac((string("/sys/class/net/") + device) + "/address");

    string address;
    d_shift = (mac >> address) ? 0 : PcapPacket::SIZEOF_ETHERNET_HEADER;

    if (d_shift == 0)
        imsg << "Device " << device << " has MAC address " << address << endl;
    else
        imsg << "Device " << device << " has no MAC address: packets lack "
                                    "Ethernet headers" << endl;
}

Here is the caller graph for this function:

void Pcap::filter ( std::string const &  filterExpr,
bool  optimize = true 
)

Definition at line 3 of file filter.cc.

{
    if (pcap_compile(d_pcap, &d_pcapFilter, 
                     filterExpr.c_str(), optimize, d_net) == -1) 
        throw Errno(1, "[Fatal] ") << "Pcap compilation of `" << filterExpr << 
                            "' failed: " << pcap_geterr(d_pcap);
    
    if (pcap_setfilter(d_pcap, &d_pcapFilter) == -1) 
         throw Errno(1, "[Fatal] ") << "Couldn't install filter `" << 
                filterExpr << ": " << pcap_geterr(d_pcap);
}

Here is the caller graph for this function:

void Pcap::loop ( u_char *  user,
pcap_handler  callback 
)

Definition at line 3 of file loop.cc.

{
    pcap_loop(d_pcap, -1, callback, user);
}

Here is the caller graph for this function:

int Pcap::shiftPacketBegin ( ) const [inline]

Definition at line 33 of file pcap.h.

{
    return d_shift;
}
void Pcap::stop ( ) [inline]

Definition at line 28 of file pcap.h.

{
    pcap_breakloop(d_pcap);
}

Here is the caller graph for this function:


Member Data Documentation

bpf_u_int32 Pcap::d_net [private]

Definition at line 11 of file pcap.h.

pcap_t* Pcap::d_pcap [private]

Definition at line 9 of file pcap.h.

int Pcap::d_shift [private]

Definition at line 12 of file pcap.h.


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