PacketParser.cpp
changeset 0 826dd5531eb0
equal deleted inserted replaced
-1:000000000000 0:826dd5531eb0
       
     1 /*
       
     2  *  $Id: PacketParser.cpp 54 2008-01-10 00:24:52Z mbroeker $
       
     3  * $URL: http://localhost/svn/cpp/qMonitor/trunk/PacketParser.cpp $
       
     4  */
       
     5 
       
     6 #include <PacketParser.h>
       
     7 #include <iomanip>
       
     8 
       
     9 PacketParser::PacketParser (int tm)
       
    10 :PacketReader (tm)
       
    11 {
       
    12     protocol = IPPROTO_IP;
       
    13     port = 0;
       
    14 }
       
    15 
       
    16 PacketParser::PacketParser (std::string filename)
       
    17 :  PacketReader (filename)
       
    18 {
       
    19     protocol = IPPROTO_IP;
       
    20     port = 0;
       
    21 }
       
    22 
       
    23 PacketParser::~PacketParser ()
       
    24 {
       
    25 }
       
    26 
       
    27 std::string PacketParser::getProtocol (int i)
       
    28 {
       
    29     proto = getprotobynumber (i);
       
    30     return ((proto != NULL) ? proto->p_name : "unknown");
       
    31 }
       
    32 
       
    33 std::string PacketParser::getPacket ()
       
    34 {
       
    35     if (port != 0)
       
    36         if ((sport != port) && (dport != port))
       
    37             return "";
       
    38 
       
    39     return str;
       
    40 }
       
    41 
       
    42 void PacketParser::setPort (unsigned short p)
       
    43 {
       
    44     port = p;
       
    45 }
       
    46 
       
    47 void PacketParser::setProtocol (int proto)
       
    48 {
       
    49     protocol = proto;
       
    50 }
       
    51 
       
    52 std::string PacketParser::read ()
       
    53 {
       
    54     std::ostringstream s;
       
    55     struct iphdr *iph;
       
    56     struct tcphdr *tcph;
       
    57     struct udphdr *udph;
       
    58 
       
    59     struct in_addr src;
       
    60     struct in_addr dst;
       
    61 
       
    62     int size = 0;
       
    63 
       
    64     uint i;
       
    65 
       
    66     str = PacketReader::read ();
       
    67 
       
    68     iph = (struct iphdr *)(str.c_str () + sizeof (struct ethhdr));
       
    69 
       
    70     switch (iph->protocol) {
       
    71     case IPPROTO_TCP:
       
    72         size = sizeof (tcphdr);
       
    73         break;
       
    74     case IPPROTO_UDP:
       
    75         size = sizeof (udphdr);
       
    76         break;
       
    77     case IPPROTO_ICMP:
       
    78         size = sizeof (icmphdr);
       
    79         break;
       
    80     case IPPROTO_IP:
       
    81         size += (sizeof (ethhdr) + sizeof (iphdr));
       
    82         break;
       
    83     default:
       
    84         size += (sizeof (ethhdr) + sizeof (iphdr));
       
    85     }
       
    86 
       
    87     src.s_addr = (iph->saddr);
       
    88     dst.s_addr = (iph->daddr);
       
    89 
       
    90     dhost = inet_ntoa (dst);
       
    91     shost = inet_ntoa (src);
       
    92 
       
    93     s.str () = "";
       
    94 
       
    95     if (protocol != iph->protocol) {
       
    96         switch (protocol) {
       
    97         case IPPROTO_IP:
       
    98             /*
       
    99              * filter the content later
       
   100              */
       
   101             break;
       
   102 
       
   103         case IPPROTO_RAW:
       
   104             for (i = size; i < str.length (); i++) {
       
   105                 if (!isgraph (str[i]))
       
   106                     s << ".";
       
   107                 else
       
   108                     s << str[i];
       
   109             }
       
   110             return s.str ();
       
   111             break;
       
   112         default:
       
   113             /*
       
   114              * discard packet
       
   115              */
       
   116             return s.str ();
       
   117         }
       
   118     }
       
   119 
       
   120     switch (iph->protocol) {
       
   121     case IPPROTO_IP:
       
   122         s << getProtocol (iph->protocol) << " " << std::setw (15) << shost << " ==> " << std::setw (15) << dhost;
       
   123         break;
       
   124 
       
   125     case IPPROTO_ICMP:
       
   126         s << getProtocol (iph->protocol) << " " << std::setw (15) << shost << " ==> " << std::setw (15) << dhost;
       
   127 
       
   128         break;
       
   129 
       
   130     case IPPROTO_TCP:
       
   131         tcph = (struct tcphdr *)(str.c_str () + sizeof (struct ethhdr) + sizeof (struct iphdr));
       
   132 
       
   133         sport = ntohs (tcph->source);
       
   134         dport = ntohs (tcph->dest);
       
   135 
       
   136         s << getProtocol (iph->protocol) << " "
       
   137             << std::setw (15) << shost << ":" << std::setw (5) << sport
       
   138             << " ==> " << std::setw (15) << dhost << ":" << std::setw (5) << dport;
       
   139 
       
   140         if (tcph->urg)
       
   141             s << (" urg ");
       
   142         if (tcph->ack) {
       
   143             s << (" ack ") << ntohl (tcph->ack_seq);
       
   144         }
       
   145         if (tcph->psh)
       
   146             s << (" psh ");
       
   147         if (tcph->rst)
       
   148             s << (" rst ");
       
   149         if (tcph->syn) {
       
   150             s << (" syn ") << ntohl (tcph->seq);
       
   151         }
       
   152         if (tcph->fin)
       
   153             s << (" fin ");
       
   154 
       
   155         break;
       
   156 
       
   157     case IPPROTO_UDP:
       
   158         udph = (struct udphdr *)(str.c_str () + sizeof (struct ethhdr) + sizeof (struct iphdr));
       
   159 
       
   160         sport = ntohs (udph->source);
       
   161         dport = ntohs (udph->dest);
       
   162 
       
   163         s << getProtocol (iph->protocol) << " "
       
   164             << std::setw (15) << shost << ":" << std::setw (5) << sport
       
   165             << " ==> " << std::setw (15) << dhost << ":" << std::setw (5) << dport;
       
   166 
       
   167         break;
       
   168 
       
   169     default:
       
   170         s << getProtocol (iph->protocol);
       
   171         break;
       
   172     }
       
   173 
       
   174     if (port != 0)
       
   175         if ((sport != port) && (dport != port))
       
   176             s.str ("");
       
   177 
       
   178     return (s.str ());
       
   179 }