pcap.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
  2. /*
  3. * Copyright (c) 1993, 1994, 1995, 1996, 1997
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. All advertising materials mentioning features or use of this software
  15. * must display the following acknowledgement:
  16. * This product includes software developed by the Computer Systems
  17. * Engineering Group at Lawrence Berkeley Laboratory.
  18. * 4. Neither the name of the University nor of the Laboratory may be used
  19. * to endorse or promote products derived from this software without
  20. * specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * @(#) $Header: /tcpdump/master/libpcap/pcap/pcap.h,v 1.4.2.11 2008-10-06 15:38:39 gianluca Exp $ (LBL)
  35. */
  36. #ifndef lib_pcap_pcap_h
  37. #define lib_pcap_pcap_h
  38. #if defined(WIN32)
  39. #include <pcap-stdinc.h>
  40. #elif defined(MSDOS)
  41. #include <sys/types.h>
  42. #include <sys/socket.h> /* u_int, u_char etc. */
  43. #else /* UN*X */
  44. #include <sys/types.h>
  45. #include <sys/time.h>
  46. #endif /* WIN32/MSDOS/UN*X */
  47. #ifndef PCAP_DONT_INCLUDE_PCAP_BPF_H
  48. #include <pcap/bpf.h>
  49. #endif
  50. #include <stdio.h>
  51. #ifdef HAVE_REMOTE
  52. // We have to define the SOCKET here, although it has been defined in sockutils.h
  53. // This is to avoid the distribution of the 'sockutils.h' file around
  54. // (for example in the WinPcap developer's pack)
  55. #ifndef SOCKET
  56. #ifdef WIN32
  57. #define SOCKET unsigned int
  58. #else
  59. #define SOCKET int
  60. #endif
  61. #endif
  62. #endif
  63. #ifdef __cplusplus
  64. extern "C" {
  65. #endif
  66. #define PCAP_VERSION_MAJOR 2
  67. #define PCAP_VERSION_MINOR 4
  68. #define PCAP_ERRBUF_SIZE 256
  69. /*
  70. * Compatibility for systems that have a bpf.h that
  71. * predates the bpf typedefs for 64-bit support.
  72. */
  73. #if BPF_RELEASE - 0 < 199406
  74. typedef int bpf_int32;
  75. typedef u_int bpf_u_int32;
  76. #endif
  77. typedef struct pcap pcap_t;
  78. typedef struct pcap_dumper pcap_dumper_t;
  79. typedef struct pcap_if pcap_if_t;
  80. typedef struct pcap_addr pcap_addr_t;
  81. /*
  82. * The first record in the file contains saved values for some
  83. * of the flags used in the printout phases of tcpdump.
  84. * Many fields here are 32 bit ints so compilers won't insert unwanted
  85. * padding; these files need to be interchangeable across architectures.
  86. *
  87. * Do not change the layout of this structure, in any way (this includes
  88. * changes that only affect the length of fields in this structure).
  89. *
  90. * Also, do not change the interpretation of any of the members of this
  91. * structure, in any way (this includes using values other than
  92. * LINKTYPE_ values, as defined in "savefile.c", in the "linktype"
  93. * field).
  94. *
  95. * Instead:
  96. *
  97. * introduce a new structure for the new format, if the layout
  98. * of the structure changed;
  99. *
  100. * send mail to "tcpdump-workers@lists.tcpdump.org", requesting
  101. * a new magic number for your new capture file format, and, when
  102. * you get the new magic number, put it in "savefile.c";
  103. *
  104. * use that magic number for save files with the changed file
  105. * header;
  106. *
  107. * make the code in "savefile.c" capable of reading files with
  108. * the old file header as well as files with the new file header
  109. * (using the magic number to determine the header format).
  110. *
  111. * Then supply the changes as a patch at
  112. *
  113. * http://sourceforge.net/projects/libpcap/
  114. *
  115. * so that future versions of libpcap and programs that use it (such as
  116. * tcpdump) will be able to read your new capture file format.
  117. */
  118. struct pcap_file_header {
  119. bpf_u_int32 magic;
  120. u_short version_major;
  121. u_short version_minor;
  122. bpf_int32 thiszone; /* gmt to local correction */
  123. bpf_u_int32 sigfigs; /* accuracy of timestamps */
  124. bpf_u_int32 snaplen; /* max length saved portion of each pkt */
  125. bpf_u_int32 linktype; /* data link type (LINKTYPE_*) */
  126. };
  127. /*
  128. * Macros for the value returned by pcap_datalink_ext().
  129. *
  130. * If LT_FCS_LENGTH_PRESENT(x) is true, the LT_FCS_LENGTH(x) macro
  131. * gives the FCS length of packets in the capture.
  132. */
  133. #define LT_FCS_LENGTH_PRESENT(x) ((x) & 0x04000000)
  134. #define LT_FCS_LENGTH(x) (((x) & 0xF0000000) >> 28)
  135. #define LT_FCS_DATALINK_EXT(x) ((((x) & 0xF) << 28) | 0x04000000)
  136. typedef enum {
  137. PCAP_D_INOUT = 0,
  138. PCAP_D_IN,
  139. PCAP_D_OUT
  140. } pcap_direction_t;
  141. /*
  142. * Generic per-packet information, as supplied by libpcap.
  143. *
  144. * The time stamp can and should be a "struct timeval", regardless of
  145. * whether your system supports 32-bit tv_sec in "struct timeval",
  146. * 64-bit tv_sec in "struct timeval", or both if it supports both 32-bit
  147. * and 64-bit applications. The on-disk format of savefiles uses 32-bit
  148. * tv_sec (and tv_usec); this structure is irrelevant to that. 32-bit
  149. * and 64-bit versions of libpcap, even if they're on the same platform,
  150. * should supply the appropriate version of "struct timeval", even if
  151. * that's not what the underlying packet capture mechanism supplies.
  152. */
  153. struct pcap_pkthdr {
  154. struct timeval ts; /* time stamp */
  155. bpf_u_int32 caplen; /* length of portion present */
  156. bpf_u_int32 len; /* length this packet (off wire) */
  157. };
  158. /*
  159. * As returned by the pcap_stats()
  160. */
  161. struct pcap_stat {
  162. u_int ps_recv; /* number of packets received */
  163. u_int ps_drop; /* number of packets dropped */
  164. u_int ps_ifdrop; /* drops by interface XXX not yet supported */
  165. #ifdef HAVE_REMOTE
  166. u_int ps_capt; /* number of packets that are received by the application; please get rid off the Win32 ifdef */
  167. u_int ps_sent; /* number of packets sent by the server on the network */
  168. u_int ps_netdrop; /* number of packets lost on the network */
  169. #endif /* HAVE_REMOTE */
  170. };
  171. #ifdef MSDOS
  172. /*
  173. * As returned by the pcap_stats_ex()
  174. */
  175. struct pcap_stat_ex {
  176. u_long rx_packets; /* total packets received */
  177. u_long tx_packets; /* total packets transmitted */
  178. u_long rx_bytes; /* total bytes received */
  179. u_long tx_bytes; /* total bytes transmitted */
  180. u_long rx_errors; /* bad packets received */
  181. u_long tx_errors; /* packet transmit problems */
  182. u_long rx_dropped; /* no space in Rx buffers */
  183. u_long tx_dropped; /* no space available for Tx */
  184. u_long multicast; /* multicast packets received */
  185. u_long collisions;
  186. /* detailed rx_errors: */
  187. u_long rx_length_errors;
  188. u_long rx_over_errors; /* receiver ring buff overflow */
  189. u_long rx_crc_errors; /* recv'd pkt with crc error */
  190. u_long rx_frame_errors; /* recv'd frame alignment error */
  191. u_long rx_fifo_errors; /* recv'r fifo overrun */
  192. u_long rx_missed_errors; /* recv'r missed packet */
  193. /* detailed tx_errors */
  194. u_long tx_aborted_errors;
  195. u_long tx_carrier_errors;
  196. u_long tx_fifo_errors;
  197. u_long tx_heartbeat_errors;
  198. u_long tx_window_errors;
  199. };
  200. #endif
  201. /*
  202. * Item in a list of interfaces.
  203. */
  204. struct pcap_if {
  205. struct pcap_if *next;
  206. char *name; /* name to hand to "pcap_open_live()" */
  207. char *description; /* textual description of interface, or NULL */
  208. struct pcap_addr *addresses;
  209. bpf_u_int32 flags; /* PCAP_IF_ interface flags */
  210. };
  211. #define PCAP_IF_LOOPBACK 0x00000001 /* interface is loopback */
  212. /*
  213. * Representation of an interface address.
  214. */
  215. struct pcap_addr {
  216. struct pcap_addr *next;
  217. struct sockaddr *addr; /* address */
  218. struct sockaddr *netmask; /* netmask for that address */
  219. struct sockaddr *broadaddr; /* broadcast address for that address */
  220. struct sockaddr *dstaddr; /* P2P destination address for that address */
  221. };
  222. typedef void (*pcap_handler)(u_char *, const struct pcap_pkthdr *,
  223. const u_char *);
  224. /*
  225. * Error codes for the pcap API.
  226. * These will all be negative, so you can check for the success or
  227. * failure of a call that returns these codes by checking for a
  228. * negative value.
  229. */
  230. #define PCAP_ERROR -1 /* generic error code */
  231. #define PCAP_ERROR_BREAK -2 /* loop terminated by pcap_breakloop */
  232. #define PCAP_ERROR_NOT_ACTIVATED -3 /* the capture needs to be activated */
  233. #define PCAP_ERROR_ACTIVATED -4 /* the operation can't be performed on already activated captures */
  234. #define PCAP_ERROR_NO_SUCH_DEVICE -5 /* no such device exists */
  235. #define PCAP_ERROR_RFMON_NOTSUP -6 /* this device doesn't support rfmon (monitor) mode */
  236. #define PCAP_ERROR_NOT_RFMON -7 /* operation supported only in monitor mode */
  237. #define PCAP_ERROR_PERM_DENIED -8 /* no permission to open the device */
  238. #define PCAP_ERROR_IFACE_NOT_UP -9 /* interface isn't up */
  239. /*
  240. * Warning codes for the pcap API.
  241. * These will all be positive and non-zero, so they won't look like
  242. * errors.
  243. */
  244. #define PCAP_WARNING 1 /* generic warning code */
  245. #define PCAP_WARNING_PROMISC_NOTSUP 2 /* this device doesn't support promiscuous mode */
  246. char *pcap_lookupdev(char *);
  247. int pcap_lookupnet(const char *, bpf_u_int32 *, bpf_u_int32 *, char *);
  248. pcap_t *pcap_create(const char *, char *);
  249. int pcap_set_snaplen(pcap_t *, int);
  250. int pcap_set_promisc(pcap_t *, int);
  251. int pcap_can_set_rfmon(pcap_t *);
  252. int pcap_set_rfmon(pcap_t *, int);
  253. int pcap_set_timeout(pcap_t *, int);
  254. int pcap_set_buffer_size(pcap_t *, int);
  255. int pcap_activate(pcap_t *);
  256. pcap_t *pcap_open_live(const char *, int, int, int, char *);
  257. pcap_t *pcap_open_dead(int, int);
  258. pcap_t *pcap_open_offline(const char *, char *);
  259. #if defined(WIN32)
  260. pcap_t *pcap_hopen_offline(intptr_t, char *);
  261. #if !defined(LIBPCAP_EXPORTS)
  262. #define pcap_fopen_offline(f,b) \
  263. pcap_hopen_offline(_get_osfhandle(_fileno(f)), b)
  264. #else /*LIBPCAP_EXPORTS*/
  265. static pcap_t *pcap_fopen_offline(FILE *, char *);
  266. #endif
  267. #else /*WIN32*/
  268. pcap_t *pcap_fopen_offline(FILE *, char *);
  269. #endif /*WIN32*/
  270. void pcap_close(pcap_t *);
  271. int pcap_loop(pcap_t *, int, pcap_handler, u_char *);
  272. int pcap_dispatch(pcap_t *, int, pcap_handler, u_char *);
  273. const u_char*
  274. pcap_next(pcap_t *, struct pcap_pkthdr *);
  275. int pcap_next_ex(pcap_t *, struct pcap_pkthdr **, const u_char **);
  276. void pcap_breakloop(pcap_t *);
  277. int pcap_stats(pcap_t *, struct pcap_stat *);
  278. int pcap_setfilter(pcap_t *, struct bpf_program *);
  279. int pcap_setdirection(pcap_t *, pcap_direction_t);
  280. int pcap_getnonblock(pcap_t *, char *);
  281. int pcap_setnonblock(pcap_t *, int, char *);
  282. int pcap_inject(pcap_t *, const void *, size_t);
  283. int pcap_sendpacket(pcap_t *, const u_char *, int);
  284. const char *pcap_statustostr(int);
  285. const char *pcap_strerror(int);
  286. char *pcap_geterr(pcap_t *);
  287. void pcap_perror(pcap_t *, char *);
  288. int pcap_compile(pcap_t *, struct bpf_program *, const char *, int,
  289. bpf_u_int32);
  290. int pcap_compile_nopcap(int, int, struct bpf_program *,
  291. const char *, int, bpf_u_int32);
  292. void pcap_freecode(struct bpf_program *);
  293. int pcap_offline_filter(struct bpf_program *, const struct pcap_pkthdr *,
  294. const u_char *);
  295. int pcap_datalink(pcap_t *);
  296. int pcap_datalink_ext(pcap_t *);
  297. int pcap_list_datalinks(pcap_t *, int **);
  298. int pcap_set_datalink(pcap_t *, int);
  299. void pcap_free_datalinks(int *);
  300. int pcap_datalink_name_to_val(const char *);
  301. const char *pcap_datalink_val_to_name(int);
  302. const char *pcap_datalink_val_to_description(int);
  303. int pcap_snapshot(pcap_t *);
  304. int pcap_is_swapped(pcap_t *);
  305. int pcap_major_version(pcap_t *);
  306. int pcap_minor_version(pcap_t *);
  307. /* XXX */
  308. FILE *pcap_file(pcap_t *);
  309. int pcap_fileno(pcap_t *);
  310. pcap_dumper_t *pcap_dump_open(pcap_t *, const char *);
  311. pcap_dumper_t *pcap_dump_fopen(pcap_t *, FILE *fp);
  312. FILE *pcap_dump_file(pcap_dumper_t *);
  313. long pcap_dump_ftell(pcap_dumper_t *);
  314. int pcap_dump_flush(pcap_dumper_t *);
  315. void pcap_dump_close(pcap_dumper_t *);
  316. void pcap_dump(u_char *, const struct pcap_pkthdr *, const u_char *);
  317. int pcap_findalldevs(pcap_if_t **, char *);
  318. void pcap_freealldevs(pcap_if_t *);
  319. const char *pcap_lib_version(void);
  320. /* XXX this guy lives in the bpf tree */
  321. u_int bpf_filter(const struct bpf_insn *, const u_char *, u_int, u_int);
  322. int bpf_validate(const struct bpf_insn *f, int len);
  323. char *bpf_image(const struct bpf_insn *, int);
  324. void bpf_dump(const struct bpf_program *, int);
  325. #if defined(WIN32)
  326. /*
  327. * Win32 definitions
  328. */
  329. int pcap_setbuff(pcap_t *p, int dim);
  330. int pcap_setmode(pcap_t *p, int mode);
  331. int pcap_setmintocopy(pcap_t *p, int size);
  332. #ifdef WPCAP
  333. /* Include file with the wpcap-specific extensions */
  334. #include <Win32-Extensions.h>
  335. #endif /* WPCAP */
  336. #define MODE_CAPT 0
  337. #define MODE_STAT 1
  338. #define MODE_MON 2
  339. #elif defined(MSDOS)
  340. /*
  341. * MS-DOS definitions
  342. */
  343. int pcap_stats_ex (pcap_t *, struct pcap_stat_ex *);
  344. void pcap_set_wait (pcap_t *p, void (*yield)(void), int wait);
  345. u_long pcap_mac_packets (void);
  346. #else /* UN*X */
  347. /*
  348. * UN*X definitions
  349. */
  350. int pcap_get_selectable_fd(pcap_t *);
  351. #endif /* WIN32/MSDOS/UN*X */
  352. #ifdef HAVE_REMOTE
  353. /* Includes most of the public stuff that is needed for the remote capture */
  354. #include <remote-ext.h>
  355. #endif /* HAVE_REMOTE */
  356. #ifdef __cplusplus
  357. }
  358. #endif
  359. #endif