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

Go to the source code of this file.

Functions

static void DHCP_state_INIT (int event)
 DHCP state machine: state INIT. More...
 
static void DHCP_state_BOUND (int event)
 DHCP state machine: state BOUND. More...
 
static void DHCP_state_RENEWING (int event)
 DHCP state machine: state RENEWING. More...
 
static void DHCP_state_REQUESTING (int event)
 DHCP state machine: state REQUESTING. More...
 
static void DHCP_state_SELECTING (int event)
 DHCP state machine: state SELECTING. More...
 
static void DHCP_state_REBINDING (int event)
 DHCP state machine: state REBINDING. More...
 
static void dhcp_fsm (void)
 DHCP state machine: the event driver.
 
static void dhcp_options_add (const BYTE *opt, unsigned max)
 Add options from a DHCP_OPT_OVERLOAD tag to 'extra_options' list.
 
static void dhcp_set_timers (void)
 Set all timers required by the DHCP state machine.
 
static void change_ip_addr (void)
 Change my_ip_addr of all tcp/udp sockets.
 
static BYTE * put_request_list (BYTE *opt, int filled)
 Append options from 'request_list' to 'opt'.
 
static int write_config (void)
 Write the transient DHCP configuration, either to file or via user-defined function.
 
static void erase_config (void)
 Erase the transient configuration.
 
static const char * state_name (void)
 Return name of current DHCP state function.
 
static const char * period (DWORD sec)
 Return nicely formatted string for a time-period.
 
static BYTE * make_boot_header (void)
 Format a BOOTP header.
 
static BYTE * put_hardware_opt (BYTE *opt)
 Fill in the hardware type/len in Client ID option tag.
 
static int DHCP_discover (void)
 Send a DHCP discover message.
 
static int DHCP_request (BOOL renew)
 Send a DHCP request message.
 
static int DHCP_release_decline (int msg_type, const char *msg)
 Send a DHCP release or decline message. More...
 
static BOOL DHCP_arp_check (DWORD my_ip)
 Send an ARP reply to announce our new IP-address. More...
 
static int DHCP_offer (const struct dhcp *in)
 Parse DHCP offer reply. More...
 
static int DHCP_is_ack (void)
 Return TRUE if DHCP message is an ACK.
 
static int DHCP_is_nack (void)
 Return TRUE if DHCP message is a NACK.
 
void W32_CALL DHCP_release (BOOL force)
 Possibly send a DHCP release. More...
 
DWORD W32_CALL DHCP_get_server (void)
 Return current DHCP server address.
 
static sock_typedhcp_open (const char *msg, BOOL use_broadcast)
 Allocate and open a UDP socket for DHCP message exchange.
 
static void store_DHCP_server_MAC (void)
 Add MAC address of DHCP sever to our ARP cache.
 
static void DHCP_state_REBOOTING (int event)
 DHCP state machine: Combined BOOT and BOOTING state.
 
int DHCP_do_boot (void)
 Our first time DHCP handler. More...
 
static int set_request_list (char *options)
 Parse a list of DHCP-request options from config-file. More...
 
static int set_user_class (const char *value)
 Parse user-class as specified in config-file. More...
 
static int set_vend_class (const char *value)
 Set vendor class option. More...
 
static void (W32_CALL *prev_hook)(const char *
 
static void W32_CALL DHCP_cfg_hook (const char *name, const char *value)
 Parser for DHCP configuration. More...
 
static void DHCP_exit (void)
 Free allocated memory.
 
void DHCP_init (void)
 Initialises the DHCP config-parser.
 
static void set_my_ip (const char *value)
 Functions called by config-file parser while reading c:/W32DHCP.TMP.
 
static void set_netmask (const char *value)
 
static void set_gateway (const char *value)
 
static void set_nameserv (const char *value)
 
static void set_server (const char *value)
 
static void set_domain (const char *value)
 
static void set_lease (const char *value)
 
static void set_renew (const char *value)
 
static void set_rebind (const char *value)
 
static BOOL eval_timers (void)
 Check timers read from config-file and set state according to following rules: More...
 
static const char * get_config_file (void)
 
static void std_erase_config (void)
 
static int std_read_config (void)
 
static int std_write_config (void)
 Write the transient DHCP configuration to file. More...
 
static void usr_erase_config (void)
 
static int usr_read_config (void)
 
static int usr_write_config (void)
 
int W32_CALL DHCP_read_config (void)
 Called from watt_sock_init() after "\c WATTCP.CFG" has been parsed. More...
 
WattDHCPConfigFunc W32_CALL DHCP_set_config_func (WattDHCPConfigFunc fn)
 Sets up an application hook for doing DHCP operations (DHCP_config_op)
 

Variables

BOOL DHCP_did_gratuitous_arp = FALSE
 
static WattDHCPConfigFunc config_func = NULL
 
static DWORD exchange_id
 
static DWORD router
 
static DWORD nameserver
 
static DWORD dhcp_server = 0
 
static DWORD dhcp_renewal = 0
 
static DWORD dhcp_rebind = 0
 
static DWORD dhcp_iplease = 0
 
static DWORD suggest_lease = 0
 
static DWORD old_ip_addr = 0
 
static BOOL bcast_flag = TRUE
 
static BOOL got_offer = FALSE
 
static BOOL configured = FALSE
 
static BOOL arp_check_ip = FALSE
 
static BOOL cfg_read = FALSE
 
static BOOL cfg_saved = FALSE
 
static int dhcp_timeout = 10
 
static int max_retries = 3
 
static int discover_loops = 0
 
static struct dhcp dhcp_in dhcp_out
 
static void(* DHCP_state )(int) = NULL
 
static time_t renewal_timeout
 
static time_t rebind_timeout
 
static time_t lease_timeout
 
static DWORD send_timeout
 
static BOOL trace_on = FALSE
 
static char config_file [MAX_VALUELEN+1] = ""
 
static sock_typesock = NULL
 
static const BYTE default_request_list []
 
static struct DHCP_list request_list
 
static struct DHCP_list extra_options = { NULL, 0 }
 
static struct DHCP_list user_class = { NULL, 0 }
 
static struct DHCP_list vend_class = { (BYTE*)"Watt-32", 7 }
 
static const char *static time_t cfg_dhcp_iplease = NULL
 
static time_t cfg_dhcp_renewal
 
static time_t cfg_dhcp_rebind
 
static const struct config_table transient_cfg []
 Open and parse Wattcp transient configuration. More...
 

Detailed Description

Dynamic Host Configuration Protocol (RFC 1541/2131/2132). These extensions gets called if "MY_IP" is set to "DHCP" or if no WATTCP.CFG file is found.

Definition in file pcdhcp.c.

Function Documentation

static BOOL DHCP_arp_check ( DWORD  my_ip)
static

Send an ARP reply to announce our new IP-address.

This is in order to fill ARP cache of other hosts (gratuitous ARP).

Optionally (if arp_check_ip is TRUE), send an ARP request to check if our assigned IP-address is really vacant.

Definition at line 420 of file pcdhcp.c.

References _arp_check_own_ip(), _arp_reply(), _pktserial, MAC_address(), and sin_mask.

static void W32_CALL DHCP_cfg_hook ( const char *  name,
const char *  value 
)
static

Parser for DHCP configuration.

Matches all "\c DHCP.xx" values from WATTCP.CFG file and make appropriate actions.

Definition at line 1338 of file pcdhcp.c.

References ARG_ATOI, ARG_FUNC, ARG_RESOLVE, ARG_STRCPY, parse_config_table(), set_request_list(), set_user_class(), and set_vend_class().

int DHCP_do_boot ( void  )

Our first time DHCP handler.

Called if:

  • we don't have a WATTCP.CFG file
  • or we specified "MY_IP = DHCP".
  • or reading a previous W32DHCP.TMP file with transient config failed.
  • or lease times call for a renew or rebind. It doesn't hurt that it is blocking.

Definition at line 1056 of file pcdhcp.c.

References dhcp_fsm(), dhcp_open(), DHCP_state_BOUND(), DHCP_state_INIT(), DHCP_state_REBINDING(), DHCP_state_RENEWING(), erase_config(), my_ip_addr, set_timeout(), sin_mask, tcp_tick(), and write_config().

static int DHCP_offer ( const struct dhcp in)
static

Parse DHCP offer reply.

Note
May be called recursively.
Todo:
make a hook for SMB-lib

Definition at line 455 of file pcdhcp.c.

References _arp_add_gateway(), _arp_kill_gateways(), _default_ttl, _inet_ntoa(), _strlcpy(), defaultdomain, dhcp_options_add(), hostname, period(), setdomainname(), sin_mask, and tcp_keep_intvl.

int W32_CALL DHCP_read_config ( void  )

Called from watt_sock_init() after "\c WATTCP.CFG" has been parsed.

Open and parse transient configuration from 'config_file' or call user-defined function to read transient configuration.

Definition at line 1774 of file pcdhcp.c.

References dhcp_open(), dhcp_set_timers(), and eval_timers().

void W32_CALL DHCP_release ( BOOL  force)

Possibly send a DHCP release.

  • force If TRUE, release unconditionally.
    Note
    Send release if we are configured and remaining lease is below minimum lease (DHCP_MIN_LEASE = 10 sec).

Definition at line 707 of file pcdhcp.c.

References dhcp_fsm(), DHCP_release_decline(), and sock_close().

static int DHCP_release_decline ( int  msg_type,
const char *  msg 
)
static

Send a DHCP release or decline message.

  • msg_type DHCP_RELEASE or DHCP_DECLINE.
  • msg Optional message describing the cause.

Definition at line 382 of file pcdhcp.c.

References make_boot_header(), and set_timeout().

static void DHCP_state_BOUND ( int  event)
static

DHCP state machine: state BOUND.

  • Awaits T1-TIMEOUT and enter RENEWING state.

Definition at line 797 of file pcdhcp.c.

References DHCP_request(), DHCP_state_RENEWING(), and my_ip_addr.

static void DHCP_state_INIT ( int  event)
static

DHCP state machine: state INIT.

  • Awaits SEND-TIMEOUT, send discover and enter SELECTING state.

Definition at line 959 of file pcdhcp.c.

References DHCP_discover(), DHCP_state_SELECTING(), and set_timeout().

static void DHCP_state_REBINDING ( int  event)
static

DHCP state machine: state REBINDING.

  • Awaits ACK and enter BOUND state.
  • Awaits NAK and enter INIT state.

Definition at line 867 of file pcdhcp.c.

References change_ip_addr(), DHCP_offer(), dhcp_set_timers(), DHCP_state_BOUND(), DHCP_state_INIT(), my_ip_addr, Random(), and set_timeout().

static void DHCP_state_RENEWING ( int  event)
static

DHCP state machine: state RENEWING.

  • Awaits SEND-TIMEOUT and send request.
  • Awaits T2-TIMEOUT and send request, enter REBINDING state.
  • Awaits ACK and enter BOUND state.
  • Awaits NAK and enter INIT state.

Definition at line 892 of file pcdhcp.c.

References change_ip_addr(), DHCP_offer(), DHCP_request(), dhcp_set_timers(), DHCP_state_BOUND(), DHCP_state_INIT(), DHCP_state_REBINDING(), my_ip_addr, Random(), and set_timeout().

static void DHCP_state_REQUESTING ( int  event)
static

DHCP state machine: state REQUESTING.

  • Awaits SEND-TIMEOUT and send request.
  • Awaits ACK and do ARP check. Enter BOUND if successful.
  • Awaits NAK and enter INIT.

Definition at line 816 of file pcdhcp.c.

References DHCP_arp_check(), DHCP_offer(), DHCP_release_decline(), DHCP_request(), dhcp_set_timers(), DHCP_state_BOUND(), DHCP_state_INIT(), my_ip_addr, Random(), set_timeout(), and store_DHCP_server_MAC().

static void DHCP_state_SELECTING ( int  event)
static

DHCP state machine: state SELECTING.

  • Awaits OFFER, set SEND_TIMER and enter REQUESTING state.
  • Awaits SEND-TIMEOUT, enter INIT state and resend discover.

Definition at line 928 of file pcdhcp.c.

References DHCP_offer(), DHCP_state_INIT(), DHCP_state_REQUESTING(), my_ip_addr, Random(), and set_timeout().

static BOOL eval_timers ( void  )
static

Check timers read from config-file and set state according to following rules:

  • now < renewal => BOUND
  • now > renewal && now < rebind => RENEWING
  • now >= rebind && now < lease-end => REBINDING
  • now >= lease-end => INIT-REBOOT
Return values
FALSEif lease times expired.
TRUEotherwise.

Definition at line 1460 of file pcdhcp.c.

References ctime_r(), DHCP_state_BOUND(), and DHCP_state_RENEWING().

static int set_request_list ( char *  options)
static

Parse a list of DHCP-request options from config-file.

e.g DHCP_REQ_LIST = 1,23,24,28,36

Definition at line 1193 of file pcdhcp.c.

static int set_user_class ( const char *  value)
static

Parse user-class as specified in config-file.

Format is: "DHCP.USERCLASS = value1,value2,...". Build up user_class.data along the way.

Definition at line 1260 of file pcdhcp.c.

static int set_vend_class ( const char *  value)
static

Set vendor class option.

Todo:
Support setting vendor class

Definition at line 1315 of file pcdhcp.c.

static int std_write_config ( void  )
static

Write the transient DHCP configuration to file.

Append to file if found, else create the file.

Note
"ifdef USE_BUFFERED_IO" ignored here.

Definition at line 1583 of file pcdhcp.c.

References _inet_ntoa(), ctime_r(), my_ip_addr, sin_mask, tcp_keep_intvl, and wattcpVersion().

Variable Documentation

const BYTE default_request_list[]
static
Initial value:
= {
DHCP_OPT_SUBNET_MASK,
DHCP_OPT_ROUTERS_ON_SNET,
DHCP_OPT_DNS_SRV,
DHCP_OPT_COOKIE_SRV,
DHCP_OPT_LPR_SRV,
DHCP_OPT_HOST_NAME,
DHCP_OPT_DOMAIN_NAME,
DHCP_OPT_IP_DEFAULT_TTL,
DHCP_OPT_IF_MTU,
DHCP_OPT_ARP_CACHE_TIMEOUT,
DHCP_OPT_ETHERNET_ENCAPSULATION,
DHCP_OPT_TCP_DEFAULT_TTL,
#if defined(USE_BSD_API)
DHCP_OPT_LOG_SRV,
DHCP_OPT_NBIOS_NAME_SRV,
DHCP_OPT_NBIOS_NODE_TYPE,
DHCP_OPT_NBIOS_SCOPE,
#endif
#if defined(USE_TFTP)
DHCP_OPT_TFTP_SERVER,
DHCP_OPT_BOOT_FILENAME
#endif
}

Definition at line 148 of file pcdhcp.c.

struct DHCP_list request_list
static
Initial value:
= {
(BYTE*) &default_request_list,
sizeof (default_request_list)
}

Definition at line 173 of file pcdhcp.c.

const struct config_table transient_cfg[]
static
Initial value:
= {
{ "DHCP.LEASE", ARG_FUNC, (void*)set_lease },
{ "DHCP.RENEW", ARG_FUNC, (void*)set_renew },
{ "DHCP.REBIND", ARG_FUNC, (void*)set_rebind },
{ "DHCP.MY_IP", ARG_FUNC, (void*)set_my_ip },
{ "DHCP.NETMASK", ARG_FUNC, (void*)set_netmask },
{ "DHCP.GATEWAY", ARG_FUNC, (void*)set_gateway },
{ "DHCP.NAMESERV", ARG_FUNC, (void*)set_nameserv },
{ "DHCP.SERVER", ARG_FUNC, (void*)set_server },
{ "DHCP.DOMAIN", ARG_FUNC, (void*)set_domain },
{ "DHCP.HOSTNAME", ARG_STRCPY, (void*)hostname },
#if defined(USE_BSD_API)
{ "DHCP.LOGHOST", ARG_STRCPY, (void*)&syslog_host_name },
#endif
{ "DHCP.DEF_TTL", ARG_ATOI, (void*)&_default_ttl },
#if !defined(USE_UDP_ONLY)
{ "DHCP.TCP_KEEP", ARG_ATOI, (void*)&tcp_keep_intvl },
#endif
{ NULL, 0, NULL }
}
convert to int
Definition: tcp.h:424
call convertion function
Definition: tcp.h:434
int _default_ttl
Definition: ip4_out.c:54
copy string value
Definition: tcp.h:432
static void set_my_ip(const char *value)
Functions called by config-file parser while reading c:/W32DHCP.TMP.
Definition: pcdhcp.c:1390
char hostname[MAX_HOSTLEN+1]
Our configured hostname.
Definition: pctcp.c:59
unsigned tcp_keep_intvl
time between keepalive probes
Definition: pctcp.c:133

Open and parse Wattcp transient configuration.

i.e. values that must be known between consecutive runs of an Watt application. We should write the configured values to this file, and read back on next run to avoid doing DHCP boot/release for every program.

Definition at line 1516 of file pcdhcp.c.