/*********************************************************************
*
* Dynamic Host Configuration Protocol (DHCP) Client
* Module for Microchip TCP/IP Stack
* -Provides automatic IP address, subnet mask, gateway address,
* DNS server address, and other configuration parameters on DHCP
* enabled networks.
* -Reference: RFC 2131, 2132
*
*********************************************************************
* FileName: DHCP.c
* Dependencies: UDP
* Processor: PIC18, PIC24F, PIC24H, dsPIC30F, dsPIC33F, PIC32
* Compiler: Microchip C32 v1.00 or higher
* Microchip C30 v3.01 or higher
* Microchip C18 v3.13 or higher
* HI-TECH PICC-18 STD 9.50PL3 or higher
* Company: Microchip Technology, Inc.
*
* Software License Agreement
*
* Copyright (C) 2002-2008 Microchip Technology Inc. All rights
* reserved.
*
* Microchip licenses to you the right to use, modify, copy, and
* distribute:
* (i) the Software when embedded on a Microchip microcontroller or
* digital signal controller product ("Device") which is
* integrated into Licensee's product; or
* (ii) ONLY the Software driver source files ENC28J60.c and
* ENC28J60.h ported to a non-Microchip device used in
* conjunction with a Microchip ethernet controller for the
* sole purpose of interfacing with the ethernet controller.
*
* You should refer to the license agreement accompanying this
* Software for additional information regarding your rights and
* obligations.
*
* THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT
* WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
* LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
* MICROCHIP BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF
* PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS
* BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE
* THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER
* SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT
* (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE.
*
*
* Author Date Comment
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Nilesh Rajbharti 3/21/01 Original (Rev 1.0)
* Nilesh Rajbharti 7/10/02 Explicitly initialized tempIPAddress
* (Rev 2.11)
* Nilesh Rajbharti 5/16/03 Increased DHCP_TIMEOUT to 2 seconds.
* Nilesh Rajbharti 5/16/03 Fixed SM_DHCP_BROADCAST logic
* where UDPPut was called before setting
* active socket.
* Robert Sloan 5/29/03 Improved DHCP State machine to handle
* NAK and renew existing IP address.
* Nilesh Rajbharti 8/15/03 Modified _DHCPRecieve() to check for
* chaddr field before accpting the packet.
* Fixed DHCPTask() where it would not
* reply to first OFFER.
* Nilesh Rajbharti 3/1/04 Used tickDiff in DHCPTask() "bind"
* state to adjust for irregular TICK_SECOND
* Without this logic, actual lease time count
* down may be incorrect.
* Howard Schlunder 5/11/06 Fixed tickDiff usage, reducing
* accumulated timing error. Fixed DHCP
* state machine requesting IP 0.0.0.0
* after lease expiration.
* Howard Schlunder 6/01/06 Added DHCPFlags.bits.bOfferReceived flag to
* allow operation on networks with multiple
* DHCP servers offering multiple addresses
* Howard Schlunder 8/01/06 Added DNS server option to DHCP request,
* untested Host Name option to DHCP request
* Howard Schlunder 1/09/06 Fixed a DHCP renewal not renewing lease time bug
* Howard Schlunder 3/16/07 Rewrote DHCP state machine
********************************************************************/
#define __DHCP_C
#include "TCPIP Stack/TCPIP.h"
#if defined(STACK_USE_DHCP_CLIENT)
// Defines how long to wait before a DHCP request times out
#define DHCP_TIMEOUT (TICK)(2*TICK_SECOND)
// DHCP state machien variable
SM_DHCP smDHCPState = SM_DHCP_GET_SOCKET;
// DHCP Client Flags to use
DHCP_CLIENT_FLAGS DHCPFlags = {{0x00}};
// Socket for DHCP client to use
static UDP_SOCKET DHCPSocket = INVALID_UDP_SOCKET;
BYTE DHCPBindCount = 0; // Counts how many times DHCP has been bound
static DWORD_VAL DHCPServerID; // DHCP Server ID cache
static DWORD_VAL DHCPLeaseTime; // DHCP Lease Time
static IP_ADDR tempIPAddress; // Temporary IP address to use when no DHCP lease
static IP_ADDR tempGateway; // Temporary gateway to use when no DHCP lease
static IP_ADDR tempMask; // Temporary mask to use when no DHCP lease
#if defined(STACK_USE_DNS)
static IP_ADDR tempDNS; // Temporary primary DNS server
static IP_ADDR tempDNS2; // Temporary secondary DNS server
#endif
// Indicates which DHCP values are currently valid
static union
{
struct
{
char IPAddress:1; // Leased IP address is valid
char Gateway:1; // Gateway address is valid
char Mask:1; // Subnet mask is valid
char DNS:1; // Primary DNS is valid
char DNS2:1; // Secondary DNS is valid
char HostName:1; // Host name is valid (not implemented)
} bits;
BYTE Val;
} ValidValues;
static BYTE _DHCPReceive(void);
static void _DHCPSend(BYTE messageType, BOOL bRenewing);
/*****************************************************************************
Function:
void DHCPReset(void)
Summary:
Resets the DHCP Client.
Description:
Resets the DHCP Client, giving up any current lease, knowledge of
DHCP servers, etc.
Precondition:
None
Parameters:
None
Returns:
None
***************************************************************************/
void DHCPReset(void)
{
// Do nothing if DHCP is disabled
if(smDHCPState == SM_DHCP_DISABLED)
return;
if(DHCPSocket != INVALID_UDP_SOCKET)
smDHCPState = SM_DHCP_SEND_DISCOVERY;
else
smDHCPState = SM_DHCP_GET_SOCKET;
DHCPBindCount = 0;
DHCPFlags.bits.bIsBound = FALSE;
}
/*****************************************************************************
Function:
void DHCPDisable(void)
Summary:
Disables the DHCP Client.
Description:
Disables the DHCP client by sending the state machine to
"SM_DHCP_DISABLED". If the board was previously configured by DHCP, the
configuration will continue to be used but the module will no longer
preform any renewals.
Precondition:
None
Parameters:
None
Returns:
None
Remarks:
Since the board continues using its old configuration, it is possible
that the lease may expire and the DHCP server provide the IP to another
client. The application should replace the current IP address and other
configuration with static information following a call to this function.
***************************************************************************/
void DHCPDisable(void)
{
if(DHCPSocket != INVALID_UDP_SOCKET)
{
UDPClose(DHCPSocket);
DHCPSocket = INVALID_UDP_SOCKET;
}
smDHCPState = SM_DHCP_DISABLED;
AppConfig.Flags.bIsDHCPEnabled = 0;
}
/*****************************************************************************
Function:
void DHCPEnable(void)
Summary:
Enables the DHCP Client.
Description:
Enables the DHCP client if it is disabled. If it is already enabled,
nothing is done.
Precondition:
None
Parameters:
None
Returns:
None
*************
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
DHCP.rar (2个子文件)
DHCPs.c 16KB
DHCP.c 28KB
共 2 条
- 1
资源评论
weixin_42653672
- 粉丝: 93
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功