diff --git a/src/pppd/codec.c b/src/pppd/codec.c index a77f18d..5a91d34 100644 --- a/src/pppd/codec.c +++ b/src/pppd/codec.c @@ -638,8 +638,6 @@ void build_padt(U8 *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb) pppoe_header->length = 0; *mulen = sizeof(struct rte_ether_hdr) + sizeof(vlan_header_t) + sizeof(pppoe_header_t); - - return SUCCESS; } /** @@ -652,14 +650,14 @@ void build_padt(U8 *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb) * output: TRUE/FALSE * return: packet buffer */ -STATUS build_config_request(unsigned char *buffer, PPP_INFO_t *s_ppp_ccb, U16 *mulen) +void build_config_request(U8 *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb) { - struct rte_ether_hdr *eth_hdr = s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].eth_hdr; - vlan_header_t *vlan_header = s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].vlan_header; - pppoe_header_t *pppoe_header = s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].pppoe_header; - ppp_payload_t *ppp_payload = s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].ppp_payload; - ppp_header_t *ppp_hdr = s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].ppp_hdr; - ppp_options_t *ppp_options = s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].ppp_options; + struct rte_ether_hdr *eth_hdr = (struct rte_ether_hdr *)buffer; + vlan_header_t *vlan_header = (vlan_header_t *)(eth_hdr + 1); + pppoe_header_t *pppoe_header = (pppoe_header_t *)(vlan_header + 1); + ppp_payload_t *ppp_payload = (ppp_payload_t *)(pppoe_header + 1); + ppp_header_t *ppp_hdr = (ppp_header_t *)(ppp_payload + 1); + ppp_options_t *ppp_options = (ppp_options_t *)(ppp_hdr + 1); srand(time(NULL)); @@ -691,7 +689,7 @@ STATUS build_config_request(unsigned char *buffer, PPP_INFO_t *s_ppp_ccb, U16 *m if (s_ppp_ccb->cp == 1) { ppp_payload->ppp_protocol = rte_cpu_to_be_16(IPCP_PROTOCOL); ppp_options->type = IP_ADDRESS; - rte_memcpy(ppp_options->val,&(s_ppp_ccb->hsi_ipv4),4); + rte_memcpy(ppp_options->val, &(s_ppp_ccb->hsi_ipv4), 4); ppp_options->length = sizeof(s_ppp_ccb->hsi_ipv4) + sizeof(ppp_options_t); pppoe_header->length += ppp_options->length; ppp_hdr->length += ppp_options->length; @@ -745,16 +743,8 @@ STATUS build_config_request(unsigned char *buffer, PPP_INFO_t *s_ppp_ccb, U16 *m pppoe_header->length = rte_cpu_to_be_16(pppoe_header->length); ppp_hdr->length = rte_cpu_to_be_16(ppp_hdr->length); - memset(buffer,0,MSG_BUF); - rte_memcpy(buffer,eth_hdr,14); - rte_memcpy(buffer+14,vlan_header,sizeof(vlan_header_t)); - rte_memcpy(buffer+14+sizeof(vlan_header_t),pppoe_header,sizeof(pppoe_header_t)); - rte_memcpy(buffer+14+sizeof(vlan_header_t)+sizeof(pppoe_header_t),ppp_payload,sizeof(ppp_payload_t)); - rte_memcpy(buffer+14+sizeof(vlan_header_t)+sizeof(pppoe_header_t)+sizeof(ppp_payload_t),ppp_hdr,sizeof(ppp_header_t)); - rte_memcpy(buffer+14+sizeof(vlan_header_t)+sizeof(pppoe_header_t)+sizeof(ppp_payload_t)+sizeof(ppp_header_t),ppp_options,rte_cpu_to_be_16(ppp_hdr->length) - sizeof(ppp_header_t)); - + VRG_LOG(DBG, vrg_ccb->fp, s_ppp_ccb, PPPLOGMSG, "User %" PRIu16 " config request built.", s_ppp_ccb->user_num); - return TRUE; } /** diff --git a/src/pppd/codec.h b/src/pppd/codec.h index 57a0496..8473b02 100644 --- a/src/pppd/codec.h +++ b/src/pppd/codec.h @@ -22,7 +22,7 @@ extern STATUS decode_ipcp(pppoe_header_t *pppoe_header, ppp_payload_t *ppp_paylo extern void DECODE_OBJID(U8 *vp, U8 vlen, U32 *oids, U8 *oids_len); -extern STATUS build_config_request(unsigned char *buffer, PPP_INFO_t *s_ppp_ccb, U16 *mulen); +void build_config_request(unsigned char *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb); extern STATUS build_config_ack(unsigned char *buffer, PPP_INFO_t *s_ppp_ccb, U16 *mulen); extern STATUS build_config_nak_rej(unsigned char *buffer, PPP_INFO_t *s_ppp_ccb, U16 *mulen); extern STATUS build_terminate_ack(unsigned char *buffer, PPP_INFO_t *s_ppp_ccb, U16 *mulen); diff --git a/src/pppd/fsm.c b/src/pppd/fsm.c index 7e929e9..288f8df 100644 --- a/src/pppd/fsm.c +++ b/src/pppd/fsm.c @@ -789,8 +789,7 @@ STATUS A_send_config_request(__attribute__((unused)) struct rte_timer *tim, __at VRG_LOG(DBG, vrg_ccb->fp, s_ppp_ccb, PPPLOGMSG, "User %" PRIu16 " config request timeout.\n", s_ppp_ccb->user_num); PPP_FSM(tim,s_ppp_ccb,E_TIMEOUT_COUNTER_EXPIRED); } - if (build_config_request(buffer,s_ppp_ccb,&mulen) < 0) - return FALSE; + build_config_request(buffer, &mulen, s_ppp_ccb); drv_xmit(vrg_ccb, buffer, mulen); s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].timer_counter--; diff --git a/unit_test/pppd/codec_test.c b/unit_test/pppd/codec_test.c index 1cf61c9..fabdcb4 100644 --- a/unit_test/pppd/codec_test.c +++ b/unit_test/pppd/codec_test.c @@ -1,5 +1,5 @@ -#include #include +#include #include #include "../../src/pppd/codec.h" #include "../../src/protocol.h" @@ -114,7 +114,64 @@ void test_build_padt() { 0x16, 0xdd, 0x81, 0x00, 0x00, 0x02, 0x88, 0x63, 0x11, 0xa7, 0x00, 0x0a, 0x00, 0x00}; - assert(build_padt(buffer, &mulen, &s_ppp_ccb_1) == SUCCESS); + build_padt(buffer, &mulen, &s_ppp_ccb_1); assert(mulen == sizeof(pkt_1)); assert(memcmp(buffer, pkt_1, mulen) == 0); } + +void test_build_config_request() { + U8 buffer[80]; + U16 mulen = 0; + + PPP_INFO_t s_ppp_ccb_1 = { + .ppp_phase = {{ + .timer_counter = 0, + .max_retransmit = 10, + },{ + .timer_counter = 0, + .max_retransmit = 10, + },}, + .user_num = 1, + .vlan = 2, + .PPP_dst_mac = (struct rte_ether_addr){ + .addr_bytes = {0x74, 0x4d, 0x28, 0x8d, 0x00, 0x31}, + }, + .session_id = htons(0x000a), + .cp = 0, + }; + + char pkt_lcp[] = {0x74, 0x4d, 0x28, 0x8d, 0x00, 0x31, 0x9c, 0x69, 0xb4, 0x61, + 0x16, 0xdd, 0x81, 0x00, 0x00, 0x02, 0x88, 0x64, 0x11, 0x00, 0x00, 0x0a, 0x00, + 0x10, 0xc0, 0x21, 0x01, 0x01, 0x00, 0x0e, 0x01, 0x04, 0x05, 0xd0, 0x05, 0x06, + 0x00, 0x01, 0x02, 0x03}; + char pkt_ipcp[] = {0x74, 0x4d, 0x28, 0x8d, 0x00, 0x31, 0x9c, 0x69, 0xb4, 0x61, + 0x16, 0xdd, 0x81, 0x00, 0x00, 0x02, 0x88, 0x64, 0x11, 0x00, 0x00, 0x0a, 0x00, + 0x0c, 0x80, 0x21, 0x01, 0x01, 0x00, 0x0a, 0x03, 0x06, 0x00, 0x00, 0x00, 0x00}; + + /* test LCP */ + build_config_request(buffer, &mulen, &s_ppp_ccb_1); + assert(mulen == sizeof(pkt_lcp)); + assert(memcmp(buffer, pkt_lcp, 26/* only memcmp to lcp field */) == 0); + ppp_header_t *test_ppp_hdr = (ppp_header_t *)(buffer + 26); + assert(test_ppp_hdr->code == CONFIG_REQUEST); + assert(test_ppp_hdr->length == htons(0x000e)); + ppp_options_t *test_ppp_options = (ppp_options_t *)(test_ppp_hdr + 1); + assert(test_ppp_options->type == 0x01); + assert(test_ppp_options->length == 0x04); + test_ppp_options = (ppp_options_t *)((U8 *)test_ppp_options + test_ppp_options->length); + assert(test_ppp_options->type == 0x05); + assert(test_ppp_options->length == 0x06); + + /* test IPCP */ + memset(buffer, 0, sizeof(buffer)); + s_ppp_ccb_1.cp = 1; + build_config_request(buffer, &mulen, &s_ppp_ccb_1); + assert(mulen == sizeof(pkt_ipcp)); + assert(memcmp(buffer, pkt_ipcp, 26/* only memcmp to ipcp field */) == 0); + test_ppp_hdr = (ppp_header_t *)(buffer + 26); + assert(test_ppp_hdr->code == CONFIG_REQUEST); + assert(test_ppp_hdr->length == htons(0x000a)); + test_ppp_options = (ppp_options_t *)(test_ppp_hdr + 1); + assert(test_ppp_options->type == 0x03); + assert(test_ppp_options->length == 0x06); +} \ No newline at end of file diff --git a/unit_test/test.c b/unit_test/test.c index 018c5f8..7857bd1 100644 --- a/unit_test/test.c +++ b/unit_test/test.c @@ -32,6 +32,7 @@ int main() test_build_padi(); test_build_padr(); test_build_padt(); + test_build_config_request(); puts("ok!"); puts("\nall test successfully"); diff --git a/unit_test/test.h b/unit_test/test.h index 28ea09a..39b084e 100644 --- a/unit_test/test.h +++ b/unit_test/test.h @@ -4,5 +4,6 @@ void test_build_padi(); void test_build_padr(); void test_build_padt(); +void test_build_config_request(); #endif \ No newline at end of file