2 * This file is part of DOS-libpcap
3 * Ported to DOS/DOSX by G. Vanem <gvanem@yahoo.no>
5 * pcap-dos.c: Interface to PKTDRVR, NDIS2 and 32-bit pmode
15 #include <limits.h> /* for INT_MAX */
18 #if defined(USE_32BIT_DRIVERS)
19 #include "msdos/pm_drvr/pmdrvr.h"
20 #include "msdos/pm_drvr/pci.h"
21 #include "msdos/pm_drvr/bios32.h"
22 #include "msdos/pm_drvr/module.h"
23 #include "msdos/pm_drvr/3c501.h"
24 #include "msdos/pm_drvr/3c503.h"
25 #include "msdos/pm_drvr/3c509.h"
26 #include "msdos/pm_drvr/3c59x.h"
27 #include "msdos/pm_drvr/3c515.h"
28 #include "msdos/pm_drvr/3c90x.h"
29 #include "msdos/pm_drvr/3c575_cb.h"
30 #include "msdos/pm_drvr/ne.h"
31 #include "msdos/pm_drvr/wd.h"
32 #include "msdos/pm_drvr/accton.h"
33 #include "msdos/pm_drvr/cs89x0.h"
34 #include "msdos/pm_drvr/rtl8139.h"
35 #include "msdos/pm_drvr/ne2k-pci.h"
41 #include "msdos/pktdrvr.h"
44 #include "msdos/ndis2.h"
47 #include <arpa/inet.h>
49 #include <net/if_arp.h>
50 #include <net/if_ether.h>
51 #include <net/if_packe.h>
54 #if defined(USE_32BIT_DRIVERS)
55 #define FLUSHK() do { _printk_safe = 1; _printk_flush(); } while (0)
56 #define NDIS_NEXT_DEV &rtl8139_dev
58 static char *rx_pool
= NULL
;
59 static void init_32bit (void);
61 static int pktq_init (struct rx_ringbuf
*q
, int size
, int num
, char *pool
);
62 static int pktq_check (struct rx_ringbuf
*q
);
63 static int pktq_inc_out (struct rx_ringbuf
*q
);
64 static int pktq_in_index (struct rx_ringbuf
*q
) LOCKED_FUNC
;
65 static void pktq_clear (struct rx_ringbuf
*q
) LOCKED_FUNC
;
67 static struct rx_elem
*pktq_in_elem (struct rx_ringbuf
*q
) LOCKED_FUNC
;
68 static struct rx_elem
*pktq_out_elem (struct rx_ringbuf
*q
);
71 #define FLUSHK() ((void)0)
72 #define NDIS_NEXT_DEV NULL
76 * Internal variables/functions in Watt-32
78 extern WORD _pktdevclass
;
79 extern BOOL _eth_is_init
;
80 extern int _w32_dynamic_host
;
81 extern int _watt_do_exit
;
82 extern int _watt_is_init
;
83 extern int _w32__bootp_on
, _w32__dhcp_on
, _w32__rarp_on
, _w32__do_mask_req
;
84 extern void (*_w32_usr_post_init
) (void);
85 extern void (*_w32_print_hook
)();
87 extern void dbug_write (const char *); /* Watt-32 lib, pcdbug.c */
88 extern int pkt_get_mtu (void);
90 static int ref_count
= 0;
92 static u_long mac_count
= 0;
93 static u_long filter_count
= 0;
95 static volatile BOOL exc_occured
= 0;
97 static struct device
*handle_to_device
[20];
99 static int pcap_activate_dos (pcap_t
*p
);
100 static int pcap_read_dos (pcap_t
*p
, int cnt
, pcap_handler callback
,
102 static void pcap_cleanup_dos (pcap_t
*p
);
103 static int pcap_stats_dos (pcap_t
*p
, struct pcap_stat
*ps
);
104 static int pcap_sendpacket_dos (pcap_t
*p
, const void *buf
, size_t len
);
105 static int pcap_setfilter_dos (pcap_t
*p
, struct bpf_program
*fp
);
107 static int ndis_probe (struct device
*dev
);
108 static int pkt_probe (struct device
*dev
);
110 static void close_driver (void);
111 static int init_watt32 (struct pcap
*pcap
, const char *dev_name
, char *err_buf
);
112 static int first_init (const char *name
, char *ebuf
, int promisc
);
114 static void watt32_recv_hook (u_char
*dummy
, const struct pcap_pkthdr
*pcap
,
118 * These are the device we always support
120 static struct device ndis_dev
= {
125 NDIS_NEXT_DEV
, /* NULL or a 32-bit device */
129 static struct device pkt_dev
= {
138 static struct device
*get_device (int fd
)
140 if (fd
<= 0 || fd
>= sizeof(handle_to_device
)/sizeof(handle_to_device
[0]))
142 return handle_to_device
[fd
-1];
146 * Private data for capturing on MS-DOS.
149 void (*wait_proc
)(void); /* call proc while waiting */
150 struct pcap_stat stat
;
153 pcap_t
*pcap_create_interface (const char *device _U_
, char *ebuf
)
157 p
= PCAP_CREATE_COMMON(ebuf
, struct pcap_dos
);
161 p
->activate_op
= pcap_activate_dos
;
166 * Open MAC-driver with name 'device_name' for live capture of
169 static int pcap_activate_dos (pcap_t
*pcap
)
171 if (pcap
->opt
.rfmon
) {
173 * No monitor mode on DOS.
175 return (PCAP_ERROR_RFMON_NOTSUP
);
179 * Turn a negative snapshot value (invalid), a snapshot value of
180 * 0 (unspecified), or a value bigger than the normal maximum
181 * value, into the maximum allowed value.
183 * If some application really *needs* a bigger snapshot
184 * length, we should just increase MAXIMUM_SNAPLEN.
186 if (pcap
->snapshot
<= 0 || pcap
->snapshot
> MAXIMUM_SNAPLEN
)
187 pcap
->snapshot
= MAXIMUM_SNAPLEN
;
189 if (pcap
->snapshot
< ETH_MIN
+8)
190 pcap
->snapshot
= ETH_MIN
+8;
192 if (pcap
->snapshot
> ETH_MAX
) /* silently accept and truncate large MTUs */
193 pcap
->snapshot
= ETH_MAX
;
195 pcap
->linktype
= DLT_EN10MB
; /* !! */
196 pcap
->cleanup_op
= pcap_cleanup_dos
;
197 pcap
->read_op
= pcap_read_dos
;
198 pcap
->stats_op
= pcap_stats_dos
;
199 pcap
->inject_op
= pcap_sendpacket_dos
;
200 pcap
->setfilter_op
= pcap_setfilter_dos
;
201 pcap
->setdirection_op
= NULL
; /* Not implemented.*/
202 pcap
->fd
= ++ref_count
;
204 pcap
->bufsize
= ETH_MAX
+100; /* add some margin */
205 pcap
->buffer
= calloc (pcap
->bufsize
, 1);
207 if (pcap
->fd
== 1) /* first time we're called */
209 if (!init_watt32(pcap
, pcap
->opt
.device
, pcap
->errbuf
) ||
210 !first_init(pcap
->opt
.device
, pcap
->errbuf
, pcap
->opt
.promisc
))
212 /* XXX - free pcap->buffer? */
215 atexit (close_driver
);
217 else if (stricmp(active_dev
->name
,pcap
->opt
.device
))
219 snprintf (pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
220 "Cannot use different devices simultaneously "
221 "(`%s' vs. `%s')", active_dev
->name
, pcap
->opt
.device
);
222 /* XXX - free pcap->buffer? */
225 handle_to_device
[pcap
->fd
-1] = active_dev
;
230 * Poll the receiver queue and call the pcap callback-handler
234 pcap_read_one (pcap_t
*p
, pcap_handler callback
, u_char
*data
)
236 struct pcap_dos
*pd
= p
->priv
;
237 struct pcap_pkthdr pcap
;
238 struct timeval now
, expiry
= { 0,0 };
241 if (p
->opt
.timeout
> 0)
243 gettimeofday2 (&now
, NULL
);
244 expiry
.tv_usec
= now
.tv_usec
+ 1000UL * p
->opt
.timeout
;
245 expiry
.tv_sec
= now
.tv_sec
;
246 while (expiry
.tv_usec
>= 1000000L)
248 expiry
.tv_usec
-= 1000000L;
255 volatile struct device
*dev
; /* might be reset by sig_handler */
257 dev
= get_device (p
->fd
);
261 PCAP_ASSERT (dev
->copy_rx_buf
|| dev
->peek_rx_buf
);
264 /* If driver has a zero-copy receive facility, peek at the queue,
265 * filter it, do the callback and release the buffer.
267 if (dev
->peek_rx_buf
)
269 PCAP_ASSERT (dev
->release_rx_buf
);
270 rx_len
= (*dev
->peek_rx_buf
) (&p
->buffer
);
274 rx_len
= (*dev
->copy_rx_buf
) (p
->buffer
, p
->snapshot
);
277 if (rx_len
> 0) /* got a packet */
283 pcap
.caplen
= min (rx_len
, p
->snapshot
);
287 (!p
->fcode
.bf_insns
|| pcap_filter(p
->fcode
.bf_insns
, p
->buffer
, pcap
.len
, pcap
.caplen
)))
291 /* Fix-me!! Should be time of arrival. Not time of
294 gettimeofday2 (&pcap
.ts
, NULL
);
295 (*callback
) (data
, &pcap
, p
->buffer
);
298 if (dev
->release_rx_buf
)
299 (*dev
->release_rx_buf
) (p
->buffer
);
301 if (pcap_pkt_debug
> 0)
303 if (callback
== watt32_recv_hook
)
304 dbug_write ("pcap_recv_hook\n");
305 else dbug_write ("pcap_read_op\n");
311 /* Has "pcap_breakloop()" been called?
315 * Yes - clear the flag that indicates that it
316 * has, and return -2 to indicate that we were
317 * told to break out of the loop.
323 /* If not to wait for a packet or pcap_cleanup_dos() called from
324 * e.g. SIGINT handler, exit loop now.
326 if (p
->opt
.timeout
<= 0 || (volatile int)p
->fd
<= 0)
329 gettimeofday2 (&now
, NULL
);
331 if (timercmp(&now
, &expiry
, >))
335 kbhit(); /* a real CPU hog */
339 (*pd
->wait_proc
)(); /* call yield func */
342 if (rx_len
< 0) /* receive error */
345 #ifdef USE_32BIT_DRIVERS
346 if (pcap_pkt_debug
> 1)
347 printk ("pkt-err %s\n", pktInfo
.error
);
355 pcap_read_dos (pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*data
)
360 * This can conceivably process more than INT_MAX packets,
361 * which would overflow the packet count, causing it either
362 * to look like a negative number, and thus cause us to
363 * return a value that looks like an error, or overflow
364 * back into positive territory, and thus cause us to
365 * return a too-low count.
367 * Therefore, if the packet count is unlimited, we clip
368 * it at INT_MAX; this routine is not expected to
369 * process packets indefinitely, so that's not an issue.
371 if (PACKET_COUNT_IS_UNLIMITED(cnt
))
378 rc
= pcap_read_one (p
, callback
, data
);
383 _w32_os_yield(); /* allow SIGINT generation, yield to Win95/NT */
389 * Return network statistics
391 static int pcap_stats_dos (pcap_t
*p
, struct pcap_stat
*ps
)
393 struct net_device_stats
*stats
;
395 struct device
*dev
= p
? get_device(p
->fd
) : NULL
;
399 strcpy (p
->errbuf
, "illegal pcap handle");
403 if (!dev
->get_stats
|| (stats
= (*dev
->get_stats
)(dev
)) == NULL
)
405 strcpy (p
->errbuf
, "device statistics not available");
412 pd
->stat
.ps_recv
= stats
->rx_packets
;
413 pd
->stat
.ps_drop
+= stats
->rx_missed_errors
;
414 pd
->stat
.ps_ifdrop
= stats
->rx_dropped
+ /* queue full */
415 stats
->rx_errors
; /* HW errors */
423 * Return detailed network/device statistics.
424 * May be called after 'dev->close' is called.
426 int pcap_stats_ex (pcap_t
*p
, struct pcap_stat_ex
*se
)
428 struct device
*dev
= p
? get_device (p
->fd
) : NULL
;
430 if (!dev
|| !dev
->get_stats
)
432 pcap_strlcpy (p
->errbuf
, "detailed device statistics not available",
437 if (!strnicmp(dev
->name
,"pkt",3))
439 pcap_strlcpy (p
->errbuf
, "pktdrvr doesn't have detailed statistics",
443 memcpy (se
, (*dev
->get_stats
)(dev
), sizeof(*se
));
448 * Simply store the filter-code for the pcap_read_dos() callback
449 * Some day the filter-code could be handed down to the active
450 * device (pkt_rx1.s or 32-bit device interrupt handler).
452 static int pcap_setfilter_dos (pcap_t
*p
, struct bpf_program
*fp
)
461 * Return # of packets received in pcap_read_dos()
463 u_long
pcap_mac_packets (void)
469 * Return # of packets passed through filter in pcap_read_dos()
471 u_long
pcap_filter_packets (void)
473 return (filter_count
);
477 * Close pcap device. Not called for offline captures.
479 static void pcap_cleanup_dos (pcap_t
*p
)
486 if (pcap_stats(p
,NULL
) < 0)
487 pd
->stat
.ps_drop
= 0;
488 if (!get_device(p
->fd
))
491 handle_to_device
[p
->fd
-1] = NULL
;
499 /* XXX - call pcap_cleanup_live_common? */
503 * Return the name of the 1st network interface,
504 * or NULL if none can be found.
506 char *pcap_lookupdev (char *ebuf
)
510 #ifdef USE_32BIT_DRIVERS
514 for (dev
= (struct device
*)dev_base
; dev
; dev
= dev
->next
)
516 PCAP_ASSERT (dev
->probe
);
518 if ((*dev
->probe
)(dev
))
521 probed_dev
= (struct device
*) dev
; /* remember last probed device */
522 return (char*) dev
->name
;
527 strcpy (ebuf
, "No driver found");
532 * Gets localnet & netmask from Watt-32.
534 int pcap_lookupnet (const char *device
, bpf_u_int32
*localnet
,
535 bpf_u_int32
*netmask
, char *errbuf
)
541 strcpy (errbuf
, "pcap_open_offline() or pcap_activate() must be "
546 mask
= _w32_sin_mask
;
547 net
= my_ip_addr
& mask
;
550 if (IN_CLASSA(*netmask
))
552 else if (IN_CLASSB(*netmask
))
554 else if (IN_CLASSC(*netmask
))
558 snprintf (errbuf
, PCAP_ERRBUF_SIZE
, "inet class for 0x%lx unknown", mask
);
562 *localnet
= htonl (net
);
563 *netmask
= htonl (mask
);
570 * Get a list of all interfaces that are present and that we probe okay.
571 * Returns -1 on error, 0 otherwise.
572 * The list may be NULL epty if no interfaces were up and could be opened.
574 int pcap_platform_finddevs (pcap_if_list_t
*devlistp
, char *errbuf
)
578 #if 0 /* Pkt drivers should have no addresses */
579 struct sockaddr_in sa_ll_1
, sa_ll_2
;
580 struct sockaddr
*addr
, *netmask
, *broadaddr
, *dstaddr
;
585 for (dev
= (struct device
*)dev_base
; dev
; dev
= dev
->next
)
587 PCAP_ASSERT (dev
->probe
);
589 if (!(*dev
->probe
)(dev
))
592 PCAP_ASSERT (dev
->close
); /* set by probe routine */
597 * XXX - find out whether it's up or running? Does that apply here?
598 * Can we find out if anything's plugged into the adapter, if it's
599 * a wired device, and set PCAP_IF_CONNECTION_STATUS_CONNECTED
600 * or PCAP_IF_CONNECTION_STATUS_DISCONNECTED?
602 if ((curdev
= add_dev(devlistp
, dev
->name
, 0,
603 dev
->long_name
, errbuf
)) == NULL
)
609 #if 0 /* Pkt drivers should have no addresses */
610 memset (&sa_ll_1
, 0, sizeof(sa_ll_1
));
611 memset (&sa_ll_2
, 0, sizeof(sa_ll_2
));
612 sa_ll_1
.sin_family
= AF_INET
;
613 sa_ll_2
.sin_family
= AF_INET
;
615 addr
= (struct sockaddr
*) &sa_ll_1
;
616 netmask
= (struct sockaddr
*) &sa_ll_1
;
617 dstaddr
= (struct sockaddr
*) &sa_ll_1
;
618 broadaddr
= (struct sockaddr
*) &sa_ll_2
;
619 memset (&sa_ll_2
.sin_addr
, 0xFF, sizeof(sa_ll_2
.sin_addr
));
621 if (add_addr_to_dev(curdev
, addr
, sizeof(*addr
),
622 netmask
, sizeof(*netmask
),
623 broadaddr
, sizeof(*broadaddr
),
624 dstaddr
, sizeof(*dstaddr
), errbuf
) < 0)
632 if (ret
== 0 && !found
)
633 strcpy (errbuf
, "No drivers found");
639 * pcap_assert() is mainly used for debugging
641 void pcap_assert (const char *what
, const char *file
, unsigned line
)
644 fprintf (stderr
, "%s (%u): Assertion \"%s\" failed\n",
651 * For pcap_offline_read(): wait and yield between printing packets
652 * to simulate the pace packets where actually recorded.
654 void pcap_set_wait (pcap_t
*p
, void (*yield
)(void), int wait
)
658 struct pcap_dos
*pd
= p
->priv
;
660 pd
->wait_proc
= yield
;
661 p
->opt
.timeout
= wait
;
666 * Initialise a named network device.
668 static struct device
*
669 open_driver (const char *dev_name
, char *ebuf
, int promisc
)
673 for (dev
= (struct device
*)dev_base
; dev
; dev
= dev
->next
)
675 PCAP_ASSERT (dev
->name
);
677 if (strcmp (dev_name
,dev
->name
))
680 if (!probed_dev
) /* user didn't call pcap_lookupdev() first */
682 PCAP_ASSERT (dev
->probe
);
684 if (!(*dev
->probe
)(dev
)) /* call the xx_probe() function */
686 snprintf (ebuf
, PCAP_ERRBUF_SIZE
, "failed to detect device `%s'", dev_name
);
689 probed_dev
= dev
; /* device is probed okay and may be used */
691 else if (dev
!= probed_dev
)
698 /* Select what traffic to receive
701 dev
->flags
|= (IFF_ALLMULTI
| IFF_PROMISC
);
702 else dev
->flags
&= ~(IFF_ALLMULTI
| IFF_PROMISC
);
704 PCAP_ASSERT (dev
->open
);
706 if (!(*dev
->open
)(dev
))
708 snprintf (ebuf
, PCAP_ERRBUF_SIZE
, "failed to activate device `%s'", dev_name
);
709 if (pktInfo
.error
&& !strncmp(dev
->name
,"pkt",3))
712 strcat (ebuf
, pktInfo
.error
);
717 /* Some devices need this to operate in promiscuous mode
719 if (promisc
&& dev
->set_multicast_list
)
720 (*dev
->set_multicast_list
) (dev
);
722 active_dev
= dev
; /* remember our active device */
726 /* 'dev_name' not matched in 'dev_base' list.
730 snprintf (ebuf
, PCAP_ERRBUF_SIZE
, "device `%s' not supported", dev_name
);
737 snprintf (ebuf
, PCAP_ERRBUF_SIZE
, "device `%s' not probed", dev_name
);
744 * Deinitialise MAC driver.
745 * Set receive mode back to default mode.
747 static void close_driver (void)
749 /* !!todo: loop over all 'handle_to_device[]' ? */
750 struct device
*dev
= active_dev
;
752 if (dev
&& dev
->close
)
760 #ifdef USE_32BIT_DRIVERS
773 static void setup_signals (void (*handler
)(int))
775 signal (SIGSEGV
,handler
);
776 signal (SIGILL
, handler
);
777 signal (SIGFPE
, handler
);
780 static void exc_handler (int sig
)
782 #ifdef USE_32BIT_DRIVERS
783 if (active_dev
->irq
> 0) /* excludes IRQ 0 */
785 disable_irq (active_dev
->irq
);
786 irq_eoi_cmd (active_dev
->irq
);
794 fputs ("Catching SIGSEGV.\n", stderr
);
797 fputs ("Catching SIGILL.\n", stderr
);
801 fputs ("Catching SIGFPE.\n", stderr
);
804 fprintf (stderr
, "Catching signal %d.\n", sig
);
809 #endif /* __DJGPP__ */
813 * Open the pcap device for the first client calling pcap_activate()
815 static int first_init (const char *name
, char *ebuf
, int promisc
)
819 #ifdef USE_32BIT_DRIVERS
820 rx_pool
= k_calloc (RECEIVE_BUF_SIZE
, RECEIVE_QUEUE_SIZE
);
823 strcpy (ebuf
, "Not enough memory (Rx pool)");
829 setup_signals (exc_handler
);
832 #ifdef USE_32BIT_DRIVERS
836 dev
= open_driver (name
, ebuf
, promisc
);
839 #ifdef USE_32BIT_DRIVERS
845 setup_signals (SIG_DFL
);
850 #ifdef USE_32BIT_DRIVERS
852 * If driver is NOT a 16-bit "pkt/ndis" driver (having a 'copy_rx_buf'
853 * set in it's probe handler), initialise near-memory ring-buffer for
856 if (dev
->copy_rx_buf
== NULL
)
858 dev
->get_rx_buf
= get_rxbuf
;
859 dev
->peek_rx_buf
= peek_rxbuf
;
860 dev
->release_rx_buf
= release_rxbuf
;
861 pktq_init (&dev
->queue
, RECEIVE_BUF_SIZE
, RECEIVE_QUEUE_SIZE
, rx_pool
);
867 #ifdef USE_32BIT_DRIVERS
868 static void init_32bit (void)
870 static int init_pci
= 0;
873 _printk_init (64*1024, NULL
); /* calls atexit(printk_exit) */
876 (void)pci_init(); /* init BIOS32+PCI interface */
883 * Hook functions for using Watt-32 together with pcap
885 static char rxbuf
[ETH_MAX
+100]; /* rx-buffer with some margin */
887 static pcap_t pcap_save
;
889 static void watt32_recv_hook (u_char
*dummy
, const struct pcap_pkthdr
*pcap
,
892 /* Fix me: assumes Ethernet II only */
893 struct ether_header
*ep
= (struct ether_header
*) buf
;
895 memcpy (rxbuf
, buf
, pcap
->caplen
);
896 etype
= ep
->ether_type
;
900 #if (WATTCP_VER >= 0x0224)
902 * This function is used by Watt-32 to poll for a packet.
903 * i.e. it's set to bypass _eth_arrived()
905 static void *pcap_recv_hook (WORD
*type
)
907 int len
= pcap_read_dos (&pcap_save
, 1, watt32_recv_hook
, NULL
);
913 return (void*) &rxbuf
;
917 * This function is called by Watt-32 (via _eth_xmit_hook).
918 * If dbug_init() was called, we should trace packets sent.
920 static int pcap_xmit_hook (const void *buf
, unsigned len
)
924 if (pcap_pkt_debug
> 0)
925 dbug_write ("pcap_xmit_hook: ");
927 if (active_dev
&& active_dev
->xmit
)
928 if ((*active_dev
->xmit
) (active_dev
, buf
, len
) > 0)
931 if (pcap_pkt_debug
> 0)
932 dbug_write (rc
? "ok\n" : "fail\n");
937 static int pcap_sendpacket_dos (pcap_t
*p
, const void *buf
, size_t len
)
939 struct device
*dev
= p
? get_device(p
->fd
) : NULL
;
941 if (!dev
|| !dev
->xmit
)
943 return (*dev
->xmit
) (dev
, buf
, len
);
947 * This function is called by Watt-32 in tcp_post_init().
948 * We should prevent Watt-32 from using BOOTP/DHCP/RARP etc.
950 static void (*prev_post_hook
) (void);
952 static void pcap_init_hook (void)
954 _w32__bootp_on
= _w32__dhcp_on
= _w32__rarp_on
= 0;
955 _w32__do_mask_req
= 0;
956 _w32_dynamic_host
= 0;
962 * Suppress PRINT message from Watt-32's sock_init()
964 static void null_print (void) {}
967 * To use features of Watt-32 (netdb functions and socket etc.)
968 * we must call sock_init(). But we set various hooks to prevent
969 * using normal PKTDRVR functions in pcpkt.c. This should hopefully
970 * make Watt-32 and pcap co-operate.
972 static int init_watt32 (struct pcap
*pcap
, const char *dev_name
, char *err_buf
)
975 int rc
, MTU
, has_ip_addr
;
976 int using_pktdrv
= 1;
978 /* If user called sock_init() first, we need to reinit in
979 * order to open debug/trace-file properly
984 env
= getenv ("PCAP_TRACE");
985 if (env
&& atoi(env
) > 0 &&
986 pcap_pkt_debug
< 0) /* if not already set */
989 pcap_pkt_debug
= atoi (env
);
992 _watt_do_exit
= 0; /* prevent sock_init() calling exit() */
993 prev_post_hook
= _w32_usr_post_init
;
994 _w32_usr_post_init
= pcap_init_hook
;
995 _w32_print_hook
= null_print
;
997 if (dev_name
&& strncmp(dev_name
,"pkt",3))
998 using_pktdrv
= FALSE
;
1001 has_ip_addr
= (rc
!= 8); /* IP-address assignment failed */
1003 /* if pcap is using a 32-bit driver w/o a pktdrvr loaded, we
1004 * just pretend Watt-32 is initialised okay.
1006 * !! fix-me: The Watt-32 config isn't done if no pktdrvr
1007 * was found. In that case my_ip_addr + sin_mask
1008 * have default values. Should be taken from another
1009 * ini-file/environment in any case (ref. tcpdump.ini)
1013 if (!using_pktdrv
|| !has_ip_addr
) /* for now .... */
1015 static const char myip
[] = "192.168.0.1";
1016 static const char mask
[] = "255.255.255.0";
1018 printf ("Just guessing, using IP %s and netmask %s\n", myip
, mask
);
1019 my_ip_addr
= aton (myip
);
1020 _w32_sin_mask
= aton (mask
);
1022 else if (rc
&& using_pktdrv
)
1024 snprintf (err_buf
, PCAP_ERRBUF_SIZE
, "sock_init() failed, code %d", rc
);
1028 /* Set recv-hook for peeking in _eth_arrived().
1030 #if (WATTCP_VER >= 0x0224)
1031 _eth_recv_hook
= pcap_recv_hook
;
1032 _eth_xmit_hook
= pcap_xmit_hook
;
1035 /* Free the pkt-drvr handle allocated in pkt_init().
1036 * The above hooks should thus use the handle reopened in open_driver()
1041 /* _eth_is_init = 1; */ /* hack to get Rx/Tx-hooks in Watt-32 working */
1044 memcpy (&pcap_save
, pcap
, sizeof(pcap_save
));
1045 MTU
= pkt_get_mtu();
1046 pcap_save
.fcode
.bf_insns
= NULL
;
1047 pcap_save
.linktype
= _eth_get_hwtype (NULL
, NULL
);
1048 pcap_save
.snapshot
= MTU
> 0 ? MTU
: ETH_MAX
; /* assume 1514 */
1050 /* prevent use of resolve() and resolve_ip()
1052 last_nameserver
= 0;
1056 int EISA_bus
= 0; /* Where is natural place for this? */
1059 * Application config hooks to set various driver parameters.
1062 static const struct config_table debug_tab
[] = {
1063 { "PKT.DEBUG", ARG_ATOI
, &pcap_pkt_debug
},
1064 { "PKT.VECTOR", ARG_ATOX_W
, NULL
},
1065 { "NDIS.DEBUG", ARG_ATOI
, NULL
},
1066 #ifdef USE_32BIT_DRIVERS
1067 { "3C503.DEBUG", ARG_ATOI
, &ei_debug
},
1068 { "3C503.IO_BASE", ARG_ATOX_W
, &el2_dev
.base_addr
},
1069 { "3C503.MEMORY", ARG_ATOX_W
, &el2_dev
.mem_start
},
1070 { "3C503.IRQ", ARG_ATOI
, &el2_dev
.irq
},
1071 { "3C505.DEBUG", ARG_ATOI
, NULL
},
1072 { "3C505.BASE", ARG_ATOX_W
, NULL
},
1073 { "3C507.DEBUG", ARG_ATOI
, NULL
},
1074 { "3C509.DEBUG", ARG_ATOI
, &el3_debug
},
1075 { "3C509.ILOOP", ARG_ATOI
, &el3_max_loop
},
1076 { "3C529.DEBUG", ARG_ATOI
, NULL
},
1077 { "3C575.DEBUG", ARG_ATOI
, &debug_3c575
},
1078 { "3C59X.DEBUG", ARG_ATOI
, &vortex_debug
},
1079 { "3C59X.IFACE0", ARG_ATOI
, &vortex_options
[0] },
1080 { "3C59X.IFACE1", ARG_ATOI
, &vortex_options
[1] },
1081 { "3C59X.IFACE2", ARG_ATOI
, &vortex_options
[2] },
1082 { "3C59X.IFACE3", ARG_ATOI
, &vortex_options
[3] },
1083 { "3C90X.DEBUG", ARG_ATOX_W
, &tc90xbc_debug
},
1084 { "ACCT.DEBUG", ARG_ATOI
, ðpk_debug
},
1085 { "CS89.DEBUG", ARG_ATOI
, &cs89_debug
},
1086 { "RTL8139.DEBUG", ARG_ATOI
, &rtl8139_debug
},
1087 /* { "RTL8139.FDUPLEX", ARG_ATOI, &rtl8139_options }, */
1088 { "SMC.DEBUG", ARG_ATOI
, &ei_debug
},
1089 /* { "E100.DEBUG", ARG_ATOI, &e100_debug }, */
1090 { "PCI.DEBUG", ARG_ATOI
, &pci_debug
},
1091 { "BIOS32.DEBUG", ARG_ATOI
, &bios32_debug
},
1092 { "IRQ.DEBUG", ARG_ATOI
, &irq_debug
},
1093 { "TIMER.IRQ", ARG_ATOI
, &timer_irq
},
1099 * pcap_config_hook() is an extension to application's config
1100 * handling. Uses Watt-32's config-table function.
1102 int pcap_config_hook (const char *keyword
, const char *value
)
1104 return parse_config_table (debug_tab
, NULL
, keyword
, value
);
1108 * Linked list of supported devices
1110 struct device
*active_dev
= NULL
; /* the device we have opened */
1111 struct device
*probed_dev
= NULL
; /* the device we have probed */
1112 const struct device
*dev_base
= &pkt_dev
; /* list of network devices */
1115 * PKTDRVR device functions
1117 int pcap_pkt_debug
= -1;
1119 static void pkt_close (struct device
*dev
)
1121 BOOL okay
= PktExitDriver();
1123 if (pcap_pkt_debug
> 1)
1124 fprintf (stderr
, "pkt_close(): %d\n", okay
);
1131 static int pkt_open (struct device
*dev
)
1135 if (dev
->flags
& IFF_PROMISC
)
1136 mode
= PDRX_ALL_PACKETS
;
1137 else mode
= PDRX_BROADCAST
;
1139 if (!PktInitDriver(mode
))
1142 PktResetStatistics (pktInfo
.handle
);
1143 PktQueueBusy (FALSE
);
1147 static int pkt_xmit (struct device
*dev
, const void *buf
, int len
)
1149 struct net_device_stats
*stats
= (struct net_device_stats
*) dev
->priv
;
1151 if (pcap_pkt_debug
> 0)
1152 dbug_write ("pcap_xmit\n");
1154 if (!PktTransmit(buf
,len
))
1162 static void *pkt_stats (struct device
*dev
)
1164 struct net_device_stats
*stats
= (struct net_device_stats
*) dev
->priv
;
1166 if (!stats
|| !PktSessStatistics(pktInfo
.handle
))
1169 stats
->rx_packets
= pktStat
.inPackets
;
1170 stats
->rx_errors
= pktStat
.lost
;
1171 stats
->rx_missed_errors
= PktRxDropped();
1175 static int pkt_probe (struct device
*dev
)
1177 if (!PktSearchDriver())
1180 dev
->open
= pkt_open
;
1181 dev
->xmit
= pkt_xmit
;
1182 dev
->close
= pkt_close
;
1183 dev
->get_stats
= pkt_stats
;
1184 dev
->copy_rx_buf
= PktReceive
; /* farmem peek and copy routine */
1185 dev
->get_rx_buf
= NULL
;
1186 dev
->peek_rx_buf
= NULL
;
1187 dev
->release_rx_buf
= NULL
;
1188 dev
->priv
= calloc (sizeof(struct net_device_stats
), 1);
1195 * NDIS device functions
1197 static void ndis_close (struct device
*dev
)
1205 static int ndis_open (struct device
*dev
)
1207 int promis
= (dev
->flags
& IFF_PROMISC
);
1210 if (!NdisInit(promis
))
1219 static void *ndis_stats (struct device
*dev
)
1221 static struct net_device_stats stats
;
1228 static int ndis_probe (struct device
*dev
)
1235 dev
->open
= ndis_open
;
1237 dev
->close
= ndis_close
;
1238 dev
->get_stats
= ndis_stats
;
1239 dev
->copy_rx_buf
= NULL
; /* to-do */
1240 dev
->get_rx_buf
= NULL
; /* upcall is from rmode driver */
1241 dev
->peek_rx_buf
= NULL
;
1242 dev
->release_rx_buf
= NULL
;
1247 * Search & probe for supported 32-bit (pmode) pcap devices
1249 #if defined(USE_32BIT_DRIVERS)
1251 struct device el2_dev LOCKED_VAR
= {
1260 struct device el3_dev LOCKED_VAR
= {
1269 struct device tc515_dev LOCKED_VAR
= {
1278 struct device tc59_dev LOCKED_VAR
= {
1287 struct device tc90xbc_dev LOCKED_VAR
= {
1296 struct device wd_dev LOCKED_VAR
= {
1305 struct device ne_dev LOCKED_VAR
= {
1314 struct device acct_dev LOCKED_VAR
= {
1316 "Accton EtherPocket",
1323 struct device cs89_dev LOCKED_VAR
= {
1325 "Crystal Semiconductor",
1332 struct device rtl8139_dev LOCKED_VAR
= {
1338 rtl8139_probe
/* dev->probe routine */
1342 * Dequeue routine is called by polling.
1343 * NOTE: the queue-element is not copied, only a pointer is
1344 * returned at '*buf'
1346 int peek_rxbuf (BYTE
**buf
)
1348 struct rx_elem
*tail
, *head
;
1350 PCAP_ASSERT (pktq_check (&active_dev
->queue
));
1353 tail
= pktq_out_elem (&active_dev
->queue
);
1354 head
= pktq_in_elem (&active_dev
->queue
);
1359 PCAP_ASSERT (tail
->size
< active_dev
->queue
.elem_size
-4-2);
1361 *buf
= &tail
->data
[0];
1362 return (tail
->size
);
1369 * Release buffer we peeked at above.
1371 int release_rxbuf (BYTE
*buf
)
1374 struct rx_elem
*tail
= pktq_out_elem (&active_dev
->queue
);
1376 PCAP_ASSERT (&tail
->data
[0] == buf
);
1380 pktq_inc_out (&active_dev
->queue
);
1385 * get_rxbuf() routine (in locked code) is called from IRQ handler
1386 * to request a buffer. Interrupts are disabled and we have a 32kB stack.
1388 BYTE
*get_rxbuf (int len
)
1392 if (len
< ETH_MIN
|| len
> ETH_MAX
)
1395 idx
= pktq_in_index (&active_dev
->queue
);
1399 static int fan_idx LOCKED_VAR
= 0;
1400 writew ("-\\|/"[fan_idx
++] | (15 << 8), /* white on black colour */
1401 0xB8000 + 2*79); /* upper-right corner, 80-col colour screen */
1404 /* writew (idx + '0' + 0x0F00, 0xB8000 + 2*78); */
1407 if (idx
!= active_dev
->queue
.out_index
)
1409 struct rx_elem
*head
= pktq_in_elem (&active_dev
->queue
);
1412 active_dev
->queue
.in_index
= idx
;
1413 return (&head
->data
[0]);
1416 /* !!to-do: drop 25% of the oldest element
1418 pktq_clear (&active_dev
->queue
);
1423 * Simple ring-buffer queue handler for reception of packets
1424 * from network driver.
1426 #define PKTQ_MARKER 0xDEADBEEF
1428 static int pktq_check (struct rx_ringbuf
*q
)
1435 if (!q
|| !q
->num_elem
|| !q
->buf_start
)
1441 for (i
= 0; i
< q
->num_elem
; i
++)
1443 buf
+= q
->elem_size
;
1444 if (*(DWORD
*)(buf
- sizeof(DWORD
)) != PKTQ_MARKER
)
1451 static int pktq_init (struct rx_ringbuf
*q
, int size
, int num
, char *pool
)
1455 q
->elem_size
= size
;
1457 q
->buf_start
= pool
;
1461 PCAP_ASSERT (size
>= sizeof(struct rx_elem
) + sizeof(DWORD
));
1465 for (i
= 0; i
< num
; i
++)
1468 struct rx_elem
*elem
= (struct rx_elem
*) pool
;
1470 /* assert dword aligned elements
1472 PCAP_ASSERT (((unsigned)(&elem
->data
[0]) & 3) == 0);
1475 *(DWORD
*) (pool
- sizeof(DWORD
)) = PKTQ_MARKER
;
1481 * Increment the queue 'out_index' (tail).
1484 static int pktq_inc_out (struct rx_ringbuf
*q
)
1487 if (q
->out_index
>= q
->num_elem
)
1489 return (q
->out_index
);
1493 * Return the queue's next 'in_index' (head).
1496 static int pktq_in_index (struct rx_ringbuf
*q
)
1498 volatile int index
= q
->in_index
+ 1;
1500 if (index
>= q
->num_elem
)
1506 * Return the queue's head-buffer.
1508 static struct rx_elem
*pktq_in_elem (struct rx_ringbuf
*q
)
1510 return (struct rx_elem
*) (q
->buf_start
+ (q
->elem_size
* q
->in_index
));
1514 * Return the queue's tail-buffer.
1516 static struct rx_elem
*pktq_out_elem (struct rx_ringbuf
*q
)
1518 return (struct rx_elem
*) (q
->buf_start
+ (q
->elem_size
* q
->out_index
));
1522 * Clear the queue ring-buffer by setting head=tail.
1524 static void pktq_clear (struct rx_ringbuf
*q
)
1526 q
->in_index
= q
->out_index
;
1530 * Symbols that must be linkable for "gcc -O0"
1538 #include "msdos/pm_drvr/ioport.h"
1539 #include "msdos/pm_drvr/dma.h"
1541 #endif /* USE_32BIT_DRIVERS */
1544 * Libpcap version string.
1547 pcap_lib_version(void)
1549 return ("DOS-" PCAP_VERSION_STRING
);