pcap.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  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. /*
  35. * Remote packet capture mechanisms and extensions from WinPcap:
  36. *
  37. * Copyright (c) 2002 - 2003
  38. * NetGroup, Politecnico di Torino (Italy)
  39. * All rights reserved.
  40. *
  41. * Redistribution and use in source and binary forms, with or without
  42. * modification, are permitted provided that the following conditions
  43. * are met:
  44. *
  45. * 1. Redistributions of source code must retain the above copyright
  46. * notice, this list of conditions and the following disclaimer.
  47. * 2. Redistributions in binary form must reproduce the above copyright
  48. * notice, this list of conditions and the following disclaimer in the
  49. * documentation and/or other materials provided with the distribution.
  50. * 3. Neither the name of the Politecnico di Torino nor the names of its
  51. * contributors may be used to endorse or promote products derived from
  52. * this software without specific prior written permission.
  53. *
  54. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  55. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  56. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  57. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  58. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  59. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  60. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  61. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  62. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  63. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  64. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  65. *
  66. */
  67. #ifndef lib_pcap_pcap_h
  68. #define lib_pcap_pcap_h
  69. #include <pcap/funcattrs.h>
  70. #include <pcap/pcap-inttypes.h>
  71. #if defined(_WIN32)
  72. #include <winsock2.h> /* u_int, u_char etc. */
  73. #include <io.h> /* _get_osfhandle() */
  74. #elif defined(MSDOS)
  75. #include <sys/types.h> /* u_int, u_char etc. */
  76. #include <sys/socket.h>
  77. #else /* UN*X */
  78. #include <sys/types.h> /* u_int, u_char etc. */
  79. #include <sys/time.h>
  80. #endif /* _WIN32/MSDOS/UN*X */
  81. #ifndef PCAP_DONT_INCLUDE_PCAP_BPF_H
  82. #include <pcap/bpf.h>
  83. #endif
  84. #include <stdio.h>
  85. #ifdef __cplusplus
  86. extern "C" {
  87. #endif
  88. /*
  89. * Version number of the current version of the pcap file format.
  90. *
  91. * NOTE: this is *NOT* the version number of the libpcap library.
  92. * To fetch the version information for the version of libpcap
  93. * you're using, use pcap_lib_version().
  94. */
  95. #define PCAP_VERSION_MAJOR 2
  96. #define PCAP_VERSION_MINOR 4
  97. #define PCAP_ERRBUF_SIZE 256
  98. /*
  99. * Compatibility for systems that have a bpf.h that
  100. * predates the bpf typedefs for 64-bit support.
  101. */
  102. #if BPF_RELEASE - 0 < 199406
  103. typedef int bpf_int32;
  104. typedef u_int bpf_u_int32;
  105. #endif
  106. typedef struct pcap pcap_t;
  107. typedef struct pcap_dumper pcap_dumper_t;
  108. typedef struct pcap_if pcap_if_t;
  109. typedef struct pcap_addr pcap_addr_t;
  110. /*
  111. * The first record in the file contains saved values for some
  112. * of the flags used in the printout phases of tcpdump.
  113. * Many fields here are 32 bit ints so compilers won't insert unwanted
  114. * padding; these files need to be interchangeable across architectures.
  115. *
  116. * Do not change the layout of this structure, in any way (this includes
  117. * changes that only affect the length of fields in this structure).
  118. *
  119. * Also, do not change the interpretation of any of the members of this
  120. * structure, in any way (this includes using values other than
  121. * LINKTYPE_ values, as defined in "savefile.c", in the "linktype"
  122. * field).
  123. *
  124. * Instead:
  125. *
  126. * introduce a new structure for the new format, if the layout
  127. * of the structure changed;
  128. *
  129. * send mail to "tcpdump-workers@lists.tcpdump.org", requesting
  130. * a new magic number for your new capture file format, and, when
  131. * you get the new magic number, put it in "savefile.c";
  132. *
  133. * use that magic number for save files with the changed file
  134. * header;
  135. *
  136. * make the code in "savefile.c" capable of reading files with
  137. * the old file header as well as files with the new file header
  138. * (using the magic number to determine the header format).
  139. *
  140. * Then supply the changes by forking the branch at
  141. *
  142. * https://github.com/the-tcpdump-group/libpcap/issues
  143. *
  144. * and issuing a pull request, so that future versions of libpcap and
  145. * programs that use it (such as tcpdump) will be able to read your new
  146. * capture file format.
  147. */
  148. struct pcap_file_header {
  149. bpf_u_int32 magic;
  150. u_short version_major;
  151. u_short version_minor;
  152. bpf_int32 thiszone; /* gmt to local correction */
  153. bpf_u_int32 sigfigs; /* accuracy of timestamps */
  154. bpf_u_int32 snaplen; /* max length saved portion of each pkt */
  155. bpf_u_int32 linktype; /* data link type (LINKTYPE_*) */
  156. };
  157. /*
  158. * Macros for the value returned by pcap_datalink_ext().
  159. *
  160. * If LT_FCS_LENGTH_PRESENT(x) is true, the LT_FCS_LENGTH(x) macro
  161. * gives the FCS length of packets in the capture.
  162. */
  163. #define LT_FCS_LENGTH_PRESENT(x) ((x) & 0x04000000)
  164. #define LT_FCS_LENGTH(x) (((x) & 0xF0000000) >> 28)
  165. #define LT_FCS_DATALINK_EXT(x) ((((x) & 0xF) << 28) | 0x04000000)
  166. typedef enum {
  167. PCAP_D_INOUT = 0,
  168. PCAP_D_IN,
  169. PCAP_D_OUT
  170. } pcap_direction_t;
  171. /*
  172. * Generic per-packet information, as supplied by libpcap.
  173. *
  174. * The time stamp can and should be a "struct timeval", regardless of
  175. * whether your system supports 32-bit tv_sec in "struct timeval",
  176. * 64-bit tv_sec in "struct timeval", or both if it supports both 32-bit
  177. * and 64-bit applications. The on-disk format of savefiles uses 32-bit
  178. * tv_sec (and tv_usec); this structure is irrelevant to that. 32-bit
  179. * and 64-bit versions of libpcap, even if they're on the same platform,
  180. * should supply the appropriate version of "struct timeval", even if
  181. * that's not what the underlying packet capture mechanism supplies.
  182. */
  183. struct pcap_pkthdr {
  184. struct timeval ts; /* time stamp */
  185. bpf_u_int32 caplen; /* length of portion present */
  186. bpf_u_int32 len; /* length this packet (off wire) */
  187. };
  188. /*
  189. * As returned by the pcap_stats()
  190. */
  191. struct pcap_stat {
  192. u_int ps_recv; /* number of packets received */
  193. u_int ps_drop; /* number of packets dropped */
  194. u_int ps_ifdrop; /* drops by interface -- only supported on some platforms */
  195. #ifdef _WIN32
  196. u_int ps_capt; /* number of packets that reach the application */
  197. u_int ps_sent; /* number of packets sent by the server on the network */
  198. u_int ps_netdrop; /* number of packets lost on the network */
  199. #endif /* _WIN32 */
  200. };
  201. #ifdef MSDOS
  202. /*
  203. * As returned by the pcap_stats_ex()
  204. */
  205. struct pcap_stat_ex {
  206. u_long rx_packets; /* total packets received */
  207. u_long tx_packets; /* total packets transmitted */
  208. u_long rx_bytes; /* total bytes received */
  209. u_long tx_bytes; /* total bytes transmitted */
  210. u_long rx_errors; /* bad packets received */
  211. u_long tx_errors; /* packet transmit problems */
  212. u_long rx_dropped; /* no space in Rx buffers */
  213. u_long tx_dropped; /* no space available for Tx */
  214. u_long multicast; /* multicast packets received */
  215. u_long collisions;
  216. /* detailed rx_errors: */
  217. u_long rx_length_errors;
  218. u_long rx_over_errors; /* receiver ring buff overflow */
  219. u_long rx_crc_errors; /* recv'd pkt with crc error */
  220. u_long rx_frame_errors; /* recv'd frame alignment error */
  221. u_long rx_fifo_errors; /* recv'r fifo overrun */
  222. u_long rx_missed_errors; /* recv'r missed packet */
  223. /* detailed tx_errors */
  224. u_long tx_aborted_errors;
  225. u_long tx_carrier_errors;
  226. u_long tx_fifo_errors;
  227. u_long tx_heartbeat_errors;
  228. u_long tx_window_errors;
  229. };
  230. #endif
  231. /*
  232. * Item in a list of interfaces.
  233. */
  234. struct pcap_if {
  235. struct pcap_if *next;
  236. char *name; /* name to hand to "pcap_open_live()" */
  237. char *description; /* textual description of interface, or NULL */
  238. struct pcap_addr *addresses;
  239. bpf_u_int32 flags; /* PCAP_IF_ interface flags */
  240. };
  241. #define PCAP_IF_LOOPBACK 0x00000001 /* interface is loopback */
  242. #define PCAP_IF_UP 0x00000002 /* interface is up */
  243. #define PCAP_IF_RUNNING 0x00000004 /* interface is running */
  244. #define PCAP_IF_WIRELESS 0x00000008 /* interface is wireless (*NOT* necessarily Wi-Fi!) */
  245. #define PCAP_IF_CONNECTION_STATUS 0x00000030 /* connection status: */
  246. #define PCAP_IF_CONNECTION_STATUS_UNKNOWN 0x00000000 /* unknown */
  247. #define PCAP_IF_CONNECTION_STATUS_CONNECTED 0x00000010 /* connected */
  248. #define PCAP_IF_CONNECTION_STATUS_DISCONNECTED 0x00000020 /* disconnected */
  249. #define PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE 0x00000030 /* not applicable */
  250. /*
  251. * Representation of an interface address.
  252. */
  253. struct pcap_addr {
  254. struct pcap_addr *next;
  255. struct sockaddr *addr; /* address */
  256. struct sockaddr *netmask; /* netmask for that address */
  257. struct sockaddr *broadaddr; /* broadcast address for that address */
  258. struct sockaddr *dstaddr; /* P2P destination address for that address */
  259. };
  260. typedef void (*pcap_handler)(u_char *, const struct pcap_pkthdr *,
  261. const u_char *);
  262. /*
  263. * Error codes for the pcap API.
  264. * These will all be negative, so you can check for the success or
  265. * failure of a call that returns these codes by checking for a
  266. * negative value.
  267. */
  268. #define PCAP_ERROR -1 /* generic error code */
  269. #define PCAP_ERROR_BREAK -2 /* loop terminated by pcap_breakloop */
  270. #define PCAP_ERROR_NOT_ACTIVATED -3 /* the capture needs to be activated */
  271. #define PCAP_ERROR_ACTIVATED -4 /* the operation can't be performed on already activated captures */
  272. #define PCAP_ERROR_NO_SUCH_DEVICE -5 /* no such device exists */
  273. #define PCAP_ERROR_RFMON_NOTSUP -6 /* this device doesn't support rfmon (monitor) mode */
  274. #define PCAP_ERROR_NOT_RFMON -7 /* operation supported only in monitor mode */
  275. #define PCAP_ERROR_PERM_DENIED -8 /* no permission to open the device */
  276. #define PCAP_ERROR_IFACE_NOT_UP -9 /* interface isn't up */
  277. #define PCAP_ERROR_CANTSET_TSTAMP_TYPE -10 /* this device doesn't support setting the time stamp type */
  278. #define PCAP_ERROR_PROMISC_PERM_DENIED -11 /* you don't have permission to capture in promiscuous mode */
  279. #define PCAP_ERROR_TSTAMP_PRECISION_NOTSUP -12 /* the requested time stamp precision is not supported */
  280. /*
  281. * Warning codes for the pcap API.
  282. * These will all be positive and non-zero, so they won't look like
  283. * errors.
  284. */
  285. #define PCAP_WARNING 1 /* generic warning code */
  286. #define PCAP_WARNING_PROMISC_NOTSUP 2 /* this device doesn't support promiscuous mode */
  287. #define PCAP_WARNING_TSTAMP_TYPE_NOTSUP 3 /* the requested time stamp type is not supported */
  288. /*
  289. * Value to pass to pcap_compile() as the netmask if you don't know what
  290. * the netmask is.
  291. */
  292. #define PCAP_NETMASK_UNKNOWN 0xffffffff
  293. /*
  294. * We're deprecating pcap_lookupdev() for various reasons (not
  295. * thread-safe, can behave weirdly with WinPcap). Callers
  296. * should use pcap_findalldevs() and use the first device.
  297. */
  298. PCAP_API char *pcap_lookupdev(char *)
  299. PCAP_DEPRECATED(pcap_lookupdev, "use 'pcap_findalldevs' and use the first device");
  300. PCAP_API int pcap_lookupnet(const char *, bpf_u_int32 *, bpf_u_int32 *, char *);
  301. PCAP_API pcap_t *pcap_create(const char *, char *);
  302. PCAP_API int pcap_set_snaplen(pcap_t *, int);
  303. PCAP_API int pcap_set_promisc(pcap_t *, int);
  304. PCAP_API int pcap_can_set_rfmon(pcap_t *);
  305. PCAP_API int pcap_set_rfmon(pcap_t *, int);
  306. PCAP_API int pcap_set_timeout(pcap_t *, int);
  307. PCAP_API int pcap_set_tstamp_type(pcap_t *, int);
  308. PCAP_API int pcap_set_immediate_mode(pcap_t *, int);
  309. PCAP_API int pcap_set_buffer_size(pcap_t *, int);
  310. PCAP_API int pcap_set_tstamp_precision(pcap_t *, int);
  311. PCAP_API int pcap_get_tstamp_precision(pcap_t *);
  312. PCAP_API int pcap_activate(pcap_t *);
  313. PCAP_API int pcap_list_tstamp_types(pcap_t *, int **);
  314. PCAP_API void pcap_free_tstamp_types(int *);
  315. PCAP_API int pcap_tstamp_type_name_to_val(const char *);
  316. PCAP_API const char *pcap_tstamp_type_val_to_name(int);
  317. PCAP_API const char *pcap_tstamp_type_val_to_description(int);
  318. #ifdef __linux__
  319. PCAP_API int pcap_set_protocol_linux(pcap_t *, int);
  320. #endif
  321. /*
  322. * Time stamp types.
  323. * Not all systems and interfaces will necessarily support all of these.
  324. *
  325. * A system that supports PCAP_TSTAMP_HOST is offering time stamps
  326. * provided by the host machine, rather than by the capture device,
  327. * but not committing to any characteristics of the time stamp;
  328. * it will not offer any of the PCAP_TSTAMP_HOST_ subtypes.
  329. *
  330. * PCAP_TSTAMP_HOST_LOWPREC is a time stamp, provided by the host machine,
  331. * that's low-precision but relatively cheap to fetch; it's normally done
  332. * using the system clock, so it's normally synchronized with times you'd
  333. * fetch from system calls.
  334. *
  335. * PCAP_TSTAMP_HOST_HIPREC is a time stamp, provided by the host machine,
  336. * that's high-precision; it might be more expensive to fetch. It might
  337. * or might not be synchronized with the system clock, and might have
  338. * problems with time stamps for packets received on different CPUs,
  339. * depending on the platform.
  340. *
  341. * PCAP_TSTAMP_ADAPTER is a high-precision time stamp supplied by the
  342. * capture device; it's synchronized with the system clock.
  343. *
  344. * PCAP_TSTAMP_ADAPTER_UNSYNCED is a high-precision time stamp supplied by
  345. * the capture device; it's not synchronized with the system clock.
  346. *
  347. * Note that time stamps synchronized with the system clock can go
  348. * backwards, as the system clock can go backwards. If a clock is
  349. * not in sync with the system clock, that could be because the
  350. * system clock isn't keeping accurate time, because the other
  351. * clock isn't keeping accurate time, or both.
  352. *
  353. * Note that host-provided time stamps generally correspond to the
  354. * time when the time-stamping code sees the packet; this could
  355. * be some unknown amount of time after the first or last bit of
  356. * the packet is received by the network adapter, due to batching
  357. * of interrupts for packet arrival, queueing delays, etc..
  358. */
  359. #define PCAP_TSTAMP_HOST 0 /* host-provided, unknown characteristics */
  360. #define PCAP_TSTAMP_HOST_LOWPREC 1 /* host-provided, low precision */
  361. #define PCAP_TSTAMP_HOST_HIPREC 2 /* host-provided, high precision */
  362. #define PCAP_TSTAMP_ADAPTER 3 /* device-provided, synced with the system clock */
  363. #define PCAP_TSTAMP_ADAPTER_UNSYNCED 4 /* device-provided, not synced with the system clock */
  364. /*
  365. * Time stamp resolution types.
  366. * Not all systems and interfaces will necessarily support all of these
  367. * resolutions when doing live captures; all of them can be requested
  368. * when reading a savefile.
  369. */
  370. #define PCAP_TSTAMP_PRECISION_MICRO 0 /* use timestamps with microsecond precision, default */
  371. #define PCAP_TSTAMP_PRECISION_NANO 1 /* use timestamps with nanosecond precision */
  372. PCAP_API pcap_t *pcap_open_live(const char *, int, int, int, char *);
  373. PCAP_API pcap_t *pcap_open_dead(int, int);
  374. PCAP_API pcap_t *pcap_open_dead_with_tstamp_precision(int, int, u_int);
  375. PCAP_API pcap_t *pcap_open_offline_with_tstamp_precision(const char *, u_int, char *);
  376. PCAP_API pcap_t *pcap_open_offline(const char *, char *);
  377. #ifdef _WIN32
  378. PCAP_API pcap_t *pcap_hopen_offline_with_tstamp_precision(intptr_t, u_int, char *);
  379. PCAP_API pcap_t *pcap_hopen_offline(intptr_t, char *);
  380. /*
  381. * If we're building libpcap, these are internal routines in savefile.c,
  382. * so we must not define them as macros.
  383. *
  384. * If we're not building libpcap, given that the version of the C runtime
  385. * with which libpcap was built might be different from the version
  386. * of the C runtime with which an application using libpcap was built,
  387. * and that a FILE structure may differ between the two versions of the
  388. * C runtime, calls to _fileno() must use the version of _fileno() in
  389. * the C runtime used to open the FILE *, not the version in the C
  390. * runtime with which libpcap was built. (Maybe once the Universal CRT
  391. * rules the world, this will cease to be a problem.)
  392. */
  393. #ifndef BUILDING_PCAP
  394. #define pcap_fopen_offline_with_tstamp_precision(f,p,b) \
  395. pcap_hopen_offline_with_tstamp_precision(_get_osfhandle(_fileno(f)), p, b)
  396. #define pcap_fopen_offline(f,b) \
  397. pcap_hopen_offline(_get_osfhandle(_fileno(f)), b)
  398. #endif
  399. #else /*_WIN32*/
  400. PCAP_API pcap_t *pcap_fopen_offline_with_tstamp_precision(FILE *, u_int, char *);
  401. PCAP_API pcap_t *pcap_fopen_offline(FILE *, char *);
  402. #endif /*_WIN32*/
  403. PCAP_API void pcap_close(pcap_t *);
  404. PCAP_API int pcap_loop(pcap_t *, int, pcap_handler, u_char *);
  405. PCAP_API int pcap_dispatch(pcap_t *, int, pcap_handler, u_char *);
  406. PCAP_API const u_char *pcap_next(pcap_t *, struct pcap_pkthdr *);
  407. PCAP_API int pcap_next_ex(pcap_t *, struct pcap_pkthdr **, const u_char **);
  408. PCAP_API void pcap_breakloop(pcap_t *);
  409. PCAP_API int pcap_stats(pcap_t *, struct pcap_stat *);
  410. PCAP_API int pcap_setfilter(pcap_t *, struct bpf_program *);
  411. PCAP_API int pcap_setdirection(pcap_t *, pcap_direction_t);
  412. PCAP_API int pcap_getnonblock(pcap_t *, char *);
  413. PCAP_API int pcap_setnonblock(pcap_t *, int, char *);
  414. PCAP_API int pcap_inject(pcap_t *, const void *, size_t);
  415. PCAP_API int pcap_sendpacket(pcap_t *, const u_char *, int);
  416. PCAP_API const char *pcap_statustostr(int);
  417. PCAP_API const char *pcap_strerror(int);
  418. PCAP_API char *pcap_geterr(pcap_t *);
  419. PCAP_API void pcap_perror(pcap_t *, const char *);
  420. PCAP_API int pcap_compile(pcap_t *, struct bpf_program *, const char *, int,
  421. bpf_u_int32);
  422. PCAP_API int pcap_compile_nopcap(int, int, struct bpf_program *,
  423. const char *, int, bpf_u_int32);
  424. PCAP_API void pcap_freecode(struct bpf_program *);
  425. PCAP_API int pcap_offline_filter(const struct bpf_program *,
  426. const struct pcap_pkthdr *, const u_char *);
  427. PCAP_API int pcap_datalink(pcap_t *);
  428. PCAP_API int pcap_datalink_ext(pcap_t *);
  429. PCAP_API int pcap_list_datalinks(pcap_t *, int **);
  430. PCAP_API int pcap_set_datalink(pcap_t *, int);
  431. PCAP_API void pcap_free_datalinks(int *);
  432. PCAP_API int pcap_datalink_name_to_val(const char *);
  433. PCAP_API const char *pcap_datalink_val_to_name(int);
  434. PCAP_API const char *pcap_datalink_val_to_description(int);
  435. PCAP_API int pcap_snapshot(pcap_t *);
  436. PCAP_API int pcap_is_swapped(pcap_t *);
  437. PCAP_API int pcap_major_version(pcap_t *);
  438. PCAP_API int pcap_minor_version(pcap_t *);
  439. PCAP_API int pcap_bufsize(pcap_t *);
  440. /* XXX */
  441. PCAP_API FILE *pcap_file(pcap_t *);
  442. PCAP_API int pcap_fileno(pcap_t *);
  443. #ifdef _WIN32
  444. PCAP_API int pcap_wsockinit(void);
  445. #endif
  446. PCAP_API pcap_dumper_t *pcap_dump_open(pcap_t *, const char *);
  447. PCAP_API pcap_dumper_t *pcap_dump_fopen(pcap_t *, FILE *fp);
  448. PCAP_API pcap_dumper_t *pcap_dump_open_append(pcap_t *, const char *);
  449. PCAP_API FILE *pcap_dump_file(pcap_dumper_t *);
  450. PCAP_API long pcap_dump_ftell(pcap_dumper_t *);
  451. PCAP_API int64_t pcap_dump_ftell64(pcap_dumper_t *);
  452. PCAP_API int pcap_dump_flush(pcap_dumper_t *);
  453. PCAP_API void pcap_dump_close(pcap_dumper_t *);
  454. PCAP_API void pcap_dump(u_char *, const struct pcap_pkthdr *, const u_char *);
  455. PCAP_API int pcap_findalldevs(pcap_if_t **, char *);
  456. PCAP_API void pcap_freealldevs(pcap_if_t *);
  457. /*
  458. * We return a pointer to the version string, rather than exporting the
  459. * version string directly.
  460. *
  461. * On at least some UNIXes, if you import data from a shared library into
  462. * an program, the data is bound into the program binary, so if the string
  463. * in the version of the library with which the program was linked isn't
  464. * the same as the string in the version of the library with which the
  465. * program is being run, various undesirable things may happen (warnings,
  466. * the string being the one from the version of the library with which the
  467. * program was linked, or even weirder things, such as the string being the
  468. * one from the library but being truncated).
  469. *
  470. * On Windows, the string is constructed at run time.
  471. */
  472. PCAP_API const char *pcap_lib_version(void);
  473. /*
  474. * On at least some versions of NetBSD and QNX, we don't want to declare
  475. * bpf_filter() here, as it's also be declared in <net/bpf.h>, with a
  476. * different signature, but, on other BSD-flavored UN*Xes, it's not
  477. * declared in <net/bpf.h>, so we *do* want to declare it here, so it's
  478. * declared when we build pcap-bpf.c.
  479. */
  480. #if !defined(__NetBSD__) && !defined(__QNX__)
  481. PCAP_API u_int bpf_filter(const struct bpf_insn *, const u_char *, u_int, u_int);
  482. #endif
  483. PCAP_API int bpf_validate(const struct bpf_insn *f, int len);
  484. PCAP_API char *bpf_image(const struct bpf_insn *, int);
  485. PCAP_API void bpf_dump(const struct bpf_program *, int);
  486. #if defined(_WIN32)
  487. /*
  488. * Win32 definitions
  489. */
  490. /*!
  491. \brief A queue of raw packets that will be sent to the network with pcap_sendqueue_transmit().
  492. */
  493. struct pcap_send_queue
  494. {
  495. u_int maxlen; /* Maximum size of the queue, in bytes. This
  496. variable contains the size of the buffer field. */
  497. u_int len; /* Current size of the queue, in bytes. */
  498. char *buffer; /* Buffer containing the packets to be sent. */
  499. };
  500. typedef struct pcap_send_queue pcap_send_queue;
  501. /*!
  502. \brief This typedef is a support for the pcap_get_airpcap_handle() function
  503. */
  504. #if !defined(AIRPCAP_HANDLE__EAE405F5_0171_9592_B3C2_C19EC426AD34__DEFINED_)
  505. #define AIRPCAP_HANDLE__EAE405F5_0171_9592_B3C2_C19EC426AD34__DEFINED_
  506. typedef struct _AirpcapHandle *PAirpcapHandle;
  507. #endif
  508. PCAP_API int pcap_setbuff(pcap_t *p, int dim);
  509. PCAP_API int pcap_setmode(pcap_t *p, int mode);
  510. PCAP_API int pcap_setmintocopy(pcap_t *p, int size);
  511. PCAP_API HANDLE pcap_getevent(pcap_t *p);
  512. PCAP_API int pcap_oid_get_request(pcap_t *, bpf_u_int32, void *, size_t *);
  513. PCAP_API int pcap_oid_set_request(pcap_t *, bpf_u_int32, const void *, size_t *);
  514. PCAP_API pcap_send_queue* pcap_sendqueue_alloc(u_int memsize);
  515. PCAP_API void pcap_sendqueue_destroy(pcap_send_queue* queue);
  516. PCAP_API int pcap_sendqueue_queue(pcap_send_queue* queue, const struct pcap_pkthdr *pkt_header, const u_char *pkt_data);
  517. PCAP_API u_int pcap_sendqueue_transmit(pcap_t *p, pcap_send_queue* queue, int sync);
  518. PCAP_API struct pcap_stat *pcap_stats_ex(pcap_t *p, int *pcap_stat_size);
  519. PCAP_API int pcap_setuserbuffer(pcap_t *p, int size);
  520. PCAP_API int pcap_live_dump(pcap_t *p, char *filename, int maxsize, int maxpacks);
  521. PCAP_API int pcap_live_dump_ended(pcap_t *p, int sync);
  522. PCAP_API int pcap_start_oem(char* err_str, int flags);
  523. PCAP_API PAirpcapHandle pcap_get_airpcap_handle(pcap_t *p);
  524. #define MODE_CAPT 0
  525. #define MODE_STAT 1
  526. #define MODE_MON 2
  527. #elif defined(MSDOS)
  528. /*
  529. * MS-DOS definitions
  530. */
  531. PCAP_API int pcap_stats_ex (pcap_t *, struct pcap_stat_ex *);
  532. PCAP_API void pcap_set_wait (pcap_t *p, void (*yield)(void), int wait);
  533. PCAP_API u_long pcap_mac_packets (void);
  534. #else /* UN*X */
  535. /*
  536. * UN*X definitions
  537. */
  538. PCAP_API int pcap_get_selectable_fd(pcap_t *);
  539. PCAP_API struct timeval *pcap_get_required_select_timeout(pcap_t *);
  540. #endif /* _WIN32/MSDOS/UN*X */
  541. /*
  542. * Remote capture definitions.
  543. *
  544. * These routines are only present if libpcap has been configured to
  545. * include remote capture support.
  546. */
  547. /*
  548. * The maximum buffer size in which address, port, interface names are kept.
  549. *
  550. * In case the adapter name or such is larger than this value, it is truncated.
  551. * This is not used by the user; however it must be aware that an hostname / interface
  552. * name longer than this value will be truncated.
  553. */
  554. #define PCAP_BUF_SIZE 1024
  555. /*
  556. * The type of input source, passed to pcap_open().
  557. */
  558. #define PCAP_SRC_FILE 2 /* local savefile */
  559. #define PCAP_SRC_IFLOCAL 3 /* local network interface */
  560. #define PCAP_SRC_IFREMOTE 4 /* interface on a remote host, using RPCAP */
  561. /*
  562. * The formats allowed by pcap_open() are the following:
  563. * - file://path_and_filename [opens a local file]
  564. * - rpcap://devicename [opens the selected device devices available on the local host, without using the RPCAP protocol]
  565. * - rpcap://host/devicename [opens the selected device available on a remote host]
  566. * - rpcap://host:port/devicename [opens the selected device available on a remote host, using a non-standard port for RPCAP]
  567. * - adaptername [to open a local adapter; kept for compability, but it is strongly discouraged]
  568. * - (NULL) [to open the first local adapter; kept for compability, but it is strongly discouraged]
  569. *
  570. * The formats allowed by the pcap_findalldevs_ex() are the following:
  571. * - file://folder/ [lists all the files in the given folder]
  572. * - rpcap:// [lists all local adapters]
  573. * - rpcap://host:port/ [lists the devices available on a remote host]
  574. *
  575. * Referring to the 'host' and 'port' parameters, they can be either numeric or literal. Since
  576. * IPv6 is fully supported, these are the allowed formats:
  577. *
  578. * - host (literal): e.g. host.foo.bar
  579. * - host (numeric IPv4): e.g. 10.11.12.13
  580. * - host (numeric IPv4, IPv6 style): e.g. [10.11.12.13]
  581. * - host (numeric IPv6): e.g. [1:2:3::4]
  582. * - port: can be either numeric (e.g. '80') or literal (e.g. 'http')
  583. *
  584. * Here you find some allowed examples:
  585. * - rpcap://host.foo.bar/devicename [everything literal, no port number]
  586. * - rpcap://host.foo.bar:1234/devicename [everything literal, with port number]
  587. * - rpcap://10.11.12.13/devicename [IPv4 numeric, no port number]
  588. * - rpcap://10.11.12.13:1234/devicename [IPv4 numeric, with port number]
  589. * - rpcap://[10.11.12.13]:1234/devicename [IPv4 numeric with IPv6 format, with port number]
  590. * - rpcap://[1:2:3::4]/devicename [IPv6 numeric, no port number]
  591. * - rpcap://[1:2:3::4]:1234/devicename [IPv6 numeric, with port number]
  592. * - rpcap://[1:2:3::4]:http/devicename [IPv6 numeric, with literal port number]
  593. */
  594. /*
  595. * URL schemes for capture source.
  596. */
  597. /*
  598. * This string indicates that the user wants to open a capture from a
  599. * local file.
  600. */
  601. #define PCAP_SRC_FILE_STRING "file://"
  602. /*
  603. * This string indicates that the user wants to open a capture from a
  604. * network interface. This string does not necessarily involve the use
  605. * of the RPCAP protocol. If the interface required resides on the local
  606. * host, the RPCAP protocol is not involved and the local functions are used.
  607. */
  608. #define PCAP_SRC_IF_STRING "rpcap://"
  609. /*
  610. * Flags to pass to pcap_open().
  611. */
  612. /*
  613. * Specifies whether promiscuous mode is to be used.
  614. */
  615. #define PCAP_OPENFLAG_PROMISCUOUS 0x00000001
  616. /*
  617. * Specifies, for an RPCAP capture, whether the data transfer (in
  618. * case of a remote capture) has to be done with UDP protocol.
  619. *
  620. * If it is '1' if you want a UDP data connection, '0' if you want
  621. * a TCP data connection; control connection is always TCP-based.
  622. * A UDP connection is much lighter, but it does not guarantee that all
  623. * the captured packets arrive to the client workstation. Moreover,
  624. * it could be harmful in case of network congestion.
  625. * This flag is meaningless if the source is not a remote interface.
  626. * In that case, it is simply ignored.
  627. */
  628. #define PCAP_OPENFLAG_DATATX_UDP 0x00000002
  629. /*
  630. * Specifies wheether the remote probe will capture its own generated
  631. * traffic.
  632. *
  633. * In case the remote probe uses the same interface to capture traffic
  634. * and to send data back to the caller, the captured traffic includes
  635. * the RPCAP traffic as well. If this flag is turned on, the RPCAP
  636. * traffic is excluded from the capture, so that the trace returned
  637. * back to the collector is does not include this traffic.
  638. *
  639. * Has no effect on local interfaces or savefiles.
  640. */
  641. #define PCAP_OPENFLAG_NOCAPTURE_RPCAP 0x00000004
  642. /*
  643. * Specifies whether the local adapter will capture its own generated traffic.
  644. *
  645. * This flag tells the underlying capture driver to drop the packets
  646. * that were sent by itself. This is useful when building applications
  647. * such as bridges that should ignore the traffic they just sent.
  648. *
  649. * Supported only on Windows.
  650. */
  651. #define PCAP_OPENFLAG_NOCAPTURE_LOCAL 0x00000008
  652. /*
  653. * This flag configures the adapter for maximum responsiveness.
  654. *
  655. * In presence of a large value for nbytes, WinPcap waits for the arrival
  656. * of several packets before copying the data to the user. This guarantees
  657. * a low number of system calls, i.e. lower processor usage, i.e. better
  658. * performance, which is good for applications like sniffers. If the user
  659. * sets the PCAP_OPENFLAG_MAX_RESPONSIVENESS flag, the capture driver will
  660. * copy the packets as soon as the application is ready to receive them.
  661. * This is suggested for real time applications (such as, for example,
  662. * a bridge) that need the best responsiveness.
  663. *
  664. * The equivalent with pcap_create()/pcap_activate() is "immediate mode".
  665. */
  666. #define PCAP_OPENFLAG_MAX_RESPONSIVENESS 0x00000010
  667. /*
  668. * Remote authentication methods.
  669. * These are used in the 'type' member of the pcap_rmtauth structure.
  670. */
  671. /*
  672. * NULL authentication.
  673. *
  674. * The 'NULL' authentication has to be equal to 'zero', so that old
  675. * applications can just put every field of struct pcap_rmtauth to zero,
  676. * and it does work.
  677. */
  678. #define RPCAP_RMTAUTH_NULL 0
  679. /*
  680. * Username/password authentication.
  681. *
  682. * With this type of authentication, the RPCAP protocol will use the username/
  683. * password provided to authenticate the user on the remote machine. If the
  684. * authentication is successful (and the user has the right to open network
  685. * devices) the RPCAP connection will continue; otherwise it will be dropped.
  686. *
  687. * *******NOTE********: the username and password are sent over the network
  688. * to the capture server *IN CLEAR TEXT*. Don't use this on a network
  689. * that you don't completely control! (And be *really* careful in your
  690. * definition of "completely"!)
  691. */
  692. #define RPCAP_RMTAUTH_PWD 1
  693. /*
  694. * This structure keeps the information needed to autheticate the user
  695. * on a remote machine.
  696. *
  697. * The remote machine can either grant or refuse the access according
  698. * to the information provided.
  699. * In case the NULL authentication is required, both 'username' and
  700. * 'password' can be NULL pointers.
  701. *
  702. * This structure is meaningless if the source is not a remote interface;
  703. * in that case, the functions which requires such a structure can accept
  704. * a NULL pointer as well.
  705. */
  706. struct pcap_rmtauth
  707. {
  708. /*
  709. * \brief Type of the authentication required.
  710. *
  711. * In order to provide maximum flexibility, we can support different types
  712. * of authentication based on the value of this 'type' variable. The currently
  713. * supported authentication methods are defined into the
  714. * \link remote_auth_methods Remote Authentication Methods Section\endlink.
  715. */
  716. int type;
  717. /*
  718. * \brief Zero-terminated string containing the username that has to be
  719. * used on the remote machine for authentication.
  720. *
  721. * This field is meaningless in case of the RPCAP_RMTAUTH_NULL authentication
  722. * and it can be NULL.
  723. */
  724. char *username;
  725. /*
  726. * \brief Zero-terminated string containing the password that has to be
  727. * used on the remote machine for authentication.
  728. *
  729. * This field is meaningless in case of the RPCAP_RMTAUTH_NULL authentication
  730. * and it can be NULL.
  731. */
  732. char *password;
  733. };
  734. /*
  735. * This routine can open a savefile, a local device, or a device on
  736. * a remote machine running an RPCAP server.
  737. *
  738. * For opening a savefile, the pcap_open_offline routines can be used,
  739. * and will work just as well; code using them will work on more
  740. * platforms than code using pcap_open() to open savefiles.
  741. *
  742. * For opening a local device, pcap_open_live() can be used; it supports
  743. * most of the capabilities that pcap_open() supports, and code using it
  744. * will work on more platforms than code using pcap_open(). pcap_create()
  745. * and pcap_activate() can also be used; they support all capabilities
  746. * that pcap_open() supports, except for the Windows-only
  747. * PCAP_OPENFLAG_NOCAPTURE_LOCAL, and support additional capabilities.
  748. *
  749. * For opening a remote capture, pcap_open() is currently the only
  750. * API available.
  751. */
  752. PCAP_API pcap_t *pcap_open(const char *source, int snaplen, int flags,
  753. int read_timeout, struct pcap_rmtauth *auth, char *errbuf);
  754. PCAP_API int pcap_createsrcstr(char *source, int type, const char *host,
  755. const char *port, const char *name, char *errbuf);
  756. PCAP_API int pcap_parsesrcstr(const char *source, int *type, char *host,
  757. char *port, char *name, char *errbuf);
  758. /*
  759. * This routine can scan a directory for savefiles, list local capture
  760. * devices, or list capture devices on a remote machine running an RPCAP
  761. * server.
  762. *
  763. * For scanning for savefiles, it can be used on both UN*X systems and
  764. * Windows systems; for each directory entry it sees, it tries to open
  765. * the file as a savefile using pcap_open_offline(), and only includes
  766. * it in the list of files if the open succeeds, so it filters out
  767. * files for which the user doesn't have read permission, as well as
  768. * files that aren't valid savefiles readable by libpcap.
  769. *
  770. * For listing local capture devices, it's just a wrapper around
  771. * pcap_findalldevs(); code using pcap_findalldevs() will work on more
  772. * platforms than code using pcap_findalldevs_ex().
  773. *
  774. * For listing remote capture devices, pcap_findalldevs_ex() is currently
  775. * the only API available.
  776. */
  777. PCAP_API int pcap_findalldevs_ex(char *source, struct pcap_rmtauth *auth,
  778. pcap_if_t **alldevs, char *errbuf);
  779. /*
  780. * Sampling methods.
  781. *
  782. * These allow pcap_loop(), pcap_dispatch(), pcap_next(), and pcap_next_ex()
  783. * to see only a sample of packets, rather than all packets.
  784. *
  785. * Currently, they work only on Windows local captures.
  786. */
  787. /*
  788. * Specifies that no sampling is to be done on the current capture.
  789. *
  790. * In this case, no sampling algorithms are applied to the current capture.
  791. */
  792. #define PCAP_SAMP_NOSAMP 0
  793. /*
  794. * Specifies that only 1 out of N packets must be returned to the user.
  795. *
  796. * In this case, the 'value' field of the 'pcap_samp' structure indicates the
  797. * number of packets (minus 1) that must be discarded before one packet got
  798. * accepted.
  799. * In other words, if 'value = 10', the first packet is returned to the
  800. * caller, while the following 9 are discarded.
  801. */
  802. #define PCAP_SAMP_1_EVERY_N 1
  803. /*
  804. * Specifies that we have to return 1 packet every N milliseconds.
  805. *
  806. * In this case, the 'value' field of the 'pcap_samp' structure indicates
  807. * the 'waiting time' in milliseconds before one packet got accepted.
  808. * In other words, if 'value = 10', the first packet is returned to the
  809. * caller; the next returned one will be the first packet that arrives
  810. * when 10ms have elapsed.
  811. */
  812. #define PCAP_SAMP_FIRST_AFTER_N_MS 2
  813. /*
  814. * This structure defines the information related to sampling.
  815. *
  816. * In case the sampling is requested, the capturing device should read
  817. * only a subset of the packets coming from the source. The returned packets
  818. * depend on the sampling parameters.
  819. *
  820. * WARNING: The sampling process is applied *after* the filtering process.
  821. * In other words, packets are filtered first, then the sampling process
  822. * selects a subset of the 'filtered' packets and it returns them to the
  823. * caller.
  824. */
  825. struct pcap_samp
  826. {
  827. /*
  828. * Method used for sampling; see above.
  829. */
  830. int method;
  831. /*
  832. * This value depends on the sampling method defined.
  833. * For its meaning, see above.
  834. */
  835. int value;
  836. };
  837. /*
  838. * New functions.
  839. */
  840. PCAP_API struct pcap_samp *pcap_setsampling(pcap_t *p);
  841. /*
  842. * RPCAP active mode.
  843. */
  844. /* Maximum length of an host name (needed for the RPCAP active mode) */
  845. #define RPCAP_HOSTLIST_SIZE 1024
  846. /*
  847. * Some minor differences between UN*X sockets and and Winsock sockets.
  848. */
  849. #ifndef _WIN32
  850. /*!
  851. * \brief In Winsock, a socket handle is of type SOCKET; in UN*X, it's
  852. * a file descriptor, and therefore a signed integer.
  853. * We define SOCKET to be a signed integer on UN*X, so that it can
  854. * be used on both platforms.
  855. */
  856. #define SOCKET int
  857. /*!
  858. * \brief In Winsock, the error return if socket() fails is INVALID_SOCKET;
  859. * in UN*X, it's -1.
  860. * We define INVALID_SOCKET to be -1 on UN*X, so that it can be used on
  861. * both platforms.
  862. */
  863. #define INVALID_SOCKET -1
  864. #endif
  865. PCAP_API SOCKET pcap_remoteact_accept(const char *address, const char *port,
  866. const char *hostlist, char *connectinghost,
  867. struct pcap_rmtauth *auth, char *errbuf);
  868. PCAP_API int pcap_remoteact_list(char *hostlist, char sep, int size,
  869. char *errbuf);
  870. PCAP_API int pcap_remoteact_close(const char *host, char *errbuf);
  871. PCAP_API void pcap_remoteact_cleanup(void);
  872. #ifdef __cplusplus
  873. }
  874. #endif
  875. #endif /* lib_pcap_pcap_h */