Changeset 68667ee for lib/trace.c


Ignore:
Timestamp:
02/23/05 11:45:44 (16 years ago)
Author:
Perry Lorier <perry@…>
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:
b06e535
Parents:
cb8c1b9
Message:

Merged in a whole heap of useful features

File:
1 edited

Legend:

Unmodified
Added
Removed
  • lib/trace.c

    r850630f r68667ee  
    3737 * @author Perry Lorier
    3838 *
     39 * @internal
    3940 */
    4041#define _GNU_SOURCE
     
    129130
    130131#if HAVE_BPF
     132/** A type encapsulating a bpf filter
     133 * This type covers the compiled bpf filter, as well as the original filter
     134 * string
     135 *
     136 */
    131137struct libtrace_filter_t {
    132138        struct bpf_insn *filter;
     
    135141#endif
    136142
     143/** The information about traces that are open
     144 * @internal
     145 */
    137146struct libtrace_t {
    138         format_t format;
    139         source_t sourcetype;
     147        format_t format;        /**< The format that this trace is in */
     148        source_t sourcetype;    /**< The type (device,file, etc */
    140149        union {
     150                /** Information about rtclients */
    141151                struct {
    142152                        char *hostname;
    143153                        short port;
    144154                } rt;
    145                 char *path;
    146                 char *interface;
     155                char *path;             /**< information for local sockets */
     156                char *interface;        /**< intormation for reading of network
     157                                             interfaces */
    147158        } conn_info;
     159        /** Information about the current state of the input device */
    148160        union {
    149161                int fd;
     
    925937}
    926938
     939/** get a pointer to the TCP header (if any) given a pointer to the IP header
     940 * @param ip            The IP header
     941 * @param[out] skipped  An output variable of the number of bytes skipped
     942 *
     943 * @returns a pointer to the TCP header, or NULL if this is not a TCP packet
     944 *
     945 * Skipped can be NULL, in which case it will be ignored by the program.
     946 */
     947struct libtrace_tcp *get_tcp_from_ip(struct libtrace_ip *ip, int *skipped)
     948{
     949#define SW_IP_OFFMASK 0xff1f
     950        struct libtrace_tcp *tcpptr = 0;
     951
     952        if ((ip->ip_p == 6) && ((ip->ip_off & SW_IP_OFFMASK) == 0))  {
     953                tcpptr = (struct libtrace_tcp *)((ptrdiff_t)ip+ (ip->ip_hl * 4));
     954        }
     955
     956        if (skipped)
     957                *skipped=(ip->ip_hl*4);
     958
     959        return tcpptr;
     960}
     961
    927962/** get a pointer to the UDP header (if any)
    928963 * @param packet        a pointer to a libtrace_packet structure
     
    940975                udpptr = (struct libtrace_udp *)((ptrdiff_t)ipptr + (ipptr->ip_hl * 4));
    941976        }
     977
    942978        return udpptr;
    943979}
     980
     981/** get a pointer to the UDP header (if any) given a pointer to the IP header
     982 * @param ip            The IP header
     983 * @param[out] skipped  An output variable of the number of bytes skipped
     984 *
     985 * @returns a pointer to the UDP header, or NULL if this is not a UDP packet
     986 *
     987 * Skipped can be NULL, in which case it will be ignored by the program.
     988 */
     989struct libtrace_udp *get_udp_from_ip(struct libtrace_ip *ip, int *skipped)
     990{
     991        struct libtrace_udp *udpptr = 0;
     992
     993        if ((ip->ip_p == 6) && ((ip->ip_off & SW_IP_OFFMASK) == 0))  {
     994                udpptr = (struct libtrace_udp *)((ptrdiff_t)ip+ (ip->ip_hl * 4));
     995        }
     996
     997        if (skipped)
     998                *skipped=(ip->ip_hl*4);
     999
     1000        return udpptr;
     1001}
     1002
    9441003
    9451004/** get a pointer to the ICMP header (if any)
     
    9601019        return icmpptr;
    9611020}
     1021
     1022/** get a pointer to the ICMP header (if any) given a pointer to the IP header
     1023 * @param ip            The IP header
     1024 * @param[out] skipped  An output variable of the number of bytes skipped
     1025 *
     1026 * @returns a pointer to the ICMP header, or NULL if this is not a ICMP packet
     1027 *
     1028 * Skipped can be NULL, in which case it will be ignored by the program.
     1029 */
     1030struct libtrace_icmp *get_icmp_from_ip(struct libtrace_ip *ip, int *skipped)
     1031{
     1032        struct libtrace_icmp *icmpptr = 0;
     1033
     1034        if ((ip->ip_p == 6) && ((ip->ip_off & SW_IP_OFFMASK) == 0))  {
     1035                icmpptr = (struct libtrace_icmp *)((ptrdiff_t)ip+ (ip->ip_hl * 4));
     1036        }
     1037
     1038        if (skipped)
     1039                *skipped=(ip->ip_hl*4);
     1040
     1041        return icmpptr;
     1042}
     1043/** parse an ip or tcp option
     1044 * @param[in,out] ptr   the pointer to the current option
     1045 * @param[in,out] len   the length of the remaining buffer
     1046 * @param[out] type     the type of the option
     1047 * @param[out] optlen   the length of the option
     1048 * @param[out] data     the data of the option
     1049 *
     1050 * @returns bool true if there is another option (and the fields are filled in)
     1051 *               or false if this was the last option.
     1052 *
     1053 * This updates ptr to point to the next option after this one, and updates
     1054 * len to be the number of bytes remaining in the options area.  Type is updated
     1055 * to be the code of this option, and data points to the data of this option,
     1056 * with optlen saying how many bytes there are.
     1057 *
     1058 * @note Beware of fragmented packets.
     1059 * @author Perry Lorier
     1060 */
     1061int trace_get_next_option(unsigned char **ptr,int *len,
     1062                        unsigned char *type,
     1063                        unsigned char *optlen,
     1064                        unsigned char **data)
     1065{
     1066        if (*len<=0)
     1067                return 0;
     1068        *type=**ptr;
     1069        switch(*type) {
     1070                case 0: /* End of options */
     1071                        return 0;
     1072                case 1: /* Pad */
     1073                        (*ptr)++;
     1074                        (*len)--;
     1075                        return 1;
     1076                default:
     1077                        *optlen = *(*ptr+1);
     1078                        if (*optlen<2)
     1079                                return 0; // I have no idea wtf is going on
     1080                                          // with these packets
     1081                        (*len)-=*optlen;
     1082                        (*data)=(*ptr+2);
     1083                        (*ptr)+=*optlen;
     1084                        if (*len<0)
     1085                                return 0;
     1086                        return 1;
     1087        }
     1088        assert(0);
     1089}
     1090
    9621091
    9631092/** Get the current time in DAG time format
     
    12441373 * @param packet the libtrace_packet opaque pointer
    12451374 * @returns
    1246  *  TRACE_EVENT_IOWAIT  Waiting on I/O on <fd>
    1247  *  TRACE_EVENT_SLEEP   Next event in <seconds>
    1248  *  TRACE_EVENT_PACKET  Packet arrived in <buffer> with size <size>
     1375 *  TRACE_EVENT_IOWAIT  Waiting on I/O on fd
     1376 *  TRACE_EVENT_SLEEP   Next event in seconds
     1377 *  TRACE_EVENT_PACKET  Packet arrived in buffer with size size
    12491378 * FIXME currently keeps a copy of the packet inside the trace pointer,
    12501379 * which in turn is stored inside the new packet object...
Note: See TracChangeset for help on using the changeset viewer.