18cf6c252SPaul Traina /* 2a4b5b39fSBill Fenner * Copyright (c) 1993, 1994, 1995, 1996, 1997, 1998 38cf6c252SPaul Traina * The Regents of the University of California. All rights reserved. 48cf6c252SPaul Traina * 58cf6c252SPaul Traina * Redistribution and use in source and binary forms, with or without 68cf6c252SPaul Traina * modification, are permitted provided that the following conditions 78cf6c252SPaul Traina * are met: 88cf6c252SPaul Traina * 1. Redistributions of source code must retain the above copyright 98cf6c252SPaul Traina * notice, this list of conditions and the following disclaimer. 108cf6c252SPaul Traina * 2. Redistributions in binary form must reproduce the above copyright 118cf6c252SPaul Traina * notice, this list of conditions and the following disclaimer in the 128cf6c252SPaul Traina * documentation and/or other materials provided with the distribution. 138cf6c252SPaul Traina * 3. All advertising materials mentioning features or use of this software 148cf6c252SPaul Traina * must display the following acknowledgement: 158cf6c252SPaul Traina * This product includes software developed by the Computer Systems 168cf6c252SPaul Traina * Engineering Group at Lawrence Berkeley Laboratory. 178cf6c252SPaul Traina * 4. Neither the name of the University nor of the Laboratory may be used 188cf6c252SPaul Traina * to endorse or promote products derived from this software without 198cf6c252SPaul Traina * specific prior written permission. 208cf6c252SPaul Traina * 218cf6c252SPaul Traina * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 228cf6c252SPaul Traina * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 238cf6c252SPaul Traina * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 248cf6c252SPaul Traina * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 258cf6c252SPaul Traina * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 268cf6c252SPaul Traina * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 278cf6c252SPaul Traina * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 288cf6c252SPaul Traina * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 298cf6c252SPaul Traina * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 308cf6c252SPaul Traina * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 318cf6c252SPaul Traina * SUCH DAMAGE. 328cf6c252SPaul Traina */ 338cf6c252SPaul Traina 348cf6c252SPaul Traina #ifndef lint 35feb4ecdbSBruce M Simpson static const char rcsid[] _U_ = 36*a0ee43a1SRui Paulo "@(#) $Header: /tcpdump/master/libpcap/pcap.c,v 1.128 2008-12-23 20:13:29 guy Exp $ (LBL)"; 37dc2c7305SBill Fenner #endif 38dc2c7305SBill Fenner 39dc2c7305SBill Fenner #ifdef HAVE_CONFIG_H 40dc2c7305SBill Fenner #include "config.h" 418cf6c252SPaul Traina #endif 428cf6c252SPaul Traina 43feb4ecdbSBruce M Simpson #ifdef WIN32 44feb4ecdbSBruce M Simpson #include <pcap-stdinc.h> 45feb4ecdbSBruce M Simpson #else /* WIN32 */ 46*a0ee43a1SRui Paulo #if HAVE_INTTYPES_H 47*a0ee43a1SRui Paulo #include <inttypes.h> 48*a0ee43a1SRui Paulo #elif HAVE_STDINT_H 49*a0ee43a1SRui Paulo #include <stdint.h> 50*a0ee43a1SRui Paulo #endif 51*a0ee43a1SRui Paulo #ifdef HAVE_SYS_BITYPES_H 52*a0ee43a1SRui Paulo #include <sys/bitypes.h> 53*a0ee43a1SRui Paulo #endif 548cf6c252SPaul Traina #include <sys/types.h> 55154bbe41SChristian S.J. Peron #include <sys/mman.h> 56feb4ecdbSBruce M Simpson #endif /* WIN32 */ 578cf6c252SPaul Traina 588cf6c252SPaul Traina #include <stdio.h> 598cf6c252SPaul Traina #include <stdlib.h> 608cf6c252SPaul Traina #include <string.h> 6104fb2745SSam Leffler #if !defined(_MSC_VER) && !defined(__BORLANDC__) 628cf6c252SPaul Traina #include <unistd.h> 63feb4ecdbSBruce M Simpson #endif 640a94d38fSBill Fenner #include <fcntl.h> 650a94d38fSBill Fenner #include <errno.h> 668cf6c252SPaul Traina 678cf6c252SPaul Traina #ifdef HAVE_OS_PROTO_H 688cf6c252SPaul Traina #include "os-proto.h" 698cf6c252SPaul Traina #endif 708cf6c252SPaul Traina 7104fb2745SSam Leffler #ifdef MSDOS 7204fb2745SSam Leffler #include "pcap-dos.h" 7304fb2745SSam Leffler #endif 7404fb2745SSam Leffler 758cf6c252SPaul Traina #include "pcap-int.h" 768cf6c252SPaul Traina 77feb4ecdbSBruce M Simpson #ifdef HAVE_DAG_API 78feb4ecdbSBruce M Simpson #include <dagnew.h> 79feb4ecdbSBruce M Simpson #include <dagapi.h> 80feb4ecdbSBruce M Simpson #endif 81feb4ecdbSBruce M Simpson 828cf6c252SPaul Traina int 83a8e07101SRui Paulo pcap_not_initialized(pcap_t *pcap) 84a8e07101SRui Paulo { 85a8e07101SRui Paulo /* this means 'not initialized' */ 86a8e07101SRui Paulo return PCAP_ERROR_NOT_ACTIVATED; 87a8e07101SRui Paulo } 88a8e07101SRui Paulo 89a8e07101SRui Paulo /* 90a8e07101SRui Paulo * Returns 1 if rfmon mode can be set on the pcap_t, 0 if it can't, 91a8e07101SRui Paulo * a PCAP_ERROR value on an error. 92a8e07101SRui Paulo */ 93a8e07101SRui Paulo int 94a8e07101SRui Paulo pcap_can_set_rfmon(pcap_t *p) 95a8e07101SRui Paulo { 96a8e07101SRui Paulo return (p->can_set_rfmon_op(p)); 97a8e07101SRui Paulo } 98a8e07101SRui Paulo 99a8e07101SRui Paulo /* 100a8e07101SRui Paulo * For systems where rfmon mode is never supported. 101a8e07101SRui Paulo */ 102a8e07101SRui Paulo static int 103a8e07101SRui Paulo pcap_cant_set_rfmon(pcap_t *p _U_) 104a8e07101SRui Paulo { 105a8e07101SRui Paulo return (0); 106a8e07101SRui Paulo } 107a8e07101SRui Paulo 108a8e07101SRui Paulo /* 109*a0ee43a1SRui Paulo * Default one-shot callback; overridden for capture types where the 110*a0ee43a1SRui Paulo * packet data cannot be guaranteed to be available after the callback 111*a0ee43a1SRui Paulo * returns, so that a copy must be made. 112a8e07101SRui Paulo */ 113*a0ee43a1SRui Paulo static void 114*a0ee43a1SRui Paulo pcap_oneshot(u_char *user, const struct pcap_pkthdr *h, const u_char *pkt) 115*a0ee43a1SRui Paulo { 116*a0ee43a1SRui Paulo struct oneshot_userdata *sp = (struct oneshot_userdata *)user; 117*a0ee43a1SRui Paulo 118*a0ee43a1SRui Paulo *sp->hdr = *h; 119*a0ee43a1SRui Paulo *sp->pkt = pkt; 120*a0ee43a1SRui Paulo } 121*a0ee43a1SRui Paulo 122*a0ee43a1SRui Paulo const u_char * 123*a0ee43a1SRui Paulo pcap_next(pcap_t *p, struct pcap_pkthdr *h) 124*a0ee43a1SRui Paulo { 125*a0ee43a1SRui Paulo struct oneshot_userdata s; 126*a0ee43a1SRui Paulo const u_char *pkt; 127*a0ee43a1SRui Paulo 128*a0ee43a1SRui Paulo s.hdr = h; 129*a0ee43a1SRui Paulo s.pkt = &pkt; 130*a0ee43a1SRui Paulo s.pd = p; 131*a0ee43a1SRui Paulo if (pcap_dispatch(p, 1, p->oneshot_callback, (u_char *)&s) <= 0) 132*a0ee43a1SRui Paulo return (0); 133*a0ee43a1SRui Paulo return (pkt); 134*a0ee43a1SRui Paulo } 135*a0ee43a1SRui Paulo 136*a0ee43a1SRui Paulo int 137*a0ee43a1SRui Paulo pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header, 138*a0ee43a1SRui Paulo const u_char **pkt_data) 139*a0ee43a1SRui Paulo { 140*a0ee43a1SRui Paulo struct oneshot_userdata s; 141*a0ee43a1SRui Paulo 142*a0ee43a1SRui Paulo s.hdr = &p->pcap_header; 143*a0ee43a1SRui Paulo s.pkt = pkt_data; 144*a0ee43a1SRui Paulo s.pd = p; 145*a0ee43a1SRui Paulo 146*a0ee43a1SRui Paulo /* Saves a pointer to the packet headers */ 147*a0ee43a1SRui Paulo *pkt_header= &p->pcap_header; 148*a0ee43a1SRui Paulo 149*a0ee43a1SRui Paulo if (p->sf.rfile != NULL) { 150*a0ee43a1SRui Paulo int status; 151*a0ee43a1SRui Paulo 152*a0ee43a1SRui Paulo /* We are on an offline capture */ 153*a0ee43a1SRui Paulo status = pcap_offline_read(p, 1, pcap_oneshot, (u_char *)&s); 154a8e07101SRui Paulo 155a8e07101SRui Paulo /* 156*a0ee43a1SRui Paulo * Return codes for pcap_offline_read() are: 157*a0ee43a1SRui Paulo * - 0: EOF 158*a0ee43a1SRui Paulo * - -1: error 159*a0ee43a1SRui Paulo * - >1: OK 160*a0ee43a1SRui Paulo * The first one ('0') conflicts with the return code of 161*a0ee43a1SRui Paulo * 0 from pcap_read() meaning "no packets arrived before 162*a0ee43a1SRui Paulo * the timeout expired", so we map it to -2 so you can 163*a0ee43a1SRui Paulo * distinguish between an EOF from a savefile and a 164*a0ee43a1SRui Paulo * "no packets arrived before the timeout expired, try 165*a0ee43a1SRui Paulo * again" from a live capture. 166*a0ee43a1SRui Paulo */ 167*a0ee43a1SRui Paulo if (status == 0) 168*a0ee43a1SRui Paulo return (-2); 169*a0ee43a1SRui Paulo else 170*a0ee43a1SRui Paulo return (status); 171*a0ee43a1SRui Paulo } 172*a0ee43a1SRui Paulo 173*a0ee43a1SRui Paulo /* 174*a0ee43a1SRui Paulo * Return codes for pcap_read() are: 175*a0ee43a1SRui Paulo * - 0: timeout 176*a0ee43a1SRui Paulo * - -1: error 177*a0ee43a1SRui Paulo * - -2: loop was broken out of with pcap_breakloop() 178*a0ee43a1SRui Paulo * - >1: OK 179*a0ee43a1SRui Paulo * The first one ('0') conflicts with the return code of 0 from 180*a0ee43a1SRui Paulo * pcap_offline_read() meaning "end of file". 181*a0ee43a1SRui Paulo */ 182*a0ee43a1SRui Paulo return (p->read_op(p, 1, pcap_oneshot, (u_char *)&s)); 183*a0ee43a1SRui Paulo } 184*a0ee43a1SRui Paulo 185*a0ee43a1SRui Paulo static void 186*a0ee43a1SRui Paulo initialize_ops(pcap_t *p) 187*a0ee43a1SRui Paulo { 188*a0ee43a1SRui Paulo /* 189*a0ee43a1SRui Paulo * Set operation pointers for operations that only work on 190*a0ee43a1SRui Paulo * an activated pcap_t to point to a routine that returns 191*a0ee43a1SRui Paulo * a "this isn't activated" error. 192a8e07101SRui Paulo */ 193a8e07101SRui Paulo p->read_op = (read_op_t)pcap_not_initialized; 194a8e07101SRui Paulo p->inject_op = (inject_op_t)pcap_not_initialized; 195a8e07101SRui Paulo p->setfilter_op = (setfilter_op_t)pcap_not_initialized; 196a8e07101SRui Paulo p->setdirection_op = (setdirection_op_t)pcap_not_initialized; 197a8e07101SRui Paulo p->set_datalink_op = (set_datalink_op_t)pcap_not_initialized; 198a8e07101SRui Paulo p->getnonblock_op = (getnonblock_op_t)pcap_not_initialized; 199a8e07101SRui Paulo p->setnonblock_op = (setnonblock_op_t)pcap_not_initialized; 200a8e07101SRui Paulo p->stats_op = (stats_op_t)pcap_not_initialized; 201a8e07101SRui Paulo #ifdef WIN32 202a8e07101SRui Paulo p->setbuff_op = (setbuff_op_t)pcap_not_initialized; 203a8e07101SRui Paulo p->setmode_op = (setmode_op_t)pcap_not_initialized; 204a8e07101SRui Paulo p->setmintocopy_op = (setmintocopy_op_t)pcap_not_initialized; 205a8e07101SRui Paulo #endif 206*a0ee43a1SRui Paulo 207*a0ee43a1SRui Paulo /* 208*a0ee43a1SRui Paulo * Default cleanup operation - implementations can override 209*a0ee43a1SRui Paulo * this, but should call pcap_cleanup_live_common() after 210*a0ee43a1SRui Paulo * doing their own additional cleanup. 211*a0ee43a1SRui Paulo */ 212a8e07101SRui Paulo p->cleanup_op = pcap_cleanup_live_common; 213a8e07101SRui Paulo 214*a0ee43a1SRui Paulo /* 215*a0ee43a1SRui Paulo * In most cases, the standard one-short callback can 216*a0ee43a1SRui Paulo * be used for pcap_next()/pcap_next_ex(). 217*a0ee43a1SRui Paulo */ 218*a0ee43a1SRui Paulo p->oneshot_callback = pcap_oneshot; 219*a0ee43a1SRui Paulo } 220*a0ee43a1SRui Paulo 221*a0ee43a1SRui Paulo pcap_t * 222*a0ee43a1SRui Paulo pcap_create_common(const char *source, char *ebuf) 223*a0ee43a1SRui Paulo { 224*a0ee43a1SRui Paulo pcap_t *p; 225*a0ee43a1SRui Paulo 226*a0ee43a1SRui Paulo p = malloc(sizeof(*p)); 227*a0ee43a1SRui Paulo if (p == NULL) { 228*a0ee43a1SRui Paulo snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s", 229*a0ee43a1SRui Paulo pcap_strerror(errno)); 230*a0ee43a1SRui Paulo return (NULL); 231*a0ee43a1SRui Paulo } 232*a0ee43a1SRui Paulo memset(p, 0, sizeof(*p)); 233*a0ee43a1SRui Paulo #ifndef WIN32 234*a0ee43a1SRui Paulo p->fd = -1; /* not opened yet */ 235*a0ee43a1SRui Paulo p->selectable_fd = -1; 236*a0ee43a1SRui Paulo p->send_fd = -1; 237*a0ee43a1SRui Paulo #endif 238*a0ee43a1SRui Paulo 239*a0ee43a1SRui Paulo p->opt.source = strdup(source); 240*a0ee43a1SRui Paulo if (p->opt.source == NULL) { 241*a0ee43a1SRui Paulo snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s", 242*a0ee43a1SRui Paulo pcap_strerror(errno)); 243*a0ee43a1SRui Paulo free(p); 244*a0ee43a1SRui Paulo return (NULL); 245*a0ee43a1SRui Paulo } 246*a0ee43a1SRui Paulo 247*a0ee43a1SRui Paulo /* 248*a0ee43a1SRui Paulo * Default to "can't set rfmon mode"; if it's supported by 249*a0ee43a1SRui Paulo * a platform, the create routine that called us can set 250*a0ee43a1SRui Paulo * the op to its routine to check whether a particular 251*a0ee43a1SRui Paulo * device supports it. 252*a0ee43a1SRui Paulo */ 253*a0ee43a1SRui Paulo p->can_set_rfmon_op = pcap_cant_set_rfmon; 254*a0ee43a1SRui Paulo 255*a0ee43a1SRui Paulo initialize_ops(p); 256*a0ee43a1SRui Paulo 257a8e07101SRui Paulo /* put in some defaults*/ 258a8e07101SRui Paulo pcap_set_timeout(p, 0); 259a8e07101SRui Paulo pcap_set_snaplen(p, 65535); /* max packet size */ 260a8e07101SRui Paulo p->opt.promisc = 0; 261a8e07101SRui Paulo p->opt.buffer_size = 0; 262a8e07101SRui Paulo return (p); 263a8e07101SRui Paulo } 264a8e07101SRui Paulo 265a8e07101SRui Paulo int 266a8e07101SRui Paulo pcap_check_activated(pcap_t *p) 267a8e07101SRui Paulo { 268a8e07101SRui Paulo if (p->activated) { 269a8e07101SRui Paulo snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "can't perform " 270a8e07101SRui Paulo " operation on activated capture"); 271a8e07101SRui Paulo return -1; 272a8e07101SRui Paulo } 273a8e07101SRui Paulo return 0; 274a8e07101SRui Paulo } 275a8e07101SRui Paulo 276a8e07101SRui Paulo int 277a8e07101SRui Paulo pcap_set_snaplen(pcap_t *p, int snaplen) 278a8e07101SRui Paulo { 279a8e07101SRui Paulo if (pcap_check_activated(p)) 280a8e07101SRui Paulo return PCAP_ERROR_ACTIVATED; 281a8e07101SRui Paulo p->snapshot = snaplen; 282a8e07101SRui Paulo return 0; 283a8e07101SRui Paulo } 284a8e07101SRui Paulo 285a8e07101SRui Paulo int 286a8e07101SRui Paulo pcap_set_promisc(pcap_t *p, int promisc) 287a8e07101SRui Paulo { 288a8e07101SRui Paulo if (pcap_check_activated(p)) 289a8e07101SRui Paulo return PCAP_ERROR_ACTIVATED; 290a8e07101SRui Paulo p->opt.promisc = promisc; 291a8e07101SRui Paulo return 0; 292a8e07101SRui Paulo } 293a8e07101SRui Paulo 294a8e07101SRui Paulo int 295a8e07101SRui Paulo pcap_set_rfmon(pcap_t *p, int rfmon) 296a8e07101SRui Paulo { 297a8e07101SRui Paulo if (pcap_check_activated(p)) 298a8e07101SRui Paulo return PCAP_ERROR_ACTIVATED; 299a8e07101SRui Paulo p->opt.rfmon = rfmon; 300a8e07101SRui Paulo return 0; 301a8e07101SRui Paulo } 302a8e07101SRui Paulo 303a8e07101SRui Paulo int 304a8e07101SRui Paulo pcap_set_timeout(pcap_t *p, int timeout_ms) 305a8e07101SRui Paulo { 306a8e07101SRui Paulo if (pcap_check_activated(p)) 307a8e07101SRui Paulo return PCAP_ERROR_ACTIVATED; 308a8e07101SRui Paulo p->md.timeout = timeout_ms; 309a8e07101SRui Paulo return 0; 310a8e07101SRui Paulo } 311a8e07101SRui Paulo 312a8e07101SRui Paulo int 313a8e07101SRui Paulo pcap_set_buffer_size(pcap_t *p, int buffer_size) 314a8e07101SRui Paulo { 315a8e07101SRui Paulo if (pcap_check_activated(p)) 316a8e07101SRui Paulo return PCAP_ERROR_ACTIVATED; 317a8e07101SRui Paulo p->opt.buffer_size = buffer_size; 318a8e07101SRui Paulo return 0; 319a8e07101SRui Paulo } 320a8e07101SRui Paulo 321a8e07101SRui Paulo int 322a8e07101SRui Paulo pcap_activate(pcap_t *p) 323a8e07101SRui Paulo { 324a8e07101SRui Paulo int status; 325a8e07101SRui Paulo 326a8e07101SRui Paulo status = p->activate_op(p); 327a8e07101SRui Paulo if (status >= 0) 328a8e07101SRui Paulo p->activated = 1; 329*a0ee43a1SRui Paulo else { 330*a0ee43a1SRui Paulo if (p->errbuf[0] == '\0') { 331*a0ee43a1SRui Paulo /* 332*a0ee43a1SRui Paulo * No error message supplied by the activate routine; 333*a0ee43a1SRui Paulo * for the benefit of programs that don't specially 334*a0ee43a1SRui Paulo * handle errors other than PCAP_ERROR, return the 335*a0ee43a1SRui Paulo * error message corresponding to the status. 336*a0ee43a1SRui Paulo */ 337*a0ee43a1SRui Paulo snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s", 338*a0ee43a1SRui Paulo pcap_statustostr(status)); 339*a0ee43a1SRui Paulo } 340*a0ee43a1SRui Paulo 341*a0ee43a1SRui Paulo /* 342*a0ee43a1SRui Paulo * Undo any operation pointer setting, etc. done by 343*a0ee43a1SRui Paulo * the activate operation. 344*a0ee43a1SRui Paulo */ 345*a0ee43a1SRui Paulo initialize_ops(p); 346*a0ee43a1SRui Paulo } 347a8e07101SRui Paulo return (status); 348a8e07101SRui Paulo } 349a8e07101SRui Paulo 350a8e07101SRui Paulo pcap_t * 351a8e07101SRui Paulo pcap_open_live(const char *source, int snaplen, int promisc, int to_ms, char *errbuf) 352a8e07101SRui Paulo { 353a8e07101SRui Paulo pcap_t *p; 354a8e07101SRui Paulo int status; 355a8e07101SRui Paulo 356a8e07101SRui Paulo p = pcap_create(source, errbuf); 357a8e07101SRui Paulo if (p == NULL) 358a8e07101SRui Paulo return (NULL); 359a8e07101SRui Paulo status = pcap_set_snaplen(p, snaplen); 360a8e07101SRui Paulo if (status < 0) 361a8e07101SRui Paulo goto fail; 362a8e07101SRui Paulo status = pcap_set_promisc(p, promisc); 363a8e07101SRui Paulo if (status < 0) 364a8e07101SRui Paulo goto fail; 365a8e07101SRui Paulo status = pcap_set_timeout(p, to_ms); 366a8e07101SRui Paulo if (status < 0) 367a8e07101SRui Paulo goto fail; 368a8e07101SRui Paulo /* 369a8e07101SRui Paulo * Mark this as opened with pcap_open_live(), so that, for 370a8e07101SRui Paulo * example, we show the full list of DLT_ values, rather 371a8e07101SRui Paulo * than just the ones that are compatible with capturing 372a8e07101SRui Paulo * when not in monitor mode. That allows existing applications 373a8e07101SRui Paulo * to work the way they used to work, but allows new applications 374a8e07101SRui Paulo * that know about the new open API to, for example, find out the 375a8e07101SRui Paulo * DLT_ values that they can select without changing whether 376a8e07101SRui Paulo * the adapter is in monitor mode or not. 377a8e07101SRui Paulo */ 378a8e07101SRui Paulo p->oldstyle = 1; 379a8e07101SRui Paulo status = pcap_activate(p); 380a8e07101SRui Paulo if (status < 0) 381a8e07101SRui Paulo goto fail; 382a8e07101SRui Paulo return (p); 383a8e07101SRui Paulo fail: 384*a0ee43a1SRui Paulo if (status == PCAP_ERROR) 385*a0ee43a1SRui Paulo snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", source, 386*a0ee43a1SRui Paulo p->errbuf); 387*a0ee43a1SRui Paulo else if (status == PCAP_ERROR_NO_SUCH_DEVICE || 388a8e07101SRui Paulo status == PCAP_ERROR_PERM_DENIED) 389*a0ee43a1SRui Paulo snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s (%s)", source, 390*a0ee43a1SRui Paulo pcap_statustostr(status), p->errbuf); 391a8e07101SRui Paulo else 392a8e07101SRui Paulo snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", source, 393a8e07101SRui Paulo pcap_statustostr(status)); 394a8e07101SRui Paulo pcap_close(p); 395a8e07101SRui Paulo return (NULL); 396a8e07101SRui Paulo } 397a8e07101SRui Paulo 398a8e07101SRui Paulo int 3998cf6c252SPaul Traina pcap_dispatch(pcap_t *p, int cnt, pcap_handler callback, u_char *user) 4008cf6c252SPaul Traina { 401feb4ecdbSBruce M Simpson return p->read_op(p, cnt, callback, user); 402feb4ecdbSBruce M Simpson } 403feb4ecdbSBruce M Simpson 404feb4ecdbSBruce M Simpson /* 405feb4ecdbSBruce M Simpson * XXX - is this necessary? 406feb4ecdbSBruce M Simpson */ 407feb4ecdbSBruce M Simpson int 408feb4ecdbSBruce M Simpson pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) 409feb4ecdbSBruce M Simpson { 410feb4ecdbSBruce M Simpson 411feb4ecdbSBruce M Simpson return p->read_op(p, cnt, callback, user); 4128cf6c252SPaul Traina } 4138cf6c252SPaul Traina 4148cf6c252SPaul Traina int 4158cf6c252SPaul Traina pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user) 4168cf6c252SPaul Traina { 417a4b5b39fSBill Fenner register int n; 418a4b5b39fSBill Fenner 4198cf6c252SPaul Traina for (;;) { 420feb4ecdbSBruce M Simpson if (p->sf.rfile != NULL) { 421feb4ecdbSBruce M Simpson /* 422feb4ecdbSBruce M Simpson * 0 means EOF, so don't loop if we get 0. 423feb4ecdbSBruce M Simpson */ 424a4b5b39fSBill Fenner n = pcap_offline_read(p, cnt, callback, user); 425feb4ecdbSBruce M Simpson } else { 426a4b5b39fSBill Fenner /* 427a4b5b39fSBill Fenner * XXX keep reading until we get something 428a4b5b39fSBill Fenner * (or an error occurs) 429a4b5b39fSBill Fenner */ 430a4b5b39fSBill Fenner do { 431feb4ecdbSBruce M Simpson n = p->read_op(p, cnt, callback, user); 432a4b5b39fSBill Fenner } while (n == 0); 433a4b5b39fSBill Fenner } 4348cf6c252SPaul Traina if (n <= 0) 4358cf6c252SPaul Traina return (n); 4368cf6c252SPaul Traina if (cnt > 0) { 4378cf6c252SPaul Traina cnt -= n; 4388cf6c252SPaul Traina if (cnt <= 0) 4398cf6c252SPaul Traina return (0); 4408cf6c252SPaul Traina } 4418cf6c252SPaul Traina } 4428cf6c252SPaul Traina } 4438cf6c252SPaul Traina 444feb4ecdbSBruce M Simpson /* 445feb4ecdbSBruce M Simpson * Force the loop in "pcap_read()" or "pcap_read_offline()" to terminate. 446feb4ecdbSBruce M Simpson */ 447feb4ecdbSBruce M Simpson void 448feb4ecdbSBruce M Simpson pcap_breakloop(pcap_t *p) 449feb4ecdbSBruce M Simpson { 450feb4ecdbSBruce M Simpson p->break_loop = 1; 451feb4ecdbSBruce M Simpson } 452feb4ecdbSBruce M Simpson 4538cf6c252SPaul Traina int 4548cf6c252SPaul Traina pcap_datalink(pcap_t *p) 4558cf6c252SPaul Traina { 4568cf6c252SPaul Traina return (p->linktype); 4578cf6c252SPaul Traina } 4588cf6c252SPaul Traina 4598cf6c252SPaul Traina int 460a8e07101SRui Paulo pcap_datalink_ext(pcap_t *p) 461a8e07101SRui Paulo { 462a8e07101SRui Paulo return (p->linktype_ext); 463a8e07101SRui Paulo } 464a8e07101SRui Paulo 465a8e07101SRui Paulo int 46609f33d61SBill Fenner pcap_list_datalinks(pcap_t *p, int **dlt_buffer) 46709f33d61SBill Fenner { 468feb4ecdbSBruce M Simpson if (p->dlt_count == 0) { 469feb4ecdbSBruce M Simpson /* 470feb4ecdbSBruce M Simpson * We couldn't fetch the list of DLTs, which means 471feb4ecdbSBruce M Simpson * this platform doesn't support changing the 472feb4ecdbSBruce M Simpson * DLT for an interface. Return a list of DLTs 473feb4ecdbSBruce M Simpson * containing only the DLT this device supports. 474feb4ecdbSBruce M Simpson */ 475feb4ecdbSBruce M Simpson *dlt_buffer = (int*)malloc(sizeof(**dlt_buffer)); 476feb4ecdbSBruce M Simpson if (*dlt_buffer == NULL) { 477feb4ecdbSBruce M Simpson (void)snprintf(p->errbuf, sizeof(p->errbuf), 478feb4ecdbSBruce M Simpson "malloc: %s", pcap_strerror(errno)); 479feb4ecdbSBruce M Simpson return (-1); 48009f33d61SBill Fenner } 481feb4ecdbSBruce M Simpson **dlt_buffer = p->linktype; 482feb4ecdbSBruce M Simpson return (1); 483feb4ecdbSBruce M Simpson } else { 484ef96d74fSMax Laier *dlt_buffer = (int*)calloc(sizeof(**dlt_buffer), p->dlt_count); 48509f33d61SBill Fenner if (*dlt_buffer == NULL) { 486feb4ecdbSBruce M Simpson (void)snprintf(p->errbuf, sizeof(p->errbuf), 487feb4ecdbSBruce M Simpson "malloc: %s", pcap_strerror(errno)); 488feb4ecdbSBruce M Simpson return (-1); 48909f33d61SBill Fenner } 49009f33d61SBill Fenner (void)memcpy(*dlt_buffer, p->dlt_list, 49109f33d61SBill Fenner sizeof(**dlt_buffer) * p->dlt_count); 49209f33d61SBill Fenner return (p->dlt_count); 49309f33d61SBill Fenner } 494feb4ecdbSBruce M Simpson } 495feb4ecdbSBruce M Simpson 496a8e07101SRui Paulo /* 497a8e07101SRui Paulo * In Windows, you might have a library built with one version of the 498a8e07101SRui Paulo * C runtime library and an application built with another version of 499a8e07101SRui Paulo * the C runtime library, which means that the library might use one 500a8e07101SRui Paulo * version of malloc() and free() and the application might use another 501a8e07101SRui Paulo * version of malloc() and free(). If so, that means something 502a8e07101SRui Paulo * allocated by the library cannot be freed by the application, so we 503a8e07101SRui Paulo * need to have a pcap_free_datalinks() routine to free up the list 504a8e07101SRui Paulo * allocated by pcap_list_datalinks(), even though it's just a wrapper 505a8e07101SRui Paulo * around free(). 506a8e07101SRui Paulo */ 507a8e07101SRui Paulo void 508a8e07101SRui Paulo pcap_free_datalinks(int *dlt_list) 509a8e07101SRui Paulo { 510a8e07101SRui Paulo free(dlt_list); 511a8e07101SRui Paulo } 512a8e07101SRui Paulo 513feb4ecdbSBruce M Simpson int 514feb4ecdbSBruce M Simpson pcap_set_datalink(pcap_t *p, int dlt) 515feb4ecdbSBruce M Simpson { 516feb4ecdbSBruce M Simpson int i; 517feb4ecdbSBruce M Simpson const char *dlt_name; 518feb4ecdbSBruce M Simpson 519feb4ecdbSBruce M Simpson if (p->dlt_count == 0 || p->set_datalink_op == NULL) { 520feb4ecdbSBruce M Simpson /* 521feb4ecdbSBruce M Simpson * We couldn't fetch the list of DLTs, or we don't 522feb4ecdbSBruce M Simpson * have a "set datalink" operation, which means 523feb4ecdbSBruce M Simpson * this platform doesn't support changing the 524feb4ecdbSBruce M Simpson * DLT for an interface. Check whether the new 525feb4ecdbSBruce M Simpson * DLT is the one this interface supports. 526feb4ecdbSBruce M Simpson */ 527feb4ecdbSBruce M Simpson if (p->linktype != dlt) 528feb4ecdbSBruce M Simpson goto unsupported; 529feb4ecdbSBruce M Simpson 530feb4ecdbSBruce M Simpson /* 531feb4ecdbSBruce M Simpson * It is, so there's nothing we need to do here. 532feb4ecdbSBruce M Simpson */ 533feb4ecdbSBruce M Simpson return (0); 534feb4ecdbSBruce M Simpson } 535feb4ecdbSBruce M Simpson for (i = 0; i < p->dlt_count; i++) 536feb4ecdbSBruce M Simpson if (p->dlt_list[i] == dlt) 537feb4ecdbSBruce M Simpson break; 538feb4ecdbSBruce M Simpson if (i >= p->dlt_count) 539feb4ecdbSBruce M Simpson goto unsupported; 54004fb2745SSam Leffler if (p->dlt_count == 2 && p->dlt_list[0] == DLT_EN10MB && 54104fb2745SSam Leffler dlt == DLT_DOCSIS) { 54204fb2745SSam Leffler /* 54304fb2745SSam Leffler * This is presumably an Ethernet device, as the first 54404fb2745SSam Leffler * link-layer type it offers is DLT_EN10MB, and the only 54504fb2745SSam Leffler * other type it offers is DLT_DOCSIS. That means that 54604fb2745SSam Leffler * we can't tell the driver to supply DOCSIS link-layer 54704fb2745SSam Leffler * headers - we're just pretending that's what we're 54804fb2745SSam Leffler * getting, as, presumably, we're capturing on a dedicated 54904fb2745SSam Leffler * link to a Cisco Cable Modem Termination System, and 55004fb2745SSam Leffler * it's putting raw DOCSIS frames on the wire inside low-level 55104fb2745SSam Leffler * Ethernet framing. 55204fb2745SSam Leffler */ 55304fb2745SSam Leffler p->linktype = dlt; 55404fb2745SSam Leffler return (0); 55504fb2745SSam Leffler } 556feb4ecdbSBruce M Simpson if (p->set_datalink_op(p, dlt) == -1) 557feb4ecdbSBruce M Simpson return (-1); 558feb4ecdbSBruce M Simpson p->linktype = dlt; 559feb4ecdbSBruce M Simpson return (0); 560feb4ecdbSBruce M Simpson 561feb4ecdbSBruce M Simpson unsupported: 562feb4ecdbSBruce M Simpson dlt_name = pcap_datalink_val_to_name(dlt); 563feb4ecdbSBruce M Simpson if (dlt_name != NULL) { 564feb4ecdbSBruce M Simpson (void) snprintf(p->errbuf, sizeof(p->errbuf), 565feb4ecdbSBruce M Simpson "%s is not one of the DLTs supported by this device", 566feb4ecdbSBruce M Simpson dlt_name); 567feb4ecdbSBruce M Simpson } else { 568feb4ecdbSBruce M Simpson (void) snprintf(p->errbuf, sizeof(p->errbuf), 569feb4ecdbSBruce M Simpson "DLT %d is not one of the DLTs supported by this device", 570feb4ecdbSBruce M Simpson dlt); 571feb4ecdbSBruce M Simpson } 572feb4ecdbSBruce M Simpson return (-1); 573feb4ecdbSBruce M Simpson } 574feb4ecdbSBruce M Simpson 575feb4ecdbSBruce M Simpson struct dlt_choice { 576feb4ecdbSBruce M Simpson const char *name; 577feb4ecdbSBruce M Simpson const char *description; 578feb4ecdbSBruce M Simpson int dlt; 579feb4ecdbSBruce M Simpson }; 580feb4ecdbSBruce M Simpson 581feb4ecdbSBruce M Simpson #define DLT_CHOICE(code, description) { #code, description, code } 582feb4ecdbSBruce M Simpson #define DLT_CHOICE_SENTINEL { NULL, NULL, 0 } 583feb4ecdbSBruce M Simpson 584feb4ecdbSBruce M Simpson static struct dlt_choice dlt_choices[] = { 585feb4ecdbSBruce M Simpson DLT_CHOICE(DLT_NULL, "BSD loopback"), 586feb4ecdbSBruce M Simpson DLT_CHOICE(DLT_EN10MB, "Ethernet"), 587feb4ecdbSBruce M Simpson DLT_CHOICE(DLT_IEEE802, "Token ring"), 588a8e07101SRui Paulo DLT_CHOICE(DLT_ARCNET, "BSD ARCNET"), 589feb4ecdbSBruce M Simpson DLT_CHOICE(DLT_SLIP, "SLIP"), 590feb4ecdbSBruce M Simpson DLT_CHOICE(DLT_PPP, "PPP"), 591feb4ecdbSBruce M Simpson DLT_CHOICE(DLT_FDDI, "FDDI"), 592ee2dd488SSam Leffler DLT_CHOICE(DLT_ATM_RFC1483, "RFC 1483 LLC-encapsulated ATM"), 593feb4ecdbSBruce M Simpson DLT_CHOICE(DLT_RAW, "Raw IP"), 594feb4ecdbSBruce M Simpson DLT_CHOICE(DLT_SLIP_BSDOS, "BSD/OS SLIP"), 595feb4ecdbSBruce M Simpson DLT_CHOICE(DLT_PPP_BSDOS, "BSD/OS PPP"), 596feb4ecdbSBruce M Simpson DLT_CHOICE(DLT_ATM_CLIP, "Linux Classical IP-over-ATM"), 597feb4ecdbSBruce M Simpson DLT_CHOICE(DLT_PPP_SERIAL, "PPP over serial"), 598feb4ecdbSBruce M Simpson DLT_CHOICE(DLT_PPP_ETHER, "PPPoE"), 599a8e07101SRui Paulo DLT_CHOICE(DLT_SYMANTEC_FIREWALL, "Symantec Firewall"), 600feb4ecdbSBruce M Simpson DLT_CHOICE(DLT_C_HDLC, "Cisco HDLC"), 601feb4ecdbSBruce M Simpson DLT_CHOICE(DLT_IEEE802_11, "802.11"), 602feb4ecdbSBruce M Simpson DLT_CHOICE(DLT_FRELAY, "Frame Relay"), 603feb4ecdbSBruce M Simpson DLT_CHOICE(DLT_LOOP, "OpenBSD loopback"), 604feb4ecdbSBruce M Simpson DLT_CHOICE(DLT_ENC, "OpenBSD encapsulated IP"), 605feb4ecdbSBruce M Simpson DLT_CHOICE(DLT_LINUX_SLL, "Linux cooked"), 606feb4ecdbSBruce M Simpson DLT_CHOICE(DLT_LTALK, "Localtalk"), 607feb4ecdbSBruce M Simpson DLT_CHOICE(DLT_PFLOG, "OpenBSD pflog file"), 608feb4ecdbSBruce M Simpson DLT_CHOICE(DLT_PRISM_HEADER, "802.11 plus Prism header"), 609feb4ecdbSBruce M Simpson DLT_CHOICE(DLT_IP_OVER_FC, "RFC 2625 IP-over-Fibre Channel"), 610feb4ecdbSBruce M Simpson DLT_CHOICE(DLT_SUNATM, "Sun raw ATM"), 611a8e07101SRui Paulo DLT_CHOICE(DLT_IEEE802_11_RADIO, "802.11 plus radiotap header"), 612feb4ecdbSBruce M Simpson DLT_CHOICE(DLT_ARCNET_LINUX, "Linux ARCNET"), 613a8e07101SRui Paulo DLT_CHOICE(DLT_JUNIPER_MLPPP, "Juniper Multi-Link PPP"), 614a8e07101SRui Paulo DLT_CHOICE(DLT_JUNIPER_MLFR, "Juniper Multi-Link Frame Relay"), 615a8e07101SRui Paulo DLT_CHOICE(DLT_JUNIPER_ES, "Juniper Encryption Services PIC"), 616a8e07101SRui Paulo DLT_CHOICE(DLT_JUNIPER_GGSN, "Juniper GGSN PIC"), 617a8e07101SRui Paulo DLT_CHOICE(DLT_JUNIPER_MFR, "Juniper FRF.16 Frame Relay"), 618a8e07101SRui Paulo DLT_CHOICE(DLT_JUNIPER_ATM2, "Juniper ATM2 PIC"), 619a8e07101SRui Paulo DLT_CHOICE(DLT_JUNIPER_SERVICES, "Juniper Advanced Services PIC"), 620a8e07101SRui Paulo DLT_CHOICE(DLT_JUNIPER_ATM1, "Juniper ATM1 PIC"), 621a8e07101SRui Paulo DLT_CHOICE(DLT_APPLE_IP_OVER_IEEE1394, "Apple IP-over-IEEE 1394"), 622a8e07101SRui Paulo DLT_CHOICE(DLT_MTP2_WITH_PHDR, "SS7 MTP2 with Pseudo-header"), 623a8e07101SRui Paulo DLT_CHOICE(DLT_MTP2, "SS7 MTP2"), 624a8e07101SRui Paulo DLT_CHOICE(DLT_MTP3, "SS7 MTP3"), 625a8e07101SRui Paulo DLT_CHOICE(DLT_SCCP, "SS7 SCCP"), 62604fb2745SSam Leffler DLT_CHOICE(DLT_DOCSIS, "DOCSIS"), 627feb4ecdbSBruce M Simpson DLT_CHOICE(DLT_LINUX_IRDA, "Linux IrDA"), 628feb4ecdbSBruce M Simpson DLT_CHOICE(DLT_IEEE802_11_RADIO_AVS, "802.11 plus AVS radio information header"), 629a8e07101SRui Paulo DLT_CHOICE(DLT_JUNIPER_MONITOR, "Juniper Passive Monitor PIC"), 63004fb2745SSam Leffler DLT_CHOICE(DLT_PPP_PPPD, "PPP for pppd, with direction flag"), 63104fb2745SSam Leffler DLT_CHOICE(DLT_JUNIPER_PPPOE, "Juniper PPPoE"), 63204fb2745SSam Leffler DLT_CHOICE(DLT_JUNIPER_PPPOE_ATM, "Juniper PPPoE/ATM"), 63304fb2745SSam Leffler DLT_CHOICE(DLT_GPRS_LLC, "GPRS LLC"), 63404fb2745SSam Leffler DLT_CHOICE(DLT_GPF_T, "GPF-T"), 63504fb2745SSam Leffler DLT_CHOICE(DLT_GPF_F, "GPF-F"), 63604fb2745SSam Leffler DLT_CHOICE(DLT_JUNIPER_PIC_PEER, "Juniper PIC Peer"), 63704fb2745SSam Leffler DLT_CHOICE(DLT_ERF_ETH, "Ethernet with Endace ERF header"), 63804fb2745SSam Leffler DLT_CHOICE(DLT_ERF_POS, "Packet-over-SONET with Endace ERF header"), 639a8e07101SRui Paulo DLT_CHOICE(DLT_LINUX_LAPD, "Linux vISDN LAPD"), 6405d18909fSSam Leffler DLT_CHOICE(DLT_JUNIPER_ETHER, "Juniper Ethernet"), 6415d18909fSSam Leffler DLT_CHOICE(DLT_JUNIPER_PPP, "Juniper PPP"), 6425d18909fSSam Leffler DLT_CHOICE(DLT_JUNIPER_FRELAY, "Juniper Frame Relay"), 6435d18909fSSam Leffler DLT_CHOICE(DLT_JUNIPER_CHDLC, "Juniper C-HDLC"), 644ef96d74fSMax Laier DLT_CHOICE(DLT_MFR, "FRF.16 Frame Relay"), 645ef96d74fSMax Laier DLT_CHOICE(DLT_JUNIPER_VP, "Juniper Voice PIC"), 646ef96d74fSMax Laier DLT_CHOICE(DLT_A429, "Arinc 429"), 647ef96d74fSMax Laier DLT_CHOICE(DLT_A653_ICM, "Arinc 653 Interpartition Communication"), 648ef96d74fSMax Laier DLT_CHOICE(DLT_USB, "USB"), 649ef96d74fSMax Laier DLT_CHOICE(DLT_BLUETOOTH_HCI_H4, "Bluetooth HCI UART transport layer"), 650a8e07101SRui Paulo DLT_CHOICE(DLT_IEEE802_16_MAC_CPS, "IEEE 802.16 MAC Common Part Sublayer"), 651a8e07101SRui Paulo DLT_CHOICE(DLT_USB_LINUX, "USB with Linux header"), 652ef96d74fSMax Laier DLT_CHOICE(DLT_CAN20B, "Controller Area Network (CAN) v. 2.0B"), 653a8e07101SRui Paulo DLT_CHOICE(DLT_IEEE802_15_4_LINUX, "IEEE 802.15.4 with Linux padding"), 654a8e07101SRui Paulo DLT_CHOICE(DLT_PPI, "Per-Packet Information"), 655a8e07101SRui Paulo DLT_CHOICE(DLT_IEEE802_16_MAC_CPS_RADIO, "IEEE 802.16 MAC Common Part Sublayer plus radiotap header"), 656a8e07101SRui Paulo DLT_CHOICE(DLT_JUNIPER_ISM, "Juniper Integrated Service Module"), 657a8e07101SRui Paulo DLT_CHOICE(DLT_IEEE802_15_4, "IEEE 802.15.4"), 658a8e07101SRui Paulo DLT_CHOICE(DLT_SITA, "SITA pseudo-header"), 659a8e07101SRui Paulo DLT_CHOICE(DLT_ERF, "Endace ERF header"), 660a8e07101SRui Paulo DLT_CHOICE(DLT_RAIF1, "Ethernet with u10 Networks pseudo-header"), 661a8e07101SRui Paulo DLT_CHOICE(DLT_IPMB, "IPMB"), 662a8e07101SRui Paulo DLT_CHOICE(DLT_JUNIPER_ST, "Juniper Secure Tunnel"), 663a8e07101SRui Paulo DLT_CHOICE(DLT_BLUETOOTH_HCI_H4_WITH_PHDR, "Bluetooth HCI UART transport layer plus pseudo-header"), 664a8e07101SRui Paulo DLT_CHOICE(DLT_AX25_KISS, "AX.25 with KISS header"), 665a8e07101SRui Paulo DLT_CHOICE(DLT_IEEE802_15_4_NONASK_PHY, "IEEE 802.15.4 with non-ASK PHY data"), 666*a0ee43a1SRui Paulo DLT_CHOICE(DLT_MPLS, "MPLS with label as link-layer header"), 667*a0ee43a1SRui Paulo DLT_CHOICE(DLT_USB_LINUX_MMAPPED, "USB with padded Linux header"), 668*a0ee43a1SRui Paulo DLT_CHOICE(DLT_DECT, "DECT"), 669*a0ee43a1SRui Paulo DLT_CHOICE(DLT_AOS, "AOS Space Data Link protocol"), 670*a0ee43a1SRui Paulo DLT_CHOICE(DLT_WIHART, "Wireless HART"), 671*a0ee43a1SRui Paulo DLT_CHOICE(DLT_FC_2, "Fibre Channel FC-2"), 672*a0ee43a1SRui Paulo DLT_CHOICE(DLT_FC_2_WITH_FRAME_DELIMS, "Fibre Channel FC-2 with frame delimiters"), 673*a0ee43a1SRui Paulo DLT_CHOICE(DLT_IPNET, "Solaris ipnet"), 674*a0ee43a1SRui Paulo DLT_CHOICE(DLT_CAN_SOCKETCAN, "CAN-bus with SocketCAN headers"), 675*a0ee43a1SRui Paulo DLT_CHOICE(DLT_IPV4, "Raw IPv4"), 676*a0ee43a1SRui Paulo DLT_CHOICE(DLT_IPV6, "Raw IPv6"), 677feb4ecdbSBruce M Simpson DLT_CHOICE_SENTINEL 678feb4ecdbSBruce M Simpson }; 679feb4ecdbSBruce M Simpson 680feb4ecdbSBruce M Simpson /* 681feb4ecdbSBruce M Simpson * This array is designed for mapping upper and lower case letter 682feb4ecdbSBruce M Simpson * together for a case independent comparison. The mappings are 683feb4ecdbSBruce M Simpson * based upon ascii character sequences. 684feb4ecdbSBruce M Simpson */ 685feb4ecdbSBruce M Simpson static const u_char charmap[] = { 686feb4ecdbSBruce M Simpson (u_char)'\000', (u_char)'\001', (u_char)'\002', (u_char)'\003', 687feb4ecdbSBruce M Simpson (u_char)'\004', (u_char)'\005', (u_char)'\006', (u_char)'\007', 688feb4ecdbSBruce M Simpson (u_char)'\010', (u_char)'\011', (u_char)'\012', (u_char)'\013', 689feb4ecdbSBruce M Simpson (u_char)'\014', (u_char)'\015', (u_char)'\016', (u_char)'\017', 690feb4ecdbSBruce M Simpson (u_char)'\020', (u_char)'\021', (u_char)'\022', (u_char)'\023', 691feb4ecdbSBruce M Simpson (u_char)'\024', (u_char)'\025', (u_char)'\026', (u_char)'\027', 692feb4ecdbSBruce M Simpson (u_char)'\030', (u_char)'\031', (u_char)'\032', (u_char)'\033', 693feb4ecdbSBruce M Simpson (u_char)'\034', (u_char)'\035', (u_char)'\036', (u_char)'\037', 694feb4ecdbSBruce M Simpson (u_char)'\040', (u_char)'\041', (u_char)'\042', (u_char)'\043', 695feb4ecdbSBruce M Simpson (u_char)'\044', (u_char)'\045', (u_char)'\046', (u_char)'\047', 696feb4ecdbSBruce M Simpson (u_char)'\050', (u_char)'\051', (u_char)'\052', (u_char)'\053', 697feb4ecdbSBruce M Simpson (u_char)'\054', (u_char)'\055', (u_char)'\056', (u_char)'\057', 698feb4ecdbSBruce M Simpson (u_char)'\060', (u_char)'\061', (u_char)'\062', (u_char)'\063', 699feb4ecdbSBruce M Simpson (u_char)'\064', (u_char)'\065', (u_char)'\066', (u_char)'\067', 700feb4ecdbSBruce M Simpson (u_char)'\070', (u_char)'\071', (u_char)'\072', (u_char)'\073', 701feb4ecdbSBruce M Simpson (u_char)'\074', (u_char)'\075', (u_char)'\076', (u_char)'\077', 702feb4ecdbSBruce M Simpson (u_char)'\100', (u_char)'\141', (u_char)'\142', (u_char)'\143', 703feb4ecdbSBruce M Simpson (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147', 704feb4ecdbSBruce M Simpson (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153', 705feb4ecdbSBruce M Simpson (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157', 706feb4ecdbSBruce M Simpson (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163', 707feb4ecdbSBruce M Simpson (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167', 708feb4ecdbSBruce M Simpson (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\133', 709feb4ecdbSBruce M Simpson (u_char)'\134', (u_char)'\135', (u_char)'\136', (u_char)'\137', 710feb4ecdbSBruce M Simpson (u_char)'\140', (u_char)'\141', (u_char)'\142', (u_char)'\143', 711feb4ecdbSBruce M Simpson (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147', 712feb4ecdbSBruce M Simpson (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153', 713feb4ecdbSBruce M Simpson (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157', 714feb4ecdbSBruce M Simpson (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163', 715feb4ecdbSBruce M Simpson (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167', 716feb4ecdbSBruce M Simpson (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\173', 717feb4ecdbSBruce M Simpson (u_char)'\174', (u_char)'\175', (u_char)'\176', (u_char)'\177', 718feb4ecdbSBruce M Simpson (u_char)'\200', (u_char)'\201', (u_char)'\202', (u_char)'\203', 719feb4ecdbSBruce M Simpson (u_char)'\204', (u_char)'\205', (u_char)'\206', (u_char)'\207', 720feb4ecdbSBruce M Simpson (u_char)'\210', (u_char)'\211', (u_char)'\212', (u_char)'\213', 721feb4ecdbSBruce M Simpson (u_char)'\214', (u_char)'\215', (u_char)'\216', (u_char)'\217', 722feb4ecdbSBruce M Simpson (u_char)'\220', (u_char)'\221', (u_char)'\222', (u_char)'\223', 723feb4ecdbSBruce M Simpson (u_char)'\224', (u_char)'\225', (u_char)'\226', (u_char)'\227', 724feb4ecdbSBruce M Simpson (u_char)'\230', (u_char)'\231', (u_char)'\232', (u_char)'\233', 725feb4ecdbSBruce M Simpson (u_char)'\234', (u_char)'\235', (u_char)'\236', (u_char)'\237', 726feb4ecdbSBruce M Simpson (u_char)'\240', (u_char)'\241', (u_char)'\242', (u_char)'\243', 727feb4ecdbSBruce M Simpson (u_char)'\244', (u_char)'\245', (u_char)'\246', (u_char)'\247', 728feb4ecdbSBruce M Simpson (u_char)'\250', (u_char)'\251', (u_char)'\252', (u_char)'\253', 729feb4ecdbSBruce M Simpson (u_char)'\254', (u_char)'\255', (u_char)'\256', (u_char)'\257', 730feb4ecdbSBruce M Simpson (u_char)'\260', (u_char)'\261', (u_char)'\262', (u_char)'\263', 731feb4ecdbSBruce M Simpson (u_char)'\264', (u_char)'\265', (u_char)'\266', (u_char)'\267', 732feb4ecdbSBruce M Simpson (u_char)'\270', (u_char)'\271', (u_char)'\272', (u_char)'\273', 733feb4ecdbSBruce M Simpson (u_char)'\274', (u_char)'\275', (u_char)'\276', (u_char)'\277', 734feb4ecdbSBruce M Simpson (u_char)'\300', (u_char)'\341', (u_char)'\342', (u_char)'\343', 735feb4ecdbSBruce M Simpson (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347', 736feb4ecdbSBruce M Simpson (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353', 737feb4ecdbSBruce M Simpson (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357', 738feb4ecdbSBruce M Simpson (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363', 739feb4ecdbSBruce M Simpson (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367', 740feb4ecdbSBruce M Simpson (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\333', 741feb4ecdbSBruce M Simpson (u_char)'\334', (u_char)'\335', (u_char)'\336', (u_char)'\337', 742feb4ecdbSBruce M Simpson (u_char)'\340', (u_char)'\341', (u_char)'\342', (u_char)'\343', 743feb4ecdbSBruce M Simpson (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347', 744feb4ecdbSBruce M Simpson (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353', 745feb4ecdbSBruce M Simpson (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357', 746feb4ecdbSBruce M Simpson (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363', 747feb4ecdbSBruce M Simpson (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367', 748feb4ecdbSBruce M Simpson (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\373', 749feb4ecdbSBruce M Simpson (u_char)'\374', (u_char)'\375', (u_char)'\376', (u_char)'\377', 750feb4ecdbSBruce M Simpson }; 751feb4ecdbSBruce M Simpson 752feb4ecdbSBruce M Simpson int 753feb4ecdbSBruce M Simpson pcap_strcasecmp(const char *s1, const char *s2) 754feb4ecdbSBruce M Simpson { 755feb4ecdbSBruce M Simpson register const u_char *cm = charmap, 756ef96d74fSMax Laier *us1 = (const u_char *)s1, 757ef96d74fSMax Laier *us2 = (const u_char *)s2; 758feb4ecdbSBruce M Simpson 759feb4ecdbSBruce M Simpson while (cm[*us1] == cm[*us2++]) 760feb4ecdbSBruce M Simpson if (*us1++ == '\0') 761feb4ecdbSBruce M Simpson return(0); 762feb4ecdbSBruce M Simpson return (cm[*us1] - cm[*--us2]); 763feb4ecdbSBruce M Simpson } 764feb4ecdbSBruce M Simpson 765feb4ecdbSBruce M Simpson int 766feb4ecdbSBruce M Simpson pcap_datalink_name_to_val(const char *name) 767feb4ecdbSBruce M Simpson { 768feb4ecdbSBruce M Simpson int i; 769feb4ecdbSBruce M Simpson 770feb4ecdbSBruce M Simpson for (i = 0; dlt_choices[i].name != NULL; i++) { 771feb4ecdbSBruce M Simpson if (pcap_strcasecmp(dlt_choices[i].name + sizeof("DLT_") - 1, 772feb4ecdbSBruce M Simpson name) == 0) 773feb4ecdbSBruce M Simpson return (dlt_choices[i].dlt); 774feb4ecdbSBruce M Simpson } 775feb4ecdbSBruce M Simpson return (-1); 776feb4ecdbSBruce M Simpson } 777feb4ecdbSBruce M Simpson 778feb4ecdbSBruce M Simpson const char * 779feb4ecdbSBruce M Simpson pcap_datalink_val_to_name(int dlt) 780feb4ecdbSBruce M Simpson { 781feb4ecdbSBruce M Simpson int i; 782feb4ecdbSBruce M Simpson 783feb4ecdbSBruce M Simpson for (i = 0; dlt_choices[i].name != NULL; i++) { 784feb4ecdbSBruce M Simpson if (dlt_choices[i].dlt == dlt) 785feb4ecdbSBruce M Simpson return (dlt_choices[i].name + sizeof("DLT_") - 1); 786feb4ecdbSBruce M Simpson } 787feb4ecdbSBruce M Simpson return (NULL); 788feb4ecdbSBruce M Simpson } 789feb4ecdbSBruce M Simpson 790feb4ecdbSBruce M Simpson const char * 791feb4ecdbSBruce M Simpson pcap_datalink_val_to_description(int dlt) 792feb4ecdbSBruce M Simpson { 793feb4ecdbSBruce M Simpson int i; 794feb4ecdbSBruce M Simpson 795feb4ecdbSBruce M Simpson for (i = 0; dlt_choices[i].name != NULL; i++) { 796feb4ecdbSBruce M Simpson if (dlt_choices[i].dlt == dlt) 797feb4ecdbSBruce M Simpson return (dlt_choices[i].description); 798feb4ecdbSBruce M Simpson } 799feb4ecdbSBruce M Simpson return (NULL); 800feb4ecdbSBruce M Simpson } 80109f33d61SBill Fenner 80209f33d61SBill Fenner int 8038cf6c252SPaul Traina pcap_snapshot(pcap_t *p) 8048cf6c252SPaul Traina { 8058cf6c252SPaul Traina return (p->snapshot); 8068cf6c252SPaul Traina } 8078cf6c252SPaul Traina 8088cf6c252SPaul Traina int 8098cf6c252SPaul Traina pcap_is_swapped(pcap_t *p) 8108cf6c252SPaul Traina { 8118cf6c252SPaul Traina return (p->sf.swapped); 8128cf6c252SPaul Traina } 8138cf6c252SPaul Traina 8148cf6c252SPaul Traina int 8158cf6c252SPaul Traina pcap_major_version(pcap_t *p) 8168cf6c252SPaul Traina { 8178cf6c252SPaul Traina return (p->sf.version_major); 8188cf6c252SPaul Traina } 8198cf6c252SPaul Traina 8208cf6c252SPaul Traina int 8218cf6c252SPaul Traina pcap_minor_version(pcap_t *p) 8228cf6c252SPaul Traina { 8238cf6c252SPaul Traina return (p->sf.version_minor); 8248cf6c252SPaul Traina } 8258cf6c252SPaul Traina 8268cf6c252SPaul Traina FILE * 8278cf6c252SPaul Traina pcap_file(pcap_t *p) 8288cf6c252SPaul Traina { 8298cf6c252SPaul Traina return (p->sf.rfile); 8308cf6c252SPaul Traina } 8318cf6c252SPaul Traina 8328cf6c252SPaul Traina int 8338cf6c252SPaul Traina pcap_fileno(pcap_t *p) 8348cf6c252SPaul Traina { 835feb4ecdbSBruce M Simpson #ifndef WIN32 8368cf6c252SPaul Traina return (p->fd); 837feb4ecdbSBruce M Simpson #else 838feb4ecdbSBruce M Simpson if (p->adapter != NULL) 839feb4ecdbSBruce M Simpson return ((int)(DWORD)p->adapter->hFile); 840feb4ecdbSBruce M Simpson else 841feb4ecdbSBruce M Simpson return (-1); 842feb4ecdbSBruce M Simpson #endif 8438cf6c252SPaul Traina } 8448cf6c252SPaul Traina 84504fb2745SSam Leffler #if !defined(WIN32) && !defined(MSDOS) 846feb4ecdbSBruce M Simpson int 847feb4ecdbSBruce M Simpson pcap_get_selectable_fd(pcap_t *p) 848feb4ecdbSBruce M Simpson { 849feb4ecdbSBruce M Simpson return (p->selectable_fd); 850feb4ecdbSBruce M Simpson } 851feb4ecdbSBruce M Simpson #endif 852feb4ecdbSBruce M Simpson 8538cf6c252SPaul Traina void 8548cf6c252SPaul Traina pcap_perror(pcap_t *p, char *prefix) 8558cf6c252SPaul Traina { 8568cf6c252SPaul Traina fprintf(stderr, "%s: %s\n", prefix, p->errbuf); 8578cf6c252SPaul Traina } 8588cf6c252SPaul Traina 8598cf6c252SPaul Traina char * 8608cf6c252SPaul Traina pcap_geterr(pcap_t *p) 8618cf6c252SPaul Traina { 8628cf6c252SPaul Traina return (p->errbuf); 8638cf6c252SPaul Traina } 8648cf6c252SPaul Traina 8650a94d38fSBill Fenner int 8660a94d38fSBill Fenner pcap_getnonblock(pcap_t *p, char *errbuf) 8670a94d38fSBill Fenner { 868feb4ecdbSBruce M Simpson return p->getnonblock_op(p, errbuf); 869feb4ecdbSBruce M Simpson } 870feb4ecdbSBruce M Simpson 871feb4ecdbSBruce M Simpson /* 872feb4ecdbSBruce M Simpson * Get the current non-blocking mode setting, under the assumption that 873feb4ecdbSBruce M Simpson * it's just the standard POSIX non-blocking flag. 874feb4ecdbSBruce M Simpson * 875feb4ecdbSBruce M Simpson * We don't look at "p->nonblock", in case somebody tweaked the FD 876feb4ecdbSBruce M Simpson * directly. 877feb4ecdbSBruce M Simpson */ 87804fb2745SSam Leffler #if !defined(WIN32) && !defined(MSDOS) 879feb4ecdbSBruce M Simpson int 880feb4ecdbSBruce M Simpson pcap_getnonblock_fd(pcap_t *p, char *errbuf) 881feb4ecdbSBruce M Simpson { 8820a94d38fSBill Fenner int fdflags; 8830a94d38fSBill Fenner 8840a94d38fSBill Fenner fdflags = fcntl(p->fd, F_GETFL, 0); 8850a94d38fSBill Fenner if (fdflags == -1) { 8860a94d38fSBill Fenner snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s", 8870a94d38fSBill Fenner pcap_strerror(errno)); 8880a94d38fSBill Fenner return (-1); 8890a94d38fSBill Fenner } 8900a94d38fSBill Fenner if (fdflags & O_NONBLOCK) 8910a94d38fSBill Fenner return (1); 8920a94d38fSBill Fenner else 8930a94d38fSBill Fenner return (0); 8940a94d38fSBill Fenner } 895feb4ecdbSBruce M Simpson #endif 8960a94d38fSBill Fenner 8970a94d38fSBill Fenner int 8980a94d38fSBill Fenner pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf) 8990a94d38fSBill Fenner { 900feb4ecdbSBruce M Simpson return p->setnonblock_op(p, nonblock, errbuf); 901feb4ecdbSBruce M Simpson } 902feb4ecdbSBruce M Simpson 90304fb2745SSam Leffler #if !defined(WIN32) && !defined(MSDOS) 904feb4ecdbSBruce M Simpson /* 905feb4ecdbSBruce M Simpson * Set non-blocking mode, under the assumption that it's just the 906feb4ecdbSBruce M Simpson * standard POSIX non-blocking flag. (This can be called by the 907feb4ecdbSBruce M Simpson * per-platform non-blocking-mode routine if that routine also 908feb4ecdbSBruce M Simpson * needs to do some additional work.) 909feb4ecdbSBruce M Simpson */ 910feb4ecdbSBruce M Simpson int 911feb4ecdbSBruce M Simpson pcap_setnonblock_fd(pcap_t *p, int nonblock, char *errbuf) 912feb4ecdbSBruce M Simpson { 9130a94d38fSBill Fenner int fdflags; 9140a94d38fSBill Fenner 9150a94d38fSBill Fenner fdflags = fcntl(p->fd, F_GETFL, 0); 9160a94d38fSBill Fenner if (fdflags == -1) { 9170a94d38fSBill Fenner snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s", 9180a94d38fSBill Fenner pcap_strerror(errno)); 9190a94d38fSBill Fenner return (-1); 9200a94d38fSBill Fenner } 9210a94d38fSBill Fenner if (nonblock) 9220a94d38fSBill Fenner fdflags |= O_NONBLOCK; 9230a94d38fSBill Fenner else 9240a94d38fSBill Fenner fdflags &= ~O_NONBLOCK; 9250a94d38fSBill Fenner if (fcntl(p->fd, F_SETFL, fdflags) == -1) { 9260a94d38fSBill Fenner snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_SETFL: %s", 9270a94d38fSBill Fenner pcap_strerror(errno)); 9280a94d38fSBill Fenner return (-1); 9290a94d38fSBill Fenner } 9300a94d38fSBill Fenner return (0); 9310a94d38fSBill Fenner } 932feb4ecdbSBruce M Simpson #endif 933feb4ecdbSBruce M Simpson 934feb4ecdbSBruce M Simpson #ifdef WIN32 935feb4ecdbSBruce M Simpson /* 936feb4ecdbSBruce M Simpson * Generate a string for the last Win32-specific error (i.e. an error generated when 937feb4ecdbSBruce M Simpson * calling a Win32 API). 938feb4ecdbSBruce M Simpson * For errors occurred during standard C calls, we still use pcap_strerror() 939feb4ecdbSBruce M Simpson */ 940feb4ecdbSBruce M Simpson char * 941feb4ecdbSBruce M Simpson pcap_win32strerror(void) 942feb4ecdbSBruce M Simpson { 943feb4ecdbSBruce M Simpson DWORD error; 944feb4ecdbSBruce M Simpson static char errbuf[PCAP_ERRBUF_SIZE+1]; 945feb4ecdbSBruce M Simpson int errlen; 94604fb2745SSam Leffler char *p; 947feb4ecdbSBruce M Simpson 948feb4ecdbSBruce M Simpson error = GetLastError(); 949feb4ecdbSBruce M Simpson FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, errbuf, 950feb4ecdbSBruce M Simpson PCAP_ERRBUF_SIZE, NULL); 951feb4ecdbSBruce M Simpson 952feb4ecdbSBruce M Simpson /* 953feb4ecdbSBruce M Simpson * "FormatMessage()" "helpfully" sticks CR/LF at the end of the 954feb4ecdbSBruce M Simpson * message. Get rid of it. 955feb4ecdbSBruce M Simpson */ 956feb4ecdbSBruce M Simpson errlen = strlen(errbuf); 957feb4ecdbSBruce M Simpson if (errlen >= 2) { 958feb4ecdbSBruce M Simpson errbuf[errlen - 1] = '\0'; 959feb4ecdbSBruce M Simpson errbuf[errlen - 2] = '\0'; 960feb4ecdbSBruce M Simpson } 96104fb2745SSam Leffler p = strchr(errbuf, '\0'); 96204fb2745SSam Leffler snprintf (p, sizeof(errbuf)-(p-errbuf), " (%lu)", error); 963feb4ecdbSBruce M Simpson return (errbuf); 964feb4ecdbSBruce M Simpson } 965feb4ecdbSBruce M Simpson #endif 9660a94d38fSBill Fenner 9670a94d38fSBill Fenner /* 968a8e07101SRui Paulo * Generate error strings for PCAP_ERROR_ and PCAP_WARNING_ values. 969a8e07101SRui Paulo */ 970a8e07101SRui Paulo const char * 971a8e07101SRui Paulo pcap_statustostr(int errnum) 972a8e07101SRui Paulo { 973a8e07101SRui Paulo static char ebuf[15+10+1]; 974a8e07101SRui Paulo 975a8e07101SRui Paulo switch (errnum) { 976a8e07101SRui Paulo 977a8e07101SRui Paulo case PCAP_WARNING: 978a8e07101SRui Paulo return("Generic warning"); 979a8e07101SRui Paulo 980a8e07101SRui Paulo case PCAP_WARNING_PROMISC_NOTSUP: 981a8e07101SRui Paulo return ("That device doesn't support promiscuous mode"); 982a8e07101SRui Paulo 983a8e07101SRui Paulo case PCAP_ERROR: 984a8e07101SRui Paulo return("Generic error"); 985a8e07101SRui Paulo 986a8e07101SRui Paulo case PCAP_ERROR_BREAK: 987a8e07101SRui Paulo return("Loop terminated by pcap_breakloop"); 988a8e07101SRui Paulo 989a8e07101SRui Paulo case PCAP_ERROR_NOT_ACTIVATED: 990a8e07101SRui Paulo return("The pcap_t has not been activated"); 991a8e07101SRui Paulo 992a8e07101SRui Paulo case PCAP_ERROR_ACTIVATED: 993a8e07101SRui Paulo return ("The setting can't be changed after the pcap_t is activated"); 994a8e07101SRui Paulo 995a8e07101SRui Paulo case PCAP_ERROR_NO_SUCH_DEVICE: 996a8e07101SRui Paulo return ("No such device exists"); 997a8e07101SRui Paulo 998a8e07101SRui Paulo case PCAP_ERROR_RFMON_NOTSUP: 999a8e07101SRui Paulo return ("That device doesn't support monitor mode"); 1000a8e07101SRui Paulo 1001a8e07101SRui Paulo case PCAP_ERROR_NOT_RFMON: 1002a8e07101SRui Paulo return ("That operation is supported only in monitor mode"); 1003a8e07101SRui Paulo 1004a8e07101SRui Paulo case PCAP_ERROR_PERM_DENIED: 1005a8e07101SRui Paulo return ("You don't have permission to capture on that device"); 1006a8e07101SRui Paulo 1007a8e07101SRui Paulo case PCAP_ERROR_IFACE_NOT_UP: 1008a8e07101SRui Paulo return ("That device is not up"); 1009a8e07101SRui Paulo } 1010a8e07101SRui Paulo (void)snprintf(ebuf, sizeof ebuf, "Unknown error: %d", errnum); 1011a8e07101SRui Paulo return(ebuf); 1012a8e07101SRui Paulo } 1013a8e07101SRui Paulo 1014a8e07101SRui Paulo /* 10158cf6c252SPaul Traina * Not all systems have strerror(). 10168cf6c252SPaul Traina */ 1017ef96d74fSMax Laier const char * 10188cf6c252SPaul Traina pcap_strerror(int errnum) 10198cf6c252SPaul Traina { 10208cf6c252SPaul Traina #ifdef HAVE_STRERROR 10218cf6c252SPaul Traina return (strerror(errnum)); 10228cf6c252SPaul Traina #else 10238cf6c252SPaul Traina extern int sys_nerr; 10248cf6c252SPaul Traina extern const char *const sys_errlist[]; 1025a8e07101SRui Paulo static char ebuf[15+10+1]; 10268cf6c252SPaul Traina 10278cf6c252SPaul Traina if ((unsigned int)errnum < sys_nerr) 10288cf6c252SPaul Traina return ((char *)sys_errlist[errnum]); 1029dc2c7305SBill Fenner (void)snprintf(ebuf, sizeof ebuf, "Unknown error: %d", errnum); 10308cf6c252SPaul Traina return(ebuf); 10318cf6c252SPaul Traina #endif 10328cf6c252SPaul Traina } 10338cf6c252SPaul Traina 1034feb4ecdbSBruce M Simpson int 1035feb4ecdbSBruce M Simpson pcap_setfilter(pcap_t *p, struct bpf_program *fp) 1036feb4ecdbSBruce M Simpson { 1037feb4ecdbSBruce M Simpson return p->setfilter_op(p, fp); 1038feb4ecdbSBruce M Simpson } 1039feb4ecdbSBruce M Simpson 1040ee2dd488SSam Leffler /* 1041ee2dd488SSam Leffler * Set direction flag, which controls whether we accept only incoming 1042ee2dd488SSam Leffler * packets, only outgoing packets, or both. 1043ee2dd488SSam Leffler * Note that, depending on the platform, some or all direction arguments 1044ee2dd488SSam Leffler * might not be supported. 1045ee2dd488SSam Leffler */ 1046ee2dd488SSam Leffler int 10475d18909fSSam Leffler pcap_setdirection(pcap_t *p, pcap_direction_t d) 1048ee2dd488SSam Leffler { 1049ee2dd488SSam Leffler if (p->setdirection_op == NULL) { 1050ee2dd488SSam Leffler snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 1051ee2dd488SSam Leffler "Setting direction is not implemented on this platform"); 1052ee2dd488SSam Leffler return -1; 1053ee2dd488SSam Leffler } else 1054ee2dd488SSam Leffler return p->setdirection_op(p, d); 1055ee2dd488SSam Leffler } 1056ee2dd488SSam Leffler 1057feb4ecdbSBruce M Simpson int 1058feb4ecdbSBruce M Simpson pcap_stats(pcap_t *p, struct pcap_stat *ps) 1059feb4ecdbSBruce M Simpson { 1060feb4ecdbSBruce M Simpson return p->stats_op(p, ps); 1061feb4ecdbSBruce M Simpson } 1062feb4ecdbSBruce M Simpson 1063feb4ecdbSBruce M Simpson static int 106404fb2745SSam Leffler pcap_stats_dead(pcap_t *p, struct pcap_stat *ps _U_) 1065feb4ecdbSBruce M Simpson { 1066feb4ecdbSBruce M Simpson snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 1067feb4ecdbSBruce M Simpson "Statistics aren't available from a pcap_open_dead pcap_t"); 1068feb4ecdbSBruce M Simpson return (-1); 1069feb4ecdbSBruce M Simpson } 1070feb4ecdbSBruce M Simpson 1071a8e07101SRui Paulo #ifdef WIN32 1072a8e07101SRui Paulo int 1073a8e07101SRui Paulo pcap_setbuff(pcap_t *p, int dim) 107404fb2745SSam Leffler { 1075a8e07101SRui Paulo return p->setbuff_op(p, dim); 1076a8e07101SRui Paulo } 1077a8e07101SRui Paulo 1078a8e07101SRui Paulo static int 1079a8e07101SRui Paulo pcap_setbuff_dead(pcap_t *p, int dim) 1080a8e07101SRui Paulo { 1081a8e07101SRui Paulo snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 1082a8e07101SRui Paulo "The kernel buffer size cannot be set on a pcap_open_dead pcap_t"); 1083a8e07101SRui Paulo return (-1); 1084a8e07101SRui Paulo } 1085a8e07101SRui Paulo 1086a8e07101SRui Paulo int 1087a8e07101SRui Paulo pcap_setmode(pcap_t *p, int mode) 1088a8e07101SRui Paulo { 1089a8e07101SRui Paulo return p->setmode_op(p, mode); 1090a8e07101SRui Paulo } 1091a8e07101SRui Paulo 1092a8e07101SRui Paulo static int 1093a8e07101SRui Paulo pcap_setmode_dead(pcap_t *p, int mode) 1094a8e07101SRui Paulo { 1095a8e07101SRui Paulo snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 1096a8e07101SRui Paulo "impossible to set mode on a pcap_open_dead pcap_t"); 1097a8e07101SRui Paulo return (-1); 1098a8e07101SRui Paulo } 1099a8e07101SRui Paulo 1100a8e07101SRui Paulo int 1101a8e07101SRui Paulo pcap_setmintocopy(pcap_t *p, int size) 1102a8e07101SRui Paulo { 1103a8e07101SRui Paulo return p->setmintocopy_op(p, size); 1104a8e07101SRui Paulo } 1105a8e07101SRui Paulo 1106a8e07101SRui Paulo static int 1107a8e07101SRui Paulo pcap_setmintocopy_dead(pcap_t *p, int size) 1108a8e07101SRui Paulo { 1109a8e07101SRui Paulo snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 1110a8e07101SRui Paulo "The mintocopy parameter cannot be set on a pcap_open_dead pcap_t"); 1111a8e07101SRui Paulo return (-1); 1112a8e07101SRui Paulo } 1113154bbe41SChristian S.J. Peron #endif 1114a8e07101SRui Paulo 1115a8e07101SRui Paulo /* 1116a8e07101SRui Paulo * On some platforms, we need to clean up promiscuous or monitor mode 1117a8e07101SRui Paulo * when we close a device - and we want that to happen even if the 1118a8e07101SRui Paulo * application just exits without explicitl closing devices. 1119a8e07101SRui Paulo * On those platforms, we need to register a "close all the pcaps" 1120a8e07101SRui Paulo * routine to be called when we exit, and need to maintain a list of 1121a8e07101SRui Paulo * pcaps that need to be closed to clean up modes. 1122a8e07101SRui Paulo * 1123a8e07101SRui Paulo * XXX - not thread-safe. 1124a8e07101SRui Paulo */ 1125a8e07101SRui Paulo 1126a8e07101SRui Paulo /* 1127a8e07101SRui Paulo * List of pcaps on which we've done something that needs to be 1128a8e07101SRui Paulo * cleaned up. 1129a8e07101SRui Paulo * If there are any such pcaps, we arrange to call "pcap_close_all()" 1130a8e07101SRui Paulo * when we exit, and have it close all of them. 1131a8e07101SRui Paulo */ 1132a8e07101SRui Paulo static struct pcap *pcaps_to_close; 1133a8e07101SRui Paulo 1134a8e07101SRui Paulo /* 1135a8e07101SRui Paulo * TRUE if we've already called "atexit()" to cause "pcap_close_all()" to 1136a8e07101SRui Paulo * be called on exit. 1137a8e07101SRui Paulo */ 1138a8e07101SRui Paulo static int did_atexit; 1139a8e07101SRui Paulo 1140a8e07101SRui Paulo static void 1141a8e07101SRui Paulo pcap_close_all(void) 1142a8e07101SRui Paulo { 1143a8e07101SRui Paulo struct pcap *handle; 1144a8e07101SRui Paulo 1145a8e07101SRui Paulo while ((handle = pcaps_to_close) != NULL) 1146a8e07101SRui Paulo pcap_close(handle); 1147a8e07101SRui Paulo } 1148a8e07101SRui Paulo 1149a8e07101SRui Paulo int 1150a8e07101SRui Paulo pcap_do_addexit(pcap_t *p) 1151a8e07101SRui Paulo { 1152a8e07101SRui Paulo /* 1153a8e07101SRui Paulo * If we haven't already done so, arrange to have 1154a8e07101SRui Paulo * "pcap_close_all()" called when we exit. 1155a8e07101SRui Paulo */ 1156a8e07101SRui Paulo if (!did_atexit) { 1157a8e07101SRui Paulo if (atexit(pcap_close_all) == -1) { 1158a8e07101SRui Paulo /* 1159a8e07101SRui Paulo * "atexit()" failed; let our caller know. 1160a8e07101SRui Paulo */ 1161a8e07101SRui Paulo strncpy(p->errbuf, "atexit failed", 1162a8e07101SRui Paulo PCAP_ERRBUF_SIZE); 1163a8e07101SRui Paulo return (0); 1164a8e07101SRui Paulo } 1165a8e07101SRui Paulo did_atexit = 1; 1166a8e07101SRui Paulo } 1167a8e07101SRui Paulo return (1); 1168a8e07101SRui Paulo } 1169a8e07101SRui Paulo 1170a8e07101SRui Paulo void 1171a8e07101SRui Paulo pcap_add_to_pcaps_to_close(pcap_t *p) 1172a8e07101SRui Paulo { 1173a8e07101SRui Paulo p->md.next = pcaps_to_close; 1174a8e07101SRui Paulo pcaps_to_close = p; 1175a8e07101SRui Paulo } 1176a8e07101SRui Paulo 1177a8e07101SRui Paulo void 1178a8e07101SRui Paulo pcap_remove_from_pcaps_to_close(pcap_t *p) 1179a8e07101SRui Paulo { 1180a8e07101SRui Paulo pcap_t *pc, *prevpc; 1181a8e07101SRui Paulo 1182a8e07101SRui Paulo for (pc = pcaps_to_close, prevpc = NULL; pc != NULL; 1183a8e07101SRui Paulo prevpc = pc, pc = pc->md.next) { 1184a8e07101SRui Paulo if (pc == p) { 1185a8e07101SRui Paulo /* 1186a8e07101SRui Paulo * Found it. Remove it from the list. 1187a8e07101SRui Paulo */ 1188a8e07101SRui Paulo if (prevpc == NULL) { 1189a8e07101SRui Paulo /* 1190a8e07101SRui Paulo * It was at the head of the list. 1191a8e07101SRui Paulo */ 1192a8e07101SRui Paulo pcaps_to_close = pc->md.next; 1193a8e07101SRui Paulo } else { 1194a8e07101SRui Paulo /* 1195a8e07101SRui Paulo * It was in the middle of the list. 1196a8e07101SRui Paulo */ 1197a8e07101SRui Paulo prevpc->md.next = pc->md.next; 1198a8e07101SRui Paulo } 1199a8e07101SRui Paulo break; 1200a8e07101SRui Paulo } 1201a8e07101SRui Paulo } 1202a8e07101SRui Paulo } 1203a8e07101SRui Paulo 1204a8e07101SRui Paulo void 1205a8e07101SRui Paulo pcap_cleanup_live_common(pcap_t *p) 1206a8e07101SRui Paulo { 1207a8e07101SRui Paulo if (p->buffer != NULL) { 120804fb2745SSam Leffler free(p->buffer); 1209a8e07101SRui Paulo p->buffer = NULL; 1210a8e07101SRui Paulo } 1211a8e07101SRui Paulo if (p->dlt_list != NULL) { 1212a8e07101SRui Paulo free(p->dlt_list); 1213a8e07101SRui Paulo p->dlt_list = NULL; 1214a8e07101SRui Paulo p->dlt_count = 0; 1215a8e07101SRui Paulo } 1216a8e07101SRui Paulo pcap_freecode(&p->fcode); 121704fb2745SSam Leffler #if !defined(WIN32) && !defined(MSDOS) 1218a8e07101SRui Paulo if (p->fd >= 0) { 121904fb2745SSam Leffler close(p->fd); 1220a8e07101SRui Paulo p->fd = -1; 1221a8e07101SRui Paulo } 1222*a0ee43a1SRui Paulo p->selectable_fd = -1; 1223*a0ee43a1SRui Paulo p->send_fd = -1; 122404fb2745SSam Leffler #endif 122504fb2745SSam Leffler } 122604fb2745SSam Leffler 1227feb4ecdbSBruce M Simpson static void 1228a8e07101SRui Paulo pcap_cleanup_dead(pcap_t *p _U_) 1229feb4ecdbSBruce M Simpson { 1230feb4ecdbSBruce M Simpson /* Nothing to do. */ 1231feb4ecdbSBruce M Simpson } 1232feb4ecdbSBruce M Simpson 1233dc2c7305SBill Fenner pcap_t * 1234dc2c7305SBill Fenner pcap_open_dead(int linktype, int snaplen) 1235dc2c7305SBill Fenner { 1236dc2c7305SBill Fenner pcap_t *p; 1237dc2c7305SBill Fenner 1238dc2c7305SBill Fenner p = malloc(sizeof(*p)); 1239dc2c7305SBill Fenner if (p == NULL) 1240dc2c7305SBill Fenner return NULL; 1241dc2c7305SBill Fenner memset (p, 0, sizeof(*p)); 1242dc2c7305SBill Fenner p->snapshot = snaplen; 1243dc2c7305SBill Fenner p->linktype = linktype; 1244feb4ecdbSBruce M Simpson p->stats_op = pcap_stats_dead; 1245a8e07101SRui Paulo #ifdef WIN32 1246a8e07101SRui Paulo p->setbuff_op = pcap_setbuff_dead; 1247a8e07101SRui Paulo p->setmode_op = pcap_setmode_dead; 1248a8e07101SRui Paulo p->setmintocopy_op = pcap_setmintocopy_dead; 1249a8e07101SRui Paulo #endif 1250a8e07101SRui Paulo p->cleanup_op = pcap_cleanup_dead; 1251a8e07101SRui Paulo p->activated = 1; 1252dc2c7305SBill Fenner return p; 1253dc2c7305SBill Fenner } 1254dc2c7305SBill Fenner 125504fb2745SSam Leffler /* 125604fb2745SSam Leffler * API compatible with WinPcap's "send a packet" routine - returns -1 125704fb2745SSam Leffler * on error, 0 otherwise. 125804fb2745SSam Leffler * 125904fb2745SSam Leffler * XXX - what if we get a short write? 126004fb2745SSam Leffler */ 126104fb2745SSam Leffler int 126204fb2745SSam Leffler pcap_sendpacket(pcap_t *p, const u_char *buf, int size) 126304fb2745SSam Leffler { 126404fb2745SSam Leffler if (p->inject_op(p, buf, size) == -1) 126504fb2745SSam Leffler return (-1); 126604fb2745SSam Leffler return (0); 126704fb2745SSam Leffler } 126804fb2745SSam Leffler 126904fb2745SSam Leffler /* 127004fb2745SSam Leffler * API compatible with OpenBSD's "send a packet" routine - returns -1 on 127104fb2745SSam Leffler * error, number of bytes written otherwise. 127204fb2745SSam Leffler */ 127304fb2745SSam Leffler int 127404fb2745SSam Leffler pcap_inject(pcap_t *p, const void *buf, size_t size) 127504fb2745SSam Leffler { 127604fb2745SSam Leffler return (p->inject_op(p, buf, size)); 127704fb2745SSam Leffler } 127804fb2745SSam Leffler 12798cf6c252SPaul Traina void 12808cf6c252SPaul Traina pcap_close(pcap_t *p) 12818cf6c252SPaul Traina { 1282a8e07101SRui Paulo if (p->opt.source != NULL) 1283a8e07101SRui Paulo free(p->opt.source); 1284a8e07101SRui Paulo p->cleanup_op(p); 12858cf6c252SPaul Traina free(p); 12868cf6c252SPaul Traina } 1287feb4ecdbSBruce M Simpson 1288feb4ecdbSBruce M Simpson /* 1289a8e07101SRui Paulo * Given a BPF program, a pcap_pkthdr structure for a packet, and the raw 1290a8e07101SRui Paulo * data for the packet, check whether the packet passes the filter. 1291a8e07101SRui Paulo * Returns the return value of the filter program, which will be zero if 1292a8e07101SRui Paulo * the packet doesn't pass and non-zero if the packet does pass. 1293a8e07101SRui Paulo */ 1294a8e07101SRui Paulo int 1295a8e07101SRui Paulo pcap_offline_filter(struct bpf_program *fp, const struct pcap_pkthdr *h, 1296a8e07101SRui Paulo const u_char *pkt) 1297a8e07101SRui Paulo { 1298a8e07101SRui Paulo struct bpf_insn *fcode = fp->bf_insns; 1299a8e07101SRui Paulo 1300a8e07101SRui Paulo if (fcode != NULL) 1301a8e07101SRui Paulo return (bpf_filter(fcode, pkt, h->len, h->caplen)); 1302a8e07101SRui Paulo else 1303a8e07101SRui Paulo return (0); 1304a8e07101SRui Paulo } 1305a8e07101SRui Paulo 1306a8e07101SRui Paulo /* 1307feb4ecdbSBruce M Simpson * We make the version string static, and return a pointer to it, rather 1308feb4ecdbSBruce M Simpson * than exporting the version string directly. On at least some UNIXes, 1309feb4ecdbSBruce M Simpson * if you import data from a shared library into an program, the data is 1310feb4ecdbSBruce M Simpson * bound into the program binary, so if the string in the version of the 1311feb4ecdbSBruce M Simpson * library with which the program was linked isn't the same as the 1312feb4ecdbSBruce M Simpson * string in the version of the library with which the program is being 1313feb4ecdbSBruce M Simpson * run, various undesirable things may happen (warnings, the string 1314feb4ecdbSBruce M Simpson * being the one from the version of the library with which the program 1315feb4ecdbSBruce M Simpson * was linked, or even weirder things, such as the string being the one 1316feb4ecdbSBruce M Simpson * from the library but being truncated). 1317feb4ecdbSBruce M Simpson */ 131804fb2745SSam Leffler #ifdef HAVE_VERSION_H 131904fb2745SSam Leffler #include "version.h" 132004fb2745SSam Leffler #else 1321*a0ee43a1SRui Paulo static const char pcap_version_string[] = "libpcap version 1.x.y"; 132204fb2745SSam Leffler #endif 132304fb2745SSam Leffler 1324feb4ecdbSBruce M Simpson #ifdef WIN32 1325feb4ecdbSBruce M Simpson /* 1326feb4ecdbSBruce M Simpson * XXX - it'd be nice if we could somehow generate the WinPcap and libpcap 1327feb4ecdbSBruce M Simpson * version numbers when building WinPcap. (It'd be nice to do so for 1328feb4ecdbSBruce M Simpson * the packet.dll version number as well.) 1329feb4ecdbSBruce M Simpson */ 1330ef96d74fSMax Laier static const char wpcap_version_string[] = "4.0"; 1331feb4ecdbSBruce M Simpson static const char pcap_version_string_fmt[] = 133204fb2745SSam Leffler "WinPcap version %s, based on %s"; 1333feb4ecdbSBruce M Simpson static const char pcap_version_string_packet_dll_fmt[] = 133404fb2745SSam Leffler "WinPcap version %s (packet.dll version %s), based on %s"; 133504fb2745SSam Leffler static char *full_pcap_version_string; 1336feb4ecdbSBruce M Simpson 1337feb4ecdbSBruce M Simpson const char * 1338feb4ecdbSBruce M Simpson pcap_lib_version(void) 1339feb4ecdbSBruce M Simpson { 1340feb4ecdbSBruce M Simpson char *packet_version_string; 134104fb2745SSam Leffler size_t full_pcap_version_string_len; 1342feb4ecdbSBruce M Simpson 134304fb2745SSam Leffler if (full_pcap_version_string == NULL) { 1344feb4ecdbSBruce M Simpson /* 1345feb4ecdbSBruce M Simpson * Generate the version string. 1346feb4ecdbSBruce M Simpson */ 1347feb4ecdbSBruce M Simpson packet_version_string = PacketGetVersion(); 1348feb4ecdbSBruce M Simpson if (strcmp(wpcap_version_string, packet_version_string) == 0) { 1349feb4ecdbSBruce M Simpson /* 1350feb4ecdbSBruce M Simpson * WinPcap version string and packet.dll version 1351feb4ecdbSBruce M Simpson * string are the same; just report the WinPcap 1352feb4ecdbSBruce M Simpson * version. 1353feb4ecdbSBruce M Simpson */ 135404fb2745SSam Leffler full_pcap_version_string_len = 135504fb2745SSam Leffler (sizeof pcap_version_string_fmt - 4) + 135604fb2745SSam Leffler strlen(wpcap_version_string) + 135704fb2745SSam Leffler strlen(pcap_version_string); 135804fb2745SSam Leffler full_pcap_version_string = 135904fb2745SSam Leffler malloc(full_pcap_version_string_len); 136004fb2745SSam Leffler sprintf(full_pcap_version_string, 136104fb2745SSam Leffler pcap_version_string_fmt, wpcap_version_string, 136204fb2745SSam Leffler pcap_version_string); 1363feb4ecdbSBruce M Simpson } else { 1364feb4ecdbSBruce M Simpson /* 1365feb4ecdbSBruce M Simpson * WinPcap version string and packet.dll version 1366feb4ecdbSBruce M Simpson * string are different; that shouldn't be the 1367feb4ecdbSBruce M Simpson * case (the two libraries should come from the 1368feb4ecdbSBruce M Simpson * same version of WinPcap), so we report both 1369feb4ecdbSBruce M Simpson * versions. 1370feb4ecdbSBruce M Simpson */ 137104fb2745SSam Leffler full_pcap_version_string_len = 137204fb2745SSam Leffler (sizeof pcap_version_string_packet_dll_fmt - 6) + 1373feb4ecdbSBruce M Simpson strlen(wpcap_version_string) + 137404fb2745SSam Leffler strlen(packet_version_string) + 137504fb2745SSam Leffler strlen(pcap_version_string); 137604fb2745SSam Leffler full_pcap_version_string = malloc(full_pcap_version_string_len); 137704fb2745SSam Leffler 137804fb2745SSam Leffler sprintf(full_pcap_version_string, 1379feb4ecdbSBruce M Simpson pcap_version_string_packet_dll_fmt, 138004fb2745SSam Leffler wpcap_version_string, packet_version_string, 138104fb2745SSam Leffler pcap_version_string); 1382feb4ecdbSBruce M Simpson } 1383feb4ecdbSBruce M Simpson } 138404fb2745SSam Leffler return (full_pcap_version_string); 1385feb4ecdbSBruce M Simpson } 138604fb2745SSam Leffler 138704fb2745SSam Leffler #elif defined(MSDOS) 138804fb2745SSam Leffler 138904fb2745SSam Leffler static char *full_pcap_version_string; 139004fb2745SSam Leffler 139104fb2745SSam Leffler const char * 139204fb2745SSam Leffler pcap_lib_version (void) 139304fb2745SSam Leffler { 139404fb2745SSam Leffler char *packet_version_string; 139504fb2745SSam Leffler size_t full_pcap_version_string_len; 139604fb2745SSam Leffler static char dospfx[] = "DOS-"; 139704fb2745SSam Leffler 139804fb2745SSam Leffler if (full_pcap_version_string == NULL) { 139904fb2745SSam Leffler /* 140004fb2745SSam Leffler * Generate the version string. 140104fb2745SSam Leffler */ 140204fb2745SSam Leffler full_pcap_version_string_len = 140304fb2745SSam Leffler sizeof dospfx + strlen(pcap_version_string); 140404fb2745SSam Leffler full_pcap_version_string = 140504fb2745SSam Leffler malloc(full_pcap_version_string_len); 140604fb2745SSam Leffler strcpy(full_pcap_version_string, dospfx); 140704fb2745SSam Leffler strcat(full_pcap_version_string, pcap_version_string); 140804fb2745SSam Leffler } 140904fb2745SSam Leffler return (full_pcap_version_string); 141004fb2745SSam Leffler } 141104fb2745SSam Leffler 141204fb2745SSam Leffler #else /* UN*X */ 1413feb4ecdbSBruce M Simpson 1414feb4ecdbSBruce M Simpson const char * 1415feb4ecdbSBruce M Simpson pcap_lib_version(void) 1416feb4ecdbSBruce M Simpson { 1417feb4ecdbSBruce M Simpson return (pcap_version_string); 1418feb4ecdbSBruce M Simpson } 1419feb4ecdbSBruce M Simpson #endif 1420