Ignore:
Timestamp:
09/27/10 14:01:58 (3 years ago)
Author:
salcock
Message:

Added a trace_get_payload_length() function which returns the original payload length for a packet - got sick of re-implementing this function everytime I wanted to know the size of the application payload.

This function currently works for TCP, UDP and ICMP packets. All other protocols
will return 0.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/protocols_transport.c

    r1522 r1661  
    4747 *      ICMP 
    4848 */ 
     49 
     50/* Get the size of the payload as it was in the original packet, i.e. prior 
     51 * to any truncation. 
     52 * 
     53 * Basically, wire length minus the packet headers. 
     54 * 
     55 * Currently only supports IP (v4 and v6) and TCP, UDP and ICMP. Will return 
     56 * 0 if an unsupported protocol header is encountered, or if one of the  
     57 * headers is truncated. 
     58 */ 
     59DLLEXPORT size_t trace_get_payload_length(const libtrace_packet_t *packet) { 
     60 
     61        void *layer; 
     62        uint16_t ethertype; 
     63        uint8_t proto; 
     64        uint32_t rem; 
     65        libtrace_ip_t *ip; 
     66        libtrace_ip6_t *ip6; 
     67        libtrace_tcp_t *tcp; 
     68        size_t len = 0; 
     69 
     70        layer = trace_get_layer3(packet, &ethertype, &rem); 
     71 
     72        if (!layer) 
     73                return 0; 
     74        switch (ethertype) { 
     75                case TRACE_ETHERTYPE_IP: 
     76                        ip = (libtrace_ip_t *)layer; 
     77                        if (rem < sizeof(libtrace_ip_t)) 
     78                                return 0; 
     79                        len = ntohs(ip->ip_len) - (4 * ip->ip_hl); 
     80                        break; 
     81                case TRACE_ETHERTYPE_IPV6: 
     82                        ip6 = (libtrace_ip6_t *)layer; 
     83                        if (rem < sizeof(libtrace_ip6_t)) 
     84                                return 0; 
     85                        len = ntohs(ip6->plen); 
     86                        break; 
     87                default: 
     88                        return 0; 
     89        } 
     90 
     91        layer = trace_get_transport(packet, &proto, &rem); 
     92        if (!layer) 
     93                return 0; 
     94         
     95        switch(proto) { 
     96                case TRACE_IPPROTO_TCP: 
     97                        tcp = (libtrace_tcp_t *)layer; 
     98                        len -= (4 * tcp->doff); 
     99                        break; 
     100                case TRACE_IPPROTO_UDP: 
     101                        len -= sizeof(libtrace_udp_t); 
     102                        break; 
     103                case TRACE_IPPROTO_ICMP: 
     104                        len -= sizeof(libtrace_icmp_t); 
     105                        break; 
     106                default: 
     107                        return 0; 
     108        } 
     109 
     110        return len; 
     111 
     112} 
    49113 
    50114DLLEXPORT void *trace_get_transport(const libtrace_packet_t *packet,  
Note: See TracChangeset for help on using the changeset viewer.