Changeset 58e9e02 for lib


Ignore:
Timestamp:
04/28/05 15:22:40 (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:
a6badc2
Parents:
48cfaed
Message:

Added trace_get_{source,destination}_port
Added an API version #define
Consitification fixes

Location:
lib
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • lib/libtrace.h

    r7bb7dda r58e9e02  
    3434#include <sys/types.h>
    3535#include <netinet/in.h>
     36
     37/** API version as 3 byte hex digits */
     38#define LIBTRACE_API_VERSION 0x010010
    3639
    3740#ifdef __cplusplus
     
    239242 * @returns a pointer to the IP header, or NULL if there is not an IP packet
    240243 */
    241 struct libtrace_ip *trace_get_ip(struct libtrace_packet_t *packet);
     244struct libtrace_ip *trace_get_ip(const struct libtrace_packet_t *packet);
    242245
    243246/** get a pointer to the TCP header (if any)
     
    246249 * @returns a pointer to the TCP header, or NULL if there is not a TCP packet
    247250 */
    248 struct libtrace_tcp *trace_get_tcp(struct libtrace_packet_t *packet);
     251struct libtrace_tcp *trace_get_tcp(const struct libtrace_packet_t *packet);
    249252
    250253/** get a pointer to the TCP header (if any) given a pointer to the IP header
     
    265268 * @returns a pointer to the UDP header, or NULL if this is not a UDP packet
    266269 */
    267 struct libtrace_udp *trace_get_udp(struct libtrace_packet_t *packet);
     270struct libtrace_udp *trace_get_udp(const struct libtrace_packet_t *packet);
    268271
    269272/** get a pointer to the UDP header (if any) given a pointer to the IP header
     
    282285 * @returns a pointer to the ICMP header, or NULL if this is not a ICMP packet
    283286 */
    284 struct libtrace_icmp *trace_get_icmp(struct libtrace_packet_t *packet);
     287struct libtrace_icmp *trace_get_icmp(const struct libtrace_packet_t *packet);
    285288
    286289/** get a pointer to the ICMP header (if any) given a pointer to the IP header
     
    479482 */
    480483int trace_bpf_filter(struct libtrace_filter_t *filter,
    481                 struct libtrace_packet_t *packet);
     484                const struct libtrace_packet_t *packet);
    482485
    483486
    484487typedef enum {USE_DEST, USE_SOURCE} serverport_t;
     488
     489/** Get the source port
     490 * @param packet        the packet to read from
     491 * @returns a port in \em HOST byte order, or equivilent to ports for this
     492 * protocol, or 0 if this protocol has no ports.
     493 * @author Perry Lorier
     494 */
     495uint16_t trace_get_source_port(const struct libtrace_packet_t *);
     496
     497/** Get the destination port
     498 * @param packet        the packet to read from
     499 * @returns a port in \em HOST byte order, or equivilent to ports for this
     500 * protocol, or 0 if this protocol has no ports.
     501 * @author Perry Lorier
     502 */
     503uint16_t trace_get_destination_port(const struct libtrace_packet_t *);
    485504
    486505/** hint at the server port in specified protocol
  • lib/trace.c

    r808a478 r58e9e02  
    925925 * @returns a pointer to the IP header, or NULL if there is not an IP packet
    926926 */
    927 struct libtrace_ip *trace_get_ip(struct libtrace_packet_t *packet) {
     927struct libtrace_ip *trace_get_ip(const struct libtrace_packet_t *packet) {
    928928        struct libtrace_ip *ipptr = 0;
    929929
     
    10191019 * @returns a pointer to the TCP header, or NULL if there is not a TCP packet
    10201020 */
    1021 struct libtrace_tcp *trace_get_tcp(struct libtrace_packet_t *packet) {
     1021struct libtrace_tcp *trace_get_tcp(const struct libtrace_packet_t *packet) {
    10221022        struct libtrace_tcp *tcpptr = 0;
    10231023        struct libtrace_ip *ipptr = 0;
     
    10401040 * Skipped can be NULL, in which case it will be ignored by the program.
    10411041 */
    1042 struct libtrace_tcp *get_tcp_from_ip(struct libtrace_ip *ip, int *skipped)
     1042struct libtrace_tcp *get_tcp_from_ip(const struct libtrace_ip *ip, int *skipped)
    10431043{
    10441044#define SW_IP_OFFMASK 0xff1f
     
    10601060 * @returns a pointer to the UDP header, or NULL if this is not a UDP packet
    10611061 */
    1062 struct libtrace_udp *trace_get_udp(struct libtrace_packet_t *packet) {
     1062struct libtrace_udp *trace_get_udp(const struct libtrace_packet_t *packet) {
    10631063        struct libtrace_udp *udpptr = 0;
    10641064        struct libtrace_ip *ipptr = 0;
     
    10821082 * Skipped can be NULL, in which case it will be ignored by the program.
    10831083 */
    1084 struct libtrace_udp *get_udp_from_ip(struct libtrace_ip *ip, int *skipped)
     1084struct libtrace_udp *get_udp_from_ip(const struct libtrace_ip *ip, int *skipped)
    10851085{
    10861086        struct libtrace_udp *udpptr = 0;
     
    11021102 * @returns a pointer to the ICMP header, or NULL if this is not a ICMP packet
    11031103 */
    1104 struct libtrace_icmp *trace_get_icmp(struct libtrace_packet_t *packet) {
     1104struct libtrace_icmp *trace_get_icmp(const struct libtrace_packet_t *packet) {
    11051105        struct libtrace_icmp *icmpptr = 0;
    11061106        struct libtrace_ip *ipptr = 0;
     
    16211621 */
    16221622int trace_bpf_filter(struct libtrace_filter_t *filter,
    1623                         struct libtrace_packet_t *packet) {
     1623                        const struct libtrace_packet_t *packet) {
    16241624#if HAVE_BPF
    16251625        void *linkptr = 0;
     
    17651765}
    17661766
     1767struct ports_t {
     1768        uint16_t src;
     1769        uint16_t dst;
     1770};
     1771
     1772/* Return the client port
     1773 */
     1774uint16_t trace_get_source_port(const struct libtrace_packet_t *packet)
     1775{
     1776        struct libtrace_ip *ip = trace_get_ip(packet);
     1777        if (6 != ip->ip_p
     1778          && 17 != ip->ip_p)
     1779                return 0;
     1780        if (0 != (ip->ip_off & SW_IP_OFFMASK))
     1781                return 0;
     1782
     1783        struct ports_t *port;
     1784        port = (struct ports_t *)((ptrdiff_t)ip + (ip->ip_hl * 4));
     1785
     1786        return htons(port->src);
     1787}
     1788
     1789/* Same as get_source_port except use the destination port */
     1790uint16_t trace_get_destination_port(const struct libtrace_packet_t *packet)
     1791{
     1792        struct libtrace_ip *ip = trace_get_ip(packet);
     1793
     1794        if (6 != ip->ip_p
     1795          && 17 != ip->ip_p)
     1796                return 0;
     1797
     1798        if (0 != (ip->ip_off & SW_IP_OFFMASK))
     1799                return 0;
     1800
     1801        struct ports_t *port;
     1802        port = (struct ports_t *)((ptrdiff_t)ip + (ip->ip_hl * 4));
     1803
     1804        return htons(port->dst);
     1805}
     1806
    17671807#define ROOT_SERVER(x) ((x) < 512)
    17681808#define ROOT_CLIENT(x) ((512 <= (x)) && ((x) < 1024))
     
    17721812#define SERVER(x) ROOT_SERVER(x) || NONROOT_SERVER(x)
    17731813#define CLIENT(x) ROOT_CLIENT(x) || NONROOT_CLIENT(x)
    1774 
    17751814
    17761815/* Attempt to deduce the 'server' port
Note: See TracChangeset for help on using the changeset viewer.