📄 pcap.3
字号:
.I callbackspecifies a routine to be called with three arguments:a.I u_charpointer which is passed in from.BR pcap_dispatch() ,a.I const struct pcap_pkthdrpointer to a structure with the following members:.RS.TP.B tsa.I struct timevalcontaining the time when the packet was captured.TP.B caplena.I bpf_u_int32giving the number of bytes of the packet that are available from thecapture.TP.B lena.I bpf_u_int32giving the length of the packet, in bytes (which might be more than thenumber of bytes available from the capture, if the length of the packetis larger than the maximum number of bytes to capture).RE.PPand a.I const u_charpointer to the first.B caplen(as given in the.I struct pcap_pkthdra pointer to which is passed to the callback routine)bytes of data from the packet (which won't necessarily be the entirepacket; to capture the entire packet, you will have to provide a valuefor.I snaplenin your call to.B pcap_open_live()that is sufficiently large to get all of the packet's data - a value of65535 should be sufficient on most if not all networks)..PPThe number of packets read is returned.0 is returned if no packets were read from a live capture (if, forexample, they were discarded because they didn't pass the packet filter,or if, on platforms that support a read timeout that starts before anypackets arrive, the timeout expires before any packets arrive, or if thefile descriptor for the capture device is in non-blocking mode and nopackets were available to be read) or if no more packets are availablein a ``savefile.'' A return of \-1 indicatesan error in which case.B pcap_perror()or.B pcap_geterr()may be used to display the error text.A return of \-2 indicates that the loop terminated due to a call to.B pcap_breakloop()before any packets were processed..ft BIf your application uses pcap_breakloop(),make sure that you explicitly check for \-1 and \-2, rather than justchecking for a return value < 0..ft R.PP.BR NOTE :when reading a live capture,.B pcap_dispatch()will not necessarily return when the read times out; on some platforms,the read timeout isn't supported, and, on other platforms, the timerdoesn't start until at least one packet arrives. This means that theread timeout should.B NOTbe used in, for example, an interactive application, to allow the packetcapture loop to ``poll'' for user input periodically, as there's noguarantee that.B pcap_dispatch()will return after the timeout expires..PP.B pcap_loop()is similar to.B pcap_dispatch()except it keeps reading packets until.I cntpackets are processed or an error occurs.It does.B notreturn when live read timeouts occur.Rather, specifying a non-zero read timeout to.B pcap_open_live()and then calling.B pcap_dispatch()allows the reception and processing of any packets that arrive when thetimeout occurs.A negative.I cntcauses.B pcap_loop()to loop forever (or at least until an error occurs). \-1 is returned onan error; 0 is returned if.I cntis exhausted; \-2 is returned if the loop terminated due to a call to.B pcap_breakloop()before any packets were processed..ft BIf your application uses pcap_breakloop(),make sure that you explicitly check for \-1 and \-2, rather than justchecking for a return value < 0..ft R.PP.B pcap_next()reads the next packet (by calling.B pcap_dispatch()with a.I cntof 1) and returns a.I u_charpointer to the data in that packet. (The.I pcap_pkthdrstruct for that packet is not supplied.).B NULLis returned if an error occured, or if no packets were read from a livecapture (if, for example, they were discarded because they didn't passthe packet filter, or if, on platforms that support a read timeout thatstarts before any packets arrive, the timeout expires before any packetsarrive, or if the file descriptor for the capture device is innon-blocking mode and no packets were available to be read), or if nomore packets are available in a ``savefile.'' Unfortunately, there isno way to determine whether an error occured or not..PP.B pcap_next_ex()reads the next packet and returns a success/failure indication:.RS.TP1the packet was read without problems.TP0packets are being read from a live capture, and the timeout expired.TP\-1an error occurred while reading the packet.TP\-2packets are being read from a ``savefile'', and there are no morepackets to read from the savefile..RE.PPIf the packet was read without problems, the pointer pointed to by the.I pkt_headerargument is set to point to the.I pcap_pkthdrstruct for the packet, and thepointer pointed to by the.I pkt_dataargument is set to point to the data in the packet..PP.B pcap_breakloop()sets a flag that will force.B pcap_dispatch()or.B pcap_loop()to return rather than looping; they will return the number of packetsthat have been processed so far, or \-2 if no packets have beenprocessed so far..PPThis routine is safe to use inside a signal handler on UNIX or a consolecontrol handler on Windows, as it merely sets a flag that is checkedwithin the loop..PPThe flag is checked in loops reading packets from the OS - a signal byitself will not necessarily terminate those loops - as well as in loopsprocessing a set of packets returned by the OS..ft BNote that if you are catching signals on UNIX systems that supportrestarting system calls after a signal, and calling pcap_breakloop()in the signal handler, you must specify, when catching those signals,that system calls should NOT be restarted by that signal. Otherwise,if the signal interrupted a call reading packets in a live capture,when your signal handler returns after calling pcap_breakloop(), thecall will be restarted, and the loop will not terminate until morepackets arrive and the call completes..PPNote also that, in a multi-threaded application, if one thread isblocked in.BR pcap_dispatch() ,.BR pcap_loop() ,.BR pcap_next() ,or.BR pcap_next_ex() ,a call to.B pcap_breakloop()in a different thread will not unblock that thread; you will need to usewhatever mechanism the OS provides for breaking a thread out of blockingcalls in order to unblock the thread, such as thread cancellation insystems that support POSIX threads..ft R.PPNote that.B pcap_next()will, on some platforms, loop reading packets from the OS; that loopwill not necessarily be terminated by a signal, so.B pcap_breakloop()should be used to terminate packet processing even if.B pcap_next()is being used..PP.B pcap_breakloop()does not guarantee that no further packets will be processed by.B pcap_dispatch()or.B pcap_loop()after it is called; at most one more packet might be processed..PPIf \-2 is returned from.B pcap_dispatch()or.BR pcap_loop() ,the flag is cleared, so a subsequent call will resume reading packets. If a positive number is returned, the flag is not cleared, so asubsequent call will return \-2 and clear the flag..PP.B pcap_inject()sends a raw packet through the network interface;.I bufpoints to the data of the packet, including the link-layer header, and.I sizeis the number of bytes in the packet.It returns the number of bytes written on success. A return of \-1indicates an error in which case.B pcap_perror()or.B pcap_geterr()may be used to display the error text.Note that, even if you successfully open the network interface, youmight not have permission to send packets on it, or it might not supportsending packets; as.I pcap_open_live()doesn't have a flag to indicate whether to open for capturing, sending,or capturing and sending, you cannot request an open that supportssending and be notified at open time whether sending will be possible.Note also that some devices might not support sending packets..PPNote that, on some platforms, the link-layer header of the packet that'ssent might not be the same as the link-layer header of the packetsupplied to.BR pcap_inject() ,as the source link-layer address, if the header contains such anaddress, might be changed to be the address assigned to the interface onwhich the packet it sent, if the platform doesn't support sendingcompletely raw and unchanged packets. Even worse, some drivers on someplatforms might change the link-layer type field to whatever valuelibpcap used when attaching to the device, even on platforms that.I donominally support sending completely raw and unchanged packets..PP.B pcap_sendpacket()is like.BR pcap_inject() ,but it returns 0 on success and \-1 on failure..RB ( pcap_inject()comes from OpenBSD;.B pcap_sendpacket()comes from WinPcap. Both are provided for compatibility.).PP.B pcap_dump()outputs a packet to the ``savefile'' opened with.BR pcap_dump_open() .Note that its calling arguments are suitable for use with.B pcap_dispatch()or.BR pcap_loop() .If called directly, the .I userparameter is of type .I pcap_dumper_tas returned by.BR pcap_dump_open() ..PP.B pcap_compile()is used to compile the string.I strinto a filter program..I programis a pointer to a.I bpf_programstruct and is filled in by.BR pcap_compile() ..I optimizecontrols whether optimization on the resulting code is performed..I netmaskspecifies the IPv4 netmask of the network on which packets are beingcaptured; it is used only when checking for IPv4 broadcast addresses inthe filter program. If the netmask of the network on which packets arebeing captured isn't known to the program, or if packets are beingcaptured on the Linux "any" pseudo-interface that can capture on morethan one network, a value of 0 can be supplied; tests for IPv4 broadcastaddreses won't be done correctly, but all other tests in the filterprogram will be OK. A return of \-1 indicates an error in which case.BR pcap_geterr()may be used to display the error text..PP.B pcap_compile_nopcap()is similar to.B pcap_compile()except that instead of passing a pcap structure, one passes thesnaplen and linktype explicitly. It is intended to be used forcompiling filters for direct BPF usage, without necessarily havingcalled.BR pcap_open() .A return of \-1 indicates an error; the error text is unavailable..RB ( pcap_compile_nopcap()is a wrapper around.BR pcap_open_dead() ,.BR pcap_compile() ,and.BR pcap_close() ;the latter three routines can be used directly in order to get the errortext for a compilation error.).B.PP.B pcap_setfilter()is used to specify a filter program..I fpis a pointer to a.I bpf_programstruct, usually the result of a call to.BR pcap_compile() ..B \-1is returned on failure, in which case.BR pcap_geterr()may be used to display the error text;.B 0is returned on success..PP.B pcap_freecode()is used to free up allocated memory pointed to by a.I bpf_programstruct generated by.B pcap_compile()when that BPF program is no longer needed, for example after ithas been made the filter program for a pcap structure by a call to.BR pcap_setfilter() ..PP.B pcap_setdirection()is used to specify a direction that packets will be captured..I pcap_direction_t is one of the constants.BR PCAP_D_IN ,.B PCAP_D_OUTor .BR PCAP_D_INOUT ..B PCAP_D_INwill only capture packets received by the device,.B PCAP_D_OUTwill only capture packets sent by the device and.B PCAP_D_INOUTwill capture packets received by or sent by the device..B PCAP_D_INOUTis the default setting if this function is not called. This isn'tnecessarily supported on all platforms; some platforms might return anerror, and some other platforms might not support.BR PCAP_D_OUT .This operation is not supported if a ``savefile'' is being read..B \-1is returned on failure,.B 0is returned on success..PP.B pcap_datalink()returns the link layer type; link layer types it can return include:.PP.RS 5.TP 5.B DLT_NULLBSD loopback encapsulation; the link layer header is a 4-byte field, in.I hostbyte order, containing a PF_ value from.B socket.hfor the network-layer protocol of the packet..IPNote that ``host byte order'' is the byte order of the machine on whichthe packets are captured, and the PF_ values are for the OS of themachine on which the packets are captured; if a live capture is beingdone, ``host byte order'' is the byte order of the machine capturing thepackets, and the PF_ values are those of the OS of the machine capturingthe packets, but if a ``savefile'' is being read, the byte order and PF_values are.I notnecessarily those of the machine reading the capture file..TP 5.B DLT_EN10MBEthernet (10Mb, 100Mb, 1000Mb, and up).TP 5.B DLT_IEEE802IEEE 802.5 Token Ring.TP 5.B DLT_ARCNETARCNET.TP 5.B DLT_SLIPSLIP; the link layer header contains, in order:.RS 10.LPa 1-byte flag, which is 0 for packets received by the machine and 1 forpackets sent by the machine;.LPa 1-byte field, the upper 4 bits of which indicate the type of packet,as per RFC 1144:.RS 5.TP 50x40an unmodified IP datagram (TYPE_IP);.TP 50x70an uncompressed-TCP IP datagram (UNCOMPRESSED_TCP), with that byte beingthe first byte of the raw IP header on the wire, containing theconnection number in the protocol field;.TP 50x80a compressed-TCP IP datagram (COMPRESSED_TCP), with that byte being thefirst byte of the compressed TCP/IP datagram header;.RE.LPfor UNCOMPRESSED_TCP, the rest of the modified IP header, and forCOMPRESSED_TCP, the compressed TCP/IP datagram header;.RE.RS 5.LPfor a total of 16 bytes; the uncompressed IP datagram follows the header..RE.TP 5.B DLT_PPPPPP; if the first 2 bytes are 0xff and 0x03, it's PPP in HDLC-likeframing, with the PPP header following those two bytes, otherwise it's
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -