fluffy/pcap.h

78 lines
2.0 KiB
C
Raw Normal View History

2011-04-21 16:12:01 -06:00
#ifndef __PCAP_H__
#define __PCAP_H__
#include <stdio.h>
#include <stdint.h>
2013-02-11 15:50:30 -07:00
#include <stdbool.h>
2011-04-21 16:12:01 -06:00
#define MAGIC 0xa1b2c3d4
2018-07-19 09:37:38 -06:00
#define MAXFRAME 262144
2011-04-21 16:12:01 -06:00
2013-07-26 14:08:48 -06:00
/*
2018-07-19 09:37:38 -06:00
* Described at http://www.tcpdump.org/linktypes.html
2013-07-26 14:08:48 -06:00
*/
2013-02-11 15:50:30 -07:00
#define LINKTYPE_ETHERNET 1
#define LINKTYPE_RAW 101
2011-04-21 16:12:01 -06:00
struct pcap_file {
2013-01-29 21:53:17 -07:00
FILE *f;
2013-02-11 15:50:30 -07:00
uint32_t linktype;
bool swap;
2011-04-21 16:12:01 -06:00
};
struct pcap_file_header {
2013-01-29 21:53:17 -07:00
uint32_t magic;
uint16_t version_major;
uint16_t version_minor;
int32_t thiszone; /* gmt to local correction */
uint32_t sigfigs; /* accuracy of timestamps */
int32_t snaplen; /* max length saved portion of each pkt */
int32_t linktype; /* data link type (LINKTYPE_*) */
2011-04-21 16:12:01 -06:00
};
struct pcap_pkthdr {
2013-01-29 21:53:17 -07:00
struct pcap_timeval {
uint32_t tv_sec;
uint32_t tv_usec;
} ts; /* time stamp */
uint32_t caplen; /* length of portion present */
uint32_t len; /* length this packet (off wire) */
2011-04-21 16:12:01 -06:00
};
#ifndef max
2013-01-29 21:53:17 -07:00
#define max(a, b) ((a)>(b)?(a):(b))
2011-04-21 16:12:01 -06:00
#endif
#ifndef min
2013-01-29 21:53:17 -07:00
#define min(a, b) ((a)<(b)?(a):(b))
2011-04-21 16:12:01 -06:00
#endif
#define bswap32(i) (((i & 0xff000000) >> 030) | \
((i & 0x00ff0000) >> 010) | \
((i & 0x0000ff00) << 010) | \
((i & 0x000000ff) << 030))
#define bswap16(i) (((i & 0xff00) >> 010) | \
((i & 0x00ff) << 010))
2013-01-29 21:53:17 -07:00
/*
2018-07-19 09:37:38 -06:00
* Debugging help
2013-01-29 21:53:17 -07:00
*/
2011-04-21 16:12:01 -06:00
#define DUMPf(fmt, args...) fprintf(stderr, "%s:%s:%d " fmt "\n", __FILE__, __FUNCTION__, __LINE__, ##args)
#define DUMP() DUMPf("")
#define DUMP_d(v) DUMPf("%s = %d", #v, v)
#define DUMP_u(v) DUMPf("%s = %u", #v, v)
#define DUMP_x(v) DUMPf("%s = 0x%x", #v, v)
#define DUMP_s(v) DUMPf("%s = %s", #v, v)
#define DUMP_c(v) DUMPf("%s = %c", #v, v)
#define DUMP_p(v) DUMPf("%s = %p", #v, v)
2013-01-29 21:53:17 -07:00
int pcap_open_in(struct pcap_file *ctx, FILE * f);
int pcap_open_out(struct pcap_file *ctx, FILE * f);
int pcap_open_out_linktype(struct pcap_file *ctx, FILE * f, int32_t linktype);
2011-04-21 16:12:01 -06:00
int pcap_read_pkthdr(struct pcap_file *ctx, struct pcap_pkthdr *hdr);
2011-04-22 12:47:08 -06:00
int pcap_write_pkthdr(struct pcap_file *ctx, struct pcap_pkthdr *hdr);
2011-04-21 16:12:01 -06:00
void pcap_close(struct pcap_file *ctx);
2013-01-29 21:53:17 -07:00
#endif /* __PCAP_H__ */