1511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall/* 2511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000 3511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall * The Regents of the University of California. All rights reserved. 4511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall * 5511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall * Redistribution and use in source and binary forms, with or without 6511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall * modification, are permitted provided that: (1) source code distributions 7511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall * retain the above copyright notice and this paragraph in its entirety, (2) 8511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall * distributions including binary code include the above copyright notice and 9511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall * this paragraph in its entirety in the documentation or other materials 10511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall * provided with the distribution, and (3) all advertising materials mentioning 11511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall * features or use of this software display the following acknowledgement: 12511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall * ``This product includes software developed by the University of California, 13511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 14511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall * the University nor the names of its contributors may be used to endorse 15511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall * or promote products derived from this software without specific prior 16511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall * written permission. 17511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 18511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 19511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 20511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall */ 21511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall 22511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall#ifndef lint 23965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughesstatic const char copyright[] _U_ = 24511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\ 25511eca30a483e912c274e1d8ba3a0f8f081e2227JP AbgrallThe Regents of the University of California. All rights reserved.\n"; 26511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall#endif 27511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall 28965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes/* 29965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * Tests how select() and poll() behave on the selectable file descriptor 30965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * for a pcap_t. 31965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * 32965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * This would be significantly different on Windows, as it'd test 33965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * how WaitForMultipleObjects() would work on the event handle for a 34965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * pcap_t. 35965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes */ 36511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall#include <pcap.h> 37511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall#include <stdio.h> 38511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall#include <stdlib.h> 39511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall#include <string.h> 40511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall#include <stdarg.h> 41511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall#include <unistd.h> 42511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall#include <errno.h> 43511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall#include <sys/types.h> 44965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes#ifdef HAVE_SYS_SELECT_H 45511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall#include <sys/select.h> 46965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes#else 47965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes#include <sys/time.h> /* older UN*Xes */ 48965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes#endif 49511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall#include <poll.h> 50511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall 51511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrallchar *program_name; 52511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall 53965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes/* 54965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * This was introduced by Clang: 55965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * 56965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * http://clang.llvm.org/docs/LanguageExtensions.html#has-attribute 57965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * 58965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * in some version (which version?); it has been picked up by GCC 5.0. 59965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes */ 60965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes#ifndef __has_attribute 61965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes /* 62965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * It's a macro, so you can check whether it's defined to check 63965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * whether it's supported. 64965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * 65965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * If it's not, define it to always return 0, so that we move on to 66965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * the fallback checks. 67965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes */ 68965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes #define __has_attribute(x) 0 69965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes#endif 70965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes 71965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes#if __has_attribute(noreturn) \ 72965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes || (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 205)) \ 73965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590)) \ 74965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes || (defined(__xlC__) && __xlC__ >= 0x0A01) \ 75965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes || (defined(__HP_aCC) && __HP_aCC >= 61000) 76965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes /* 77965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * Compiler with support for it, or GCC 2.5 and later, or Solaris Studio 12 78965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * (Sun C 5.9) and later, or IBM XL C 10.1 and later (do any earlier 79965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * versions of XL C support this?), or HP aCC A.06.10 and later. 80965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes */ 81965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes #define PCAP_NORETURN __attribute((noreturn)) 82965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes#elif defined( _MSC_VER ) 83965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes #define PCAP_NORETURN __declspec(noreturn) 84965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes#else 85965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes #define PCAP_NORETURN 86965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes#endif 87965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes 88965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes#if __has_attribute(__format__) \ 89965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes || (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 203)) \ 90965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes || (defined(__xlC__) && __xlC__ >= 0x0A01) \ 91965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes || (defined(__HP_aCC) && __HP_aCC >= 61000) 92965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes /* 93965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * Compiler with support for it, or GCC 2.3 and later, or IBM XL C 10.1 94965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * and later (do any earlier versions of XL C support this?), 95965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes * or HP aCC A.06.10 and later. 96965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes */ 97965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes #define PCAP_PRINTFLIKE(x,y) __attribute__((__format__(__printf__,x,y))) 98965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes#else 99965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes #define PCAP_PRINTFLIKE(x,y) 100965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes#endif 101965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughes 102511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall/* Forwards */ 103511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrallstatic void countme(u_char *, const struct pcap_pkthdr *, const u_char *); 104965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughesstatic void PCAP_NORETURN usage(void); 105965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughesstatic void PCAP_NORETURN error(const char *, ...) PCAP_PRINTFLIKE(1, 2); 106965a4b5291eead2d8f826d2c87e58a12bb56a214Elliott Hughesstatic void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2); 107511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrallstatic char *copy_argv(char **); 108511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall 109511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrallstatic pcap_t *pd; 110511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall 111511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrallint 112511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrallmain(int argc, char **argv) 113511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall{ 114511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall register int op; 115511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall bpf_u_int32 localnet, netmask; 116511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall register char *cp, *cmdbuf, *device; 117511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall int doselect, dopoll, dotimeout, dononblock; 118511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall struct bpf_program fcode; 119511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall char ebuf[PCAP_ERRBUF_SIZE]; 120511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall int selectable_fd; 121511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall int status; 122511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall int packet_count; 123511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall 124511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall device = NULL; 125511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall doselect = 0; 126511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall dopoll = 0; 127511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall dotimeout = 0; 128511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall dononblock = 0; 129511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall if ((cp = strrchr(argv[0], '/')) != NULL) 130511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall program_name = cp + 1; 131511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall else 132511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall program_name = argv[0]; 133511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall 134511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall opterr = 0; 135511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall while ((op = getopt(argc, argv, "i:sptn")) != -1) { 136511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall switch (op) { 137511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall 138511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall case 'i': 139511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall device = optarg; 140511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall break; 141511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall 142511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall case 's': 143511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall doselect = 1; 144511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall break; 145511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall 146511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall case 'p': 147511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall dopoll = 1; 148511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall break; 149511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall 150511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall case 't': 151511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall dotimeout = 1; 152511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall break; 153511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall 154511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall case 'n': 155511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall dononblock = 1; 156511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall break; 157511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall 158511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall default: 159511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall usage(); 160511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall /* NOTREACHED */ 161511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall } 162511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall } 163511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall 164511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall if (doselect && dopoll) { 165511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall fprintf(stderr, "selpolltest: choose select (-s) or poll (-p), but not both\n"); 166511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall return 1; 167511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall } 168511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall if (dotimeout && !doselect && !dopoll) { 169511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall fprintf(stderr, "selpolltest: timeout (-t) requires select (-s) or poll (-p)\n"); 170511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall return 1; 171511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall } 172511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall if (device == NULL) { 173511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall device = pcap_lookupdev(ebuf); 174511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall if (device == NULL) 175511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall error("%s", ebuf); 176511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall } 177511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall *ebuf = '\0'; 178511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall pd = pcap_open_live(device, 65535, 0, 1000, ebuf); 179511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall if (pd == NULL) 180511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall error("%s", ebuf); 181511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall else if (*ebuf) 182511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall warning("%s", ebuf); 183511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) { 184511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall localnet = 0; 185511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall netmask = 0; 186511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall warning("%s", ebuf); 187511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall } 188511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall cmdbuf = copy_argv(&argv[optind]); 189511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall 190511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall if (pcap_compile(pd, &fcode, cmdbuf, 1, netmask) < 0) 191511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall error("%s", pcap_geterr(pd)); 192511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall 193511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall if (pcap_setfilter(pd, &fcode) < 0) 194511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall error("%s", pcap_geterr(pd)); 195511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall if (pcap_get_selectable_fd(pd) == -1) 196511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall error("pcap_get_selectable_fd() fails"); 197511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall if (dononblock) { 198511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall if (pcap_setnonblock(pd, 1, ebuf) == -1) 199511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall error("pcap_setnonblock failed: %s", ebuf); 200511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall } 201511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall selectable_fd = pcap_get_selectable_fd(pd); 202511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall printf("Listening on %s\n", device); 203511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall if (doselect) { 204511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall for (;;) { 205511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall fd_set setread, setexcept; 206511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall struct timeval seltimeout; 207511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall 208511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall FD_ZERO(&setread); 209511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall FD_SET(selectable_fd, &setread); 210511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall FD_ZERO(&setexcept); 211511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall FD_SET(selectable_fd, &setexcept); 212511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall if (dotimeout) { 213511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall seltimeout.tv_sec = 0; 214511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall seltimeout.tv_usec = 1000; 215511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall status = select(selectable_fd + 1, &setread, 216511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall NULL, &setexcept, &seltimeout); 217511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall } else { 218511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall status = select(selectable_fd + 1, &setread, 219511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall NULL, &setexcept, NULL); 220511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall } 221511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall if (status == -1) { 222511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall printf("Select returns error (%s)\n", 223511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall strerror(errno)); 224511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall } else { 225511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall if (status == 0) 226511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall printf("Select timed out: "); 227511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall else 228511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall printf("Select returned a descriptor: "); 229511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall if (FD_ISSET(selectable_fd, &setread)) 230511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall printf("readable, "); 231511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall else 232511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall printf("not readable, "); 233511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall if (FD_ISSET(selectable_fd, &setexcept)) 234511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall printf("exceptional condition\n"); 235511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall else 236511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall printf("no exceptional condition\n"); 237511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall packet_count = 0; 238511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall status = pcap_dispatch(pd, -1, countme, 239511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall (u_char *)&packet_count); 240511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall if (status < 0) 241511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall break; 242511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall printf("%d packets seen, %d packets counted after select returns\n", 243511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall status, packet_count); 244511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall } 245511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall } 246511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall } else if (dopoll) { 247511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall for (;;) { 248511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall struct pollfd fd; 249511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall int polltimeout; 250511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall 251511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall fd.fd = selectable_fd; 252511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall fd.events = POLLIN; 253511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall if (dotimeout) 254511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall polltimeout = 1; 255511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall else 256511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall polltimeout = -1; 257511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall status = poll(&fd, 1, polltimeout); 258511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall if (status == -1) { 259511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall printf("Poll returns error (%s)\n", 260511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall strerror(errno)); 261511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall } else { 262511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall if (status == 0) 263511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall printf("Poll timed out\n"); 264511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall else { 265511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall printf("Poll returned a descriptor: "); 266511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall if (fd.revents & POLLIN) 267511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall printf("readable, "); 268511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall else 269511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall printf("not readable, "); 270511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall if (fd.revents & POLLERR) 271511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall printf("exceptional condition, "); 272511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall else 273511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall printf("no exceptional condition, "); 274511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall if (fd.revents & POLLHUP) 275511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall printf("disconnect, "); 276511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall else 277511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall printf("no disconnect, "); 278511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall if (fd.revents & POLLNVAL) 279511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall printf("invalid\n"); 280511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall else 281511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall printf("not invalid\n"); 282511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall } 283511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall packet_count = 0; 284511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall status = pcap_dispatch(pd, -1, countme, 285511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall (u_char *)&packet_count); 286511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall if (status < 0) 287511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall break; 288511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall printf("%d packets seen, %d packets counted after poll returns\n", 289511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall status, packet_count); 290511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall } 291511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall } 292511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall } else { 293511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall for (;;) { 294511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall packet_count = 0; 295511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall status = pcap_dispatch(pd, -1, countme, 296511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall (u_char *)&packet_count); 297511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall if (status < 0) 298511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall break; 299511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall printf("%d packets seen, %d packets counted after pcap_dispatch returns\n", 300511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall status, packet_count); 301511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall } 302511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall } 303511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall if (status == -2) { 304511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall /* 305511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall * We got interrupted, so perhaps we didn't 306511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall * manage to finish a line we were printing. 307511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall * Print an extra newline, just in case. 308511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall */ 309511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall putchar('\n'); 310511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall } 311511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall (void)fflush(stdout); 312511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall if (status == -1) { 313511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall /* 314511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall * Error. Report it. 315511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall */ 316511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall (void)fprintf(stderr, "%s: pcap_loop: %s\n", 317511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall program_name, pcap_geterr(pd)); 318511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall } 319511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall pcap_close(pd); 320511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall exit(status == -1 ? 1 : 0); 321511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall} 322511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall 323511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrallstatic void 324511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrallcountme(u_char *user, const struct pcap_pkthdr *h, const u_char *sp) 325511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall{ 326511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall int *counterp = (int *)user; 327511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall 328511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall (*counterp)++; 329511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall} 330511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall 331511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrallstatic void 332511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrallusage(void) 333511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall{ 334511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall (void)fprintf(stderr, "Usage: %s [ -sptn ] [ -i interface ] [expression]\n", 335511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall program_name); 336511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall exit(1); 337511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall} 338511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall 339511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall/* VARARGS */ 340511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrallstatic void 341511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrallerror(const char *fmt, ...) 342511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall{ 343511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall va_list ap; 344511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall 345511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall (void)fprintf(stderr, "%s: ", program_name); 346511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall va_start(ap, fmt); 347511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall (void)vfprintf(stderr, fmt, ap); 348511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall va_end(ap); 349511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall if (*fmt) { 350511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall fmt += strlen(fmt); 351511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall if (fmt[-1] != '\n') 352511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall (void)fputc('\n', stderr); 353511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall } 354511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall exit(1); 355511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall /* NOTREACHED */ 356511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall} 357511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall 358511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall/* VARARGS */ 359511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrallstatic void 360511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrallwarning(const char *fmt, ...) 361511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall{ 362511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall va_list ap; 363511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall 364511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall (void)fprintf(stderr, "%s: WARNING: ", program_name); 365511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall va_start(ap, fmt); 366511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall (void)vfprintf(stderr, fmt, ap); 367511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall va_end(ap); 368511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall if (*fmt) { 369511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall fmt += strlen(fmt); 370511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall if (fmt[-1] != '\n') 371511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall (void)fputc('\n', stderr); 372511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall } 373511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall} 374511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall 375511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall/* 376511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall * Copy arg vector into a new buffer, concatenating arguments with spaces. 377511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall */ 378511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrallstatic char * 379511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrallcopy_argv(register char **argv) 380511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall{ 381511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall register char **p; 382511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall register u_int len = 0; 383511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall char *buf; 384511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall char *src, *dst; 385511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall 386511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall p = argv; 387511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall if (*p == 0) 388511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall return 0; 389511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall 390511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall while (*p) 391511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall len += strlen(*p++) + 1; 392511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall 393511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall buf = (char *)malloc(len); 394511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall if (buf == NULL) 395511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall error("copy_argv: malloc"); 396511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall 397511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall p = argv; 398511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall dst = buf; 399511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall while ((src = *p++) != NULL) { 400511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall while ((*dst++ = *src++) != '\0') 401511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall ; 402511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall dst[-1] = ' '; 403511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall } 404511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall dst[-1] = '\0'; 405511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall 406511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall return buf; 407511eca30a483e912c274e1d8ba3a0f8f081e2227JP Abgrall} 408