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 HAVE_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 HAVE_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 #ifdef HAVE_REMOTE
00471     if (p->rmt_clientside)
00472         return(p->rmt_sockdata);
00473 #endif
00474 #ifndef WIN32
00475     return (p->fd);
00476 #else
00477     if (p->adapter != NULL)
00478         return ((int)(DWORD)p->adapter->hFile);
00479     else
00480         return (-1);
00481 #endif
00482 }
00483 
00484 void
00485 pcap_perror(pcap_t *p, char *prefix)
00486 {
00487     fprintf(stderr, "%s: %s\n", prefix, p->errbuf);
00488 }
00489 
00490 char *
00491 pcap_geterr(pcap_t *p)
00492 {
00493     return (p->errbuf);
00494 }
00495 
00496 /*
00497  * NOTE: in the future, these may need to call platform-dependent routines,
00498  * e.g. on platforms with memory-mapped packet-capture mechanisms where
00499  * "pcap_read()" uses "select()" or "poll()" to wait for packets to arrive.
00500  */
00501 int
00502 pcap_getnonblock(pcap_t *p, char *errbuf)
00503 {
00504 #ifndef WIN32
00505     int fdflags;
00506 #endif
00507 
00508     if (p->sf.rfile != NULL) {
00509         /*
00510          * This is a savefile, not a live capture file, so
00511          * never say it's in non-blocking mode.
00512          */
00513         return (0);
00514     }
00515 #ifndef WIN32
00516     fdflags = fcntl(p->fd, F_GETFL, 0);
00517     if (fdflags == -1) {
00518         snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
00519             pcap_strerror(errno));
00520         return (-1);
00521     }
00522     if (fdflags & O_NONBLOCK)
00523         return (1);
00524     else
00525         return (0);
00526 #else
00527     return (p->nonblock);
00528 #endif
00529 }
00530 
00531 int
00532 pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf)
00533 {
00534 #ifndef WIN32
00535     int fdflags;
00536 #else
00537     int newtimeout;
00538 #endif
00539 
00540     if (p->sf.rfile != NULL) {
00541         /*
00542          * This is a savefile, not a live capture file, so
00543          * ignore requests to put it in non-blocking mode.
00544          */
00545         return (0);
00546     }
00547 #ifndef WIN32
00548     fdflags = fcntl(p->fd, F_GETFL, 0);
00549     if (fdflags == -1) {
00550         snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
00551             pcap_strerror(errno));
00552         return (-1);
00553     }
00554     if (nonblock)
00555         fdflags |= O_NONBLOCK;
00556     else
00557         fdflags &= ~O_NONBLOCK;
00558     if (fcntl(p->fd, F_SETFL, fdflags) == -1) {
00559         snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_SETFL: %s",
00560             pcap_strerror(errno));
00561         return (-1);
00562     }
00563 #else
00564     if (nonblock) {
00565         /*
00566          * Set the read timeout to -1 for non-blocking mode.
00567          */
00568         newtimeout = -1;
00569     } else {
00570         /*
00571          * Restore the timeout set when the device was opened.
00572          * (Note that this may be -1, in which case we're not
00573          * really leaving non-blocking mode.)
00574          */
00575         newtimeout = p->timeout;
00576     }
00577     if (!PacketSetReadTimeout(p->adapter, newtimeout)) {
00578         snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
00579             "PacketSetReadTimeout: %s", pcap_win32strerror());
00580         return (-1);
00581     }
00582     p->nonblock = (newtimeout == -1);
00583 #endif
00584     return (0);
00585 }
00586 
00587 #ifdef WIN32
00588 /*
00589  * Generate a string for the last Win32-specific error (i.e. an error generated when 
00590  * calling a Win32 API).
00591  * For errors occurred during standard C calls, we still use pcap_strerror()
00592  */
00593 char *
00594 pcap_win32strerror(void)
00595 {
00596     DWORD error;
00597     static char errbuf[PCAP_ERRBUF_SIZE+1];
00598     int errlen;
00599 
00600     error = GetLastError();
00601     FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, errbuf,
00602         PCAP_ERRBUF_SIZE, NULL);
00603 
00604     /*
00605      * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
00606      * message.  Get rid of it.
00607      */
00608     errlen = strlen(errbuf);
00609     if (errlen >= 2) {
00610         errbuf[errlen - 1] = '\0';
00611         errbuf[errlen - 2] = '\0';
00612     }
00613     return (errbuf);
00614 }
00615 #endif
00616 
00617 /*
00618  * Not all systems have strerror().
00619  */
00620 char *
00621 pcap_strerror(int errnum)
00622 {
00623 #ifdef HAVE_STRERROR
00624     return (strerror(errnum));
00625 #else
00626     extern int sys_nerr;
00627     extern const char *const sys_errlist[];
00628     static char ebuf[20];
00629 
00630     if ((unsigned int)errnum < sys_nerr)
00631         return ((char *)sys_errlist[errnum]);
00632     (void)snprintf(ebuf, sizeof ebuf, "Unknown error: %d", errnum);
00633     return(ebuf);
00634 #endif
00635 }
00636 
00637 pcap_t *
00638 pcap_open_dead(int linktype, int snaplen)
00639 {
00640     pcap_t *p;
00641 
00642     p = malloc(sizeof(*p));
00643     if (p == NULL)
00644         return NULL;
00645     memset (p, 0, sizeof(*p));
00646 #ifndef WIN32
00647     p->fd = -1;
00648 #else
00649     p->adapter = NULL;
00650 #endif /* WIN32 */
00651     p->snapshot = snaplen;
00652     p->linktype = linktype;
00653     return p;
00654 }
00655 
00656 void
00657 pcap_close(pcap_t *p)
00658 {
00659 #ifdef HAVE_REMOTE
00660     if (p->rmt_clientside)
00661         pcap_close_remote(p);
00662 #endif
00663 
00664     /*XXX*/
00665 #ifndef WIN32
00666     if (p->fd >= 0) {
00667 #ifdef linux
00668         pcap_close_linux(p);
00669 #endif
00670         close(p->fd);
00671     }
00672 #else /* WIN32 */
00673     if (p->adapter != NULL) {
00674         PacketCloseAdapter(p->adapter);
00675         p->adapter = NULL;
00676     }
00677 #endif /* WIN32 */
00678     if (p->sf.rfile != NULL) {
00679         if (p->sf.rfile != stdin)
00680             (void)fclose(p->sf.rfile);
00681         if (p->sf.base != NULL)
00682             free(p->sf.base);
00683     } else if (p->buffer != NULL)
00684         free(p->buffer);
00685     if (p->dlt_list != NULL)
00686         free(p->dlt_list);
00687 
00688     pcap_freecode(&p->fcode);
00689     free(p);
00690 }

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