// Project:DHCP Demo // Compiler: BC++ 3.1 // Compile mode: large // Project: client.c // ..\Lib\8000L.Lib // ..\Lib\TCPIPL.Lib [Date on 2003/08/06 or later] // ..\Lib\DHCP1001.Lib // The Dynamic Host Configuration Protocol (DHCP) provides configuration parameters to Internet hosts. // DHCP consists of two components: a protocol for delivering host-specific configuration // parameters from a DHCP server to a host and a mechanism for allocation of // network addresses to hosts. // DHCP is built on a client-server model, where designated DHCP server hosts allocate // network addresses and deliver configuration parameters to dynamically configured hosts. // Throughout the remainder of this document, the term "server" refers to a host providing // initialization /* Client Server | | Begins initialization | | | |----------------------------->| | DHCPDISCOVER | | Determines | configuration | | |<---------------------------- | | DHCPOFFER | | | | | |----------------------------->| | DHCPREQUEST | | | | Commits configuration | | |<-----------------------------| | DHCPACK | | | Initialization complete | | | |----------------------------->| | DHCPRELEASE | | Discards lease | | | Timeline diagram of messages exchanged between DHCP client and servers when allocating a new network address */ // Follow the steps below to get the information from DHCP server. // Step1. // Declare the #define DHCP_CONFIG 1 or #define DHCP_CONFIG 2 // Step2. // Declare the 3 variables for saving the new address. // extern unsigned char NewIP[]; // extern unsigned char NewMask[]; // extern unsigned char NewGateway[]; // Step3 // Call the function,DHCP_Get // DHCP_Get(1,100); //1 means the index into network configuration table (The availabe number is 1~32) // //100 means the desired length of lease time (Unit: sec) // Note: // 1.You must to add #define DHCP_CONFIG 1 or 2 in the program to // choose set of configuration options for server to return to client: // #define DHCP_CONFIG 1: Request IP address only from DHCP server // #define DHCP_CONFIG 2: Request IP address, Subnet Mask, Router from DNS server // // 2.Maximum delay in DHCP client retry loop // #define MAXDELAY 1000 in DHCP.h (Defalut) // // 3.Add the fucntion,DHCP_Get after Ninit() and Portinit(). // // 3.Refer to TCPIP.H for more details about error code. // Hardware: 8000E(8x3x) series // Refer DHCP.h // Refer to RFC1533 and RFC2131 for more details about DHCP options // http://www.faqs.org/rfcs/rfc1533.html // http://www.apps.ietf.org/rfc/rfc2131.html // for more details. // [08/Aug/2003] by Sean #include #include "..\lib\8000e.h" #include "..\lib\tcpip32.h" #include "..\lib\dhcp.h" #define DHCP_CONFIG 2 extern unsigned char NewIP[]; extern unsigned char NewMask[]; extern unsigned char NewGateway[]; void main(int argc,char *argv[]) { int i,iRet,ierror; struct ip host_ip; GetSavedIp(&host_ip); for(i=0;i<4;i++) host_ip.addr.c[i]=0; if (SUCCESS==lhip("I8000E", &host_ip)) { if(Ninit()<0) { Print("Ninit failure\r\n"); exit(0); } if(Portinit("*")<0) { Print("Portinit failure\r\n"); exit(0); } GetIp(NewIP); Print("Current IP:%d:%d:%d:%d\r\n",NewIP[0],NewIP[1],NewIP[2],NewIP[3]); GetMask(NewMask); Print("Current SubMask IP:%d:%d:%d:%d\r\n",NewMask[0],NewMask[1],NewMask[2],NewMask[3]); GetGateway(NewGateway); Print("Current Gateway IP:%d:%d:%d:%d\r\n",NewGateway[0],NewGateway[1],NewGateway[2],NewGateway[3]); iRet=DHCP_Get(1,100); if(iRet) { Print("DHCP return:%d\r\n",iRet); SetIp(NewIP); SetMask(NewMask); SetGateway(NewGateway); iRet=DHCP_Release(1); if(!iRet) { Print("DHCP release successfully!!\r\n\r\n"); GetIp(NewIP); Print("New IP:%d:%d:%d:%d\r\n",NewIP[0],NewIP[1],NewIP[2],NewIP[3]); GetMask(NewMask); Print("New SubMask IP:%d:%d:%d:%d\r\n",NewMask[0],NewMask[1],NewMask[2],NewMask[3]); GetGateway(NewGateway); Print("New Gateway IP:%d:%d:%d:%d\r\n",NewGateway[0],NewGateway[1],NewGateway[2],NewGateway[3]); } else Print("DHCP release return:%d\r\n",iRet); } else Print("DHCP return:%d\r\n",iRet); } else Print("Can not find I8000E\r\n"); }