pcap.3pcap.in 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. .\" Copyright (c) 1994, 1996, 1997
  2. .\" The Regents of the University of California. All rights reserved.
  3. .\"
  4. .\" Redistribution and use in source and binary forms, with or without
  5. .\" modification, are permitted provided that: (1) source code distributions
  6. .\" retain the above copyright notice and this paragraph in its entirety, (2)
  7. .\" distributions including binary code include the above copyright notice and
  8. .\" this paragraph in its entirety in the documentation or other materials
  9. .\" provided with the distribution, and (3) all advertising materials mentioning
  10. .\" features or use of this software display the following acknowledgement:
  11. .\" ``This product includes software developed by the University of California,
  12. .\" Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  13. .\" the University nor the names of its contributors may be used to endorse
  14. .\" or promote products derived from this software without specific prior
  15. .\" written permission.
  16. .\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  17. .\" WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  18. .\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  19. .\"
  20. .TH PCAP 3PCAP "20 April 2018"
  21. .SH NAME
  22. pcap \- Packet Capture library
  23. .SH SYNOPSIS
  24. .nf
  25. .ft B
  26. #include <pcap/pcap.h>
  27. .LP
  28. .ft B
  29. .ft
  30. .fi
  31. .SH DESCRIPTION
  32. The Packet Capture library
  33. provides a high level interface to packet capture systems. All packets
  34. on the network, even those destined for other hosts, are accessible
  35. through this mechanism.
  36. It also supports saving captured packets to a ``savefile'', and reading
  37. packets from a ``savefile''.
  38. .SS Opening a capture handle for reading
  39. To open a handle for a live capture, given the name of the network or
  40. other interface on which the capture should be done, call
  41. .BR pcap_create (),
  42. set the appropriate options on the handle, and then activate it with
  43. .BR pcap_activate ().
  44. .PP
  45. To obtain a list of devices that can be opened for a live capture, call
  46. .BR pcap_findalldevs ();
  47. to free the list returned by
  48. .BR pcap_findalldevs (),
  49. call
  50. .BR pcap_freealldevs ().
  51. .BR pcap_lookupdev ()
  52. will return the first device on that list that is not a ``loopback``
  53. network interface.
  54. .PP
  55. To open a handle for a ``savefile'' from which to read packets, given the
  56. pathname of the ``savefile'', call
  57. .BR pcap_open_offline ();
  58. to set up a handle for a ``savefile'', given a
  59. .B "FILE\ *"
  60. referring to a file already opened for reading, call
  61. .BR pcap_fopen_offline ().
  62. .PP
  63. In order to get a ``fake''
  64. .B pcap_t
  65. for use in routines that require a
  66. .B pcap_t
  67. as an argument, such as routines to open a ``savefile'' for writing and
  68. to compile a filter expression, call
  69. .BR pcap_open_dead ().
  70. .PP
  71. .BR pcap_create (),
  72. .BR pcap_open_offline (),
  73. .BR pcap_fopen_offline (),
  74. and
  75. .BR pcap_open_dead ()
  76. return a pointer to a
  77. .BR pcap_t ,
  78. which is the handle used for reading packets from the capture stream or
  79. the ``savefile'', and for finding out information about the capture
  80. stream or ``savefile''.
  81. To close a handle, use
  82. .BR pcap_close ().
  83. .PP
  84. The options that can be set on a capture handle include
  85. .IP "snapshot length"
  86. If, when capturing, you capture the entire contents of the packet, that
  87. requires more CPU time to copy the packet to your application, more disk
  88. and possibly network bandwidth to write the packet data to a file, and
  89. more disk space to save the packet. If you don't need the entire
  90. contents of the packet - for example, if you are only interested in the
  91. TCP headers of packets - you can set the "snapshot length" for the
  92. capture to an appropriate value. If the snapshot length is set to
  93. .IR snaplen ,
  94. and
  95. .I snaplen
  96. is less
  97. than the size of a packet that is captured, only the first
  98. .I snaplen
  99. bytes of that packet will be captured and provided as packet data.
  100. .IP
  101. A snapshot length of 65535 should be sufficient, on most if not all
  102. networks, to capture all the data available from the packet.
  103. .IP
  104. The snapshot length is set with
  105. .BR pcap_set_snaplen ().
  106. .IP "promiscuous mode"
  107. On broadcast LANs such as Ethernet, if the network isn't switched, or if
  108. the adapter is connected to a "mirror port" on a switch to which all
  109. packets passing through the switch are sent, a network adapter receives
  110. all packets on the LAN, including unicast or multicast packets not sent
  111. to a network address that the network adapter isn't configured to
  112. recognize.
  113. .IP
  114. Normally, the adapter will discard those packets; however, many network
  115. adapters support "promiscuous mode", which is a mode in which all
  116. packets, even if they are not sent to an address that the adapter
  117. recognizes, are provided to the host. This is useful for passively
  118. capturing traffic between two or more other hosts for analysis.
  119. .IP
  120. Note that even if an application does not set promiscuous mode, the
  121. adapter could well be in promiscuous mode for some other reason.
  122. .IP
  123. For now, this doesn't work on the "any" device; if an argument of "any"
  124. or NULL is supplied, the setting of promiscuous mode is ignored.
  125. .IP
  126. Promiscuous mode is set with
  127. .BR pcap_set_promisc ().
  128. .IP "monitor mode"
  129. On IEEE 802.11 wireless LANs, even if an adapter is in promiscuous mode,
  130. it will supply to the host only frames for the network with which it's
  131. associated. It might also supply only data frames, not management or
  132. control frames, and might not provide the 802.11 header or radio
  133. information pseudo-header for those frames.
  134. .IP
  135. In "monitor mode", sometimes also called "rfmon mode" (for "Radio
  136. Frequency MONitor"), the adapter will supply all frames that it
  137. receives, with 802.11 headers, and might supply a pseudo-header with
  138. radio information about the frame as well.
  139. .IP
  140. Note that in monitor mode the adapter might disassociate from the
  141. network with which it's associated, so that you will not be able to use
  142. any wireless networks with that adapter. This could prevent accessing
  143. files on a network server, or resolving host names or network addresses,
  144. if you are capturing in monitor mode and are not connected to another
  145. network with another adapter.
  146. .IP
  147. Monitor mode is set with
  148. .BR pcap_set_rfmon (),
  149. and
  150. .BR pcap_can_set_rfmon ()
  151. can be used to determine whether an adapter can be put into monitor
  152. mode.
  153. .IP "packet buffer timeout"
  154. If, when capturing, packets are delivered as soon as they arrive, the
  155. application capturing the packets will be woken up for each packet as it
  156. arrives, and might have to make one or more calls to the operating
  157. system to fetch each packet.
  158. .IP
  159. If, instead, packets are not delivered as soon as they arrive, but are
  160. delivered after a short delay (called a "packet buffer timeout"), more
  161. than one packet can be accumulated before the packets are delivered, so
  162. that a single wakeup would be done for multiple packets, and each set of
  163. calls made to the operating system would supply multiple packets, rather
  164. than a single packet. This reduces the per-packet CPU overhead if
  165. packets are arriving at a high rate, increasing the number of packets
  166. per second that can be captured.
  167. .IP
  168. The packet buffer timeout is required so that an application won't wait
  169. for the operating system's capture buffer to fill up before packets are
  170. delivered; if packets are arriving slowly, that wait could take an
  171. arbitrarily long period of time.
  172. .IP
  173. Not all platforms support a packet buffer timeout; on platforms that
  174. don't, the packet buffer timeout is ignored. A zero value for the
  175. timeout, on platforms that support a packet buffer timeout, will cause a
  176. read to wait forever to allow enough packets to arrive, with no timeout.
  177. A negative value is invalid; the result of setting the timeout to a
  178. negative value is unpredictable.
  179. .IP
  180. .BR NOTE :
  181. the packet buffer timeout cannot be used to cause calls that read
  182. packets to return within a limited period of time, because, on some
  183. platforms, the packet buffer timeout isn't supported, and, on other
  184. platforms, the timer doesn't start until at least one packet arrives.
  185. This means that the packet buffer timeout should
  186. .B NOT
  187. be used, for example, in an interactive application to allow the packet
  188. capture loop to ``poll'' for user input periodically, as there's no
  189. guarantee that a call reading packets will return after the timeout
  190. expires even if no packets have arrived.
  191. .IP
  192. The packet buffer timeout is set with
  193. .BR pcap_set_timeout ().
  194. .IP "buffer size"
  195. Packets that arrive for a capture are stored in a buffer, so that they
  196. do not have to be read by the application as soon as they arrive. On
  197. some platforms, the buffer's size can be set; a size that's too small
  198. could mean that, if too many packets are being captured and the snapshot
  199. length doesn't limit the amount of data that's buffered, packets could
  200. be dropped if the buffer fills up before the application can read
  201. packets from it, while a size that's too large could use more
  202. non-pageable operating system memory than is necessary to prevent
  203. packets from being dropped.
  204. .IP
  205. The buffer size is set with
  206. .BR pcap_set_buffer_size ().
  207. .IP "timestamp type"
  208. On some platforms, the time stamp given to packets on live captures can
  209. come from different sources that can have different resolutions or that
  210. can have different relationships to the time values for the current time
  211. supplied by routines on the native operating system. See
  212. .BR pcap-tstamp (@MAN_MISC_INFO@)
  213. for a list of time stamp types.
  214. .IP
  215. The time stamp type is set with
  216. .BR pcap_set_tstamp_type ().
  217. .PP
  218. Reading packets from a network interface may require that you have
  219. special privileges:
  220. .TP
  221. .B Under SunOS 3.x or 4.x with NIT or BPF:
  222. You must have read access to
  223. .I /dev/nit
  224. or
  225. .IR /dev/bpf* .
  226. .TP
  227. .B Under Solaris with DLPI:
  228. You must have read/write access to the network pseudo device, e.g.
  229. .IR /dev/le .
  230. On at least some versions of Solaris, however, this is not sufficient to
  231. allow
  232. .I tcpdump
  233. to capture in promiscuous mode; on those versions of Solaris, you must
  234. be root, or the application capturing packets
  235. must be installed setuid to root, in order to capture in promiscuous
  236. mode. Note that, on many (perhaps all) interfaces, if you don't capture
  237. in promiscuous mode, you will not see any outgoing packets, so a capture
  238. not done in promiscuous mode may not be very useful.
  239. .IP
  240. In newer versions of Solaris, you must have been given the
  241. .B net_rawaccess
  242. privilege; this is both necessary and sufficient to give you access to the
  243. network pseudo-device - there is no need to change the privileges on
  244. that device. A user can be given that privilege by, for example, adding
  245. that privilege to the user's
  246. .B defaultpriv
  247. key with the
  248. .B usermod (@MAN_ADMIN_COMMANDS@)
  249. command.
  250. .TP
  251. .B Under HP-UX with DLPI:
  252. You must be root or the application capturing packets must be installed
  253. setuid to root.
  254. .TP
  255. .B Under IRIX with snoop:
  256. You must be root or the application capturing packets must be installed
  257. setuid to root.
  258. .TP
  259. .B Under Linux:
  260. You must be root or the application capturing packets must be installed
  261. setuid to root (unless your distribution has a kernel
  262. that supports capability bits such as CAP_NET_RAW and code to allow
  263. those capability bits to be given to particular accounts and to cause
  264. those bits to be set on a user's initial processes when they log in, in
  265. which case you must have CAP_NET_RAW in order to capture and
  266. CAP_NET_ADMIN to enumerate network devices with, for example, the
  267. .B \-D
  268. flag).
  269. .TP
  270. .B Under ULTRIX and Digital UNIX/Tru64 UNIX:
  271. Any user may capture network traffic.
  272. However, no user (not even the super-user) can capture in promiscuous
  273. mode on an interface unless the super-user has enabled promiscuous-mode
  274. operation on that interface using
  275. .IR pfconfig (8),
  276. and no user (not even the super-user) can capture unicast traffic
  277. received by or sent by the machine on an interface unless the super-user
  278. has enabled copy-all-mode operation on that interface using
  279. .IR pfconfig ,
  280. so
  281. .I useful
  282. packet capture on an interface probably requires that either
  283. promiscuous-mode or copy-all-mode operation, or both modes of
  284. operation, be enabled on that interface.
  285. .TP
  286. .B Under BSD (this includes macOS):
  287. You must have read access to
  288. .I /dev/bpf*
  289. on systems that don't have a cloning BPF device, or to
  290. .I /dev/bpf
  291. on systems that do.
  292. On BSDs with a devfs (this includes macOS), this might involve more
  293. than just having somebody with super-user access setting the ownership
  294. or permissions on the BPF devices - it might involve configuring devfs
  295. to set the ownership or permissions every time the system is booted,
  296. if the system even supports that; if it doesn't support that, you might
  297. have to find some other way to make that happen at boot time.
  298. .PP
  299. Reading a saved packet file doesn't require special privileges.
  300. .PP
  301. The packets read from the handle may include a ``pseudo-header''
  302. containing various forms of packet meta-data, and probably includes a
  303. link-layer header whose contents can differ for different network
  304. interfaces. To determine the format of the packets supplied by the
  305. handle, call
  306. .BR pcap_datalink ();
  307. .I https://www.tcpdump.org/linktypes.html
  308. lists the values it returns and describes the packet formats that
  309. correspond to those values.
  310. .PP
  311. Do
  312. .B NOT
  313. assume that the packets for a given capture or ``savefile`` will have
  314. any given link-layer header type, such as
  315. .B DLT_EN10MB
  316. for Ethernet. For example, the "any" device on Linux will have a
  317. link-layer header type of
  318. .B DLT_LINUX_SLL
  319. even if all devices on the system at the time the "any" device is opened
  320. have some other data link type, such as
  321. .B DLT_EN10MB
  322. for Ethernet.
  323. .PP
  324. To obtain the
  325. .B "FILE\ *"
  326. corresponding to a
  327. .B pcap_t
  328. opened for a ``savefile'', call
  329. .BR pcap_file ().
  330. .TP
  331. .B Routines
  332. .RS
  333. .TP
  334. .BR pcap_create (3PCAP)
  335. get a
  336. .B pcap_t
  337. for live capture
  338. .TP
  339. .BR pcap_activate (3PCAP)
  340. activate a
  341. .B pcap_t
  342. for live capture
  343. .TP
  344. .BR pcap_findalldevs (3PCAP)
  345. get a list of devices that can be opened for a live capture
  346. .TP
  347. .BR pcap_freealldevs (3PCAP)
  348. free list of devices
  349. .TP
  350. .BR pcap_lookupdev (3PCAP)
  351. get first non-loopback device on that list
  352. .TP
  353. .BR pcap_open_offline (3PCAP)
  354. open a
  355. .B pcap_t
  356. for a ``savefile'', given a pathname
  357. .TP
  358. .BR pcap_open_offline_with_tstamp_precision (3PCAP)
  359. open a
  360. .B pcap_t
  361. for a ``savefile'', given a pathname, and specify the precision to
  362. provide for packet time stamps
  363. .TP
  364. .BR pcap_fopen_offline (3PCAP)
  365. open a
  366. .B pcap_t
  367. for a ``savefile'', given a
  368. .B "FILE\ *"
  369. .TP
  370. .BR pcap_fopen_offline_with_tstamp_precision (3PCAP)
  371. open a
  372. .B pcap_t
  373. for a ``savefile'', given a
  374. .BR "FILE\ *" ,
  375. and specify the precision to provide for packet time stamps
  376. .TP
  377. .BR pcap_open_dead (3PCAP)
  378. create a ``fake''
  379. .B pcap_t
  380. .TP
  381. .BR pcap_close (3PCAP)
  382. close a
  383. .B pcap_t
  384. .TP
  385. .BR pcap_set_snaplen (3PCAP)
  386. set the snapshot length for a not-yet-activated
  387. .B pcap_t
  388. for live capture
  389. .TP
  390. .BR pcap_snapshot (3PCAP)
  391. get the snapshot length for a
  392. .B pcap_t
  393. .TP
  394. .BR pcap_set_promisc (3PCAP)
  395. set promiscuous mode for a not-yet-activated
  396. .B pcap_t
  397. for live capture
  398. .TP
  399. .BR pcap_set_protocol_linux (3PCAP)
  400. set capture protocol for a not-yet-activated
  401. .B pcap_t
  402. for live capture (Linux only)
  403. .TP
  404. .BR pcap_set_rfmon (3PCAP)
  405. set monitor mode for a not-yet-activated
  406. .B pcap_t
  407. for live capture
  408. .TP
  409. .BR pcap_can_set_rfmon (3PCAP)
  410. determine whether monitor mode can be set for a
  411. .B pcap_t
  412. for live capture
  413. .TP
  414. .BR pcap_set_timeout (3PCAP)
  415. set packet buffer timeout for a not-yet-activated
  416. .B pcap_t
  417. for live capture
  418. .TP
  419. .BR pcap_set_buffer_size (3PCAP)
  420. set buffer size for a not-yet-activated
  421. .B pcap_t
  422. for live capture
  423. .TP
  424. .BR pcap_set_tstamp_type (3PCAP)
  425. set time stamp type for a not-yet-activated
  426. .B pcap_t
  427. for live capture
  428. .TP
  429. .BR pcap_list_tstamp_types (3PCAP)
  430. get list of available time stamp types for a not-yet-activated
  431. .B pcap_t
  432. for live capture
  433. .TP
  434. .BR pcap_free_tstamp_types (3PCAP)
  435. free list of available time stamp types
  436. .TP
  437. .BR pcap_tstamp_type_val_to_name (3PCAP)
  438. get name for a time stamp type
  439. .TP
  440. .BR pcap_tstamp_type_val_to_description (3PCAP)
  441. get description for a time stamp type
  442. .TP
  443. .BR pcap_tstamp_type_name_to_val (3PCAP)
  444. get time stamp type corresponding to a name
  445. .TP
  446. .BR pcap_set_tstamp_precision (3PCAP)
  447. set time stamp precision for a not-yet-activated
  448. .B pcap_t
  449. for live capture
  450. .TP
  451. .BR pcap_get_tstamp_precision (3PCAP)
  452. get the time stamp precision of a
  453. .B pcap_t
  454. for live capture
  455. .TP
  456. .BR pcap_datalink (3PCAP)
  457. get link-layer header type for a
  458. .B pcap_t
  459. .TP
  460. .BR pcap_file (3PCAP)
  461. get the
  462. .B "FILE\ *"
  463. for a
  464. .B pcap_t
  465. opened for a ``savefile''
  466. .TP
  467. .BR pcap_is_swapped (3PCAP)
  468. determine whether a ``savefile'' being read came from a machine with the
  469. opposite byte order
  470. .TP
  471. .BR pcap_major_version (3PCAP)
  472. .PD 0
  473. .TP
  474. .BR pcap_minor_version (3PCAP)
  475. get the major and minor version of the file format version for a
  476. ``savefile''
  477. .PD
  478. .RE
  479. .SS Selecting a link-layer header type for a live capture
  480. Some devices may provide more than one link-layer header type. To
  481. obtain a list of all link-layer header types provided by a device, call
  482. .BR pcap_list_datalinks ()
  483. on an activated
  484. .B pcap_t
  485. for the device.
  486. To free a list of link-layer header types, call
  487. .BR pcap_free_datalinks ().
  488. To set the link-layer header type for a device, call
  489. .BR pcap_set_datalink ().
  490. This should be done after the device has been activated but before any
  491. packets are read and before any filters are compiled or installed.
  492. .TP
  493. .B Routines
  494. .RS
  495. .TP
  496. .BR pcap_list_datalinks (3PCAP)
  497. get a list of link-layer header types for a device
  498. .TP
  499. .BR pcap_free_datalinks (3PCAP)
  500. free list of link-layer header types
  501. .TP
  502. .BR pcap_set_datalink (3PCAP)
  503. set link-layer header type for a device
  504. .TP
  505. .BR pcap_datalink_val_to_name (3PCAP)
  506. get name for a link-layer header type
  507. .TP
  508. .BR pcap_datalink_val_to_description (3PCAP)
  509. get description for a link-layer header type
  510. .TP
  511. .BR pcap_datalink_name_to_val (3PCAP)
  512. get link-layer header type corresponding to a name
  513. .RE
  514. .SS Reading packets
  515. Packets are read with
  516. .BR pcap_dispatch ()
  517. or
  518. .BR pcap_loop (),
  519. which process one or more packets, calling a callback routine for each
  520. packet, or with
  521. .BR pcap_next ()
  522. or
  523. .BR pcap_next_ex (),
  524. which return the next packet.
  525. The callback for
  526. .BR pcap_dispatch ()
  527. and
  528. .BR pcap_loop ()
  529. is supplied a pointer to a
  530. .IR "struct pcap_pkthdr" ,
  531. which includes the following members:
  532. .RS
  533. .TP
  534. .B ts
  535. a
  536. .I struct timeval
  537. containing the time when the packet was captured
  538. .TP
  539. .B caplen
  540. a
  541. .I bpf_u_int32
  542. giving the number of bytes of the packet that are available from the
  543. capture
  544. .TP
  545. .B len
  546. a
  547. .I bpf_u_int32
  548. giving the length of the packet, in bytes (which might be more than the
  549. number of bytes available from the capture, if the length of the packet
  550. is larger than the maximum number of bytes to capture).
  551. .RE
  552. .PP
  553. The callback is also supplied a
  554. .I const u_char
  555. pointer to the first
  556. .B caplen
  557. (as given in the
  558. .I struct pcap_pkthdr
  559. mentioned above)
  560. bytes of data from the packet. This won't necessarily be the entire
  561. packet; to capture the entire packet, you will have to provide a value
  562. for
  563. .I snaplen
  564. in your call to
  565. .BR pcap_set_snaplen ()
  566. that is sufficiently large to get all of the packet's data - a value of
  567. 65535 should be sufficient on most if not all networks). When reading
  568. from a ``savefile'', the snapshot length specified when the capture was
  569. performed will limit the amount of packet data available.
  570. .PP
  571. .BR pcap_next ()
  572. is passed an argument that points to a
  573. .I struct pcap_pkthdr
  574. structure, and fills it in with the time stamp and length values for the
  575. packet. It returns a
  576. .I const u_char
  577. to the first
  578. .B caplen
  579. bytes of the packet on success, and NULL on error.
  580. .PP
  581. .BR pcap_next_ex ()
  582. is passed two pointer arguments, one of which points to a
  583. .IR struct pcap_pkthdr *
  584. and one of which points to a
  585. .IR "const u_char" *.
  586. It sets the first pointer to point to a
  587. .I struct pcap_pkthdr
  588. structure with the time stamp and length values for the packet, and sets
  589. the second pointer to point to the first
  590. .B caplen
  591. bytes of the packet.
  592. .PP
  593. To force the loop in
  594. .BR pcap_dispatch ()
  595. or
  596. .BR pcap_loop ()
  597. to terminate, call
  598. .BR pcap_breakloop ().
  599. .PP
  600. By default, when reading packets from an interface opened for a live
  601. capture,
  602. .BR pcap_dispatch (),
  603. .BR pcap_next (),
  604. and
  605. .BR pcap_next_ex ()
  606. will, if no packets are currently available to be read, block waiting
  607. for packets to become available. On some, but
  608. .I not
  609. all, platforms, if a packet buffer timeout was specified, the wait will
  610. terminate after the packet buffer timeout expires; applications should
  611. be prepared for this, as it happens on some platforms, but should not
  612. rely on it, as it does not happen on other platforms. Note that the
  613. wait might, or might not, terminate even if no packets are available;
  614. applications should be prepared for this to happen, but must not rely on
  615. it happening.
  616. .PP
  617. A handle can be put into ``non-blocking mode'', so that those routines
  618. will, rather than blocking, return an indication that no packets are
  619. available to read. Call
  620. .BR pcap_setnonblock ()
  621. to put a handle into non-blocking mode or to take it out of non-blocking
  622. mode; call
  623. .BR pcap_getnonblock ()
  624. to determine whether a handle is in non-blocking mode. Note that
  625. non-blocking mode does not work correctly in Mac OS X 10.6.
  626. .PP
  627. Non-blocking mode is often combined with routines such as
  628. .BR select (2)
  629. or
  630. .BR poll (2)
  631. or other routines a platform offers to wait for any of a set of
  632. descriptors to be ready to read. To obtain, for a handle, a descriptor
  633. that can be used in those routines, call
  634. .BR pcap_get_selectable_fd ().
  635. Not all handles have such a descriptor available;
  636. .BR pcap_get_selectable_fd ()
  637. will return \-1 if no such descriptor exists. In addition, for various
  638. reasons, one or more of those routines will not work properly with the
  639. descriptor; the documentation for
  640. .BR pcap_get_selectable_fd ()
  641. gives details. Note that, just as an attempt to read packets from a
  642. .B pcap_t
  643. may not return any packets if the packet buffer timeout expires, a
  644. .BR select (),
  645. .BR poll (),
  646. or other such call may, if the packet buffer timeout expires, indicate
  647. that a descriptor is ready to read even if there are no packets
  648. available to read.
  649. .TP
  650. .B Routines
  651. .RS
  652. .TP
  653. .BR pcap_dispatch (3PCAP)
  654. read a bufferful of packets from a
  655. .B pcap_t
  656. open for a live capture or the full set of packets from a
  657. .B pcap_t
  658. open for a ``savefile''
  659. .TP
  660. .BR pcap_loop (3PCAP)
  661. read packets from a
  662. .B pcap_t
  663. until an interrupt or error occurs
  664. .TP
  665. .BR pcap_next (3PCAP)
  666. read the next packet from a
  667. .B pcap_t
  668. without an indication whether an error occurred
  669. .TP
  670. .BR pcap_next_ex (3PCAP)
  671. read the next packet from a
  672. .B pcap_t
  673. with an error indication on an error
  674. .TP
  675. .BR pcap_breakloop (3PCAP)
  676. prematurely terminate the loop in
  677. .BR pcap_dispatch ()
  678. or
  679. .BR pcap_loop ()
  680. .TP
  681. .BR pcap_setnonblock (3PCAP)
  682. set or clear non-blocking mode on a
  683. .B pcap_t
  684. .TP
  685. .BR pcap_getnonblock (3PCAP)
  686. get the state of non-blocking mode for a
  687. .B pcap_t
  688. .TP
  689. .BR pcap_get_selectable_fd (3PCAP)
  690. attempt to get a descriptor for a
  691. .B pcap_t
  692. that can be used in calls such as
  693. .BR select (2)
  694. and
  695. .BR poll (2)
  696. .RE
  697. .SS Filters
  698. In order to cause only certain packets to be returned when reading
  699. packets, a filter can be set on a handle. For a live capture, the
  700. filtering will be performed in kernel mode, if possible, to avoid
  701. copying ``uninteresting'' packets from the kernel to user mode.
  702. .PP
  703. A filter can be specified as a text string; the syntax and semantics of
  704. the string are as described by
  705. .BR pcap-filter (@MAN_MISC_INFO@).
  706. A filter string is compiled into a program in a pseudo-machine-language
  707. by
  708. .BR pcap_compile ()
  709. and the resulting program can be made a filter for a handle with
  710. .BR pcap_setfilter ().
  711. The result of
  712. .BR pcap_compile ()
  713. can be freed with a call to
  714. .BR pcap_freecode ().
  715. .BR pcap_compile ()
  716. may require a network mask for certain expressions in the filter string;
  717. .BR pcap_lookupnet ()
  718. can be used to find the network address and network mask for a given
  719. capture device.
  720. .PP
  721. A compiled filter can also be applied directly to a packet that has been
  722. read using
  723. .BR pcap_offline_filter ().
  724. .TP
  725. .B Routines
  726. .RS
  727. .TP
  728. .BR pcap_compile (3PCAP)
  729. compile filter expression to a pseudo-machine-language code program
  730. .TP
  731. .BR pcap_freecode (3PCAP)
  732. free a filter program
  733. .TP
  734. .BR pcap_setfilter (3PCAP)
  735. set filter for a
  736. .B pcap_t
  737. .TP
  738. .BR pcap_lookupnet (3PCAP)
  739. get network address and network mask for a capture device
  740. .TP
  741. .BR pcap_offline_filter (3PCAP)
  742. apply a filter program to a packet
  743. .RE
  744. .SS Incoming and outgoing packets
  745. By default, libpcap will attempt to capture both packets sent by the
  746. machine and packets received by the machine. To limit it to capturing
  747. only packets received by the machine or, if possible, only packets sent
  748. by the machine, call
  749. .BR pcap_setdirection ().
  750. .TP
  751. .BR Routines
  752. .RS
  753. .TP
  754. .BR pcap_setdirection (3PCAP)
  755. specify whether to capture incoming packets, outgoing packets, or both
  756. .RE
  757. .SS Capture statistics
  758. To get statistics about packets received and dropped in a live capture,
  759. call
  760. .BR pcap_stats ().
  761. .TP
  762. .B Routines
  763. .RS
  764. .TP
  765. .BR pcap_stats (3PCAP)
  766. get capture statistics
  767. .RE
  768. .SS Opening a handle for writing captured packets
  769. To open a ``savefile`` to which to write packets, given the pathname the
  770. ``savefile'' should have, call
  771. .BR pcap_dump_open ().
  772. To open a ``savefile`` to which to write packets, given the pathname the
  773. ``savefile'' should have, call
  774. .BR pcap_dump_open ();
  775. to set up a handle for a ``savefile'', given a
  776. .B "FILE\ *"
  777. referring to a file already opened for writing, call
  778. .BR pcap_dump_fopen ().
  779. They each return pointers to a
  780. .BR pcap_dumper_t ,
  781. which is the handle used for writing packets to the ``savefile''. If it
  782. succeeds, it will have created the file if it doesn't exist and
  783. truncated the file if it does exist.
  784. To close a
  785. .BR pcap_dumper_t ,
  786. call
  787. .BR pcap_dump_close ().
  788. .TP
  789. .B Routines
  790. .RS
  791. .TP
  792. .BR pcap_dump_open (3PCAP)
  793. open a
  794. .B pcap_dumper_t
  795. for a ``savefile``, given a pathname
  796. .TP
  797. .BR pcap_dump_fopen (3PCAP)
  798. open a
  799. .B pcap_dumper_t
  800. for a ``savefile``, given a
  801. .B "FILE\ *"
  802. .TP
  803. .BR pcap_dump_close (3PCAP)
  804. close a
  805. .B pcap_dumper_t
  806. .TP
  807. .BR pcap_dump_file (3PCAP)
  808. get the
  809. .B "FILE\ *"
  810. for a
  811. .B pcap_dumper_t
  812. opened for a ``savefile''
  813. .RE
  814. .SS Writing packets
  815. To write a packet to a
  816. .BR pcap_dumper_t ,
  817. call
  818. .BR pcap_dump ().
  819. Packets written with
  820. .BR pcap_dump ()
  821. may be buffered, rather than being immediately written to the
  822. ``savefile''. Closing the
  823. .B pcap_dumper_t
  824. will cause all buffered-but-not-yet-written packets to be written to the
  825. ``savefile''.
  826. To force all packets written to the
  827. .BR pcap_dumper_t ,
  828. and not yet written to the ``savefile'' because they're buffered by the
  829. .BR pcap_dumper_t ,
  830. to be written to the ``savefile'', without closing the
  831. .BR pcap_dumper_t ,
  832. call
  833. .BR pcap_dump_flush ().
  834. .TP
  835. .B Routines
  836. .RS
  837. .TP
  838. .BR pcap_dump (3PCAP)
  839. write packet to a
  840. .B pcap_dumper_t
  841. .TP
  842. .BR pcap_dump_flush (3PCAP)
  843. flush buffered packets written to a
  844. .B pcap_dumper_t
  845. to the ``savefile''
  846. .TP
  847. .BR pcap_dump_ftell (3PCAP)
  848. get current file position for a
  849. .B pcap_dumper_t
  850. .RE
  851. .SS Injecting packets
  852. If you have the required privileges, you can inject packets onto a
  853. network with a
  854. .B pcap_t
  855. for a live capture, using
  856. .BR pcap_inject ()
  857. or
  858. .BR pcap_sendpacket ().
  859. (The two routines exist for compatibility with both OpenBSD and WinPcap;
  860. they perform the same function, but have different return values.)
  861. .TP
  862. .B Routines
  863. .RS
  864. .TP
  865. .BR pcap_inject (3PCAP)
  866. .PD 0
  867. .TP
  868. .BR pcap_sendpacket (3PCAP)
  869. transmit a packet
  870. .PD
  871. .RE
  872. .SS Reporting errors
  873. Some routines return error or warning status codes; to convert them to a
  874. string, use
  875. .BR pcap_statustostr ().
  876. .TP
  877. .B Routines
  878. .RS
  879. .TP
  880. .BR pcap_statustostr (3PCAP)
  881. get a string for an error or warning status code
  882. .RE
  883. .SS Getting library version information
  884. To get a string giving version information about libpcap, call
  885. .BR pcap_lib_version ().
  886. .TP
  887. .B Routines
  888. .RS
  889. .TP
  890. .BR pcap_lib_version (3PCAP)
  891. get library version string
  892. .RE
  893. .SH BACKWARDS COMPATIBILITY
  894. .PP
  895. In versions of libpcap prior to 1.0, the
  896. .B pcap.h
  897. header file was not in a
  898. .B pcap
  899. directory on most platforms; if you are writing an application that must
  900. work on versions of libpcap prior to 1.0, include
  901. .BR <pcap.h> ,
  902. which will include
  903. .B <pcap/pcap.h>
  904. for you, rather than including
  905. .BR <pcap/pcap.h> .
  906. .PP
  907. .BR pcap_create ()
  908. and
  909. .BR pcap_activate ()
  910. were not available in versions of libpcap prior to 1.0; if you are
  911. writing an application that must work on versions of libpcap prior to
  912. 1.0, either use
  913. .BR pcap_open_live ()
  914. to get a handle for a live capture or, if you want to be able to use the
  915. additional capabilities offered by using
  916. .BR pcap_create ()
  917. and
  918. .BR pcap_activate (),
  919. use an
  920. .BR autoconf (1)
  921. script or some other configuration script to check whether the libpcap
  922. 1.0 APIs are available and use them only if they are.
  923. .SH SEE ALSO
  924. autoconf(1), tcpdump(1), tcpslice(1), pcap-filter(@MAN_MISC_INFO@), pfconfig(8),
  925. usermod(@MAN_ADMIN_COMMANDS@)
  926. .SH AUTHORS
  927. The original authors of libpcap are:
  928. .LP
  929. Van Jacobson,
  930. Craig Leres and
  931. Steven McCanne, all of the
  932. Lawrence Berkeley National Laboratory, University of California, Berkeley, CA.
  933. .LP
  934. The current version is available from "The Tcpdump Group"'s Web site at
  935. .LP
  936. .RS
  937. .I https://www.tcpdump.org/
  938. .RE
  939. .SH BUGS
  940. To report a security issue please send an e-mail to security@tcpdump.org.
  941. .LP
  942. To report bugs and other problems, contribute patches, request a
  943. feature, provide generic feedback etc please see the file
  944. .I CONTRIBUTING
  945. in the libpcap source tree root.