1*57e22627SCy Schubert /* 2*57e22627SCy Schubert * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000 3*57e22627SCy Schubert * The Regents of the University of California. All rights reserved. 4*57e22627SCy Schubert * 5*57e22627SCy Schubert * Redistribution and use in source and binary forms, with or without 6*57e22627SCy Schubert * modification, are permitted provided that: (1) source code distributions 7*57e22627SCy Schubert * retain the above copyright notice and this paragraph in its entirety, (2) 8*57e22627SCy Schubert * distributions including binary code include the above copyright notice and 9*57e22627SCy Schubert * this paragraph in its entirety in the documentation or other materials 10*57e22627SCy Schubert * provided with the distribution, and (3) all advertising materials mentioning 11*57e22627SCy Schubert * features or use of this software display the following acknowledgement: 12*57e22627SCy Schubert * ``This product includes software developed by the University of California, 13*57e22627SCy Schubert * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 14*57e22627SCy Schubert * the University nor the names of its contributors may be used to endorse 15*57e22627SCy Schubert * or promote products derived from this software without specific prior 16*57e22627SCy Schubert * written permission. 17*57e22627SCy Schubert * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 18*57e22627SCy Schubert * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 19*57e22627SCy Schubert * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 20*57e22627SCy Schubert */ 21*57e22627SCy Schubert 22*57e22627SCy Schubert #include "varattrs.h" 23*57e22627SCy Schubert 24*57e22627SCy Schubert #ifndef lint 25*57e22627SCy Schubert static const char copyright[] _U_ = 26*57e22627SCy Schubert "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\ 27*57e22627SCy Schubert The Regents of the University of California. All rights reserved.\n"; 28*57e22627SCy Schubert #endif 29*57e22627SCy Schubert 30*57e22627SCy Schubert #include <pcap.h> 31*57e22627SCy Schubert #include <stdio.h> 32*57e22627SCy Schubert #include <stdlib.h> 33*57e22627SCy Schubert #include <string.h> 34*57e22627SCy Schubert #include <stdarg.h> 35*57e22627SCy Schubert #ifdef _WIN32 36*57e22627SCy Schubert #include "getopt.h" 37*57e22627SCy Schubert #else 38*57e22627SCy Schubert #include <unistd.h> 39*57e22627SCy Schubert #endif 40*57e22627SCy Schubert #include <errno.h> 41*57e22627SCy Schubert 42*57e22627SCy Schubert #include "pcap/funcattrs.h" 43*57e22627SCy Schubert 44*57e22627SCy Schubert #ifdef _WIN32 45*57e22627SCy Schubert #include "portability.h" 46*57e22627SCy Schubert #endif 47*57e22627SCy Schubert 48*57e22627SCy Schubert #define MAXIMUM_SNAPLEN 65535 49*57e22627SCy Schubert 50*57e22627SCy Schubert static char *program_name; 51*57e22627SCy Schubert 52*57e22627SCy Schubert /* Forwards */ 53*57e22627SCy Schubert static void PCAP_NORETURN usage(void); 54*57e22627SCy Schubert static void PCAP_NORETURN error(const char *, ...) PCAP_PRINTFLIKE(1, 2); 55*57e22627SCy Schubert static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2); 56*57e22627SCy Schubert 57*57e22627SCy Schubert int 58*57e22627SCy Schubert main(int argc, char **argv) 59*57e22627SCy Schubert { 60*57e22627SCy Schubert register int op; 61*57e22627SCy Schubert register char *cp, *device; 62*57e22627SCy Schubert int dorfmon, dopromisc, snaplen, useactivate, bufsize; 63*57e22627SCy Schubert char ebuf[PCAP_ERRBUF_SIZE]; 64*57e22627SCy Schubert pcap_if_t *devlist; 65*57e22627SCy Schubert pcap_t *pd; 66*57e22627SCy Schubert int status = 0; 67*57e22627SCy Schubert 68*57e22627SCy Schubert device = NULL; 69*57e22627SCy Schubert dorfmon = 0; 70*57e22627SCy Schubert dopromisc = 0; 71*57e22627SCy Schubert snaplen = MAXIMUM_SNAPLEN; 72*57e22627SCy Schubert bufsize = 0; 73*57e22627SCy Schubert useactivate = 0; 74*57e22627SCy Schubert if ((cp = strrchr(argv[0], '/')) != NULL) 75*57e22627SCy Schubert program_name = cp + 1; 76*57e22627SCy Schubert else 77*57e22627SCy Schubert program_name = argv[0]; 78*57e22627SCy Schubert 79*57e22627SCy Schubert opterr = 0; 80*57e22627SCy Schubert while ((op = getopt(argc, argv, "i:Ips:aB:")) != -1) { 81*57e22627SCy Schubert switch (op) { 82*57e22627SCy Schubert 83*57e22627SCy Schubert case 'i': 84*57e22627SCy Schubert device = optarg; 85*57e22627SCy Schubert break; 86*57e22627SCy Schubert 87*57e22627SCy Schubert case 'I': 88*57e22627SCy Schubert dorfmon = 1; 89*57e22627SCy Schubert useactivate = 1; /* required for rfmon */ 90*57e22627SCy Schubert break; 91*57e22627SCy Schubert 92*57e22627SCy Schubert case 'p': 93*57e22627SCy Schubert dopromisc = 1; 94*57e22627SCy Schubert break; 95*57e22627SCy Schubert 96*57e22627SCy Schubert case 's': { 97*57e22627SCy Schubert char *end; 98*57e22627SCy Schubert 99*57e22627SCy Schubert snaplen = strtol(optarg, &end, 0); 100*57e22627SCy Schubert if (optarg == end || *end != '\0' 101*57e22627SCy Schubert || snaplen < 0 || snaplen > MAXIMUM_SNAPLEN) 102*57e22627SCy Schubert error("invalid snaplen %s", optarg); 103*57e22627SCy Schubert else if (snaplen == 0) 104*57e22627SCy Schubert snaplen = MAXIMUM_SNAPLEN; 105*57e22627SCy Schubert break; 106*57e22627SCy Schubert } 107*57e22627SCy Schubert 108*57e22627SCy Schubert case 'B': 109*57e22627SCy Schubert bufsize = atoi(optarg)*1024; 110*57e22627SCy Schubert if (bufsize <= 0) 111*57e22627SCy Schubert error("invalid packet buffer size %s", optarg); 112*57e22627SCy Schubert useactivate = 1; /* required for bufsize */ 113*57e22627SCy Schubert break; 114*57e22627SCy Schubert 115*57e22627SCy Schubert case 'a': 116*57e22627SCy Schubert useactivate = 1; 117*57e22627SCy Schubert break; 118*57e22627SCy Schubert 119*57e22627SCy Schubert default: 120*57e22627SCy Schubert usage(); 121*57e22627SCy Schubert /* NOTREACHED */ 122*57e22627SCy Schubert } 123*57e22627SCy Schubert } 124*57e22627SCy Schubert 125*57e22627SCy Schubert if (device == NULL) { 126*57e22627SCy Schubert if (pcap_findalldevs(&devlist, ebuf) == -1) 127*57e22627SCy Schubert error("%s", ebuf); 128*57e22627SCy Schubert if (devlist == NULL) 129*57e22627SCy Schubert error("no interfaces available for capture"); 130*57e22627SCy Schubert device = strdup(devlist->name); 131*57e22627SCy Schubert pcap_freealldevs(devlist); 132*57e22627SCy Schubert } 133*57e22627SCy Schubert if (useactivate) { 134*57e22627SCy Schubert pd = pcap_create(device, ebuf); 135*57e22627SCy Schubert if (pd == NULL) 136*57e22627SCy Schubert error("%s: pcap_create failed: %s", device, ebuf); 137*57e22627SCy Schubert status = pcap_set_snaplen(pd, snaplen); 138*57e22627SCy Schubert if (status != 0) 139*57e22627SCy Schubert error("%s: pcap_set_snaplen failed: %s", 140*57e22627SCy Schubert device, pcap_statustostr(status)); 141*57e22627SCy Schubert if (dopromisc) { 142*57e22627SCy Schubert status = pcap_set_promisc(pd, 1); 143*57e22627SCy Schubert if (status != 0) 144*57e22627SCy Schubert error("%s: pcap_set_promisc failed: %s", 145*57e22627SCy Schubert device, pcap_statustostr(status)); 146*57e22627SCy Schubert } 147*57e22627SCy Schubert if (dorfmon) { 148*57e22627SCy Schubert status = pcap_set_rfmon(pd, 1); 149*57e22627SCy Schubert if (status != 0) 150*57e22627SCy Schubert error("%s: pcap_set_rfmon failed: %s", 151*57e22627SCy Schubert device, pcap_statustostr(status)); 152*57e22627SCy Schubert } 153*57e22627SCy Schubert status = pcap_set_timeout(pd, 1000); 154*57e22627SCy Schubert if (status != 0) 155*57e22627SCy Schubert error("%s: pcap_set_timeout failed: %s", 156*57e22627SCy Schubert device, pcap_statustostr(status)); 157*57e22627SCy Schubert if (bufsize != 0) { 158*57e22627SCy Schubert status = pcap_set_buffer_size(pd, bufsize); 159*57e22627SCy Schubert if (status != 0) 160*57e22627SCy Schubert error("%s: pcap_set_buffer_size failed: %s", 161*57e22627SCy Schubert device, pcap_statustostr(status)); 162*57e22627SCy Schubert } 163*57e22627SCy Schubert status = pcap_activate(pd); 164*57e22627SCy Schubert if (status < 0) { 165*57e22627SCy Schubert /* 166*57e22627SCy Schubert * pcap_activate() failed. 167*57e22627SCy Schubert */ 168*57e22627SCy Schubert error("%s: %s\n(%s)", device, 169*57e22627SCy Schubert pcap_statustostr(status), pcap_geterr(pd)); 170*57e22627SCy Schubert } else if (status > 0) { 171*57e22627SCy Schubert /* 172*57e22627SCy Schubert * pcap_activate() succeeded, but it's warning us 173*57e22627SCy Schubert * of a problem it had. 174*57e22627SCy Schubert */ 175*57e22627SCy Schubert warning("%s: %s\n(%s)", device, 176*57e22627SCy Schubert pcap_statustostr(status), pcap_geterr(pd)); 177*57e22627SCy Schubert } else 178*57e22627SCy Schubert printf("%s opened successfully\n", device); 179*57e22627SCy Schubert } else { 180*57e22627SCy Schubert *ebuf = '\0'; 181*57e22627SCy Schubert pd = pcap_open_live(device, 65535, 0, 1000, ebuf); 182*57e22627SCy Schubert if (pd == NULL) 183*57e22627SCy Schubert error("%s", ebuf); 184*57e22627SCy Schubert else if (*ebuf) 185*57e22627SCy Schubert warning("%s", ebuf); 186*57e22627SCy Schubert else 187*57e22627SCy Schubert printf("%s opened successfully\n", device); 188*57e22627SCy Schubert } 189*57e22627SCy Schubert pcap_close(pd); 190*57e22627SCy Schubert exit(status < 0 ? 1 : 0); 191*57e22627SCy Schubert } 192*57e22627SCy Schubert 193*57e22627SCy Schubert static void 194*57e22627SCy Schubert usage(void) 195*57e22627SCy Schubert { 196*57e22627SCy Schubert (void)fprintf(stderr, 197*57e22627SCy Schubert "Usage: %s [ -Ipa ] [ -i interface ] [ -s snaplen ] [ -B bufsize ]\n", 198*57e22627SCy Schubert program_name); 199*57e22627SCy Schubert exit(1); 200*57e22627SCy Schubert } 201*57e22627SCy Schubert 202*57e22627SCy Schubert /* VARARGS */ 203*57e22627SCy Schubert static void 204*57e22627SCy Schubert error(const char *fmt, ...) 205*57e22627SCy Schubert { 206*57e22627SCy Schubert va_list ap; 207*57e22627SCy Schubert 208*57e22627SCy Schubert (void)fprintf(stderr, "%s: ", program_name); 209*57e22627SCy Schubert va_start(ap, fmt); 210*57e22627SCy Schubert (void)vfprintf(stderr, fmt, ap); 211*57e22627SCy Schubert va_end(ap); 212*57e22627SCy Schubert if (*fmt) { 213*57e22627SCy Schubert fmt += strlen(fmt); 214*57e22627SCy Schubert if (fmt[-1] != '\n') 215*57e22627SCy Schubert (void)fputc('\n', stderr); 216*57e22627SCy Schubert } 217*57e22627SCy Schubert exit(1); 218*57e22627SCy Schubert /* NOTREACHED */ 219*57e22627SCy Schubert } 220*57e22627SCy Schubert 221*57e22627SCy Schubert /* VARARGS */ 222*57e22627SCy Schubert static void 223*57e22627SCy Schubert warning(const char *fmt, ...) 224*57e22627SCy Schubert { 225*57e22627SCy Schubert va_list ap; 226*57e22627SCy Schubert 227*57e22627SCy Schubert (void)fprintf(stderr, "%s: WARNING: ", program_name); 228*57e22627SCy Schubert va_start(ap, fmt); 229*57e22627SCy Schubert (void)vfprintf(stderr, fmt, ap); 230*57e22627SCy Schubert va_end(ap); 231*57e22627SCy Schubert if (*fmt) { 232*57e22627SCy Schubert fmt += strlen(fmt); 233*57e22627SCy Schubert if (fmt[-1] != '\n') 234*57e22627SCy Schubert (void)fputc('\n', stderr); 235*57e22627SCy Schubert } 236*57e22627SCy Schubert } 237