00001 /* 00002 * Copyright (c) 1993, 1994, 1995, 1996, 1997, 1998 00003 * The Regents of the University of California. All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 3. All advertising materials mentioning features or use of this software 00014 * must display the following acknowledgement: 00015 * This product includes software developed by the Computer Systems 00016 * Engineering Group at Lawrence Berkeley Laboratory. 00017 * 4. Neither the name of the University nor of the Laboratory may be used 00018 * to endorse or promote products derived from this software without 00019 * specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 00022 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00023 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00024 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 00025 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00026 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00027 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00028 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00029 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00030 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00031 * SUCH DAMAGE. 00032 */ 00033 00034 #ifndef lint 00035 static const char rcsid[] _U_ = 00036 "@(#) $Header: /tcpdump/master/libpcap/pcap.c,v 1.70 2003/12/18 23:32:34 guy Exp $ (LBL)"; 00037 #endif 00038 00039 #ifdef HAVE_CONFIG_H 00040 #include "config.h" 00041 #endif 00042 00043 #ifdef WIN32 00044 #include <pcap-stdinc.h> 00045 #else /* WIN32 */ 00046 #include <sys/types.h> 00047 #endif /* WIN32 */ 00048 00049 #include <stdio.h> 00050 #include <stdlib.h> 00051 #include <string.h> 00052 #ifndef WIN32 00053 #include <unistd.h> 00054 #endif 00055 #include <fcntl.h> 00056 #include <errno.h> 00057 00058 #ifdef HAVE_OS_PROTO_H 00059 #include "os-proto.h" 00060 #endif 00061 00062 #include "pcap-int.h" 00063 00064 #ifdef HAVE_DAG_API 00065 #include <dagnew.h> 00066 #include <dagapi.h> 00067 #endif 00068 00069 #ifdef HAVE_REMOTE 00070 #include <pcap-remote.h> 00071 #endif 00072 00073 00074 int 00075 pcap_dispatch(pcap_t *p, int cnt, pcap_handler callback, u_char *user) 00076 { 00077 00078 return p->read_op(p, cnt, callback, user); 00079 } 00080 00081 /* 00082 * XXX - is this necessary? 00083 */ 00084 int 00085 pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) 00086 { 00087 00088 return p->read_op(p, cnt, callback, user); 00089 } 00090 00091 int 00092 pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user) 00093 { 00094 register int n; 00095 00096 for (;;) { 00097 if (p->sf.rfile != NULL) { 00098 /* 00099 * 0 means EOF, so don't loop if we get 0. 00100 */ 00101 n = pcap_offline_read(p, cnt, callback, user); 00102 } else { 00103 /* 00104 * XXX keep reading until we get something 00105 * (or an error occurs) 00106 */ 00107 do { 00108 n = p->read_op(p, cnt, callback, user); 00109 } while (n == 0); 00110 } 00111 if (n <= 0) 00112 return (n); 00113 if (cnt > 0) { 00114 cnt -= n; 00115 if (cnt <= 0) 00116 return (0); 00117 } 00118 } 00119 } 00120 00121 struct singleton { 00122 struct pcap_pkthdr *hdr; 00123 const u_char *pkt; 00124 }; 00125 00126 00127 static void 00128 pcap_oneshot(u_char *userData, const struct pcap_pkthdr *h, const u_char *pkt) 00129 { 00130 struct singleton *sp = (struct singleton *)userData; 00131 *sp->hdr = *h; 00132 sp->pkt = pkt; 00133 } 00134 00135 const u_char * 00136 pcap_next(pcap_t *p, struct pcap_pkthdr *h) 00137 { 00138 struct singleton s; 00139 00140 s.hdr = h; 00141 if (pcap_dispatch(p, 1, pcap_oneshot, (u_char*)&s) <= 0) 00142 return (0); 00143 return (s.pkt); 00144 } 00145 00146 struct pkt_for_fakecallback { 00147 struct pcap_pkthdr *hdr; 00148 const u_char **pkt; 00149 }; 00150 00151 static void 00152 pcap_fakecallback(u_char *userData, const struct pcap_pkthdr *h, 00153 const u_char *pkt) 00154 { 00155 struct pkt_for_fakecallback *sp = (struct pkt_for_fakecallback *)userData; 00156 00157 *sp->hdr = *h; 00158 *sp->pkt = pkt; 00159 } 00160 00161 int 00162 pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header, 00163 const u_char **pkt_data) 00164 { 00165 struct pkt_for_fakecallback s; 00166 00167 s.hdr = &p->pcap_header; 00168 s.pkt = pkt_data; 00169 00170 /* Saves a pointer to the packet headers */ 00171 *pkt_header= &p->pcap_header; 00172 00173 #ifdef HAVE_REMOTE 00174 /* Checks the capture type */ 00175 if (p->rmt_clientside) 00176 { 00177 /* We are on an remote capture */ 00178 if (!p->rmt_capstarted) 00179 { 00180 // if the capture has not started yet, please start it 00181 if (pcap_startcapture_remote(p) ) 00182 return -1; 00183 } 00184 00185 return pcap_read_nocb_remote(p, pkt_header, (u_char **) pkt_data); 00186 } 00187 #endif /* HAVE_REMOTE */ 00188 00189 if (p->sf.rfile != NULL) { 00190 int status; 00191 00192 /* We are on an offline capture */ 00193 status = pcap_offline_read(p, 1, pcap_fakecallback, 00194 (u_char *)&s); 00195 00196 /* 00197 * Return codes for pcap_offline_read() are: 00198 * - 0: EOF 00199 * - -1: error 00200 * - >1: OK 00201 * The first one ('0') conflicts with the return code of 00202 * 0 from pcap_read() meaning "no packets arrived before 00203 * the timeout expired", so we map it to -2 so you can 00204 * distinguish between an EOF from a savefile and a 00205 * "no packets arrived before the timeout expired, try 00206 * again" from a live capture. 00207 */ 00208 if (status == 0) 00209 return (-2); 00210 else 00211 return (status); 00212 } 00213 00214 /* 00215 * Return codes for pcap_read() are: 00216 * - 0: timeout 00217 * - -1: error 00218 * - -2: loop was broken out of with pcap_breakloop() 00219 * - >1: OK 00220 * The first one ('0') conflicts with the return code of 0 from 00221 * pcap_offline_read() meaning "end of file". 00222 */ 00223 return (p->read_op(p, 1, pcap_fakecallback, (u_char *)&s)); 00224 } 00225 00226 /* 00227 * Force the loop in "pcap_read()" or "pcap_read_offline()" to terminate. 00228 */ 00229 void 00230 pcap_breakloop(pcap_t *p) 00231 { 00232 p->break_loop = 1; 00233 } 00234 00235 int 00236 pcap_datalink(pcap_t *p) 00237 { 00238 return (p->linktype); 00239 } 00240 00241 int 00242 pcap_list_datalinks(pcap_t *p, int **dlt_buffer) 00243 { 00244 if (p->dlt_count == 0) { 00245 /* 00246 * We couldn't fetch the list of DLTs, which means 00247 * this platform doesn't support changing the 00248 * DLT for an interface. Return a list of DLTs 00249 * containing only the DLT this device supports. 00250 */ 00251 *dlt_buffer = (int*)malloc(sizeof(**dlt_buffer)); 00252 if (*dlt_buffer == NULL) { 00253 (void)snprintf(p->errbuf, sizeof(p->errbuf), 00254 "malloc: %s", pcap_strerror(errno)); 00255 return (-1); 00256 } 00257 **dlt_buffer = p->linktype; 00258 return (1); 00259 } else { 00260 *dlt_buffer = (int*)malloc(sizeof(**dlt_buffer) * p->dlt_count); 00261 if (*dlt_buffer == NULL) { 00262 (void)snprintf(p->errbuf, sizeof(p->errbuf), 00263 "malloc: %s", pcap_strerror(errno)); 00264 return (-1); 00265 } 00266 (void)memcpy(*dlt_buffer, p->dlt_list, 00267 sizeof(**dlt_buffer) * p->dlt_count); 00268 return (p->dlt_count); 00269 } 00270 } 00271 00272 int 00273 pcap_set_datalink(pcap_t *p, int dlt) 00274 { 00275 int i; 00276 const char *dlt_name; 00277 00278 if (p->dlt_count == 0 || p->set_datalink_op == NULL) { 00279 /* 00280 * We couldn't fetch the list of DLTs, or we don't 00281 * have a "set datalink" operation, which means 00282 * this platform doesn't support changing the 00283 * DLT for an interface. Check whether the new 00284 * DLT is the one this interface supports. 00285 */ 00286 if (p->linktype != dlt) 00287 goto unsupported; 00288 00289 /* 00290 * It is, so there's nothing we need to do here. 00291 */ 00292 return (0); 00293 } 00294 for (i = 0; i < p->dlt_count; i++) 00295 if (p->dlt_list[i] == dlt) 00296 break; 00297 if (i >= p->dlt_count) 00298 goto unsupported; 00299 if (p->dlt_count == 2 && p->dlt_list[0] == DLT_EN10MB && 00300 dlt == DLT_DOCSIS) { 00301 /* 00302 * This is presumably an Ethernet device, as the first 00303 * link-layer type it offers is DLT_EN10MB, and the only 00304 * other type it offers is DLT_DOCSIS. That means that 00305 * we can't tell the driver to supply DOCSIS link-layer 00306 * headers - we're just pretending that's what we're 00307 * getting, as, presumably, we're capturing on a dedicated 00308 * link to a Cisco Cable Modem Termination System, and 00309 * it's putting raw DOCSIS frames on the wire inside low-level 00310 * Ethernet framing. 00311 */ 00312 p->linktype = dlt; 00313 return (0); 00314 } 00315 if (p->set_datalink_op(p, dlt) == -1) 00316 return (-1); 00317 p->linktype = dlt; 00318 return (0); 00319 00320 unsupported: 00321 dlt_name = pcap_datalink_val_to_name(dlt); 00322 if (dlt_name != NULL) { 00323 (void) snprintf(p->errbuf, sizeof(p->errbuf), 00324 "%s is not one of the DLTs supported by this device", 00325 dlt_name); 00326 } else { 00327 (void) snprintf(p->errbuf, sizeof(p->errbuf), 00328 "DLT %d is not one of the DLTs supported by this device", 00329 dlt); 00330 } 00331 return (-1); 00332 } 00333 00334 struct dlt_choice { 00335 const char *name; 00336 const char *description; 00337 int dlt; 00338 }; 00339 00340 #define DLT_CHOICE(code, description) { #code, description, code } 00341 #define DLT_CHOICE_SENTINEL { NULL, NULL, 0 } 00342 00343 static struct dlt_choice dlt_choices[] = { 00344 DLT_CHOICE(DLT_NULL, "BSD loopback"), 00345 DLT_CHOICE(DLT_EN10MB, "Ethernet"), 00346 DLT_CHOICE(DLT_IEEE802, "Token ring"), 00347 DLT_CHOICE(DLT_ARCNET, "ARCNET"), 00348 DLT_CHOICE(DLT_SLIP, "SLIP"), 00349 DLT_CHOICE(DLT_PPP, "PPP"), 00350 DLT_CHOICE(DLT_FDDI, "FDDI"), 00351 DLT_CHOICE(DLT_ATM_RFC1483, "RFC 1483 IP-over-ATM"), 00352 DLT_CHOICE(DLT_RAW, "Raw IP"), 00353 DLT_CHOICE(DLT_SLIP_BSDOS, "BSD/OS SLIP"), 00354 DLT_CHOICE(DLT_PPP_BSDOS, "BSD/OS PPP"), 00355 DLT_CHOICE(DLT_ATM_CLIP, "Linux Classical IP-over-ATM"), 00356 DLT_CHOICE(DLT_PPP_SERIAL, "PPP over serial"), 00357 DLT_CHOICE(DLT_PPP_ETHER, "PPPoE"), 00358 DLT_CHOICE(DLT_C_HDLC, "Cisco HDLC"), 00359 DLT_CHOICE(DLT_IEEE802_11, "802.11"), 00360 DLT_CHOICE(DLT_FRELAY, "Frame Relay"), 00361 DLT_CHOICE(DLT_LOOP, "OpenBSD loopback"), 00362 DLT_CHOICE(DLT_ENC, "OpenBSD encapsulated IP"), 00363 DLT_CHOICE(DLT_LINUX_SLL, "Linux cooked"), 00364 DLT_CHOICE(DLT_LTALK, "Localtalk"), 00365 DLT_CHOICE(DLT_PFLOG, "OpenBSD pflog file"), 00366 DLT_CHOICE(DLT_PRISM_HEADER, "802.11 plus Prism header"), 00367 DLT_CHOICE(DLT_IP_OVER_FC, "RFC 2625 IP-over-Fibre Channel"), 00368 DLT_CHOICE(DLT_SUNATM, "Sun raw ATM"), 00369 DLT_CHOICE(DLT_IEEE802_11_RADIO, "802.11 plus radio information header"), 00370 DLT_CHOICE(DLT_ARCNET_LINUX, "Linux ARCNET"), 00371 DLT_CHOICE(DLT_DOCSIS, "DOCSIS"), 00372 DLT_CHOICE(DLT_LINUX_IRDA, "Linux IrDA"), 00373 DLT_CHOICE_SENTINEL 00374 }; 00375 00376 /* 00377 * This array is designed for mapping upper and lower case letter 00378 * together for a case independent comparison. The mappings are 00379 * based upon ascii character sequences. 00380 */ 00381 static const u_char charmap[] = { 00382 (u_char)'\000', (u_char)'\001', (u_char)'\002', (u_char)'\003', 00383 (u_char)'\004', (u_char)'\005', (u_char)'\006', (u_char)'\007', 00384 (u_char)'\010', (u_char)'\011', (u_char)'\012', (u_char)'\013', 00385 (u_char)'\014', (u_char)'\015', (u_char)'\016', (u_char)'\017', 00386 (u_char)'\020', (u_char)'\021', (u_char)'\022', (u_char)'\023', 00387 (u_char)'\024', (u_char)'\025', (u_char)'\026', (u_char)'\027', 00388 (u_char)'\030', (u_char)'\031', (u_char)'\032', (u_char)'\033', 00389 (u_char)'\034', (u_char)'\035', (u_char)'\036', (u_char)'\037', 00390 (u_char)'\040', (u_char)'\041', (u_char)'\042', (u_char)'\043', 00391 (u_char)'\044', (u_char)'\045', (u_char)'\046', (u_char)'\047', 00392 (u_char)'\050', (u_char)'\051', (u_char)'\052', (u_char)'\053', 00393 (u_char)'\054', (u_char)'\055', (u_char)'\056', (u_char)'\057', 00394 (u_char)'\060', (u_char)'\061', (u_char)'\062', (u_char)'\063', 00395 (u_char)'\064', (u_char)'\065', (u_char)'\066', (u_char)'\067', 00396 (u_char)'\070', (u_char)'\071', (u_char)'\072', (u_char)'\073', 00397 (u_char)'\074', (u_char)'\075', (u_char)'\076', (u_char)'\077', 00398 (u_char)'\100', (u_char)'\141', (u_char)'\142', (u_char)'\143', 00399 (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147', 00400 (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153', 00401 (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157', 00402 (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163', 00403 (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167', 00404 (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\133', 00405 (u_char)'\134', (u_char)'\135', (u_char)'\136', (u_char)'\137', 00406 (u_char)'\140', (u_char)'\141', (u_char)'\142', (u_char)'\143', 00407 (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147', 00408 (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153', 00409 (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157', 00410 (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163', 00411 (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167', 00412 (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\173', 00413 (u_char)'\174', (u_char)'\175', (u_char)'\176', (u_char)'\177', 00414 (u_char)'\200', (u_char)'\201', (u_char)'\202', (u_char)'\203', 00415 (u_char)'\204', (u_char)'\205', (u_char)'\206', (u_char)'\207', 00416 (u_char)'\210', (u_char)'\211', (u_char)'\212', (u_char)'\213', 00417 (u_char)'\214', (u_char)'\215', (u_char)'\216', (u_char)'\217', 00418 (u_char)'\220', (u_char)'\221', (u_char)'\222', (u_char)'\223', 00419 (u_char)'\224', (u_char)'\225', (u_char)'\226', (u_char)'\227', 00420 (u_char)'\230', (u_char)'\231', (u_char)'\232', (u_char)'\233', 00421 (u_char)'\234', (u_char)'\235', (u_char)'\236', (u_char)'\237', 00422 (u_char)'\240', (u_char)'\241', (u_char)'\242', (u_char)'\243', 00423 (u_char)'\244', (u_char)'\245', (u_char)'\246', (u_char)'\247', 00424 (u_char)'\250', (u_char)'\251', (u_char)'\252', (u_char)'\253', 00425 (u_char)'\254', (u_char)'\255', (u_char)'\256', (u_char)'\257', 00426 (u_char)'\260', (u_char)'\261', (u_char)'\262', (u_char)'\263', 00427 (u_char)'\264', (u_char)'\265', (u_char)'\266', (u_char)'\267', 00428 (u_char)'\270', (u_char)'\271', (u_char)'\272', (u_char)'\273', 00429 (u_char)'\274', (u_char)'\275', (u_char)'\276', (u_char)'\277', 00430 (u_char)'\300', (u_char)'\341', (u_char)'\342', (u_char)'\343', 00431 (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347', 00432 (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353', 00433 (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357', 00434 (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363', 00435 (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367', 00436 (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\333', 00437 (u_char)'\334', (u_char)'\335', (u_char)'\336', (u_char)'\337', 00438 (u_char)'\340', (u_char)'\341', (u_char)'\342', (u_char)'\343', 00439 (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347', 00440 (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353', 00441 (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357', 00442 (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363', 00443 (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367', 00444 (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\373', 00445 (u_char)'\374', (u_char)'\375', (u_char)'\376', (u_char)'\377', 00446 }; 00447 00448 int 00449 pcap_strcasecmp(const char *s1, const char *s2) 00450 { 00451 register const u_char *cm = charmap, 00452 *us1 = (u_char *)s1, 00453 *us2 = (u_char *)s2; 00454 00455 while (cm[*us1] == cm[*us2++]) 00456 if (*us1++ == '\0') 00457 return(0); 00458 return (cm[*us1] - cm[*--us2]); 00459 } 00460 00461 int 00462 pcap_datalink_name_to_val(const char *name) 00463 { 00464 int i; 00465 00466 for (i = 0; dlt_choices[i].name != NULL; i++) { 00467 if (pcap_strcasecmp(dlt_choices[i].name + sizeof("DLT_") - 1, 00468 name) == 0) 00469 return (dlt_choices[i].dlt); 00470 } 00471 return (-1); 00472 } 00473 00474 const char * 00475 pcap_datalink_val_to_name(int dlt) 00476 { 00477 int i; 00478 00479 for (i = 0; dlt_choices[i].name != NULL; i++) { 00480 if (dlt_choices[i].dlt == dlt) 00481 return (dlt_choices[i].name + sizeof("DLT_") - 1); 00482 } 00483 return (NULL); 00484 } 00485 00486 const char * 00487 pcap_datalink_val_to_description(int dlt) 00488 { 00489 int i; 00490 00491 for (i = 0; dlt_choices[i].name != NULL; i++) { 00492 if (dlt_choices[i].dlt == dlt) 00493 return (dlt_choices[i].description); 00494 } 00495 return (NULL); 00496 } 00497 00498 int 00499 pcap_snapshot(pcap_t *p) 00500 { 00501 return (p->snapshot); 00502 } 00503 00504 int 00505 pcap_is_swapped(pcap_t *p) 00506 { 00507 return (p->sf.swapped); 00508 } 00509 00510 int 00511 pcap_major_version(pcap_t *p) 00512 { 00513 return (p->sf.version_major); 00514 } 00515 00516 int 00517 pcap_minor_version(pcap_t *p) 00518 { 00519 return (p->sf.version_minor); 00520 } 00521 00522 FILE * 00523 pcap_file(pcap_t *p) 00524 { 00525 return (p->sf.rfile); 00526 } 00527 00528 int 00529 pcap_fileno(pcap_t *p) 00530 { 00531 #ifdef HAVE_REMOTE 00532 if (p->rmt_clientside) 00533 return(p->rmt_sockdata); 00534 #endif /* HAVE_REMOTE */ 00535 00536 #ifndef WIN32 00537 return (p->fd); 00538 #else 00539 if (p->adapter != NULL) 00540 return ((int)(DWORD)p->adapter->hFile); 00541 else 00542 return (-1); 00543 #endif 00544 } 00545 00546 #ifndef WIN32 00547 int 00548 pcap_get_selectable_fd(pcap_t *p) 00549 { 00550 return (p->selectable_fd); 00551 } 00552 #endif 00553 00554 void 00555 pcap_perror(pcap_t *p, char *prefix) 00556 { 00557 fprintf(stderr, "%s: %s\n", prefix, p->errbuf); 00558 } 00559 00560 char * 00561 pcap_geterr(pcap_t *p) 00562 { 00563 return (p->errbuf); 00564 } 00565 00566 int 00567 pcap_getnonblock(pcap_t *p, char *errbuf) 00568 { 00569 return p->getnonblock_op(p, errbuf); 00570 } 00571 00572 /* 00573 * Get the current non-blocking mode setting, under the assumption that 00574 * it's just the standard POSIX non-blocking flag. 00575 * 00576 * We don't look at "p->nonblock", in case somebody tweaked the FD 00577 * directly. 00578 */ 00579 #ifndef WIN32 00580 int 00581 pcap_getnonblock_fd(pcap_t *p, char *errbuf) 00582 { 00583 int fdflags; 00584 00585 fdflags = fcntl(p->fd, F_GETFL, 0); 00586 if (fdflags == -1) { 00587 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s", 00588 pcap_strerror(errno)); 00589 return (-1); 00590 } 00591 if (fdflags & O_NONBLOCK) 00592 return (1); 00593 else 00594 return (0); 00595 } 00596 #endif 00597 00598 int 00599 pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf) 00600 { 00601 return p->setnonblock_op(p, nonblock, errbuf); 00602 } 00603 00604 #ifndef WIN32 00605 /* 00606 * Set non-blocking mode, under the assumption that it's just the 00607 * standard POSIX non-blocking flag. (This can be called by the 00608 * per-platform non-blocking-mode routine if that routine also 00609 * needs to do some additional work.) 00610 */ 00611 int 00612 pcap_setnonblock_fd(pcap_t *p, int nonblock, char *errbuf) 00613 { 00614 int fdflags; 00615 00616 fdflags = fcntl(p->fd, F_GETFL, 0); 00617 if (fdflags == -1) { 00618 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s", 00619 pcap_strerror(errno)); 00620 return (-1); 00621 } 00622 if (nonblock) 00623 fdflags |= O_NONBLOCK; 00624 else 00625 fdflags &= ~O_NONBLOCK; 00626 if (fcntl(p->fd, F_SETFL, fdflags) == -1) { 00627 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_SETFL: %s", 00628 pcap_strerror(errno)); 00629 return (-1); 00630 } 00631 return (0); 00632 } 00633 #endif 00634 00635 #ifdef WIN32 00636 /* 00637 * Generate a string for the last Win32-specific error (i.e. an error generated when 00638 * calling a Win32 API). 00639 * For errors occurred during standard C calls, we still use pcap_strerror() 00640 */ 00641 char * 00642 pcap_win32strerror(void) 00643 { 00644 DWORD error; 00645 static char errbuf[PCAP_ERRBUF_SIZE+1]; 00646 int errlen; 00647 00648 error = GetLastError(); 00649 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, errbuf, 00650 PCAP_ERRBUF_SIZE, NULL); 00651 00652 /* 00653 * "FormatMessage()" "helpfully" sticks CR/LF at the end of the 00654 * message. Get rid of it. 00655 */ 00656 errlen = strlen(errbuf); 00657 if (errlen >= 2) { 00658 errbuf[errlen - 1] = '\0'; 00659 errbuf[errlen - 2] = '\0'; 00660 } 00661 return (errbuf); 00662 } 00663 #endif 00664 00665 /* 00666 * Not all systems have strerror(). 00667 */ 00668 char * 00669 pcap_strerror(int errnum) 00670 { 00671 #ifdef HAVE_STRERROR 00672 return (strerror(errnum)); 00673 #else 00674 extern int sys_nerr; 00675 extern const char *const sys_errlist[]; 00676 static char ebuf[20]; 00677 00678 if ((unsigned int)errnum < sys_nerr) 00679 return ((char *)sys_errlist[errnum]); 00680 (void)snprintf(ebuf, sizeof ebuf, "Unknown error: %d", errnum); 00681 return(ebuf); 00682 #endif 00683 } 00684 00685 int 00686 pcap_setfilter(pcap_t *p, struct bpf_program *fp) 00687 { 00688 return p->setfilter_op(p, fp); 00689 } 00690 00691 int 00692 pcap_stats(pcap_t *p, struct pcap_stat *ps) 00693 { 00694 return p->stats_op(p, ps); 00695 } 00696 00697 static int 00698 pcap_stats_dead(pcap_t *p, struct pcap_stat *ps) 00699 { 00700 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 00701 "Statistics aren't available from a pcap_open_dead pcap_t"); 00702 return (-1); 00703 } 00704 00705 static void 00706 pcap_close_dead(pcap_t *p) 00707 { 00708 /* Nothing to do. */ 00709 } 00710 00711 pcap_t * 00712 pcap_open_dead(int linktype, int snaplen) 00713 { 00714 pcap_t *p; 00715 00716 p = malloc(sizeof(*p)); 00717 if (p == NULL) 00718 return NULL; 00719 memset (p, 0, sizeof(*p)); 00720 p->snapshot = snaplen; 00721 p->linktype = linktype; 00722 p->stats_op = pcap_stats_dead; 00723 p->close_op = pcap_close_dead; 00724 return p; 00725 } 00726 00727 void 00728 pcap_close(pcap_t *p) 00729 { 00730 p->close_op(p); 00731 if (p->dlt_list != NULL) 00732 free(p->dlt_list); 00733 pcap_freecode(&p->fcode); 00734 free(p); 00735 } 00736 00737 /* 00738 * We make the version string static, and return a pointer to it, rather 00739 * than exporting the version string directly. On at least some UNIXes, 00740 * if you import data from a shared library into an program, the data is 00741 * bound into the program binary, so if the string in the version of the 00742 * library with which the program was linked isn't the same as the 00743 * string in the version of the library with which the program is being 00744 * run, various undesirable things may happen (warnings, the string 00745 * being the one from the version of the library with which the program 00746 * was linked, or even weirder things, such as the string being the one 00747 * from the library but being truncated). 00748 */ 00749 #ifdef WIN32 00750 /* 00751 * XXX - it'd be nice if we could somehow generate the WinPcap and libpcap 00752 * version numbers when building WinPcap. (It'd be nice to do so for 00753 * the packet.dll version number as well.) 00754 */ 00755 static const char wpcap_version_string[] = "3.0"; 00756 static const char pcap_version_string_fmt[] = 00757 "WinPcap version %s, based on libpcap version 0.8"; 00758 static const char pcap_version_string_packet_dll_fmt[] = 00759 "WinPcap version %s (packet.dll version %s), based on libpcap version 0.8"; 00760 static char *pcap_version_string; 00761 00762 const char * 00763 pcap_lib_version(void) 00764 { 00765 char *packet_version_string; 00766 size_t pcap_version_string_len; 00767 00768 if (pcap_version_string == NULL) { 00769 /* 00770 * Generate the version string. 00771 */ 00772 packet_version_string = PacketGetVersion(); 00773 if (strcmp(wpcap_version_string, packet_version_string) == 0) { 00774 /* 00775 * WinPcap version string and packet.dll version 00776 * string are the same; just report the WinPcap 00777 * version. 00778 */ 00779 pcap_version_string_len = 00780 (sizeof pcap_version_string_fmt - 2) + 00781 strlen(wpcap_version_string); 00782 pcap_version_string = malloc(pcap_version_string_len); 00783 sprintf(pcap_version_string, pcap_version_string_fmt, 00784 wpcap_version_string); 00785 } else { 00786 /* 00787 * WinPcap version string and packet.dll version 00788 * string are different; that shouldn't be the 00789 * case (the two libraries should come from the 00790 * same version of WinPcap), so we report both 00791 * versions. 00792 */ 00793 pcap_version_string_len = 00794 (sizeof pcap_version_string_packet_dll_fmt - 4) + 00795 strlen(wpcap_version_string) + 00796 strlen(packet_version_string); 00797 pcap_version_string = malloc(pcap_version_string_len); 00798 sprintf(pcap_version_string, 00799 pcap_version_string_packet_dll_fmt, 00800 wpcap_version_string, packet_version_string); 00801 } 00802 } 00803 return (pcap_version_string); 00804 } 00805 #else 00806 #include "version.h" 00807 00808 const char * 00809 pcap_lib_version(void) 00810 { 00811 return (pcap_version_string); 00812 } 00813 #endif
documentation. Copyright (c) 2002-2003 Politecnico di Torino. All rights reserved.