]> The Tcpdump Group git mirrors - libpcap/blob - pcap_next_ex.3pcap
ATM: Simplify protocol and message type handling.
[libpcap] / pcap_next_ex.3pcap
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_NEXT_EX 3PCAP "5 March 2022"
21 .SH NAME
22 pcap_next_ex, pcap_next \- read the next packet from a pcap_t
23 .SH SYNOPSIS
24 .nf
25 .ft B
26 #include <pcap/pcap.h>
27 .ft
28 .LP
29 .ft B
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);
33 .ft
34 .fi
35 .SH DESCRIPTION
36 .BR pcap_next_ex ()
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
39 .I pkt_header
40 argument is set to point to the
41 .B pcap_pkthdr
42 struct for the packet, and the
43 pointer pointed to by the
44 .I pkt_data
45 argument is set to point to the data in the packet. The
46 .B struct pcap_pkthdr
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
49 .BR pcap_next_ex (),
50 .BR pcap_next (),
51 .BR pcap_loop (3PCAP),
52 or
53 .BR pcap_dispatch (3PCAP);
54 if the code needs them to remain valid, it must make a copy of them.
55 .PP
56 .BR pcap_next ()
57 reads the next packet (by calling
58 .BR pcap_dispatch ()
59 with a
60 .I cnt
61 of 1) and returns a
62 .B u_char
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
66 .BR pcap_next_ex (),
67 .BR pcap_next (),
68 .BR pcap_loop (),
69 or
70 .BR pcap_dispatch ();
71 if the code needs it to remain valid, it must make a copy of it.
72 The
73 .B pcap_pkthdr
74 structure pointed to by
75 .I h
76 is filled in with the appropriate values for the packet.
77 .PP
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
82 .B pcap_t
83 value also passed to
84 .BR pcap_loop ()
85 or
86 .BR pcap_dispatch ().
87 .I https://www.tcpdump.org/linktypes.html
88 lists the values
89 .BR pcap_datalink ()
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 ().
99 .PP
100 Do
101 .B NOT
102 assume that the packets for a given capture or ``savefile`` will have
103 any given link-layer header type, such as
104 .B DLT_EN10MB
105 for Ethernet. For example, the "any" device on Linux will have a
106 link-layer header type of
107 .B DLT_LINUX_SLL
108 or
109 .B DLT_LINUX_SLL2
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
112 .B DLT_EN10MB
113 for Ethernet.
114 .SH RETURN VALUE
115 .BR pcap_next_ex ()
116 returns
117 .B 1
118 if the packet was read without problems,
119 .B 0
120 if packets are
121 being read from a live capture and the packet buffer timeout expired,
122 .B PCAP_ERROR_BREAK
123 if packets
124 are being read from a ``savefile'' and there are no more packets to read
125 from the savefile,
126 .B PCAP_ERROR_NOT_ACTIVATED
127 if called on a capture handle that has been created but not activated,
128 or
129 .B PCAP_ERROR
130 if an error occurred while reading the packet. If
131 .B PCAP_ERROR
132 is returned,
133 .BR pcap_geterr (3PCAP)
134 or
135 .BR pcap_perror (3PCAP)
136 may be called with
137 .I p
138 as an argument to fetch or display the error text.
139 .PP
140 .BR pcap_next ()
141 returns a pointer to the packet data on success, and returns
142 .B NULL
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.
151 .SH SEE ALSO
152 .BR pcap (3PCAP)