📄 dhcp.c
字号:
#include "config.h"
#include "serial.h"
#include "socket.h"
#include "dhcp.h"
#include "init.h"
#include "eeprom.h"
//#define DHCP_DEBUG
RIP_MSG xdata MSG _at_ 0x7d00;
extern u_char xdata Mac[6]; //
extern bit Debug_Off;
extern bit Enable_DHCP_Timer;
u_char xdata SubMask[4];
u_char xdata Gateway[4];
u_char xdata IpAddr[4];
u_char xdata DNS[4];
u_char xdata OLD_SIP[4];
u_char xdata DHCPS_IP[4];
u_int xdata S_port; // DHCP Server Port Number
un_l2cval xdata lease_time;
u_long xdata my_time, next_time;
u_char xdata dhcp_state;
u_char xdata retry_count = 0;
u_char xdata DHCP_Timeout = 0;
u_long xdata DHCP_XID;
/*
********************************************************************************
* Description : Send DHCP discover message
* Arguments : s - socket number
* Returns : None
* Note :
********************************************************************************
*/
void send_DHCP_DISCOVER(SOCKET s)
{
u_int i;
u_char addr[4];
MSG.op = DHCP_BOOTREQUEST;
MSG.htype = DHCP_HTYPE10MB;
MSG.hlen = DHCP_HLENETHERNET;
MSG.hops = DHCP_HOPS;
MSG.xid = DHCP_XID;
MSG.secs = DHCP_SECS;
MSG.flags = DHCP_FLAGSBROADCAST;
MSG.ciaddr[0] = 0;
MSG.ciaddr[1] = 0;
MSG.ciaddr[2] = 0;
MSG.ciaddr[3] = 0;
MSG.yiaddr[0] = 0;
MSG.yiaddr[1] = 0;
MSG.yiaddr[2] = 0;
MSG.yiaddr[3] = 0;
MSG.siaddr[0] = 0;
MSG.siaddr[1] = 0;
MSG.siaddr[2] = 0;
MSG.siaddr[3] = 0;
MSG.giaddr[0] = 0;
MSG.giaddr[1] = 0;
MSG.giaddr[2] = 0;
MSG.giaddr[3] = 0;
// setting default Mac Address Value.
MSG.chaddr[0] = Mac[0];
MSG.chaddr[1] = Mac[1];
MSG.chaddr[2] = Mac[2];
MSG.chaddr[3] = Mac[3];
MSG.chaddr[4] = Mac[4];
MSG.chaddr[5] = Mac[5];
for (i = 6; i < 16; i++) MSG.chaddr[i] = 0;
for (i = 0; i < 64; i++) MSG.sname[i] = 0;
for (i = 0; i < 128; i++) MSG.file[i] = 0;
// MAGIC_COOKIE
MSG.OPT[0] = MAGIC0;
MSG.OPT[1] = MAGIC1;
MSG.OPT[2] = MAGIC2;
MSG.OPT[3] = MAGIC3;
// Option Request Param.
MSG.OPT[4] = dhcpMessageType;
MSG.OPT[5] = 0x01;
MSG.OPT[6] = DHCP_DISCOVER;
MSG.OPT[7] = dhcpParamRequest;
MSG.OPT[8] = 0x05;
MSG.OPT[9] = subnetMask;
MSG.OPT[10] = routersOnSubnet;
MSG.OPT[11] = dns;
MSG.OPT[12] = dhcpT1value;
MSG.OPT[13] = dhcpT2value;
MSG.OPT[14] = endOption;
// Null Padding
for (i = 15; i < 312; i++) MSG.OPT[i] = 0;
/* DST IP : BroadCasting*/
S_port = DHCP_SERVER_PORT;
for (i=0; i<4; i++) addr[i] = 0xFF;
#ifdef DHCP_DEBUG
PutString("send_DHCP_DISCOVER : ");
#endif
sendto(s, (UCHAR *)(&MSG.op), RIP_MSG_SIZE, addr, S_port);
#ifdef DHCP_DEBUG
PutStringLn("OK");
#endif
}
/*
********************************************************************************
* Description : Send DHCP request message
* Arguments : s - socket number
* Returns : None
* Note :
********************************************************************************
*/
void send_DHCP_REQUEST(SOCKET s)
{
u_int i;
u_char addr[4];
MSG.op = DHCP_BOOTREQUEST;
MSG.htype = DHCP_HTYPE10MB;
MSG.hlen = DHCP_HLENETHERNET;
MSG.hops = DHCP_HOPS;
MSG.xid = DHCP_XID;
MSG.secs = DHCP_SECS;
MSG.flags = DHCP_FLAGSBROADCAST;
MSG.ciaddr[0] = 0;
MSG.ciaddr[1] = 0;
MSG.ciaddr[2] = 0;
MSG.ciaddr[3] = 0;
MSG.yiaddr[0] = 0;
MSG.yiaddr[1] = 0;
MSG.yiaddr[2] = 0;
MSG.yiaddr[3] = 0;
MSG.siaddr[0] = 0;
MSG.siaddr[1] = 0;
MSG.siaddr[2] = 0;
MSG.siaddr[3] = 0;
MSG.giaddr[0] = 0;
MSG.giaddr[1] = 0;
MSG.giaddr[2] = 0;
MSG.giaddr[3] = 0;
MSG.chaddr[0] = Mac[0];
MSG.chaddr[1] = Mac[1];
MSG.chaddr[2] = Mac[2];
MSG.chaddr[3] = Mac[3];
MSG.chaddr[4] = Mac[4];
MSG.chaddr[5] = Mac[5];
for (i = 6; i < 16; i++) MSG.chaddr[i] = 0;
for (i = 0; i < 64; i++) MSG.sname[i] = 0;
for (i = 0; i < 128; i++) MSG.file[i] = 0;
// MAGIC_COOKIE
MSG.OPT[0] = MAGIC0;
MSG.OPT[1] = MAGIC1;
MSG.OPT[2] = MAGIC2;
MSG.OPT[3] = MAGIC3;
// Option Request Param.
MSG.OPT[4] = dhcpMessageType;
MSG.OPT[5] = 0x01;
MSG.OPT[6] = DHCP_REQUEST;
MSG.OPT[7] = dhcpClientIdentifier;
MSG.OPT[8] = 0x07;
MSG.OPT[9] = 0x01;
MSG.OPT[10] = Mac[0];
MSG.OPT[11] = Mac[1];
MSG.OPT[12] = Mac[2];
MSG.OPT[13] = Mac[3];
MSG.OPT[14] = Mac[4];
MSG.OPT[15] = Mac[5];
MSG.OPT[16] = dhcpRequestedIPaddr;
MSG.OPT[17] = 0x04;
MSG.OPT[18] = IpAddr[0];
MSG.OPT[19] = IpAddr[1];
MSG.OPT[20] = IpAddr[2];
MSG.OPT[21] = IpAddr[3];
MSG.OPT[22] = dhcpServerIdentifier;
MSG.OPT[23] = 0x04;
MSG.OPT[24] = DHCPS_IP[0];
MSG.OPT[25] = DHCPS_IP[1];
MSG.OPT[26] = DHCPS_IP[2];
MSG.OPT[27] = DHCPS_IP[3];
MSG.OPT[28] = hostName;
MSG.OPT[29] = 0x0C;
MSG.OPT[30] = 'I';
MSG.OPT[31] = 'I';
MSG.OPT[32] = 'M';
MSG.OPT[33] = '7';
MSG.OPT[34] = '1';
MSG.OPT[35] = '0';
MSG.OPT[36] = Mac[0];
MSG.OPT[37] = Mac[1];
MSG.OPT[38] = Mac[2];
MSG.OPT[39] = Mac[3];
MSG.OPT[40] = Mac[4];
MSG.OPT[41] = Mac[5];
MSG.OPT[42] = dhcpParamRequest;
MSG.OPT[43] = 0x05;
MSG.OPT[44] = subnetMask;
MSG.OPT[45] = routersOnSubnet;
MSG.OPT[46] = dns;
MSG.OPT[47] = dhcpT1value;
MSG.OPT[48] = dhcpT2value;
MSG.OPT[49] = endOption;
for (i = 50; i < 312; i++) MSG.OPT[i] = 0;
/* DST IP : BroadCasting*/
S_port = DHCP_SERVER_PORT;
for (i=0; i<4; i++) addr[i] = 0xFF;
#ifdef DHCP_DEBUG
PutStringLn("send_DHCP_REQUEST");
#endif
sendto(s, (UCHAR *)(&MSG.op), RIP_MSG_SIZE, addr, S_port);
}
/*
********************************************************************************
* Description : DHCP message parsing routine
* Arguments : s - socket number, length - receive data size
* Returns : message type
* Note :
********************************************************************************
*/
char parseDHCPMSG(SOCKET s, UINT length)
{
u_char ServerAddrIn[4];
u_int ServerPort;
UINT len;
UCHAR type,i,opt_len;
UCHAR xdata* p;
UCHAR xdata* e;
len = recvfrom(s, (UCHAR*)&MSG.op, length, ServerAddrIn, &ServerPort);
if (ServerPort == DHCP_SERVER_PORT)
{
/*
#ifdef DHCP_DEBUG
PutString("\n\r DHCP MSG received : len = ");PutHTOA(len);
PutString("\n\r MAC : ");
for (i = 0; i < 6; i++) {
PutHTOA(Mac[i]);PutByte(' ');
}
#endif
*/
for (i = 0; i < 6; i++) {
if (MSG.chaddr[i] != Mac[i]) return(0);
}
//DHCP Server IP
for(i = 0; i < 4; i++) {
DHCPS_IP[i] = MSG.siaddr[i];
}
for (i = 0; i < 4; i++)
{
IpAddr[i] = MSG.yiaddr[i];
/*
#ifdef DHCP_DEBUG
PutHTOA(MSG.yiaddr[i]);
PutByte(':');
#endif
*/
}
/*
#ifdef DHCP_DEBUG
PutStringLn("");
#endif
*/
}
type = 0;
p = (UCHAR xdata*)(&MSG.op);
p = p + 240;
e = p + (len - 240);
while ( p < e )
{
switch ( *p )
{
case endOption :
goto PARSE_END;
break;
case padOption :
p++;
break;
case dhcpMessageType :
p++;
p++;
type = *p++;
/*
#ifdef DHCP_DEBUG
PutString("dhcpMessageType : ");
PutHTOA(type);
PutStringLn("");
#endif
*/
break;
case subnetMask :
p++;
p++;
for (i = 0; i < 4; i++) SubMask[i] = *p++;
/*
#ifdef DHCP_DEBUG
PutString("subnetMask : ");
for (i = 0; i < 4; i++)
{
PutHTOA(SubMask[i]);
PutByte(':');
}
PutStringLn("");
#endif
*/
break;
case routersOnSubnet :
p++;
p++;
for (i = 0; i < 4; i++) Gateway[i] = *p++;
/*
#ifdef DHCP_DEBUG
PutString("routersOnSubnet : ");
for (i = 0; i < 4; i++)
{
PutHTOA(Gateway[i]);
PutByte(':');
}
PutStringLn("");
#endif
*/
break;
case dns :
p++;
p++;
for (i = 0; i < 4; i++) DNS[i] = *p++;
/*
#ifdef DHCP_DEBUG
PutString("dns : ");
for (i = 0; i < 4; i++)
{
PutHTOA(DNS[i]);
PutByte(':');
}
PutStringLn("");
#endif
*/
break;
case dhcpIPaddrLeaseTime :
p++;
opt_len = *p++;
lease_time.cVal[0] = *p++;
lease_time.cVal[1] = *p++;
lease_time.cVal[2] = *p++;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -