Changeset 15749d5 for lib


Ignore:
Timestamp:
09/27/10 14:01:58 (11 years ago)
Author:
Shane Alcock <salcock@…>
Branches:
4.0.1-hotfixes, cachetimestamps, develop, dpdk-ndag, etsilive, getfragoff, help, libtrace4, master, ndag_format, pfring, rc-4.0.1, rc-4.0.2, rc-4.0.3, rc-4.0.4, ringdecrementfix, ringperformance, ringtimestampfixes
Children:
7e8a25a
Parents:
5d081ff
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.

Location:
lib
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • lib/libtrace.h.in

    r22e626a r15749d5  
    19981998size_t trace_get_framing_length(const libtrace_packet_t *packet);
    19991999
     2000/** Get the length of the original payload content of the packet (in bytes).
     2001 * @param packet        The packet to determine the payload length for
     2002 * @return The size of the packet payload (without headers). Returns 0 if
     2003 * unable to calculate payload length correctly.
     2004 *
     2005 * This function reports the amount of data that followed the transport header
     2006 * when the packet was originally captured, i.e. prior to any snapping. Best
     2007 * described as the wire length minus the packet headers.
     2008 *
     2009 * Currently only supports some protocols and will return 0 if an unsupported
     2010 * protocol header is encountered, or if one of the headers is truncated.
     2011 *
     2012 * @note Supports IPv4, IPv6, TCP, UDP and ICMP.
     2013 */
     2014DLLEXPORT SIMPLE_FUNCTION
     2015size_t trace_get_payload_length(const libtrace_packet_t *packet);
     2016
    20002017/** Truncate ("snap") the packet to the suggested length
    20012018 * @param packet        The packet to truncate
  • lib/protocols_transport.c

    rf6730d8 r15749d5  
    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.