Main Page | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

SRC/pcdhcp.c File Reference


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.

Go to the source code of this file.

Functions

void DHCP_state_INIT (int event)
 DHCP state machine: state INIT.
void DHCP_state_BOUND (int event)
 DHCP state machine: state BOUND.
void DHCP_state_RENEWING (int event)
 DHCP state machine: state RENEWING.
void DHCP_state_REQUESTING (int event)
 DHCP state machine: state REQUESTING.
void DHCP_state_SELECTING (int event)
 DHCP state machine: state SELECTING.
void DHCP_state_REBINDING (int event)
 DHCP state machine: state REBINDING.
void dhcp_fsm (void)
 DHCP state machine: the event driver.
void dhcp_options_add (const BYTE *opt, unsigned max)
 Add options from a DHCP_OPT_OVERLOAD tag to 'extra_options' list.
void dhcp_set_timers (void)
 Set all timers required by the DHCP state machine.
void change_ip_addr (void)
 Change my_ip_addr of all tcp/udp sockets.
BYTEput_request_list (BYTE *opt, int filled)
 Append options from 'request_list' to 'opt'.
int write_config (void)
 Write the transient DHCP configuration, either to file or via user-defined function.
void erase_config (void)
 Erase the transient configuration.
const char * state_name (void)
 Return name of current DHCP state function.
const char * period (DWORD sec)
 Return nicely formatted string for a time-period.
BYTEmake_boot_header (void)
 Format a BOOTP header.
BYTEput_hardware_opt (BYTE *opt)
 Fill in the hardware type/len in Client ID option tag.
int DHCP_discover (void)
 Send a DHCP discover message.
int DHCP_request (BOOL renew)
 Send a DHCP request message.
int DHCP_release_decline (int msg_type, const char *msg)
 Send a DHCP release or decline message.
BOOL DHCP_arp_check (DWORD my_ip)
 Send an ARP reply to announce our new IP-address.
int DHCP_offer (const struct dhcp *in)
 Parse DHCP offer reply.
int DHCP_is_ack (void)
 Return TRUE if DHCP message is an ACK.
int DHCP_is_nack (void)
 Return TRUE if DHCP message is a NACK.
void DHCP_release (BOOL force)
 Possibly send a DHCP release.
DWORD DHCP_get_server (void)
 Return current DHCP server address.
sock_typedhcp_open (const char *msg, BOOL use_broadcast)
 Allocate and open a UDP socket for DHCP message exchange.
void arp_add_server (void)
 Add MAC address of DHCP sever to our ARP cache.
void DHCP_state_REBOOTING (int event)
 DHCP state machine: Combined BOOT and BOOTING state.
int DHCP_do_boot (void)
 Our first time DHCP handler.
int set_request_list (char *options)
 Parse a list of DHCP-request options from config-file.
int set_user_class (const char *value)
 Parse user-class as specified in config-file.
int set_vend_class (const char *value)
 Set vendor class option.
void DHCP_cfg_hook (const char *name, const char *value)
 Parser for DHCP configuration.
void DHCP_exit (void)
 Free allocated memory.
void DHCP_init (void)
 Initialises the DHCP config-parser.
void set_my_ip (const char *value)
 Functions called by config-file parser while reading C:.TMP.
void set_netmask (const char *value)
void set_gateway (const char *value)
void set_nameserv (const char *value)
void set_server (const char *value)
void set_domain (const char *value)
void set_lease (const char *value)
void set_renew (const char *value)
void set_rebind (const char *value)
BOOL eval_timers (void)
 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.
const char * get_config_file (void)
void std_erase_config (void)
int std_read_config (void)
int std_write_config (void)
 Write the transient DHCP configuration to file.
void usr_erase_config (void)
int usr_read_config (void)
int usr_write_config (void)
int DHCP_read_config (void)
 Called from watt_sock_init() after "\c WATTCP.CFG" has been parsed.
WattDHCPConfigFunc 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
WattDHCPConfigFunc config_func = NULL
DWORD exchange_id
DWORD router
DWORD nameserver
DWORD dhcp_server = 0
DWORD dhcp_renewal = 0
DWORD dhcp_rebind = 0
DWORD dhcp_iplease = 0
DWORD suggest_lease = 0
DWORD old_ip_addr = 0
BOOL bcast_flag = TRUE
BOOL got_offer = FALSE
BOOL configured = FALSE
BOOL arp_check_ip = FALSE
BOOL cfg_read = FALSE
BOOL cfg_saved = FALSE
int dhcp_timeout = 10
int max_retries = 3
int discover_loops = 0
dhcp dhcp_in dhcp_out
void(* DHCP_state )(int)=NULL
time_t renewal_timeout
time_t rebind_timeout
time_t lease_timeout
DWORD send_timeout
BOOL trace_on = FALSE
char config_file [MAX_VALUELEN+1] = ""
sock_typesock = NULL
const BYTE default_request_list []
DHCP_list request_list
DHCP_list extra_options = { NULL, 0 }
DHCP_list user_class = { NULL, 0 }
DHCP_list vend_class = { (BYTE*)"Watt-32", 7 }
void(* prev_hook )(const char *, const char *)=NULL
time_t cfg_dhcp_iplease
time_t cfg_dhcp_renewal
time_t cfg_dhcp_rebind
const struct config_table transient_cfg []
 Open and parse Wattcp transient configuration.


Function Documentation

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 417 of file pcdhcp.c.

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

void 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 1339 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 1054 of file pcdhcp.c.

References addwattcpd(), 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().

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(), BYTE, defaultdomain, dhcp::dh_file, dhcp::dh_opt, dhcp::dh_sname, dhcp_options_add(), DWORD, hostname, period(), setdomainname(), sin_mask, StrLcpy(), and tcp_keep_intvl.

int 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 1776 of file pcdhcp.c.

References dhcp_open(), dhcp_set_timers(), eval_timers(), and sock_type::udp.

void 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 705 of file pcdhcp.c.

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

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 379 of file pcdhcp.c.

References BYTE, dhcp::dh_opt, DWORD, make_boot_header(), and set_timeout().

void DHCP_state_BOUND int  event  )  [static]
 

DHCP state machine: state BOUND.

  • Awaits T1-TIMEOUT and enter RENEWING state.
Definition at line 795 of file pcdhcp.c.

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

void DHCP_state_INIT int  event  )  [static]
 

DHCP state machine: state INIT.

  • Awaits SEND-TIMEOUT, send discover and enter SELECTING state.
Definition at line 957 of file pcdhcp.c.

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

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 865 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().

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 890 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().

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 814 of file pcdhcp.c.

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

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 926 of file pcdhcp.c.

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

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:
    FALSE if lease times expired.
    TRUE otherwise.
Definition at line 1461 of file pcdhcp.c.

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

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 1195 of file pcdhcp.c.

References BYTE.

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 1261 of file pcdhcp.c.

References BYTE, and dhcp::dh_opt.

int set_vend_class const char *  value  )  [static]
 

Set vendor class option.

Todo:
Support setting vendor class
Definition at line 1316 of file pcdhcp.c.

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 1581 of file pcdhcp.c.

References _default_ttl, _inet_ntoa(), DWORD, hostname, my_ip_addr, sin_mask, and tcp_keep_intvl.


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 145 of file pcdhcp.c.

struct DHCP_list request_list [static]
 

Initial value:

{ (BYTE*) &default_request_list, sizeof (default_request_list) }
Definition at line 170 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 } }
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 1514 of file pcdhcp.c.


Generated on Wed Aug 4 08:55:57 2010 for Watt-32 tcp/ip by doxygen 1.3.8