Home | History | Annotate | Download | only in libpcap
      1 /*
      2  *  This file is part of DOS-libpcap
      3  *  Ported to DOS/DOSX by G. Vanem <gvanem (at) yahoo.no>
      4  *
      5  *  pcap-dos.c: Interface to PKTDRVR, NDIS2 and 32-bit pmode
      6  *              network drivers.
      7  */
      8 
      9 #include <stdio.h>
     10 #include <stdlib.h>
     11 #include <string.h>
     12 #include <signal.h>
     13 #include <float.h>
     14 #include <fcntl.h>
     15 #include <io.h>
     16 
     17 #if defined(USE_32BIT_DRIVERS)
     18   #include "msdos/pm_drvr/pmdrvr.h"
     19   #include "msdos/pm_drvr/pci.h"
     20   #include "msdos/pm_drvr/bios32.h"
     21   #include "msdos/pm_drvr/module.h"
     22   #include "msdos/pm_drvr/3c501.h"
     23   #include "msdos/pm_drvr/3c503.h"
     24   #include "msdos/pm_drvr/3c509.h"
     25   #include "msdos/pm_drvr/3c59x.h"
     26   #include "msdos/pm_drvr/3c515.h"
     27   #include "msdos/pm_drvr/3c90x.h"
     28   #include "msdos/pm_drvr/3c575_cb.h"
     29   #include "msdos/pm_drvr/ne.h"
     30   #include "msdos/pm_drvr/wd.h"
     31   #include "msdos/pm_drvr/accton.h"
     32   #include "msdos/pm_drvr/cs89x0.h"
     33   #include "msdos/pm_drvr/rtl8139.h"
     34   #include "msdos/pm_drvr/ne2k-pci.h"
     35 #endif
     36 
     37 #include "pcap.h"
     38 #include "pcap-dos.h"
     39 #include "pcap-int.h"
     40 #include "msdos/pktdrvr.h"
     41 
     42 #ifdef USE_NDIS2
     43 #include "msdos/ndis2.h"
     44 #endif
     45 
     46 #include <arpa/inet.h>
     47 #include <net/if.h>
     48 #include <net/if_arp.h>
     49 #include <net/if_ether.h>
     50 #include <net/if_packe.h>
     51 #include <tcp.h>
     52 
     53 #if defined(USE_32BIT_DRIVERS)
     54   #define FLUSHK()       do { _printk_safe = 1; _printk_flush(); } while (0)
     55   #define NDIS_NEXT_DEV  &rtl8139_dev
     56 
     57   static char *rx_pool = NULL;
     58   static void init_32bit (void);
     59 
     60   static int  pktq_init     (struct rx_ringbuf *q, int size, int num, char *pool);
     61   static int  pktq_check    (struct rx_ringbuf *q);
     62   static int  pktq_inc_out  (struct rx_ringbuf *q);
     63   static int  pktq_in_index (struct rx_ringbuf *q) LOCKED_FUNC;
     64   static void pktq_clear    (struct rx_ringbuf *q) LOCKED_FUNC;
     65 
     66   static struct rx_elem *pktq_in_elem  (struct rx_ringbuf *q) LOCKED_FUNC;
     67   static struct rx_elem *pktq_out_elem (struct rx_ringbuf *q);
     68 
     69 #else
     70   #define FLUSHK()      ((void)0)
     71   #define NDIS_NEXT_DEV  NULL
     72 #endif
     73 
     74 /*
     75  * Internal variables/functions in Watt-32
     76  */
     77 extern WORD  _pktdevclass;
     78 extern BOOL  _eth_is_init;
     79 extern int   _w32_dynamic_host;
     80 extern int   _watt_do_exit;
     81 extern int   _watt_is_init;
     82 extern int   _w32__bootp_on, _w32__dhcp_on, _w32__rarp_on, _w32__do_mask_req;
     83 extern void (*_w32_usr_post_init) (void);
     84 extern void (*_w32_print_hook)();
     85 
     86 extern void dbug_write (const char *);  /* Watt-32 lib, pcdbug.c */
     87 extern int  pkt_get_mtu (void);
     88 
     89 static int ref_count = 0;
     90 
     91 static u_long mac_count    = 0;
     92 static u_long filter_count = 0;
     93 
     94 static volatile BOOL exc_occured = 0;
     95 
     96 static struct device *handle_to_device [20];
     97 
     98 static int  pcap_activate_dos (pcap_t *p);
     99 static int  pcap_read_dos (pcap_t *p, int cnt, pcap_handler callback,
    100                            u_char *data);
    101 static void pcap_cleanup_dos (pcap_t *p);
    102 static int  pcap_stats_dos (pcap_t *p, struct pcap_stat *ps);
    103 static int  pcap_sendpacket_dos (pcap_t *p, const void *buf, size_t len);
    104 static int  pcap_setfilter_dos (pcap_t *p, struct bpf_program *fp);
    105 
    106 static int  ndis_probe (struct device *dev);
    107 static int  pkt_probe  (struct device *dev);
    108 
    109 static void close_driver (void);
    110 static int  init_watt32 (struct pcap *pcap, const char *dev_name, char *err_buf);
    111 static int  first_init (const char *name, char *ebuf, int promisc);
    112 
    113 static void watt32_recv_hook (u_char *dummy, const struct pcap_pkthdr *pcap,
    114                               const u_char *buf);
    115 
    116 /*
    117  * These are the device we always support
    118  */
    119 static struct device ndis_dev = {
    120               "ndis",
    121               "NDIS2 LanManager",
    122               0,
    123               0,0,0,0,0,0,
    124               NDIS_NEXT_DEV,  /* NULL or a 32-bit device */
    125               ndis_probe
    126             };
    127 
    128 static struct device pkt_dev = {
    129               "pkt",
    130               "Packet-Driver",
    131               0,
    132               0,0,0,0,0,0,
    133               &ndis_dev,
    134               pkt_probe
    135             };
    136 
    137 static struct device *get_device (int fd)
    138 {
    139   if (fd <= 0 || fd >= sizeof(handle_to_device)/sizeof(handle_to_device[0]))
    140      return (NULL);
    141   return handle_to_device [fd-1];
    142 }
    143 
    144 /*
    145  * Private data for capturing on MS-DOS.
    146  */
    147 struct pcap_dos {
    148 	void (*wait_proc)(void); /* call proc while waiting */
    149 	struct pcap_stat stat;
    150 };
    151 
    152 pcap_t *pcap_create_interface (const char *device _U_, char *ebuf)
    153 {
    154 	pcap_t *p;
    155 
    156 	p = pcap_create_common(ebuf, sizeof (struct pcap_dos));
    157 	if (p == NULL)
    158 		return (NULL);
    159 
    160 	p->activate_op = pcap_activate_dos;
    161 	return (p);
    162 }
    163 
    164 /*
    165  * Open MAC-driver with name 'device_name' for live capture of
    166  * network packets.
    167  */
    168 static int pcap_activate_dos (pcap_t *pcap)
    169 {
    170   if (pcap->opt.rfmon) {
    171     /*
    172      * No monitor mode on DOS.
    173      */
    174     return (PCAP_ERROR_RFMON_NOTSUP);
    175   }
    176 
    177   if (pcap->snapshot < ETH_MIN+8)
    178       pcap->snapshot = ETH_MIN+8;
    179 
    180   if (pcap->snapshot > ETH_MAX)   /* silently accept and truncate large MTUs */
    181       pcap->snapshot = ETH_MAX;
    182 
    183   pcap->linktype          = DLT_EN10MB;  /* !! */
    184   pcap->cleanup_op        = pcap_cleanup_dos;
    185   pcap->read_op           = pcap_read_dos;
    186   pcap->stats_op          = pcap_stats_dos;
    187   pcap->inject_op         = pcap_sendpacket_dos;
    188   pcap->setfilter_op      = pcap_setfilter_dos;
    189   pcap->setdirection_op   = NULL;  /* Not implemented.*/
    190   pcap->fd                = ++ref_count;
    191 
    192   pcap->bufsize = ETH_MAX+100;     /* add some margin */
    193   pcap->buffer = calloc (pcap->bufsize, 1);
    194 
    195   if (pcap->fd == 1)  /* first time we're called */
    196   {
    197     if (!init_watt32(pcap, pcap->opt.device, pcap->errbuf) ||
    198         !first_init(pcap->opt.device, pcap->errbuf, pcap->opt.promisc))
    199     {
    200       return (PCAP_ERROR);
    201     }
    202     atexit (close_driver);
    203   }
    204   else if (stricmp(active_dev->name,pcap->opt.device))
    205   {
    206     pcap_snprintf (pcap->errbuf, PCAP_ERRBUF_SIZE,
    207                    "Cannot use different devices simultaneously "
    208                    "(`%s' vs. `%s')", active_dev->name, pcap->opt.device);
    209     return (PCAP_ERROR);
    210   }
    211   handle_to_device [pcap->fd-1] = active_dev;
    212   return (0);
    213 }
    214 
    215 /*
    216  * Poll the receiver queue and call the pcap callback-handler
    217  * with the packet.
    218  */
    219 static int
    220 pcap_read_one (pcap_t *p, pcap_handler callback, u_char *data)
    221 {
    222   struct pcap_dos *pd = p->priv;
    223   struct pcap_pkthdr pcap;
    224   struct timeval     now, expiry = { 0,0 };
    225   int    rx_len = 0;
    226 
    227   if (p->opt.timeout > 0)
    228   {
    229     gettimeofday2 (&now, NULL);
    230     expiry.tv_usec = now.tv_usec + 1000UL * p->opt.timeout;
    231     expiry.tv_sec  = now.tv_sec;
    232     while (expiry.tv_usec >= 1000000L)
    233     {
    234       expiry.tv_usec -= 1000000L;
    235       expiry.tv_sec++;
    236     }
    237   }
    238 
    239   while (!exc_occured)
    240   {
    241     volatile struct device *dev; /* might be reset by sig_handler */
    242 
    243     dev = get_device (p->fd);
    244     if (!dev)
    245        break;
    246 
    247     PCAP_ASSERT (dev->copy_rx_buf || dev->peek_rx_buf);
    248     FLUSHK();
    249 
    250     /* If driver has a zero-copy receive facility, peek at the queue,
    251      * filter it, do the callback and release the buffer.
    252      */
    253     if (dev->peek_rx_buf)
    254     {
    255       PCAP_ASSERT (dev->release_rx_buf);
    256       rx_len = (*dev->peek_rx_buf) (&p->buffer);
    257     }
    258     else
    259     {
    260       rx_len = (*dev->copy_rx_buf) (p->buffer, p->snapshot);
    261     }
    262 
    263     if (rx_len > 0)  /* got a packet */
    264     {
    265       mac_count++;
    266 
    267       FLUSHK();
    268 
    269       pcap.caplen = min (rx_len, p->snapshot);
    270       pcap.len    = rx_len;
    271 
    272       if (callback &&
    273           (!p->fcode.bf_insns || bpf_filter(p->fcode.bf_insns, p->buffer, pcap.len, pcap.caplen)))
    274       {
    275         filter_count++;
    276 
    277         /* Fix-me!! Should be time of arrival. Not time of
    278          * capture.
    279          */
    280         gettimeofday2 (&pcap.ts, NULL);
    281         (*callback) (data, &pcap, p->buffer);
    282       }
    283 
    284       if (dev->release_rx_buf)
    285         (*dev->release_rx_buf) (p->buffer);
    286 
    287       if (pcap_pkt_debug > 0)
    288       {
    289         if (callback == watt32_recv_hook)
    290              dbug_write ("pcap_recv_hook\n");
    291         else dbug_write ("pcap_read_op\n");
    292       }
    293       FLUSHK();
    294       return (1);
    295     }
    296 
    297     /* Has "pcap_breakloop()" been called?
    298      */
    299     if (p->break_loop) {
    300       /*
    301        * Yes - clear the flag that indicates that it
    302        * has, and return -2 to indicate that we were
    303        * told to break out of the loop.
    304        */
    305       p->break_loop = 0;
    306       return (-2);
    307     }
    308 
    309     /* If not to wait for a packet or pcap_cleanup_dos() called from
    310      * e.g. SIGINT handler, exit loop now.
    311      */
    312     if (p->opt.timeout <= 0 || (volatile int)p->fd <= 0)
    313        break;
    314 
    315     gettimeofday2 (&now, NULL);
    316 
    317     if (timercmp(&now, &expiry, >))
    318        break;
    319 
    320 #ifndef DJGPP
    321     kbhit();    /* a real CPU hog */
    322 #endif
    323 
    324     if (pd->wait_proc)
    325       (*pd->wait_proc)();     /* call yield func */
    326   }
    327 
    328   if (rx_len < 0)            /* receive error */
    329   {
    330     pd->stat.ps_drop++;
    331 #ifdef USE_32BIT_DRIVERS
    332     if (pcap_pkt_debug > 1)
    333        printk ("pkt-err %s\n", pktInfo.error);
    334 #endif
    335     return (-1);
    336   }
    337   return (0);
    338 }
    339 
    340 static int
    341 pcap_read_dos (pcap_t *p, int cnt, pcap_handler callback, u_char *data)
    342 {
    343   int rc, num = 0;
    344 
    345   while (num <= cnt || PACKET_COUNT_IS_UNLIMITED(cnt))
    346   {
    347     if (p->fd <= 0)
    348        return (-1);
    349     rc = pcap_read_one (p, callback, data);
    350     if (rc > 0)
    351        num++;
    352     if (rc < 0)
    353        break;
    354     _w32_os_yield();  /* allow SIGINT generation, yield to Win95/NT */
    355   }
    356   return (num);
    357 }
    358 
    359 /*
    360  * Return network statistics
    361  */
    362 static int pcap_stats_dos (pcap_t *p, struct pcap_stat *ps)
    363 {
    364   struct net_device_stats *stats;
    365   struct pcap_dos         *pd;
    366   struct device           *dev = p ? get_device(p->fd) : NULL;
    367 
    368   if (!dev)
    369   {
    370     strcpy (p->errbuf, "illegal pcap handle");
    371     return (-1);
    372   }
    373 
    374   if (!dev->get_stats || (stats = (*dev->get_stats)(dev)) == NULL)
    375   {
    376     strcpy (p->errbuf, "device statistics not available");
    377     return (-1);
    378   }
    379 
    380   FLUSHK();
    381 
    382   pd = p->priv;
    383   pd->stat.ps_recv   = stats->rx_packets;
    384   pd->stat.ps_drop  += stats->rx_missed_errors;
    385   pd->stat.ps_ifdrop = stats->rx_dropped +  /* queue full */
    386                          stats->rx_errors;    /* HW errors */
    387   if (ps)
    388      *ps = pd->stat;
    389 
    390   return (0);
    391 }
    392 
    393 /*
    394  * Return detailed network/device statistics.
    395  * May be called after 'dev->close' is called.
    396  */
    397 int pcap_stats_ex (pcap_t *p, struct pcap_stat_ex *se)
    398 {
    399   struct device *dev = p ? get_device (p->fd) : NULL;
    400 
    401   if (!dev || !dev->get_stats)
    402   {
    403     strlcpy (p->errbuf, "detailed device statistics not available",
    404              PCAP_ERRBUF_SIZE);
    405     return (-1);
    406   }
    407 
    408   if (!strnicmp(dev->name,"pkt",3))
    409   {
    410     strlcpy (p->errbuf, "pktdrvr doesn't have detailed statistics",
    411              PCAP_ERRBUF_SIZE);
    412     return (-1);
    413   }
    414   memcpy (se, (*dev->get_stats)(dev), sizeof(*se));
    415   return (0);
    416 }
    417 
    418 /*
    419  * Simply store the filter-code for the pcap_read_dos() callback
    420  * Some day the filter-code could be handed down to the active
    421  * device (pkt_rx1.s or 32-bit device interrupt handler).
    422  */
    423 static int pcap_setfilter_dos (pcap_t *p, struct bpf_program *fp)
    424 {
    425   if (!p)
    426      return (-1);
    427   p->fcode = *fp;
    428   return (0);
    429 }
    430 
    431 /*
    432  * Return # of packets received in pcap_read_dos()
    433  */
    434 u_long pcap_mac_packets (void)
    435 {
    436   return (mac_count);
    437 }
    438 
    439 /*
    440  * Return # of packets passed through filter in pcap_read_dos()
    441  */
    442 u_long pcap_filter_packets (void)
    443 {
    444   return (filter_count);
    445 }
    446 
    447 /*
    448  * Close pcap device. Not called for offline captures.
    449  */
    450 static void pcap_cleanup_dos (pcap_t *p)
    451 {
    452   struct pcap_dos *pd;
    453 
    454   if (!exc_occured)
    455   {
    456     pd = p->priv;
    457     if (pcap_stats(p,NULL) < 0)
    458        pd->stat.ps_drop = 0;
    459     if (!get_device(p->fd))
    460        return;
    461 
    462     handle_to_device [p->fd-1] = NULL;
    463     p->fd = 0;
    464     if (ref_count > 0)
    465         ref_count--;
    466     if (ref_count > 0)
    467        return;
    468   }
    469   close_driver();
    470 }
    471 
    472 /*
    473  * Return the name of the 1st network interface,
    474  * or NULL if none can be found.
    475  */
    476 char *pcap_lookupdev (char *ebuf)
    477 {
    478   struct device *dev;
    479 
    480 #ifdef USE_32BIT_DRIVERS
    481   init_32bit();
    482 #endif
    483 
    484   for (dev = (struct device*)dev_base; dev; dev = dev->next)
    485   {
    486     PCAP_ASSERT (dev->probe);
    487 
    488     if ((*dev->probe)(dev))
    489     {
    490       FLUSHK();
    491       probed_dev = (struct device*) dev; /* remember last probed device */
    492       return (char*) dev->name;
    493     }
    494   }
    495 
    496   if (ebuf)
    497      strcpy (ebuf, "No driver found");
    498   return (NULL);
    499 }
    500 
    501 /*
    502  * Gets localnet & netmask from Watt-32.
    503  */
    504 int pcap_lookupnet (const char *device, bpf_u_int32 *localnet,
    505                     bpf_u_int32 *netmask, char *errbuf)
    506 {
    507   DWORD mask, net;
    508 
    509   if (!_watt_is_init)
    510   {
    511     strcpy (errbuf, "pcap_open_offline() or pcap_activate() must be "
    512                     "called first");
    513     return (-1);
    514   }
    515 
    516   mask  = _w32_sin_mask;
    517   net = my_ip_addr & mask;
    518   if (net == 0)
    519   {
    520     if (IN_CLASSA(*netmask))
    521        net = IN_CLASSA_NET;
    522     else if (IN_CLASSB(*netmask))
    523        net = IN_CLASSB_NET;
    524     else if (IN_CLASSC(*netmask))
    525        net = IN_CLASSC_NET;
    526     else
    527     {
    528       pcap_snprintf (errbuf, PCAP_ERRBUF_SIZE, "inet class for 0x%lx unknown", mask);
    529       return (-1);
    530     }
    531   }
    532   *localnet = htonl (net);
    533   *netmask = htonl (mask);
    534 
    535   ARGSUSED (device);
    536   return (0);
    537 }
    538 
    539 /*
    540  * Get a list of all interfaces that are present and that we probe okay.
    541  * Returns -1 on error, 0 otherwise.
    542  * The list, as returned through "alldevsp", may be NULL if no interfaces
    543  * were up and could be opened.
    544  */
    545 int pcap_platform_finddevs  (pcap_if_t **alldevsp, char *errbuf)
    546 {
    547   struct device     *dev;
    548   struct sockaddr_in sa_ll_1, sa_ll_2;
    549   struct sockaddr   *addr, *netmask, *broadaddr, *dstaddr;
    550   pcap_if_t *devlist = NULL;
    551   int       ret = 0;
    552   size_t    addr_size = sizeof(*addr);
    553 
    554   for (dev = (struct device*)dev_base; dev; dev = dev->next)
    555   {
    556     PCAP_ASSERT (dev->probe);
    557 
    558     if (!(*dev->probe)(dev))
    559        continue;
    560 
    561     PCAP_ASSERT (dev->close);  /* set by probe routine */
    562     FLUSHK();
    563     (*dev->close) (dev);
    564 
    565     memset (&sa_ll_1, 0, sizeof(sa_ll_1));
    566     memset (&sa_ll_2, 0, sizeof(sa_ll_2));
    567     sa_ll_1.sin_family = AF_INET;
    568     sa_ll_2.sin_family = AF_INET;
    569 
    570     addr      = (struct sockaddr*) &sa_ll_1;
    571     netmask   = (struct sockaddr*) &sa_ll_1;
    572     dstaddr   = (struct sockaddr*) &sa_ll_1;
    573     broadaddr = (struct sockaddr*) &sa_ll_2;
    574     memset (&sa_ll_2.sin_addr, 0xFF, sizeof(sa_ll_2.sin_addr));
    575 
    576     if (pcap_add_if(&devlist, dev->name, dev->flags,
    577                     dev->long_name, errbuf) < 0)
    578     {
    579       ret = -1;
    580       break;
    581     }
    582 #if 0   /* Pkt drivers should have no addresses */
    583     if (add_addr_to_iflist(&devlist, dev->name, dev->flags, addr, addr_size,
    584                            netmask, addr_size, broadaddr, addr_size,
    585                            dstaddr, addr_size, errbuf) < 0)
    586     {
    587       ret = -1;
    588       break;
    589     }
    590 #endif
    591   }
    592 
    593   if (devlist && ret < 0)
    594   {
    595     pcap_freealldevs (devlist);
    596     devlist = NULL;
    597   }
    598   else
    599   if (!devlist)
    600      strcpy (errbuf, "No drivers found");
    601 
    602   *alldevsp = devlist;
    603   return (ret);
    604 }
    605 
    606 /*
    607  * pcap_assert() is mainly used for debugging
    608  */
    609 void pcap_assert (const char *what, const char *file, unsigned line)
    610 {
    611   FLUSHK();
    612   fprintf (stderr, "%s (%u): Assertion \"%s\" failed\n",
    613            file, line, what);
    614   close_driver();
    615   _exit (-1);
    616 }
    617 
    618 /*
    619  * For pcap_offline_read(): wait and yield between printing packets
    620  * to simulate the pace packets where actually recorded.
    621  */
    622 void pcap_set_wait (pcap_t *p, void (*yield)(void), int wait)
    623 {
    624   if (p)
    625   {
    626     struct pcap_dos *pd = p->priv;
    627 
    628     pd->wait_proc  = yield;
    629     p->opt.timeout = wait;
    630   }
    631 }
    632 
    633 /*
    634  * Initialise a named network device.
    635  */
    636 static struct device *
    637 open_driver (const char *dev_name, char *ebuf, int promisc)
    638 {
    639   struct device *dev;
    640 
    641   for (dev = (struct device*)dev_base; dev; dev = dev->next)
    642   {
    643     PCAP_ASSERT (dev->name);
    644 
    645     if (strcmp (dev_name,dev->name))
    646        continue;
    647 
    648     if (!probed_dev)   /* user didn't call pcap_lookupdev() first */
    649     {
    650       PCAP_ASSERT (dev->probe);
    651 
    652       if (!(*dev->probe)(dev))    /* call the xx_probe() function */
    653       {
    654         pcap_snprintf (ebuf, PCAP_ERRBUF_SIZE, "failed to detect device `%s'", dev_name);
    655         return (NULL);
    656       }
    657       probed_dev = dev;  /* device is probed okay and may be used */
    658     }
    659     else if (dev != probed_dev)
    660     {
    661       goto not_probed;
    662     }
    663 
    664     FLUSHK();
    665 
    666     /* Select what traffic to receive
    667      */
    668     if (promisc)
    669          dev->flags |=  (IFF_ALLMULTI | IFF_PROMISC);
    670     else dev->flags &= ~(IFF_ALLMULTI | IFF_PROMISC);
    671 
    672     PCAP_ASSERT (dev->open);
    673 
    674     if (!(*dev->open)(dev))
    675     {
    676       pcap_snprintf (ebuf, PCAP_ERRBUF_SIZE, "failed to activate device `%s'", dev_name);
    677       if (pktInfo.error && !strncmp(dev->name,"pkt",3))
    678       {
    679         strcat (ebuf, ": ");
    680         strcat (ebuf, pktInfo.error);
    681       }
    682       return (NULL);
    683     }
    684 
    685     /* Some devices need this to operate in promiscous mode
    686      */
    687     if (promisc && dev->set_multicast_list)
    688        (*dev->set_multicast_list) (dev);
    689 
    690     active_dev = dev;   /* remember our active device */
    691     break;
    692   }
    693 
    694   /* 'dev_name' not matched in 'dev_base' list.
    695    */
    696   if (!dev)
    697   {
    698     pcap_snprintf (ebuf, PCAP_ERRBUF_SIZE, "device `%s' not supported", dev_name);
    699     return (NULL);
    700   }
    701 
    702 not_probed:
    703   if (!probed_dev)
    704   {
    705     pcap_snprintf (ebuf, PCAP_ERRBUF_SIZE, "device `%s' not probed", dev_name);
    706     return (NULL);
    707   }
    708   return (dev);
    709 }
    710 
    711 /*
    712  * Deinitialise MAC driver.
    713  * Set receive mode back to default mode.
    714  */
    715 static void close_driver (void)
    716 {
    717   /* !!todo: loop over all 'handle_to_device[]' ? */
    718   struct device *dev = active_dev;
    719 
    720   if (dev && dev->close)
    721   {
    722     (*dev->close) (dev);
    723     FLUSHK();
    724   }
    725 
    726   active_dev = NULL;
    727 
    728 #ifdef USE_32BIT_DRIVERS
    729   if (rx_pool)
    730   {
    731     k_free (rx_pool);
    732     rx_pool = NULL;
    733   }
    734   if (dev)
    735      pcibios_exit();
    736 #endif
    737 }
    738 
    739 
    740 #ifdef __DJGPP__
    741 static void setup_signals (void (*handler)(int))
    742 {
    743   signal (SIGSEGV,handler);
    744   signal (SIGILL, handler);
    745   signal (SIGFPE, handler);
    746 }
    747 
    748 static void exc_handler (int sig)
    749 {
    750 #ifdef USE_32BIT_DRIVERS
    751   if (active_dev->irq > 0)    /* excludes IRQ 0 */
    752   {
    753     disable_irq (active_dev->irq);
    754     irq_eoi_cmd (active_dev->irq);
    755     _printk_safe = 1;
    756   }
    757 #endif
    758 
    759   switch (sig)
    760   {
    761     case SIGSEGV:
    762          fputs ("Catching SIGSEGV.\n", stderr);
    763          break;
    764     case SIGILL:
    765          fputs ("Catching SIGILL.\n", stderr);
    766          break;
    767     case SIGFPE:
    768          _fpreset();
    769          fputs ("Catching SIGFPE.\n", stderr);
    770          break;
    771     default:
    772          fprintf (stderr, "Catching signal %d.\n", sig);
    773   }
    774   exc_occured = 1;
    775   close_driver();
    776 }
    777 #endif  /* __DJGPP__ */
    778 
    779 
    780 /*
    781  * Open the pcap device for the first client calling pcap_activate()
    782  */
    783 static int first_init (const char *name, char *ebuf, int promisc)
    784 {
    785   struct device *dev;
    786 
    787 #ifdef USE_32BIT_DRIVERS
    788   rx_pool = k_calloc (RECEIVE_BUF_SIZE, RECEIVE_QUEUE_SIZE);
    789   if (!rx_pool)
    790   {
    791     strcpy (ebuf, "Not enough memory (Rx pool)");
    792     return (0);
    793   }
    794 #endif
    795 
    796 #ifdef __DJGPP__
    797   setup_signals (exc_handler);
    798 #endif
    799 
    800 #ifdef USE_32BIT_DRIVERS
    801   init_32bit();
    802 #endif
    803 
    804   dev = open_driver (name, ebuf, promisc);
    805   if (!dev)
    806   {
    807 #ifdef USE_32BIT_DRIVERS
    808     k_free (rx_pool);
    809     rx_pool = NULL;
    810 #endif
    811 
    812 #ifdef __DJGPP__
    813     setup_signals (SIG_DFL);
    814 #endif
    815     return (0);
    816   }
    817 
    818 #ifdef USE_32BIT_DRIVERS
    819   /*
    820    * If driver is NOT a 16-bit "pkt/ndis" driver (having a 'copy_rx_buf'
    821    * set in it's probe handler), initialise near-memory ring-buffer for
    822    * the 32-bit device.
    823    */
    824   if (dev->copy_rx_buf == NULL)
    825   {
    826     dev->get_rx_buf     = get_rxbuf;
    827     dev->peek_rx_buf    = peek_rxbuf;
    828     dev->release_rx_buf = release_rxbuf;
    829     pktq_init (&dev->queue, RECEIVE_BUF_SIZE, RECEIVE_QUEUE_SIZE, rx_pool);
    830   }
    831 #endif
    832   return (1);
    833 }
    834 
    835 #ifdef USE_32BIT_DRIVERS
    836 static void init_32bit (void)
    837 {
    838   static int init_pci = 0;
    839 
    840   if (!_printk_file)
    841      _printk_init (64*1024, NULL); /* calls atexit(printk_exit) */
    842 
    843   if (!init_pci)
    844      (void)pci_init();             /* init BIOS32+PCI interface */
    845   init_pci = 1;
    846 }
    847 #endif
    848 
    849 
    850 /*
    851  * Hook functions for using Watt-32 together with pcap
    852  */
    853 static char rxbuf [ETH_MAX+100]; /* rx-buffer with some margin */
    854 static WORD etype;
    855 static pcap_t pcap_save;
    856 
    857 static void watt32_recv_hook (u_char *dummy, const struct pcap_pkthdr *pcap,
    858                               const u_char *buf)
    859 {
    860   /* Fix me: assumes Ethernet II only */
    861   struct ether_header *ep = (struct ether_header*) buf;
    862 
    863   memcpy (rxbuf, buf, pcap->caplen);
    864   etype = ep->ether_type;
    865   ARGSUSED (dummy);
    866 }
    867 
    868 #if (WATTCP_VER >= 0x0224)
    869 /*
    870  * This function is used by Watt-32 to poll for a packet.
    871  * i.e. it's set to bypass _eth_arrived()
    872  */
    873 static void *pcap_recv_hook (WORD *type)
    874 {
    875   int len = pcap_read_dos (&pcap_save, 1, watt32_recv_hook, NULL);
    876 
    877   if (len < 0)
    878      return (NULL);
    879 
    880   *type = etype;
    881   return (void*) &rxbuf;
    882 }
    883 
    884 /*
    885  * This function is called by Watt-32 (via _eth_xmit_hook).
    886  * If dbug_init() was called, we should trace packets sent.
    887  */
    888 static int pcap_xmit_hook (const void *buf, unsigned len)
    889 {
    890   int rc = 0;
    891 
    892   if (pcap_pkt_debug > 0)
    893      dbug_write ("pcap_xmit_hook: ");
    894 
    895   if (active_dev && active_dev->xmit)
    896      if ((*active_dev->xmit) (active_dev, buf, len) > 0)
    897         rc = len;
    898 
    899   if (pcap_pkt_debug > 0)
    900      dbug_write (rc ? "ok\n" : "fail\n");
    901   return (rc);
    902 }
    903 #endif
    904 
    905 static int pcap_sendpacket_dos (pcap_t *p, const void *buf, size_t len)
    906 {
    907   struct device *dev = p ? get_device(p->fd) : NULL;
    908 
    909   if (!dev || !dev->xmit)
    910      return (-1);
    911   return (*dev->xmit) (dev, buf, len);
    912 }
    913 
    914 /*
    915  * This function is called by Watt-32 in tcp_post_init().
    916  * We should prevent Watt-32 from using BOOTP/DHCP/RARP etc.
    917  */
    918 static void (*prev_post_hook) (void);
    919 
    920 static void pcap_init_hook (void)
    921 {
    922   _w32__bootp_on = _w32__dhcp_on = _w32__rarp_on = 0;
    923   _w32__do_mask_req = 0;
    924   _w32_dynamic_host = 0;
    925   if (prev_post_hook)
    926     (*prev_post_hook)();
    927 }
    928 
    929 /*
    930  * Supress PRINT message from Watt-32's sock_init()
    931  */
    932 static void null_print (void) {}
    933 
    934 /*
    935  * To use features of Watt-32 (netdb functions and socket etc.)
    936  * we must call sock_init(). But we set various hooks to prevent
    937  * using normal PKTDRVR functions in pcpkt.c. This should hopefully
    938  * make Watt-32 and pcap co-operate.
    939  */
    940 static int init_watt32 (struct pcap *pcap, const char *dev_name, char *err_buf)
    941 {
    942   char *env;
    943   int   rc, MTU, has_ip_addr;
    944   int   using_pktdrv = 1;
    945 
    946   /* If user called sock_init() first, we need to reinit in
    947    * order to open debug/trace-file properly
    948    */
    949   if (_watt_is_init)
    950      sock_exit();
    951 
    952   env = getenv ("PCAP_TRACE");
    953   if (env && atoi(env) > 0 &&
    954       pcap_pkt_debug < 0)   /* if not already set */
    955   {
    956     dbug_init();
    957     pcap_pkt_debug = atoi (env);
    958   }
    959 
    960   _watt_do_exit      = 0;    /* prevent sock_init() calling exit() */
    961   prev_post_hook     = _w32_usr_post_init;
    962   _w32_usr_post_init = pcap_init_hook;
    963   _w32_print_hook    = null_print;
    964 
    965   if (dev_name && strncmp(dev_name,"pkt",3))
    966      using_pktdrv = FALSE;
    967 
    968   rc = sock_init();
    969   has_ip_addr = (rc != 8);  /* IP-address assignment failed */
    970 
    971   /* if pcap is using a 32-bit driver w/o a pktdrvr loaded, we
    972    * just pretend Watt-32 is initialised okay.
    973    *
    974    * !! fix-me: The Watt-32 config isn't done if no pktdrvr
    975    *            was found. In that case my_ip_addr + sin_mask
    976    *            have default values. Should be taken from another
    977    *            ini-file/environment in any case (ref. tcpdump.ini)
    978    */
    979   _watt_is_init = 1;
    980 
    981   if (!using_pktdrv || !has_ip_addr)  /* for now .... */
    982   {
    983     static const char myip[] = "192.168.0.1";
    984     static const char mask[] = "255.255.255.0";
    985 
    986     printf ("Just guessing, using IP %s and netmask %s\n", myip, mask);
    987     my_ip_addr    = aton (myip);
    988     _w32_sin_mask = aton (mask);
    989   }
    990   else if (rc && using_pktdrv)
    991   {
    992     pcap_snprintf (err_buf, PCAP_ERRBUF_SIZE, "sock_init() failed, code %d", rc);
    993     return (0);
    994   }
    995 
    996   /* Set recv-hook for peeking in _eth_arrived().
    997    */
    998 #if (WATTCP_VER >= 0x0224)
    999   _eth_recv_hook = pcap_recv_hook;
   1000   _eth_xmit_hook = pcap_xmit_hook;
   1001 #endif
   1002 
   1003   /* Free the pkt-drvr handle allocated in pkt_init().
   1004    * The above hooks should thus use the handle reopened in open_driver()
   1005    */
   1006   if (using_pktdrv)
   1007   {
   1008     _eth_release();
   1009 /*  _eth_is_init = 1; */  /* hack to get Rx/Tx-hooks in Watt-32 working */
   1010   }
   1011 
   1012   memcpy (&pcap_save, pcap, sizeof(pcap_save));
   1013   MTU = pkt_get_mtu();
   1014   pcap_save.fcode.bf_insns = NULL;
   1015   pcap_save.linktype       = _eth_get_hwtype (NULL, NULL);
   1016   pcap_save.snapshot       = MTU > 0 ? MTU : ETH_MAX; /* assume 1514 */
   1017 
   1018 #if 1
   1019   /* prevent use of resolve() and resolve_ip()
   1020    */
   1021   last_nameserver = 0;
   1022 #endif
   1023   return (1);
   1024 }
   1025 
   1026 int EISA_bus = 0;  /* Where is natural place for this? */
   1027 
   1028 /*
   1029  * Application config hooks to set various driver parameters.
   1030  */
   1031 
   1032 static const struct config_table debug_tab[] = {
   1033             { "PKT.DEBUG",       ARG_ATOI,   &pcap_pkt_debug    },
   1034             { "PKT.VECTOR",      ARG_ATOX_W, NULL               },
   1035             { "NDIS.DEBUG",      ARG_ATOI,   NULL               },
   1036 #ifdef USE_32BIT_DRIVERS
   1037             { "3C503.DEBUG",     ARG_ATOI,   &ei_debug          },
   1038             { "3C503.IO_BASE",   ARG_ATOX_W, &el2_dev.base_addr },
   1039             { "3C503.MEMORY",    ARG_ATOX_W, &el2_dev.mem_start },
   1040             { "3C503.IRQ",       ARG_ATOI,   &el2_dev.irq       },
   1041             { "3C505.DEBUG",     ARG_ATOI,   NULL               },
   1042             { "3C505.BASE",      ARG_ATOX_W, NULL               },
   1043             { "3C507.DEBUG",     ARG_ATOI,   NULL               },
   1044             { "3C509.DEBUG",     ARG_ATOI,   &el3_debug         },
   1045             { "3C509.ILOOP",     ARG_ATOI,   &el3_max_loop      },
   1046             { "3C529.DEBUG",     ARG_ATOI,   NULL               },
   1047             { "3C575.DEBUG",     ARG_ATOI,   &debug_3c575       },
   1048             { "3C59X.DEBUG",     ARG_ATOI,   &vortex_debug      },
   1049             { "3C59X.IFACE0",    ARG_ATOI,   &vortex_options[0] },
   1050             { "3C59X.IFACE1",    ARG_ATOI,   &vortex_options[1] },
   1051             { "3C59X.IFACE2",    ARG_ATOI,   &vortex_options[2] },
   1052             { "3C59X.IFACE3",    ARG_ATOI,   &vortex_options[3] },
   1053             { "3C90X.DEBUG",     ARG_ATOX_W, &tc90xbc_debug     },
   1054             { "ACCT.DEBUG",      ARG_ATOI,   &ethpk_debug       },
   1055             { "CS89.DEBUG",      ARG_ATOI,   &cs89_debug        },
   1056             { "RTL8139.DEBUG",   ARG_ATOI,   &rtl8139_debug     },
   1057         /*  { "RTL8139.FDUPLEX", ARG_ATOI,   &rtl8139_options   }, */
   1058             { "SMC.DEBUG",       ARG_ATOI,   &ei_debug          },
   1059         /*  { "E100.DEBUG",      ARG_ATOI,   &e100_debug        }, */
   1060             { "PCI.DEBUG",       ARG_ATOI,   &pci_debug         },
   1061             { "BIOS32.DEBUG",    ARG_ATOI,   &bios32_debug      },
   1062             { "IRQ.DEBUG",       ARG_ATOI,   &irq_debug         },
   1063             { "TIMER.IRQ",       ARG_ATOI,   &timer_irq         },
   1064 #endif
   1065             { NULL }
   1066           };
   1067 
   1068 /*
   1069  * pcap_config_hook() is an extension to application's config
   1070  * handling. Uses Watt-32's config-table function.
   1071  */
   1072 int pcap_config_hook (const char *keyword, const char *value)
   1073 {
   1074   return parse_config_table (debug_tab, NULL, keyword, value);
   1075 }
   1076 
   1077 /*
   1078  * Linked list of supported devices
   1079  */
   1080 struct device       *active_dev = NULL;      /* the device we have opened */
   1081 struct device       *probed_dev = NULL;      /* the device we have probed */
   1082 const struct device *dev_base   = &pkt_dev;  /* list of network devices */
   1083 
   1084 /*
   1085  * PKTDRVR device functions
   1086  */
   1087 int pcap_pkt_debug = -1;
   1088 
   1089 static void pkt_close (struct device *dev)
   1090 {
   1091   BOOL okay = PktExitDriver();
   1092 
   1093   if (pcap_pkt_debug > 1)
   1094      fprintf (stderr, "pkt_close(): %d\n", okay);
   1095 
   1096   if (dev->priv)
   1097      free (dev->priv);
   1098   dev->priv = NULL;
   1099 }
   1100 
   1101 static int pkt_open (struct device *dev)
   1102 {
   1103   PKT_RX_MODE mode;
   1104 
   1105   if (dev->flags & IFF_PROMISC)
   1106        mode = PDRX_ALL_PACKETS;
   1107   else mode = PDRX_BROADCAST;
   1108 
   1109   if (!PktInitDriver(mode))
   1110      return (0);
   1111 
   1112   PktResetStatistics (pktInfo.handle);
   1113   PktQueueBusy (FALSE);
   1114   return (1);
   1115 }
   1116 
   1117 static int pkt_xmit (struct device *dev, const void *buf, int len)
   1118 {
   1119   struct net_device_stats *stats = (struct net_device_stats*) dev->priv;
   1120 
   1121   if (pcap_pkt_debug > 0)
   1122      dbug_write ("pcap_xmit\n");
   1123 
   1124   if (!PktTransmit(buf,len))
   1125   {
   1126     stats->tx_errors++;
   1127     return (0);
   1128   }
   1129   return (len);
   1130 }
   1131 
   1132 static void *pkt_stats (struct device *dev)
   1133 {
   1134   struct net_device_stats *stats = (struct net_device_stats*) dev->priv;
   1135 
   1136   if (!stats || !PktSessStatistics(pktInfo.handle))
   1137      return (NULL);
   1138 
   1139   stats->rx_packets       = pktStat.inPackets;
   1140   stats->rx_errors        = pktStat.lost;
   1141   stats->rx_missed_errors = PktRxDropped();
   1142   return (stats);
   1143 }
   1144 
   1145 static int pkt_probe (struct device *dev)
   1146 {
   1147   if (!PktSearchDriver())
   1148      return (0);
   1149 
   1150   dev->open           = pkt_open;
   1151   dev->xmit           = pkt_xmit;
   1152   dev->close          = pkt_close;
   1153   dev->get_stats      = pkt_stats;
   1154   dev->copy_rx_buf    = PktReceive;  /* farmem peek and copy routine */
   1155   dev->get_rx_buf     = NULL;
   1156   dev->peek_rx_buf    = NULL;
   1157   dev->release_rx_buf = NULL;
   1158   dev->priv           = calloc (sizeof(struct net_device_stats), 1);
   1159   if (!dev->priv)
   1160      return (0);
   1161   return (1);
   1162 }
   1163 
   1164 /*
   1165  * NDIS device functions
   1166  */
   1167 static void ndis_close (struct device *dev)
   1168 {
   1169 #ifdef USE_NDIS2
   1170   NdisShutdown();
   1171 #endif
   1172   ARGSUSED (dev);
   1173 }
   1174 
   1175 static int ndis_open (struct device *dev)
   1176 {
   1177   int promis = (dev->flags & IFF_PROMISC);
   1178 
   1179 #ifdef USE_NDIS2
   1180   if (!NdisInit(promis))
   1181      return (0);
   1182   return (1);
   1183 #else
   1184   ARGSUSED (promis);
   1185   return (0);
   1186 #endif
   1187 }
   1188 
   1189 static void *ndis_stats (struct device *dev)
   1190 {
   1191   static struct net_device_stats stats;
   1192 
   1193   /* to-do */
   1194   ARGSUSED (dev);
   1195   return (&stats);
   1196 }
   1197 
   1198 static int ndis_probe (struct device *dev)
   1199 {
   1200 #ifdef USE_NDIS2
   1201   if (!NdisOpen())
   1202      return (0);
   1203 #endif
   1204 
   1205   dev->open           = ndis_open;
   1206   dev->xmit           = NULL;
   1207   dev->close          = ndis_close;
   1208   dev->get_stats      = ndis_stats;
   1209   dev->copy_rx_buf    = NULL;       /* to-do */
   1210   dev->get_rx_buf     = NULL;       /* upcall is from rmode driver */
   1211   dev->peek_rx_buf    = NULL;
   1212   dev->release_rx_buf = NULL;
   1213   return (0);
   1214 }
   1215 
   1216 /*
   1217  * Search & probe for supported 32-bit (pmode) pcap devices
   1218  */
   1219 #if defined(USE_32BIT_DRIVERS)
   1220 
   1221 struct device el2_dev LOCKED_VAR = {
   1222               "3c503",
   1223               "EtherLink II",
   1224               0,
   1225               0,0,0,0,0,0,
   1226               NULL,
   1227               el2_probe
   1228             };
   1229 
   1230 struct device el3_dev LOCKED_VAR = {
   1231               "3c509",
   1232               "EtherLink III",
   1233               0,
   1234               0,0,0,0,0,0,
   1235               &el2_dev,
   1236               el3_probe
   1237             };
   1238 
   1239 struct device tc515_dev LOCKED_VAR = {
   1240               "3c515",
   1241               "EtherLink PCI",
   1242               0,
   1243               0,0,0,0,0,0,
   1244               &el3_dev,
   1245               tc515_probe
   1246             };
   1247 
   1248 struct device tc59_dev LOCKED_VAR = {
   1249               "3c59x",
   1250               "EtherLink PCI",
   1251               0,
   1252               0,0,0,0,0,0,
   1253               &tc515_dev,
   1254               tc59x_probe
   1255             };
   1256 
   1257 struct device tc90xbc_dev LOCKED_VAR = {
   1258               "3c90x",
   1259               "EtherLink 90X",
   1260               0,
   1261               0,0,0,0,0,0,
   1262               &tc59_dev,
   1263               tc90xbc_probe
   1264             };
   1265 
   1266 struct device wd_dev LOCKED_VAR = {
   1267               "wd",
   1268               "Westen Digital",
   1269               0,
   1270               0,0,0,0,0,0,
   1271               &tc90xbc_dev,
   1272               wd_probe
   1273             };
   1274 
   1275 struct device ne_dev LOCKED_VAR = {
   1276               "ne",
   1277               "NEx000",
   1278               0,
   1279               0,0,0,0,0,0,
   1280               &wd_dev,
   1281               ne_probe
   1282             };
   1283 
   1284 struct device acct_dev LOCKED_VAR = {
   1285               "acct",
   1286               "Accton EtherPocket",
   1287               0,
   1288               0,0,0,0,0,0,
   1289               &ne_dev,
   1290               ethpk_probe
   1291             };
   1292 
   1293 struct device cs89_dev LOCKED_VAR = {
   1294               "cs89",
   1295               "Crystal Semiconductor",
   1296               0,
   1297               0,0,0,0,0,0,
   1298               &acct_dev,
   1299               cs89x0_probe
   1300             };
   1301 
   1302 struct device rtl8139_dev LOCKED_VAR = {
   1303               "rtl8139",
   1304               "RealTek PCI",
   1305               0,
   1306               0,0,0,0,0,0,
   1307               &cs89_dev,
   1308               rtl8139_probe     /* dev->probe routine */
   1309             };
   1310 
   1311 /*
   1312  * Dequeue routine is called by polling.
   1313  * NOTE: the queue-element is not copied, only a pointer is
   1314  * returned at '*buf'
   1315  */
   1316 int peek_rxbuf (BYTE **buf)
   1317 {
   1318   struct rx_elem *tail, *head;
   1319 
   1320   PCAP_ASSERT (pktq_check (&active_dev->queue));
   1321 
   1322   DISABLE();
   1323   tail = pktq_out_elem (&active_dev->queue);
   1324   head = pktq_in_elem (&active_dev->queue);
   1325   ENABLE();
   1326 
   1327   if (head != tail)
   1328   {
   1329     PCAP_ASSERT (tail->size < active_dev->queue.elem_size-4-2);
   1330 
   1331     *buf = &tail->data[0];
   1332     return (tail->size);
   1333   }
   1334   *buf = NULL;
   1335   return (0);
   1336 }
   1337 
   1338 /*
   1339  * Release buffer we peeked at above.
   1340  */
   1341 int release_rxbuf (BYTE *buf)
   1342 {
   1343 #ifndef NDEBUG
   1344   struct rx_elem *tail = pktq_out_elem (&active_dev->queue);
   1345 
   1346   PCAP_ASSERT (&tail->data[0] == buf);
   1347 #else
   1348   ARGSUSED (buf);
   1349 #endif
   1350   pktq_inc_out (&active_dev->queue);
   1351   return (1);
   1352 }
   1353 
   1354 /*
   1355  * get_rxbuf() routine (in locked code) is called from IRQ handler
   1356  * to request a buffer. Interrupts are disabled and we have a 32kB stack.
   1357  */
   1358 BYTE *get_rxbuf (int len)
   1359 {
   1360   int idx;
   1361 
   1362   if (len < ETH_MIN || len > ETH_MAX)
   1363      return (NULL);
   1364 
   1365   idx = pktq_in_index (&active_dev->queue);
   1366 
   1367 #ifdef DEBUG
   1368   {
   1369     static int fan_idx LOCKED_VAR = 0;
   1370     writew ("-\\|/"[fan_idx++] | (15 << 8),      /* white on black colour */
   1371             0xB8000 + 2*79);  /* upper-right corner, 80-col colour screen */
   1372     fan_idx &= 3;
   1373   }
   1374 /* writew (idx + '0' + 0x0F00, 0xB8000 + 2*78); */
   1375 #endif
   1376 
   1377   if (idx != active_dev->queue.out_index)
   1378   {
   1379     struct rx_elem *head = pktq_in_elem (&active_dev->queue);
   1380 
   1381     head->size = len;
   1382     active_dev->queue.in_index = idx;
   1383     return (&head->data[0]);
   1384   }
   1385 
   1386   /* !!to-do: drop 25% of the oldest element
   1387    */
   1388   pktq_clear (&active_dev->queue);
   1389   return (NULL);
   1390 }
   1391 
   1392 /*
   1393  *  Simple ring-buffer queue handler for reception of packets
   1394  *  from network driver.
   1395  */
   1396 #define PKTQ_MARKER  0xDEADBEEF
   1397 
   1398 static int pktq_check (struct rx_ringbuf *q)
   1399 {
   1400 #ifndef NDEBUG
   1401   int   i;
   1402   char *buf;
   1403 #endif
   1404 
   1405   if (!q || !q->num_elem || !q->buf_start)
   1406      return (0);
   1407 
   1408 #ifndef NDEBUG
   1409   buf = q->buf_start;
   1410 
   1411   for (i = 0; i < q->num_elem; i++)
   1412   {
   1413     buf += q->elem_size;
   1414     if (*(DWORD*)(buf - sizeof(DWORD)) != PKTQ_MARKER)
   1415        return (0);
   1416   }
   1417 #endif
   1418   return (1);
   1419 }
   1420 
   1421 static int pktq_init (struct rx_ringbuf *q, int size, int num, char *pool)
   1422 {
   1423   int i;
   1424 
   1425   q->elem_size = size;
   1426   q->num_elem  = num;
   1427   q->buf_start = pool;
   1428   q->in_index  = 0;
   1429   q->out_index = 0;
   1430 
   1431   PCAP_ASSERT (size >= sizeof(struct rx_elem) + sizeof(DWORD));
   1432   PCAP_ASSERT (num);
   1433   PCAP_ASSERT (pool);
   1434 
   1435   for (i = 0; i < num; i++)
   1436   {
   1437 #if 0
   1438     struct rx_elem *elem = (struct rx_elem*) pool;
   1439 
   1440     /* assert dword aligned elements
   1441      */
   1442     PCAP_ASSERT (((unsigned)(&elem->data[0]) & 3) == 0);
   1443 #endif
   1444     pool += size;
   1445     *(DWORD*) (pool - sizeof(DWORD)) = PKTQ_MARKER;
   1446   }
   1447   return (1);
   1448 }
   1449 
   1450 /*
   1451  * Increment the queue 'out_index' (tail).
   1452  * Check for wraps.
   1453  */
   1454 static int pktq_inc_out (struct rx_ringbuf *q)
   1455 {
   1456   q->out_index++;
   1457   if (q->out_index >= q->num_elem)
   1458       q->out_index = 0;
   1459   return (q->out_index);
   1460 }
   1461 
   1462 /*
   1463  * Return the queue's next 'in_index' (head).
   1464  * Check for wraps.
   1465  */
   1466 static int pktq_in_index (struct rx_ringbuf *q)
   1467 {
   1468   volatile int index = q->in_index + 1;
   1469 
   1470   if (index >= q->num_elem)
   1471       index = 0;
   1472   return (index);
   1473 }
   1474 
   1475 /*
   1476  * Return the queue's head-buffer.
   1477  */
   1478 static struct rx_elem *pktq_in_elem (struct rx_ringbuf *q)
   1479 {
   1480   return (struct rx_elem*) (q->buf_start + (q->elem_size * q->in_index));
   1481 }
   1482 
   1483 /*
   1484  * Return the queue's tail-buffer.
   1485  */
   1486 static struct rx_elem *pktq_out_elem (struct rx_ringbuf *q)
   1487 {
   1488   return (struct rx_elem*) (q->buf_start + (q->elem_size * q->out_index));
   1489 }
   1490 
   1491 /*
   1492  * Clear the queue ring-buffer by setting head=tail.
   1493  */
   1494 static void pktq_clear (struct rx_ringbuf *q)
   1495 {
   1496   q->in_index = q->out_index;
   1497 }
   1498 
   1499 /*
   1500  * Symbols that must be linkable for "gcc -O0"
   1501  */
   1502 #undef __IOPORT_H
   1503 #undef __DMA_H
   1504 
   1505 #define extern
   1506 #define __inline__
   1507 
   1508 #include "msdos/pm_drvr/ioport.h"
   1509 #include "msdos/pm_drvr/dma.h"
   1510 
   1511 #endif /* USE_32BIT_DRIVERS */
   1512 
   1513