1 /* $OpenBSD: pcap.c,v 1.12 2010/06/26 16:47:07 henning Exp $ */ 2 3 /* 4 * Copyright (c) 1993, 1994, 1995, 1996, 1997, 1998 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the Computer Systems 18 * Engineering Group at Lawrence Berkeley Laboratory. 19 * 4. Neither the name of the University nor of the Laboratory may be used 20 * to endorse or promote products derived from this software without 21 * specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/types.h> 37 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <string.h> 41 #include <unistd.h> 42 #include <errno.h> 43 #include <fcntl.h> 44 45 #ifdef HAVE_OS_PROTO_H 46 #include "os-proto.h" 47 #endif 48 49 #include "pcap-int.h" 50 51 static const char pcap_version_string[] = "OpenBSD libpcap"; 52 53 int 54 pcap_dispatch(pcap_t *p, int cnt, pcap_handler callback, u_char *user) 55 { 56 57 if (p->sf.rfile != NULL) 58 return (pcap_offline_read(p, cnt, callback, user)); 59 return (pcap_read(p, cnt, callback, user)); 60 } 61 62 int 63 pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user) 64 { 65 register int n; 66 67 for (;;) { 68 if (p->sf.rfile != NULL) 69 n = pcap_offline_read(p, cnt, callback, user); 70 else { 71 /* 72 * XXX keep reading until we get something 73 * (or an error occurs) 74 */ 75 do { 76 n = pcap_read(p, cnt, callback, user); 77 } while (n == 0); 78 } 79 if (n <= 0) 80 return (n); 81 if (cnt > 0) { 82 cnt -= n; 83 if (cnt <= 0) 84 return (0); 85 } 86 } 87 } 88 89 struct singleton { 90 struct pcap_pkthdr *hdr; 91 const u_char *pkt; 92 }; 93 94 95 static void 96 pcap_oneshot(u_char *userData, const struct pcap_pkthdr *h, const u_char *pkt) 97 { 98 struct singleton *sp = (struct singleton *)userData; 99 *sp->hdr = *h; 100 sp->pkt = pkt; 101 } 102 103 const u_char * 104 pcap_next(pcap_t *p, struct pcap_pkthdr *h) 105 { 106 struct singleton s; 107 108 s.hdr = h; 109 if (pcap_dispatch(p, 1, pcap_oneshot, (u_char*)&s) <= 0) 110 return (0); 111 return (s.pkt); 112 } 113 114 struct pkt_for_fakecallback { 115 struct pcap_pkthdr *hdr; 116 const u_char **pkt; 117 }; 118 119 static void 120 pcap_fakecallback(u_char *userData, const struct pcap_pkthdr *h, 121 const u_char *pkt) 122 { 123 struct pkt_for_fakecallback *sp = (struct pkt_for_fakecallback *)userData; 124 125 *sp->hdr = *h; 126 *sp->pkt = pkt; 127 } 128 129 int 130 pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header, 131 const u_char **pkt_data) 132 { 133 struct pkt_for_fakecallback s; 134 135 s.hdr = &p->pcap_header; 136 s.pkt = pkt_data; 137 138 /* Saves a pointer to the packet headers */ 139 *pkt_header= &p->pcap_header; 140 141 if (p->sf.rfile != NULL) { 142 int status; 143 144 /* We are on an offline capture */ 145 status = pcap_offline_read(p, 1, pcap_fakecallback, 146 (u_char *)&s); 147 148 /* 149 * Return codes for pcap_offline_read() are: 150 * - 0: EOF 151 * - -1: error 152 * - >1: OK 153 * The first one ('0') conflicts with the return code of 154 * 0 from pcap_read() meaning "no packets arrived before 155 * the timeout expired", so we map it to -2 so you can 156 * distinguish between an EOF from a savefile and a 157 * "no packets arrived before the timeout expired, try 158 * again" from a live capture. 159 */ 160 if (status == 0) 161 return (-2); 162 else 163 return (status); 164 } 165 166 /* 167 * Return codes for pcap_read() are: 168 * - 0: timeout 169 * - -1: error 170 * - -2: loop was broken out of with pcap_breakloop() 171 * - >1: OK 172 * The first one ('0') conflicts with the return code of 0 from 173 * pcap_offline_read() meaning "end of file". 174 */ 175 return (pcap_read(p, 1, pcap_fakecallback, (u_char *)&s)); 176 } 177 178 /* 179 * Force the loop in "pcap_read()" or "pcap_read_offline()" to terminate. 180 */ 181 void 182 pcap_breakloop(pcap_t *p) 183 { 184 p->break_loop = 1; 185 } 186 187 int 188 pcap_datalink(pcap_t *p) 189 { 190 return (p->linktype); 191 } 192 193 int 194 pcap_list_datalinks(pcap_t *p, int **dlt_buffer) 195 { 196 if (p->dlt_count == 0) { 197 /* 198 * We couldn't fetch the list of DLTs, which means 199 * this platform doesn't support changing the 200 * DLT for an interface. Return a list of DLTs 201 * containing only the DLT this device supports. 202 */ 203 *dlt_buffer = (int*)malloc(sizeof(**dlt_buffer)); 204 if (*dlt_buffer == NULL) { 205 (void)snprintf(p->errbuf, sizeof(p->errbuf), 206 "malloc: %s", pcap_strerror(errno)); 207 return (-1); 208 } 209 **dlt_buffer = p->linktype; 210 return (1); 211 } else { 212 *dlt_buffer = (int*)calloc(sizeof(**dlt_buffer), p->dlt_count); 213 if (*dlt_buffer == NULL) { 214 (void)snprintf(p->errbuf, sizeof(p->errbuf), 215 "malloc: %s", pcap_strerror(errno)); 216 return (-1); 217 } 218 (void)memcpy(*dlt_buffer, p->dlt_list, 219 sizeof(**dlt_buffer) * p->dlt_count); 220 return (p->dlt_count); 221 } 222 } 223 224 struct dlt_choice { 225 const char *name; 226 const char *description; 227 int dlt; 228 }; 229 230 static struct dlt_choice dlts[] = { 231 #define DLT_CHOICE(code, description) { #code, description, code } 232 DLT_CHOICE(DLT_NULL, "no link-layer encapsulation"), 233 DLT_CHOICE(DLT_EN10MB, "Ethernet (10Mb)"), 234 DLT_CHOICE(DLT_EN3MB, "Experimental Ethernet (3Mb)"), 235 DLT_CHOICE(DLT_AX25, "Amateur Radio AX.25"), 236 DLT_CHOICE(DLT_PRONET, "Proteon ProNET Token Ring"), 237 DLT_CHOICE(DLT_CHAOS, "Chaos"), 238 DLT_CHOICE(DLT_IEEE802, "IEEE 802 Networks"), 239 DLT_CHOICE(DLT_ARCNET, "ARCNET"), 240 DLT_CHOICE(DLT_SLIP, "Serial Line IP"), 241 DLT_CHOICE(DLT_PPP, "Point-to-point Protocol"), 242 DLT_CHOICE(DLT_FDDI, "FDDI"), 243 DLT_CHOICE(DLT_ATM_RFC1483, "LLC/SNAP encapsulated atm"), 244 DLT_CHOICE(DLT_LOOP, "loopback type (af header)"), 245 DLT_CHOICE(DLT_ENC, "IPSEC enc type (af header, spi, flags)"), 246 DLT_CHOICE(DLT_RAW, "raw IP"), 247 DLT_CHOICE(DLT_SLIP_BSDOS, "BSD/OS Serial Line IP"), 248 DLT_CHOICE(DLT_PPP_BSDOS, "BSD/OS Point-to-point Protocol"), 249 DLT_CHOICE(DLT_PFSYNC, "Packet filter state syncing"), 250 DLT_CHOICE(DLT_PPP_ETHER, "PPP over Ethernet; session only w/o ether header"), 251 DLT_CHOICE(DLT_IEEE802_11, "IEEE 802.11 wireless"), 252 DLT_CHOICE(DLT_PFLOG, "Packet filter logging, by pcap people"), 253 DLT_CHOICE(DLT_IEEE802_11_RADIO, "IEEE 802.11 plus WLAN header"), 254 #undef DLT_CHOICE 255 { NULL, NULL, -1} 256 }; 257 258 int 259 pcap_datalink_name_to_val(const char *name) 260 { 261 int i; 262 263 for (i = 0; dlts[i].name != NULL; i++) { 264 /* Skip leading "DLT_" */ 265 if (strcasecmp(dlts[i].name + 4, name) == 0) 266 return (dlts[i].dlt); 267 } 268 return (-1); 269 } 270 271 const char * 272 pcap_datalink_val_to_name(int dlt) 273 { 274 int i; 275 276 for (i = 0; dlts[i].name != NULL; i++) { 277 if (dlts[i].dlt == dlt) 278 return (dlts[i].name + 4); /* Skip leading "DLT_" */ 279 } 280 return (NULL); 281 } 282 283 const char * 284 pcap_datalink_val_to_description(int dlt) 285 { 286 int i; 287 288 for (i = 0; dlts[i].name != NULL; i++) { 289 if (dlts[i].dlt == dlt) 290 return (dlts[i].description); 291 } 292 return (NULL); 293 } 294 295 int 296 pcap_snapshot(pcap_t *p) 297 { 298 return (p->snapshot); 299 } 300 301 int 302 pcap_is_swapped(pcap_t *p) 303 { 304 return (p->sf.swapped); 305 } 306 307 int 308 pcap_major_version(pcap_t *p) 309 { 310 return (p->sf.version_major); 311 } 312 313 int 314 pcap_minor_version(pcap_t *p) 315 { 316 return (p->sf.version_minor); 317 } 318 319 FILE * 320 pcap_file(pcap_t *p) 321 { 322 return (p->sf.rfile); 323 } 324 325 int 326 pcap_fileno(pcap_t *p) 327 { 328 return (p->fd); 329 } 330 331 void 332 pcap_perror(pcap_t *p, char *prefix) 333 { 334 fprintf(stderr, "%s: %s\n", prefix, p->errbuf); 335 } 336 337 int 338 pcap_get_selectable_fd(pcap_t *p) 339 { 340 return (p->fd); 341 } 342 343 char * 344 pcap_geterr(pcap_t *p) 345 { 346 return (p->errbuf); 347 } 348 349 int 350 pcap_getnonblock(pcap_t *p, char *errbuf) 351 { 352 int fdflags; 353 354 fdflags = fcntl(p->fd, F_GETFL, 0); 355 if (fdflags == -1) { 356 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s", 357 pcap_strerror(errno)); 358 return (-1); 359 } 360 if (fdflags & O_NONBLOCK) 361 return (1); 362 else 363 return (0); 364 } 365 366 int 367 pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf) 368 { 369 int fdflags; 370 371 fdflags = fcntl(p->fd, F_GETFL, 0); 372 if (fdflags == -1) { 373 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s", 374 pcap_strerror(errno)); 375 return (-1); 376 } 377 if (nonblock) 378 fdflags |= O_NONBLOCK; 379 else 380 fdflags &= ~O_NONBLOCK; 381 if (fcntl(p->fd, F_SETFL, fdflags) == -1) { 382 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_SETFL: %s", 383 pcap_strerror(errno)); 384 return (-1); 385 } 386 return (0); 387 } 388 389 /* 390 * Not all systems have strerror(). 391 */ 392 char * 393 pcap_strerror(int errnum) 394 { 395 #ifdef HAVE_STRERROR 396 return (strerror(errnum)); 397 #else 398 extern int sys_nerr; 399 extern const char *const sys_errlist[]; 400 static char ebuf[20]; 401 402 if ((unsigned int)errnum < sys_nerr) 403 return ((char *)sys_errlist[errnum]); 404 (void)snprintf(ebuf, sizeof ebuf, "Unknown error: %d", errnum); 405 return(ebuf); 406 #endif 407 } 408 409 pcap_t * 410 pcap_open_dead(int linktype, int snaplen) 411 { 412 pcap_t *p; 413 414 p = malloc(sizeof(*p)); 415 if (p == NULL) 416 return NULL; 417 memset (p, 0, sizeof(*p)); 418 p->snapshot = snaplen; 419 p->linktype = linktype; 420 p->fd = -1; 421 return p; 422 } 423 424 void 425 pcap_close(pcap_t *p) 426 { 427 /*XXX*/ 428 if (p->fd >= 0) 429 close(p->fd); 430 if (p->sf.rfile != NULL) { 431 (void)fclose(p->sf.rfile); 432 if (p->sf.base != NULL) 433 free(p->sf.base); 434 } else if (p->buffer != NULL) 435 free(p->buffer); 436 #ifdef linux 437 if (p->md.device != NULL) 438 free(p->md.device); 439 #endif 440 pcap_freecode(&p->fcode); 441 if (p->dlt_list != NULL) 442 free(p->dlt_list); 443 free(p); 444 } 445 446 const char * 447 pcap_lib_version(void) 448 { 449 return (pcap_version_string); 450 } 451 452