⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dhcp.c

📁 DHCP implementation on LPC22xx
💻 C
字号:
//!  The DHCP Stack implementation starts here. 
/*!
  This is the DHCP.c file where the main() function is. In this function the
  the peripherals are initialised and the TCP connection request is initiated.
*/
#include "string.h"
#include "tcpip.h"
#include "dhcp.h"
#include "udp.h"


static const unsigned char xid[4] = {0xad, 0xde, 0x12, 0x23};   /*!< static const unsigned char xid[4]. */	static const unsigned char magic_cookie[4] = {99, 130, 83, 99};	/*!< static const unsigned char magic_cookie[4]. */	

unsigned char client_ip[4],Your_client_ip[4]; /*!< unsigned char client_ip[4],Your_client_ip[4]. */	
unsigned char Next_server_ip[4],Client_MAC[6];/*!< unsigned char Next_server_ip[4],Client_MAC[6]. */	
unsigned char Subnet_mask[4],Server_id[4];	 /*!< unsigned char Subnet_mask[4],Server_id[4]. */
unsigned char dhcp_buff[350];				 /*!< unsigned char dhcp_buff[350].              */

extern unsigned short MyIP[];
extern unsigned char Received_DHCP_Offer_frame;
extern unsigned char Received_DHCP_Ack_frame;
/**
******************************************************************************
  Module Name:	add_msg_type
  Module ID:
  Purpose:		To add the "type of DHCP message" in DHCP frame i.e. DHCP Discover,DHCP requesr,DHCP Offer 
  Author:		Saud
  Date Written:	
********************************************************************************/
//! The add_msg_type() function will add the "type of DHCP message" in DHCP frame 
//  i.e. DHCP Discover,DHCP requesr,DHCP Offer 
/*!
\function name add_msg_type()
\param unsigned char *optptr, unsigned char type.
\return static unsigned char *.
*/
static unsigned char *
add_msg_type(unsigned char *optptr, unsigned char type){  *optptr++ = DHCP_OPTION_MSG_TYPE;  *optptr++ = 1;  *optptr++ = type;  return optptr;}
/**
******************************************************************************
  Module Name:	add_server_id
  Module ID:
  Purpose:		To add the "Server id" in DHCP frame 
  Author:		Saud
  Date Written:	
********************************************************************************/
//! The add_server_id() function will add the To add the "Server id" in DHCP frame 
/*!
\function name add_server_id()
\param unsigned char unsigned char *optptr.
\return static unsigned char *.
*/
static unsigned char *
add_server_id(unsigned char *optptr)
{
  unsigned char i=0;
  *optptr++ = DHCP_OPTION_SERVER_ID;
  *optptr++ = 4;
  for(i=0;i<4;i++)
  *optptr++ = Server_id[i];
  
  return optptr;
}
/**
******************************************************************************
  Module Name:	add_req_ipadd
  Module ID:
  Purpose:		To add ip address request in DHCP frame 
  Author:		Saud
  Date Written:	25-9-07

********************************************************************************/
//! The add_req_ipadd function To add ip address request in DHCP frame 
/*!
\function name add_req_ipadd ()
\param unsigned char unsigned char *optptr.
\return static unsigned char *.
*/
static unsigned char*
add_req_ipaddr(unsigned char *optptr)
{
  *optptr++ = DHCP_OPTION_REQ_IPADDR;
  *optptr++ = 4;
  *optptr++ = MyIP[0]&0x00ff;
  *optptr++ = (MyIP[0]&0xff00)>>8;
  *optptr++ = MyIP[1]&0x00ff;
  *optptr++ = (MyIP[1]&0xff00)>>8;
  return optptr;
}
/**
******************************************************************************
  Module Name:	add_end
  Module ID:
  Purpose:		To add end byte i.e. End-of-Frame in DHCP frame 
  Author:		Saud
  Date Written:	25-9-07

********************************************************************************/
//! The add_end function To add end byte i.e. End-of-Frame in DHCP frame 
/*!
\function name add_end ()
\param unsigned char unsigned char *optptr.
\return static unsigned char *.
*/static unsigned char *add_end(unsigned char *optptr){  *optptr++ = DHCP_OPTION_END;  return optptr;}
/**
******************************************************************************
  Module Name:	add_req_options 
  Module ID:
  Purpose:		to add DHCP request options i.e. Subnetmask, Router ip add, DNS server add. 
  Author:		Saud
  Date Written:	25-9-07
********************************************************************************/
//! The add_req_options To add DHCP request options i.e. Subnetmask, Router ip add, DNS server add. 
/*!
\function name add_req_options()
\param unsigned char unsigned char *optptr.
\return static unsigned char *.
*/
static unsigned char *add_req_options(unsigned char *optptr){  *optptr++ = DHCP_OPTION_REQ_LIST;  *optptr++ = 3;  *optptr++ = DHCP_OPTION_SUBNET_MASK;  *optptr++ = DHCP_OPTION_ROUTER;  *optptr++ = DHCP_OPTION_DNS_SERVER;  return optptr;}/**
******************************************************************************
  Module Name:	create_msg 
  Module ID:
  Purpose:		to Create the DHCP frame 
  Author:		Saud
  Date Written:	25-9-07
********************************************************************************/
//! The add_req_options To add DHCP request options i.e. Subnetmask, Router ip add, DNS server add. 
/*!
\function name add_req_options()
\param unsigned char unsigned char *optptr.
\return static unsigned char *.
*/
void create_msg(void)
{
   unsigned char i,j;
   dhcp_buff[0]= DHCPDISCOVER; 	 /*opcode*/
   dhcp_buff[1]= DHCP_HTYPE_ETHERNET;	/*htype*/
   dhcp_buff[2]= 6;		   /*hlen*/
   dhcp_buff[3]= 0;		   /*hops*/
  
   dhcp_buff[4]=xid[0];	 /*xid*/
   dhcp_buff[5]=xid[1];
   dhcp_buff[6]=xid[2];
   dhcp_buff[7]=xid[3];

   dhcp_buff[8]=0; /*seconds*/
   dhcp_buff[9]=0;

   dhcp_buff[10]=0x80; /*flags*/
   dhcp_buff[11]=0;

   for(i=0,j=12;i<16;i++)	 /*ip addresses*/
   dhcp_buff[j++]=0;

   dhcp_buff[28]= 0x00;
   dhcp_buff[29]= 0x50;
   dhcp_buff[30]= 0xC2;
   dhcp_buff[31]= 0x5a;
   dhcp_buff[32]= 0x72;
   dhcp_buff[33]= 0xa5;
      
   dhcp_buff[236]=magic_cookie[0];	 /*Magic cookie*/	  /* magic cookies = 236  */
   dhcp_buff[237]=magic_cookie[1];
   dhcp_buff[238]=magic_cookie[2];
   dhcp_buff[239]=magic_cookie[3];
}
/**
******************************************************************************

  Module Name:	send_dhcp_discover_frame

  Module ID:

  Purpose:		Write SPI sniffed data into circular buffer

  Author:		Saud

  Date Written:	25-9-07

********************************************************************************/
void send_dhcp_discover_frame(void){
    unsigned char *end;
	clear_dhcpbuffer();
	create_msg();
	end = add_msg_type(&dhcp_buff[240], DHCPDISCOVER);    end = add_req_options(end);    end = add_end(end);
	memcpy(UDP_TX_BUF,dhcp_buff,512);
    UDPTxDataCount = 512;
    DHCPTransmitTxBuffer();}

/**
******************************************************************************

  Module Name:	send_dhcp_request_frame

  Module ID:

  Purpose:		Write SPI sniffed data into circular buffer

  Author:		Saud

  Date Written:	25-9-07

********************************************************************************/
void send_dhcp_request_frame(void)
{
  unsigned char *end;
  clear_dhcpbuffer();
  create_msg();
  end = add_msg_type(&dhcp_buff[240], DHCPREQUEST);
  end = add_server_id(end);
  end = add_req_ipaddr(end);
  end = add_end(end);
  memcpy(UDP_TX_BUF,dhcp_buff,350);
  UDPTxDataCount = 512;
  DHCPTransmitTxBuffer();
}

/**
******************************************************************************

  Module Name:	DHCPTransmitTxBuffer

  Module ID:

  Purpose:		Write SPI sniffed data into circular buffer

  Author:		Saud

  Date Written:	25-9-07

********************************************************************************/
void DHCPTransmitTxBuffer(void)
{
 unsigned short bootpc=68;
 unsigned short bootps=67;
 TxFrame3Size = ETH_HEADER_SIZE + IP_HEADER_SIZE + UDP_HEADER_SIZE + UDPTxDataCount;
 PrepareUDP_DATA_FRAME(bootpc,bootps);

}

/**
******************************************************************************

  Module Name:	process_dhcp_offer_frame

  Module ID:

  Purpose:		Write SPI sniffed data into circular buffer

  Author:		Saud

  Date Written:	25-9-07

********************************************************************************/
void process_dhcp_offer_frame(void)
{
 unsigned short i=0,j=0;
 for(i=12,j=0;i<16;i++,j++)
 client_ip[j] =  UDP_rxbuff[i];
 MyIP[0] =  (UDP_rxbuff[16])| (UDP_rxbuff[17]<<8);
 MyIP[1] =  (UDP_rxbuff[18])| (UDP_rxbuff[19]<<8);
 for(i=20,j=0;i<24;i++,j++)
 Next_server_ip[j] =  UDP_rxbuff[i];
 for(i=28,j=0;i<34;i++,j++)
 Client_MAC[j]= UDP_rxbuff[i];
 for(i=245,j=0;i<249;i++,j++)
 Subnet_mask[j]= UDP_rxbuff[i];
 for(i=269,j=0;i<273;i++,j++)
 Server_id[j] = UDP_rxbuff[i];
}

/**
******************************************************************************

  Module Name:	clear_dhcpbuffer

  Module ID:

  Purpose:		Write SPI sniffed data into circular buffer

  Author:		Saud

  Date Written:	25-9-07

********************************************************************************/
 void clear_dhcpbuffer(void)
 {
 	unsigned short i=0;
	for(i=0;i<350;i++)
	dhcp_buff[i]=0;
	
 }


/**
******************************************************************************

  Module Name:	Get_board_IP_Add

  Module ID:

  Purpose:		Write SPI sniffed data into circular buffer

  Author:		Saud

  Date Written:	25-9-07

********************************************************************************/
void Get_board_IP_Add(void)
{
   
   *(unsigned char *)UDP_IP = 255;//169;               // defining the LOG Server IP address
   *((unsigned char *)UDP_IP + 1) = 255;//254;          
   *((unsigned char *)UDP_IP + 2) = 255;//233;         
   *((unsigned char *)UDP_IP + 3) = 255;//9;   

   send_dhcp_discover_frame();
   do
   {
   DoNetworkStuff();//udp_readdata();
   send_dhcp_discover_frame();
   }while(!Received_DHCP_Offer_frame);
   process_dhcp_offer_frame();
   send_dhcp_request_frame();
   do
   {
    DoNetworkStuff();//udp_readdata();

   }while(!Received_DHCP_Ack_frame);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -