diff options
-rw-r--r-- | libdhcp/dhcp.c | 90 |
1 files changed, 39 insertions, 51 deletions
diff --git a/libdhcp/dhcp.c b/libdhcp/dhcp.c index 51e81d2..77f4b61 100644 --- a/libdhcp/dhcp.c +++ b/libdhcp/dhcp.c @@ -233,36 +233,27 @@ static const uint8_t dhcp_magic_cookie[] = {99, 130, 83, 99}; #endif /* DHCP state machine. */ -#define STATE_DHCP_INIT 0 ///< Initialize -#define STATE_DHCP_DISCOVER 1 ///< send DISCOVER and wait OFFER -#define STATE_DHCP_REQUEST 2 ///< send REQEUST and wait ACK or NACK -#define STATE_DHCP_LEASED 3 ///< ReceiveD ACK and IP leased -#define STATE_DHCP_REREQUEST 4 ///< send REQUEST for maintaining leased IP -#define STATE_DHCP_RELEASE 5 ///< No use -#define STATE_DHCP_STOP 6 ///< Stop processing DHCP - -struct net_ip4_addr DHCP_SIP; // DHCP Server IP address -struct net_ip4_addr DHCP_REAL_SIP; // For extract my DHCP server in a few DHCP server - -// Network information from DHCP Server -struct net_ip4_addr OLD_allocated_ip = {0}; // Previous IP address -struct dhcp_lease global_lease = { - .lifetime = DHCP_INFINITY, -}; - -int8_t dhcp_state = STATE_DHCP_INIT; // DHCP state -int8_t dhcp_retry_count = 0; - -volatile uint32_t dhcp_tick_1s = 0; // unit 1 second -uint32_t dhcp_tick_next = DHCP_WAIT_TIME ; - -uint32_t global_xid; // Any number - -struct dhcp_msg* pDHCPMSG; // Buffer pointer for DHCP processing - -char global_hostname[64]; - -struct net_eth_addr global_eth_addr; // DHCP Client MAC address. +#define STATE_DHCP_INIT 0 /* Initialize */ +#define STATE_DHCP_DISCOVER 1 /* send DISCOVER and wait OFFER */ +#define STATE_DHCP_REQUEST 2 /* send REQEUST and wait ACK or NACK */ +#define STATE_DHCP_LEASED 3 /* ReceiveD ACK and IP leased */ +#define STATE_DHCP_REREQUEST 4 /* send REQUEST for maintaining leased IP */ +#define STATE_DHCP_RELEASE 5 /* No use */ +#define STATE_DHCP_STOP 6 /* Stop processing DHCP */ + +/* Global state. */ +struct net_ip4_addr DHCP_SIP; /* DHCP Server IP address */ +struct net_ip4_addr DHCP_REAL_SIP; /* For extract my DHCP server in a few DHCP server */ +struct net_ip4_addr OLD_allocated_ip = {0}; /* Previous IP address */ +struct dhcp_lease global_lease = {.lifetime = DHCP_INFINITY}; +int8_t dhcp_state = STATE_DHCP_INIT; /* DHCP state */ +int8_t dhcp_retry_count = 0; +volatile uint32_t dhcp_tick_1s = 0; /* unit 1 second */ +uint32_t dhcp_tick_next = DHCP_WAIT_TIME ; +uint32_t global_xid; /* Any number */ +struct dhcp_msg *pDHCPMSG; /* Buffer pointer for DHCP processing */ +char global_hostname[64]; +struct net_eth_addr global_eth_addr; /* DHCP Client MAC address. */ #define mem_encode(dst, obj) ({ \ typeof(obj) _obj = obj; \ @@ -423,7 +414,7 @@ static void dhcp_send_DECLINE(implements_net_udpsock *sock) { dhcp_msg_init(pDHCPMSG, &k); pDHCPMSG->flags = uint16be_marshal(0); - // Option Request Param. + /* Option Request Param. */ pDHCPMSG->options[k++] = DHCP_OPT_DHCP_MSG_TYPE; pDHCPMSG->options[k++] = 1; pDHCPMSG->options[k++] = DHCP_MSGTYP_DECLINE; @@ -558,17 +549,17 @@ static uint8_t dhcp_check_timeout(implements_net_udpsock *sock) { switch ( dhcp_state ) { case STATE_DHCP_DISCOVER : - //debugf("<<timeout>> state : STATE_DHCP_DISCOVER"); + /*debugf("<<timeout>> state : STATE_DHCP_DISCOVER");*/ dhcp_send_DISCOVER(sock); break; case STATE_DHCP_REQUEST : - //debugf("<<timeout>> state : STATE_DHCP_REQUEST"); + /*debugf("<<timeout>> state : STATE_DHCP_REQUEST");*/ dhcp_send_REQUEST(sock); break; case STATE_DHCP_REREQUEST : - //debugf("<<timeout>> state : STATE_DHCP_REREQUEST"); + /*debugf("<<timeout>> state : STATE_DHCP_REREQUEST");*/ dhcp_send_REQUEST(sock); break; @@ -580,7 +571,7 @@ static uint8_t dhcp_check_timeout(implements_net_udpsock *sock) { dhcp_tick_next = dhcp_tick_1s + DHCP_WAIT_TIME; dhcp_retry_count++; } - } else { // timeout occurred + } else { /* timeout occurred */ switch(dhcp_state) { case STATE_DHCP_DISCOVER: @@ -604,30 +595,30 @@ static int8_t dhcp_check_leasedIP(implements_net_udpsock *sock) { uint8_t tmp; int32_t ret; - //WIZchip RCR value changed for ARP Timeout count control + /* WIZchip RCR value changed for ARP Timeout count control */ tmp = getRCR(); setRCR(0x03); - // IP conflict detection : ARP request - ARP reply - // Broadcasting ARP Request for check the IP conflict using UDP sendto() function + /* IP conflict detection : ARP request - ARP reply */ + /* Broadcasting ARP Request for check the IP conflict using UDP sendto() function */ ret = sendto(sock, "CHECK_IP_CONFLICT", 17, global_lease.addr, 5000); - // RCR value restore + /* RCR value restore */ setRCR(tmp); if(ret == SOCKERR_TIMEOUT) { - // UDP send Timeout occurred : allocated IP address is unique, DHCP Success + /* UDP send Timeout occurred : allocated IP address is unique, DHCP Success */ debugf("\r\n> Check leased IP - OK"); return 1; } - // Received ARP reply or etc : IP address conflict occur, DHCP Failed + /* Received ARP reply or etc : IP address conflict occur, DHCP Failed */ dhcp_send_DECLINE(sock); ret = dhcp_tick_1s; - while((dhcp_tick_1s - ret) < 2) ; // wait for 1s over; wait to complete to send DECLINE message; + while ((dhcp_tick_1s - ret) < 2) {} /* wait for 1s over; wait to complete to send DECLINE message; */ return 0; } @@ -647,10 +638,7 @@ uint8_t DHCP_run(implements_net_udpsock *sock, dhcp_callback_t cb) { switch ( dhcp_state ) { case STATE_DHCP_INIT: - global_lease.addr.octets[0] = 0; - global_lease.addr.octets[1] = 0; - global_lease.addr.octets[2] = 0; - global_lease.addr.octets[3] = 0; + global_lease.addr = net_ip4_addr_zero; dhcp_send_DISCOVER(sock); dhcp_state = STATE_DHCP_DISCOVER; break; @@ -669,13 +657,13 @@ uint8_t DHCP_run(implements_net_udpsock *sock, dhcp_callback_t cb) { if (msg_type == DHCP_MSGTYP_ACK) { debugf("> Receive DHCP_ACK"); if (dhcp_check_leasedIP(sock)) { - // Network info assignment from DHCP + /* Network info assignment from DHCP */ cb(DHCP_ASSIGN, global_lease); dhcp_reset_timeout(); dhcp_state = STATE_DHCP_LEASED; } else { - // IP address conflict occurred + /* IP address conflict occurred */ dhcp_reset_timeout(); cb(DHCP_CONFLICT, global_lease); dhcp_state = STATE_DHCP_INIT; @@ -687,7 +675,7 @@ uint8_t DHCP_run(implements_net_udpsock *sock, dhcp_callback_t cb) { dhcp_state = STATE_DHCP_DISCOVER; } else - ret = dhcp_check_timeout(); + ret = dhcp_check_timeout(sock); break; case STATE_DHCP_LEASED : @@ -727,7 +715,7 @@ uint8_t DHCP_run(implements_net_udpsock *sock, dhcp_callback_t cb) { dhcp_state = STATE_DHCP_DISCOVER; } else - ret = check_DHCP_timeout(); + ret = dhcp_check_timeout(sock); break; default : break; @@ -779,7 +767,7 @@ void DHCP_init(void *buf) { global_eth_addr.octets[4] ^ global_eth_addr.octets[5]); - // WIZchip Netinfo Clear + /* WIZchip Netinfo Clear */ setSIPR(net_ip4_addr_zero); setGAR(net_ip4_addr_zero); |