/*************************************************************************
* Copyright(c) 2009, xxxcorporation
* All rights reserved
* dhcp_client.c : dhcp client c file
* version: 1.0
* author: ywg
* time: 2009-02-07
* history: none
**************************************************************************/
#include "dhcp_config.h"
#include "dhcp_type.h"
#include "dhcp_error.h"
#include "dhcp_client.h"
#include "dhcp_system.h"
#include "ip_cfg.h"
#include "drv_os.h"
struct dhcp_opt60
{
int our_len;
int you_len;
char our_optbuf[32];
char you_optbuf[32];
};
//#define DHCP_DEBUG printf
//#define DHCP_VENDOR_CLASS_ID_OPTION_NEEDED 1
#define DHCP_VENDOR_SPECIFIC_INFO_OPTION_NEEDED 1
//#define DHCP_DO_ARP_CHECK 1
static int DHCP_VENDOR_CLASS_ID_OPTION_NEEDED=0;
static int DHCP_OUR_OPTION60_NEEDED=0;
static int DHCP_YOU_OPTION60_NEEDED=0;
static int DHCP_DO_ARP_CHECK=0;
static u32_t xid = 0xABCD0000;
static int state;
static int dhcp_timing;
static int dhcp_time_sw=0;
static int dhcp_time_bisc = 500; /* dhcp timer 500us */
static char current_if[16];
static struct dhcp_info dhcp_desp;
static struct dhcp_opt60 dhcp_opt60;
static char * dhcp_msg_queue;
static pthread_mutex_t dhcp_locker;
static DRVOS_Sem_t *dhcp_sem;
#if defined(__LINUX_SYS__)
static u32_t msg_project_id=2;
static DRVOS_MsgQueue_p msg_queue_id;
#endif
void GetDHCP(struct dhcp_msg *hdr,void *p, int optionlen)
{
int i;
u8_t * tmpayload;
UIN16BIT temp;
UINT32BIT temp32;
//memset(hdr,0, DHCP_MSG_LEN+4);
memset(hdr,0, sizeof(struct dhcp_msg));
tmpayload = (u8_t*)p;
hdr->op = *(tmpayload);
hdr->htype = *(tmpayload + 1);
hdr->hlen = *(tmpayload + 2);
hdr->hops = *(tmpayload + 3);
temp32.byte.ucByte0 = *(tmpayload+4);
temp32.byte.ucByte1 = *(tmpayload+5);
temp32.byte.ucByte2 = *(tmpayload+6);
temp32.byte.ucByte3 = *(tmpayload+7);
hdr->xid = temp32.uiWord32;
temp.byte.ucByte0 = *(tmpayload+8);
temp.byte.ucByte1 = *(tmpayload+9);
hdr->secs = temp.uiWord16;
temp.byte.ucByte0 = *(tmpayload+10);
temp.byte.ucByte1 = *(tmpayload+11);
hdr->flags = temp.uiWord16;
temp32.byte.ucByte0 = *(tmpayload+12);
temp32.byte.ucByte1 = *(tmpayload+13);
temp32.byte.ucByte2 = *(tmpayload+14);
temp32.byte.ucByte3 = *(tmpayload+15);
hdr->ciaddr.addr = temp32.uiWord32;
temp32.byte.ucByte0 = *(tmpayload+16);
temp32.byte.ucByte1 = *(tmpayload+17);
temp32.byte.ucByte2 = *(tmpayload+18);
temp32.byte.ucByte3 = *(tmpayload+19);
hdr->yiaddr.addr = temp32.uiWord32;
temp32.byte.ucByte0 = *(tmpayload+20);
temp32.byte.ucByte1 = *(tmpayload+21);
temp32.byte.ucByte2 = *(tmpayload+22);
temp32.byte.ucByte3 = *(tmpayload+23);
hdr->siaddr.addr = temp32.uiWord32;
temp32.byte.ucByte0 = *(tmpayload+24);
temp32.byte.ucByte1 = *(tmpayload+25);
temp32.byte.ucByte2 = *(tmpayload+26);
temp32.byte.ucByte3 = *(tmpayload+27);
hdr->giaddr.addr = temp32.uiWord32;
memcpy(hdr->chaddr, tmpayload + 28, DHCP_CHADDR_LEN);
memcpy(hdr->sname, tmpayload + 28+DHCP_CHADDR_LEN, DHCP_SNAME_LEN);
memcpy(hdr->file, tmpayload + 28+DHCP_CHADDR_LEN+DHCP_SNAME_LEN, DHCP_FILE_LEN);
temp32.byte.ucByte0 = *(tmpayload+236);
temp32.byte.ucByte1 = *(tmpayload+237);
temp32.byte.ucByte2 = *(tmpayload+238);
temp32.byte.ucByte3 = *(tmpayload+239);
hdr->cookie = temp32.uiWord32;
/* copy options */
for(i=0; i<optionlen; i++){
if(i > DHCP_OPTIONS_LEN){
DHCP_DEBUG("GetDHCP : copy options error max_len=%d, fact_len=%d\n", DHCP_OPTIONS_LEN, optionlen);
}
hdr->options[i] = *(tmpayload + 240 + i);
}
}
void SetDHCP(struct dhcp_msg *hdr,void *p,int optionlen)
{
int i;
u8_t * tmpayload;
UIN16BIT temp;
UINT32BIT temp32;
tmpayload = (u8_t*)p;
*(tmpayload) = hdr->op;
*(tmpayload + 1) = hdr->htype;
*(tmpayload + 2) = hdr->hlen;
*(tmpayload + 3) = hdr->hops;
temp32.uiWord32 = hdr->xid;
*(tmpayload+4) = temp32.byte.ucByte0;
*(tmpayload+5) = temp32.byte.ucByte1;
*(tmpayload+6) = temp32.byte.ucByte2;
*(tmpayload+7) = temp32.byte.ucByte3;
temp.uiWord16 = hdr->secs;
*(tmpayload+8) = temp.byte.ucByte0;
*(tmpayload+9) = temp.byte.ucByte1;
temp.uiWord16 = hdr->flags;
*(tmpayload+10) = temp.byte.ucByte0;
*(tmpayload+11) = temp.byte.ucByte1;
temp32.uiWord32 = hdr->ciaddr.addr;
*(tmpayload+12) = temp32.byte.ucByte0;
*(tmpayload+13) = temp32.byte.ucByte1;
*(tmpayload+14) = temp32.byte.ucByte2;
*(tmpayload+15) = temp32.byte.ucByte3;
temp32.uiWord32 = hdr->yiaddr.addr;
*(tmpayload+16) = temp32.byte.ucByte0;
*(tmpayload+17) = temp32.byte.ucByte1;
*(tmpayload+18) = temp32.byte.ucByte2;
*(tmpayload+19) = temp32.byte.ucByte3;
temp32.uiWord32 = hdr->siaddr.addr;
*(tmpayload+20) = temp32.byte.ucByte0;
*(tmpayload+21) = temp32.byte.ucByte1;
*(tmpayload+22) = temp32.byte.ucByte2;
*(tmpayload+23) = temp32.byte.ucByte3;
temp32.uiWord32 = hdr->giaddr.addr;
*(tmpayload+24) = temp32.byte.ucByte0;
*(tmpayload+25) = temp32.byte.ucByte1;
*(tmpayload+26) = temp32.byte.ucByte2;
*(tmpayload+27) = temp32.byte.ucByte3;
memcpy(tmpayload + 28, hdr->chaddr, DHCP_CHADDR_LEN);
memcpy(tmpayload + 28+DHCP_CHADDR_LEN, hdr->sname, DHCP_SNAME_LEN);
memcpy(tmpayload + 28+DHCP_CHADDR_LEN+DHCP_SNAME_LEN, hdr->file, DHCP_FILE_LEN);
temp32.uiWord32 = hdr->cookie;
*(tmpayload+236) = temp32.byte.ucByte0;
*(tmpayload+237) = temp32.byte.ucByte1;
*(tmpayload+238) = temp32.byte.ucByte2;
*(tmpayload+239) = temp32.byte.ucByte3;
/* copy options */
for(i=0; i<optionlen; i++){
if(i > DHCP_OPTIONS_LEN){
DHCP_DEBUG("SetDHCP : copy options error max_len=%d, fact_len=%d\n", DHCP_OPTIONS_LEN, optionlen);
}
*(tmpayload + 240 + i) = hdr->options[i];
}
}
void dhcp_option_cfg()
{
/* process all avaliable options */
}
/*
* Concatenate an option type and length field to the outgoing
* DHCP message.
*
*/
static void dhcp_option(struct dhcp_info *dhcp, u8_t option_type, u8_t option_len)
{
dhcp->msg_out->options[dhcp->options_out_len++] = option_type;
dhcp->msg_out->options[dhcp->options_out_len++] = option_len;
}
/*
* Concatenate a single byte to the outgoing DHCP message.
*
*/
static void dhcp_option_byte(struct dhcp_info *dhcp, u8_t value)
{
dhcp->msg_out->options[dhcp->options_out_len++] = value;
}
static void dhcp_option_short(struct dhcp_info *dhcp, u16_t value)
{
dhcp->msg_out->options[dhcp->options_out_len++] = (value & 0xff00U) >> 8;
dhcp->msg_out->options[dhcp->options_out_len++] = value & 0x00ffU;
}
static void dhcp_option_long(struct dhcp_info *dhcp, u32_t value)
{
dhcp->msg_out->options[dhcp->options_out_len++] = (value & 0xff000000UL) >> 24;
dhcp->msg_out->options[dhcp->options_out_len++] = (value & 0x00ff0000UL) >> 16;
dhcp->msg_out->options[dhcp->options_out_len++] = (value & 0x0000ff00UL) >> 8;
dhcp->msg_out->options[dhcp->options_out_len++] = (value & 0x000000ffUL);
}
/**
* Add a DHCP message trailer
*
* Adds the END option to the DHCP message, and if
* necessary, up to three padding bytes.
*/
static void dhcp_option_trailer(struct dhcp_info *dhcp)
{
dhcp->msg_out->options[dhcp->options_out_len++] = DHCP_OPTION_END;
/* packet is too small, or not 4 byte aligned? */
while ((dhcp->options_out_len < DHCP_MIN_OPTIONS_LEN) || (dhcp->options_out_len & 3)) {
/* LWIP_DEBUGF(DHCP_DEBUG,
评论0