1/* 2 * Copyright (c) 1993, 1994, 1995, 1996, 1997, 1998 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the Computer Systems 16 * Engineering Group at Lawrence Berkeley Laboratory. 17 * 4. Neither the name of the University nor of the Laboratory may be used 18 * to endorse or promote products derived from this software without 19 * specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#ifndef lint 35static const char rcsid[] _U_ =
| 1/* 2 * Copyright (c) 1993, 1994, 1995, 1996, 1997, 1998 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the Computer Systems 16 * Engineering Group at Lawrence Berkeley Laboratory. 17 * 4. Neither the name of the University nor of the Laboratory may be used 18 * to endorse or promote products derived from this software without 19 * specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#ifndef lint 35static const char rcsid[] _U_ =
|
36 "@(#) $Header: /tcpdump/master/libpcap/pcap.c,v 1.88.2.8 2005/08/13 22:29:46 hannes Exp $ (LBL)";
| 36 "@(#) $Header: /tcpdump/master/libpcap/pcap.c,v 1.88.2.19 2007/09/19 02:50:52 guy Exp $ (LBL)";
|
37#endif 38 39#ifdef HAVE_CONFIG_H 40#include "config.h" 41#endif 42 43#ifdef WIN32 44#include <pcap-stdinc.h> 45#else /* WIN32 */ 46#include <sys/types.h> 47#endif /* WIN32 */ 48 49#include <stdio.h> 50#include <stdlib.h> 51#include <string.h> 52#if !defined(_MSC_VER) && !defined(__BORLANDC__) 53#include <unistd.h> 54#endif 55#include <fcntl.h> 56#include <errno.h> 57 58#ifdef HAVE_OS_PROTO_H 59#include "os-proto.h" 60#endif 61 62#ifdef MSDOS 63#include "pcap-dos.h" 64#endif 65 66#include "pcap-int.h" 67 68#ifdef HAVE_DAG_API 69#include <dagnew.h> 70#include <dagapi.h> 71#endif 72 73int 74pcap_dispatch(pcap_t *p, int cnt, pcap_handler callback, u_char *user) 75{ 76 77 return p->read_op(p, cnt, callback, user); 78} 79 80/* 81 * XXX - is this necessary? 82 */ 83int 84pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) 85{ 86 87 return p->read_op(p, cnt, callback, user); 88} 89 90int 91pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user) 92{ 93 register int n; 94 95 for (;;) { 96 if (p->sf.rfile != NULL) { 97 /* 98 * 0 means EOF, so don't loop if we get 0. 99 */ 100 n = pcap_offline_read(p, cnt, callback, user); 101 } else { 102 /* 103 * XXX keep reading until we get something 104 * (or an error occurs) 105 */ 106 do { 107 n = p->read_op(p, cnt, callback, user); 108 } while (n == 0); 109 } 110 if (n <= 0) 111 return (n); 112 if (cnt > 0) { 113 cnt -= n; 114 if (cnt <= 0) 115 return (0); 116 } 117 } 118} 119 120struct singleton { 121 struct pcap_pkthdr *hdr; 122 const u_char *pkt; 123}; 124 125 126static void 127pcap_oneshot(u_char *userData, const struct pcap_pkthdr *h, const u_char *pkt) 128{ 129 struct singleton *sp = (struct singleton *)userData; 130 *sp->hdr = *h; 131 sp->pkt = pkt; 132} 133 134const u_char * 135pcap_next(pcap_t *p, struct pcap_pkthdr *h) 136{ 137 struct singleton s; 138 139 s.hdr = h; 140 if (pcap_dispatch(p, 1, pcap_oneshot, (u_char*)&s) <= 0) 141 return (0); 142 return (s.pkt); 143} 144 145struct pkt_for_fakecallback { 146 struct pcap_pkthdr *hdr; 147 const u_char **pkt; 148}; 149 150static void 151pcap_fakecallback(u_char *userData, const struct pcap_pkthdr *h, 152 const u_char *pkt) 153{ 154 struct pkt_for_fakecallback *sp = (struct pkt_for_fakecallback *)userData; 155 156 *sp->hdr = *h; 157 *sp->pkt = pkt; 158} 159 160int 161pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header, 162 const u_char **pkt_data) 163{ 164 struct pkt_for_fakecallback s; 165 166 s.hdr = &p->pcap_header; 167 s.pkt = pkt_data; 168 169 /* Saves a pointer to the packet headers */ 170 *pkt_header= &p->pcap_header; 171 172 if (p->sf.rfile != NULL) { 173 int status; 174 175 /* We are on an offline capture */ 176 status = pcap_offline_read(p, 1, pcap_fakecallback, 177 (u_char *)&s); 178 179 /* 180 * Return codes for pcap_offline_read() are: 181 * - 0: EOF 182 * - -1: error 183 * - >1: OK 184 * The first one ('0') conflicts with the return code of 185 * 0 from pcap_read() meaning "no packets arrived before 186 * the timeout expired", so we map it to -2 so you can 187 * distinguish between an EOF from a savefile and a 188 * "no packets arrived before the timeout expired, try 189 * again" from a live capture. 190 */ 191 if (status == 0) 192 return (-2); 193 else 194 return (status); 195 } 196 197 /* 198 * Return codes for pcap_read() are: 199 * - 0: timeout 200 * - -1: error 201 * - -2: loop was broken out of with pcap_breakloop() 202 * - >1: OK 203 * The first one ('0') conflicts with the return code of 0 from 204 * pcap_offline_read() meaning "end of file". 205 */ 206 return (p->read_op(p, 1, pcap_fakecallback, (u_char *)&s)); 207} 208 209/* 210 * Force the loop in "pcap_read()" or "pcap_read_offline()" to terminate. 211 */ 212void 213pcap_breakloop(pcap_t *p) 214{ 215 p->break_loop = 1; 216} 217 218int 219pcap_datalink(pcap_t *p) 220{ 221 return (p->linktype); 222} 223 224int 225pcap_list_datalinks(pcap_t *p, int **dlt_buffer) 226{ 227 if (p->dlt_count == 0) { 228 /* 229 * We couldn't fetch the list of DLTs, which means 230 * this platform doesn't support changing the 231 * DLT for an interface. Return a list of DLTs 232 * containing only the DLT this device supports. 233 */ 234 *dlt_buffer = (int*)malloc(sizeof(**dlt_buffer)); 235 if (*dlt_buffer == NULL) { 236 (void)snprintf(p->errbuf, sizeof(p->errbuf), 237 "malloc: %s", pcap_strerror(errno)); 238 return (-1); 239 } 240 **dlt_buffer = p->linktype; 241 return (1); 242 } else {
| 37#endif 38 39#ifdef HAVE_CONFIG_H 40#include "config.h" 41#endif 42 43#ifdef WIN32 44#include <pcap-stdinc.h> 45#else /* WIN32 */ 46#include <sys/types.h> 47#endif /* WIN32 */ 48 49#include <stdio.h> 50#include <stdlib.h> 51#include <string.h> 52#if !defined(_MSC_VER) && !defined(__BORLANDC__) 53#include <unistd.h> 54#endif 55#include <fcntl.h> 56#include <errno.h> 57 58#ifdef HAVE_OS_PROTO_H 59#include "os-proto.h" 60#endif 61 62#ifdef MSDOS 63#include "pcap-dos.h" 64#endif 65 66#include "pcap-int.h" 67 68#ifdef HAVE_DAG_API 69#include <dagnew.h> 70#include <dagapi.h> 71#endif 72 73int 74pcap_dispatch(pcap_t *p, int cnt, pcap_handler callback, u_char *user) 75{ 76 77 return p->read_op(p, cnt, callback, user); 78} 79 80/* 81 * XXX - is this necessary? 82 */ 83int 84pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) 85{ 86 87 return p->read_op(p, cnt, callback, user); 88} 89 90int 91pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user) 92{ 93 register int n; 94 95 for (;;) { 96 if (p->sf.rfile != NULL) { 97 /* 98 * 0 means EOF, so don't loop if we get 0. 99 */ 100 n = pcap_offline_read(p, cnt, callback, user); 101 } else { 102 /* 103 * XXX keep reading until we get something 104 * (or an error occurs) 105 */ 106 do { 107 n = p->read_op(p, cnt, callback, user); 108 } while (n == 0); 109 } 110 if (n <= 0) 111 return (n); 112 if (cnt > 0) { 113 cnt -= n; 114 if (cnt <= 0) 115 return (0); 116 } 117 } 118} 119 120struct singleton { 121 struct pcap_pkthdr *hdr; 122 const u_char *pkt; 123}; 124 125 126static void 127pcap_oneshot(u_char *userData, const struct pcap_pkthdr *h, const u_char *pkt) 128{ 129 struct singleton *sp = (struct singleton *)userData; 130 *sp->hdr = *h; 131 sp->pkt = pkt; 132} 133 134const u_char * 135pcap_next(pcap_t *p, struct pcap_pkthdr *h) 136{ 137 struct singleton s; 138 139 s.hdr = h; 140 if (pcap_dispatch(p, 1, pcap_oneshot, (u_char*)&s) <= 0) 141 return (0); 142 return (s.pkt); 143} 144 145struct pkt_for_fakecallback { 146 struct pcap_pkthdr *hdr; 147 const u_char **pkt; 148}; 149 150static void 151pcap_fakecallback(u_char *userData, const struct pcap_pkthdr *h, 152 const u_char *pkt) 153{ 154 struct pkt_for_fakecallback *sp = (struct pkt_for_fakecallback *)userData; 155 156 *sp->hdr = *h; 157 *sp->pkt = pkt; 158} 159 160int 161pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header, 162 const u_char **pkt_data) 163{ 164 struct pkt_for_fakecallback s; 165 166 s.hdr = &p->pcap_header; 167 s.pkt = pkt_data; 168 169 /* Saves a pointer to the packet headers */ 170 *pkt_header= &p->pcap_header; 171 172 if (p->sf.rfile != NULL) { 173 int status; 174 175 /* We are on an offline capture */ 176 status = pcap_offline_read(p, 1, pcap_fakecallback, 177 (u_char *)&s); 178 179 /* 180 * Return codes for pcap_offline_read() are: 181 * - 0: EOF 182 * - -1: error 183 * - >1: OK 184 * The first one ('0') conflicts with the return code of 185 * 0 from pcap_read() meaning "no packets arrived before 186 * the timeout expired", so we map it to -2 so you can 187 * distinguish between an EOF from a savefile and a 188 * "no packets arrived before the timeout expired, try 189 * again" from a live capture. 190 */ 191 if (status == 0) 192 return (-2); 193 else 194 return (status); 195 } 196 197 /* 198 * Return codes for pcap_read() are: 199 * - 0: timeout 200 * - -1: error 201 * - -2: loop was broken out of with pcap_breakloop() 202 * - >1: OK 203 * The first one ('0') conflicts with the return code of 0 from 204 * pcap_offline_read() meaning "end of file". 205 */ 206 return (p->read_op(p, 1, pcap_fakecallback, (u_char *)&s)); 207} 208 209/* 210 * Force the loop in "pcap_read()" or "pcap_read_offline()" to terminate. 211 */ 212void 213pcap_breakloop(pcap_t *p) 214{ 215 p->break_loop = 1; 216} 217 218int 219pcap_datalink(pcap_t *p) 220{ 221 return (p->linktype); 222} 223 224int 225pcap_list_datalinks(pcap_t *p, int **dlt_buffer) 226{ 227 if (p->dlt_count == 0) { 228 /* 229 * We couldn't fetch the list of DLTs, which means 230 * this platform doesn't support changing the 231 * DLT for an interface. Return a list of DLTs 232 * containing only the DLT this device supports. 233 */ 234 *dlt_buffer = (int*)malloc(sizeof(**dlt_buffer)); 235 if (*dlt_buffer == NULL) { 236 (void)snprintf(p->errbuf, sizeof(p->errbuf), 237 "malloc: %s", pcap_strerror(errno)); 238 return (-1); 239 } 240 **dlt_buffer = p->linktype; 241 return (1); 242 } else {
|
243 *dlt_buffer = (int*)malloc(sizeof(**dlt_buffer) * p->dlt_count);
| 243 *dlt_buffer = (int*)calloc(sizeof(**dlt_buffer), p->dlt_count);
|
244 if (*dlt_buffer == NULL) { 245 (void)snprintf(p->errbuf, sizeof(p->errbuf), 246 "malloc: %s", pcap_strerror(errno)); 247 return (-1); 248 } 249 (void)memcpy(*dlt_buffer, p->dlt_list, 250 sizeof(**dlt_buffer) * p->dlt_count); 251 return (p->dlt_count); 252 } 253} 254 255int 256pcap_set_datalink(pcap_t *p, int dlt) 257{ 258 int i; 259 const char *dlt_name; 260 261 if (p->dlt_count == 0 || p->set_datalink_op == NULL) { 262 /* 263 * We couldn't fetch the list of DLTs, or we don't 264 * have a "set datalink" operation, which means 265 * this platform doesn't support changing the 266 * DLT for an interface. Check whether the new 267 * DLT is the one this interface supports. 268 */ 269 if (p->linktype != dlt) 270 goto unsupported; 271 272 /* 273 * It is, so there's nothing we need to do here. 274 */ 275 return (0); 276 } 277 for (i = 0; i < p->dlt_count; i++) 278 if (p->dlt_list[i] == dlt) 279 break; 280 if (i >= p->dlt_count) 281 goto unsupported; 282 if (p->dlt_count == 2 && p->dlt_list[0] == DLT_EN10MB && 283 dlt == DLT_DOCSIS) { 284 /* 285 * This is presumably an Ethernet device, as the first 286 * link-layer type it offers is DLT_EN10MB, and the only 287 * other type it offers is DLT_DOCSIS. That means that 288 * we can't tell the driver to supply DOCSIS link-layer 289 * headers - we're just pretending that's what we're 290 * getting, as, presumably, we're capturing on a dedicated 291 * link to a Cisco Cable Modem Termination System, and 292 * it's putting raw DOCSIS frames on the wire inside low-level 293 * Ethernet framing. 294 */ 295 p->linktype = dlt; 296 return (0); 297 } 298 if (p->set_datalink_op(p, dlt) == -1) 299 return (-1); 300 p->linktype = dlt; 301 return (0); 302 303unsupported: 304 dlt_name = pcap_datalink_val_to_name(dlt); 305 if (dlt_name != NULL) { 306 (void) snprintf(p->errbuf, sizeof(p->errbuf), 307 "%s is not one of the DLTs supported by this device", 308 dlt_name); 309 } else { 310 (void) snprintf(p->errbuf, sizeof(p->errbuf), 311 "DLT %d is not one of the DLTs supported by this device", 312 dlt); 313 } 314 return (-1); 315} 316 317struct dlt_choice { 318 const char *name; 319 const char *description; 320 int dlt; 321}; 322 323#define DLT_CHOICE(code, description) { #code, description, code } 324#define DLT_CHOICE_SENTINEL { NULL, NULL, 0 } 325 326static struct dlt_choice dlt_choices[] = { 327 DLT_CHOICE(DLT_NULL, "BSD loopback"), 328 DLT_CHOICE(DLT_EN10MB, "Ethernet"), 329 DLT_CHOICE(DLT_IEEE802, "Token ring"), 330 DLT_CHOICE(DLT_ARCNET, "ARCNET"), 331 DLT_CHOICE(DLT_SLIP, "SLIP"), 332 DLT_CHOICE(DLT_PPP, "PPP"), 333 DLT_CHOICE(DLT_FDDI, "FDDI"), 334 DLT_CHOICE(DLT_ATM_RFC1483, "RFC 1483 LLC-encapsulated ATM"), 335 DLT_CHOICE(DLT_RAW, "Raw IP"), 336 DLT_CHOICE(DLT_SLIP_BSDOS, "BSD/OS SLIP"), 337 DLT_CHOICE(DLT_PPP_BSDOS, "BSD/OS PPP"), 338 DLT_CHOICE(DLT_ATM_CLIP, "Linux Classical IP-over-ATM"), 339 DLT_CHOICE(DLT_PPP_SERIAL, "PPP over serial"), 340 DLT_CHOICE(DLT_PPP_ETHER, "PPPoE"), 341 DLT_CHOICE(DLT_C_HDLC, "Cisco HDLC"), 342 DLT_CHOICE(DLT_IEEE802_11, "802.11"), 343 DLT_CHOICE(DLT_FRELAY, "Frame Relay"), 344 DLT_CHOICE(DLT_LOOP, "OpenBSD loopback"), 345 DLT_CHOICE(DLT_ENC, "OpenBSD encapsulated IP"), 346 DLT_CHOICE(DLT_LINUX_SLL, "Linux cooked"), 347 DLT_CHOICE(DLT_LTALK, "Localtalk"), 348 DLT_CHOICE(DLT_PFLOG, "OpenBSD pflog file"), 349 DLT_CHOICE(DLT_PRISM_HEADER, "802.11 plus Prism header"), 350 DLT_CHOICE(DLT_IP_OVER_FC, "RFC 2625 IP-over-Fibre Channel"), 351 DLT_CHOICE(DLT_SUNATM, "Sun raw ATM"), 352 DLT_CHOICE(DLT_IEEE802_11_RADIO, "802.11 plus BSD radio information header"), 353 DLT_CHOICE(DLT_APPLE_IP_OVER_IEEE1394, "Apple IP-over-IEEE 1394"), 354 DLT_CHOICE(DLT_ARCNET_LINUX, "Linux ARCNET"), 355 DLT_CHOICE(DLT_DOCSIS, "DOCSIS"), 356 DLT_CHOICE(DLT_LINUX_IRDA, "Linux IrDA"),
| 244 if (*dlt_buffer == NULL) { 245 (void)snprintf(p->errbuf, sizeof(p->errbuf), 246 "malloc: %s", pcap_strerror(errno)); 247 return (-1); 248 } 249 (void)memcpy(*dlt_buffer, p->dlt_list, 250 sizeof(**dlt_buffer) * p->dlt_count); 251 return (p->dlt_count); 252 } 253} 254 255int 256pcap_set_datalink(pcap_t *p, int dlt) 257{ 258 int i; 259 const char *dlt_name; 260 261 if (p->dlt_count == 0 || p->set_datalink_op == NULL) { 262 /* 263 * We couldn't fetch the list of DLTs, or we don't 264 * have a "set datalink" operation, which means 265 * this platform doesn't support changing the 266 * DLT for an interface. Check whether the new 267 * DLT is the one this interface supports. 268 */ 269 if (p->linktype != dlt) 270 goto unsupported; 271 272 /* 273 * It is, so there's nothing we need to do here. 274 */ 275 return (0); 276 } 277 for (i = 0; i < p->dlt_count; i++) 278 if (p->dlt_list[i] == dlt) 279 break; 280 if (i >= p->dlt_count) 281 goto unsupported; 282 if (p->dlt_count == 2 && p->dlt_list[0] == DLT_EN10MB && 283 dlt == DLT_DOCSIS) { 284 /* 285 * This is presumably an Ethernet device, as the first 286 * link-layer type it offers is DLT_EN10MB, and the only 287 * other type it offers is DLT_DOCSIS. That means that 288 * we can't tell the driver to supply DOCSIS link-layer 289 * headers - we're just pretending that's what we're 290 * getting, as, presumably, we're capturing on a dedicated 291 * link to a Cisco Cable Modem Termination System, and 292 * it's putting raw DOCSIS frames on the wire inside low-level 293 * Ethernet framing. 294 */ 295 p->linktype = dlt; 296 return (0); 297 } 298 if (p->set_datalink_op(p, dlt) == -1) 299 return (-1); 300 p->linktype = dlt; 301 return (0); 302 303unsupported: 304 dlt_name = pcap_datalink_val_to_name(dlt); 305 if (dlt_name != NULL) { 306 (void) snprintf(p->errbuf, sizeof(p->errbuf), 307 "%s is not one of the DLTs supported by this device", 308 dlt_name); 309 } else { 310 (void) snprintf(p->errbuf, sizeof(p->errbuf), 311 "DLT %d is not one of the DLTs supported by this device", 312 dlt); 313 } 314 return (-1); 315} 316 317struct dlt_choice { 318 const char *name; 319 const char *description; 320 int dlt; 321}; 322 323#define DLT_CHOICE(code, description) { #code, description, code } 324#define DLT_CHOICE_SENTINEL { NULL, NULL, 0 } 325 326static struct dlt_choice dlt_choices[] = { 327 DLT_CHOICE(DLT_NULL, "BSD loopback"), 328 DLT_CHOICE(DLT_EN10MB, "Ethernet"), 329 DLT_CHOICE(DLT_IEEE802, "Token ring"), 330 DLT_CHOICE(DLT_ARCNET, "ARCNET"), 331 DLT_CHOICE(DLT_SLIP, "SLIP"), 332 DLT_CHOICE(DLT_PPP, "PPP"), 333 DLT_CHOICE(DLT_FDDI, "FDDI"), 334 DLT_CHOICE(DLT_ATM_RFC1483, "RFC 1483 LLC-encapsulated ATM"), 335 DLT_CHOICE(DLT_RAW, "Raw IP"), 336 DLT_CHOICE(DLT_SLIP_BSDOS, "BSD/OS SLIP"), 337 DLT_CHOICE(DLT_PPP_BSDOS, "BSD/OS PPP"), 338 DLT_CHOICE(DLT_ATM_CLIP, "Linux Classical IP-over-ATM"), 339 DLT_CHOICE(DLT_PPP_SERIAL, "PPP over serial"), 340 DLT_CHOICE(DLT_PPP_ETHER, "PPPoE"), 341 DLT_CHOICE(DLT_C_HDLC, "Cisco HDLC"), 342 DLT_CHOICE(DLT_IEEE802_11, "802.11"), 343 DLT_CHOICE(DLT_FRELAY, "Frame Relay"), 344 DLT_CHOICE(DLT_LOOP, "OpenBSD loopback"), 345 DLT_CHOICE(DLT_ENC, "OpenBSD encapsulated IP"), 346 DLT_CHOICE(DLT_LINUX_SLL, "Linux cooked"), 347 DLT_CHOICE(DLT_LTALK, "Localtalk"), 348 DLT_CHOICE(DLT_PFLOG, "OpenBSD pflog file"), 349 DLT_CHOICE(DLT_PRISM_HEADER, "802.11 plus Prism header"), 350 DLT_CHOICE(DLT_IP_OVER_FC, "RFC 2625 IP-over-Fibre Channel"), 351 DLT_CHOICE(DLT_SUNATM, "Sun raw ATM"), 352 DLT_CHOICE(DLT_IEEE802_11_RADIO, "802.11 plus BSD radio information header"), 353 DLT_CHOICE(DLT_APPLE_IP_OVER_IEEE1394, "Apple IP-over-IEEE 1394"), 354 DLT_CHOICE(DLT_ARCNET_LINUX, "Linux ARCNET"), 355 DLT_CHOICE(DLT_DOCSIS, "DOCSIS"), 356 DLT_CHOICE(DLT_LINUX_IRDA, "Linux IrDA"),
|
| 357 DLT_CHOICE(DLT_LINUX_LAPD, "Linux vISDN LAPD"),
|
357 DLT_CHOICE(DLT_IEEE802_11_RADIO_AVS, "802.11 plus AVS radio information header"), 358 DLT_CHOICE(DLT_SYMANTEC_FIREWALL, "Symantec Firewall"), 359 DLT_CHOICE(DLT_JUNIPER_ATM1, "Juniper ATM1 PIC"), 360 DLT_CHOICE(DLT_JUNIPER_ATM2, "Juniper ATM2 PIC"), 361 DLT_CHOICE(DLT_JUNIPER_MLPPP, "Juniper Multi-Link PPP"),
| 358 DLT_CHOICE(DLT_IEEE802_11_RADIO_AVS, "802.11 plus AVS radio information header"), 359 DLT_CHOICE(DLT_SYMANTEC_FIREWALL, "Symantec Firewall"), 360 DLT_CHOICE(DLT_JUNIPER_ATM1, "Juniper ATM1 PIC"), 361 DLT_CHOICE(DLT_JUNIPER_ATM2, "Juniper ATM2 PIC"), 362 DLT_CHOICE(DLT_JUNIPER_MLPPP, "Juniper Multi-Link PPP"),
|
362 DLT_CHOICE(DLT_PPP_PPPD, "PPP for pppd, with direction flag"), 363 DLT_CHOICE(DLT_JUNIPER_PPPOE, "Juniper PPPoE"), 364 DLT_CHOICE(DLT_JUNIPER_PPPOE_ATM, "Juniper PPPoE/ATM"), 365 DLT_CHOICE(DLT_GPRS_LLC, "GPRS LLC"), 366 DLT_CHOICE(DLT_GPF_T, "GPF-T"), 367 DLT_CHOICE(DLT_GPF_F, "GPF-F"), 368 DLT_CHOICE(DLT_JUNIPER_PIC_PEER, "Juniper PIC Peer"), 369 DLT_CHOICE(DLT_JUNIPER_MLFR, "Juniper Multi-Link Frame Relay"),
| 363 DLT_CHOICE(DLT_PPP_PPPD, "PPP for pppd, with direction flag"), 364 DLT_CHOICE(DLT_JUNIPER_PPPOE, "Juniper PPPoE"), 365 DLT_CHOICE(DLT_JUNIPER_PPPOE_ATM, "Juniper PPPoE/ATM"), 366 DLT_CHOICE(DLT_GPRS_LLC, "GPRS LLC"), 367 DLT_CHOICE(DLT_GPF_T, "GPF-T"), 368 DLT_CHOICE(DLT_GPF_F, "GPF-F"), 369 DLT_CHOICE(DLT_JUNIPER_PIC_PEER, "Juniper PIC Peer"), 370 DLT_CHOICE(DLT_JUNIPER_MLFR, "Juniper Multi-Link Frame Relay"),
|
370 DLT_CHOICE(DLT_ERF_ETH, "Ethernet with Endace ERF header"), 371 DLT_CHOICE(DLT_ERF_POS, "Packet-over-SONET with Endace ERF header"), 372 DLT_CHOICE(DLT_JUNIPER_GGSN, "Juniper GGSN PIC"), 373 DLT_CHOICE(DLT_JUNIPER_ES, "Juniper Encryption Services PIC"), 374 DLT_CHOICE(DLT_JUNIPER_MONITOR, "Juniper Passive Monitor PIC"), 375 DLT_CHOICE(DLT_JUNIPER_SERVICES, "Juniper Advanced Services PIC"),
| 371 DLT_CHOICE(DLT_ERF_ETH, "Ethernet with Endace ERF header"), 372 DLT_CHOICE(DLT_ERF_POS, "Packet-over-SONET with Endace ERF header"), 373 DLT_CHOICE(DLT_JUNIPER_GGSN, "Juniper GGSN PIC"), 374 DLT_CHOICE(DLT_JUNIPER_ES, "Juniper Encryption Services PIC"), 375 DLT_CHOICE(DLT_JUNIPER_MONITOR, "Juniper Passive Monitor PIC"), 376 DLT_CHOICE(DLT_JUNIPER_SERVICES, "Juniper Advanced Services PIC"),
|
376 DLT_CHOICE(DLT_JUNIPER_MFR, "Juniper FRF.16 Frame Relay"), 377 DLT_CHOICE(DLT_JUNIPER_ETHER, "Juniper Ethernet"), 378 DLT_CHOICE(DLT_JUNIPER_PPP, "Juniper PPP"), 379 DLT_CHOICE(DLT_JUNIPER_FRELAY, "Juniper Frame Relay"), 380 DLT_CHOICE(DLT_JUNIPER_CHDLC, "Juniper C-HDLC"),
| 377 DLT_CHOICE(DLT_JUNIPER_MFR, "Juniper FRF.16 Frame Relay"), 378 DLT_CHOICE(DLT_JUNIPER_ETHER, "Juniper Ethernet"), 379 DLT_CHOICE(DLT_JUNIPER_PPP, "Juniper PPP"), 380 DLT_CHOICE(DLT_JUNIPER_FRELAY, "Juniper Frame Relay"), 381 DLT_CHOICE(DLT_JUNIPER_CHDLC, "Juniper C-HDLC"), 382 DLT_CHOICE(DLT_MFR, "FRF.16 Frame Relay"), 383 DLT_CHOICE(DLT_JUNIPER_VP, "Juniper Voice PIC"), 384 DLT_CHOICE(DLT_MTP2, "SS7 MTP2"), 385 DLT_CHOICE(DLT_A429, "Arinc 429"), 386 DLT_CHOICE(DLT_A653_ICM, "Arinc 653 Interpartition Communication"), 387 DLT_CHOICE(DLT_USB, "USB"), 388 DLT_CHOICE(DLT_BLUETOOTH_HCI_H4, "Bluetooth HCI UART transport layer"), 389 DLT_CHOICE(DLT_CAN20B, "Controller Area Network (CAN) v. 2.0B"), 390 DLT_CHOICE(DLT_MTP2_WITH_PHDR, "SS7 MTP2 with Pseudo-header"),
|
381 DLT_CHOICE_SENTINEL 382}; 383 384/* 385 * This array is designed for mapping upper and lower case letter 386 * together for a case independent comparison. The mappings are 387 * based upon ascii character sequences. 388 */ 389static const u_char charmap[] = { 390 (u_char)'\000', (u_char)'\001', (u_char)'\002', (u_char)'\003', 391 (u_char)'\004', (u_char)'\005', (u_char)'\006', (u_char)'\007', 392 (u_char)'\010', (u_char)'\011', (u_char)'\012', (u_char)'\013', 393 (u_char)'\014', (u_char)'\015', (u_char)'\016', (u_char)'\017', 394 (u_char)'\020', (u_char)'\021', (u_char)'\022', (u_char)'\023', 395 (u_char)'\024', (u_char)'\025', (u_char)'\026', (u_char)'\027', 396 (u_char)'\030', (u_char)'\031', (u_char)'\032', (u_char)'\033', 397 (u_char)'\034', (u_char)'\035', (u_char)'\036', (u_char)'\037', 398 (u_char)'\040', (u_char)'\041', (u_char)'\042', (u_char)'\043', 399 (u_char)'\044', (u_char)'\045', (u_char)'\046', (u_char)'\047', 400 (u_char)'\050', (u_char)'\051', (u_char)'\052', (u_char)'\053', 401 (u_char)'\054', (u_char)'\055', (u_char)'\056', (u_char)'\057', 402 (u_char)'\060', (u_char)'\061', (u_char)'\062', (u_char)'\063', 403 (u_char)'\064', (u_char)'\065', (u_char)'\066', (u_char)'\067', 404 (u_char)'\070', (u_char)'\071', (u_char)'\072', (u_char)'\073', 405 (u_char)'\074', (u_char)'\075', (u_char)'\076', (u_char)'\077', 406 (u_char)'\100', (u_char)'\141', (u_char)'\142', (u_char)'\143', 407 (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147', 408 (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153', 409 (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157', 410 (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163', 411 (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167', 412 (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\133', 413 (u_char)'\134', (u_char)'\135', (u_char)'\136', (u_char)'\137', 414 (u_char)'\140', (u_char)'\141', (u_char)'\142', (u_char)'\143', 415 (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147', 416 (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153', 417 (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157', 418 (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163', 419 (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167', 420 (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\173', 421 (u_char)'\174', (u_char)'\175', (u_char)'\176', (u_char)'\177', 422 (u_char)'\200', (u_char)'\201', (u_char)'\202', (u_char)'\203', 423 (u_char)'\204', (u_char)'\205', (u_char)'\206', (u_char)'\207', 424 (u_char)'\210', (u_char)'\211', (u_char)'\212', (u_char)'\213', 425 (u_char)'\214', (u_char)'\215', (u_char)'\216', (u_char)'\217', 426 (u_char)'\220', (u_char)'\221', (u_char)'\222', (u_char)'\223', 427 (u_char)'\224', (u_char)'\225', (u_char)'\226', (u_char)'\227', 428 (u_char)'\230', (u_char)'\231', (u_char)'\232', (u_char)'\233', 429 (u_char)'\234', (u_char)'\235', (u_char)'\236', (u_char)'\237', 430 (u_char)'\240', (u_char)'\241', (u_char)'\242', (u_char)'\243', 431 (u_char)'\244', (u_char)'\245', (u_char)'\246', (u_char)'\247', 432 (u_char)'\250', (u_char)'\251', (u_char)'\252', (u_char)'\253', 433 (u_char)'\254', (u_char)'\255', (u_char)'\256', (u_char)'\257', 434 (u_char)'\260', (u_char)'\261', (u_char)'\262', (u_char)'\263', 435 (u_char)'\264', (u_char)'\265', (u_char)'\266', (u_char)'\267', 436 (u_char)'\270', (u_char)'\271', (u_char)'\272', (u_char)'\273', 437 (u_char)'\274', (u_char)'\275', (u_char)'\276', (u_char)'\277', 438 (u_char)'\300', (u_char)'\341', (u_char)'\342', (u_char)'\343', 439 (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347', 440 (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353', 441 (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357', 442 (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363', 443 (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367', 444 (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\333', 445 (u_char)'\334', (u_char)'\335', (u_char)'\336', (u_char)'\337', 446 (u_char)'\340', (u_char)'\341', (u_char)'\342', (u_char)'\343', 447 (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347', 448 (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353', 449 (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357', 450 (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363', 451 (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367', 452 (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\373', 453 (u_char)'\374', (u_char)'\375', (u_char)'\376', (u_char)'\377', 454}; 455 456int 457pcap_strcasecmp(const char *s1, const char *s2) 458{ 459 register const u_char *cm = charmap,
| 391 DLT_CHOICE_SENTINEL 392}; 393 394/* 395 * This array is designed for mapping upper and lower case letter 396 * together for a case independent comparison. The mappings are 397 * based upon ascii character sequences. 398 */ 399static const u_char charmap[] = { 400 (u_char)'\000', (u_char)'\001', (u_char)'\002', (u_char)'\003', 401 (u_char)'\004', (u_char)'\005', (u_char)'\006', (u_char)'\007', 402 (u_char)'\010', (u_char)'\011', (u_char)'\012', (u_char)'\013', 403 (u_char)'\014', (u_char)'\015', (u_char)'\016', (u_char)'\017', 404 (u_char)'\020', (u_char)'\021', (u_char)'\022', (u_char)'\023', 405 (u_char)'\024', (u_char)'\025', (u_char)'\026', (u_char)'\027', 406 (u_char)'\030', (u_char)'\031', (u_char)'\032', (u_char)'\033', 407 (u_char)'\034', (u_char)'\035', (u_char)'\036', (u_char)'\037', 408 (u_char)'\040', (u_char)'\041', (u_char)'\042', (u_char)'\043', 409 (u_char)'\044', (u_char)'\045', (u_char)'\046', (u_char)'\047', 410 (u_char)'\050', (u_char)'\051', (u_char)'\052', (u_char)'\053', 411 (u_char)'\054', (u_char)'\055', (u_char)'\056', (u_char)'\057', 412 (u_char)'\060', (u_char)'\061', (u_char)'\062', (u_char)'\063', 413 (u_char)'\064', (u_char)'\065', (u_char)'\066', (u_char)'\067', 414 (u_char)'\070', (u_char)'\071', (u_char)'\072', (u_char)'\073', 415 (u_char)'\074', (u_char)'\075', (u_char)'\076', (u_char)'\077', 416 (u_char)'\100', (u_char)'\141', (u_char)'\142', (u_char)'\143', 417 (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147', 418 (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153', 419 (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157', 420 (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163', 421 (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167', 422 (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\133', 423 (u_char)'\134', (u_char)'\135', (u_char)'\136', (u_char)'\137', 424 (u_char)'\140', (u_char)'\141', (u_char)'\142', (u_char)'\143', 425 (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147', 426 (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153', 427 (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157', 428 (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163', 429 (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167', 430 (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\173', 431 (u_char)'\174', (u_char)'\175', (u_char)'\176', (u_char)'\177', 432 (u_char)'\200', (u_char)'\201', (u_char)'\202', (u_char)'\203', 433 (u_char)'\204', (u_char)'\205', (u_char)'\206', (u_char)'\207', 434 (u_char)'\210', (u_char)'\211', (u_char)'\212', (u_char)'\213', 435 (u_char)'\214', (u_char)'\215', (u_char)'\216', (u_char)'\217', 436 (u_char)'\220', (u_char)'\221', (u_char)'\222', (u_char)'\223', 437 (u_char)'\224', (u_char)'\225', (u_char)'\226', (u_char)'\227', 438 (u_char)'\230', (u_char)'\231', (u_char)'\232', (u_char)'\233', 439 (u_char)'\234', (u_char)'\235', (u_char)'\236', (u_char)'\237', 440 (u_char)'\240', (u_char)'\241', (u_char)'\242', (u_char)'\243', 441 (u_char)'\244', (u_char)'\245', (u_char)'\246', (u_char)'\247', 442 (u_char)'\250', (u_char)'\251', (u_char)'\252', (u_char)'\253', 443 (u_char)'\254', (u_char)'\255', (u_char)'\256', (u_char)'\257', 444 (u_char)'\260', (u_char)'\261', (u_char)'\262', (u_char)'\263', 445 (u_char)'\264', (u_char)'\265', (u_char)'\266', (u_char)'\267', 446 (u_char)'\270', (u_char)'\271', (u_char)'\272', (u_char)'\273', 447 (u_char)'\274', (u_char)'\275', (u_char)'\276', (u_char)'\277', 448 (u_char)'\300', (u_char)'\341', (u_char)'\342', (u_char)'\343', 449 (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347', 450 (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353', 451 (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357', 452 (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363', 453 (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367', 454 (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\333', 455 (u_char)'\334', (u_char)'\335', (u_char)'\336', (u_char)'\337', 456 (u_char)'\340', (u_char)'\341', (u_char)'\342', (u_char)'\343', 457 (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347', 458 (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353', 459 (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357', 460 (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363', 461 (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367', 462 (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\373', 463 (u_char)'\374', (u_char)'\375', (u_char)'\376', (u_char)'\377', 464}; 465 466int 467pcap_strcasecmp(const char *s1, const char *s2) 468{ 469 register const u_char *cm = charmap,
|
460 *us1 = (u_char *)s1, 461 *us2 = (u_char *)s2;
| 470 *us1 = (const u_char *)s1, 471 *us2 = (const u_char *)s2;
|
462 463 while (cm[*us1] == cm[*us2++]) 464 if (*us1++ == '\0') 465 return(0); 466 return (cm[*us1] - cm[*--us2]); 467} 468 469int 470pcap_datalink_name_to_val(const char *name) 471{ 472 int i; 473 474 for (i = 0; dlt_choices[i].name != NULL; i++) { 475 if (pcap_strcasecmp(dlt_choices[i].name + sizeof("DLT_") - 1, 476 name) == 0) 477 return (dlt_choices[i].dlt); 478 } 479 return (-1); 480} 481 482const char * 483pcap_datalink_val_to_name(int dlt) 484{ 485 int i; 486 487 for (i = 0; dlt_choices[i].name != NULL; i++) { 488 if (dlt_choices[i].dlt == dlt) 489 return (dlt_choices[i].name + sizeof("DLT_") - 1); 490 } 491 return (NULL); 492} 493 494const char * 495pcap_datalink_val_to_description(int dlt) 496{ 497 int i; 498 499 for (i = 0; dlt_choices[i].name != NULL; i++) { 500 if (dlt_choices[i].dlt == dlt) 501 return (dlt_choices[i].description); 502 } 503 return (NULL); 504} 505 506int 507pcap_snapshot(pcap_t *p) 508{ 509 return (p->snapshot); 510} 511 512int 513pcap_is_swapped(pcap_t *p) 514{ 515 return (p->sf.swapped); 516} 517 518int 519pcap_major_version(pcap_t *p) 520{ 521 return (p->sf.version_major); 522} 523 524int 525pcap_minor_version(pcap_t *p) 526{ 527 return (p->sf.version_minor); 528} 529 530FILE * 531pcap_file(pcap_t *p) 532{ 533 return (p->sf.rfile); 534} 535 536int 537pcap_fileno(pcap_t *p) 538{ 539#ifndef WIN32 540 return (p->fd); 541#else 542 if (p->adapter != NULL) 543 return ((int)(DWORD)p->adapter->hFile); 544 else 545 return (-1); 546#endif 547} 548 549#if !defined(WIN32) && !defined(MSDOS) 550int 551pcap_get_selectable_fd(pcap_t *p) 552{ 553 return (p->selectable_fd); 554} 555#endif 556 557void 558pcap_perror(pcap_t *p, char *prefix) 559{ 560 fprintf(stderr, "%s: %s\n", prefix, p->errbuf); 561} 562 563char * 564pcap_geterr(pcap_t *p) 565{ 566 return (p->errbuf); 567} 568 569int 570pcap_getnonblock(pcap_t *p, char *errbuf) 571{ 572 return p->getnonblock_op(p, errbuf); 573} 574 575/* 576 * Get the current non-blocking mode setting, under the assumption that 577 * it's just the standard POSIX non-blocking flag. 578 * 579 * We don't look at "p->nonblock", in case somebody tweaked the FD 580 * directly. 581 */ 582#if !defined(WIN32) && !defined(MSDOS) 583int 584pcap_getnonblock_fd(pcap_t *p, char *errbuf) 585{ 586 int fdflags; 587 588 fdflags = fcntl(p->fd, F_GETFL, 0); 589 if (fdflags == -1) { 590 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s", 591 pcap_strerror(errno)); 592 return (-1); 593 } 594 if (fdflags & O_NONBLOCK) 595 return (1); 596 else 597 return (0); 598} 599#endif 600 601int 602pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf) 603{ 604 return p->setnonblock_op(p, nonblock, errbuf); 605} 606 607#if !defined(WIN32) && !defined(MSDOS) 608/* 609 * Set non-blocking mode, under the assumption that it's just the 610 * standard POSIX non-blocking flag. (This can be called by the 611 * per-platform non-blocking-mode routine if that routine also 612 * needs to do some additional work.) 613 */ 614int 615pcap_setnonblock_fd(pcap_t *p, int nonblock, char *errbuf) 616{ 617 int fdflags; 618 619 fdflags = fcntl(p->fd, F_GETFL, 0); 620 if (fdflags == -1) { 621 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s", 622 pcap_strerror(errno)); 623 return (-1); 624 } 625 if (nonblock) 626 fdflags |= O_NONBLOCK; 627 else 628 fdflags &= ~O_NONBLOCK; 629 if (fcntl(p->fd, F_SETFL, fdflags) == -1) { 630 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_SETFL: %s", 631 pcap_strerror(errno)); 632 return (-1); 633 } 634 return (0); 635} 636#endif 637 638#ifdef WIN32 639/* 640 * Generate a string for the last Win32-specific error (i.e. an error generated when 641 * calling a Win32 API). 642 * For errors occurred during standard C calls, we still use pcap_strerror() 643 */ 644char * 645pcap_win32strerror(void) 646{ 647 DWORD error; 648 static char errbuf[PCAP_ERRBUF_SIZE+1]; 649 int errlen; 650 char *p; 651 652 error = GetLastError(); 653 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, errbuf, 654 PCAP_ERRBUF_SIZE, NULL); 655 656 /* 657 * "FormatMessage()" "helpfully" sticks CR/LF at the end of the 658 * message. Get rid of it. 659 */ 660 errlen = strlen(errbuf); 661 if (errlen >= 2) { 662 errbuf[errlen - 1] = '\0'; 663 errbuf[errlen - 2] = '\0'; 664 } 665 p = strchr(errbuf, '\0'); 666 snprintf (p, sizeof(errbuf)-(p-errbuf), " (%lu)", error); 667 return (errbuf); 668} 669#endif 670 671/* 672 * Not all systems have strerror(). 673 */
| 472 473 while (cm[*us1] == cm[*us2++]) 474 if (*us1++ == '\0') 475 return(0); 476 return (cm[*us1] - cm[*--us2]); 477} 478 479int 480pcap_datalink_name_to_val(const char *name) 481{ 482 int i; 483 484 for (i = 0; dlt_choices[i].name != NULL; i++) { 485 if (pcap_strcasecmp(dlt_choices[i].name + sizeof("DLT_") - 1, 486 name) == 0) 487 return (dlt_choices[i].dlt); 488 } 489 return (-1); 490} 491 492const char * 493pcap_datalink_val_to_name(int dlt) 494{ 495 int i; 496 497 for (i = 0; dlt_choices[i].name != NULL; i++) { 498 if (dlt_choices[i].dlt == dlt) 499 return (dlt_choices[i].name + sizeof("DLT_") - 1); 500 } 501 return (NULL); 502} 503 504const char * 505pcap_datalink_val_to_description(int dlt) 506{ 507 int i; 508 509 for (i = 0; dlt_choices[i].name != NULL; i++) { 510 if (dlt_choices[i].dlt == dlt) 511 return (dlt_choices[i].description); 512 } 513 return (NULL); 514} 515 516int 517pcap_snapshot(pcap_t *p) 518{ 519 return (p->snapshot); 520} 521 522int 523pcap_is_swapped(pcap_t *p) 524{ 525 return (p->sf.swapped); 526} 527 528int 529pcap_major_version(pcap_t *p) 530{ 531 return (p->sf.version_major); 532} 533 534int 535pcap_minor_version(pcap_t *p) 536{ 537 return (p->sf.version_minor); 538} 539 540FILE * 541pcap_file(pcap_t *p) 542{ 543 return (p->sf.rfile); 544} 545 546int 547pcap_fileno(pcap_t *p) 548{ 549#ifndef WIN32 550 return (p->fd); 551#else 552 if (p->adapter != NULL) 553 return ((int)(DWORD)p->adapter->hFile); 554 else 555 return (-1); 556#endif 557} 558 559#if !defined(WIN32) && !defined(MSDOS) 560int 561pcap_get_selectable_fd(pcap_t *p) 562{ 563 return (p->selectable_fd); 564} 565#endif 566 567void 568pcap_perror(pcap_t *p, char *prefix) 569{ 570 fprintf(stderr, "%s: %s\n", prefix, p->errbuf); 571} 572 573char * 574pcap_geterr(pcap_t *p) 575{ 576 return (p->errbuf); 577} 578 579int 580pcap_getnonblock(pcap_t *p, char *errbuf) 581{ 582 return p->getnonblock_op(p, errbuf); 583} 584 585/* 586 * Get the current non-blocking mode setting, under the assumption that 587 * it's just the standard POSIX non-blocking flag. 588 * 589 * We don't look at "p->nonblock", in case somebody tweaked the FD 590 * directly. 591 */ 592#if !defined(WIN32) && !defined(MSDOS) 593int 594pcap_getnonblock_fd(pcap_t *p, char *errbuf) 595{ 596 int fdflags; 597 598 fdflags = fcntl(p->fd, F_GETFL, 0); 599 if (fdflags == -1) { 600 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s", 601 pcap_strerror(errno)); 602 return (-1); 603 } 604 if (fdflags & O_NONBLOCK) 605 return (1); 606 else 607 return (0); 608} 609#endif 610 611int 612pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf) 613{ 614 return p->setnonblock_op(p, nonblock, errbuf); 615} 616 617#if !defined(WIN32) && !defined(MSDOS) 618/* 619 * Set non-blocking mode, under the assumption that it's just the 620 * standard POSIX non-blocking flag. (This can be called by the 621 * per-platform non-blocking-mode routine if that routine also 622 * needs to do some additional work.) 623 */ 624int 625pcap_setnonblock_fd(pcap_t *p, int nonblock, char *errbuf) 626{ 627 int fdflags; 628 629 fdflags = fcntl(p->fd, F_GETFL, 0); 630 if (fdflags == -1) { 631 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s", 632 pcap_strerror(errno)); 633 return (-1); 634 } 635 if (nonblock) 636 fdflags |= O_NONBLOCK; 637 else 638 fdflags &= ~O_NONBLOCK; 639 if (fcntl(p->fd, F_SETFL, fdflags) == -1) { 640 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_SETFL: %s", 641 pcap_strerror(errno)); 642 return (-1); 643 } 644 return (0); 645} 646#endif 647 648#ifdef WIN32 649/* 650 * Generate a string for the last Win32-specific error (i.e. an error generated when 651 * calling a Win32 API). 652 * For errors occurred during standard C calls, we still use pcap_strerror() 653 */ 654char * 655pcap_win32strerror(void) 656{ 657 DWORD error; 658 static char errbuf[PCAP_ERRBUF_SIZE+1]; 659 int errlen; 660 char *p; 661 662 error = GetLastError(); 663 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, errbuf, 664 PCAP_ERRBUF_SIZE, NULL); 665 666 /* 667 * "FormatMessage()" "helpfully" sticks CR/LF at the end of the 668 * message. Get rid of it. 669 */ 670 errlen = strlen(errbuf); 671 if (errlen >= 2) { 672 errbuf[errlen - 1] = '\0'; 673 errbuf[errlen - 2] = '\0'; 674 } 675 p = strchr(errbuf, '\0'); 676 snprintf (p, sizeof(errbuf)-(p-errbuf), " (%lu)", error); 677 return (errbuf); 678} 679#endif 680 681/* 682 * Not all systems have strerror(). 683 */
|
674char *
| 684const char *
|
675pcap_strerror(int errnum) 676{ 677#ifdef HAVE_STRERROR 678 return (strerror(errnum)); 679#else 680 extern int sys_nerr; 681 extern const char *const sys_errlist[]; 682 static char ebuf[20]; 683 684 if ((unsigned int)errnum < sys_nerr) 685 return ((char *)sys_errlist[errnum]); 686 (void)snprintf(ebuf, sizeof ebuf, "Unknown error: %d", errnum); 687 return(ebuf); 688#endif 689} 690 691int 692pcap_setfilter(pcap_t *p, struct bpf_program *fp) 693{ 694 return p->setfilter_op(p, fp); 695} 696 697/* 698 * Set direction flag, which controls whether we accept only incoming 699 * packets, only outgoing packets, or both. 700 * Note that, depending on the platform, some or all direction arguments 701 * might not be supported. 702 */ 703int 704pcap_setdirection(pcap_t *p, pcap_direction_t d) 705{ 706 if (p->setdirection_op == NULL) { 707 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 708 "Setting direction is not implemented on this platform"); 709 return -1; 710 } else 711 return p->setdirection_op(p, d); 712} 713 714int 715pcap_stats(pcap_t *p, struct pcap_stat *ps) 716{ 717 return p->stats_op(p, ps); 718} 719 720static int 721pcap_stats_dead(pcap_t *p, struct pcap_stat *ps _U_) 722{ 723 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 724 "Statistics aren't available from a pcap_open_dead pcap_t"); 725 return (-1); 726} 727 728void 729pcap_close_common(pcap_t *p) 730{ 731 if (p->buffer != NULL) 732 free(p->buffer); 733#if !defined(WIN32) && !defined(MSDOS) 734 if (p->fd >= 0) 735 close(p->fd); 736#endif 737} 738 739static void 740pcap_close_dead(pcap_t *p _U_) 741{ 742 /* Nothing to do. */ 743} 744 745pcap_t * 746pcap_open_dead(int linktype, int snaplen) 747{ 748 pcap_t *p; 749 750 p = malloc(sizeof(*p)); 751 if (p == NULL) 752 return NULL; 753 memset (p, 0, sizeof(*p)); 754 p->snapshot = snaplen; 755 p->linktype = linktype; 756 p->stats_op = pcap_stats_dead; 757 p->close_op = pcap_close_dead; 758 return p; 759} 760 761/* 762 * API compatible with WinPcap's "send a packet" routine - returns -1 763 * on error, 0 otherwise. 764 * 765 * XXX - what if we get a short write? 766 */ 767int 768pcap_sendpacket(pcap_t *p, const u_char *buf, int size) 769{ 770 if (p->inject_op(p, buf, size) == -1) 771 return (-1); 772 return (0); 773} 774 775/* 776 * API compatible with OpenBSD's "send a packet" routine - returns -1 on 777 * error, number of bytes written otherwise. 778 */ 779int 780pcap_inject(pcap_t *p, const void *buf, size_t size) 781{ 782 return (p->inject_op(p, buf, size)); 783} 784 785void 786pcap_close(pcap_t *p) 787{ 788 p->close_op(p); 789 if (p->dlt_list != NULL) 790 free(p->dlt_list); 791 pcap_freecode(&p->fcode); 792 free(p); 793} 794 795/* 796 * We make the version string static, and return a pointer to it, rather 797 * than exporting the version string directly. On at least some UNIXes, 798 * if you import data from a shared library into an program, the data is 799 * bound into the program binary, so if the string in the version of the 800 * library with which the program was linked isn't the same as the 801 * string in the version of the library with which the program is being 802 * run, various undesirable things may happen (warnings, the string 803 * being the one from the version of the library with which the program 804 * was linked, or even weirder things, such as the string being the one 805 * from the library but being truncated). 806 */ 807#ifdef HAVE_VERSION_H 808#include "version.h" 809#else
| 685pcap_strerror(int errnum) 686{ 687#ifdef HAVE_STRERROR 688 return (strerror(errnum)); 689#else 690 extern int sys_nerr; 691 extern const char *const sys_errlist[]; 692 static char ebuf[20]; 693 694 if ((unsigned int)errnum < sys_nerr) 695 return ((char *)sys_errlist[errnum]); 696 (void)snprintf(ebuf, sizeof ebuf, "Unknown error: %d", errnum); 697 return(ebuf); 698#endif 699} 700 701int 702pcap_setfilter(pcap_t *p, struct bpf_program *fp) 703{ 704 return p->setfilter_op(p, fp); 705} 706 707/* 708 * Set direction flag, which controls whether we accept only incoming 709 * packets, only outgoing packets, or both. 710 * Note that, depending on the platform, some or all direction arguments 711 * might not be supported. 712 */ 713int 714pcap_setdirection(pcap_t *p, pcap_direction_t d) 715{ 716 if (p->setdirection_op == NULL) { 717 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 718 "Setting direction is not implemented on this platform"); 719 return -1; 720 } else 721 return p->setdirection_op(p, d); 722} 723 724int 725pcap_stats(pcap_t *p, struct pcap_stat *ps) 726{ 727 return p->stats_op(p, ps); 728} 729 730static int 731pcap_stats_dead(pcap_t *p, struct pcap_stat *ps _U_) 732{ 733 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 734 "Statistics aren't available from a pcap_open_dead pcap_t"); 735 return (-1); 736} 737 738void 739pcap_close_common(pcap_t *p) 740{ 741 if (p->buffer != NULL) 742 free(p->buffer); 743#if !defined(WIN32) && !defined(MSDOS) 744 if (p->fd >= 0) 745 close(p->fd); 746#endif 747} 748 749static void 750pcap_close_dead(pcap_t *p _U_) 751{ 752 /* Nothing to do. */ 753} 754 755pcap_t * 756pcap_open_dead(int linktype, int snaplen) 757{ 758 pcap_t *p; 759 760 p = malloc(sizeof(*p)); 761 if (p == NULL) 762 return NULL; 763 memset (p, 0, sizeof(*p)); 764 p->snapshot = snaplen; 765 p->linktype = linktype; 766 p->stats_op = pcap_stats_dead; 767 p->close_op = pcap_close_dead; 768 return p; 769} 770 771/* 772 * API compatible with WinPcap's "send a packet" routine - returns -1 773 * on error, 0 otherwise. 774 * 775 * XXX - what if we get a short write? 776 */ 777int 778pcap_sendpacket(pcap_t *p, const u_char *buf, int size) 779{ 780 if (p->inject_op(p, buf, size) == -1) 781 return (-1); 782 return (0); 783} 784 785/* 786 * API compatible with OpenBSD's "send a packet" routine - returns -1 on 787 * error, number of bytes written otherwise. 788 */ 789int 790pcap_inject(pcap_t *p, const void *buf, size_t size) 791{ 792 return (p->inject_op(p, buf, size)); 793} 794 795void 796pcap_close(pcap_t *p) 797{ 798 p->close_op(p); 799 if (p->dlt_list != NULL) 800 free(p->dlt_list); 801 pcap_freecode(&p->fcode); 802 free(p); 803} 804 805/* 806 * We make the version string static, and return a pointer to it, rather 807 * than exporting the version string directly. On at least some UNIXes, 808 * if you import data from a shared library into an program, the data is 809 * bound into the program binary, so if the string in the version of the 810 * library with which the program was linked isn't the same as the 811 * string in the version of the library with which the program is being 812 * run, various undesirable things may happen (warnings, the string 813 * being the one from the version of the library with which the program 814 * was linked, or even weirder things, such as the string being the one 815 * from the library but being truncated). 816 */ 817#ifdef HAVE_VERSION_H 818#include "version.h" 819#else
|
810static const char pcap_version_string[] = "libpcap version 0.9[.x]";
| 820static const char pcap_version_string[] = "libpcap version 0.9.8";
|
811#endif 812 813#ifdef WIN32 814/* 815 * XXX - it'd be nice if we could somehow generate the WinPcap and libpcap 816 * version numbers when building WinPcap. (It'd be nice to do so for 817 * the packet.dll version number as well.) 818 */
| 821#endif 822 823#ifdef WIN32 824/* 825 * XXX - it'd be nice if we could somehow generate the WinPcap and libpcap 826 * version numbers when building WinPcap. (It'd be nice to do so for 827 * the packet.dll version number as well.) 828 */
|
819static const char wpcap_version_string[] = "3.1";
| 829static const char wpcap_version_string[] = "4.0";
|
820static const char pcap_version_string_fmt[] = 821 "WinPcap version %s, based on %s"; 822static const char pcap_version_string_packet_dll_fmt[] = 823 "WinPcap version %s (packet.dll version %s), based on %s"; 824static char *full_pcap_version_string; 825 826const char * 827pcap_lib_version(void) 828{ 829 char *packet_version_string; 830 size_t full_pcap_version_string_len; 831 832 if (full_pcap_version_string == NULL) { 833 /* 834 * Generate the version string. 835 */ 836 packet_version_string = PacketGetVersion(); 837 if (strcmp(wpcap_version_string, packet_version_string) == 0) { 838 /* 839 * WinPcap version string and packet.dll version 840 * string are the same; just report the WinPcap 841 * version. 842 */ 843 full_pcap_version_string_len = 844 (sizeof pcap_version_string_fmt - 4) + 845 strlen(wpcap_version_string) + 846 strlen(pcap_version_string); 847 full_pcap_version_string = 848 malloc(full_pcap_version_string_len); 849 sprintf(full_pcap_version_string, 850 pcap_version_string_fmt, wpcap_version_string, 851 pcap_version_string); 852 } else { 853 /* 854 * WinPcap version string and packet.dll version 855 * string are different; that shouldn't be the 856 * case (the two libraries should come from the 857 * same version of WinPcap), so we report both 858 * versions. 859 */ 860 full_pcap_version_string_len = 861 (sizeof pcap_version_string_packet_dll_fmt - 6) + 862 strlen(wpcap_version_string) + 863 strlen(packet_version_string) + 864 strlen(pcap_version_string); 865 full_pcap_version_string = malloc(full_pcap_version_string_len); 866 867 sprintf(full_pcap_version_string, 868 pcap_version_string_packet_dll_fmt, 869 wpcap_version_string, packet_version_string, 870 pcap_version_string); 871 } 872 } 873 return (full_pcap_version_string); 874} 875 876#elif defined(MSDOS) 877 878static char *full_pcap_version_string; 879 880const char * 881pcap_lib_version (void) 882{ 883 char *packet_version_string; 884 size_t full_pcap_version_string_len; 885 static char dospfx[] = "DOS-"; 886 887 if (full_pcap_version_string == NULL) { 888 /* 889 * Generate the version string. 890 */ 891 full_pcap_version_string_len = 892 sizeof dospfx + strlen(pcap_version_string); 893 full_pcap_version_string = 894 malloc(full_pcap_version_string_len); 895 strcpy(full_pcap_version_string, dospfx); 896 strcat(full_pcap_version_string, pcap_version_string); 897 } 898 return (full_pcap_version_string); 899} 900 901#else /* UN*X */ 902 903const char * 904pcap_lib_version(void) 905{ 906 return (pcap_version_string); 907} 908#endif
| 830static const char pcap_version_string_fmt[] = 831 "WinPcap version %s, based on %s"; 832static const char pcap_version_string_packet_dll_fmt[] = 833 "WinPcap version %s (packet.dll version %s), based on %s"; 834static char *full_pcap_version_string; 835 836const char * 837pcap_lib_version(void) 838{ 839 char *packet_version_string; 840 size_t full_pcap_version_string_len; 841 842 if (full_pcap_version_string == NULL) { 843 /* 844 * Generate the version string. 845 */ 846 packet_version_string = PacketGetVersion(); 847 if (strcmp(wpcap_version_string, packet_version_string) == 0) { 848 /* 849 * WinPcap version string and packet.dll version 850 * string are the same; just report the WinPcap 851 * version. 852 */ 853 full_pcap_version_string_len = 854 (sizeof pcap_version_string_fmt - 4) + 855 strlen(wpcap_version_string) + 856 strlen(pcap_version_string); 857 full_pcap_version_string = 858 malloc(full_pcap_version_string_len); 859 sprintf(full_pcap_version_string, 860 pcap_version_string_fmt, wpcap_version_string, 861 pcap_version_string); 862 } else { 863 /* 864 * WinPcap version string and packet.dll version 865 * string are different; that shouldn't be the 866 * case (the two libraries should come from the 867 * same version of WinPcap), so we report both 868 * versions. 869 */ 870 full_pcap_version_string_len = 871 (sizeof pcap_version_string_packet_dll_fmt - 6) + 872 strlen(wpcap_version_string) + 873 strlen(packet_version_string) + 874 strlen(pcap_version_string); 875 full_pcap_version_string = malloc(full_pcap_version_string_len); 876 877 sprintf(full_pcap_version_string, 878 pcap_version_string_packet_dll_fmt, 879 wpcap_version_string, packet_version_string, 880 pcap_version_string); 881 } 882 } 883 return (full_pcap_version_string); 884} 885 886#elif defined(MSDOS) 887 888static char *full_pcap_version_string; 889 890const char * 891pcap_lib_version (void) 892{ 893 char *packet_version_string; 894 size_t full_pcap_version_string_len; 895 static char dospfx[] = "DOS-"; 896 897 if (full_pcap_version_string == NULL) { 898 /* 899 * Generate the version string. 900 */ 901 full_pcap_version_string_len = 902 sizeof dospfx + strlen(pcap_version_string); 903 full_pcap_version_string = 904 malloc(full_pcap_version_string_len); 905 strcpy(full_pcap_version_string, dospfx); 906 strcat(full_pcap_version_string, pcap_version_string); 907 } 908 return (full_pcap_version_string); 909} 910 911#else /* UN*X */ 912 913const char * 914pcap_lib_version(void) 915{ 916 return (pcap_version_string); 917} 918#endif
|