## modules/up/src/Core/network/Pcap.cc

/* [<][>][^][v][top][bottom][index][help] */

## FUNCTIONS

This source file includes following functions.

1. pcapReadHandler
2. pcapProcessHandler
3. Pcap
4. Pcap
5. issueRead

       1 //
       2 // $Id: Pcap.cc,v 1.1.1.1 2000/03/10 16:32:19 engin Exp $
       3 //
       4 // Author(s): Cengiz Alaettinoglu
       5
       6 #ifdef HAVE_CONFIG_H
       7 #include <config.h>
       8 #endif
       9
      10 #if defined(HAVE_PCAP_H)
      11
      12 #include "util/Types.hh"
      13 #include "util/Trail.hh"
      14 #include "util/Handler.hh"
      15 #include "util/Buffer.hh"
      16 #include "sys/File.hh"
      17 #include "sys/Pipe.hh"
      18 #include "sys/Time.hh"
      19 #include "sched/Timer.hh"
      20 #include "sched/Dispatcher.hh"
      21
      22 #include "network/Pcap.hh"
      23 #include "network/Headers.hh"
      24 #include "network/Network.hh"
      25
      26 extern "C" {
      27 #include <sys/socket.h>
      28 #include <net/if.h>
      29     //#include <net/if_arp.h>
      30 #include <netinet/in.h>
      31 #include <netinet/if_ether.h>
      32 #include <netinet/in_systm.h>
      33     //#include <netinet/ip.h>
      34
      35 #include <pcap.h>
      36
      37 int     pcap_read(pcap_t *, int cnt, pcap_handler, u_char *);
      38 }
      39
      40 static int tuba=0, idrp=0; //some pcaps want these flags declared
      41
      42 // Locals
      43 static TraceCode        tracePcap("network");
      44 const int DefaultSnapLen = 68;
      45
      46 static void pcapReadHandler(void* pcap, void*) {
         /* [<][>][^][v][top][bottom][index][help] */
      47    ((Pcap *) pcap)->issueRead();
      48 }
      49
      50 static void pcapProcessHandler(u_char *pcap,
         /* [<][>][^][v][top][bottom][index][help] */
      51                                const struct pcap_pkthdr *h,
      52                                const u_char *p) {
      53     u_int caplen = h->caplen;
      54     u_int length = h->len;
      55     u_short pay_type;
      56
      57     switch (((Pcap*)pcap)->datalink_) {
      58     case DLT_EN10MB :
      59     case DLT_IEEE802 :
      60         if (h->caplen < sizeof(struct ether_header)) {
      61             TRACE(tracePcap, "ethernet header not found\n");
      62             return;
      63         }
      64         length-= sizeof(struct ether_header);
      65         caplen-= sizeof(struct ether_header);
      66         pay_type = ntohs(((struct ether_header *)p)->ether_type);
      67         p += sizeof(struct ether_header);
      68         break;
      69     case DLT_ATM_RFC1483 :
      70         if (h->caplen < 8) {
      71             TRACE(tracePcap, "ATM header not found\n");
      72             return;
      73         }
      74         if (p[4] == 0xaa || p[5] == 0xaa || p[6] == 0x03) {
      75             /* if first 4 bytes are cookie/vpci */
      76             p+= 4;
      77             length-= 4;
      78             caplen-= 4;
      79         } else if (p[0] != 0xaa || p[1] != 0xaa || p[2] != 0x03) {
      80             /*XXX assume 802.6 MAC header from fore driver */
      81             p+= 20;
      82             length-= 20;
      83             caplen-= 20;
      84         }
      85         pay_type = p[6] << 8 | p[7];
      86         length-= 8;
      87         caplen-= 8;
      88         p+= 8;
      89         break;
      90     case DLT_SLIP :
      91         //case DLT_SLIP_BSDOS :
      92     case DLT_PPP :
      93         //case DLT_PPP_BSDOS :
      94     case DLT_FDDI :
      95     case DLT_NULL :
      96         //case DLT_RAW :
      97         ERROR("unsupported link layer %d\n", ((Pcap*)pcap)->datalink_);
      98         return;
      99     }
     100     if (pay_type != ETHERTYPE_IP)
     101         return;
     102     if (caplen < sizeof(IP)) {
     103         TRACE(tracePcap, "ip header not found\n");
     104         return;
     105     }
     106     IP *ip= (IP*)p;
     107     ip->ntoh();
     108
     109     Address src(ip->source);
     110     Address dst(ip->destination);
     111
     112    ((Pcap*) pcap)->receive(src, dst, ip->protocol);
     113 }
     114
     115 Pcap::Pcap(char *filter, char *device) : ListNode() {
         /* [<][>][^][v][top][bottom][index][help] */
     116    int snaplen = DefaultSnapLen;
     117    int promiscuous = 1;
     118    char ebuf[PCAP_ERRBUF_SIZE];
     119    bpf_u_int32 localnet, netmask;
     120    struct bpf_program fcode;
     121    int Oflag = 1;                       /* run filter code optimizer */
     122    int i;
     123
     124    if (device == NULL) {
     125       device = pcap_lookupdev(ebuf);
     126       if (device == NULL)
     127          FATAL("%s\n", ebuf);
     128    }
     129
     130    pcap_descriptor = pcap_open_live(device, snaplen, promiscuous, 1000, ebuf);
     131    if (pcap_descriptor == NULL)
     132       FATAL("%s\n", ebuf);
     133
     134    datalink_= pcap_datalink(pcap_descriptor);
     135
     136    i = pcap_snapshot(pcap_descriptor);
     137    if (snaplen < i) {
     138       ERROR("snaplen raised from %d to %d", snaplen, i);
     139       snaplen = i;
     140    }
     141
     142    if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
     143       localnet = 0;
     144       netmask = 0;
     145       ERROR("%s\n", ebuf);
     146    }
     147
     148    if (pcap_compile(pcap_descriptor, &fcode, filter, Oflag, netmask) < 0)
     149       FATAL("%s\n", pcap_geterr(pcap_descriptor));
     150
     151    if (pcap_setfilter(pcap_descriptor, &fcode) < 0)
     152       FATAL("%s\n", pcap_geterr(pcap_descriptor));
     153
     154    Handler rH(pcapReadHandler, this);
     155    Handler nH((CallBackFunc)NULL, (void *)NULL);
     156    // the pcap_t points to memory where the first word is the file descriptor
     157    file = new File(* (int *) pcap_descriptor, FileModeReadOnly, rH, nH);
     158 }
     159
     160 Pcap::~Pcap() {
         /* [<][>][^][v][top][bottom][index][help] */
     161    pcap_close(pcap_descriptor);
     162    delete file;
     163 }
     164
     165 void Pcap::issueRead() {
         /* [<][>][^][v][top][bottom][index][help] */
     166    if (pcap_read(pcap_descriptor,
     167                  1,
     168                  pcapProcessHandler,
     169                  (u_char *) this) < 0)
     170       TRACE(tracePcap, "pcap: %s\n", pcap_geterr(pcap_descriptor));
     171 }
     172
     173 //  Copyright (c) 1994 by the University of Southern California.
     174 //  All rights reserved.
     175 //
     176 //  Permission to use, copy, modify, and distribute this software and
     177 //  its documentation in source and binary forms for lawful
     178 //  non-commercial purposes and without fee is hereby granted, provided
     179 //  that the above copyright notice appear in all copies and that both
     180 //  the copyright notice and this permission notice appear in supporting
     181 //  documentation, and that any documentation, advertising materials,
     182 //  and other materials related to such distribution and use acknowledge
     183 //  that the software was developed by the University of Southern
     184 //  California and/or Information Sciences Institute.
     185 //  The name of the University of Southern California may not
     186 //  be used to endorse or promote products derived from this software
     187 //  without specific prior written permission.
     188 //
     189 //  THE UNIVERSITY OF SOUTHERN CALIFORNIA DOES NOT MAKE ANY REPRESENTATIONS
     190 //  ABOUT THE SUITABILITY OF THIS SOFTWARE FOR ANY PURPOSE.  THIS SOFTWARE IS
     191 //  PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
     192 //  INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
     193 //  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, TITLE, AND
     194 //  NON-INFRINGEMENT.
     195 //
     196 //  IN NO EVENT SHALL USC, OR ANY OTHER CONTRIBUTOR BE LIABLE FOR ANY
     197 //  SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES, WHETHER IN CONTRACT,
     198 //  TORT, OR OTHER FORM OF ACTION, ARISING OUT OF OR IN CONNECTION WITH,
     199 //  THE USE OR PERFORMANCE OF THIS SOFTWARE.
     200 //
     201 //  Questions concerning this software should be directed to
     202 //  scan@isi.edu.
     203 //
     204
     205
     206 #endif HAVE_PCAP
     207

/* [<][>][^][v][top][bottom][index][help] */