Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

BPF_Program.cc

Go to the documentation of this file.
00001 // $Id: BPF_Program.cc,v 1.3 2005/09/08 07:42:10 vern Exp $
00002 //
00003 // Copyright (c) 1996-2004
00004 //      The Regents of the University of California.  All rights reserved.
00005 //
00006 // Redistribution and use in source and binary forms, with or without
00007 // modification, are permitted provided that: (1) source code distributions
00008 // retain the above copyright notice and this paragraph in its entirety, (2)
00009 // distributions including binary code include the above copyright notice and
00010 // this paragraph in its entirety in the documentation or other materials
00011 // provided with the distribution, and (3) all advertising materials mentioning
00012 // features or use of this software display the following acknowledgement:
00013 // ``This product includes software developed by the University of California,
00014 // Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
00015 // the University nor the names of its contributors may be used to endorse
00016 // or promote products derived from this software without specific prior
00017 // written permission.
00018 // THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
00019 // WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
00020 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00021 
00022 #include "config.h"
00023 
00024 #include "util.h"
00025 #include "BPF_Program.h"
00026 
00027 #ifdef DONT_HAVE_LIBPCAP_PCAP_FREECODE
00028 extern "C" {
00029 #include "pcap-int.h"
00030 
00031 int pcap_freecode(pcap_t* unused, struct bpf_program* program)
00032         {
00033         program->bf_len = 0;
00034 
00035         if ( program->bf_insns )
00036                 {
00037                 free((char*) program->bf_insns);        // copied from libpcap
00038                 program->bf_insns = 0;
00039                 }
00040 
00041         return 0;
00042         }
00043 
00044 pcap_t* pcap_open_dead(int linktype, int snaplen)
00045         {
00046         pcap_t* p;
00047 
00048         p = (pcap_t*) malloc(sizeof(*p));
00049         if ( ! p )
00050                 return 0;
00051 
00052         memset(p, 0, sizeof(*p));
00053 
00054         p->fd = -1;
00055         p->snapshot = snaplen;
00056         p->linktype = linktype;
00057 
00058         return p;
00059         }
00060 
00061 int pcap_compile_nopcap(int snaplen_arg, int linktype_arg,
00062                         struct bpf_program* program, char* buf,
00063                         int optimize, bpf_u_int32 mask)
00064         {
00065         pcap_t* p;
00066         int ret;
00067 
00068         p = pcap_open_dead(linktype_arg, snaplen_arg);
00069         if ( ! p )
00070                 return -1;
00071 
00072         ret = pcap_compile(p, program, buf, optimize, mask);
00073         pcap_close(p);
00074 
00075         return ret;
00076         }
00077 }
00078 #endif
00079 
00080 BPF_Program::BPF_Program()
00081         {
00082         m_compiled = false;
00083         }
00084 
00085 BPF_Program::~BPF_Program()
00086         {
00087         FreeCode();
00088         }
00089 
00090 bool BPF_Program::Compile(pcap_t* pcap, const char* filter, uint32 netmask,
00091                           char* errbuf, unsigned int errbuf_len, bool optimize)
00092         {
00093         if ( ! pcap )
00094                 return false;
00095 
00096         FreeCode();
00097 
00098         if ( pcap_compile(pcap, &m_program, (char *) filter, optimize, netmask) < 0 )
00099                 {
00100                 if ( errbuf )
00101                         safe_snprintf(errbuf, errbuf_len,
00102                                       "pcap_compile(%s): %s", filter,
00103                                       pcap_geterr(pcap));
00104 
00105                 return false;
00106                 }
00107 
00108         m_compiled = true;
00109 
00110         return true;
00111         }
00112 
00113 bool BPF_Program::Compile(int snaplen, int linktype, const char* filter,
00114                                 uint32 netmask, char* errbuf, bool optimize)
00115         {
00116         FreeCode();
00117 
00118 #ifdef LIBPCAP_PCAP_COMPILE_NOPCAP_HAS_ERROR_PARAMETER
00119         char my_error[PCAP_ERRBUF_SIZE];
00120 
00121         int err = pcap_compile_nopcap(snaplen, linktype, &m_program,
00122                                      (char *) filter, optimize, netmask, error);
00123         if ( err < 0 && errbuf )
00124                 safe_strncpy(errbuf, my_errbuf, PCAP_ERRBUF_SIZE);
00125 #else
00126         int err = pcap_compile_nopcap(snaplen, linktype, &m_program,
00127                                      (char*) filter, optimize, netmask);
00128 #endif
00129         if ( err == 0 )
00130                 m_compiled = true;
00131 
00132         return err == 0;
00133         }
00134 
00135 bpf_program* BPF_Program::GetProgram()
00136         {
00137         return m_compiled ? &m_program : 0;
00138         }
00139 
00140 void BPF_Program::FreeCode()
00141         {
00142         if ( m_compiled )
00143                 {
00144 #ifdef DONT_HAVE_LIBPCAP_PCAP_FREECODE
00145                 pcap_freecode(NULL, &m_program);
00146 #else
00147                 pcap_freecode(&m_program);
00148 #endif
00149                 m_compiled = false;
00150                 }
00151         }

Generated on Wed Sep 14 02:55:58 2005 for bro_docs by doxygen 1.3.5