1 .\" Copyright (c) 1994, 1996, 1997
2 .\" The Regents of the University of California. All rights reserved.
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.
20 .TH PCAP_NEXT_EX 3PCAP "5 March 2022"
22 pcap_next_ex, pcap_next \- read the next packet from a pcap_t
26 #include <pcap/pcap.h>
30 int pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header,
31 const u_char **pkt_data);
32 const u_char *pcap_next(pcap_t *p, struct pcap_pkthdr *h);
37 reads the next packet and returns a success/failure indication.
38 If the packet was read without problems, the pointer pointed to by the
40 argument is set to point to the
42 struct for the packet, and the
43 pointer pointed to by the
45 argument is set to point to the data in the packet. The
47 and the packet data are not to be freed by the caller, and are not
48 guaranteed to be valid after the next call to
51 .BR pcap_loop (3PCAP),
53 .BR pcap_dispatch (3PCAP);
54 if the code needs them to remain valid, it must make a copy of them.
57 reads the next packet (by calling
63 pointer to the data in that packet. The
64 packet data is not to be freed by the caller, and is not
65 guaranteed to be valid after the next call to
71 if the code needs it to remain valid, it must make a copy of it.
74 structure pointed to by
76 is filled in with the appropriate values for the packet.
78 The bytes of data from the packet begin with a link-layer header. The
79 format of the link-layer header is indicated by the return value of the
80 .BR pcap_datalink (3PCAP)
81 routine when handed the
87 .I https://www.tcpdump.org/linktypes.html
90 can return and describes the packet formats that
91 correspond to those values. The value it returns will be valid for all
92 packets received unless and until
93 .BR pcap_set_datalink (3PCAP)
94 is called; after a successful call to
95 .BR pcap_set_datalink (),
96 all subsequent packets will have a link-layer header of the type
97 specified by the link-layer header type value passed to
98 .BR pcap_set_datalink ().
102 assume that the packets for a given capture or ``savefile`` will have
103 any given link-layer header type, such as
105 for Ethernet. For example, the "any" device on Linux will have a
106 link-layer header type of
110 even if all devices on the system at the time the "any" device is opened
111 have some other data link type, such as
118 if the packet was read without problems,
121 being read from a live capture and the packet buffer timeout expired,
124 are being read from a ``savefile'' and there are no more packets to read
126 .B PCAP_ERROR_NOT_ACTIVATED
127 if called on a capture handle that has been created but not activated,
130 if an error occurred while reading the packet. If
133 .BR pcap_geterr (3PCAP)
135 .BR pcap_perror (3PCAP)
138 as an argument to fetch or display the error text.
141 returns a pointer to the packet data on success, and returns
143 if an error occurred, or if no packets were read from a live capture
144 (if, for example, they were discarded because they didn't pass the
145 packet filter, or if, on platforms that support a packet buffer timeout
146 that starts before any packets arrive, the timeout expires before any
147 packets arrive, or if the file descriptor for the capture device is in
148 non-blocking mode and no packets were available to be read), or if no
149 more packets are available in a ``savefile''. Unfortunately, there is no
150 way to determine whether an error occurred or not.