Skip to content

Commit

Permalink
Update for packet handling issue futurewei-cloud#102
Browse files Browse the repository at this point in the history
  • Loading branch information
w2520n2520 committed Aug 29, 2020
1 parent 68b2bce commit ef44971
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
12 changes: 11 additions & 1 deletion include/aca_dhcp_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ struct dhcp_entry_data {
#define DHCP_MSG_HWTYPE_ETH_LEN (0x6)
#define DHCP_MSG_MAGIC_COOKIE (0x63825363) //magic cookie per RFC2131

#pragma pack(push, 1)
struct dhcp_message {
uint8_t op;
uint8_t htype;
Expand Down Expand Up @@ -89,11 +90,11 @@ struct dhcp_message {
#define DHCP_OPT_CODE_IP_LEASE_TIME (0x33)
#define DHCP_OPT_CODE_SERVER_ID (0x36)
#define DHCP_OPT_CODE_REQ_IP (0x32)
#define DHCP_OPT_CODE_CLIENT_ID (0x3d)

#define DHCP_OPT_CLV_HEADER (0x2) //CLV = Code + Length + Value
#define DHCP_OPT_DEFAULT_IP_LEASE_TIME (86400) //One day

#pragma pack(push, 1)
struct dhcp_message_type {
uint8_t code;
uint8_t len;
Expand All @@ -117,13 +118,21 @@ struct dhcp_req_ip {
uint8_t len;
uint32_t req_ip;
};

struct dhcp_client_id {
uint8_t code;
uint8_t len;
uint8_t type;
uint8_t *client_id;
};
#pragma pack(pop)

union dhcp_message_options {
dhcp_message_type *dhcpmsgtype;
dhcp_ip_lease_time *ipleasetime;
dhcp_server_id *serverid;
dhcp_req_ip *reqip;
dhcp_client_id *clientid;
};

class ACA_Dhcp_Server : public aca_dhcp_programming_if::ACA_Dhcp_Programming_Interface {
Expand Down Expand Up @@ -163,6 +172,7 @@ class ACA_Dhcp_Server : public aca_dhcp_programming_if::ACA_Dhcp_Programming_Int
uint8_t _get_message_type(dhcp_message *dhcpmsg);
uint32_t _get_server_id(dhcp_message *dhcpmsg);
uint32_t _get_requested_ip(dhcp_message *dhcpmsg);
string _get_client_id(dhcp_message *dhcpmsg);

void _parse_dhcp_none(dhcp_message *dhcpmsg);
void _parse_dhcp_discover(dhcp_message *dhcpmsg);
Expand Down
35 changes: 33 additions & 2 deletions src/dhcp/aca_dhcp_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,38 @@ uint32_t ACA_Dhcp_Server::_get_requested_ip(dhcp_message *dhcpmsg)
return unopt.reqip->req_ip;
}

string ACA_Dhcp_Server::_get_client_id(dhcp_message *dhcpmsg)
{
dhcp_message_options unopt;
string cid;

if (!dhcpmsg) {
ACA_LOG_ERROR("DHCP message is null!\n");
return nullptr;
}

// get client identifier from option
unopt.clientid = (dhcp_client_id *)_get_option(dhcpmsg, DHCP_OPT_CODE_CLIENT_ID);
if (unopt.clientid) {
for (int i = 0; i < unopt.clientid->len - 1; i++) {
cid.push_back(unopt.clientid->client_id[i]);
cid.push_back(':');
}
cid.pop_back();

return cid;
}

// get client identifier from chaddr
for (int i = 0; i < dhcpmsg->hlen; i++) {
cid.push_back(dhcpmsg->chaddr[i]);
cid.push_back(':');
}
cid.pop_back();

return cid;
}

void ACA_Dhcp_Server::_pack_dhcp_message(dhcp_message *rpl, dhcp_message *req)
{
if (!rpl || !req) {
Expand Down Expand Up @@ -515,8 +547,7 @@ void ACA_Dhcp_Server::_parse_dhcp_discover(dhcp_message *dhcpmsg)
dhcp_entry_data *pData = nullptr;
dhcp_message *dhcpoffer = nullptr;

mac_address = (char *)dhcpmsg->chaddr;
mac_address.substr(0, dhcpmsg->hlen);
mac_address = _get_client_id(dhcpmsg);
pData = _search_dhcp_entry(mac_address);
if (!pData) {
ACA_LOG_ERROR("DHCP entry does not exist! (mac = %s)\n", mac_address.c_str());
Expand Down

0 comments on commit ef44971

Please sign in to comment.