📄 dhcpccommonlib.c
字号:
/******************************************************************************** dhcpcDefaultsSet - assign host requirements defaults for client** This routine fills the client's parameters structure with the default* values specified in the Host Requirements Documents (RFC's 1122 and 1123),* the Path MTU Discovery description (RFC 1191), or the Router Discovery* specification (RFC 1256). This data is assigned before processing a message * received from a DHCP server, so that it can override the defaults.** RETURNS: N/A** ERRNO: N/A** NOMANUAL*/void dhcpcDefaultsSet ( struct dhcp_param * pNewParam ) { /* Default IP layer parameters, per host. */ pNewParam->ip_forward = FALSE; pNewParam->nonlocal_srcroute = FALSE; pNewParam->max_dgram_size = _HRD_MAX_DGRAM; pNewParam->default_ip_ttl = _HRD_IP_TTL; /* Default IP layer parameters, per interface. */ pNewParam->intf_mtu = _HRD_MTU; pNewParam->all_subnet_local = FALSE; pNewParam->mask_discover = FALSE; pNewParam->mask_supplier = FALSE; pNewParam->router_discover = TRUE; pNewParam->router_solicit.s_addr = _HRD_ROUTER; /* Default link layer parameters, per interface. */ pNewParam->trailer = FALSE; pNewParam->arp_cache_timeout = _HRD_ARP_TIME; pNewParam->ether_encap = FALSE; /* Default link layer parameters, per host. */ pNewParam->default_tcp_ttl = _HRD_TCP_TTL; pNewParam->keepalive_inter = _HRD_TCP_TIME; pNewParam->keepalive_garba = FALSE; }/******************************************************************************** dhcpcEventAdd - send event notification to monitor task** This routine adds event descriptors to the event queue for later handling * by the DHCP client monitor task. ** If the <source> parameter is set to DHCP_USER_EVENT, the routine was called * by one of the following API routines: dhcpcBind(), dhcpcRelease(), * dhcpcVerify(), or dhcpcShutdown(). The <type> parameter indicates the * corresponding action: DHCP_USER_BIND to initiate the lease negotiation * process, DHCP_USER_VERIFY to verify an active lease, DHCP_USER_RELEASE to * relinquish an active lease, and DHCP_USER_SHUTDOWN to release all active * leases and disable the DHCP client library. ** If the <source> parameter is set to DHCP_AUTO_EVENT, the routine was called* in response to a timeout in the DHCP client state machine and the* <type> parameter is set to DHCP_TIMEOUT.** The <pLeaseId> parameter identifies the associated lease for timeout events* and the first three user events.** The <intFlag> parameter indicates if this routine is executing at interrupt * level. If it is FALSE, then interrupts must be locked out to prevent* write conflicts to the event ring buffer between user requests and watchdog * timers.** RETURNS: OK if event added successfully, or ERROR otherwise.** ERRNO: N/A** NOMANUAL*/ STATUS dhcpcEventAdd ( int source, /* automatic event or user request */ int type, /* event identifier */ void * pLeaseId, /* internal lease identifier */ BOOL intFlag /* executing at interrupt level? */ ) { EVENT_DATA newEvent; int status; int key = 0; newEvent.source = source; newEvent.type = type; newEvent.leaseId = pLeaseId; /* * Add the event to the monitor task's event list. * Disable interrupts if necessary. */ if (!intFlag) key = intLock (); status = rngBufPut (dhcpcEventRing, (char *)&newEvent, sizeof (newEvent)); if (!intFlag) intUnlock (key); if (status != sizeof (newEvent)) return (ERROR); /* Signal the monitor task of the event arrival. */ status = semGive (dhcpcEventSem); if (status == ERROR) return (ERROR); return (OK); }/******************************************************************************** dhcpcInputHook - generic packet filter for DHCP messages** This routine extracts DHCP server responses from incoming ethernet traffic. * It is called by the network interface driver when a new input frame comes in * from the network. It is attached to the network driver with etherHook * routines.** RETURNS: TRUE if the ethernet frame is handled by this routine. No further* processing by the network driver is needed.* FALSE if the frame is not a server response. The ethernet frame is* then handled by the network driver.** ERRNO: N/A** NOMANUAL*/ BOOL dhcpcInputHook ( struct ifnet * pIf, /* interface where packet was received */ FAST char * pInput, /* contents of received packet */ FAST int length /* length of received packet */ ) { int retVal; /* Return values */ int key; EVENT_DATA newEvent; void * pLeaseId; /* Adjust message size (ignores trailers added by some drivers). */ if (length > DHCP_MSG_SIZE) length = DHCP_MSG_SIZE; /* * These routines access the dhcpcIfMsg and dhcpif globals. Those * variables must be replaced with interface-specific equivalents. */ /* Copy messages received at client port to interface-specific buffer. */ retVal = dhcpcDestCheck (pInput, length); if (retVal != OK) return (FALSE); /* * Validate message checksums and lengths. Verify "op" field, cookie * and transaction identifier. This routine will accept BOOTP or DHCP * responses. (The tests also alter the received message - must return * TRUE to network driver after this point). */ retVal = dhcpcBodyCheck (); if (retVal == OK) { /* DHCP message found - check for known transaction ID. */ pLeaseId = dhcpcXidVerify (dhcpcIfMsg.dhcp->xid); if (pLeaseId == NULL) return (TRUE); /* Signal WIDE project DHCP code when a valid message arrives. */ newEvent.source = DHCP_AUTO_EVENT; newEvent.type = DHCP_MSG_ARRIVED; newEvent.leaseId = pLeaseId; newEvent.length = length; /* Lock interrupts to prevent conflicts with timeout thread. */ key = intLock (); retVal = rngBufPut (dhcpcEventRing, (char *) &newEvent, sizeof(newEvent)); intUnlock (key); if (retVal == sizeof (newEvent)) { /* Ignore storage error - notifications with no message are OK. */ rngBufPut (dhcpcMsgRing, pInput, length); semGive (dhcpcEventSem); } } return (TRUE); }/******************************************************************************** dhcpArpInputHook - packet filter for ARP replies** This routine filters out ARP replies from incoming ethernet traffic. * It is called by the network interface driver when a new input frame comes * in from the network. It is attached to the network driver with etherHook * routines.** RETURNS: TRUE if the ethernet frame is handled by this routine. No further* processing by the network driver is needed.* FALSE if the frame is not an ARP reply. The ethernet frame is then* handled by the network driver.** ERRNO: N/A** NOMANUAL*/ BOOL dhcpArpInputHook ( struct ifnet * pIf, /* interface where packet was received */ FAST char * pInput, /* contents of received packet */ FAST int length /* length of received packet */ ) { FAST struct ether_header * pEtherHdr; /* pointer to ethernet header */ FAST struct ether_arp * pArp; /* pointer to ethernet ARP packet */ int bufLen; pEtherHdr = (struct ether_header *)pInput; if (length <= SIZEOF_ETHERHEADER) return (FALSE); if (ntohs (pEtherHdr->ether_type != ETHERTYPE_ARP)) return (FALSE); /* * Copy the input packet one byte at a time to accomodate board * specific memory access requirements. */ bufLen = length - SIZEOF_ETHERHEADER; /* Adjust message size (ignores trailers added by some drivers). */ if (bufLen > sizeof (struct ether_arp)) bufLen = sizeof (struct ether_arp); bcopyBytes ( (char *) ( (u_char *)pInput + SIZEOF_ETHERHEADER), inputBuffer, bufLen); pArp = (struct ether_arp *)&inputBuffer; /* * Check if the packet is a valid ARP reply. */ if (pArp->arp_op != ARPOP_REPLY) return FALSE; /* Check address of sender against (our) address stored in global. */ if (bcmp ( (char *)dhcpArpSpa, (char *)pArp->arp_spa, pArp->arp_pln) != 0) return FALSE; /* Valid reply: signal state machine to continue. */ semGive (dhcpcArpEventSem); /* Delete input hook so only one ARP message is accepted. */ if (muxDevExists (pIf->if_name, pIf->if_unit)) etherInputHookDelete (dhcpArpInputHook, pIf->if_name, pIf->if_unit); else etherInputHookDelete (dhcpArpInputHook, NULL, pIf->if_unit); return (TRUE); }/******************************************************************************** dhcpcParamsCopy - copy current network parameters.** This routine copies the network parameters to the caller-supplied structure* from the lease indicated by the <pLeaseData> parameter. It is called * internally by the dhcpcParamsGet() routine.** RETURNS: N/A** ERRNO: N/A** NOMANUAL** INTERNAL* The data structure accessed by this routine was defined by the original* public domain code used as a basis for the DHCP implementation. The * structure tag and field names are not required to follow the coding * standards (and they don't).*/void dhcpcParamsCopy ( LEASE_DATA * pLeaseData, struct dhcp_param * pParamList ) { int loop; int limit = 0; struct dhcp_param * pDhcpcParam; /* * The DHCP client parameters received from the server are stored in a * large structure which either contains the actual parameter value * or a pointer to the data. Because the parameters received are * determined by the server configuration and many parameters are * optional, any of the fields could use the default value determined * earlier. In most cases, the default value is an empty field (or * zero in the case of numeric values). However, some parameters contain * default settings specified by the host requirements documents which are * used if the server does not provide a value. The following code copies * any available parameters into the provided target structure. If * necessary, it checks for empty fields. The code is divided into logical * blocks according to the various data types used by the DHCP parameters. */ pDhcpcParam = pLeaseData->dhcpcParam; /* Process all string parameters. */ bcopy (pDhcpcParam->got_option, pParamList->got_option, GOTOPTSIZ); if (pParamList->sname != NULL && pDhcpcParam->sname != NULL) strcpy (pParamList->sname, pDhcpcParam->sname); if (pParamList->file != NULL && pDhcpcParam->file != NULL) strcpy (pParamList->file, pDhcpcParam->file); if (pParamList->hostname != NULL && pDhcpcParam->hostname != NULL) strcpy (pParamList->hostname, pDhcpcParam->hostname); if (pParamList->merit_dump != NULL && pDhcpcParam->merit_dump != NULL) strcpy (pParamList->merit_dump, pDhcpcParam->merit_dump); if (pParamList->dns_domain != NULL && pDhcpcParam->dns_domain != NULL) strcpy (pParamList->dns_domain, pDhcpcParam->dns_domain); if (pParamList->root_path != NULL && pDhcpcParam->root_path != NULL) strcpy (pParamList->root_path, pDhcpcParam->root_path); if (pParamList->extensions_path != NULL && pDhcpcParam->extensions_path != NULL) strcpy (pParamList->extensions_path, pDhcpcParam->extensions_path); if (pParamList->nis_domain != NULL && pDhcpcParam->nis_domain != NULL) strcpy (pParamList->nis_domain, pDhcpcParam->nis_domain); if (pParamList->nb_scope != NULL && pDhcpcParam->nb_scope != NULL) strcpy (pParamList->nb_scope, pDhcpcParam->nb_scope); if (pParamList->errmsg != NULL && pDhcpcParam->errmsg != NULL) strcpy (pParamList->errmsg, pDhcpcParam->errmsg); if (pParamList->nisp_domain != NULL && pDhcpcParam->nisp_domain != NULL) strcpy (pParamList->nisp_domain, pDhcpcParam->nisp_domain); /* Process all boolean parameters. */ pParamList->ip_forward = pDhcpcParam->ip_forward; pParamList->nonlocal_srcroute = pDhcpcParam->nonlocal_srcroute; pParamList->all_subnet_local = pDhcpcParam->all_subnet_local;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -