📄 dhcpcstate2.c
字号:
/* dhcpcState2.c - DHCP client runtime state machine (lease maintenance) *//* Copyright 1984 - 2002 Wind River Systems, Inc. */#include "copyright_wrs.h"/*modification history--------------------01t,25apr02,wap correct timeout calculations for RENEWING & REBINDING states (SPR #69069)01s,23apr02,wap use dhcpTime() instead of time() (SPR #68900)01r,07mar02,wap Remember to re-enable the BPF filter and set a watchdog timeout when we enter the REBINDING state in bound() (SPR #73243)01q,06dec01,wap Fix reboot_verify() so that it properly constructs retransmit request (make_request() called with wrong type) (SPR #70938)01p,12oct01,rae merge from truestack (SPRs 67822, 27426, 30344)01o,24oct00,spm fixed modification history after tor3_x merge01n,23oct00,niq merged from version 01r of tor3_x branch (base version 01m)01m,04dec97,spm added code review modifications01l,06oct97,spm removed reference to deleted endDriver global01k,03sep97,spm added specified minimum timeouts to lease reacquisition01j,02sep97,spm removed excess IMPORT statement and extra event hook parameter01i,26aug97,spm major overhaul: reorganized code and changed user interface to support multiple leases at runtime01h,06aug97,spm removed parameters linked list to reduce memory required01g,15jul97,spm replaced floating point to prevent ss5 exception (SPR #8738); removed unneeded checkpoint messages01f,10jun97,spm isolated incoming messages in state machine from input hooks01e,02jun97,spm changed DHCP option tags to prevent name conflicts (SPR #8667) and updated man pages01d,06may97,spm changed memory access to align IP header on four byte boundary01c,18apr97,spm added conditional include DHCPC_DEBUG for displayed output01b,07apr97,spm added code to use Host Requirements defaults and cleanup memory on exit, rewrote documentation01a,29jan97,spm extracted from dhcpc.c to reduce object size*//*DESCRIPTIONThis library contains a portion of the finite state machine for the WIDE project DHCP client, modified for vxWorks compatibility.INTERNALThis module contains the functions used during and after the BOUND state. It was created to reduce the size of the boot ROM image so that the DHCP clientcan be used with targets like the MV147 which have limited ROM capacity. When executing at boot time, the DHCP client does not use any of the routines defined in this module.INCLUDE_FILES: dhcpcLib.h*//* * WIDE Project DHCP Implementation * Copyright (c) 1995 Akihiro Tominaga * Copyright (c) 1995 WIDE Project * All rights reserved. * * Permission to use, copy, modify and distribute this software and its * documentation is hereby granted, provided only with the following * conditions are satisfied: * * 1. Both the copyright notice and this permission notice appear in * all copies of the software, derivative works or modified versions, * and any portions thereof, and that both notices appear in * supporting documentation. * 2. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by WIDE Project and * its contributors. * 3. Neither the name of WIDE Project nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND WIDE * PROJECT DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES * WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. ALSO, THERE * IS NO WARRANTY IMPLIED OR OTHERWISE, NOR IS SUPPORT PROVIDED. * * Feedback of the results generated from any improvements or * extensions made to this software would be much appreciated. * Any such feedback should be sent to: * * Akihiro Tominaga * WIDE Project * Keio University, Endo 5322, Kanagawa, Japan * (E-mail: dhcp-dist@wide.ad.jp) * * WIDE project has the rights to redistribute these changes. *//* includes */#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <signal.h>#include <fcntl.h>#include <sys/types.h>#include <sys/socket.h>#include <sys/ioctl.h>#include <net/if.h>#include <netinet/in.h>#include <netinet/in_systm.h>#include <netinet/if_ether.h>#include <netinet/ip.h>#include <netinet/udp.h>#include <arpa/inet.h>#include "vxWorks.h"#include "ioLib.h" /* ioctl declaration */#include "rngLib.h"#include "wdLib.h"#include "time.h"#include "inetLib.h"#include "logLib.h"#include "taskLib.h"#include "sysLib.h"#include "vxLib.h"#include "netLib.h"#include "sys/ioctl.h"#include "net/bpf.h"#include "dhcp/dhcpcCommonLib.h"#include "dhcp/dhcpcStateLib.h"#include "dhcp/dhcpcInternal.h"/* defines *//* Retransmission delay is timer value plus/minus one second (RFC 1541). */#define SLEEP_RANDOM(timer) ( (timer - 1) + (rand () % 2) )#define REQUEST_RETRANS 4 /* Max number of retransmissions. (RFC 1541). *//* globals */IMPORT SEM_ID dhcpcMutexSem; /* Protects status indicator */IMPORT struct dhcpLeaseData dhcpBootLease; /* Boot lease time / address *//********************************************************************************* use_parameter - reset the network according to the parameters provided** If no event hook is present for the lease described by <pLeaseData>, this * routine sets the network address, broadcast address and subnet mask for * a network interface to the values chosen by the DHCP client and acknowledged * by the offering DHCP server. The configuration of the network interface is* also changed automatically for any lease established during system startup,* whether or not an event hook is present. This routine calls any installed * event notification hook to indicate that new lease parameters are available.** RETURNS: 0 if setup completed, or 1 if error occurs.** ERRNO: N/A* * NOMANUAL*/int use_parameter (struct dhcp_param *paramp, /* Current DHCP parameters */ LEASE_DATA * pLeaseData /* lease-specific status information */ ){ struct in_addr addr; struct in_addr mask; struct in_addr brdaddr; int status = 0; int length; char *bufp; void *pCookie; /* * For now, use the address of the lease-specific data structure as the * internal lease identifier. This could be replaced with a more * sophisticated mapping if necessary. */ pCookie = (void *) pLeaseData; if (pLeaseData->cacheHookRtn != NULL) {#ifdef DHCPC_DEBUG logMsg ("Saving lease data.\n", 0, 0, 0, 0, 0, 0);#endif#if BSD<44 length = ntohs (dhcpcMsgIn.udp->uh_ulen) + (dhcpcMsgIn.ip->ip_v_hl & 0xff) * WORD;#else length = ntohs (dhcpcMsgIn.udp->uh_ulen) + dhcpcMsgIn.ip->ip_hl * WORD;#endif bufp = (char *) dhcpcMsgIn.ip; (*pLeaseData->cacheHookRtn) (DHCP_CACHE_WRITE, &pLeaseData->dhcpcParam->lease_origin, &length, bufp); } if (pLeaseData->autoConfig || pLeaseData->leaseType == DHCP_AUTOMATIC) { /* * If automatic configuration was requested or the lease was * established during system startup, the client library will * reconfigure the transmit/receive interface to use the retrieved * addressing information. Fetch the values for the interface address, * subnet mask, and broadcast address, if available. */ bzero ((char *) &addr, sizeof (struct in_addr)); bzero ((char *) &mask, sizeof (struct in_addr)); bzero ((char *) &brdaddr, sizeof (struct in_addr)); addr.s_addr = paramp->yiaddr.s_addr; /* Set subnet mask, if available. */ if (paramp->subnet_mask != NULL) mask.s_addr = paramp->subnet_mask->s_addr; else mask.s_addr = 0; /* Set broadcast address, if available. */ if (paramp->brdcast_addr != NULL) brdaddr.s_addr = paramp->brdcast_addr->s_addr; else brdaddr.s_addr = 0; } /* * Set the transmit/receive interface to use the new parameters. */ if (pLeaseData->autoConfig || pLeaseData->leaseType == DHCP_AUTOMATIC) { /* Set new address info. Returns 1 if address unchanged. */ status = config_if (&pLeaseData->ifData, &addr, ((mask.s_addr != 0) ? &mask : NULL), ((brdaddr.s_addr != 0) ? &brdaddr : NULL)); if (status == 0) set_route (paramp); else if (status == -1) /* Error. */ return (1); /* Send an ARP reply to update the ARP cache on other hosts. */ arp_reply (¶mp->yiaddr, &pLeaseData->ifData); } /* * If an event notification hook is present, send an * indication that a new set of parameters is available. */ if (pLeaseData->eventHookRtn != NULL) (*pLeaseData->eventHookRtn) (DHCPC_LEASE_NEW, pCookie); return (0);}/********************************************************************************* release - relinquish a DHCP lease** This routine sends a message to the DHCP server relinquishing the active* lease contained in the <pEvent> event descriptor. If <shutdownFlag> is TRUE,* the routine cleans up all lease-specific data structures because the user* issued a dhcpcRelease() or dhcpcShutdown() call. Otherwise, the routine is* handling a fatal error in the state machine and the lease-specific data is* retained to allow retries of the dhcpcBind() or dhcpcInformGet() operation.** RETURNS: N/A** ERRNO: N/A** NOMANUAL*/void release (LEASE_DATA * pLeaseData, /* lease-specific status information */ BOOL shutdownFlag /* remove lease-specific data? */ ){ char errmsg[255]; struct dhcp_reqspec tmp_reqspec; int boundstat; bzero ((char *) &tmp_reqspec, sizeof (tmp_reqspec)); bzero ((char *) &errmsg, sizeof (errmsg)); semTake (dhcpcMutexSem, WAIT_FOREVER); /* Reset status indicator. */ if (pLeaseData->leaseGood) { boundstat = 1; pLeaseData->leaseGood = FALSE; } else /* Not bound - don't send release message. */ boundstat = 0; semGive (dhcpcMutexSem); if (boundstat) { switch (pLeaseData->currState) { case BOUND: /* fall-through */ case RENEWING: /* fall-through */ case REBINDING: /* fall-through */ case VERIFY: /* fall-through */ case VERIFYING: /* fall-through */ if (pLeaseData->dhcpcParam != NULL) { set_relinfo (&tmp_reqspec, pLeaseData, errmsg); dhcp_release (&tmp_reqspec, &pLeaseData->ifData, pLeaseData->oldFlag); /* * Disable the underlying network interface if * it used the (soon-to-be) relinquished lease. */ if (pLeaseData->autoConfig || pLeaseData->leaseType == DHCP_AUTOMATIC) down_if (&pLeaseData->ifData); if (pLeaseData->cacheHookRtn != NULL) { (*pLeaseData->cacheHookRtn) (DHCP_CACHE_ERASE, NULL, NULL, NULL); } clean_param (pLeaseData->dhcpcParam); free (pLeaseData->dhcpcParam); pLeaseData->dhcpcParam = NULL; } break; default: break; } } /* Remove lease information if appropriate. */ if (shutdownFlag) dhcpcLeaseCleanup (pLeaseData); return;}/********************************************************************************* alarm_bound - timeout during bound state** This routine sends a timeout notification to the client monitor task when * a lease timer expires. It is called at interrupt level by a watchdog timer.* The monitor task will eventually advance the lease from the BOUND state to* the RENEWING or the REBINDING state, depending on which interval elapsed.** RETURNS: N/A** ERRNO: N/A** NOMANUAL*/void alarm_bound (LEASE_DATA * pLeaseData /* lease-specific status information */ ){ /* * Ignore the timeout if a state transition occurred * before processing completed. */ if (pLeaseData->currState != BOUND) return; /* Construct and send a timeout message to the lease monitor task. */ dhcpcEventAdd (DHCP_AUTO_EVENT, DHCP_TIMEOUT, pLeaseData, TRUE); return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -