📄 dhcpclib.c
字号:
* possible DHCP values in the address string. If the target address indicated * by <pAddrString> contains both lease duration and lease origin values, it was* assigned by a DHCP server and the routine sets the status flag accessed by * <pDhcpStatus> so that the lease will be verified or renewed. This routine is * called when initializing the network during system startup and will not * function correctly if used in any other context. Any lease information * attached to the address string is removed when this routine is executed.** RETURNS: OK if setup completes, or ERROR if the address string is invalid.** ERRNO: N/A** NOMANUAL*/STATUS dhcpcLeaseGet ( char * pAddrString, /* client address string from bootline */ BOOL * pDhcpStatus /* DHCP lease values found? */ ) { char * pDelim; char * pOffset; int result; dhcpcBootLease.lease_duration = 0; /* Defaults to expired. */ dhcpcBootLease.lease_origin = 0; /* Defaults to invalid value. */ /* Read DHCP lease values and remove from address string. */ result = bootLeaseExtract (pAddrString, &dhcpcBootLease.lease_duration, &dhcpcBootLease.lease_origin); if (result == 2) *pDhcpStatus = TRUE; /* DHCP lease values read successfully. */ else { *pDhcpStatus = FALSE; /* Valid DHCP lease values not present. */ if (result == 0) { /* * The ":" field separator for either the netmask or duration was * not found, so no DHCP lease values are present. */ return (OK); } if (result == 1) { /* * Only the lease duration field was present. * The DHCP lease values have been removed. */ return (ERROR); } /* * Parsing one of the lease values failed. Remove * any DHCP lease data from the address string. */ /* Find delimiter for netmask. */ pOffset = index (pAddrString, ':'); /* * Start at the delimiter for DHCP lease values. The netmask separator * is actually always present at this point, but check for it anyway. */ if (pOffset != NULL) { pDelim = pOffset + 1; /* * Find the lease duration tag. This field separator is also * guaranteed to be present, or the extract routine would have * returned 0. */ pOffset = index (pDelim, ':'); /* Remove the DHCP lease values from string. */ if (pOffset != NULL) *pOffset = EOS; } return (ERROR); } return (OK); }/********************************************************************************* dhcpcConfigSet - set system configuration according to active DHCP lease** This routine verifies or renews any DHCP lease established during the system * boot. If a DHCP lease was established, the dhcpcInit() call in this routine* will setup the necessary data structures and create a cookie identifying the * boot lease. The cookie is stored in the pDhcpcBootCookie global variable to* provide users with access to the boot-time lease. This routine is called when* initializing the network from usrNetInit() in usrNetwork.c and will not * function correctly if executed in any other context.** RETURNS: OK if configuration completed, or ERROR otherwise.** ERRNO: N/A** NOMANUAL*/STATUS dhcpcConfigSet ( BOOT_PARAMS * pParams, /* structure for parsed bootline */ int * pNetmask, /* specified subnet mask */ int dhcpStatus /* DHCP or BOOTP used? */ ) { char netDev [BOOT_DEV_LEN + 1]; struct dhcp_param bootParams; BOOL backplaneBoot; u_long result; struct ifnet * pIf; char tempString[30]; LEASE_DATA * pLeaseData; backplaneBoot = FALSE; if ( (strncmp (pParams->bootDev, "bp", 2) == 0) || (strncmp (pParams->bootDev, "sm", 2) == 0)) backplaneBoot = TRUE; /* Check for serial interfaces - DHCP requires broadcast-capable devices. */ if ( (strncmp (pParams->bootDev, "ppp", 3) == 0) || (strncmp (pParams->bootDev, "sl", 2) == 0)) { if (dhcpStatus == FALSE) /* No DHCP lease is in use. */ return (OK); else return (ERROR); /* Can't verify current lease. */ } if (dhcpStatus == FALSE) { /* * BOOTP reply was selected during system startup or the IP address * was assigned manually. Lease renewal is not needed. */ return (OK); } /* Verify DHCP lease obtained during boot. */ bzero ( (char *)&bootParams, sizeof (struct dhcp_param)); sprintf (netDev, "%s%d", pParams->bootDev, pParams->unitNum); /* Fill in client address obtained from bootline. */ if (backplaneBoot) result = inet_addr (pParams->bad); else result = inet_addr (pParams->ead); if (result == ERROR) { if (backplaneBoot) printf("Invalid target address \"%s\"\n", pParams->bad); else printf("Invalid target address \"%s\"\n", pParams->ead); return (ERROR); } dhcpcBootLease.yiaddr.s_addr = result; pIf = ifunit (netDev); if (pIf == NULL) { printf ("Invalid device \"%s\"\n", netDev); return (ERROR); } /* * Initialize required variables and get the lease identifier. * The resulting lease will always apply the address information * to the specified network interface. */ pDhcpcBootCookie = dhcpcInit (pIf, TRUE); if (pDhcpcBootCookie == NULL) { printf ("Error initializing DHCP boot lease.\n"); return (ERROR); } /* * 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 *)pDhcpcBootCookie; /* Set the lease descriptor to identify the boot lease. */ pLeaseData->leaseType = DHCP_AUTOMATIC; dhcpcOptionSet (pDhcpcBootCookie, _DHCP_LEASE_TIME_TAG, dhcpcBootLease.lease_duration, 0, NULL); /* Execute the bind call synchronously to verify the boot lease. */ if (dhcpcBind (pDhcpcBootCookie, TRUE) != OK) { printf ("Can't renew DHCP boot lease.\n"); pDhcpcBootCookie = NULL; return (ERROR); } bootParams.file = pParams->bootFile; bootParams.subnet_mask = (struct in_addr *)pNetmask; if (dhcpcParamsGet (pDhcpcBootCookie, &bootParams) == ERROR) { printf ("Can't get network data from DHCP boot lease.\n"); pDhcpcBootCookie = NULL; return (ERROR); } /* Fill in host address. */ inet_ntoa_b (bootParams.siaddr, pParams->had); /* Fill in target address so later reboots will detect DHCP leases. */ if (*pNetmask == 0) sprintf(tempString, "::%lx:%lx", bootParams.lease_duration, bootParams.lease_origin); else sprintf (tempString, ":%x:%lx:%lx", ntohl (*pNetmask), bootParams.lease_duration, bootParams.lease_origin); if (backplaneBoot) { inet_ntoa_b (bootParams.yiaddr, pParams->bad); strcat (pParams->bad, tempString); } else { inet_ntoa_b (bootParams.yiaddr, pParams->ead); strcat (pParams->ead, tempString); } return (OK); }/********************************************************************************* dhcpcInit - assign network interface and setup lease request** This routine creates the data structures used to obtain a set of parameters * with DHCP and must be called before each attempt at establishing a DHCP * lease, but after the dhcpcLibInit() routine has initialized the global data* structures. The <pIf> argument indicates the network device which will be * used for transmission and reception of DHCP messages during the lifetime of * the lease. If the <autoConfig> parameter is set to TRUE, any address * information obtained will automatically be applied to that interface. The * specified interface must access a device capable of sending broadcast * messages. Currently, only Ethernet devices and the shared-memory network* drivers are supported.** The routine also uses the <autoConfig> parameter to select the default option * request list for a lease. If set to FALSE, no specific lease options are * requested since any configuration parameters obtained are not intended for * the underlying network device. In that case, any specific options required * may be added to the request list at any time before the corresponding * dhcpcBind() call. If <autoConfig> is TRUE, this routine sets the * configuration parameters to request the minimal address information * (subnet mask and broadcast address) necessary for reconfiguring the network * device specified by <pIf>.** The internal lease identifier returned by this routine must be used in* subsequent calls to the DHCP client library.** NOTE* This routine is called automatically during system startup if the DHCP * client was used to obtain the VxWorks boot parameters. The resulting * lease will always reconfigure the network boot device. Therefore, any* further calls to this routine which specify the network boot device for * use in obtaining additional DHCP leases must set <autoConfig> to FALSE.* Otherwise, that device will be unable to maintain a stable configuration.* The global variable pDhcpcBootCookie provides access to the configuration * parameters for any DHCP lease created during system startup.* * RETURNS: Lease handle for later use, or NULL if lease setup fails.** ERRNO: S_dhcpcLib_NOT_INITIALIZED, S_dhcpcLib_NO_DEVICE, S_dhcpcLib_BAD_OPTION,* S_dhcpcLib_MAX_LEASES_REACHED, S_dhcpcLib_MEM_ERROR** SEE ALSO* dhcpcOptionSet(), dhcpcEventHookAdd()*/void * dhcpcInit ( struct ifnet * pIf, /* network device used by client */ BOOL autoConfig /* reconfigure network device? */ ) { void * pCookie; LEASE_DATA * pLeaseData; struct dhcp_reqspec * pReqSpec; int offset; if (!dhcpcInitialized) { errno = S_dhcpcLib_NOT_INITIALIZED; return (NULL); } if (pIf == NULL) { errno = S_dhcpcLib_NO_DEVICE; return (NULL); } if (autoConfig != TRUE && autoConfig != FALSE) { errno = S_dhcpcLib_BAD_OPTION; return (NULL); } /* Find an unused entry in the array of lease-specific variables. */ for (offset = 0; offset < dhcpcMaxLeases; offset++) if (dhcpcLeaseList [offset] == NULL) break; if (offset == dhcpcMaxLeases) { errno = S_dhcpcLib_MAX_LEASES_REACHED; return (NULL); } /* Allocate all lease-specific variables. */ pLeaseData = (LEASE_DATA *)calloc (1, sizeof (LEASE_DATA)); if (pLeaseData == NULL) { errno = S_dhcpcLib_MEM_ERROR; return (NULL); } pLeaseData->initFlag = FALSE; pLeaseData->autoConfig = autoConfig; /* * Use the current data storage hook routine (if any) * throughout the lifetime of this lease. */ pLeaseData->cacheHookRtn = dhcpcCacheHookRtn; /* * For now, use the lease data pointer as the unique lease identifier. * This could be changed later to shield the internal structures from * the user, but it allows fast data retrieval. */ pCookie = (void *)pLeaseData; pReqSpec = &pLeaseData->leaseReqSpec; bzero ( (char *)pReqSpec, sizeof (struct dhcp_reqspec)); bzero ( (char *)&pLeaseData->ifData, sizeof (struct if_info)); /* Initialize WIDE project global containing network device data. */ sprintf (pLeaseData->ifData.name, "%s", pIf->if_name); pLeaseData->ifData.unit = pIf->if_unit; pLeaseData->ifData.iface = pIf;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -