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

Go to the source code of this file.

Functions

static const char * get_ARP_flags (WORD flg)
 
static const char * get_route_flags (WORD flg)
 
static BOOL LAN_lookup (DWORD ip, eth_address *eth)
 Lookup host in fixed/dynamic list.
 
static BOOL is_on_LAN (DWORD ip)
 This should probably go into route.c.
 
static void arp_daemon (void)
 ARP background daemon. More...
 
static BOOL arp_check_common (DWORD ip, eth_address *eth)
 Common stuff for _arp_resolve() and _arp_lookup()
 
int W32_CALL _arp_gateways_get (const struct gate_entry **rc)
 Return start of gate_list[]. More...
 
static int ping_gateway (DWORD host, void *eth)
 
static void check_dead_gw (void)
 
static BOOL legal_mask (DWORD mask, DWORD addr)
 
static int MS_CDECL compare (const struct gate_entry *a, const struct gate_entry *b)
 Compare two entries in gateway-list. More...
 
BOOL W32_CALL _arp_add_gateway (const char *config_string, DWORD ip)
 Add a gateway to the routing table. More...
 
void W32_CALL _arp_kill_gateways (void)
 Delete all gateways.
 
BOOL W32_CALL _arp_have_default_gw (void)
 Check if we have at least one default gateway.
 
static BOOL arp_send (const arp_Header *arp, unsigned line)
 Low-level ARP send function.
 
static BOOL arp_send_request (DWORD ip)
 Send broadcast ARP request.
 
BOOL W32_CALL _arp_reply (const void *e_dst, DWORD src_ip, DWORD dst_ip)
 Send unicast/broadcast ARP reply. More...
 
static void arp_move_entry (int to_index, int from_index)
 Move an ARP entry from from_index to to_index.
 
int W32_CALL _arp_cache_get (const struct arp_entry **rc)
 Return start of arp_list[]. More...
 
static BOOL LAN_start_lookup (DWORD ip)
 Start a host lookup on the LAN. More...
 
static BOOL LAN_lookup_pending (DWORD ip)
 Lookup host in pending list.
 
static void arp_check_timeouts (BOOL check_dynamic_entries)
 Check ARP entries for timeout. More...
 
static void route_move_entry (int to_index, int from_index)
 
static BOOL route_make_new_slot (DWORD host_ip, DWORD gate_ip, DWORD mask)
 
BOOL W32_CALL _arp_register (DWORD use_this_gateway_ip, DWORD for_this_host_ip)
 Register a new host as gateway. More...
 
static BOOL route_lookup (DWORD ip, eth_address *eth)
 Gets MAC of gateway needed to reach the given host.
 
static BOOL route_lookup_pending (DWORD ip)
 Returns TRUE if the lookup of the gateway (assigned to the supplied ip) is still pending.
 
static BOOL route_start_lookup (DWORD ip)
 Start a route lookup.
 
static void route_check_timeouts (BOOL check_dynamic_entries)
 Periodic route checker. More...
 
BOOL W32_CALL _arp_start_lookup (DWORD ip)
 "PUBLIC" ARP/ROUTE FUNCTIONS. More...
 
BOOL W32_CALL _arp_lookup (DWORD ip, eth_address *eth)
 Lookup MAC-address of 'ip'. More...
 
BOOL W32_CALL _arp_lookup_pending (DWORD ip)
 An ARP-lookup timeout check. More...
 
BOOL W32_CALL _arp_lookup_fixed (DWORD ip, eth_address *eth)
 Lookup fixed MAC-address of 'ip'. More...
 
BOOL W32_CALL _arp_resolve (DWORD ip, eth_address *eth)
 The blocking lookup function visible to higher functions. More...
 
BOOL W32_CALL _arp_cache_add (DWORD ip, const void *eth, BOOL expires)
 Add given IP/Ether address to ARP-cache. More...
 
BOOL W32_CALL _arp_cache_del (DWORD ip)
 Delete given 'ip' address from ARP-cache (dynamic, fixed or pending). More...
 
static void (W32_CALL *prev_cfg_hook)(const char *
 Parser for "\c ARP.xx" keywords in "\c WATTCP.CFG".
 
static const char *static void
W32_CALL 
arp_parse (const char *name, const char *value)
 
void W32_CALL _arp_init (void)
 Setup config-table parse function and add background ARP deamon. More...
 
BOOL W32_CALL _arp_handler (const arp_Header *ah, BOOL brdcast)
 Receive ARP handler. More...
 
BOOL W32_CALL _arp_check_own_ip (eth_address *other_guy)
 Used by DHCP initialisation. More...
 
int W32_CALL _arp_check_gateways (void)
 Return number of default gateways.
 
void W32_CALL _arp_cache_dump (void)
 
void W32_CALL _arp_routes_dump (void)
 
void W32_CALL _arp_gateways_dump (void)
 
void W32_CALL _arp_debug_dump (void)
 Debug-dump of configured gateways, route table and ARP cache. More...
 
static DWORD route_destin (DWORD ip)
 
static int callback (void)
 
int main (int argc, char **argv)
 

Variables

static int arp_trace_level = 0
 
static int arp_timeout = 2
 
static int arp_alive = 300
 
static int arp_rexmit_to = 250
 
static BOOL dead_gw_detect = FALSE
 
static BOOL arp_gratiotous = FALSE
 
static int gate_top = 0
 
static struct gate_entry gate_list [GATE_TOP_OF_CACHE]
 
static WORD icmp_id = 0
 Send a ping (with TTL=1) to a gateway. More...
 
static WORD icmp_seq = 0
 
static int arp_first_pending = ARP_TOP_PENDING
 
static int arp_first_free = 0
 
static int arp_first_dynamic = 0
 
static struct arp_entry arp_list [ARP_TOP_OF_CACHE]
 
static int route_first_pending = ROUTE_TOP_PENDING
 
static int route_first_free = 0
 
static struct route_entry route_list [ROUTE_TOP_OF_CACHE]
 
static int num_okay = 0
 
static int num_fail = 0
 

Detailed Description

Address Resolution Protocol.

2002-09 Gundolf von Bachhaus: 90% rewrite - Optional non-blocking ARP lookup & redirect handling Gateway / Route / ARP data stored & accessed separately Module encapsulated - no global variables.

Definition in file pcarp.c.

Function Documentation

BOOL W32_CALL _arp_add_gateway ( const char *  config_string,
DWORD  ip 
)

Add a gateway to the routing table.

The format of 'config_string' is: gateway [,subnet], mask]]

If 'config_string' is NULL, simply add 'ip' with zero mask and subnet.

eg. "gateway = 129.97.176.1" -> becomes the default route eg. "gateway = 129.97.176.2, 129.97.0.0, 255.255.0.0"

The first example shows how a default gateway is created. A default gateway is used if no other choices exist.

The second example shows how to specify a gateway for a particular subnet. In this example, whenever the 'top' 16 bits of the destination are 129.97.*.*, that gateway will be used.

Todo:
Move to route.c and rename to route_add().

Definition at line 247 of file pcarp.c.

References aton(), compare(), gate_entry::gate_ip, gate_entry::mask, and gate_entry::subnet.

BOOL W32_CALL _arp_cache_add ( DWORD  ip,
const void eth,
BOOL  expires 
)

Add given IP/Ether address to ARP-cache.

ARP cache functions.

Note
'ip' is on host order.

Definition at line 1352 of file pcarp.c.

References _arp_cache_del(), _inet_ntoa(), arp_move_entry(), arp_entry::expiry, arp_entry::flags, arp_entry::hardware, arp_entry::ip, is_on_LAN(), MAC_address(), my_ip_addr, and set_timeout().

BOOL W32_CALL _arp_cache_del ( DWORD  ip)

Delete given 'ip' address from ARP-cache (dynamic, fixed or pending).

Note
'ip' is on host order.

Definition at line 1421 of file pcarp.c.

References arp_move_entry(), arp_entry::flags, and arp_entry::ip.

int W32_CALL _arp_cache_get ( const struct arp_entry **  rc)

Return start of arp_list[].

Note
Called need to inspect the '*rc->flags' for validity.

Definition at line 527 of file pcarp.c.

BOOL W32_CALL _arp_check_own_ip ( eth_address *  other_guy)

Used by DHCP initialisation.

Do an ARP resolve on our own IP address to check if someone else is replying. Return non-zero if someone replied.

Note
Blocks waiting for reply or timeout.

Definition at line 1729 of file pcarp.c.

References _arp_resolve(), and my_ip_addr.

W32_CALL void W32_CALL _arp_debug_dump ( void  )

Debug-dump of configured gateways, route table and ARP cache.

Redone + moved here from pcdbug.c for encapsulation reasons GvB 2002-09

Definition at line 1907 of file pcarp.c.

References arp_entry::expiry, gate_entry::gate_ip, arp_entry::hardware, arp_entry::ip, MAC_address(), gate_entry::mask, set_timeout(), sin_mask, and gate_entry::subnet.

int W32_CALL _arp_gateways_get ( const struct gate_entry **  rc)

Return start of gate_list[].

Gateway functions.

Note
Only 'gate_top' gateways are valid in result.

Definition at line 94 of file pcarp.c.

BOOL W32_CALL _arp_handler ( const arp_Header ah,
BOOL  brdcast 
)
void W32_CALL _arp_init ( void  )

Setup config-table parse function and add background ARP deamon.

'Internal' interface to pctcp.c & others.

Definition at line 1547 of file pcarp.c.

References arp_daemon().

BOOL W32_CALL _arp_lookup ( DWORD  ip,
eth_address *  eth 
)

Lookup MAC-address of 'ip'.

Return values
TRUEMAC for 'ip' is known.

Definition at line 1186 of file pcarp.c.

References arp_check_common(), is_on_LAN(), LAN_lookup(), and route_lookup().

BOOL W32_CALL _arp_lookup_fixed ( DWORD  ip,
eth_address *  eth 
)

Lookup fixed MAC-address of 'ip'.

Return values
TRUESupplied 'ip' has a fixed MAC entry.

Definition at line 1219 of file pcarp.c.

References arp_check_common(), and is_on_LAN().

BOOL W32_CALL _arp_lookup_pending ( DWORD  ip)

An ARP-lookup timeout check.

Return values
TRUEThe lookup is currently underway ("pending").
FALSEThe IP has either been resolved (_arp_lookup() == TRUE), or the lookup has timed out, i.e. the host is unreachable.

Definition at line 1206 of file pcarp.c.

References is_on_LAN(), LAN_lookup_pending(), and route_lookup_pending().

BOOL W32_CALL _arp_register ( DWORD  use_this_gateway_ip,
DWORD  for_this_host_ip 
)

Register a new host as gateway.

ICMP redirection.

Called on ICMP-redirects.

Redirects are sent from a gateway/router, telling us that a different gateway is better suited to connect to the specified host than the one we were using.

Definition at line 843 of file pcarp.c.

References is_on_LAN(), LAN_lookup(), LAN_start_lookup(), and sin_mask.

BOOL W32_CALL _arp_reply ( const void e_dst,
DWORD  src_ip,
DWORD  dst_ip 
)

Send unicast/broadcast ARP reply.

'src_ip' and 'dst_ip' on host order.

Definition at line 489 of file pcarp.c.

References _eth_addr, _eth_brdcast, _eth_formatpacket(), _eth_get_hwtype(), arp_send(), arp_Header::hwAddrLen, MAC_address(), and arp_Header::protoAddrLen.

BOOL W32_CALL _arp_resolve ( DWORD  ip,
eth_address *  eth 
)

The blocking lookup function visible to higher functions.

Blocking ARP resolve functions.

Definition at line 1279 of file pcarp.c.

References _watt_cbroke, arp_check_common(), arp_daemon(), is_on_LAN(), LAN_lookup(), LAN_lookup_pending(), LAN_start_lookup(), route_lookup(), route_lookup_pending(), route_start_lookup(), and tcp_tick().

BOOL W32_CALL _arp_start_lookup ( DWORD  ip)

"PUBLIC" ARP/ROUTE FUNCTIONS.

New non-blocking functions, GvB 2002-09.

These are the main functions visible from outside the module. Most of them simply check if the destination host IP is on the LAN or needs to be routed over a gateway, and then relay on to the apropriate LAN_...() or route_...() function. Initialise an ARP lookup. This is called by 'higher' routines to start an ARP request. This function is non-blocking, i.e. returns 'immediately'.

Definition at line 1169 of file pcarp.c.

References arp_check_common(), is_on_LAN(), LAN_start_lookup(), and route_start_lookup().

static void arp_check_timeouts ( BOOL  check_dynamic_entries)
static

Check ARP entries for timeout.

This function runs through the 'pending' section of the ARP table and checks for entries that have either expired or require a re-send. The 'dynamic' entries are checked for expiry if requested.

Definition at line 665 of file pcarp.c.

References arp_move_entry(), arp_send_request(), chk_timeout(), arp_entry::expiry, arp_entry::flags, arp_entry::ip, arp_entry::retransmit_to, and set_timeout().

static void arp_daemon ( void  )
static

ARP background daemon.

Calls timeout-checks / retransmitters.

We don't check the dynamic entries for a timeout on every call, once a second is plenty enough.

Definition at line 1494 of file pcarp.c.

References _arp_check_gateways(), arp_check_timeouts(), chk_timeout(), route_check_timeouts(), and set_timeout().

static int MS_CDECL compare ( const struct gate_entry a,
const struct gate_entry b 
)
static

Compare two entries in gateway-list.

Sort such that entries with the longest netmasks (32-mask_len()) are put first. Thus the default route (mask 0.0.0.0) is matched last in route_destin().

From http://www.linuxfoundation.org/collaborate/workgroups/networking/networkoverview: LPM (Longest Prefix Match) is the lookup algorithm. The route with the longest netmask is the one chosen. Netmask 0, which is the shortest netmask, is for the default gateway.

Definition at line 217 of file pcarp.c.

References gate_entry::mask.

static BOOL LAN_start_lookup ( DWORD  ip)
static

Start a host lookup on the LAN.

This is called by 'higher' routines to start an ARP request. This function is non-blocking, i.e. returns 'immediately'.

Definition at line 540 of file pcarp.c.

References arp_send_request(), arp_entry::expiry, arp_entry::flags, arp_entry::ip, arp_entry::retransmit_to, and set_timeout().

static void route_check_timeouts ( BOOL  check_dynamic_entries)
static

Periodic route checker.

Run through all pending entries and check if an attempt to reach a gateway was successfull, or has timed-out. If the attempt timed-out, we try the next fitting gateway. If there are no more fitting gateways to try, the connection has failed.

Definition at line 1046 of file pcarp.c.

References gate_entry::gate_ip, is_on_LAN(), LAN_lookup(), LAN_lookup_pending(), LAN_start_lookup(), gate_entry::mask, and gate_entry::subnet.

Variable Documentation

WORD icmp_id = 0
static

Send a ping (with TTL=1) to a gateway.

Ref. RFC-1122.

Definition at line 105 of file pcarp.c.