PacketReader.cpp
author Markus Bröker <mbroeker@largo.homelinux.org>
Sat, 13 Dec 2008 15:45:18 +0100
changeset 0 826dd5531eb0
child 8 6097dc1fe2cf
permissions -rw-r--r--
svn copy of qmonitor
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     1
/*
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     2
 *  $Id: PacketReader.cpp 54 2008-01-10 00:24:52Z mbroeker $
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     3
 * $URL: http://localhost/svn/cpp/qMonitor/trunk/PacketReader.cpp $
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     4
 */
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     5
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     6
#include <PacketReader.h>
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     7
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     8
PacketReader::PacketReader (int tm)
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     9
{
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    10
    timeout = tm;
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    11
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    12
    if ((p_socket = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) == -1) {
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    13
        perror ("SocketException");
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    14
        throw std::runtime_error ("SocketException: root access required");
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    15
    }
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    16
}
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    17
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    18
PacketReader::PacketReader (std::string filename)
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    19
{
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    20
    if ((p_socket = open (filename.c_str (), O_RDONLY)) == -1) {
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    21
        throw std::runtime_error ("FileException");
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    22
    }
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    23
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    24
    timeout = -1;
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    25
}
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    26
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    27
PacketReader::~PacketReader ()
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    28
{
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    29
    close (p_socket);
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    30
}
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    31
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    32
std::string PacketReader::read ()
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    33
{
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    34
    static char s[IP_MAXPACKET + 1];
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    35
    int c;
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    36
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    37
    c =::read (p_socket, s, IP_MAXPACKET);
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    38
    s[c] = 0;
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    39
    return std::string (s, c);
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    40
}
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    41
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    42
bool PacketReader::ready ()
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    43
{
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    44
    if (p_socket)
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    45
        return true;
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    46
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    47
    return false;
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    48
}
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    49
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    50
bool PacketReader::dataAvailable ()
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    51
{
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    52
    pollfd fdin;
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    53
    int event = 1;
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    54
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    55
    fdin.fd = p_socket;
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    56
    fdin.events = POLLIN;
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    57
    fdin.revents = 0;
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    58
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    59
    if (poll (&fdin, event, timeout) > 0)
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    60
        return true;
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    61
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    62
    return false;
826dd5531eb0 svn copy of qmonitor
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    63
}