📄 dhcpcstate1.c
字号:
if (pLeaseData->prevState == INIT) { /* Clear any previous parameter settings. */ if (pLeaseData->dhcpcParam != NULL) { clean_param (pLeaseData->dhcpcParam); free (pLeaseData->dhcpcParam); pLeaseData->dhcpcParam = NULL; } pLeaseData->prevState = WAIT_OFFER; } if (pEvent->type == DHCP_TIMEOUT) {#ifdef DHCPC_DEBUG logMsg ("dhcp: timed out in WAIT_OFFER state.\n", 0, 0, 0, 0, 0, 0);#endif /* Handle timeout - no DHCP offers received yet. */ retry = pLeaseData->numRetry; timer = pLeaseData->timeout; retry++; if (retry == DISCOVER_RETRANS) /* Retransmission limit reached. */ {#ifdef DHCPC_DEBUG logMsg ("No lease offers received by client.\n", 0, 0, 0, 0, 0, 0);#endif /* * No response to new DHCP message format, which can be ignored * by older DHCP servers as too short. Try the earlier format, * which padded the options field to a minimum length. If * successful, all subsequent messages will add that padding. */ if (!pLeaseData->oldFlag) { pLeaseData->oldFlag = TRUE; timer = FIRSTTIMER / 2; /* Counteract later doubling. */ retry = 0; } else return (ERROR); } /* Try to retransmit appropriate DHCP message for current state. */ /* Recreate DHCP_DISCOVER message using the same transaction ID. */ length = make_discover (pLeaseData, FALSE); gen_retransmit (pLeaseData, length); if (timer < MAXTIMER) { /* Double retransmission delay with each attempt. (RFC 1541). */ timer *= 2; } /* Set retransmission timer to randomized exponential backoff. */ wdStart (pLeaseData->timer, sysClkRateGet() * SLEEP_RANDOM (timer), (FUNCPTR)retrans_wait_offer, (int)pLeaseData); pLeaseData->timeout = timer; pLeaseData->numRetry = retry; } else { /* * Process DHCP message stored in receive buffer by monitor task. * The 4-byte alignment of the IP header needed by Sun BSP's is * guaranteed by the Berkeley Packet Filter during input. */ pMsgData = pLeaseData->msgBuffer; align_msg (&dhcpcMsgIn, pMsgData); /* Examine type of message. Accept DHCP offers or BOOTP replies. */ option = (char *)pickup_opt (dhcpcMsgIn.dhcp, DHCPLEN (dhcpcMsgIn.udp), _DHCP_MSGTYPE_TAG); if (option == NULL) { /* * Message type not found - check message length. Ignore * untyped DHCP messages, but accept (shorter) BOOTP replies. * This test might still treat (illegal) untyped DHCP messages * like BOOTP messages if they are short enough, but that case * can't be avoided. */ if (DHCPLEN (dhcpcMsgIn.udp) > DFLTBOOTPLEN) return (OK); } else { if (*OPTBODY (option) != DHCPOFFER) return (OK); } /* Allocate memory for configuration parameters. */ pParams = (struct dhcp_param *)calloc (1, sizeof (struct dhcp_param)); if (pParams == NULL) return (OK); /* Fill in host requirements defaults. */ dhcpcDefaultsSet (pParams); /* Check offered parameters. Save in lease structure if acceptable. */ if (dhcp_msgtoparam (dhcpcMsgIn.dhcp, DHCPLEN (dhcpcMsgIn.udp), pParams) == OK) { /* * Accept static BOOTP address or verify that the * address offered by the DHCP server is non-zero * and check offered lease length against minimum value. */ if ( (pParams->msgtype == DHCP_BOOTP || pParams->server_id.s_addr != 0) && pParams->lease_duration >= dhcpcMinLease) { /* * Initial offer accepted. Set lease data * to execute next routine and start timer. */ pParams->lease_origin = pLeaseData->initEpoch; pLeaseData->dhcpcParam = pParams; pLeaseData->currState = SELECTING; /* * Reset timer from retransmission * interval to limit for collecting replies. */ wdCancel (pLeaseData->timer); wdStart (pLeaseData->timer, sysClkRateGet() * pLeaseData->leaseReqSpec.waitsecs, (FUNCPTR)alarm_selecting, (int)pLeaseData); } else { /* * Offer is insufficient. Remove stored parameters * and set lease data to repeat current routine. */ pLeaseData->currState = WAIT_OFFER; clean_param (pParams); free (pParams); } } else { /* * Conversion unsuccessful - remove stored parameters * and set lease data to repeat current routine. */ pLeaseData->currState = WAIT_OFFER; clean_param (pParams); free (pParams); } } return (OK); }/********************************************************************************* selecting - Second offering state of client finite state machine** This routine continues the second state of the finite state machine. * It compares additional offers received from DHCP servers to the current* offer, and selects the offer which provides the longest lease. When the * time limit specified by the DHCPC_OFFER_TIMEOUT definition passes,* processing of the selected DHCP offer will continue with the requesting() * routine. If no DHCP offers were received, the BOOTP reply selected by the * wait_offer() routine will be used by the lease.** .IP* This routine is invoked by the event handler of the client monitor task,* and should only be called internally. Any user requests generated by* incorrect calls or delayed responses to the dhcpcBind() and dhcpcVerify() * routines are ignored.** RETURNS: OK (processing complete), DHCPC_DONE (remove lease),* DHCPC_MORE (continue), or ERROR.** ERRNO: N/A** NOMANUAL*/int selecting ( EVENT_DATA * pEvent /* pointer to event descriptor */ ) { struct dhcp_param *pParams = NULL; struct sockaddr_in dest; struct ifnet * pIf; int status; LEASE_DATA * pLeaseData = NULL; char * pMsgData; int length; char * option;#ifdef DHCPC_DEBUG logMsg ("dhcp: Entered SELECTING state.\n", 0, 0, 0, 0, 0, 0);#endif /* * Use the cookie to access the lease-specific data structures. For now, * just typecast the cookie. This translation could be replaced with a more * sophisticated lookup at some point. */ pLeaseData = (LEASE_DATA *)pEvent->leaseId; /* * The DHCP_USER_RELEASE event occurs in response to the dhcpcRelease() * or dhcpcShutdown() call. Remove all data structures for this lease. */ if (pEvent->type == DHCP_USER_RELEASE) { dhcpcLeaseCleanup (pLeaseData); return (DHCPC_DONE); } /* Ignore bind and verify user events, which are meaningless here. */ if (pEvent->source == DHCP_USER_EVENT) return (OK); if (pEvent->type == DHCP_TIMEOUT) { /* Collection time ended - parameters structure holds chosen offer. */ if (pLeaseData->dhcpcParam->msgtype == DHCP_BOOTP) { /* * No DHCP request is needed if a BOOTP reply is chosen. Set * lease data to process future events with the bound() routine. */ pLeaseData->leaseType = DHCP_BOOTP; pLeaseData->prevState = SELECTING; pLeaseData->currState = BOUND; status = use_parameter (pLeaseData->dhcpcParam, pLeaseData); semTake (dhcpcMutexSem, WAIT_FOREVER); if (status != 0) {#ifdef DHCPC_DEBUG logMsg ("Error configuring network. Shutting down.\n", 0, 0, 0, 0, 0, 0);#endif pLeaseData->leaseGood = FALSE; } else { pLeaseData->leaseGood = TRUE; } semGive (dhcpcMutexSem); return (OK); } /* * A DHCP offer was selected. Build and send the DHCP request using * the original transaction ID from the discover message. */ length = make_request (pLeaseData, REQUESTING, FALSE); if (length < 0) {#ifdef DHCPC_DEBUG logMsg ("Error making DHCP request. Entering INIT state.\n", 0, 0, 0, 0, 0, 0);#endif pLeaseData->prevState = SELECTING; pLeaseData->currState = INIT; return (DHCPC_MORE); } dhcpcMsgOut.udp->uh_sum = 0; dhcpcMsgOut.udp->uh_sum = udp_cksum (&spudph, (char *)dhcpcMsgOut.udp, ntohs (spudph.ulen)); bzero ( (char *)&dest, sizeof (struct sockaddr_in)); dest.sin_len = sizeof (struct sockaddr_in); dest.sin_family = AF_INET; dest.sin_addr.s_addr = dhcpcMsgOut.ip->ip_dst.s_addr; pIf = pLeaseData->ifData.iface; if (dhcpSend (pIf, &dest, sbuf.buf, length, TRUE) == ERROR) {#ifdef DHCPC_DEBUG logMsg ("Can't send DHCPREQUEST\n", 0, 0, 0, 0, 0, 0);#endif pLeaseData->prevState = SELECTING; pLeaseData->currState = INIT; return (DHCPC_MORE); } /* * DHCP request sent. Set lease data to execute next state and * start the retransmission timer. */ pLeaseData->prevState = SELECTING; pLeaseData->currState = REQUESTING; pLeaseData->timeout = FIRSTTIMER; pLeaseData->numRetry = 0; wdStart (pLeaseData->timer, sysClkRateGet() * SLEEP_RANDOM (pLeaseData->timeout), (FUNCPTR)retrans_requesting, (int)pLeaseData); } else { /* * Process DHCP message stored in receive buffer by monitor task. * The 4-byte alignment of the IP header needed by Sun BSP's is * guaranteed by the Berkeley Packet Filter during input. */ pMsgData = pLeaseData->msgBuffer; align_msg (&dhcpcMsgIn, pMsgData); /* Examine type of message. Only accept DHCP offers. */ option = (char *)pickup_opt (dhcpcMsgIn.dhcp, DHCPLEN (dhcpcMsgIn.udp), _DHCP_MSGTYPE_TAG); if (option == NULL) { /* * Message type not found - discard untyped DHCP messages, and * any BOOTP replies. */ return (OK); } else { if (*OPTBODY (option) != DHCPOFFER) return (OK); } /* Allocate memory for configuration parameters. */ pParams = (struct dhcp_param *)calloc (1, sizeof (struct dhcp_param)); if (pParams == NULL) return (OK); /* Fill in host requirements defaults. */ dhcpcDefaultsSet (pParams); /* Switch to offered parameters if they provide a longer DHCP lease. * * First, check that we can decode the parameters. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -