Main Page Modules Data Structures File List Data Fields Globals Related Pages

# pcap.c

Go to the documentation of this file.

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

documentation. Copyright (c) 2002-2003 Politecnico di Torino. All rights reserved.