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

PcapNetwork Class Reference

Inheritance diagram for PcapNetwork:

Inheritance graph
[legend]
Collaboration diagram for PcapNetwork:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 PcapNetwork ()
int rchannel ()
int schannel ()
virtual int command (int argc, const char *const *argv)
virtual int open (int mode, const char *)=0
virtual int skiphdr ()=0
virtual double gents (pcap_pkthdr *)=0
int recv (u_char *buf, int len, sockaddr &, double &)
int send (u_char *buf, int len)
int recv (netpkt_handler callback, void *clientdata)
void close ()
void reset ()
int filter (const char *)
int stat_pkts ()
int stat_pdrops ()
int mode ()

Static Public Member Functions

int nonblock (int fd)
int parsemode (const char *)
char * modename (int)

Public Attributes

double offset_
double t_firstpkt_

Protected Member Functions

virtual void bindvars ()=0

Static Protected Member Functions

void phandler (u_char *u, const pcap_pkthdr *h, const u_char *p)
void phandler_callback (u_char *u, const pcap_pkthdr *h, const u_char *p)

Protected Attributes

char errbuf_ [PCAP_ERRBUF_SIZE]
char srcname_ [PATH_MAX]
int pfd_
int pcnt_
int state_
int optimize_
pcap_t * pcap_
bpf_program bpfpgm_
pcap_stat pcs_
unsigned int local_netmask_
int mode_

Constructor & Destructor Documentation

PcapNetwork::PcapNetwork  )  [inline]
 

Definition at line 110 of file net-pcap.cc.

References local_netmask_, pcnt_, pfd_, and t_firstpkt_.

00110                       : t_firstpkt_(0.0),
00111                 pfd_(-1), pcnt_(0), local_netmask_(0) { }


Member Function Documentation

void PcapNetwork::bindvars  )  [protected, pure virtual]
 

Implemented in PcapLiveNetwork, and PcapFileNetwork.

Definition at line 237 of file net-pcap.cc.

References optimize_.

Referenced by PcapLiveNetwork::bindvars().

00238 {
00239         bind_bool("optimize_", &optimize_);
00240 }

void PcapNetwork::close  ) 
 

Definition at line 254 of file net-pcap.cc.

References pcap_, PNET_PSTATE_ACTIVE, reset(), and state_.

Referenced by command(), PcapFileNetwork::open(), and PcapLiveNetwork::open().

00255 {
00256         if (state_ == PNET_PSTATE_ACTIVE && pcap_)
00257                 pcap_close(pcap_);
00258         reset();
00259 }

Here is the call graph for this function:

int PcapNetwork::command int  argc,
const char *const *  argv
[virtual]
 

Reimplemented from Network.

Reimplemented in PcapLiveNetwork, and PcapFileNetwork.

Definition at line 430 of file net-pcap.cc.

References close(), Network::command(), filter(), PNET_PSTATE_ACTIVE, srcname_, stat_pdrops(), stat_pkts(), and state_.

Referenced by PcapFileNetwork::command(), and PcapLiveNetwork::command().

00431 {
00432         Tcl& tcl = Tcl::instance();
00433         if (argc == 2) {
00434                 if (strcmp(argv[1], "close") == 0) {
00435                         close();
00436                         return (TCL_OK);
00437                 }
00438                 if (strcmp(argv[1], "srcname") == 0) {
00439                         tcl.result(srcname_);
00440                         return (TCL_OK);
00441                 }
00442                 if (strcmp(argv[1], "pkts") == 0) {
00443                         tcl.resultf("%d", stat_pkts());
00444                         return (TCL_OK);
00445                 }
00446                 if (strcmp(argv[1], "pdrops") == 0) {
00447                         tcl.resultf("%d", stat_pdrops());
00448                         return (TCL_OK);
00449                 }
00450         } else if (argc == 3) {
00451                 if (strcmp(argv[1], "filter") == 0) {
00452                         if (state_ != PNET_PSTATE_ACTIVE) {
00453                                 fprintf(stderr, "net/pcap obj(%s): can't install filter prior to opening data source\n",
00454                                         name());
00455                                 return (TCL_ERROR);
00456                         }
00457                         int plen;
00458                         if ((plen = filter(argv[2])) < 0) {
00459                                 fprintf(stderr, "problem compiling/installing filter program\n");
00460                                 return (TCL_ERROR);
00461                         }
00462                         tcl.resultf("%d", plen);
00463                         return (TCL_OK);
00464                 }
00465         }
00466         return (Network::command(argc, argv));
00467 }

Here is the call graph for this function:

int PcapNetwork::filter const char *   ) 
 

Definition at line 265 of file net-pcap.cc.

References bpfpgm_, local_netmask_, optimize_, and pcap_.

Referenced by command().

00266 {
00267         if (pcap_compile(pcap_, &bpfpgm_, (char *)pgm,
00268             optimize_, local_netmask_) < 0) {
00269                 fprintf(stderr, "pcapnet obj(%s): couldn't compile filter pgm",
00270                         name());
00271                 return -1;
00272         }
00273         if (pcap_setfilter(pcap_, &bpfpgm_) < 0) {
00274                 fprintf(stderr, "pcapnet obj(%s): couldn't set filter pgm",
00275                         name());
00276                 return -1;
00277         }
00278         return(bpfpgm_.bf_len);
00279 }

virtual double PcapNetwork::gents pcap_pkthdr *   )  [pure virtual]
 

Implemented in PcapLiveNetwork, and PcapFileNetwork.

Referenced by recv().

int Network::mode  )  [inline, inherited]
 

Definition at line 71 of file net.h.

References Network::mode_.

Referenced by PcapFileNetwork::command(), PcapLiveNetwork::command(), IPNetwork::command(), TapAgent::linknet(), TCPTapAgent::recvpkt(), TapAgent::recvpkt(), IPTapAgent::recvpkt(), TCPTapAgent::sendpkt(), TapAgent::sendpkt(), and IPTapAgent::sendpkt().

00071 { return mode_; }

char * Network::modename int   )  [static, inherited]
 

Definition at line 154 of file net.cc.

Referenced by Network::command().

00155 {
00156         switch (mode) {
00157         case O_RDONLY:
00158                 return ("readonly");
00159         case O_WRONLY:
00160                 return ("writeonly");
00161         case O_RDWR:
00162                 return ("readwrite");
00163         }
00164         return ("unknown");
00165 }

int Network::nonblock int  fd  )  [static, inherited]
 

Definition at line 111 of file net.cc.

Referenced by UDPIPNetwork::open().

00112 {       
00113 #ifdef WIN32
00114         u_long flag = 1;
00115         if (ioctlsocket(fd, FIONBIO, &flag) == -1) {
00116                 fprintf(stderr,
00117                     "Network::nonblock(): ioctlsocket: FIONBIO: %lu\n",
00118                     GetLastError());
00119                 return -1;
00120         }
00121 #else
00122         int flags;
00123         if ((flags = fcntl(fd, F_GETFL, 0)) < 0) {
00124                 perror("Network::nonblock(): fcntl");
00125                 return (-1);
00126         }
00127 #if defined(hpux) || defined(__hpux)
00128         flags |= O_NONBLOCK;
00129 #else
00130         flags |= O_NONBLOCK|O_NDELAY;
00131 #endif
00132         if (fcntl(fd, F_SETFL, flags) == -1) {
00133                 perror("Network::nonblock(): fcntl: F_SETFL");
00134                 return -1;
00135         }
00136 #endif
00137         return 0;
00138 }

virtual int PcapNetwork::open int  mode,
const char * 
[pure virtual]
 

Implemented in PcapLiveNetwork, and PcapFileNetwork.

int Network::parsemode const char *   )  [static, inherited]
 

Definition at line 141 of file net.cc.

Referenced by PcapFileNetwork::command(), PcapLiveNetwork::command(), and IPNetwork::command().

00142 {
00143         if (strcmp(mname, "readonly") == 0) {
00144                 return (O_RDONLY);
00145         } else if (strcmp(mname, "readwrite") == 0) {
00146                 return (O_RDWR);
00147         } else if (strcmp(mname, "writeonly") == 0) {
00148                 return (O_WRONLY);
00149         }
00150         return (::atoi(mname));
00151 }

void PcapNetwork::phandler u_char *  u,
const pcap_pkthdr *  h,
const u_char *  p
[static, protected]
 

Definition at line 320 of file net-pcap.cc.

References pcap_singleton::hdr, and pcap_singleton::pkt.

Referenced by recv().

00321 {
00322         pcap_singleton *ps = (pcap_singleton*) userdata;
00323         ps->hdr = (pcap_pkthdr*)ph;
00324         ps->pkt = (u_char*)pkt;
00325 }

void PcapNetwork::phandler_callback u_char *  u,
const pcap_pkthdr *  h,
const u_char *  p
[static, protected]
 

Definition at line 328 of file net-pcap.cc.

References Packet::accessdata(), Packet::alloc(), pcap_singleton_callback::callback, pcap_singleton_callback::clientdata, pcap_singleton_callback::net, pcnt_, skiphdr(), and t_firstpkt_.

Referenced by recv().

00329 {
00330         pcap_singleton_callback *ps = (pcap_singleton_callback*) userdata;      
00331 
00332         Packet *p = Packet::alloc(ph->caplen);
00333         PcapNetwork *inst = ps->net;
00334         
00335         if (++(inst->pcnt_) == 1) {
00336                 // mark time stamp of first pkt
00337                 inst->t_firstpkt_ = ph->ts.tv_sec + ph->ts.tv_usec * 0.000001;
00338         }
00339 
00340         // link layer header will be placed at the beginning from pcap
00341         int s = inst->skiphdr();        // go to IP header
00342         memcpy(p->accessdata(), pkt + s, ph->caplen - s);
00343 
00344         ps->callback(ps->clientdata, p, ph->ts);
00345 }

Here is the call graph for this function:

int PcapNetwork::rchannel  )  [inline, virtual]
 

Implements Network.

Definition at line 112 of file net-pcap.cc.

References pfd_.

00112 { return(pfd_); }

int PcapNetwork::recv netpkt_handler  callback,
void *  clientdata
[virtual]
 

Reimplemented from Network.

Definition at line 398 of file net-pcap.cc.

References pcap_, phandler_callback(), PNET_PSTATE_ACTIVE, and state_.

00399 {
00400         if (state_ != PNET_PSTATE_ACTIVE) {
00401                 fprintf(stderr, "warning: net/pcap obj(%s) read-- not active\n",
00402                         name());
00403                 return -1;
00404         }
00405 
00406         int pktcnt = -1;                // all in buffer, or until error
00407         int np;                 // counts # of pkts dispatched
00408         pcap_singleton_callback ps = { callback, clientdata, this };
00409         
00410         np = pcap_dispatch(pcap_, pktcnt, phandler_callback, (u_char *)&ps);
00411 
00412 #ifdef MY_OWN_PCAP      // directly access pcap_t's member
00413         assert( pcap_->cc == 0 ); // i.e. we have emptied pcap's buffer
00414 #endif // MY_OWN_PCAP
00415         return np;
00416 }

Here is the call graph for this function:

int PcapNetwork::recv u_char *  buf,
int  len,
sockaddr &  ,
double & 
[virtual]
 

Implements Network.

Definition at line 348 of file net-pcap.cc.

References gents(), pcap_singleton::hdr, len, MIN, pcap_, pcnt_, phandler(), pcap_singleton::pkt, PNET_PSTATE_ACTIVE, skiphdr(), state_, t_firstpkt_, and ts.

00349 {
00350 
00351         if (state_ != PNET_PSTATE_ACTIVE) {
00352                 fprintf(stderr, "warning: net/pcap obj(%s) read-- not active\n",
00353                         name());
00354                 return -1;
00355         }
00356 
00357         int pktcnt = 1;         // all in buffer, or until error
00358         int np;                 // counts # of pkts dispatched
00359         pcap_singleton ps = { 0, 0 };
00360         np = pcap_dispatch(pcap_, pktcnt, phandler, (u_char*) &ps);
00361         if (np < 0) {
00362                 fprintf(stderr,
00363                         "PcapNetwork(%s): recv: pcap_dispatch: %s\n",
00364                             name(), pcap_strerror(errno));
00365                 return (np);
00366         } else if (np == 0) {
00367                 /* we get here on EOF of a Pcap/File Network */
00368                 return (np);
00369         } else if (np != pktcnt) {
00370                 fprintf(stderr,
00371                         "PcapNetwork(%s): warning: recv: pcap_dispatch: requested pktcnt (%d) doesn't match actual (%d)\n",
00372                             name(), pktcnt, np);
00373         }
00374 
00375         pcap_pkthdr* ph = ps.hdr;
00376 
00377         if (ph == NULL || ps.pkt == NULL) {
00378                 fprintf(stderr,
00379                         "PcapNetwork(%s): recv: pcap_dispatch: no packet present\n",
00380                             name());
00381                 return (-1);
00382         }
00383 
00384         if (++pcnt_ == 1) {
00385                 // mark time stamp of first pkt
00386                 t_firstpkt_ = ph->ts.tv_sec + ph->ts.tv_usec * 0.000001;
00387         }
00388 
00389         int n = MIN(ph->caplen, (unsigned)len);
00390         ts = gents(ph); // mark with timestamp
00391         // link layer header will be placed at the beginning from pcap
00392         int s = skiphdr();      // go to IP header
00393         memcpy(buf, ps.pkt + s, n - s);
00394         return n - s;
00395 }

Here is the call graph for this function:

void PcapNetwork::reset  ) 
 

Definition at line 243 of file net-pcap.cc.

References errbuf_, pcap_, pcnt_, pfd_, PNET_PSTATE_INACTIVE, srcname_, and state_.

Referenced by close(), and PcapLiveNetwork::PcapLiveNetwork().

00244 {
00245         state_ = PNET_PSTATE_INACTIVE;
00246         pfd_ = -1;
00247         pcap_ = NULL;
00248         *errbuf_ = '\0';
00249         *srcname_ = '\0';
00250         pcnt_ = 0;
00251 }

int PcapNetwork::schannel  )  [inline, virtual]
 

Implements Network.

Definition at line 113 of file net-pcap.cc.

References pfd_.

00113 { return(pfd_); }

int PcapNetwork::send u_char *  buf,
int  len
[virtual]
 

Implements Network.

Definition at line 420 of file net-pcap.cc.

References len, and pfd_.

00421 {
00422         int n;
00423 
00424         if ((n = write(pfd_, buf, len)) < 0)
00425                 perror("write to pcap fd");
00426 
00427         return n;
00428 }

virtual int PcapNetwork::skiphdr  )  [pure virtual]
 

Implemented in PcapLiveNetwork, and PcapFileNetwork.

Referenced by phandler_callback(), and recv().

int PcapNetwork::stat_pdrops  ) 
 

Definition at line 293 of file net-pcap.cc.

References pcap_, and pcs_.

Referenced by command().

00294 {
00295         if (pcap_stats(pcap_, &pcs_) < 0)
00296                 return (-1);
00297 
00298         return (pcs_.ps_drop);
00299 }

int PcapNetwork::stat_pkts  ) 
 

Definition at line 283 of file net-pcap.cc.

References pcap_, and pcs_.

Referenced by command().

00284 {
00285         if (pcap_stats(pcap_, &pcs_) < 0)
00286                 return (-1);
00287 
00288         return (pcs_.ps_recv);
00289 }


Member Data Documentation

struct bpf_program PcapNetwork::bpfpgm_ [protected]
 

Definition at line 144 of file net-pcap.cc.

Referenced by filter().

char PcapNetwork::errbuf_[PCAP_ERRBUF_SIZE] [protected]
 

Definition at line 137 of file net-pcap.cc.

Referenced by PcapLiveNetwork::autodevname(), PcapFileNetwork::open(), PcapLiveNetwork::open(), and reset().

unsigned int PcapNetwork::local_netmask_ [protected]
 

Definition at line 147 of file net-pcap.cc.

Referenced by filter(), PcapLiveNetwork::open(), and PcapNetwork().

int Network::mode_ [protected, inherited]
 

Definition at line 76 of file net.h.

Referenced by UDPIPNetwork::bind(), Network::command(), UDPIPNetwork::connect(), Network::mode(), Network::Network(), PcapFileNetwork::open(), PcapLiveNetwork::open(), IPNetwork::open(), UDPIPNetwork::open(), IPNetwork::recv(), and IPNetwork::reset().

double PcapNetwork::offset_
 

Definition at line 129 of file net-pcap.cc.

Referenced by PcapFileNetwork::bindvars(), PcapLiveNetwork::bindvars(), and PcapFileNetwork::gents().

int PcapNetwork::optimize_ [protected]
 

Definition at line 142 of file net-pcap.cc.

Referenced by bindvars(), and filter().

pcap_t* PcapNetwork::pcap_ [protected]
 

Definition at line 143 of file net-pcap.cc.

Referenced by close(), filter(), PcapFileNetwork::open(), PcapLiveNetwork::open(), recv(), reset(), stat_pdrops(), and stat_pkts().

int PcapNetwork::pcnt_ [protected]
 

Definition at line 140 of file net-pcap.cc.

Referenced by PcapNetwork(), phandler_callback(), recv(), and reset().

struct pcap_stat PcapNetwork::pcs_ [protected]
 

Definition at line 145 of file net-pcap.cc.

Referenced by stat_pdrops(), and stat_pkts().

int PcapNetwork::pfd_ [protected]
 

Definition at line 139 of file net-pcap.cc.

Referenced by PcapFileNetwork::open(), PcapLiveNetwork::open(), PcapNetwork(), rchannel(), reset(), schannel(), and send().

char PcapNetwork::srcname_[PATH_MAX] [protected]
 

Definition at line 138 of file net-pcap.cc.

Referenced by PcapLiveNetwork::command(), command(), PcapFileNetwork::open(), PcapLiveNetwork::open(), and reset().

int PcapNetwork::state_ [protected]
 

Definition at line 141 of file net-pcap.cc.

Referenced by close(), command(), PcapFileNetwork::open(), PcapLiveNetwork::open(), recv(), and reset().

double PcapNetwork::t_firstpkt_
 

Definition at line 130 of file net-pcap.cc.

Referenced by PcapFileNetwork::gents(), PcapNetwork(), phandler_callback(), and recv().


The documentation for this class was generated from the following file:
Generated on Tue Apr 20 13:07:13 2004 for NS2.26SourcesOriginal by doxygen 1.3.3