dhcp.c 24 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
/**
******************************************************************************
* @file         dhcp.c                                         
* @version      V1.0                          
* @date         2018-06-18                 
* @brief        自动获取IP函数               
*
* @company      深圳炜世科技有限公司
* @information  WIZnet W5100S官方代理商,全程技术支持,价格优势大!
* @website      www.wisioe.com
* @forum        www.w5100s.com                                                                                                               
* @qqGroup      579842114                                                     
******************************************************************************
*/
  
#include <stdio.h>
#include <string.h>  
#include "w5100s.h"
#include "socket.h"
#include "dhcp.h"
#include "utility.h"
#include "w5100s_conf.h"

//#define DHCP_DEBUG
DHCP_Get DHCP_GET;
uint8*   SRC_MAC_ADDR   = EXTERN_DHCP_MAC;          // 本地MAC地址
uint8*   GET_SN_MASK    = EXTERN_DHCP_SN;           // 从DHCP服务器获取到的子网掩码
uint8*   GET_GW_IP      = EXTERN_DHCP_GW;           // 从DHCP服务器获取到的网关地址
uint8*   GET_DNS_IP     = EXTERN_DHCP_DNS;          // 从DHCP服务器获取到的DNS服务器地址
uint8*   DHCP_HOST_NAME = EXTERN_DHCP_NAME;         // 主机名
uint8*   GET_SIP        = EXTERN_DHCP_SIP;          // 从DHCP服务器获取到的本机IP
uint8    DHCP_SIP[4] = {0};                         // 已发现的DNS服务器地址
uint8    DHCP_REAL_SIP[4] = {0};                    // 从DHCP列表中选择使用的DHCP服务器
uint8    OLD_SIP[4];                                // 最初获取到的IP地址

uint8    dhcp_state       = STATE_DHCP_READY;       // DHCP客户端状态
uint8    dhcp_retry_count = 0;                      // DHCP重试次数
uint8    DHCP_timeout     = 0;                      // DHCP超时标志位
uint32   dhcp_lease_time;                           // 租约时间
uint32   next_dhcp_time   = 0;                      // DHCP超时时间
uint32   dhcp_tick_cnt    = 0;                       
uint8    DHCP_timer;

uint8    Conflict_flag = 0;
uint32   DHCP_XID        = DEFAULT_XID;        
uint8    EXTERN_DHCPBUF[1024];
RIP_MSG* pRIPMSG = (RIP_MSG*)EXTERN_DHCPBUF;        // DHCP消息指针

void  send_DHCP_DISCOVER(void);                     // 向DHCP服务器发送发现报文
void  send_DHCP_REQUEST(void);                      // 向DHCP服务器发送请求报文
void  send_DHCP_RELEASE_DECLINE(char msgtype);      // 向DHCP服务器发送释放报文

void  reset_DHCP_time(void);                        // 初始化DHCP计时器
void  DHCP_timer_handler(void);                     // DHCP计时器
void  check_DHCP_Timeout(void);                     // 检查是否超时
uint8 check_leasedIP(void);                         // 检查获取的IP是否冲突
uint8 parseDHCPMSG(uint16 length);                  // 从DHCP服务器接收消息并解析

/**
*@brief  DHCP定时初始化
*@param  无
*@return 无
*/
void reset_DHCP_time(void)
{
  dhcp_time = 0;
  dhcp_tick_cnt = 0;
  next_dhcp_time = dhcp_time + DHCP_WAIT_TIME;
  dhcp_retry_count = 0;
}

/**
*@brief  发送DISCOVER信息给DHCP服务器
*@param  无
*@return 无
*/
void send_DHCP_DISCOVER(void)
{
  uint8 ip[4] = {255,255,255,255};
  uint16 i=0;
  //the host name modified
  uint8 host_name[12];
  *((uint32*)DHCP_SIP)=0;
  *((uint32*)DHCP_REAL_SIP)=0;
  memset((void*)pRIPMSG,0,sizeof(RIP_MSG));         // 清空pRIPMSG的 sizeof(RIP_MSG) 个空间
  
  pRIPMSG->op = DHCP_BOOTREQUEST;
  pRIPMSG->htype = DHCP_HTYPE10MB;
  pRIPMSG->hlen = DHCP_HLENETHERNET;
  pRIPMSG->hops = DHCP_HOPS;
  pRIPMSG->xid = htonl(DHCP_XID);
  pRIPMSG->secs = htons(DHCP_SECS);
  pRIPMSG->flags =htons(DHCP_FLAGSBROADCAST);
  pRIPMSG->chaddr[0] = SRC_MAC_ADDR[0];
  pRIPMSG->chaddr[1] = SRC_MAC_ADDR[1];
  pRIPMSG->chaddr[2] = SRC_MAC_ADDR[2];
  pRIPMSG->chaddr[3] = SRC_MAC_ADDR[3];
  pRIPMSG->chaddr[4] = SRC_MAC_ADDR[4];
  pRIPMSG->chaddr[5] = SRC_MAC_ADDR[5];    
  
  
  //  MAGIC_COOKIE 
  pRIPMSG->OPT[i++] = (uint8)((MAGIC_COOKIE >> 24)& 0xFF);
  pRIPMSG->OPT[i++] = (uint8)((MAGIC_COOKIE >> 16)& 0xFF);
  pRIPMSG->OPT[i++] = (uint8)((MAGIC_COOKIE >> 8)& 0xFF);
  pRIPMSG->OPT[i++] = (uint8)(MAGIC_COOKIE& 0xFF);
  
  //  Option Request Param. 
  pRIPMSG->OPT[i++] = dhcpMessageType;
  pRIPMSG->OPT[i++] = 0x01;
  pRIPMSG->OPT[i++] = DHCP_DISCOVER;
  
  // Client identifier
  pRIPMSG->OPT[i++] = dhcpClientIdentifier;
  pRIPMSG->OPT[i++] = 0x07;
  pRIPMSG->OPT[i++] = 0x01;
  pRIPMSG->OPT[i++] = SRC_MAC_ADDR[0];
  pRIPMSG->OPT[i++] = SRC_MAC_ADDR[1];
  pRIPMSG->OPT[i++] = SRC_MAC_ADDR[2];
  pRIPMSG->OPT[i++] = SRC_MAC_ADDR[3];
  pRIPMSG->OPT[i++] = SRC_MAC_ADDR[4];
  pRIPMSG->OPT[i++] = SRC_MAC_ADDR[5];
  
  // host name
  pRIPMSG->OPT[i++] = hostName;
  // set the host name
  sprintf((char*)host_name,"%.4s-%02X%02X%02X",DEVICE_ID,SRC_MAC_ADDR[3],SRC_MAC_ADDR[4],SRC_MAC_ADDR[5]);

  pRIPMSG->OPT[i++] = (uint8)strlen((char*)host_name);
  
  strcpy((char*)(&(pRIPMSG->OPT[i])),(char*)host_name);
  
  i+=(uint16)strlen((char*)host_name);  
  
  pRIPMSG->OPT[i++] = dhcpParamRequest;
  pRIPMSG->OPT[i++] = 0x06;
  pRIPMSG->OPT[i++] = subnetMask;
  pRIPMSG->OPT[i++] = routersOnSubnet;
  pRIPMSG->OPT[i++] = dns;
  pRIPMSG->OPT[i++] = domainName;
  pRIPMSG->OPT[i++] = dhcpT1value;
  pRIPMSG->OPT[i++] = dhcpT2value;
  pRIPMSG->OPT[i++] = endOption;
  
  //send broadcasting packet 
  //printf(" send dhcp discover %s\r\n",EXTERN_DHCPBUF);
  //for(uint8 i=0; i<3; i++)
  //Delay_ms(800);
  sendto(SOCK_DHCP, (uint8 *)pRIPMSG, sizeof(RIP_MSG), ip, DHCP_SERVER_PORT);
  #ifdef DHCP_DEBUG  
  printf(" sent DHCP_DISCOVER\r\n");
  #endif  
}

/**
*@brief  将请求消息发送到DHCP服务器
*@param  无
*@return 无
*/
void send_DHCP_REQUEST(void)
{
  uint8  ip[4];
  uint16 i = 0;
  //uint16 len=0;
  uint8  host_name[12];
  
  memset((void*)pRIPMSG,0,sizeof(RIP_MSG));
  
  pRIPMSG->op = DHCP_BOOTREQUEST;
  pRIPMSG->htype = DHCP_HTYPE10MB;
  pRIPMSG->hlen = DHCP_HLENETHERNET;
  pRIPMSG->hops = DHCP_HOPS;
  pRIPMSG->xid = htonl(DHCP_XID);
  pRIPMSG->secs = htons(DHCP_SECS);
  
  if(dhcp_state < STATE_DHCP_LEASED)
    pRIPMSG->flags = htons(DHCP_FLAGSBROADCAST);
  else
  {
    pRIPMSG->flags = 0;                               // For Unicast
    memcpy(pRIPMSG->ciaddr,GET_SIP,4);
  }    
  
  memcpy(pRIPMSG->chaddr,SRC_MAC_ADDR,6);
  
  //  MAGIC_COOKIE 
  pRIPMSG->OPT[i++] = (uint8)((MAGIC_COOKIE >> 24) & 0xFF);
  pRIPMSG->OPT[i++] = (uint8)((MAGIC_COOKIE >> 16) & 0xFF);
  pRIPMSG->OPT[i++] = (uint8)((MAGIC_COOKIE >> 8) & 0xFF);
  pRIPMSG->OPT[i++] = (uint8)(MAGIC_COOKIE & 0xFF);
  
  //  Option Request Param. 
  pRIPMSG->OPT[i++] = dhcpMessageType;
  pRIPMSG->OPT[i++] = 0x01;
  pRIPMSG->OPT[i++] = DHCP_REQUEST;
  
  pRIPMSG->OPT[i++] = dhcpClientIdentifier;
  pRIPMSG->OPT[i++] = 0x07;
  pRIPMSG->OPT[i++] = 0x01;
  pRIPMSG->OPT[i++] = SRC_MAC_ADDR[0];
  pRIPMSG->OPT[i++] = SRC_MAC_ADDR[1];
  pRIPMSG->OPT[i++] = SRC_MAC_ADDR[2];
  pRIPMSG->OPT[i++] = SRC_MAC_ADDR[3];
  pRIPMSG->OPT[i++] = SRC_MAC_ADDR[4];
  pRIPMSG->OPT[i++] = SRC_MAC_ADDR[5];  
  
  if(dhcp_state < STATE_DHCP_LEASED)
  {
    pRIPMSG->OPT[i++] = dhcpRequestedIPaddr;
    pRIPMSG->OPT[i++] = 0x04;
    pRIPMSG->OPT[i++] = GET_SIP[0];
    pRIPMSG->OPT[i++] = GET_SIP[1];
    pRIPMSG->OPT[i++] = GET_SIP[2];
    pRIPMSG->OPT[i++] = GET_SIP[3];
  
    pRIPMSG->OPT[i++] = dhcpServerIdentifier;
    pRIPMSG->OPT[i++] = 0x04;
    pRIPMSG->OPT[i++] = DHCP_SIP[0];
    pRIPMSG->OPT[i++] = DHCP_SIP[1];
    pRIPMSG->OPT[i++] = DHCP_SIP[2];
    pRIPMSG->OPT[i++] = DHCP_SIP[3];
  }
  
  // host name
  pRIPMSG->OPT[i++] = hostName;
  //set the host name
  sprintf((char*)host_name,(char*)"%.4s-%02X%02X%02X",DEVICE_ID,SRC_MAC_ADDR[3],SRC_MAC_ADDR[4],SRC_MAC_ADDR[5]);
  pRIPMSG->OPT[i++] = (uint8)strlen((char*)host_name);  
  strcpy((char*)&(pRIPMSG->OPT[i]),(char*)host_name);
  i+=strlen((char*)host_name);
    
  pRIPMSG->OPT[i++] = dhcpParamRequest;
  pRIPMSG->OPT[i++] = 0x08;
  pRIPMSG->OPT[i++] = subnetMask;
  pRIPMSG->OPT[i++] = routersOnSubnet;
  pRIPMSG->OPT[i++] = dns;
  pRIPMSG->OPT[i++] = domainName;
  pRIPMSG->OPT[i++] = dhcpT1value;
  pRIPMSG->OPT[i++] = dhcpT2value;
  pRIPMSG->OPT[i++] = performRouterDiscovery;
  pRIPMSG->OPT[i++] = staticRoute;
  pRIPMSG->OPT[i++] = endOption;
  
  //  send broadcasting packet 
  if(dhcp_state < STATE_DHCP_LEASED) memset(ip,0xFF,4);
  else 
  memcpy(ip,DHCP_SIP,4);
  sendto(SOCK_DHCP, (uint8 *)pRIPMSG, sizeof(RIP_MSG), ip, DHCP_SERVER_PORT);
#ifdef DHCP_DEBUG
  printf(" sent DHCP_REQUEST\r\n");
#endif
}

/**
*@brief  发送释放消息
*@param  msgtype:是否为7
*@return 无
*/
void send_DHCP_RELEASE_DECLINE(char msgtype)
{
  uint16 i =0;
  uint8  ip[4];
  
  memset((void*)pRIPMSG,0,sizeof(RIP_MSG));
  
  pRIPMSG->op = DHCP_BOOTREQUEST;
  pRIPMSG->htype = DHCP_HTYPE10MB;
  pRIPMSG->hlen = DHCP_HLENETHERNET;
  pRIPMSG->hops = DHCP_HOPS;
  pRIPMSG->xid = htonl(DHCP_XID);
  pRIPMSG->secs = htons(DHCP_SECS);
  pRIPMSG->flags =0;// DHCP_FLAGSBROADCAST;
  
  memcpy(pRIPMSG->chaddr,SRC_MAC_ADDR,6);
  
  //  MAGIC_COOKIE 
  pRIPMSG->OPT[i++] = (uint8)((MAGIC_COOKIE >> 24) & 0xFF);
  pRIPMSG->OPT[i++] = (uint8)((MAGIC_COOKIE >> 16) & 0xFF);
  pRIPMSG->OPT[i++] = (uint8)((MAGIC_COOKIE >> 8) & 0xFF);
  pRIPMSG->OPT[i++] = (uint8)(MAGIC_COOKIE & 0xFF);
  
  //  Option Request Param. 
  pRIPMSG->OPT[i++] = dhcpMessageType;
  pRIPMSG->OPT[i++] = 0x01;
  pRIPMSG->OPT[i++] = ((!msgtype) ? DHCP_RELEASE : DHCP_DECLINE);
  
  pRIPMSG->OPT[i++] = dhcpClientIdentifier;
  pRIPMSG->OPT[i++] = 0x07;
  pRIPMSG->OPT[i++] = 0x01;
  pRIPMSG->OPT[i++] = SRC_MAC_ADDR[0];
  pRIPMSG->OPT[i++] = SRC_MAC_ADDR[1];
  pRIPMSG->OPT[i++] = SRC_MAC_ADDR[2];
  pRIPMSG->OPT[i++] = SRC_MAC_ADDR[3];
  pRIPMSG->OPT[i++] = SRC_MAC_ADDR[4];
  pRIPMSG->OPT[i++] = SRC_MAC_ADDR[5];  
  
  pRIPMSG->OPT[i++] = dhcpServerIdentifier;
  pRIPMSG->OPT[i++] = 0x04;
  pRIPMSG->OPT[i++] = DHCP_SIP[0];
  pRIPMSG->OPT[i++] = DHCP_SIP[1];
  pRIPMSG->OPT[i++] = DHCP_SIP[2];
  pRIPMSG->OPT[i++] = DHCP_SIP[3];
  
  if(msgtype)
  {
  pRIPMSG->OPT[i++] = dhcpRequestedIPaddr;
  pRIPMSG->OPT[i++] = 0x04;
  pRIPMSG->OPT[i++] = GET_SIP[0];
  pRIPMSG->OPT[i++] = GET_SIP[1];
  pRIPMSG->OPT[i++] = GET_SIP[2];
  pRIPMSG->OPT[i++] = GET_SIP[3];    
  #ifdef DHCP_DEBUG    
  printf(" sent DHCP_DECLINE\r\n");
  #endif    
  }
  #ifdef DHCP_DEBUG  
  else
  {
  printf(" sent DHCP_RELEASE\r\n");
  }
  #endif  
  
  pRIPMSG->OPT[i++] = endOption;  
  
  
  if(msgtype) memset(ip,0xFF,4);
  else 
  memcpy(ip,DHCP_SIP,4);
  // printf(" send dhcp decline\r\n");
  sendto(SOCK_DHCP, (uint8 *)pRIPMSG, sizeof(RIP_MSG), ip, DHCP_SERVER_PORT);
}

/**
*@brief  解析获得的DHCP消息
*@param  length:解析消息长度
*@return 0:解析失败  其他:解析成功
*/
uint8 parseDHCPMSG(uint16 length)
{
  uint8  svr_addr[6];
  uint16 svr_port;
  uint16 len;
  uint8  * p;
  uint8  * e;
  uint8  type;
  uint8  opt_len = 0;
  
  len = recvfrom(SOCK_DHCP, (uint8 *)pRIPMSG, length, svr_addr, &svr_port);
  
  #ifdef DHCP_DEBUG
    printf(" DHCP_SIP:%u.%u.%u.%u\r\n",DHCP_SIP[0],DHCP_SIP[1],DHCP_SIP[2],DHCP_SIP[3]);
    printf(" DHCP_RIP:%u.%u.%u.%u\r\n",DHCP_REAL_SIP[0],DHCP_REAL_SIP[1],DHCP_REAL_SIP[2],DHCP_REAL_SIP[3]);
    printf(" svr_addr:%u.%u.%u.%u\r\n",svr_addr[0],svr_addr[1],svr_addr[2],svr_addr[3]);
  #endif  
  
  if(pRIPMSG->op != DHCP_BOOTREPLY || svr_port != DHCP_SERVER_PORT)
  {
    #ifdef DHCP_DEBUG     
    printf(" DHCP : NO DHCP MSG\r\n");
    #endif    
    return 0;
  }
  if(memcmp(pRIPMSG->chaddr,SRC_MAC_ADDR,6) != 0 || pRIPMSG->xid != htonl(DHCP_XID))
  {
    #ifdef DHCP_DEBUG
    printf(" No My DHCP Message. This message is ignored.\r\n");
    printf(" \tSRC_MAC_ADDR(%02X.%02X.%02X.",SRC_MAC_ADDR[0],SRC_MAC_ADDR[1],SRC_MAC_ADDR[2]);
    printf(" %02X.%02X.%02X)",SRC_MAC_ADDR[3],SRC_MAC_ADDR[4],SRC_MAC_ADDR[5]);
    printf(" , pRIPMSG->chaddr(%02X.%02X.%02X.",(char)pRIPMSG->chaddr[0],(char)pRIPMSG->chaddr[1],(char)pRIPMSG->chaddr[2]);
    printf(" %02X.%02X.%02X)",pRIPMSG->chaddr[3],pRIPMSG->chaddr[4],pRIPMSG->chaddr[5]);
    printf(" \tpRIPMSG->xid(%08X), DHCP_XID(%08X)",pRIPMSG->xid,(DHCP_XID));
    printf(" \tpRIMPMSG->yiaddr:%d.%d.%d.%d\r\n",pRIPMSG->yiaddr[0],pRIPMSG->yiaddr[1],pRIPMSG->yiaddr[2],pRIPMSG->yiaddr[3]);
    #endif        
    return 0;
  }
  
  if( *((uint32*)DHCP_SIP) != 0x00000000 )
  {
    if( *((uint32*)DHCP_REAL_SIP) != *((uint32*)svr_addr) && 
    *((uint32*)DHCP_SIP) != *((uint32*)svr_addr) ) 
    {
      #ifdef DHCP_DEBUG    
      printf(" Another DHCP sever send a response message. This is ignored.\r\n");
      printf(" \tIP:%u.%u.%u.%u\r\n",svr_addr[0],svr_addr[1],svr_addr[2],svr_addr[3]);
      #endif        
      return 0;                
    }
  }  
  memcpy(GET_SIP,pRIPMSG->yiaddr,4);
  
  #ifdef DHCP_DEBUG  
  printf(" DHCP MSG received\r\n");
  printf(" yiaddr : %u.%u.%u.%u\r\n",GET_SIP[0],GET_SIP[1],GET_SIP[2],GET_SIP[3]);
  #endif  
  type = 0;
  p = (uint8 *)(&pRIPMSG->op);
  p = p + 240;
  e = p + (len - 240);
  #ifdef DHCP_DEBUG
  printf(" p : %08X  e : %08X  len : %d\r\n", (uint32)p, (uint32)e, len);
  #endif
  while ( p < e ) 
  {
    switch ( *p++ ) 
    {
      case endOption :
        return type;
    
      case padOption :
        break;
      
      case dhcpMessageType :
        opt_len = *p++;
        type = *p;
        #ifdef DHCP_DEBUG      
        printf(" dhcpMessageType : %02X\r\n", type);
        #endif      
        break;
      
      case subnetMask :
        opt_len =* p++;
        memcpy(GET_SN_MASK,p,4);
        #ifdef DHCP_DEBUG
        printf(" subnetMask : ");
        printf(" %u.%u.%u.%u\r\n",GET_SN_MASK[0],GET_SN_MASK[1],GET_SN_MASK[2],GET_SN_MASK[3]);
        #endif      
        break;
      
      case routersOnSubnet :
        opt_len = *p++;
        memcpy(GET_GW_IP,p,4);
        #ifdef DHCP_DEBUG
        printf(" routersOnSubnet : ");
        printf(" %u.%u.%u.%u\r\n",GET_GW_IP[0],GET_GW_IP[1],GET_GW_IP[2],GET_GW_IP[3]);
        #endif      
        break;
      
      case dns :
        opt_len = *p++;
        memcpy(GET_DNS_IP,p,4);
        break;
      
      case dhcpIPaddrLeaseTime :
        opt_len = *p++;
        dhcp_lease_time = ntohl(*((uint32*)p));
        #ifdef DHCP_DEBUG      
        printf(" dhcpIPaddrLeaseTime : %d\r\n", dhcp_lease_time);
        #endif      
        break;
      
      case dhcpServerIdentifier :
        opt_len = *p++;
        #ifdef DHCP_DEBUG            
        printf(" DHCP_SIP : %u.%u.%u.%u\r\n", DHCP_SIP[0], DHCP_SIP[1], DHCP_SIP[2], DHCP_SIP[3]);
        #endif      
        if( *((uint32*)DHCP_SIP) == 0 || 
        *((uint32*)DHCP_REAL_SIP) == *((uint32*)svr_addr) || 
        *((uint32*)DHCP_SIP) == *((uint32*)svr_addr) )
        {
          memcpy(DHCP_SIP,p,4);
          memcpy(DHCP_REAL_SIP,svr_addr,4);  // Copy the real ip address of my DHCP server
          #ifdef DHCP_DEBUG            
          printf(" My dhcpServerIdentifier : ");
          printf(" %u.%u.%u.%u\r\n", DHCP_SIP[0], DHCP_SIP[1], DHCP_SIP[2], DHCP_SIP[3]);
          printf(" My DHCP server real IP address : ");
          printf(" %u.%u.%u.%u\r\n", DHCP_REAL_SIP[0], DHCP_REAL_SIP[1], DHCP_REAL_SIP[2], DHCP_REAL_SIP[3]);
          #endif            
        }
        else
        {
        #ifdef DHCP_DEBUG         
        printf(" Another dhcpServerIdentifier : \r\n");
        printf(" \tMY(%u.%u.%u.%u) ", DHCP_SIP[0], DHCP_SIP[1], DHCP_SIP[2], DHCP_SIP[3]);
        printf(" Another(%u.%u.%u.%u)\r\n", svr_addr[0], svr_addr[1], svr_addr[2], svr_addr[3]);
        #endif        
        }
        break;
        
      default :
        opt_len = *p++;
        #ifdef DHCP_DEBUG      
        printf(" opt_len : %u\r\n", opt_len);
        #endif      
        break;
    } // switch
    p+=opt_len;
  } // while  
  return 0;
}

/**
*@brief  检查DHCP状态
*@param  s: socket数
*@return 返回得到的DHCP状态
*        DHCP_RET_NONE: 未获取到IP地址
*        DHCP_RET_TIMEOUT:超时
*        DHCP_RET_UPDATE: 获取成功
*        DHCP_RET_CONFLICT:IP地址冲突
*/
uint8 check_DHCP_state(SOCKET s) 
{
  uint16 len;                                 // 定义一个表示接收数据大小变量
  uint8  type;                                // 定义一个表示接收封包类型变量
  
  type = 0;
  if(getSn_SR(s)!=SOCK_CLOSED)                // socket处于打开状态
  {
    if ((len = getSn_RX_RSR(s)) > 0)          // 接收到数据
    {
      type = parseDHCPMSG(len);               // 解析收到的封包类型
    }
  }
  else                                        // socket处于关闭状态,重新初始化socket
  {
    if(dhcp_state == STATE_DHCP_READY)
    {
      #ifdef DHCP_DEBUG      
      printf(" state : STATE_DHCP_READY\r\n");
      #endif       
    }  
    if(!socket(SOCK_DHCP,Sn_MR_UDP,DHCP_CLIENT_PORT,0x00))          // 初始化socket和端口
    {
      #ifdef DHCP_DEBUG     
      printf(" Fail to create the DHCPC_SOCK(%u)\r\n",SOCK_DHCP);
      #endif   
      return DHCP_RET_ERR;                    // socket初始化错误
    }
  }
  switch ( dhcp_state )
  {
    case STATE_DHCP_READY:                    // DHCP初始化状态
      DHCP_timeout = 0;                       // DHCP超时标志设置为1
      reset_DHCP_time();                      // 复位超时时间
      send_DHCP_DISCOVER();                   // 发送DISCOVER包
  
      DHCP_timer = 0;                         // set_timer0(DHCP_timer_handler);     
      dhcp_state = STATE_DHCP_DISCOVER;       // DHCP的DISCOVER状态
      break;   
  
    case STATE_DHCP_DISCOVER:
      if (type == DHCP_OFFER) 
      {
        reset_DHCP_time();                    // 复位超时时间
        send_DHCP_REQUEST();                  // 发送REQUEST包
        dhcp_state = STATE_DHCP_REQUEST;
        #ifdef DHCP_DEBUG      
        printf(" state : STATE_DHCP_REQUEST\r\n");
        #endif      
      }
      else 
        check_DHCP_Timeout();                 // 检查是否超时
      break;
    
    case STATE_DHCP_REQUEST :                 // DHCP的REQUEST状态
      if (type == DHCP_ACK)                   // 接收到DHCP服务器回应的off封包
      {
        reset_DHCP_time();
        if (check_leasedIP()) 
        {
          #ifdef DHCP_DEBUG          
          printf(" state : STATE_DHCP_LEASED\r\n");
          #endif    
          dhcp_state = STATE_DHCP_LEASED;
          return DHCP_RET_UPDATE;
        } 
        else 
        {
          #ifdef DHCP_DEBUG          
          printf(" state : STATE_DHCP_DISCOVER\r\n");
          #endif        
          dhcp_state = STATE_DHCP_DISCOVER;
          return DHCP_RET_CONFLICT;
        }
      }  
      else if (type == DHCP_NAK) 
      {
        reset_DHCP_time();                    // 复位超时时间
        dhcp_state = STATE_DHCP_DISCOVER;
        #ifdef DHCP_DEBUG        
        printf(" state : STATE_DHCP_DISCOVER\r\n");
        #endif      
      }
      else 
        check_DHCP_Timeout();                 // 检查是否超时
      break;
      
    case STATE_DHCP_LEASED :
      if ((dhcp_lease_time != 0xffffffff) && (dhcp_time>(dhcp_lease_time/2))) 
      {
        type = 0;
        memcpy(OLD_SIP,GET_SIP,4);
        DHCP_XID++;
        send_DHCP_REQUEST();
        dhcp_state = STATE_DHCP_REREQUEST;
        #ifdef DHCP_DEBUG
        printf(" state : STATE_DHCP_REREQUEST\r\n");
        #endif
        reset_DHCP_time();
      }
      break;
    case STATE_DHCP_REREQUEST :
      if (type == DHCP_ACK) 
      {
        if(memcmp(OLD_SIP,GET_SIP,4)!=0)  
        {
          #ifdef DHCP_DEBUG
          printf(" The IP address from the DHCP server is updated.\r\n");
          printf(" OLD_SIP=%u.%u.%u.%u",OLD_SIP[0],OLD_SIP[1],OLD_SIP[2],OLD_SIP[3]);
          printf(" GET_SIP=%u.%u.%u.%u\r\n",GET_SIP[0],GET_SIP[1],GET_SIP[2],GET_SIP[3]);
          #endif
          return DHCP_RET_UPDATE;
        }
        #ifdef DHCP_DEBUG
        else
        {
          printf(" state : STATE_DHCP_LEASED : same IP\r\n");
        }
        #endif
        reset_DHCP_time();
        dhcp_state = STATE_DHCP_LEASED;
      } 
      else if (type == DHCP_NAK) 
      {
        reset_DHCP_time();
        dhcp_state = STATE_DHCP_DISCOVER;
        #ifdef DHCP_DEBUG
        printf(" state : STATE_DHCP_DISCOVER\r\n");
        #endif
      } 
      else 
        check_DHCP_Timeout();
      break;
      
      default :
        dhcp_state = STATE_DHCP_READY;
        break;
  }
  if (DHCP_timeout == 1)
  {
    //kill_timer0(DHCP_timer);
    dhcp_state = STATE_DHCP_READY;
    return DHCP_RET_TIMEOUT;
  }
  return DHCP_RET_NONE;
}

/**
*@brief  检查DHCP请求释放超时
*@param  无
*@return 无
*/
void check_DHCP_Timeout(void)
{
  if(dhcp_retry_count < MAX_DHCP_RETRY) 
  {
    if(dhcp_time > next_dhcp_time) 
    {
      dhcp_time = 0;
      next_dhcp_time = dhcp_time + DHCP_WAIT_TIME;
      dhcp_retry_count++;
      switch ( dhcp_state ) 
      {
        case STATE_DHCP_DISCOVER :
          #ifdef DHCP_DEBUG         
          printf(" <<timeout>> state : STATE_DHCP_DISCOVER\r\n");
          #endif        
          send_DHCP_DISCOVER();
          break;
        
        case STATE_DHCP_REQUEST :
          #ifdef DHCP_DEBUG         
          printf(" <<timeout>> state : STATE_DHCP_REQUEST\r\n");
          #endif        
          send_DHCP_REQUEST();
          break;    
        
        case STATE_DHCP_REREQUEST :
          #ifdef DHCP_DEBUG         
          printf(" <<timeout>> state : STATE_DHCP_REREQUEST\r\n");
          #endif        
          send_DHCP_REQUEST();
          break;
        
        default :
          break;
      }
    }
  } 
  else 
  {
    reset_DHCP_time();
    DHCP_timeout = 1;
  
    send_DHCP_DISCOVER();
    dhcp_state = STATE_DHCP_DISCOVER;
    #ifdef DHCP_DEBUG    
    printf(" timeout : state : STATE_DHCP_DISCOVER\r\n");
    #endif    
  }
}

/**
*@brief  检查获取的IP是否冲突
*@param  无
*@return 无
*/
uint8 check_leasedIP(void)
{
  uint8_t tmp;
  int32_t ret;

  //WIZchip RCR value changed for ARP Timeout count control
  tmp = getRCR();
  setRCR(0x03);

  // IP conflict detection : ARP request - ARP reply
    
  ret = sendto(SOCK_DHCP, (uint8_t *)"CHECK_IP_CONFLICT", 17, GET_SIP, 5000);

  // RCR value restore
  setRCR(tmp);

  if(ret == SOCKERR_TIMEOUT) {
    // UDP send Timeout occurred : allocated IP address is unique, DHCP Success

#ifdef _DHCP_DEBUG_
    printf("\r\n> Check leased IP - OK\r\n");
#endif

    return 1;
  }
  else
    return 0;
}
  
/**
*@brief  DHCP 定时器操作
*@param  无
*@return 无
*/
void DHCP_timer_handler(void)
{
  if(dhcp_tick_cnt++ > 1000)
  {
    dhcp_tick_cnt = 0;
    dhcp_time++;
  }
}

/**   
*@brief  初始化DHCP客户端
*@param  无
*@return 无
*/
void init_dhcp_client(void)
{
  uint8 ip_0[4]={0,};
  DHCP_XID = 0x12345678;
  memset(OLD_SIP,0,sizeof(OLD_SIP));
  memset(DHCP_SIP,0,sizeof(DHCP_SIP));
  memset(DHCP_REAL_SIP,0,sizeof(GET_SN_MASK));
  
  // clear ip setted flag 
  iinchip_init();
  setSUBR(ip_0);
  setGAR(ip_0);
  setSIPR(ip_0);
  printf(" mac=%02x:%02x:%02x:%02x:%02x:%02x\r\n",SRC_MAC_ADDR[0],SRC_MAC_ADDR[1],SRC_MAC_ADDR[2],SRC_MAC_ADDR[3],SRC_MAC_ADDR[4],SRC_MAC_ADDR[5]);  
  
  dhcp_state = STATE_DHCP_READY;
  #ifdef DHCP_DEBUG
  printf(" init_dhcp_client:%u\r\n",SOCK_DHCP);
  #endif   
}
/**
*@brief  执行DHCP Client
*@param  无
*@return 无
*/
void do_dhcp(void)
{
  uint8 dhcpret=0;
  ip_from=IP_FROM_DHCP;                                // IP配置方法选择为DHCP
  dhcp_timer_init();                                   // 初始化DHCP定时器
  if(Conflict_flag == 1)
  {
    init_dhcp_client();                                // 初始化DHCP客户端 
    Conflict_flag =0;
  }
  
  dhcpret = check_DHCP_state(SOCK_DHCP);               // 获取DHCP服务状态
  
  switch(dhcpret)
  {
    case DHCP_RET_NONE:                                // IP地址获取不成功 
//      printf(" IP地址获取不成功\r\n");
      break;
    
    case DHCP_RET_TIMEOUT:                             // IP地址获取超时 
//      printf(" IP地址获取超时\r\n");
      break;
    
    case DHCP_RET_UPDATE:                              // 成功获取到IP地址  
      dhcp_ok=1; 
      set_w5100s_netinfo();                            // 将获取到的IP地址写入W5100S寄存器
      printf(" 已从DHCP服务器成功获得IP地址\r\n");
      
      break;
    
    case DHCP_RET_CONFLICT:                            // IP地址获取冲突 
      printf(" IP地址获取冲突\r\n");
      dhcp_state = STATE_DHCP_READY; 
      printf(" 重试中\r\n");
      dhcp_ok=0; 
      break;     

    default:
      break;
  }
}

/**
*@brief    This function is to get the Max size to receive.
*@param    s: socket number
*@return  This function return the RX buffer size has been setted
*/
void iinchip_init(void)
{
  setMR( MR_RST );
  #ifdef __DEF_IINCHIP_DBG__
    printf(" MR value is %02x \r\n",IINCHIP_READ_COMMON(MR));
  #endif
}