tcpdump  4.99.1
About: tcpdump is a tool for network monitoring and data acquisition.
  Fossies Dox: tcpdump-4.99.1.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

tcpdump.c File Reference
#include "netdissect-stdinc.h"
#include <sys/stat.h>
#include "missing/getopt_long.h"
#include <pcap.h>
#include <signal.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <sys/resource.h>
#include <pwd.h>
#include <grp.h>
#include "netdissect.h"
#include "interface.h"
#include "addrtoname.h"
#include "machdep.h"
#include "pcap-missing.h"
#include "ascii_strcasecmp.h"
#include "print.h"
#include "fptype.h"
Include dependency graph for tcpdump.c:

Go to the source code of this file.

Data Structures

struct  dump_info
 

Macros

#define PATH_SEPARATOR   '/'
 
#define PATH_MAX   1024
 
#define B_FLAG
 
#define B_FLAG_USAGE
 
#define D_FLAG
 
#define I_FLAG
 
#define j_FLAG
 
#define j_FLAG_USAGE
 
#define J_FLAG
 
#define Q_FLAG
 
#define Q_FLAG_USAGE
 
#define U_FLAG
 
#define SHORTOPTS   "aAb" B_FLAG "c:C:d" D_FLAG "eE:fF:G:hHi:" I_FLAG j_FLAG J_FLAG "KlLm:M:nNOpq" Q_FLAG "r:s:StT:u" U_FLAG "vV:w:W:xXy:Yz:Z:#"
 
#define OPTION_VERSION   128
 
#define OPTION_TSTAMP_PRECISION   129
 
#define OPTION_IMMEDIATE_MODE   130
 
#define OPTION_PRINT   131
 
#define OPTION_LIST_REMOTE_INTERFACES   132
 
#define OPTION_TSTAMP_MICRO   133
 
#define OPTION_TSTAMP_NANO   134
 
#define OPTION_FP_TYPE   135
 
#define OPTION_COUNT   136
 
#define LIST_REMOTE_INTERFACES_USAGE
 
#define IMMEDIATE_MODE_USAGE   ""
 
#define O_BINARY   0
 

Functions

static void error (const char *,...)
 
static void warning (const char *,...)
 
static void exit_tcpdump (int)
 
static void cleanup (int)
 
static void child_cleanup (int)
 
static void print_version (FILE *)
 
static void print_usage (FILE *)
 
static void show_dlts_and_exit (pcap_t *, const char *device)
 
static void print_packet (u_char *, const struct pcap_pkthdr *, const u_char *)
 
static void dump_packet_and_trunc (u_char *, const struct pcap_pkthdr *, const u_char *)
 
static void dump_packet (u_char *, const struct pcap_pkthdr *, const u_char *)
 
static void droproot (const char *, const char *)
 
static void verbose_stats_dump (int sig)
 
static void info (int)
 
static int getWflagChars (int x)
 
static void MakeFilename (char *buffer, char *orig_name, int cnt, int max_chars)
 
static char * get_next_file (FILE *VFile, char *ptr)
 
static char * copy_argv (char **argv)
 
static char * read_infile (char *fname)
 
static pcap_t * open_interface (const char *device, netdissect_options *ndo, char *ebuf)
 
int main (int argc, char **argv)
 
static void compress_savefile (const char *filename)
 
static void print_packets_captured (void)
 

Variables

static const char copyright []
 
static long Cflag
 
static int Cflag_count
 
int dflag
 
static int Gflag
 
static int Gflag_count
 
static time_t Gflag_time
 
static int Lflag
 
static int Iflag
 
static int lflag
 
static int pflag
 
static int Wflag
 
static int WflagChars
 
static char * zflag = NULL
 
static int timeout = 1000
 
static int count_mode
 
static int infodelay
 
static int infoprint
 
char * program_name
 
static void(*)(int) setsignal (int sig, void(*func)(int))
 
static u_int packets_captured
 
static pcap_t * pd
 
static pcap_dumper_t * pdd = NULL
 
static int supports_monitor_mode
 
int optind
 
int opterr
 
char * optarg
 
static const struct option longopts []
 

Macro Definition Documentation

◆ B_FLAG

#define B_FLAG

Definition at line 631 of file tcpdump.c.

◆ B_FLAG_USAGE

#define B_FLAG_USAGE

Definition at line 632 of file tcpdump.c.

◆ D_FLAG

#define D_FLAG

Definition at line 638 of file tcpdump.c.

◆ I_FLAG

#define I_FLAG

Definition at line 644 of file tcpdump.c.

◆ IMMEDIATE_MODE_USAGE

#define IMMEDIATE_MODE_USAGE   ""

Definition at line 766 of file tcpdump.c.

◆ j_FLAG

#define j_FLAG

Definition at line 652 of file tcpdump.c.

◆ J_FLAG

#define J_FLAG

Definition at line 654 of file tcpdump.c.

◆ j_FLAG_USAGE

#define j_FLAG_USAGE

Definition at line 653 of file tcpdump.c.

◆ LIST_REMOTE_INTERFACES_USAGE

#define LIST_REMOTE_INTERFACES_USAGE

Definition at line 760 of file tcpdump.c.

◆ O_BINARY

#define O_BINARY   0

Definition at line 1060 of file tcpdump.c.

◆ OPTION_COUNT

#define OPTION_COUNT   136

Definition at line 705 of file tcpdump.c.

◆ OPTION_FP_TYPE

#define OPTION_FP_TYPE   135

Definition at line 704 of file tcpdump.c.

◆ OPTION_IMMEDIATE_MODE

#define OPTION_IMMEDIATE_MODE   130

Definition at line 699 of file tcpdump.c.

◆ OPTION_LIST_REMOTE_INTERFACES

#define OPTION_LIST_REMOTE_INTERFACES   132

Definition at line 701 of file tcpdump.c.

◆ OPTION_PRINT

#define OPTION_PRINT   131

Definition at line 700 of file tcpdump.c.

◆ OPTION_TSTAMP_MICRO

#define OPTION_TSTAMP_MICRO   133

Definition at line 702 of file tcpdump.c.

◆ OPTION_TSTAMP_NANO

#define OPTION_TSTAMP_NANO   134

Definition at line 703 of file tcpdump.c.

◆ OPTION_TSTAMP_PRECISION

#define OPTION_TSTAMP_PRECISION   129

Definition at line 698 of file tcpdump.c.

◆ OPTION_VERSION

#define OPTION_VERSION   128

Definition at line 697 of file tcpdump.c.

◆ PATH_MAX

#define PATH_MAX   1024

Definition at line 167 of file tcpdump.c.

◆ PATH_SEPARATOR

#define PATH_SEPARATOR   '/'

Definition at line 134 of file tcpdump.c.

◆ Q_FLAG

#define Q_FLAG

Definition at line 665 of file tcpdump.c.

◆ Q_FLAG_USAGE

#define Q_FLAG_USAGE

Definition at line 666 of file tcpdump.c.

◆ SHORTOPTS

#define SHORTOPTS   "aAb" B_FLAG "c:C:d" D_FLAG "eE:fF:G:hHi:" I_FLAG j_FLAG J_FLAG "KlLm:M:nNOpq" Q_FLAG "r:s:StT:u" U_FLAG "vV:w:W:xXy:Yz:Z:#"

Definition at line 675 of file tcpdump.c.

◆ U_FLAG

#define U_FLAG

Definition at line 672 of file tcpdump.c.

Function Documentation

◆ child_cleanup()

static void child_cleanup ( int  )
static

Referenced by main().

◆ cleanup()

static void cleanup ( int  signo)
static

Definition at line 2670 of file tcpdump.c.

References exit_tcpdump(), info(), pd, and S_SUCCESS.

Referenced by main(), and ppp_hdlc().

◆ compress_savefile()

static void compress_savefile ( const char *  filename)
static

Definition at line 2820 of file tcpdump.c.

Referenced by dump_packet_and_trunc().

◆ copy_argv()

static char* copy_argv ( char **  argv)
static

Definition at line 1023 of file tcpdump.c.

References error().

Referenced by main().

◆ droproot()

static void droproot ( const char *  username,
const char *  chroot_dir 
)
static

Definition at line 772 of file tcpdump.c.

References chroot(), DIAG_OFF_CLANG, DIAG_ON_CLANG, error(), and initgroups().

Referenced by main().

◆ dump_packet()

static void dump_packet ( u_char *  user,
const struct pcap_pkthdr *  h,
const u_char *  sp 
)
static

Definition at line 3043 of file tcpdump.c.

References info(), infodelay, infoprint, dump_info::ndo, packets_captured, dump_info::pdd, and pretty_print_packet().

Referenced by main().

◆ dump_packet_and_trunc()

static void dump_packet_and_trunc ( u_char *  user,
const struct pcap_pkthdr *  h,
const u_char *  sp 
)
static

◆ error()

static void error ( const char *  fmt,
  ... 
)
static

◆ exit_tcpdump()

static void exit_tcpdump ( int  status)
static

Definition at line 412 of file tcpdump.c.

References nd_cleanup().

Referenced by cleanup(), dump_packet_and_trunc(), error(), and main().

◆ get_next_file()

static char* get_next_file ( FILE *  VFile,
char *  ptr 
)
static

Definition at line 874 of file tcpdump.c.

References PATH_MAX.

Referenced by main().

◆ getWflagChars()

static int getWflagChars ( int  x)
static

Definition at line 826 of file tcpdump.c.

Referenced by main().

◆ info()

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 1462 of file tcpdump.c.

References abort_on_misalignment(), ascii_strcasecmp(), bpf_dump(), Cflag, child_cleanup(), cleanup(), cmds, copy_argv(), count_mode, dump_info::CurrentFileName, dflag, DIAG_OFF_CLANG, DIAG_ON_CLANG, droproot(), dump_packet(), dump_packet_and_trunc(), error(), exit_tcpdump(), float_type_check(), get_if_printer(), get_next_file(), getopt_long(), getWflagChars(), Gflag, Gflag_count, Gflag_time, Iflag, info(), init_print(), INT64_T_CONSTANT, isatty(), Lflag, lflag, longopts, MakeFilename(), MAXIMUM_SNAPLEN, nd_have_smi_support(), nd_init(), nd_load_smi_module(), dump_info::ndo, netdissect_options::ndo_Aflag, netdissect_options::ndo_bflag, netdissect_options::ndo_eflag, netdissect_options::ndo_espsecret, netdissect_options::ndo_fflag, netdissect_options::ndo_Hflag, netdissect_options::ndo_if_printer, netdissect_options::ndo_Kflag, netdissect_options::ndo_nflag, netdissect_options::ndo_Nflag, netdissect_options::ndo_packet_number, netdissect_options::ndo_packettype, netdissect_options::ndo_qflag, ndo_set_function_pointers(), netdissect_options::ndo_Sflag, netdissect_options::ndo_sigsecret, netdissect_options::ndo_snaplen, netdissect_options::ndo_suppress_default_print, netdissect_options::ndo_tflag, netdissect_options::ndo_tstamp_precision, netdissect_options::ndo_uflag, netdissect_options::ndo_vflag, netdissect_options::ndo_xflag, netdissect_options::ndo_Xflag, open_interface(), optarg, optind, OPTION_COUNT, OPTION_FP_TYPE, OPTION_IMMEDIATE_MODE, OPTION_LIST_REMOTE_INTERFACES, OPTION_PRINT, OPTION_TSTAMP_MICRO, OPTION_TSTAMP_NANO, OPTION_TSTAMP_PRECISION, OPTION_VERSION, packets_captured, PATH_MAX, PATH_SEPARATOR, pcap_datalink_name_to_val(), pcap_datalink_val_to_description(), pcap_datalink_val_to_name(), pd, dump_info::pd, pdd, dump_info::pdd, pflag, PLURAL_SUFFIX, print, print_packet(), print_usage(), print_version(), netdissect_options::program_name, program_name, PT_AODV, PT_CARP, PT_CNFP, PT_DOMAIN, PT_LMP, PT_PGM, PT_PGM_ZMTP1, PT_PTP, PT_RADIUS, PT_RESP, PT_RPC, PT_RTCP, PT_RTP, PT_SNMP, PT_SOMEIP, PT_TFTP, PT_VAT, PT_VXLAN, PT_WB, PT_ZMTP1, read_infile(), S_ERR_HOST_PROGRAM, S_SUCCESS, setlinebuf(), setsignal, SHORTOPTS, show_dlts_and_exit(), strdup(), strtoint64_t, strtol(), time(), timeout, verbose_stats_dump(), warning(), dump_info::WFileName, Wflag, WflagChars, and zflag.

◆ MakeFilename()

static void MakeFilename ( char *  buffer,
char *  orig_name,
int  cnt,
int  max_chars 
)
static

Definition at line 841 of file tcpdump.c.

References error(), Gflag, Gflag_time, PATH_MAX, snprintf(), and strftime().

Referenced by dump_packet_and_trunc(), and main().

◆ open_interface()

static pcap_t* open_interface ( const char *  device,
netdissect_options ndo,
char *  ebuf 
)
static

◆ print_packet()

static void print_packet ( u_char *  user,
const struct pcap_pkthdr *  h,
const u_char *  sp 
)
static

Definition at line 3068 of file tcpdump.c.

References count_mode, info(), infodelay, infoprint, packets_captured, and pretty_print_packet().

Referenced by main().

◆ print_packets_captured()

static void print_packets_captured ( void  )
static

Definition at line 3103 of file tcpdump.c.

References infodelay, and packets_captured.

Referenced by verbose_stats_dump().

◆ print_usage()

static void print_usage ( FILE *  f)
static

◆ print_version()

static void print_version ( FILE *  f)
static

Definition at line 3132 of file tcpdump.c.

References nd_smi_version_string(), and program_name.

Referenced by main(), print_usage(), and slow_print().

◆ read_infile()

static char* read_infile ( char *  fname)
static

Definition at line 1064 of file tcpdump.c.

References close(), error(), O_BINARY, our_fstat, our_statb, and read().

Referenced by main().

◆ show_dlts_and_exit()

static void show_dlts_and_exit ( pcap_t *  pc,
const char *  device 
)
static

Definition at line 453 of file tcpdump.c.

Referenced by main().

◆ verbose_stats_dump()

static void verbose_stats_dump ( int  sig)
static

Definition at line 3124 of file tcpdump.c.

References print_packets_captured().

Referenced by main().

◆ warning()

static void warning ( const char *  fmt,
  ... 
)
static

Definition at line 396 of file tcpdump.c.

References program_name.

Referenced by main(), and open_interface().

Variable Documentation

◆ Cflag

long Cflag
static

Definition at line 186 of file tcpdump.c.

Referenced by dump_packet_and_trunc(), and main().

◆ Cflag_count

int Cflag_count
static

Definition at line 188 of file tcpdump.c.

Referenced by dump_packet_and_trunc().

◆ copyright

const char copyright[]
static
Initial value:
=
"@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
The Regents of the University of California. All rights reserved.\n"

Definition at line 59 of file tcpdump.c.

◆ count_mode

int count_mode
static

Definition at line 234 of file tcpdump.c.

Referenced by main(), and print_packet().

◆ dflag

int dflag

Definition at line 209 of file tcpdump.c.

Referenced by main().

◆ Gflag

int Gflag
static

Definition at line 210 of file tcpdump.c.

Referenced by dump_packet_and_trunc(), main(), and MakeFilename().

◆ Gflag_count

int Gflag_count
static

Definition at line 211 of file tcpdump.c.

Referenced by dump_packet_and_trunc(), and main().

◆ Gflag_time

time_t Gflag_time
static

Definition at line 212 of file tcpdump.c.

Referenced by dump_packet_and_trunc(), main(), and MakeFilename().

◆ Iflag

int Iflag
static

Definition at line 214 of file tcpdump.c.

Referenced by main(), and open_interface().

◆ infodelay

int infodelay
static

◆ infoprint

int infoprint
static

Definition at line 237 of file tcpdump.c.

Referenced by dump_packet(), dump_packet_and_trunc(), info(), and print_packet().

◆ Lflag

int Lflag
static

Definition at line 213 of file tcpdump.c.

Referenced by main().

◆ lflag

int lflag
static

Definition at line 219 of file tcpdump.c.

Referenced by isis_print_ext_is_reach(), and main().

◆ longopts

const struct option longopts[]
static

Definition at line 453 of file tcpdump.c.

Referenced by main().

◆ optarg

char* optarg
extern

Definition at line 82 of file getopt_long.c.

Referenced by getopt_internal(), main(), and parse_long_options().

◆ opterr

int opterr
extern

Definition at line 83 of file getopt_long.c.

◆ optind

int optind
extern

Definition at line 83 of file getopt_long.c.

Referenced by getopt_internal(), main(), and parse_long_options().

◆ packets_captured

u_int packets_captured
static

◆ pd

pcap_t* pd
static

Definition at line 304 of file tcpdump.c.

Referenced by cleanup(), decode_prefix6(), dump_packet_and_trunc(), info(), main(), and pd_valid().

◆ pdd

pcap_dumper_t* pdd = NULL
static

Definition at line 305 of file tcpdump.c.

Referenced by main().

◆ pflag

int pflag
static

Definition at line 220 of file tcpdump.c.

Referenced by main(), and open_interface().

◆ program_name

char* program_name

Definition at line 239 of file tcpdump.c.

Referenced by error(), info(), main(), print_usage(), print_version(), warning(), and warnx().

◆ setsignal

void(*)(int) setsignal(int sig, void(*func)(int)) ( int  sig,
void(*)(int)  func 
)
static

Definition at line 249 of file tcpdump.c.

Referenced by main().

◆ supports_monitor_mode

int supports_monitor_mode
static

Definition at line 307 of file tcpdump.c.

Referenced by open_interface().

◆ timeout

int timeout = 1000
static

Definition at line 230 of file tcpdump.c.

Referenced by main(), open_interface(), and print_attr_num().

◆ Wflag

int Wflag
static

Definition at line 227 of file tcpdump.c.

Referenced by dump_packet_and_trunc(), and main().

◆ WflagChars

int WflagChars
static

Definition at line 228 of file tcpdump.c.

Referenced by dump_packet_and_trunc(), and main().

◆ zflag

char* zflag = NULL
static

Definition at line 229 of file tcpdump.c.

Referenced by dump_packet_and_trunc(), and main().