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[] =
    00036     "@(#) $Header: /tcpdump/master/libpcap/pcap.c,v 1.45 2003/01/23 07:24:52 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 #ifdef REMOTE
    00063 #include <pcap-remote.h>
    00064 #endif
    00065
    00066 #include "pcap-int.h"
    00067
    00068 int
    00069 pcap_dispatch(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
    00070 {
    00071
    00072     if (p->sf.rfile != NULL)
    00073         return (pcap_offline_read(p, cnt, callback, user));
    00074     return (pcap_read(p, cnt, callback, user));
    00075 }
    00076
    00077 int
    00078 pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
    00079 {
    00080     register int n;
    00081
    00082     for (;;) {
    00083         if (p->sf.rfile != NULL)
    00084             n = pcap_offline_read(p, cnt, callback, user);
    00085         else {
    00086             /*
    00087              * XXX keep reading until we get something
    00088              * (or an error occurs)
    00089              */
    00090             do {
    00091                 n = pcap_read(p, cnt, callback, user);
    00092             } while (n == 0);
    00093         }
    00094         if (n <= 0)
    00095             return (n);
    00096         if (cnt > 0) {
    00097             cnt -= n;
    00098             if (cnt <= 0)
    00099                 return (0);
    00100         }
    00101     }
    00102 }
    00103
    00104 struct singleton {
    00105     struct pcap_pkthdr *hdr;
    00106     const u_char *pkt;
    00107 };
    00108
    00109
    00110 static void
    00111 pcap_oneshot(u_char *userData, const struct pcap_pkthdr *h, const u_char *pkt)
    00112 {
    00113     struct singleton *sp = (struct singleton *)userData;
    00114     *sp->hdr = *h;
    00115     sp->pkt = pkt;
    00116 }
    00117
    00118 const u_char *
    00119 pcap_next(pcap_t *p, struct pcap_pkthdr *h)
    00120 {
    00121     struct singleton s;
    00122
    00123     s.hdr = h;
    00124     if (pcap_dispatch(p, 1, pcap_oneshot, (u_char*)&s) <= 0)
    00125         return (0);
    00126     return (s.pkt);
    00127 }
    00128
    00129
    00130
    00131 // MODIFICATIONS FOR PCAP_NEXT_EX
    00132 struct pkt_for_fakecallback {
    00133     struct pcap_pkthdr *hdr;
    00134     const u_char **pkt;
    00135 };
    00136
    00137
    00138 static void
    00139 pcap_fakecallback(u_char *userData, const struct pcap_pkthdr *h, const u_char *pkt)
    00140 {
    00141     struct pkt_for_fakecallback *sp = (struct pkt_for_fakecallback *)userData;
    00142     *sp->hdr = *h;
    00143     *sp->pkt = pkt;
    00144 }
    00145
    00146
    00147 int
    00148 pcap_next_ex(pcap_t *p, struct pcap_pkthdr **pkt_header, u_char **pkt_data)
    00149 {
    00150 struct pkt_for_fakecallback s;
    00151
    00152     s.hdr= &(p->pcap_header);
    00153     s.pkt= pkt_data;
    00154
    00155     // Saves a pointer to the packet headers
    00156     *pkt_header= &(p->pcap_header);
    00157
    00158     /* Check the capture type */
    00159 #ifdef REMOTE
    00160     if (p->rmt_clientside)
    00161     {
    00162         /* We are on an remote capture */
    00163         if (!p->rmt_capstarted)
    00164         {
    00165             // if the capture has not started yet, please start it
    00166             if (pcap_startcapture_remote(p) )
    00167                 return -1;
    00168             p->rmt_capstarted= 1;
    00169         }
    00170
    00171         return pcap_read_nocb_remote(p, pkt_header, pkt_data);
    00172     }
    00173 #endif
    00174
    00175     if (p->sf.rfile != NULL)
    00176     {
    00177     int status;
    00178
    00179         /* We are on an offline capture */
    00180         status= pcap_offline_read(p, 1, pcap_fakecallback, (u_char*)&s);
    00181
    00182         /*
    00183             Return codes for pcap_offline_read() are:
    00184             -  0: EOF
    00185             - -1: error
    00186             - >1: OK
    00187             The first one ('0') conflicts with the return code of the pcap_read()
    00188         */
    00189         if (status == 0)
    00190             return -2;
    00191         else
    00192             return status;
    00193     }
    00194
    00195     /*
    00196         Return codes for pcap_read() are:
    00197         -  0: timeout
    00198         - -1: error
    00199         - >1: OK
    00200         The first one ('0') conflicts with the return code of the pcap_offline_read()
    00201     */
    00202     return (pcap_read(p, 1, pcap_fakecallback, (u_char*)&s));
    00203 }
    00204 // END MODIFICATIONS FOR PCAP_NEXT_EX
    00205
    00206
    00207
    00208 int
    00209 pcap_datalink(pcap_t *p)
    00210 {
    00211     return (p->linktype);
    00212 }
    00213
    00214 int
    00215 pcap_list_datalinks(pcap_t *p, int **dlt_buffer)
    00216 {
    00217     if (p->dlt_count == 0) {
    00218         /*
    00219          * We couldn't fetch the list of DLTs, which means
    00220          * this platform doesn't support changing the
    00221          * DLT for an interface.  Return a list of DLTs
    00222          * containing only the DLT this device supports.
    00223          */
    00224         *dlt_buffer = (int*)malloc(sizeof(**dlt_buffer));
    00225         if (*dlt_buffer == NULL) {
    00226             (void)snprintf(p->errbuf, sizeof(p->errbuf),
    00227                 "malloc: %s", pcap_strerror(errno));
    00228             return (-1);
    00229         }
    00230         **dlt_buffer = p->linktype;
    00231         return (1);
    00232     } else {
    00233         *dlt_buffer = (int*)malloc(sizeof(**dlt_buffer) * p->dlt_count);
    00234         if (*dlt_buffer == NULL) {
    00235             (void)snprintf(p->errbuf, sizeof(p->errbuf),
    00236                 "malloc: %s", pcap_strerror(errno));
    00237             return (-1);
    00238         }
    00239         (void)memcpy(*dlt_buffer, p->dlt_list,
    00240             sizeof(**dlt_buffer) * p->dlt_count);
    00241         return (p->dlt_count);
    00242     }
    00243 }
    00244
    00245 int
    00246 pcap_set_datalink(pcap_t *p, int dlt)
    00247 {
    00248     int i;
    00249     const char *dlt_name;
    00250
    00251     if (p->dlt_count == 0) {
    00252         /*
    00253          * We couldn't fetch the list of DLTs, which means
    00254          * this platform doesn't support changing the
    00255          * DLT for an interface.  Check whether the new
    00256          * DLT is the one this interface supports.
    00257          */
    00258         if (p->linktype != dlt)
    00259             goto unsupported;
    00260
    00261         /*
    00262          * It is, so there's nothing we need to do here.
    00263          */
    00264         return (0);
    00265     }
    00266     for (i = 0; i < p->dlt_count; i++)
    00267         if (p->dlt_list[i] == dlt)
    00268             break;
    00269     if (i >= p->dlt_count)
    00270         goto unsupported;
    00271     if (pcap_set_datalink_platform(p, dlt) == -1)
    00272         return (-1);
    00273     p->linktype = dlt;
    00274     return (0);
    00275
    00276 unsupported:
    00277     dlt_name = pcap_datalink_val_to_name(dlt);
    00278     if (dlt_name != NULL) {
    00279         (void) snprintf(p->errbuf, sizeof(p->errbuf),
    00280             "%s is not one of the DLTs supported by this device",
    00281             dlt_name);
    00282     } else {
    00283         (void) snprintf(p->errbuf, sizeof(p->errbuf),
    00284             "DLT %d is not one of the DLTs supported by this device",
    00285             dlt);
    00286     }
    00287     return (-1);
    00288 }
    00289
    00290 struct dlt_choice {
    00291     const char *name;
    00292     int dlt;
    00293 };
    00294
    00295 #define DLT_CHOICE(code) { #code, code }
    00296 #define DLT_CHOICE_SENTINEL { NULL, 0 }
    00297
    00298 static struct dlt_choice dlt_choices[] = {
    00299     DLT_CHOICE(DLT_ARCNET),
    00300     DLT_CHOICE(DLT_ARCNET_LINUX),
    00301     DLT_CHOICE(DLT_EN10MB),
    00302     DLT_CHOICE(DLT_SLIP),
    00303     DLT_CHOICE(DLT_SLIP_BSDOS),
    00304     DLT_CHOICE(DLT_NULL),
    00305     DLT_CHOICE(DLT_LOOP),
    00306     DLT_CHOICE(DLT_PPP),
    00307     DLT_CHOICE(DLT_C_HDLC),
    00308     DLT_CHOICE(DLT_PPP_SERIAL),
    00309     DLT_CHOICE(DLT_PPP_ETHER),
    00310     DLT_CHOICE(DLT_PPP_BSDOS),
    00311     DLT_CHOICE(DLT_FDDI),
    00312     DLT_CHOICE(DLT_IEEE802),
    00313     DLT_CHOICE(DLT_IEEE802_11),
    00314     DLT_CHOICE(DLT_PRISM_HEADER),
    00315     DLT_CHOICE(DLT_IEEE802_11_RADIO),
    00316     DLT_CHOICE(DLT_ATM_RFC1483),
    00317     DLT_CHOICE(DLT_ATM_CLIP),
    00318     DLT_CHOICE(DLT_SUNATM),
    00319     DLT_CHOICE(DLT_RAW),
    00320     DLT_CHOICE(DLT_LINUX_SLL),
    00321     DLT_CHOICE(DLT_LTALK),
    00322     DLT_CHOICE(DLT_IP_OVER_FC),
    00323     DLT_CHOICE(DLT_FRELAY),
    00324     DLT_CHOICE_SENTINEL
    00325 };
    00326
    00327 /*
    00328  * This array is designed for mapping upper and lower case letter
    00329  * together for a case independent comparison.  The mappings are
    00330  * based upon ascii character sequences.
    00331  */
    00332 static const u_char charmap[] = {
    00333     (u_char)'\000', (u_char)'\001', (u_char)'\002', (u_char)'\003',
    00334     (u_char)'\004', (u_char)'\005', (u_char)'\006', (u_char)'\007',
    00335     (u_char)'\010', (u_char)'\011', (u_char)'\012', (u_char)'\013',
    00336     (u_char)'\014', (u_char)'\015', (u_char)'\016', (u_char)'\017',
    00337     (u_char)'\020', (u_char)'\021', (u_char)'\022', (u_char)'\023',
    00338     (u_char)'\024', (u_char)'\025', (u_char)'\026', (u_char)'\027',
    00339     (u_char)'\030', (u_char)'\031', (u_char)'\032', (u_char)'\033',
    00340     (u_char)'\034', (u_char)'\035', (u_char)'\036', (u_char)'\037',
    00341     (u_char)'\040', (u_char)'\041', (u_char)'\042', (u_char)'\043',
    00342     (u_char)'\044', (u_char)'\045', (u_char)'\046', (u_char)'\047',
    00343     (u_char)'\050', (u_char)'\051', (u_char)'\052', (u_char)'\053',
    00344     (u_char)'\054', (u_char)'\055', (u_char)'\056', (u_char)'\057',
    00345     (u_char)'\060', (u_char)'\061', (u_char)'\062', (u_char)'\063',
    00346     (u_char)'\064', (u_char)'\065', (u_char)'\066', (u_char)'\067',
    00347     (u_char)'\070', (u_char)'\071', (u_char)'\072', (u_char)'\073',
    00348     (u_char)'\074', (u_char)'\075', (u_char)'\076', (u_char)'\077',
    00349     (u_char)'\100', (u_char)'\141', (u_char)'\142', (u_char)'\143',
    00350     (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147',
    00351     (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153',
    00352     (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157',
    00353     (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163',
    00354     (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167',
    00355     (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\133',
    00356     (u_char)'\134', (u_char)'\135', (u_char)'\136', (u_char)'\137',
    00357     (u_char)'\140', (u_char)'\141', (u_char)'\142', (u_char)'\143',
    00358     (u_char)'\144', (u_char)'\145', (u_char)'\146', (u_char)'\147',
    00359     (u_char)'\150', (u_char)'\151', (u_char)'\152', (u_char)'\153',
    00360     (u_char)'\154', (u_char)'\155', (u_char)'\156', (u_char)'\157',
    00361     (u_char)'\160', (u_char)'\161', (u_char)'\162', (u_char)'\163',
    00362     (u_char)'\164', (u_char)'\165', (u_char)'\166', (u_char)'\167',
    00363     (u_char)'\170', (u_char)'\171', (u_char)'\172', (u_char)'\173',
    00364     (u_char)'\174', (u_char)'\175', (u_char)'\176', (u_char)'\177',
    00365     (u_char)'\200', (u_char)'\201', (u_char)'\202', (u_char)'\203',
    00366     (u_char)'\204', (u_char)'\205', (u_char)'\206', (u_char)'\207',
    00367     (u_char)'\210', (u_char)'\211', (u_char)'\212', (u_char)'\213',
    00368     (u_char)'\214', (u_char)'\215', (u_char)'\216', (u_char)'\217',
    00369     (u_char)'\220', (u_char)'\221', (u_char)'\222', (u_char)'\223',
    00370     (u_char)'\224', (u_char)'\225', (u_char)'\226', (u_char)'\227',
    00371     (u_char)'\230', (u_char)'\231', (u_char)'\232', (u_char)'\233',
    00372     (u_char)'\234', (u_char)'\235', (u_char)'\236', (u_char)'\237',
    00373     (u_char)'\240', (u_char)'\241', (u_char)'\242', (u_char)'\243',
    00374     (u_char)'\244', (u_char)'\245', (u_char)'\246', (u_char)'\247',
    00375     (u_char)'\250', (u_char)'\251', (u_char)'\252', (u_char)'\253',
    00376     (u_char)'\254', (u_char)'\255', (u_char)'\256', (u_char)'\257',
    00377     (u_char)'\260', (u_char)'\261', (u_char)'\262', (u_char)'\263',
    00378     (u_char)'\264', (u_char)'\265', (u_char)'\266', (u_char)'\267',
    00379     (u_char)'\270', (u_char)'\271', (u_char)'\272', (u_char)'\273',
    00380     (u_char)'\274', (u_char)'\275', (u_char)'\276', (u_char)'\277',
    00381     (u_char)'\300', (u_char)'\341', (u_char)'\342', (u_char)'\343',
    00382     (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347',
    00383     (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353',
    00384     (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357',
    00385     (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363',
    00386     (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367',
    00387     (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\333',
    00388     (u_char)'\334', (u_char)'\335', (u_char)'\336', (u_char)'\337',
    00389     (u_char)'\340', (u_char)'\341', (u_char)'\342', (u_char)'\343',
    00390     (u_char)'\344', (u_char)'\345', (u_char)'\346', (u_char)'\347',
    00391     (u_char)'\350', (u_char)'\351', (u_char)'\352', (u_char)'\353',
    00392     (u_char)'\354', (u_char)'\355', (u_char)'\356', (u_char)'\357',
    00393     (u_char)'\360', (u_char)'\361', (u_char)'\362', (u_char)'\363',
    00394     (u_char)'\364', (u_char)'\365', (u_char)'\366', (u_char)'\367',
    00395     (u_char)'\370', (u_char)'\371', (u_char)'\372', (u_char)'\373',
    00396     (u_char)'\374', (u_char)'\375', (u_char)'\376', (u_char)'\377',
    00397 };
    00398
    00399 static int
    00400 pcap_strcasecmp(const char *s1, const char *s2)
    00401 {
    00402     register const u_char   *cm = charmap,
    00403                 *us1 = (u_char *)s1,
    00404                 *us2 = (u_char *)s2;
    00405
    00406     while (cm[*us1] == cm[*us2++])
    00407         if (*us1++ == '\0')
    00408             return(0);
    00409     return (cm[*us1] - cm[*--us2]);
    00410 }
    00411
    00412 int
    00413 pcap_datalink_name_to_val(const char *name)
    00414 {
    00415     int i;
    00416
    00417     for (i = 0; dlt_choices[i].name != NULL; i++) {
    00418         if (pcap_strcasecmp(dlt_choices[i].name + sizeof("DLT_") - 1,
    00419             name) == 0)
    00420             return (dlt_choices[i].dlt);
    00421     }
    00422     return (-1);
    00423 }
    00424
    00425 const char *
    00426 pcap_datalink_val_to_name(int dlt)
    00427 {
    00428     int i;
    00429
    00430     for (i = 0; dlt_choices[i].name != NULL; i++) {
    00431         if (dlt_choices[i].dlt == dlt)
    00432             return (dlt_choices[i].name + sizeof("DLT_") - 1);
    00433     }
    00434     return (NULL);
    00435 }
    00436
    00437 int
    00438 pcap_snapshot(pcap_t *p)
    00439 {
    00440     return (p->snapshot);
    00441 }
    00442
    00443 int
    00444 pcap_is_swapped(pcap_t *p)
    00445 {
    00446     return (p->sf.swapped);
    00447 }
    00448
    00449 int
    00450 pcap_major_version(pcap_t *p)
    00451 {
    00452     return (p->sf.version_major);
    00453 }
    00454
    00455 int
    00456 pcap_minor_version(pcap_t *p)
    00457 {
    00458     return (p->sf.version_minor);
    00459 }
    00460
    00461 FILE *
    00462 pcap_file(pcap_t *p)
    00463 {
    00464     return (p->sf.rfile);
    00465 }
    00466
    00467 int
    00468 pcap_fileno(pcap_t *p)
    00469 {
    00470 #ifndef WIN32
    00471     return (p->fd);
    00472 #else
    00473     if (p->adapter != NULL)
    00474         return ((int)(DWORD)p->adapter->hFile);
    00475     else
    00476         return (-1);
    00477 #endif
    00478 }
    00479
    00480 void
    00481 pcap_perror(pcap_t *p, char *prefix)
    00482 {
    00483     fprintf(stderr, "%s: %s\n", prefix, p->errbuf);
    00484 }
    00485
    00486 char *
    00487 pcap_geterr(pcap_t *p)
    00488 {
    00489     return (p->errbuf);
    00490 }
    00491
    00492 /*
    00493  * NOTE: in the future, these may need to call platform-dependent routines,
    00494  * e.g. on platforms with memory-mapped packet-capture mechanisms where
    00495  * "pcap_read()" uses "select()" or "poll()" to wait for packets to arrive.
    00496  */
    00497 int
    00498 pcap_getnonblock(pcap_t *p, char *errbuf)
    00499 {
    00500 #ifndef WIN32
    00501     int fdflags;
    00502 #endif
    00503
    00504     if (p->sf.rfile != NULL) {
    00505         /*
    00506          * This is a savefile, not a live capture file, so
    00507          * never say it's in non-blocking mode.
    00508          */
    00509         return (0);
    00510     }
    00511 #ifndef WIN32
    00512     fdflags = fcntl(p->fd, F_GETFL, 0);
    00513     if (fdflags == -1) {
    00514         snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
    00515             pcap_strerror(errno));
    00516         return (-1);
    00517     }
    00518     if (fdflags & O_NONBLOCK)
    00519         return (1);
    00520     else
    00521         return (0);
    00522 #else
    00523     return (p->nonblock);
    00524 #endif
    00525 }
    00526
    00527 int
    00528 pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf)
    00529 {
    00530 #ifndef WIN32
    00531     int fdflags;
    00532 #else
    00533     int newtimeout;
    00534 #endif
    00535
    00536     if (p->sf.rfile != NULL) {
    00537         /*
    00538          * This is a savefile, not a live capture file, so
    00539          * ignore requests to put it in non-blocking mode.
    00540          */
    00541         return (0);
    00542     }
    00543 #ifndef WIN32
    00544     fdflags = fcntl(p->fd, F_GETFL, 0);
    00545     if (fdflags == -1) {
    00546         snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
    00547             pcap_strerror(errno));
    00548         return (-1);
    00549     }
    00550     if (nonblock)
    00551         fdflags |= O_NONBLOCK;
    00552     else
    00553         fdflags &= ~O_NONBLOCK;
    00554     if (fcntl(p->fd, F_SETFL, fdflags) == -1) {
    00555         snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_SETFL: %s",
    00556             pcap_strerror(errno));
    00557         return (-1);
    00558     }
    00559 #else
    00560     if (nonblock) {
    00561         /*
    00562          * Set the read timeout to -1 for non-blocking mode.
    00563          */
    00564         newtimeout = -1;
    00565     } else {
    00566         /*
    00567          * Restore the timeout set when the device was opened.
    00568          * (Note that this may be -1, in which case we're not
    00569          * really leaving non-blocking mode.)
    00570          */
    00571         newtimeout = p->timeout;
    00572     }
    00573     if (!PacketSetReadTimeout(p->adapter, newtimeout)) {
    00574         snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
    00575             "PacketSetReadTimeout: %s", pcap_win32strerror());
    00576         return (-1);
    00577     }
    00578     p->nonblock = (newtimeout == -1);
    00579 #endif
    00580     return (0);
    00581 }
    00582
    00583 #ifdef WIN32
    00584 /*
    00585  * Generate a string for the last Win32-specific error (i.e. an error generated when
    00586  * calling a Win32 API).
    00587  * For errors occurred during standard C calls, we still use pcap_strerror()
    00588  */
    00589 char *
    00590 pcap_win32strerror(void)
    00591 {
    00592     DWORD error;
    00593     static char errbuf[PCAP_ERRBUF_SIZE+1];
    00594     int errlen;
    00595
    00596     error = GetLastError();
    00597     FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, errbuf,
    00598         PCAP_ERRBUF_SIZE, NULL);
    00599
    00600     /*
    00601      * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
    00602      * message.  Get rid of it.
    00603      */
    00604     errlen = strlen(errbuf);
    00605     if (errlen >= 2) {
    00606         errbuf[errlen - 1] = '\0';
    00607         errbuf[errlen - 2] = '\0';
    00608     }
    00609     return (errbuf);
    00610 }
    00611 #endif
    00612
    00613 /*
    00614  * Not all systems have strerror().
    00615  */
    00616 char *
    00617 pcap_strerror(int errnum)
    00618 {
    00619 #ifdef HAVE_STRERROR
    00620     return (strerror(errnum));
    00621 #else
    00622     extern int sys_nerr;
    00623     extern const char *const sys_errlist[];
    00624     static char ebuf[20];
    00625
    00626     if ((unsigned int)errnum < sys_nerr)
    00627         return ((char *)sys_errlist[errnum]);
    00628     (void)snprintf(ebuf, sizeof ebuf, "Unknown error: %d", errnum);
    00629     return(ebuf);
    00630 #endif
    00631 }
    00632
    00633 pcap_t *
    00634 pcap_open_dead(int linktype, int snaplen)
    00635 {
    00636     pcap_t *p;
    00637
    00638     p = malloc(sizeof(*p));
    00639     if (p == NULL)
    00640         return NULL;
    00641     memset (p, 0, sizeof(*p));
    00642 #ifndef WIN32
    00643     p->fd = -1;
    00644 #else
    00645     p->adapter = NULL;
    00646 #endif /* WIN32 */
    00647     p->snapshot = snaplen;
    00648     p->linktype = linktype;
    00649     return p;
    00650 }
    00651
    00652 void
    00653 pcap_close(pcap_t *p)
    00654 {
    00655 #ifdef REMOTE
    00656     if (p->rmt_clientside)
    00657         pcap_close_remote(p);
    00658 #endif
    00659
    00660     /*XXX*/
    00661 #ifndef WIN32
    00662     if (p->fd >= 0) {
    00663 #ifdef linux
    00664         pcap_close_linux(p);
    00665 #endif
    00666         close(p->fd);
    00667     }
    00668 #else /* WIN32 */
    00669     if (p->adapter != NULL) {
    00670         PacketCloseAdapter(p->adapter);
    00671         p->adapter = NULL;
    00672     }
    00673 #endif /* WIN32 */
    00674     if (p->sf.rfile != NULL) {
    00675         if (p->sf.rfile != stdin)
    00676             (void)fclose(p->sf.rfile);
    00677         if (p->sf.base != NULL)
    00678             free(p->sf.base);
    00679     } else if (p->buffer != NULL)
    00680         free(p->buffer);
    00681     if (p->dlt_list != NULL)
    00682         free(p->dlt_list);
    00683
    00684     pcap_freecode(&p->fcode);
    00685     free(p);
    00686 }

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