📄 dhcpclib.c
字号:
break; if (offset == dhcpcMaxLeases) { errno = S_dhcpcLib_BAD_COOKIE; return (ERROR); } if (!dhcpcInitialized || !pLeaseData->initFlag) { errno = S_dhcpcLib_NOT_INITIALIZED; return (ERROR); } if (syncFlag != TRUE && syncFlag != FALSE) { errno = S_dhcpcLib_BAD_OPTION; return (ERROR); } /* Examine type of DHCP lease to determine action required. */ if (pLeaseData->leaseType == DHCP_BOOTP) { return (OK); /* BOOTP leases are always valid. */ } /* * Allocate space for any options in outgoing messages * and fill in the options field. */ pOptList = pLeaseData->leaseReqSpec.pOptList; if (pOptList != NULL) { pOptions = malloc (pOptList->optlen); if (pOptions == NULL) { errno = S_dhcpcLib_BAD_OPTION; return (ERROR); } pLeaseData->leaseReqSpec.optlen = pOptList->optlen; dhcpcOptFieldCreate (pOptList, pOptions); free (pOptList); pLeaseData->leaseReqSpec.pOptList = NULL; pLeaseData->leaseReqSpec.pOptions = pOptions; } /* Wait for results if the startup lease is being renewed. */ if (pLeaseData->leaseType == DHCP_AUTOMATIC) pLeaseData->waitFlag = TRUE; else pLeaseData->waitFlag = syncFlag; if (pLeaseData->leaseType == DHCP_MANUAL && pLeaseData->leaseGood) { /* If redundant bind is requested, change to verification. */ result = dhcpcVerify (pCookie); return (result); } else /* Obtain initial lease or renew startup lease. */ { /* * Add a filter for incoming DHCP packets and assign the selected * interface to the BPF device. */ result = ioctl (pLeaseData->ifData.bpfDev, BIOCSETF, (int)&dhcpread); if (result != 0) { errno = S_dhcpcLib_BAD_DEVICE; return (ERROR); } bzero ( (char *)&ifr, sizeof (struct ifreq)); sprintf (ifr.ifr_name, "%s%d", pLeaseData->ifData.iface->if_name, pLeaseData->ifData.iface->if_unit); result = ioctl (pLeaseData->ifData.bpfDev, BIOCSETIF, (int)&ifr); if (result != 0) { errno = S_dhcpcLib_BAD_DEVICE; return (ERROR); } dhcp_client (pCookie, TRUE); /* Perform bind process. */ /* Check results of synchronous lease attempt. */ if (pLeaseData->waitFlag) { pLeaseData->waitFlag = FALSE; /* Disable further signals. */ semTake (dhcpcMutexSem, WAIT_FOREVER); if (pLeaseData->leaseGood) result = OK; else result = ERROR; semGive (dhcpcMutexSem); } else result = OK; } /* * If waitFlag was TRUE, the negotiation has completed. Otherwise, it * has begun, and the installed event hook routine will be called at the * appropriate time. */ return (result); }/********************************************************************************* dhcpcVerify - renew an established lease** This routine schedules the lease identified by the <pCookie> parameter* for immediate renewal according to the process described in RFC 1541.* If the renewal is unsuccessful, the lease negotiation process restarts.* The routine is valid as long as the lease is currently active. The* routine is also called automatically in response to a dhcpcBind() call* for an existing lease.** NOTE* This routine is only intended for active leases obtained with the* dhcpcBind() routine. It should not be used for parameters resulting* from the dhcpcInformGet() routine.** NOTE* This routine will disable the underlying network interface if the * verification fails and automatic configuration was requested. This may* occur without warning if no event hook is installed.** RETURNS: OK if verification scheduled, or ERROR otherwise.** ERRNO: S_dhcpcLib_BAD_COOKIE, S_dhcpcLib_NOT_INITIALIZED, S_dhcpcLib_NOT_BOUND* */STATUS dhcpcVerify ( void * pCookie /* identifier returned by dhcpcInit() */ ) { int offset; LEASE_DATA * pLeaseData = NULL; STATUS result = OK; /* * 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 *)pCookie; for (offset = 0; offset < dhcpcMaxLeases; offset++) if (dhcpcLeaseList [offset] != NULL && dhcpcLeaseList [offset] == pLeaseData) break; if (offset == dhcpcMaxLeases) { errno = S_dhcpcLib_BAD_COOKIE; return (ERROR); } if (!dhcpcInitialized || !pLeaseData->initFlag) { errno = S_dhcpcLib_NOT_INITIALIZED; return (ERROR); } semTake (dhcpcMutexSem, WAIT_FOREVER); if (!pLeaseData->leaseGood) result = ERROR; semGive (dhcpcMutexSem); if (result == ERROR) { errno = S_dhcpcLib_NOT_BOUND; return (ERROR); } /* Construct and send a verification request to the client monitor task. */ result = dhcpcEventAdd (DHCP_USER_EVENT, DHCP_USER_VERIFY, pLeaseData, FALSE); return (result); }/********************************************************************************* dhcpcRelease - relinquish specified lease** This routine schedules the lease identified by the <pCookie> parameter* for immediate release, regardless of time remaining, and removes all* the associated data structures. After the release completes, a new* call to dhcpcInit() is required before attempting another lease.** NOTE* This routine will disable the underlying network interface if automatic * configuration was requested. This may occur without warning if no event * hook is installed.** RETURNS: OK if release scheduled, or ERROR otherwise.** ERRNO: S_dhcpcLib_BAD_COOKIE, S_dhcpcLib_NOT_INITIALIZED**/STATUS dhcpcRelease ( void * pCookie /* identifier returned by dhcpcInit() */ ) { int offset; LEASE_DATA * pLeaseData = NULL; STATUS result = OK; /* * 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 *)pCookie; for (offset = 0; offset < dhcpcMaxLeases; offset++) if (dhcpcLeaseList [offset] != NULL && dhcpcLeaseList [offset] == pLeaseData) break; if (offset == dhcpcMaxLeases) { errno = S_dhcpcLib_BAD_COOKIE; return (ERROR); } if (!dhcpcInitialized || !pLeaseData->initFlag) { errno = S_dhcpcLib_NOT_INITIALIZED; 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 *)pCookie; /* Construct and send a release request to the client monitor task. */ result = dhcpcEventAdd (DHCP_USER_EVENT, DHCP_USER_RELEASE, pLeaseData, FALSE); /* Wait for lease cleanup to complete before returning */ if (result == OK) while (dhcpcLeaseList [offset] != NULL) taskDelay (1); return (result); }/********************************************************************************* dhcpcInformGet - obtain additional configuration parameters with DHCP** This routine uses DHCP to retrieve additional configuration parameters for* a client with the externally configured network address given by the* <pAddrString> parameter. It sends an INFORM message and waits for a reply* following the process described in RFC 2131. The <pCookie> argument contains* the return value of an earlier dhcpcInit() call and is used to access the* resulting configuration. Unlike the dhcpcBind() call, this routine does not* establish a lease with a server.** The <syncFlag> parameter specifies whether the message exchange started by * this routine will execute synchronously or asynchronously. An asynchronous * execution will return after sending the initial message, but a synchronous * execution will only return once the process completes.** When a server responds with an acknowledgement message, any event hook* provided will be called to process the configuration parameters. The hook* is also called if the message exchange fails. The results of an asynchronous* execution are not available unless an event hook is installed.** NOTE* This routine is designed as an alternative to the dhcpcBind() routine.* It should not be used for any dhcpcInit() identifer corresponding to an* active or pending lease.** RETURNS: OK if routine completes, or ERROR otherwise.** ERRNO: S_dhcpcLib_BAD_COOKIE, S_dhcpcLib_NOT_INITIALIZED, S_dhcpcLib_BAD_OPTION* */STATUS dhcpcInformGet ( void * pCookie, /* identifier returned by dhcpcInit() */ char * pAddrString, /* known address assigned to client */ BOOL syncFlag /* synchronous or asynchronous execution? */ ) { int offset; STATUS result = OK; LEASE_DATA * pLeaseData = NULL; struct dhcpcOpts * pOptList; char * pOptions; /* * 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 *)pCookie; for (offset = 0; offset < dhcpcMaxLeases; offset++) if (dhcpcLeaseList [offset] != NULL && dhcpcLeaseList [offset] == pLeaseData) break; if (offset == dhcpcMaxLeases) { errno = S_dhcpcLib_BAD_COOKIE; return (ERROR); } if (!dhcpcInitialized || !pLeaseData->initFlag) { errno = S_dhcpcLib_NOT_INITIALIZED; return (ERROR); } if (syncFlag != TRUE && syncFlag != FALSE) { errno = S_dhcpcLib_BAD_OPTION; return (ERROR); } /* Set the requested IP address field to the externally assigned value. */ pLeaseData->leaseReqSpec.ipaddr.s_addr = inet_addr (pAddrString); if (pLeaseData->leaseReqSpec.ipaddr.s_addr == -1) { errno = S_dhcpcLib_BAD_OPTION; return (ERROR); } /* * Allocate space for any options in outgoing messages * and fill in the options field. */ pOptList = pLeaseData->leaseReqSpec.pOptList; if (pOptList != NULL) { pOptions = malloc (pOptList->optlen); if (pOptions == NULL) { errno = S_dhcpcLib_BAD_OPTION; return (ERROR); } pLeaseData->leaseReqSpec.optlen = pOptList->optlen; dhcpcOptFieldCreate (pOptList, pOptions); free (pOptList); pLeaseData->leaseReqSpec.pOptList = NULL; pLeaseData->leaseReqSpec.pOptions = pOptions; } /* Start the message exchange to get additional parameters.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -