📄 pcap.3
字号:
.\" @(#) $Header: /tcpdump/master/libpcap/pcap.3,v 1.64.2.9 2006/01/22 20:12:10 guy Exp $.\".\" Copyright (c) 1994, 1996, 1997.\" The Regents of the University of California. All rights reserved..\".\" Redistribution and use in source and binary forms, with or without.\" modification, are permitted provided that: (1) source code distributions.\" retain the above copyright notice and this paragraph in its entirety, (2).\" distributions including binary code include the above copyright notice and.\" this paragraph in its entirety in the documentation or other materials.\" provided with the distribution, and (3) all advertising materials mentioning.\" features or use of this software display the following acknowledgement:.\" ``This product includes software developed by the University of California,.\" Lawrence Berkeley Laboratory and its contributors.'' Neither the name of.\" the University nor the names of its contributors may be used to endorse.\" or promote products derived from this software without specific prior.\" written permission..\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED.\" WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF.\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE..\".TH PCAP 3 "27 February 2004".SH NAMEpcap \- Packet Capture library.SH SYNOPSIS.nf.ft B#include <pcap.h>.ft.LP.nf.ft Bchar errbuf[PCAP_ERRBUF_SIZE];.ft.LP.ft Bpcap_t *pcap_open_live(const char *device, int snaplen,.ti +8int promisc, int to_ms, char *errbuf)pcap_t *pcap_open_dead(int linktype, int snaplen)pcap_t *pcap_open_offline(const char *fname, char *errbuf)pcap_t *pcap_fopen_offline(FILE *fp, char *errbuf)pcap_dumper_t *pcap_dump_open(pcap_t *p, const char *fname)pcap_dumper_t *pcap_dump_fopen(pcap_t *p, FILE *fp).ft.LP.ft Bint pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf);int pcap_getnonblock(pcap_t *p, char *errbuf);.ft.LP.ft Bint pcap_findalldevs(pcap_if_t **alldevsp, char *errbuf)void pcap_freealldevs(pcap_if_t *alldevs)char *pcap_lookupdev(char *errbuf)int pcap_lookupnet(const char *device, bpf_u_int32 *netp,.ti +8bpf_u_int32 *maskp, char *errbuf).ft.LP.ft Btypedef void (*pcap_handler)(u_char *user, const struct pcap_pkthdr *h,.ti +8 const u_char *bytes);.ft Bint pcap_dispatch(pcap_t *p, int cnt,.ti +8pcap_handler callback, u_char *user)int pcap_loop(pcap_t *p, int cnt,.ti +8pcap_handler callback, u_char *user)void pcap_dump(u_char *user, struct pcap_pkthdr *h,.ti +8u_char *sp).ft.LP.ft Bint pcap_compile(pcap_t *p, struct bpf_program *fp,.ti +8char *str, int optimize, bpf_u_int32 netmask)int pcap_setfilter(pcap_t *p, struct bpf_program *fp)void pcap_freecode(struct bpf_program *)int pcap_setdirection(pcap_t *p, pcap_direction_t d).ft.LP.ft Bconst u_char *pcap_next(pcap_t *p, struct pcap_pkthdr *h)int pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header,.ti +8const u_char **pkt_data).ft.LP.ft Bvoid pcap_breakloop(pcap_t *).ft.LP.ft Bint pcap_inject(pcap_t *p, const void *buf, size_t size)int pcap_sendpacket(pcap_t *p, const u_char *buf, int size).ft.LP.ft Bint pcap_datalink(pcap_t *p)int pcap_list_datalinks(pcap_t *p, int **dlt_buf);int pcap_set_datalink(pcap_t *p, int dlt);int pcap_datalink_name_to_val(const char *name);const char *pcap_datalink_val_to_name(int dlt);const char *pcap_datalink_val_to_description(int dlt);int pcap_snapshot(pcap_t *p)int pcap_is_swapped(pcap_t *p)int pcap_major_version(pcap_t *p)int pcap_minor_version(pcap_t *p)int pcap_stats(pcap_t *p, struct pcap_stat *ps)FILE *pcap_file(pcap_t *p)int pcap_fileno(pcap_t *p)int pcap_get_selectable_fd(pcap_t *p);void pcap_perror(pcap_t *p, char *prefix)char *pcap_geterr(pcap_t *p)char *pcap_strerror(int error)const char *pcap_lib_version(void).ft.LP.ft Bvoid pcap_close(pcap_t *p)int pcap_dump_flush(pcap_dumper_t *p)long pcap_dump_ftell(pcap_dumper_t *p)FILE *pcap_dump_file(pcap_dumper_t *p)void pcap_dump_close(pcap_dumper_t *p).ft.fi.SH DESCRIPTIONThe Packet Capture libraryprovides a high level interface to packet capture systems. All packetson the network, even those destined for other hosts, are accessiblethrough this mechanism..PP.SH ROUTINESNOTE:.I errbufin.BR pcap_open_live() ,.BR pcap_open_dead() ,.BR pcap_open_offline() ,.BR pcap_fopen_offline() ,.BR pcap_setnonblock() ,.BR pcap_getnonblock() ,.BR pcap_findalldevs() ,.BR pcap_lookupdev() ,and.B pcap_lookupnet()is assumed to be able to hold at least.B PCAP_ERRBUF_SIZEchars..PP.B pcap_open_live()is used to obtain a packet capture descriptor to lookat packets on the network..I deviceis a string that specifies the network device to open; on Linux systemswith 2.2 or later kernels, a.I deviceargument of "any" or.B NULLcan be used to capture packets from all interfaces..I snaplenspecifies the maximum number of bytes to capture. If this value is lessthan the size of a packet that is captured, only the first.I snaplenbytes of that packet will be captured and provided as packet data. Avalue of 65535 should be sufficient, on most if not all networks, tocapture all the data available from the packet..I promiscspecifies if the interface is to be put into promiscuous mode.(Note that even if this parameter is false, the interfacecould well be in promiscuous mode for some other reason.) For now, thisdoesn't work on the "any" device; if an argument of "any" or NULL issupplied, the.I promiscflag is ignored..I to_msspecifies the read timeout in milliseconds. The read timeout is used toarrange that the read not necessarily return immediately when a packetis seen, but that it wait for some amount of time to allow more packetsto arrive and to read multiple packets from the OS kernel in oneoperation. Not all platforms support a read timeout; on platforms thatdon't, the read timeout is ignored. A zero value for.IR to_ms ,on platforms that support a read timeout,will cause a read to wait forever to allow enough packets toarrive, with no timeout..I errbufis used to return error or warning text. It will be set to error text when.B pcap_open_live()fails and returns.BR NULL ..I errbufmay also be set to warning text when.B pcap_open_live()succeds; to detect this case the caller should store a zero-length string in.I errbufbefore calling.B pcap_open_live()and display the warning to the user if.I errbufis no longer a zero-length string..PP.B pcap_open_dead()is used for creating a.B pcap_tstructure to use when calling the other functions in libpcap. It istypically used when just using libpcap for compiling BPF code..PP.B pcap_open_offline()is called to open a ``savefile'' for reading..I fnamespecifies the name of the file to open. The file hasthe same format as those used by.B tcpdump(1)and.BR tcpslice(1) .The name "-" in a synonym for.BR stdin .Alternatively, you may call.B pcap_fopen_offline()to read dumped data from an existing open stream.IR fp .Note that on Windows, that stream should be opened in binary mode..I errbufis used to return error text and is only set when.B pcap_open_offline()or.B pcap_fopen_offline()fails and returns.BR NULL ..PP.B pcap_dump_open()is called to open a ``savefile'' for writing. The name "-" in a synonymfor.BR stdout ..B NULLis returned on failure..I pis a.I pcapstruct as returned by.B pcap_open_offline()or.BR pcap_open_live() ..I fnamespecifies the name of the file to open. Alternatively, you may call.B pcap_dump_fopen()to write data to an existing open stream.IR fp .Note that on Windows, that stream should be opened in binary mode.If.B NULLis returned,.B pcap_geterr()can be used to get the error text..PP.PP.B pcap_setnonblock()puts a capture descriptor, opened with.BR pcap_open_live() ,into ``non-blocking'' mode, or takes it out of ``non-blocking'' mode,depending on whether the.I nonblockargument is non-zero or zero. It has no effect on ``savefiles''.If there is an error, \-1 is returned and.I errbufis filled in with an appropriate error message; otherwise, 0 isreturned.In``non-blocking'' mode, an attempt to read from the capture descriptorwith.B pcap_dispatch()will, if no packets are currently available to be read, return 0immediately rather than blocking waiting for packets to arrive..B pcap_loop()and.B pcap_next()will not work in ``non-blocking'' mode..PP.B pcap_getnonblock()returns the current ``non-blocking'' state of the capture descriptor; italways returns 0 on ``savefiles''.If there is an error, \-1 is returned and.I errbufis filled in with an appropriate error message..PP.B pcap_findalldevs()constructs a list of network devices that can be opened with.BR pcap_open_live() .(Note that there may be network devices that cannot be opened with.BR pcap_open_live()by theprocess calling.BR pcap_findalldevs() ,because, for example, that process might not have sufficient privilegesto open them for capturing; if so, those devices will not appear on thelist.).I alldevspis set to point to the first element of the list; each element of thelist is of type.BR pcap_if_t ,and has the following members:.RS.TP.B nextif not.BR NULL ,a pointer to the next element in the list;.B NULLfor the last element of the list.TP.B namea pointer to a string giving a name for the device to pass to.B pcap_open_live().TP.B descriptionif not.BR NULL ,a pointer to a string giving a human-readable description of the device.TP.B addressesa pointer to the first element of a list of addresses for the interface.TP.B flagsinterface flags:.RS.TP.B PCAP_IF_LOOPBACKset if the interface is a loopback interface.RE.RE.PPEach element of the list of addresses is of type.BR pcap_addr_t ,and has the following members:.RS.TP.B nextif not.BR NULL ,a pointer to the next element in the list;.B NULLfor the last element of the list.TP.B addra pointer to a.B "struct sockaddr"containing an address.TP.B netmaskif not.BR NULL ,a pointer to a.B "struct sockaddr"that contains the netmask corresponding to the address pointed to by.B addr.TP.B broadaddrif not.BR NULL ,a pointer to a.B "struct sockaddr"that contains the broadcast address corresponding to the address pointedto by.BR addr ;may be null if the interface doesn't support broadcasts.TP.B dstaddrif not.BR NULL ,a pointer to a.B "struct sockaddr"that contains the destination address corresponding to the address pointedto by.BR addr ;may be null if the interface isn't a point-to-point interface.RE.PPNote that not all the addresses in the list of addresses arenecessarily IPv4 or IPv6 addresses - you must check the.B sa_familymember of the.B "struct sockaddr"before interpreting the contents of the address..PP.B \-1is returned on failure, in which case.B errbufis filled in with an appropriate error message;.B 0is returned on success..PP.B pcap_freealldevs()is used to free a list allocated by.BR pcap_findalldevs() ..PP.B pcap_lookupdev()returns a pointer to a network device suitable for use with.B pcap_open_live()and.BR pcap_lookupnet() .If there is an error,.B NULLis returned and.I errbufis filled in with an appropriate error message..PP.B pcap_lookupnet()is used to determine the network number and maskassociated with the network device.BR device .Both.I netpand.I maskpare.I bpf_u_int32pointers.A return of \-1 indicates an error in which case.I errbufis filled in with an appropriate error message..PP.B pcap_dispatch()is used to collect and process packets..I cntspecifies the maximum number of packets to process before returning.This is not a minimum number; when reading a live capture, only onebufferful of packets is read at a time, so fewer than.I cntpackets may be processed. A.I cntof \-1 processes all the packets received in one buffer when reading alive capture, or all the packets in the file when reading a``savefile''.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -