pcap.c revision 127664
117683Spst/* 239291Sfenner * Copyright (c) 1993, 1994, 1995, 1996, 1997, 1998 317683Spst * The Regents of the University of California. All rights reserved. 417683Spst * 517683Spst * Redistribution and use in source and binary forms, with or without 617683Spst * modification, are permitted provided that the following conditions 717683Spst * are met: 817683Spst * 1. Redistributions of source code must retain the above copyright 917683Spst * notice, this list of conditions and the following disclaimer. 1017683Spst * 2. Redistributions in binary form must reproduce the above copyright 1117683Spst * notice, this list of conditions and the following disclaimer in the 1217683Spst * documentation and/or other materials provided with the distribution. 1317683Spst * 3. All advertising materials mentioning features or use of this software 1417683Spst * must display the following acknowledgement: 1517683Spst * This product includes software developed by the Computer Systems 1617683Spst * Engineering Group at Lawrence Berkeley Laboratory. 1717683Spst * 4. Neither the name of the University nor of the Laboratory may be used 1817683Spst * to endorse or promote products derived from this software without 1917683Spst * specific prior written permission. 2017683Spst * 2117683Spst * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2217683Spst * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2317683Spst * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2417683Spst * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2517683Spst * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2617683Spst * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2717683Spst * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2817683Spst * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2917683Spst * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3017683Spst * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3117683Spst * SUCH DAMAGE. 3217683Spst */ 3317683Spst 3417683Spst#ifndef lint 35127664Sbmsstatic const char rcsid[] _U_ = 36127664Sbms "@(#) $Header: /tcpdump/master/libpcap/pcap.c,v 1.63.2.9 2004/03/25 22:40:52 guy Exp $ (LBL)"; 3717683Spst#endif 3817683Spst 3975107Sfenner#ifdef HAVE_CONFIG_H 4075107Sfenner#include "config.h" 4175107Sfenner#endif 4275107Sfenner 43127664Sbms#ifdef WIN32 44127664Sbms#include <pcap-stdinc.h> 45127664Sbms#else /* WIN32 */ 4617683Spst#include <sys/types.h> 47127664Sbms#endif /* WIN32 */ 4817683Spst 4917683Spst#include <stdio.h> 5017683Spst#include <stdlib.h> 5117683Spst#include <string.h> 52127664Sbms#ifndef _MSC_VER 5317683Spst#include <unistd.h> 54127664Sbms#endif 5598530Sfenner#include <fcntl.h> 5698530Sfenner#include <errno.h> 5717683Spst 5817683Spst#ifdef HAVE_OS_PROTO_H 5917683Spst#include "os-proto.h" 6017683Spst#endif 6117683Spst 6217683Spst#include "pcap-int.h" 6317683Spst 64127664Sbms#ifdef HAVE_DAG_API 65127664Sbms#include <dagnew.h> 66127664Sbms#include <dagapi.h> 67127664Sbms#endif 68127664Sbms 6917683Spstint 7017683Spstpcap_dispatch(pcap_t *p, int cnt, pcap_handler callback, u_char *user) 7117683Spst{ 7217683Spst 73127664Sbms return p->read_op(p, cnt, callback, user); 7417683Spst} 7517683Spst 76127664Sbms/* 77127664Sbms * XXX - is this necessary? 78127664Sbms */ 7917683Spstint 80127664Sbmspcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) 81127664Sbms{ 82127664Sbms 83127664Sbms return p->read_op(p, cnt, callback, user); 84127664Sbms} 85127664Sbms 86127664Sbmsint 8717683Spstpcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user) 8817683Spst{ 8939291Sfenner register int n; 9039291Sfenner 9117683Spst for (;;) { 92127664Sbms if (p->sf.rfile != NULL) { 93127664Sbms /* 94127664Sbms * 0 means EOF, so don't loop if we get 0. 95127664Sbms */ 9639291Sfenner n = pcap_offline_read(p, cnt, callback, user); 97127664Sbms } else { 9839291Sfenner /* 9939291Sfenner * XXX keep reading until we get something 10039291Sfenner * (or an error occurs) 10139291Sfenner */ 10239291Sfenner do { 103127664Sbms n = p->read_op(p, cnt, callback, user); 10439291Sfenner } while (n == 0); 10539291Sfenner } 10617683Spst if (n <= 0) 10717683Spst return (n); 10817683Spst if (cnt > 0) { 10917683Spst cnt -= n; 11017683Spst if (cnt <= 0) 11117683Spst return (0); 11217683Spst } 11317683Spst } 11417683Spst} 11517683Spst 11617683Spststruct singleton { 11717683Spst struct pcap_pkthdr *hdr; 11817683Spst const u_char *pkt; 11917683Spst}; 12017683Spst 12117683Spst 12217683Spststatic void 12317683Spstpcap_oneshot(u_char *userData, const struct pcap_pkthdr *h, const u_char *pkt) 12417683Spst{ 12517683Spst struct singleton *sp = (struct singleton *)userData; 12617683Spst *sp->hdr = *h; 12717683Spst sp->pkt = pkt; 12817683Spst} 12917683Spst 13017683Spstconst u_char * 13117683Spstpcap_next(pcap_t *p, struct pcap_pkthdr *h) 13217683Spst{ 13317683Spst struct singleton s; 13417683Spst 13517683Spst s.hdr = h; 13617683Spst if (pcap_dispatch(p, 1, pcap_oneshot, (u_char*)&s) <= 0) 13717683Spst return (0); 13817683Spst return (s.pkt); 13917683Spst} 14017683Spst 141127664Sbmsstruct pkt_for_fakecallback { 142127664Sbms struct pcap_pkthdr *hdr; 143127664Sbms const u_char **pkt; 144127664Sbms}; 145127664Sbms 146127664Sbmsstatic void 147127664Sbmspcap_fakecallback(u_char *userData, const struct pcap_pkthdr *h, 148127664Sbms const u_char *pkt) 149127664Sbms{ 150127664Sbms struct pkt_for_fakecallback *sp = (struct pkt_for_fakecallback *)userData; 151127664Sbms 152127664Sbms *sp->hdr = *h; 153127664Sbms *sp->pkt = pkt; 154127664Sbms} 155127664Sbms 156127664Sbmsint 157127664Sbmspcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header, 158127664Sbms const u_char **pkt_data) 159127664Sbms{ 160127664Sbms struct pkt_for_fakecallback s; 161127664Sbms 162127664Sbms s.hdr = &p->pcap_header; 163127664Sbms s.pkt = pkt_data; 164127664Sbms 165127664Sbms /* Saves a pointer to the packet headers */ 166127664Sbms *pkt_header= &p->pcap_header; 167127664Sbms 168127664Sbms if (p->sf.rfile != NULL) { 169127664Sbms int status; 170127664Sbms 171127664Sbms /* We are on an offline capture */ 172127664Sbms status = pcap_offline_read(p, 1, pcap_fakecallback, 173127664Sbms (u_char *)&s); 174127664Sbms 175127664Sbms /* 176127664Sbms * Return codes for pcap_offline_read() are: 177127664Sbms * - 0: EOF 178127664Sbms * - -1: error 179127664Sbms * - >1: OK 180127664Sbms * The first one ('0') conflicts with the return code of 181127664Sbms * 0 from pcap_read() meaning "no packets arrived before 182127664Sbms * the timeout expired", so we map it to -2 so you can 183127664Sbms * distinguish between an EOF from a savefile and a 184127664Sbms * "no packets arrived before the timeout expired, try 185127664Sbms * again" from a live capture. 186127664Sbms */ 187127664Sbms if (status == 0) 188127664Sbms return (-2); 189127664Sbms else 190127664Sbms return (status); 191127664Sbms } 192127664Sbms 193127664Sbms /* 194127664Sbms * Return codes for pcap_read() are: 195127664Sbms * - 0: timeout 196127664Sbms * - -1: error 197127664Sbms * - -2: loop was broken out of with pcap_breakloop() 198127664Sbms * - >1: OK 199127664Sbms * The first one ('0') conflicts with the return code of 0 from 200127664Sbms * pcap_offline_read() meaning "end of file". 201127664Sbms */ 202127664Sbms return (p->read_op(p, 1, pcap_fakecallback, (u_char *)&s)); 203127664Sbms} 204127664Sbms 205127664Sbms/* 206127664Sbms * Force the loop in "pcap_read()" or "pcap_read_offline()" to terminate. 207127664Sbms */ 208127664Sbmsvoid 209127664Sbmspcap_breakloop(pcap_t *p) 210127664Sbms{ 211127664Sbms p->break_loop = 1; 212127664Sbms} 213127664Sbms 21417683Spstint 21517683Spstpcap_datalink(pcap_t *p) 21617683Spst{ 21717683Spst return (p->linktype); 21817683Spst} 21917683Spst 22017683Spstint 221109839Sfennerpcap_list_datalinks(pcap_t *p, int **dlt_buffer) 222109839Sfenner{ 223127664Sbms if (p->dlt_count == 0) { 224127664Sbms /* 225127664Sbms * We couldn't fetch the list of DLTs, which means 226127664Sbms * this platform doesn't support changing the 227127664Sbms * DLT for an interface. Return a list of DLTs 228127664Sbms * containing only the DLT this device supports. 229127664Sbms */ 230127664Sbms *dlt_buffer = (int*)malloc(sizeof(**dlt_buffer)); 231127664Sbms if (*dlt_buffer == NULL) { 232127664Sbms (void)snprintf(p->errbuf, sizeof(p->errbuf), 233127664Sbms "malloc: %s", pcap_strerror(errno)); 234127664Sbms return (-1); 235127664Sbms } 236127664Sbms **dlt_buffer = p->linktype; 237127664Sbms return (1); 238127664Sbms } else { 239127664Sbms *dlt_buffer = (int*)malloc(sizeof(**dlt_buffer) * p->dlt_count); 240127664Sbms if (*dlt_buffer == NULL) { 241127664Sbms (void)snprintf(p->errbuf, sizeof(p->errbuf), 242127664Sbms "malloc: %s", pcap_strerror(errno)); 243127664Sbms return (-1); 244127664Sbms } 245127664Sbms (void)memcpy(*dlt_buffer, p->dlt_list, 246127664Sbms sizeof(**dlt_buffer) * p->dlt_count); 247127664Sbms return (p->dlt_count); 248109839Sfenner } 249127664Sbms} 250127664Sbms 251127664Sbmsint 252127664Sbmspcap_set_datalink(pcap_t *p, int dlt) 253127664Sbms{ 254127664Sbms int i; 255127664Sbms const char *dlt_name; 256127664Sbms 257127664Sbms if (p->dlt_count == 0 || p->set_datalink_op == NULL) { 258127664Sbms /* 259127664Sbms * We couldn't fetch the list of DLTs, or we don't 260127664Sbms * have a "set datalink" operation, which means 261127664Sbms * this platform doesn't support changing the 262127664Sbms * DLT for an interface. Check whether the new 263127664Sbms * DLT is the one this interface supports. 264127664Sbms */ 265127664Sbms if (p->linktype != dlt) 266127664Sbms goto unsupported; 267127664Sbms 268127664Sbms /* 269127664Sbms * It is, so there's nothing we need to do here. 270127664Sbms */ 271127664Sbms return (0); 272109839Sfenner } 273127664Sbms for (i = 0; i < p->dlt_count; i++) 274127664Sbms if (p->dlt_list[i] == dlt) 275127664Sbms break; 276127664Sbms if (i >= p->dlt_count) 277127664Sbms goto unsupported; 278127664Sbms if (p->set_datalink_op(p, dlt) == -1) 279127664Sbms return (-1); 280127664Sbms p->linktype = dlt; 281127664Sbms return (0); 282127664Sbms 283127664Sbmsunsupported: 284127664Sbms dlt_name = pcap_datalink_val_to_name(dlt); 285127664Sbms if (dlt_name != NULL) { 286127664Sbms (void) snprintf(p->errbuf, sizeof(p->errbuf), 287127664Sbms "%s is not one of the DLTs supported by this device", 288127664Sbms dlt_name); 289127664Sbms } else { 290127664Sbms (void) snprintf(p->errbuf, sizeof(p->errbuf), 291127664Sbms "DLT %d is not one of the DLTs supported by this device", 292127664Sbms dlt); 293127664Sbms } 294127664Sbms return (-1); 295109839Sfenner} 296109839Sfenner 297127664Sbmsstruct dlt_choice { 298127664Sbms const char *name; 299127664Sbms const char *description; 300127664Sbms int dlt; 301127664Sbms}; 302127664Sbms 303127664Sbms#define DLT_CHOICE(code, description) { #code, description, code } 304127664Sbms#define DLT_CHOICE_SENTINEL { NULL, NULL, 0 } 305127664Sbms 306127664Sbmsstatic struct dlt_choice dlt_choices[] = { 307127664Sbms DLT_CHOICE(DLT_NULL, "BSD loopback"), 308127664Sbms DLT_CHOICE(DLT_EN10MB, "Ethernet"), 309127664Sbms DLT_CHOICE(DLT_IEEE802, "Token ring"), 310127664Sbms DLT_CHOICE(DLT_ARCNET, "ARCNET"), 311127664Sbms DLT_CHOICE(DLT_SLIP, "SLIP"), 312127664Sbms DLT_CHOICE(DLT_PPP, "PPP"), 313127664Sbms DLT_CHOICE(DLT_FDDI, "FDDI"), 314127664Sbms DLT_CHOICE(DLT_ATM_RFC1483, "RFC 1483 IP-over-ATM"), 315127664Sbms DLT_CHOICE(DLT_RAW, "Raw IP"), 316127664Sbms DLT_CHOICE(DLT_SLIP_BSDOS, "BSD/OS SLIP"), 317127664Sbms DLT_CHOICE(DLT_PPP_BSDOS, "BSD/OS PPP"), 318127664Sbms DLT_CHOICE(DLT_ATM_CLIP, "Linux Classical IP-over-ATM"), 319127664Sbms DLT_CHOICE(DLT_PPP_SERIAL, "PPP over serial"), 320127664Sbms DLT_CHOICE(DLT_PPP_ETHER, "PPPoE"), 321127664Sbms DLT_CHOICE(DLT_C_HDLC, "Cisco HDLC"), 322127664Sbms DLT_CHOICE(DLT_IEEE802_11, "802.11"), 323127664Sbms DLT_CHOICE(DLT_FRELAY, "Frame Relay"), 324127664Sbms DLT_CHOICE(DLT_LOOP, "OpenBSD loopback"), 325127664Sbms DLT_CHOICE(DLT_ENC, "OpenBSD encapsulated IP"), 326127664Sbms DLT_CHOICE(DLT_LINUX_SLL, "Linux cooked"), 327127664Sbms DLT_CHOICE(DLT_LTALK, "Localtalk"), 328127664Sbms DLT_CHOICE(DLT_PFLOG, "OpenBSD pflog file"), 329127664Sbms DLT_CHOICE(DLT_PRISM_HEADER, "802.11 plus Prism header"), 330127664Sbms DLT_CHOICE(DLT_IP_OVER_FC, "RFC 2625 IP-over-Fibre Channel"), 331127664Sbms DLT_CHOICE(DLT_SUNATM, "Sun raw ATM"), 332127664Sbms DLT_CHOICE(DLT_IEEE802_11_RADIO, "802.11 plus BSD radio information header"), 333127664Sbms DLT_CHOICE(DLT_APPLE_IP_OVER_IEEE1394, "Apple IP-over-IEEE 1394"), 334127664Sbms DLT_CHOICE(DLT_ARCNET_LINUX, "Linux ARCNET"), 335127664Sbms DLT_CHOICE(DLT_LINUX_IRDA, "Linux IrDA"), 336127664Sbms DLT_CHOICE(DLT_IEEE802_11_RADIO_AVS, "802.11 plus AVS radio information header"), 337127664Sbms DLT_CHOICE_SENTINEL 338127664Sbms}; 339127664Sbms 340127664Sbms/* 341127664Sbms * This array is designed for mapping upper and lower case letter 342127664Sbms * together for a case independent comparison. The mappings are 343127664Sbms * based upon ascii character sequences. 344127664Sbms */ 345127664Sbmsstatic const u_char charmap[] = { 346127664Sbms (u_char)'\000', (u_char)'\001', (u_char)'\002', (u_char)'\003', 347127664Sbms (u_char)'\004', (u_char)'\005', (u_char)'\006', (u_char)'\007', 348127664Sbms (u_char)'\010', (u_char)'\011', (u_char)'\012', (u_char)'\013', 349127664Sbms (u_char)'\014', (u_char)'\015', (u_char)'\016', (u_char)'\017', 350127664Sbms (u_char)'\020', (u_char)'\021', (u_char)'\022', (u_char)'\023', 351127664Sbms (u_char)'\024', (u_char)'\025', (u_char)'\026', (u_char)'\027', 352127664Sbms (u_char)'\030', (u_char)'\031', (u_char)'\032', (u_char)'\033', 353127664Sbms (u_char)'\034', (u_char)'\035', (u_char)'\036', (u_char)'\037', 354127664Sbms (u_char)'\040', (u_char)'\041', (u_char)'\042', (u_char)'\043', 355127664Sbms (u_char)'\044', (u_char)'\045', (u_char)'\046', (u_char)'\047', 356127664Sbms (u_char)'\050', (u_char)'\051', (u_char)'\052', (u_char)'\053', 357127664Sbms (u_char)'\054', (u_char)'\055', (u_char)'\056', (u_char)'\057', 358127664Sbms (u_char)'\060', (u_char)'\061', (u_char)'\062', (u_char)'\063', 359127664Sbms (u_char)'\064', (u_char)'\065', (u_char)'\066', (u_char)'\067', 360127664Sbms (u_char)'\070', (u_char)'\071', (u_char)'\072', (u_char)'\073', 361127664Sbms (u_char)'\074', (u_char)'\075', (u_char)'\076', (u_char)'\077', 362127664Sbms (u_char)'\100', (u_char)'\141', (u_char)'\142', (u_char)'\143', 363127664Sbms (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147', 364127664Sbms (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153', 365127664Sbms (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157', 366127664Sbms (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163', 367127664Sbms (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167', 368127664Sbms (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\133', 369127664Sbms (u_char)'\134', (u_char)'\135', (u_char)'\136', (u_char)'\137', 370127664Sbms (u_char)'\140', (u_char)'\141', (u_char)'\142', (u_char)'\143', 371127664Sbms (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147', 372127664Sbms (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153', 373127664Sbms (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157', 374127664Sbms (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163', 375127664Sbms (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167', 376127664Sbms (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\173', 377127664Sbms (u_char)'\174', (u_char)'\175', (u_char)'\176', (u_char)'\177', 378127664Sbms (u_char)'\200', (u_char)'\201', (u_char)'\202', (u_char)'\203', 379127664Sbms (u_char)'\204', (u_char)'\205', (u_char)'\206', (u_char)'\207', 380127664Sbms (u_char)'\210', (u_char)'\211', (u_char)'\212', (u_char)'\213', 381127664Sbms (u_char)'\214', (u_char)'\215', (u_char)'\216', (u_char)'\217', 382127664Sbms (u_char)'\220', (u_char)'\221', (u_char)'\222', (u_char)'\223', 383127664Sbms (u_char)'\224', (u_char)'\225', (u_char)'\226', (u_char)'\227', 384127664Sbms (u_char)'\230', (u_char)'\231', (u_char)'\232', (u_char)'\233', 385127664Sbms (u_char)'\234', (u_char)'\235', (u_char)'\236', (u_char)'\237', 386127664Sbms (u_char)'\240', (u_char)'\241', (u_char)'\242', (u_char)'\243', 387127664Sbms (u_char)'\244', (u_char)'\245', (u_char)'\246', (u_char)'\247', 388127664Sbms (u_char)'\250', (u_char)'\251', (u_char)'\252', (u_char)'\253', 389127664Sbms (u_char)'\254', (u_char)'\255', (u_char)'\256', (u_char)'\257', 390127664Sbms (u_char)'\260', (u_char)'\261', (u_char)'\262', (u_char)'\263', 391127664Sbms (u_char)'\264', (u_char)'\265', (u_char)'\266', (u_char)'\267', 392127664Sbms (u_char)'\270', (u_char)'\271', (u_char)'\272', (u_char)'\273', 393127664Sbms (u_char)'\274', (u_char)'\275', (u_char)'\276', (u_char)'\277', 394127664Sbms (u_char)'\300', (u_char)'\341', (u_char)'\342', (u_char)'\343', 395127664Sbms (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347', 396127664Sbms (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353', 397127664Sbms (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357', 398127664Sbms (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363', 399127664Sbms (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367', 400127664Sbms (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\333', 401127664Sbms (u_char)'\334', (u_char)'\335', (u_char)'\336', (u_char)'\337', 402127664Sbms (u_char)'\340', (u_char)'\341', (u_char)'\342', (u_char)'\343', 403127664Sbms (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347', 404127664Sbms (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353', 405127664Sbms (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357', 406127664Sbms (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363', 407127664Sbms (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367', 408127664Sbms (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\373', 409127664Sbms (u_char)'\374', (u_char)'\375', (u_char)'\376', (u_char)'\377', 410127664Sbms}; 411127664Sbms 412109839Sfennerint 413127664Sbmspcap_strcasecmp(const char *s1, const char *s2) 414127664Sbms{ 415127664Sbms register const u_char *cm = charmap, 416127664Sbms *us1 = (u_char *)s1, 417127664Sbms *us2 = (u_char *)s2; 418127664Sbms 419127664Sbms while (cm[*us1] == cm[*us2++]) 420127664Sbms if (*us1++ == '\0') 421127664Sbms return(0); 422127664Sbms return (cm[*us1] - cm[*--us2]); 423127664Sbms} 424127664Sbms 425127664Sbmsint 426127664Sbmspcap_datalink_name_to_val(const char *name) 427127664Sbms{ 428127664Sbms int i; 429127664Sbms 430127664Sbms for (i = 0; dlt_choices[i].name != NULL; i++) { 431127664Sbms if (pcap_strcasecmp(dlt_choices[i].name + sizeof("DLT_") - 1, 432127664Sbms name) == 0) 433127664Sbms return (dlt_choices[i].dlt); 434127664Sbms } 435127664Sbms return (-1); 436127664Sbms} 437127664Sbms 438127664Sbmsconst char * 439127664Sbmspcap_datalink_val_to_name(int dlt) 440127664Sbms{ 441127664Sbms int i; 442127664Sbms 443127664Sbms for (i = 0; dlt_choices[i].name != NULL; i++) { 444127664Sbms if (dlt_choices[i].dlt == dlt) 445127664Sbms return (dlt_choices[i].name + sizeof("DLT_") - 1); 446127664Sbms } 447127664Sbms return (NULL); 448127664Sbms} 449127664Sbms 450127664Sbmsconst char * 451127664Sbmspcap_datalink_val_to_description(int dlt) 452127664Sbms{ 453127664Sbms int i; 454127664Sbms 455127664Sbms for (i = 0; dlt_choices[i].name != NULL; i++) { 456127664Sbms if (dlt_choices[i].dlt == dlt) 457127664Sbms return (dlt_choices[i].description); 458127664Sbms } 459127664Sbms return (NULL); 460127664Sbms} 461127664Sbms 462127664Sbmsint 46317683Spstpcap_snapshot(pcap_t *p) 46417683Spst{ 46517683Spst return (p->snapshot); 46617683Spst} 46717683Spst 46817683Spstint 46917683Spstpcap_is_swapped(pcap_t *p) 47017683Spst{ 47117683Spst return (p->sf.swapped); 47217683Spst} 47317683Spst 47417683Spstint 47517683Spstpcap_major_version(pcap_t *p) 47617683Spst{ 47717683Spst return (p->sf.version_major); 47817683Spst} 47917683Spst 48017683Spstint 48117683Spstpcap_minor_version(pcap_t *p) 48217683Spst{ 48317683Spst return (p->sf.version_minor); 48417683Spst} 48517683Spst 48617683SpstFILE * 48717683Spstpcap_file(pcap_t *p) 48817683Spst{ 48917683Spst return (p->sf.rfile); 49017683Spst} 49117683Spst 49217683Spstint 49317683Spstpcap_fileno(pcap_t *p) 49417683Spst{ 495127664Sbms#ifndef WIN32 49617683Spst return (p->fd); 497127664Sbms#else 498127664Sbms if (p->adapter != NULL) 499127664Sbms return ((int)(DWORD)p->adapter->hFile); 500127664Sbms else 501127664Sbms return (-1); 502127664Sbms#endif 50317683Spst} 50417683Spst 505127664Sbms#ifndef WIN32 506127664Sbmsint 507127664Sbmspcap_get_selectable_fd(pcap_t *p) 508127664Sbms{ 509127664Sbms return (p->selectable_fd); 510127664Sbms} 511127664Sbms#endif 512127664Sbms 51317683Spstvoid 51417683Spstpcap_perror(pcap_t *p, char *prefix) 51517683Spst{ 51617683Spst fprintf(stderr, "%s: %s\n", prefix, p->errbuf); 51717683Spst} 51817683Spst 51917683Spstchar * 52017683Spstpcap_geterr(pcap_t *p) 52117683Spst{ 52217683Spst return (p->errbuf); 52317683Spst} 52417683Spst 525127664Sbmsint 526127664Sbmspcap_getnonblock(pcap_t *p, char *errbuf) 527127664Sbms{ 528127664Sbms return p->getnonblock_op(p, errbuf); 529127664Sbms} 530127664Sbms 53117683Spst/* 532127664Sbms * Get the current non-blocking mode setting, under the assumption that 533127664Sbms * it's just the standard POSIX non-blocking flag. 534127664Sbms * 535127664Sbms * We don't look at "p->nonblock", in case somebody tweaked the FD 536127664Sbms * directly. 53798530Sfenner */ 538127664Sbms#ifndef WIN32 53998530Sfennerint 540127664Sbmspcap_getnonblock_fd(pcap_t *p, char *errbuf) 54198530Sfenner{ 54298530Sfenner int fdflags; 54398530Sfenner 54498530Sfenner fdflags = fcntl(p->fd, F_GETFL, 0); 54598530Sfenner if (fdflags == -1) { 54698530Sfenner snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s", 54798530Sfenner pcap_strerror(errno)); 54898530Sfenner return (-1); 54998530Sfenner } 55098530Sfenner if (fdflags & O_NONBLOCK) 55198530Sfenner return (1); 55298530Sfenner else 55398530Sfenner return (0); 55498530Sfenner} 555127664Sbms#endif 55698530Sfenner 55798530Sfennerint 55898530Sfennerpcap_setnonblock(pcap_t *p, int nonblock, char *errbuf) 55998530Sfenner{ 560127664Sbms return p->setnonblock_op(p, nonblock, errbuf); 561127664Sbms} 562127664Sbms 563127664Sbms#ifndef WIN32 564127664Sbms/* 565127664Sbms * Set non-blocking mode, under the assumption that it's just the 566127664Sbms * standard POSIX non-blocking flag. (This can be called by the 567127664Sbms * per-platform non-blocking-mode routine if that routine also 568127664Sbms * needs to do some additional work.) 569127664Sbms */ 570127664Sbmsint 571127664Sbmspcap_setnonblock_fd(pcap_t *p, int nonblock, char *errbuf) 572127664Sbms{ 57398530Sfenner int fdflags; 57498530Sfenner 57598530Sfenner fdflags = fcntl(p->fd, F_GETFL, 0); 57698530Sfenner if (fdflags == -1) { 57798530Sfenner snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s", 57898530Sfenner pcap_strerror(errno)); 57998530Sfenner return (-1); 58098530Sfenner } 58198530Sfenner if (nonblock) 58298530Sfenner fdflags |= O_NONBLOCK; 58398530Sfenner else 58498530Sfenner fdflags &= ~O_NONBLOCK; 58598530Sfenner if (fcntl(p->fd, F_SETFL, fdflags) == -1) { 58698530Sfenner snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_SETFL: %s", 58798530Sfenner pcap_strerror(errno)); 58898530Sfenner return (-1); 58998530Sfenner } 59098530Sfenner return (0); 59198530Sfenner} 592127664Sbms#endif 59398530Sfenner 594127664Sbms#ifdef WIN32 59598530Sfenner/* 596127664Sbms * Generate a string for the last Win32-specific error (i.e. an error generated when 597127664Sbms * calling a Win32 API). 598127664Sbms * For errors occurred during standard C calls, we still use pcap_strerror() 599127664Sbms */ 600127664Sbmschar * 601127664Sbmspcap_win32strerror(void) 602127664Sbms{ 603127664Sbms DWORD error; 604127664Sbms static char errbuf[PCAP_ERRBUF_SIZE+1]; 605127664Sbms int errlen; 606127664Sbms 607127664Sbms error = GetLastError(); 608127664Sbms FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, errbuf, 609127664Sbms PCAP_ERRBUF_SIZE, NULL); 610127664Sbms 611127664Sbms /* 612127664Sbms * "FormatMessage()" "helpfully" sticks CR/LF at the end of the 613127664Sbms * message. Get rid of it. 614127664Sbms */ 615127664Sbms errlen = strlen(errbuf); 616127664Sbms if (errlen >= 2) { 617127664Sbms errbuf[errlen - 1] = '\0'; 618127664Sbms errbuf[errlen - 2] = '\0'; 619127664Sbms } 620127664Sbms return (errbuf); 621127664Sbms} 622127664Sbms#endif 623127664Sbms 624127664Sbms/* 62517683Spst * Not all systems have strerror(). 62617683Spst */ 62717683Spstchar * 62817683Spstpcap_strerror(int errnum) 62917683Spst{ 63017683Spst#ifdef HAVE_STRERROR 63117683Spst return (strerror(errnum)); 63217683Spst#else 63317683Spst extern int sys_nerr; 63417683Spst extern const char *const sys_errlist[]; 63517683Spst static char ebuf[20]; 63617683Spst 63717683Spst if ((unsigned int)errnum < sys_nerr) 63817683Spst return ((char *)sys_errlist[errnum]); 63975107Sfenner (void)snprintf(ebuf, sizeof ebuf, "Unknown error: %d", errnum); 64017683Spst return(ebuf); 64117683Spst#endif 64217683Spst} 64317683Spst 644127664Sbmsint 645127664Sbmspcap_setfilter(pcap_t *p, struct bpf_program *fp) 646127664Sbms{ 647127664Sbms return p->setfilter_op(p, fp); 648127664Sbms} 649127664Sbms 650127664Sbmsint 651127664Sbmspcap_stats(pcap_t *p, struct pcap_stat *ps) 652127664Sbms{ 653127664Sbms return p->stats_op(p, ps); 654127664Sbms} 655127664Sbms 656127664Sbmsstatic int 657127664Sbmspcap_stats_dead(pcap_t *p, struct pcap_stat *ps) 658127664Sbms{ 659127664Sbms snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 660127664Sbms "Statistics aren't available from a pcap_open_dead pcap_t"); 661127664Sbms return (-1); 662127664Sbms} 663127664Sbms 664127664Sbmsstatic void 665127664Sbmspcap_close_dead(pcap_t *p) 666127664Sbms{ 667127664Sbms /* Nothing to do. */ 668127664Sbms} 669127664Sbms 67075107Sfennerpcap_t * 67175107Sfennerpcap_open_dead(int linktype, int snaplen) 67275107Sfenner{ 67375107Sfenner pcap_t *p; 67475107Sfenner 67575107Sfenner p = malloc(sizeof(*p)); 67675107Sfenner if (p == NULL) 67775107Sfenner return NULL; 67875107Sfenner memset (p, 0, sizeof(*p)); 67975107Sfenner p->snapshot = snaplen; 68075107Sfenner p->linktype = linktype; 681127664Sbms p->stats_op = pcap_stats_dead; 682127664Sbms p->close_op = pcap_close_dead; 68375107Sfenner return p; 68475107Sfenner} 68575107Sfenner 68617683Spstvoid 68717683Spstpcap_close(pcap_t *p) 68817683Spst{ 689127664Sbms p->close_op(p); 690109839Sfenner if (p->dlt_list != NULL) 691109839Sfenner free(p->dlt_list); 69275107Sfenner pcap_freecode(&p->fcode); 69317683Spst free(p); 69417683Spst} 695127664Sbms 696127664Sbms/* 697127664Sbms * We make the version string static, and return a pointer to it, rather 698127664Sbms * than exporting the version string directly. On at least some UNIXes, 699127664Sbms * if you import data from a shared library into an program, the data is 700127664Sbms * bound into the program binary, so if the string in the version of the 701127664Sbms * library with which the program was linked isn't the same as the 702127664Sbms * string in the version of the library with which the program is being 703127664Sbms * run, various undesirable things may happen (warnings, the string 704127664Sbms * being the one from the version of the library with which the program 705127664Sbms * was linked, or even weirder things, such as the string being the one 706127664Sbms * from the library but being truncated). 707127664Sbms */ 708127664Sbms#ifdef WIN32 709127664Sbms/* 710127664Sbms * XXX - it'd be nice if we could somehow generate the WinPcap and libpcap 711127664Sbms * version numbers when building WinPcap. (It'd be nice to do so for 712127664Sbms * the packet.dll version number as well.) 713127664Sbms */ 714127664Sbmsstatic const char wpcap_version_string[] = "3.0"; 715127664Sbmsstatic const char pcap_version_string_fmt[] = 716127664Sbms "WinPcap version %s, based on libpcap version 0.8"; 717127664Sbmsstatic const char pcap_version_string_packet_dll_fmt[] = 718127664Sbms "WinPcap version %s (packet.dll version %s), based on libpcap version 0.8"; 719127664Sbmsstatic char *pcap_version_string; 720127664Sbms 721127664Sbmsconst char * 722127664Sbmspcap_lib_version(void) 723127664Sbms{ 724127664Sbms char *packet_version_string; 725127664Sbms size_t pcap_version_string_len; 726127664Sbms 727127664Sbms if (pcap_version_string == NULL) { 728127664Sbms /* 729127664Sbms * Generate the version string. 730127664Sbms */ 731127664Sbms packet_version_string = PacketGetVersion(); 732127664Sbms if (strcmp(wpcap_version_string, packet_version_string) == 0) { 733127664Sbms /* 734127664Sbms * WinPcap version string and packet.dll version 735127664Sbms * string are the same; just report the WinPcap 736127664Sbms * version. 737127664Sbms */ 738127664Sbms pcap_version_string_len = 739127664Sbms (sizeof pcap_version_string_fmt - 2) + 740127664Sbms strlen(wpcap_version_string); 741127664Sbms pcap_version_string = malloc(pcap_version_string_len); 742127664Sbms sprintf(pcap_version_string, pcap_version_string_fmt, 743127664Sbms wpcap_version_string); 744127664Sbms } else { 745127664Sbms /* 746127664Sbms * WinPcap version string and packet.dll version 747127664Sbms * string are different; that shouldn't be the 748127664Sbms * case (the two libraries should come from the 749127664Sbms * same version of WinPcap), so we report both 750127664Sbms * versions. 751127664Sbms */ 752127664Sbms pcap_version_string_len = 753127664Sbms (sizeof pcap_version_string_packet_dll_fmt - 4) + 754127664Sbms strlen(wpcap_version_string) + 755127664Sbms strlen(packet_version_string); 756127664Sbms pcap_version_string = malloc(pcap_version_string_len); 757127664Sbms sprintf(pcap_version_string, 758127664Sbms pcap_version_string_packet_dll_fmt, 759127664Sbms wpcap_version_string, packet_version_string); 760127664Sbms } 761127664Sbms } 762127664Sbms return (pcap_version_string); 763127664Sbms} 764127664Sbms#else 765127664Sbms#include "version.h" 766127664Sbms 767127664Sbmsconst char * 768127664Sbmspcap_lib_version(void) 769127664Sbms{ 770127664Sbms return (pcap_version_string); 771127664Sbms} 772127664Sbms#endif 773