Watt-32 tcp/ip  2.2 dev-rel.10
Functions | Variables
winpkt.c File Reference

Go to the source code of this file.

Functions

static enum eth_init_result open_winpcap_adapter (const char *name)
 Open the WinPcap named device.
 
static enum eth_init_result open_airpcap_adapter (const char *name)
 Open the named AirPcap device. More...
 
static enum eth_init_result open_wanpacket_adapter (const char *name)
 Open the named WanPacket device. More...
 
static enum eth_init_result open_swsvpkt_adapter (const char *name)
 Open the named SwsVpkt device.
 
static BOOL get_if_stat_swsvpkt (const struct SwsVpktUsr *usr, BOOL *is_up)
 
static BOOL get_perm_mac_address (void *mac)
 
static BOOL get_interface_mtu (DWORD *mtu)
 
static void show_link_details (void)
 
static void set_txmode (const char *value)
 Select async (overlapping) tx-mode. More...
 
static void swsvpkt_callback (struct SwsVpktUsr *usr, const void *buf, unsigned len)
 Our low-level SwsVpkt receive callback.
 
static DWORD __stdcall winpcap_recv_thread (void *arg)
 Our low-level WinPcap capture thread. More...
 
void winpkt_trace (const char *fmt,...)
 
void winpkt_trace_fclose (void)
 
static BOOL parse_config_pass_1 (void)
 WATTCP.CFG parser for "WINPKT." keywords in WATTCP.CFG.
 
static BOOL find_winpcap_adapter (char *aname, size_t size)
 Traverse 'adapters_list' and select 1st device with an IPv4 address. More...
 
static BOOL find_swsvpkt_adapter (char *aname, size_t size)
 
static BOOL find_adapter (char *aname, size_t size)
 Search for the first proper WinPcap or SwsVpkt device. More...
 
static BOOL legal_recv_mode (WORD bits)
 
int W32_CALL pkt_eth_init (mac_address *mac_addr)
 Initialise WinPcap/SwsVpkt/AirPcap/WanPacket and return our MAC address. More...
 
int W32_CALL pkt_release (void)
 
int W32_CALL pkt_get_addr (mac_address *eth)
 Return the MAC address.
 
int W32_CALL pkt_set_addr (const void *eth)
 Set a new MAC source address.
 
BOOL W32_CALL pkt_is_init (void)
 
BOOL W32_CALL pkt_check_address (DWORD my_ip)
 
int W32_CALL pkt_buf_wipe (void)
 Clear the receive queue.
 
int W32_CALL pkt_waiting (void)
 Return number of packets waiting in queue.
 
void W32_CALL pkt_free_pkt (const void *pkt)
 Release a packet from the receive queue. More...
 
struct pkt_rx_elementpkt_poll_recv (void)
 
int W32_CALL pkt_send (const void *tx, int length)
 Send a link-layer frame. More...
 
int W32_CALL pkt_set_rcv_mode (int mode)
 Sets the receive mode of the interface. More...
 
int W32_CALL pkt_get_rcv_mode (void)
 Gets the receive mode of the interface (can never be mode 0). More...
 
static BOOL get_stats_pcap (const ADAPTER *adapter, struct PktStats *stats, struct PktStats *total)
 
static BOOL get_stats_swsvpkt (const struct SwsVpktUsr *usr, struct PktStats *stats, struct PktStats *total)
 
int W32_CALL pkt_get_stats (struct PktStats *stats, struct PktStats *total)
 Get PKTDRVR statistics. More...
 
DWORD W32_CALL pkt_dropped (void)
 Return number of packets dropped.
 
int W32_CALL pkt_get_mtu (void)
 Return PKTDRVR maximum-transmit-units (MTU). More...
 
const char *W32_CALL pkt_strerror (int code)
 Return textual error representing error-code. More...
 
int pkt_get_multicast_list (mac_address *listbuf, int *len)
 Gets the current list of multicast addresses from the PKTDRVR. More...
 
int pkt_set_multicast_list (const void *listbuf, int len)
 Sets the list of multicast addresses for which the PKTDRVR is responsible. More...
 
static BOOL get_if_mtu_pcap (const ADAPTER *adapter, DWORD *mtu)
 
static BOOL get_if_mtu_generic (const void *adapter, DWORD *mtu)
 
static BOOL get_if_type_pcap (const ADAPTER *adapter, WORD *type)
 
static BOOL get_if_type_swsvpkt (const struct SwsVpktUsr *usr, WORD *type)
 
static BOOL get_if_type_airpcap (const AIR_ADAPTER *adapter, WORD *type)
 
static BOOL get_descr_pcap (const ADAPTER *adapter, char *buf, size_t max)
 
static BOOL get_descr_swsvpkt (const struct SwsVpktUsr *usr, char *buf, size_t max)
 
static BOOL get_if_speed_pcap (const ADAPTER *adapter, DWORD *Mbit_s)
 
static BOOL get_if_speed_airpcap (const AIR_ADAPTER *adapter, DWORD *Mbit_s)
 
static BOOL get_interface_speed (DWORD *speed)
 
static BOOL get_phys_media (int *media)
 
static BOOL get_if_stat_pcap (const ADAPTER *adapter, BOOL *is_up)
 
static BOOL ndis_set_loopback (BOOL enable)
 
int W32_CALL pkt_get_api_ver (WORD *ver_p)
 Return version of spec. More...
 
int W32_CALL pkt_get_drvr_ver (WORD *major, WORD *minor, WORD *unused, WORD *build)
 Return version of PKTDRVR.
 
const char *W32_CALL pkt_get_drvr_name (void)
 
const char *W32_CALL pkt_get_device_name (void)
 
const char *W32_CALL pkt_get_drvr_descr (void)
 
W32_FUNC WORD W32_CALL pkt_get_drvr_class (void)
 

Variables

WORD _pktdevclass = PDCLASS_UNKNOWN
 Driver class; <tcp.h> for values. More...
 
WORD _pkt_ip_ofs = 0
 Offset from MAC-header to IP-header. More...
 
BOOL _pktserial = FALSE
 Driver is a serial (SLIP/PPP) driver. More...
 
int _pkt_errno = 0
 Last PKTDRVR error code. More...
 
int _pkt_rxmode = RXMODE_DEFAULT
 active receive mode More...
 
int _pkt_rxmode0 = -1
 startup receive mode More...
 
int _pkt_forced_rxmode = -1
 wattcp.cfg receive mode More...
 
char _pktdrvrname [MAX_VALUELEN+1] = ""
 Name of PKDRVR.
 
char _pktdrvr_descr [MAX_VALUELEN+1] = ""
 
struct pkt_info_pkt_inf = NULL
 module data that will be locked
 
static int pkt_num_rx_bufs = RX_BUFS
 
static DWORD num_tx_pkt = 0UL
 
static DWORD num_tx_bytes = 0UL
 
static DWORD num_tx_errors = 0UL
 
static DWORD num_rx_pkt = 0UL
 
static DWORD num_rx_bytes = 0UL
 
static DWORD pkt_drop_cnt = 0UL
 
static int pkt_txretries = 2
 
static int thr_realtime = 0
 
static BOOL thr_stopped = FALSE
 
static BOOL config_pass_1_done = FALSE
 
static char dump_fname [MAX_PATHLEN] = "$(TEMP)\\winpkt_dump.txt"
 
static const struct search_list logical_media []
 
static const struct search_list phys_media []
 
const char * winpkt_trace_func
 
const char * winpkt_trace_file
 
UINT winpkt_trace_line
 
UINT winpkt_trace_level = 0
 
static FILE * trace_file
 

Detailed Description

Packet-driver like interface to WinPcap and SwsVpkt.

Copyright (c) 2004-2006 Gisle Vanem gvane.nosp@m.m@ya.nosp@m.hoo.n.nosp@m.o

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. All advertising materials mentioning features or use of this software must display the following acknowledgement: This product includes software developed by Gisle Vanem Bergen, Norway.

THIS SOFTWARE IS PROVIDED BY ME (Gisle Vanem) AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL I OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Definition in file winpkt.c.

Function Documentation

static BOOL find_adapter ( char *  aname,
size_t  size 
)
static

Search for the first proper WinPcap or SwsVpkt device.

Only called if user didn't specify a device in WATTCP.CFG (with "winpkt.device").

Definition at line 322 of file winpkt.c.

References _eth_airpcap, _eth_SwsVpkt, _eth_winpcap, and find_winpcap_adapter().

static BOOL find_winpcap_adapter ( char *  aname,
size_t  size 
)
static

Traverse 'adapters_list' and select 1st device with an IPv4 address.

Assuming that device is a physical adapter we can use.

Definition at line 263 of file winpkt.c.

References _strlcpy().

static BOOL get_if_type_airpcap ( const AIR_ADAPTER *  adapter,
WORD *  type 
)
static
Todo:
Fix me

Definition at line 1249 of file winpkt.c.

static BOOL get_if_type_swsvpkt ( const struct SwsVpktUsr usr,
WORD *  type 
)
static
Todo:
Fix me

Definition at line 1242 of file winpkt.c.

static enum eth_init_result open_airpcap_adapter ( const char *  name)
static

Open the named AirPcap device.

<

Todo:

Definition at line 1551 of file winpkt.c.

References get_if_type_airpcap().

static enum eth_init_result open_wanpacket_adapter ( const char *  name)
static

Open the named WanPacket device.

<

Todo:

Definition at line 1576 of file winpkt.c.

int W32_CALL pkt_eth_init ( mac_address *  mac_addr)
void W32_CALL pkt_free_pkt ( const void pkt)

Release a packet from the receive queue.

If USE_FAST_PKT, pkt has already been free'd. Just update drop count.

Definition at line 700 of file winpkt.c.

References _pkt_ip_ofs.

int W32_CALL pkt_get_api_ver ( WORD *  ver_p)

Return version of spec.

this PKTDRVR conforms to.

Return values
-1on error. Return major version in upper 8 bit, minor in lower.

Definition at line 1422 of file winpkt.c.

References _eth_airpcap, _eth_SwsVpkt, and _eth_winpcap.

int W32_CALL pkt_get_mtu ( void  )

Return PKTDRVR maximum-transmit-units (MTU).

Note
This includes length of MAC-header.

Definition at line 1136 of file winpkt.c.

References _pktdevclass.

int pkt_get_multicast_list ( mac_address *  listbuf,
int *  len 
)

Gets the current list of multicast addresses from the PKTDRVR.

  • listbuf is the buffer into which the list is placed
  • len on input: length of listbuf on output: length of list returned
Return values
0upon error - _pkt_errno is set
1if retrieval was successful,

Definition at line 1165 of file winpkt.c.

References _pkt_errno, _pktdevlevel, _pktserial, and get_rmode_data().

int W32_CALL pkt_get_rcv_mode ( void  )

Gets the receive mode of the interface (can never be mode 0).

Return values
!0Okay - _pkt_errno is 0, _pkt_rxmode and retval is current mode.
0Error - _pkt_errno is set.

_pkt_rxmode is one of the following modes: (upon successful return)

  • 1 - turn off receiver
  • 2 - receive only packets sent to this interface
  • 3 - mode 2 plus broadcast packets (default)
  • 4 - mode 3 plus limited multicast packets
  • 5 - mode 3 plus all multicast packets
  • 6 - all packets (a.k.a promiscuous mode)

Definition at line 1017 of file winpkt.c.

References _eth_airpcap, _eth_SwsVpkt, _pkt_errno, _pkt_rxmode, _pktdevlevel, and _pktserial.

int W32_CALL pkt_get_stats ( struct PktStats stats,
struct PktStats total 
)

Get PKTDRVR statistics.

Return both 'stats' for current session and 'total' statistics since driver loaded.

Definition at line 1122 of file winpkt.c.

References get_rmode_data().

int W32_CALL pkt_release ( void  )
Todo:
We might be called between 1st and 2nd packet-driver upcall. Need to wait for 2nd upcall to finish or else freeing the RMCB too early could cause a crash or a stuck PKTDRVR.

Definition at line 577 of file winpkt.c.

References _eth_winpcap, _watt_fatal_error, pkt_release_handle(), pkt_reset_handle(), and release_real_mem().

int W32_CALL pkt_send ( const void tx,
int  length 
)

Send a link-layer frame.

For PPP/SLIP 'tx' contains no MAC-header. For EtherNet, Token-Ring, ARCNET and FDDI, 'tx' contains the complete frame.

Todo:
Change this so that we doesn't retry if the driver was stuck for "too long". I've seen drivers using approx. 100 msec on each iteration below. Retrying if the driver is stuck is not worth the time.

Definition at line 937 of file winpkt.c.

References _eth_SwsVpkt, pkt_tx_buf(), and Wait().

int pkt_set_multicast_list ( const void listbuf,
int  len 
)

Sets the list of multicast addresses for which the PKTDRVR is responsible.

  • listbuf is the buffer containing the list
  • len is the length of listbuf
Return values
0upon error - _pkt_errno is set
1if set was successful

Definition at line 1174 of file winpkt.c.

References _pkt_errno, _pktdevlevel, and _pktserial.

int W32_CALL pkt_set_rcv_mode ( int  mode)

Sets the receive mode of the interface.

mode is one of the following modes:

  • 1 - turn off receiver
  • 2 - receive only packets sent to this interface
  • 3 - mode 2 plus broadcast packets (default)
  • 4 - mode 3 plus limited multicast packets
  • 5 - mode 3 plus all multicast packets
  • 6 - all packets (AKA promiscuous mode)
Return values
0- upon error - _pkt_errno is set
1- if the mode was set successfully

Definition at line 984 of file winpkt.c.

References _eth_airpcap, _eth_SwsVpkt, _pkt_errno, _pkt_rxmode, _pktdevlevel, and _pktserial.

const char* W32_CALL pkt_strerror ( int  code)

Return textual error representing error-code.

Todo:
Return strings for these: PDERR_GEN_FAIL PDERR_NO_DRIVER PDERR_NO_CLASS PDERR_NO_MULTICAST PDERR_NO_SPACE
  • GetLastError(). Use win_strerror()

Definition at line 1145 of file winpkt.c.

References _strlcpy().

static void set_txmode ( const char *  value)
static

Select async (overlapping) tx-mode.

NOT YET.

Definition at line 727 of file winpkt.c.

static DWORD __stdcall winpcap_recv_thread ( void arg)
static

Our low-level WinPcap capture thread.

Loop waiting for packet(s) in PacketReceivePacket(). In '_pkt_release()' the receive event-handle (adapter->Readvent) is set. This causes 'WaitForSingleObject()' in 'PacketReceivePacket()' to return. It is vital that the ADAPTER object isn't deallocated before 'PacketReceivePacket()' returns.

The return-value is > 0 if thread exited on it's own. Otherwise it is 0 (from 'GetExitCodeThread()').

Note
: If multiple threads are using Watt-32 function, there could be a race contention here. Maybe 'ENTER_CRIT()' should be moved further up? E.g. if 'gethostbyname()' is called in one side-thread and 'select()' is called in the main-thread, this could cause race hazards in several places. Need to test this carefully. Ref. the '_WINDOWS_NSL' code in Lynx/Win32.
Todo:
: Take the 'adapter' info from 'arg' instead of using the global '_pkt_inf' structure.

Definition at line 754 of file winpkt.c.

Variable Documentation

int _pkt_errno = 0

Last PKTDRVR error code.

Last PKTDRVR error code.

Definition at line 88 of file winpkt.c.

int _pkt_forced_rxmode = -1

wattcp.cfg receive mode

Forced Rx-mode via WATTCP.CFG.

Definition at line 91 of file winpkt.c.

WORD _pkt_ip_ofs = 0

Offset from MAC-header to IP-header.

Offset from MAC-header to IP-header.

Definition at line 86 of file winpkt.c.

int _pkt_rxmode = RXMODE_DEFAULT

active receive mode

Current PKTDRVR/WinPcap Rx-mode.

Definition at line 89 of file winpkt.c.

int _pkt_rxmode0 = -1

startup receive mode

Startup receive mode.

Definition at line 90 of file winpkt.c.

WORD _pktdevclass = PDCLASS_UNKNOWN

Driver class; <tcp.h> for values.

Driver class; <tcp.h> for values.

Definition at line 85 of file winpkt.c.

BOOL _pktserial = FALSE

Driver is a serial (SLIP/PPP) driver.

Driver is a serial (SLIP/PPP) driver.

Definition at line 87 of file winpkt.c.

const struct search_list logical_media[]
static
Initial value:
= {
{ NdisMediumNull, "Unknown?" },
{ NdisMedium802_3, "802.3" },
{ NdisMedium802_5, "802.5" },
{ NdisMediumFddi, "FDDI" },
{ NdisMediumWan, "WAN" },
{ NdisMediumLocalTalk, "LocalTalk" },
{ NdisMediumDix, "DIX" },
{ NdisMediumArcnetRaw, "ArcNet (raw)" },
{ NdisMediumArcnet878_2, "ArcNet (878.2)" },
{ NdisMediumAtm, "ATM" },
{ NdisMediumWirelessWan, "WiFi" },
{ NdisMediumIrda, "IrDA" },
{ NdisMediumBpc, "BPC" },
{ NdisMediumCoWan, "CoWan" },
{ NdisMedium1394, "1394 FW" },
{ NdisMediumInfiniBand, "Inf-band" },
{ NdisMediumTunnel, "Tunnel" },
{ NdisMediumNative802_11, "Native 802.11" },
{ NdisMediumLoopback, "Loopback" },
{ NdisMediumWiMAX, "WiMax" },
{ NdisMediumIP, "IP" },
{ NdisMediumCHDLC, "CHDLC" },
{ NdisMediumPPPSerial, "PPP-ser" }
}

Definition at line 119 of file winpkt.c.

const struct search_list phys_media[]
static
Initial value:
= {
{ ND_PHY_WirelessLan, "Wireless LAN" },
{ ND_PHY_CableModem, "Cable" },
{ ND_PHY_PhoneLine, "Phone" },
{ ND_PHY_PowerLine, "PowerLine" },
{ ND_PHY_DSL, "DSL" },
{ ND_PHY_FibreChannel, "Fibre" },
{ ND_PHY_1394, "IEEE1394" },
{ ND_PHY_WirelessWan, "Wireless WAN" },
{ ND_PHY_Native802_11, "802.11" },
{ ND_PHY_Bluetooth, "Bluetooth" },
{ ND_PHY_Infiniband, "Infiniband" },
{ ND_PHY_WiMax, "Wi-Max" },
{ ND_PHY_UWB, "UWB" },
{ ND_PHY_802_3, "802.3" },
{ ND_PHY_802_5, "80.25" },
{ ND_PHY_Irda, "Irda" },
{ ND_PHY_WiredWAN, "Wired WAN" },
{ ND_PHY_WiredCoWan, "Wired CoWan" },
{ ND_PHY_Other, "Other?" }
}

Definition at line 145 of file winpkt.c.