dhcp.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. //*****************************************************************************
  2. //
  3. //! \file dhcp.c
  4. //! \brief DHCP APIs implement file.
  5. //! \details Processing DHCP protocol as DISCOVER, OFFER, REQUEST, ACK, NACK and DECLINE.
  6. //! \version 1.1.1
  7. //! \date 2019/10/08
  8. //! \par Revision history
  9. //! <2019/10/08> compare DHCP server ip address
  10. //! <2013/11/18> 1st Release
  11. //! <2012/12/20> V1.1.0
  12. //! 1. Optimize code
  13. //! 2. Add reg_dhcp_cbfunc()
  14. //! 3. Add DHCP_stop()
  15. //! 4. Integrate check_DHCP_state() & DHCP_run() to DHCP_run()
  16. //! 5. Don't care system endian
  17. //! 6. Add comments
  18. //! <2012/12/26> V1.1.1
  19. //! 1. Modify variable declaration: dhcp_tick_1s is declared volatile for code optimization
  20. //! \author Eric Jung & MidnightCow
  21. //! \copyright
  22. //!
  23. //! Copyright (c) 2013, WIZnet Co., LTD.
  24. //! All rights reserved.
  25. //!
  26. //! Redistribution and use in source and binary forms, with or without
  27. //! modification, are permitted provided that the following conditions
  28. //! are met:
  29. //!
  30. //! * Redistributions of source code must retain the above copyright
  31. //! notice, this list of conditions and the following disclaimer.
  32. //! * Redistributions in binary form must reproduce the above copyright
  33. //! notice, this list of conditions and the following disclaimer in the
  34. //! documentation and/or other materials provided with the distribution.
  35. //! * Neither the name of the <ORGANIZATION> nor the names of its
  36. //! contributors may be used to endorse or promote products derived
  37. //! from this software without specific prior written permission.
  38. //!
  39. //! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  40. //! AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. //! IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  42. //! ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  43. //! LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  44. //! CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  45. //! SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  46. //! INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  47. //! CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  48. //! ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  49. //! THE POSSIBILITY OF SUCH DAMAGE.
  50. //
  51. //*****************************************************************************
  52. #include "socket.h"
  53. #include "dhcp.h"
  54. /* If you want to display debug & processing message, Define _DHCP_DEBUG_ in dhcp.h */
  55. #ifdef _DHCP_DEBUG_
  56. #include <stdio.h>
  57. #endif
  58. #define _DHCP_DEBUG_
  59. /* DHCP state machine. */
  60. #define STATE_DHCP_INIT 0 ///< Initialize
  61. #define STATE_DHCP_DISCOVER 1 ///< send DISCOVER and wait OFFER
  62. #define STATE_DHCP_REQUEST 2 ///< send REQEUST and wait ACK or NACK
  63. #define STATE_DHCP_LEASED 3 ///< ReceiveD ACK and IP leased
  64. #define STATE_DHCP_REREQUEST 4 ///< send REQUEST for maintaining leased IP
  65. #define STATE_DHCP_RELEASE 5 ///< No use
  66. #define STATE_DHCP_STOP 6 ///< Stop processing DHCP
  67. #define DHCP_FLAGSBROADCAST 0x8000 ///< The broadcast value of flags in @ref RIP_MSG
  68. #define DHCP_FLAGSUNICAST 0x0000 ///< The unicast value of flags in @ref RIP_MSG
  69. /* DHCP message OP code */
  70. #define DHCP_BOOTREQUEST 1 ///< Request Message used in op of @ref RIP_MSG
  71. #define DHCP_BOOTREPLY 2 ///< Reply Message used i op of @ref RIP_MSG
  72. /* DHCP message type */
  73. #define DHCP_DISCOVER 1 ///< DISCOVER message in OPT of @ref RIP_MSG
  74. #define DHCP_OFFER 2 ///< OFFER message in OPT of @ref RIP_MSG
  75. #define DHCP_REQUEST 3 ///< REQUEST message in OPT of @ref RIP_MSG
  76. #define DHCP_DECLINE 4 ///< DECLINE message in OPT of @ref RIP_MSG
  77. #define DHCP_ACK 5 ///< ACK message in OPT of @ref RIP_MSG
  78. #define DHCP_NAK 6 ///< NACK message in OPT of @ref RIP_MSG
  79. #define DHCP_RELEASE 7 ///< RELEASE message in OPT of @ref RIP_MSG. No use
  80. #define DHCP_INFORM 8 ///< INFORM message in OPT of @ref RIP_MSG. No use
  81. #define DHCP_HTYPE10MB 1 ///< Used in type of @ref RIP_MSG
  82. #define DHCP_HTYPE100MB 2 ///< Used in type of @ref RIP_MSG
  83. #define DHCP_HLENETHERNET 6 ///< Used in hlen of @ref RIP_MSG
  84. #define DHCP_HOPS 0 ///< Used in hops of @ref RIP_MSG
  85. #define DHCP_SECS 0 ///< Used in secs of @ref RIP_MSG
  86. #define INFINITE_LEASETIME 0xffffffff ///< Infinite lease time
  87. #define OPT_SIZE 312 /// Max OPT size of @ref RIP_MSG
  88. #define RIP_MSG_SIZE (236 + OPT_SIZE) /// Max size of @ref RIP_MSG
  89. /*
  90. * @brief DHCP option and value (cf. RFC1533)
  91. */
  92. enum {
  93. padOption = 0,
  94. subnetMask = 1,
  95. timerOffset = 2,
  96. routersOnSubnet = 3,
  97. timeServer = 4,
  98. nameServer = 5,
  99. dns = 6,
  100. logServer = 7,
  101. cookieServer = 8,
  102. lprServer = 9,
  103. impressServer = 10,
  104. resourceLocationServer = 11,
  105. hostName = 12,
  106. bootFileSize = 13,
  107. meritDumpFile = 14,
  108. domainName = 15,
  109. swapServer = 16,
  110. rootPath = 17,
  111. extentionsPath = 18,
  112. IPforwarding = 19,
  113. nonLocalSourceRouting = 20,
  114. policyFilter = 21,
  115. maxDgramReasmSize = 22,
  116. defaultIPTTL = 23,
  117. pathMTUagingTimeout = 24,
  118. pathMTUplateauTable = 25,
  119. ifMTU = 26,
  120. allSubnetsLocal = 27,
  121. broadcastAddr = 28,
  122. performMaskDiscovery = 29,
  123. maskSupplier = 30,
  124. performRouterDiscovery = 31,
  125. routerSolicitationAddr = 32,
  126. staticRoute = 33,
  127. trailerEncapsulation = 34,
  128. arpCacheTimeout = 35,
  129. ethernetEncapsulation = 36,
  130. tcpDefaultTTL = 37,
  131. tcpKeepaliveInterval = 38,
  132. tcpKeepaliveGarbage = 39,
  133. nisDomainName = 40,
  134. nisServers = 41,
  135. ntpServers = 42,
  136. vendorSpecificInfo = 43,
  137. netBIOSnameServer = 44,
  138. netBIOSdgramDistServer = 45,
  139. netBIOSnodeType = 46,
  140. netBIOSscope = 47,
  141. xFontServer = 48,
  142. xDisplayManager = 49,
  143. dhcpRequestedIPaddr = 50,
  144. dhcpIPaddrLeaseTime = 51,
  145. dhcpOptionOverload = 52,
  146. dhcpMessageType = 53,
  147. dhcpServerIdentifier = 54,
  148. dhcpParamRequest = 55,
  149. dhcpMsg = 56,
  150. dhcpMaxMsgSize = 57,
  151. dhcpT1value = 58,
  152. dhcpT2value = 59,
  153. dhcpClassIdentifier = 60,
  154. dhcpClientIdentifier = 61,
  155. endOption = 255
  156. };
  157. /*
  158. * @brief DHCP message format
  159. */
  160. typedef struct {
  161. uint8_t op; ///< @ref DHCP_BOOTREQUEST or @ref DHCP_BOOTREPLY
  162. uint8_t htype; ///< @ref DHCP_HTYPE10MB or @ref DHCP_HTYPE100MB
  163. uint8_t hlen; ///< @ref DHCP_HLENETHERNET
  164. uint8_t hops; ///< @ref DHCP_HOPS
  165. uint32_t xid; ///< @ref DHCP_XID This increase one every DHCP transaction.
  166. uint16_t secs; ///< @ref DHCP_SECS
  167. uint16_t flags; ///< @ref DHCP_FLAGSBROADCAST or @ref DHCP_FLAGSUNICAST
  168. uint8_t ciaddr[4]; ///< @ref Request IP to DHCP sever
  169. uint8_t yiaddr[4]; ///< @ref Offered IP from DHCP server
  170. uint8_t siaddr[4]; ///< No use
  171. uint8_t giaddr[4]; ///< No use
  172. uint8_t chaddr[16]; ///< DHCP client 6bytes MAC address. Others is filled to zero
  173. uint8_t sname[64]; ///< No use
  174. uint8_t file[128]; ///< No use
  175. uint8_t OPT[OPT_SIZE]; ///< Option
  176. } RIP_MSG;
  177. uint8_t DHCP_SOCKET; // Socket number for DHCP
  178. uint8_t DHCP_SIP[4]; // DHCP Server IP address
  179. uint8_t DHCP_REAL_SIP[4]; // For extract my DHCP server in a few DHCP server
  180. // Network information from DHCP Server
  181. uint8_t OLD_allocated_ip[4] = {
  182. 0,
  183. }; // Previous IP address
  184. uint8_t DHCP_allocated_ip[4] = {
  185. 0,
  186. }; // IP address from DHCP
  187. uint8_t DHCP_allocated_gw[4] = {
  188. 0,
  189. }; // Gateway address from DHCP
  190. uint8_t DHCP_allocated_sn[4] = {
  191. 0,
  192. }; // Subnet mask from DHCP
  193. uint8_t DHCP_allocated_dns[4] = {
  194. 0,
  195. }; // DNS address from DHCP
  196. int8_t dhcp_state = STATE_DHCP_INIT; // DHCP state
  197. int8_t dhcp_retry_count = 0;
  198. uint32_t dhcp_lease_time = INFINITE_LEASETIME;
  199. volatile uint32_t dhcp_tick_1s = 0; // unit 1 second
  200. uint32_t dhcp_tick_next = DHCP_WAIT_TIME;
  201. uint32_t DHCP_XID; // Any number
  202. RIP_MSG* pDHCPMSG; // Buffer pointer for DHCP processing
  203. uint8_t HOST_NAME[] = DCHP_HOST_NAME;
  204. uint8_t DHCP_CHADDR[6]; // DHCP Client MAC address.
  205. /* The default callback function */
  206. void default_ip_assign(void);
  207. void default_ip_update(void);
  208. void default_ip_conflict(void);
  209. /* Callback handler */
  210. void (*dhcp_ip_assign)(void) =
  211. default_ip_assign; /* handler to be called when the IP address from DHCP server is first assigned */
  212. void (*dhcp_ip_update)(void) =
  213. default_ip_update; /* handler to be called when the IP address from DHCP server is updated */
  214. void (*dhcp_ip_conflict)(void) =
  215. default_ip_conflict; /* handler to be called when the IP address from DHCP server is conflict */
  216. char NibbleToHex(uint8_t nibble);
  217. /* send DISCOVER message to DHCP server */
  218. void send_DHCP_DISCOVER(void);
  219. /* send REQEUST message to DHCP server */
  220. void send_DHCP_REQUEST(void);
  221. /* send DECLINE message to DHCP server */
  222. void send_DHCP_DECLINE(void);
  223. /* IP conflict check by sending ARP-request to leased IP and wait ARP-response. */
  224. int8_t check_DHCP_leasedIP(void);
  225. /* check the timeout in DHCP process */
  226. uint8_t check_DHCP_timeout(void);
  227. /* Initialize to timeout process. */
  228. void reset_DHCP_timeout(void);
  229. /* Parse message as OFFER and ACK and NACK from DHCP server.*/
  230. int8_t parseDHCPCMSG(void);
  231. /* The default handler of ip assign first */
  232. void default_ip_assign(void) {
  233. setSIPR(DHCP_allocated_ip);
  234. setSUBR(DHCP_allocated_sn);
  235. setGAR(DHCP_allocated_gw);
  236. }
  237. /* The default handler of ip changed */
  238. void default_ip_update(void) {
  239. /* WIZchip Software Reset */
  240. setMR(MR_RST);
  241. getMR(); // for delay
  242. default_ip_assign();
  243. setSHAR(DHCP_CHADDR);
  244. }
  245. /* The default handler of ip changed */
  246. void default_ip_conflict(void) {
  247. // WIZchip Software Reset
  248. setMR(MR_RST);
  249. getMR(); // for delay
  250. setSHAR(DHCP_CHADDR);
  251. }
  252. /* register the call back func. */
  253. void reg_dhcp_cbfunc(void (*ip_assign)(void), void (*ip_update)(void), void (*ip_conflict)(void)) {
  254. dhcp_ip_assign = default_ip_assign;
  255. dhcp_ip_update = default_ip_update;
  256. dhcp_ip_conflict = default_ip_conflict;
  257. if(ip_assign) dhcp_ip_assign = ip_assign;
  258. if(ip_update) dhcp_ip_update = ip_update;
  259. if(ip_conflict) dhcp_ip_conflict = ip_conflict;
  260. }
  261. /* make the common DHCP message */
  262. void makeDHCPMSG(void) {
  263. uint8_t bk_mac[6];
  264. uint8_t* ptmp;
  265. uint8_t i;
  266. getSHAR(bk_mac);
  267. pDHCPMSG->op = DHCP_BOOTREQUEST;
  268. pDHCPMSG->htype = DHCP_HTYPE10MB;
  269. pDHCPMSG->hlen = DHCP_HLENETHERNET;
  270. pDHCPMSG->hops = DHCP_HOPS;
  271. ptmp = (uint8_t*)(&pDHCPMSG->xid);
  272. *(ptmp + 0) = (uint8_t)((DHCP_XID & 0xFF000000) >> 24);
  273. *(ptmp + 1) = (uint8_t)((DHCP_XID & 0x00FF0000) >> 16);
  274. *(ptmp + 2) = (uint8_t)((DHCP_XID & 0x0000FF00) >> 8);
  275. *(ptmp + 3) = (uint8_t)((DHCP_XID & 0x000000FF) >> 0);
  276. pDHCPMSG->secs = DHCP_SECS;
  277. ptmp = (uint8_t*)(&pDHCPMSG->flags);
  278. *(ptmp + 0) = (uint8_t)((DHCP_FLAGSBROADCAST & 0xFF00) >> 8);
  279. *(ptmp + 1) = (uint8_t)((DHCP_FLAGSBROADCAST & 0x00FF) >> 0);
  280. pDHCPMSG->ciaddr[0] = 0;
  281. pDHCPMSG->ciaddr[1] = 0;
  282. pDHCPMSG->ciaddr[2] = 0;
  283. pDHCPMSG->ciaddr[3] = 0;
  284. pDHCPMSG->yiaddr[0] = 0;
  285. pDHCPMSG->yiaddr[1] = 0;
  286. pDHCPMSG->yiaddr[2] = 0;
  287. pDHCPMSG->yiaddr[3] = 0;
  288. pDHCPMSG->siaddr[0] = 0;
  289. pDHCPMSG->siaddr[1] = 0;
  290. pDHCPMSG->siaddr[2] = 0;
  291. pDHCPMSG->siaddr[3] = 0;
  292. pDHCPMSG->giaddr[0] = 0;
  293. pDHCPMSG->giaddr[1] = 0;
  294. pDHCPMSG->giaddr[2] = 0;
  295. pDHCPMSG->giaddr[3] = 0;
  296. pDHCPMSG->chaddr[0] = DHCP_CHADDR[0];
  297. pDHCPMSG->chaddr[1] = DHCP_CHADDR[1];
  298. pDHCPMSG->chaddr[2] = DHCP_CHADDR[2];
  299. pDHCPMSG->chaddr[3] = DHCP_CHADDR[3];
  300. pDHCPMSG->chaddr[4] = DHCP_CHADDR[4];
  301. pDHCPMSG->chaddr[5] = DHCP_CHADDR[5];
  302. for(i = 6; i < 16; i++) pDHCPMSG->chaddr[i] = 0;
  303. for(i = 0; i < 64; i++) pDHCPMSG->sname[i] = 0;
  304. for(i = 0; i < 128; i++) pDHCPMSG->file[i] = 0;
  305. // MAGIC_COOKIE
  306. pDHCPMSG->OPT[0] = (uint8_t)((MAGIC_COOKIE & 0xFF000000) >> 24);
  307. pDHCPMSG->OPT[1] = (uint8_t)((MAGIC_COOKIE & 0x00FF0000) >> 16);
  308. pDHCPMSG->OPT[2] = (uint8_t)((MAGIC_COOKIE & 0x0000FF00) >> 8);
  309. pDHCPMSG->OPT[3] = (uint8_t)(MAGIC_COOKIE & 0x000000FF) >> 0;
  310. }
  311. /* SEND DHCP DISCOVER */
  312. void send_DHCP_DISCOVER(void) {
  313. uint16_t i;
  314. uint8_t ip[4];
  315. uint16_t k = 0;
  316. makeDHCPMSG();
  317. DHCP_SIP[0] = 0;
  318. DHCP_SIP[1] = 0;
  319. DHCP_SIP[2] = 0;
  320. DHCP_SIP[3] = 0;
  321. DHCP_REAL_SIP[0] = 0;
  322. DHCP_REAL_SIP[1] = 0;
  323. DHCP_REAL_SIP[2] = 0;
  324. DHCP_REAL_SIP[3] = 0;
  325. k = 4; // because MAGIC_COOKIE already made by makeDHCPMSG()
  326. // Option Request Param
  327. pDHCPMSG->OPT[k++] = dhcpMessageType;
  328. pDHCPMSG->OPT[k++] = 0x01;
  329. pDHCPMSG->OPT[k++] = DHCP_DISCOVER;
  330. // Client identifier
  331. pDHCPMSG->OPT[k++] = dhcpClientIdentifier;
  332. pDHCPMSG->OPT[k++] = 0x07;
  333. pDHCPMSG->OPT[k++] = 0x01;
  334. pDHCPMSG->OPT[k++] = DHCP_CHADDR[0];
  335. pDHCPMSG->OPT[k++] = DHCP_CHADDR[1];
  336. pDHCPMSG->OPT[k++] = DHCP_CHADDR[2];
  337. pDHCPMSG->OPT[k++] = DHCP_CHADDR[3];
  338. pDHCPMSG->OPT[k++] = DHCP_CHADDR[4];
  339. pDHCPMSG->OPT[k++] = DHCP_CHADDR[5];
  340. // host name
  341. pDHCPMSG->OPT[k++] = hostName;
  342. pDHCPMSG->OPT[k++] = 0; // fill zero length of hostname
  343. for(i = 0; HOST_NAME[i] != 0; i++) pDHCPMSG->OPT[k++] = HOST_NAME[i];
  344. pDHCPMSG->OPT[k++] = NibbleToHex(DHCP_CHADDR[3] >> 4);
  345. pDHCPMSG->OPT[k++] = NibbleToHex(DHCP_CHADDR[3]);
  346. pDHCPMSG->OPT[k++] = NibbleToHex(DHCP_CHADDR[4] >> 4);
  347. pDHCPMSG->OPT[k++] = NibbleToHex(DHCP_CHADDR[4]);
  348. pDHCPMSG->OPT[k++] = NibbleToHex(DHCP_CHADDR[5] >> 4);
  349. pDHCPMSG->OPT[k++] = NibbleToHex(DHCP_CHADDR[5]);
  350. pDHCPMSG->OPT[k - (i + 6 + 1)] = i + 6; // length of hostname
  351. pDHCPMSG->OPT[k++] = dhcpParamRequest;
  352. pDHCPMSG->OPT[k++] = 0x06; // length of request
  353. pDHCPMSG->OPT[k++] = subnetMask;
  354. pDHCPMSG->OPT[k++] = routersOnSubnet;
  355. pDHCPMSG->OPT[k++] = dns;
  356. pDHCPMSG->OPT[k++] = domainName;
  357. pDHCPMSG->OPT[k++] = dhcpT1value;
  358. pDHCPMSG->OPT[k++] = dhcpT2value;
  359. pDHCPMSG->OPT[k++] = endOption;
  360. for(i = k; i < OPT_SIZE; i++) pDHCPMSG->OPT[i] = 0;
  361. // send broadcasting packet
  362. ip[0] = 255;
  363. ip[1] = 255;
  364. ip[2] = 255;
  365. ip[3] = 255;
  366. #ifdef _DHCP_DEBUG_
  367. eth_printf("> Send DHCP_DISCOVER");
  368. #endif
  369. sendto(DHCP_SOCKET, (uint8_t*)pDHCPMSG, RIP_MSG_SIZE, ip, DHCP_SERVER_PORT);
  370. }
  371. /* SEND DHCP REQUEST */
  372. void send_DHCP_REQUEST(void) {
  373. int i;
  374. uint8_t ip[4];
  375. uint16_t k = 0;
  376. makeDHCPMSG();
  377. if(dhcp_state == STATE_DHCP_LEASED || dhcp_state == STATE_DHCP_REREQUEST) {
  378. *((uint8_t*)(&pDHCPMSG->flags)) = ((DHCP_FLAGSUNICAST & 0xFF00) >> 8);
  379. *((uint8_t*)(&pDHCPMSG->flags) + 1) = (DHCP_FLAGSUNICAST & 0x00FF);
  380. pDHCPMSG->ciaddr[0] = DHCP_allocated_ip[0];
  381. pDHCPMSG->ciaddr[1] = DHCP_allocated_ip[1];
  382. pDHCPMSG->ciaddr[2] = DHCP_allocated_ip[2];
  383. pDHCPMSG->ciaddr[3] = DHCP_allocated_ip[3];
  384. ip[0] = DHCP_SIP[0];
  385. ip[1] = DHCP_SIP[1];
  386. ip[2] = DHCP_SIP[2];
  387. ip[3] = DHCP_SIP[3];
  388. } else {
  389. ip[0] = 255;
  390. ip[1] = 255;
  391. ip[2] = 255;
  392. ip[3] = 255;
  393. }
  394. k = 4; // because MAGIC_COOKIE already made by makeDHCPMSG()
  395. // Option Request Param.
  396. pDHCPMSG->OPT[k++] = dhcpMessageType;
  397. pDHCPMSG->OPT[k++] = 0x01;
  398. pDHCPMSG->OPT[k++] = DHCP_REQUEST;
  399. pDHCPMSG->OPT[k++] = dhcpClientIdentifier;
  400. pDHCPMSG->OPT[k++] = 0x07;
  401. pDHCPMSG->OPT[k++] = 0x01;
  402. pDHCPMSG->OPT[k++] = DHCP_CHADDR[0];
  403. pDHCPMSG->OPT[k++] = DHCP_CHADDR[1];
  404. pDHCPMSG->OPT[k++] = DHCP_CHADDR[2];
  405. pDHCPMSG->OPT[k++] = DHCP_CHADDR[3];
  406. pDHCPMSG->OPT[k++] = DHCP_CHADDR[4];
  407. pDHCPMSG->OPT[k++] = DHCP_CHADDR[5];
  408. if(ip[3] == 255) // if(dchp_state == STATE_DHCP_LEASED || dchp_state == DHCP_REREQUEST_STATE)
  409. {
  410. pDHCPMSG->OPT[k++] = dhcpRequestedIPaddr;
  411. pDHCPMSG->OPT[k++] = 0x04;
  412. pDHCPMSG->OPT[k++] = DHCP_allocated_ip[0];
  413. pDHCPMSG->OPT[k++] = DHCP_allocated_ip[1];
  414. pDHCPMSG->OPT[k++] = DHCP_allocated_ip[2];
  415. pDHCPMSG->OPT[k++] = DHCP_allocated_ip[3];
  416. pDHCPMSG->OPT[k++] = dhcpServerIdentifier;
  417. pDHCPMSG->OPT[k++] = 0x04;
  418. pDHCPMSG->OPT[k++] = DHCP_SIP[0];
  419. pDHCPMSG->OPT[k++] = DHCP_SIP[1];
  420. pDHCPMSG->OPT[k++] = DHCP_SIP[2];
  421. pDHCPMSG->OPT[k++] = DHCP_SIP[3];
  422. }
  423. // host name
  424. pDHCPMSG->OPT[k++] = hostName;
  425. pDHCPMSG->OPT[k++] = 0; // length of hostname
  426. for(i = 0; HOST_NAME[i] != 0; i++) pDHCPMSG->OPT[k++] = HOST_NAME[i];
  427. pDHCPMSG->OPT[k++] = NibbleToHex(DHCP_CHADDR[3] >> 4);
  428. pDHCPMSG->OPT[k++] = NibbleToHex(DHCP_CHADDR[3]);
  429. pDHCPMSG->OPT[k++] = NibbleToHex(DHCP_CHADDR[4] >> 4);
  430. pDHCPMSG->OPT[k++] = NibbleToHex(DHCP_CHADDR[4]);
  431. pDHCPMSG->OPT[k++] = NibbleToHex(DHCP_CHADDR[5] >> 4);
  432. pDHCPMSG->OPT[k++] = NibbleToHex(DHCP_CHADDR[5]);
  433. pDHCPMSG->OPT[k - (i + 6 + 1)] = i + 6; // length of hostname
  434. pDHCPMSG->OPT[k++] = dhcpParamRequest;
  435. pDHCPMSG->OPT[k++] = 0x08;
  436. pDHCPMSG->OPT[k++] = subnetMask;
  437. pDHCPMSG->OPT[k++] = routersOnSubnet;
  438. pDHCPMSG->OPT[k++] = dns;
  439. pDHCPMSG->OPT[k++] = domainName;
  440. pDHCPMSG->OPT[k++] = dhcpT1value;
  441. pDHCPMSG->OPT[k++] = dhcpT2value;
  442. pDHCPMSG->OPT[k++] = performRouterDiscovery;
  443. pDHCPMSG->OPT[k++] = staticRoute;
  444. pDHCPMSG->OPT[k++] = endOption;
  445. for(i = k; i < OPT_SIZE; i++) pDHCPMSG->OPT[i] = 0;
  446. #ifdef _DHCP_DEBUG_
  447. eth_printf("> Send DHCP_REQUEST");
  448. #endif
  449. sendto(DHCP_SOCKET, (uint8_t*)pDHCPMSG, RIP_MSG_SIZE, ip, DHCP_SERVER_PORT);
  450. }
  451. /* SEND DHCP DHCPDECLINE */
  452. void send_DHCP_DECLINE(void) {
  453. int i;
  454. uint8_t ip[4];
  455. uint16_t k = 0;
  456. makeDHCPMSG();
  457. k = 4; // because MAGIC_COOKIE already made by makeDHCPMSG()
  458. *((uint8_t*)(&pDHCPMSG->flags)) = ((DHCP_FLAGSUNICAST & 0xFF00) >> 8);
  459. *((uint8_t*)(&pDHCPMSG->flags) + 1) = (DHCP_FLAGSUNICAST & 0x00FF);
  460. // Option Request Param.
  461. pDHCPMSG->OPT[k++] = dhcpMessageType;
  462. pDHCPMSG->OPT[k++] = 0x01;
  463. pDHCPMSG->OPT[k++] = DHCP_DECLINE;
  464. pDHCPMSG->OPT[k++] = dhcpClientIdentifier;
  465. pDHCPMSG->OPT[k++] = 0x07;
  466. pDHCPMSG->OPT[k++] = 0x01;
  467. pDHCPMSG->OPT[k++] = DHCP_CHADDR[0];
  468. pDHCPMSG->OPT[k++] = DHCP_CHADDR[1];
  469. pDHCPMSG->OPT[k++] = DHCP_CHADDR[2];
  470. pDHCPMSG->OPT[k++] = DHCP_CHADDR[3];
  471. pDHCPMSG->OPT[k++] = DHCP_CHADDR[4];
  472. pDHCPMSG->OPT[k++] = DHCP_CHADDR[5];
  473. pDHCPMSG->OPT[k++] = dhcpRequestedIPaddr;
  474. pDHCPMSG->OPT[k++] = 0x04;
  475. pDHCPMSG->OPT[k++] = DHCP_allocated_ip[0];
  476. pDHCPMSG->OPT[k++] = DHCP_allocated_ip[1];
  477. pDHCPMSG->OPT[k++] = DHCP_allocated_ip[2];
  478. pDHCPMSG->OPT[k++] = DHCP_allocated_ip[3];
  479. pDHCPMSG->OPT[k++] = dhcpServerIdentifier;
  480. pDHCPMSG->OPT[k++] = 0x04;
  481. pDHCPMSG->OPT[k++] = DHCP_SIP[0];
  482. pDHCPMSG->OPT[k++] = DHCP_SIP[1];
  483. pDHCPMSG->OPT[k++] = DHCP_SIP[2];
  484. pDHCPMSG->OPT[k++] = DHCP_SIP[3];
  485. pDHCPMSG->OPT[k++] = endOption;
  486. for(i = k; i < OPT_SIZE; i++) pDHCPMSG->OPT[i] = 0;
  487. //send broadcasting packet
  488. ip[0] = 0xFF;
  489. ip[1] = 0xFF;
  490. ip[2] = 0xFF;
  491. ip[3] = 0xFF;
  492. #ifdef _DHCP_DEBUG_
  493. eth_printf("\r\n> Send DHCP_DECLINE");
  494. #endif
  495. sendto(DHCP_SOCKET, (uint8_t*)pDHCPMSG, RIP_MSG_SIZE, ip, DHCP_SERVER_PORT);
  496. }
  497. /* PARSE REPLY pDHCPMSG */
  498. int8_t parseDHCPMSG(void) {
  499. uint8_t svr_addr[6];
  500. uint16_t svr_port;
  501. uint16_t len;
  502. uint8_t* p;
  503. uint8_t* e;
  504. uint8_t type = 0;
  505. uint8_t opt_len;
  506. if((len = getSn_RX_RSR(DHCP_SOCKET)) > 0) {
  507. len = recvfrom(DHCP_SOCKET, (uint8_t*)pDHCPMSG, len, svr_addr, &svr_port);
  508. #ifdef _DHCP_DEBUG_
  509. eth_printf(
  510. "DHCP message : %d.%d.%d.%d(%d) %d received.",
  511. svr_addr[0],
  512. svr_addr[1],
  513. svr_addr[2],
  514. svr_addr[3],
  515. svr_port,
  516. len);
  517. #endif
  518. } else
  519. return 0;
  520. if(svr_port == DHCP_SERVER_PORT) {
  521. // compare mac address
  522. if((pDHCPMSG->chaddr[0] != DHCP_CHADDR[0]) || (pDHCPMSG->chaddr[1] != DHCP_CHADDR[1]) ||
  523. (pDHCPMSG->chaddr[2] != DHCP_CHADDR[2]) || (pDHCPMSG->chaddr[3] != DHCP_CHADDR[3]) ||
  524. (pDHCPMSG->chaddr[4] != DHCP_CHADDR[4]) || (pDHCPMSG->chaddr[5] != DHCP_CHADDR[5])) {
  525. #ifdef _DHCP_DEBUG_
  526. eth_printf("No My DHCP Message. This message is ignored.");
  527. #endif
  528. return 0;
  529. }
  530. //compare DHCP server ip address
  531. if((DHCP_SIP[0] != 0) || (DHCP_SIP[1] != 0) || (DHCP_SIP[2] != 0) || (DHCP_SIP[3] != 0)) {
  532. if(((svr_addr[0] != DHCP_SIP[0]) || (svr_addr[1] != DHCP_SIP[1]) ||
  533. (svr_addr[2] != DHCP_SIP[2]) || (svr_addr[3] != DHCP_SIP[3])) &&
  534. ((svr_addr[0] != DHCP_REAL_SIP[0]) || (svr_addr[1] != DHCP_REAL_SIP[1]) ||
  535. (svr_addr[2] != DHCP_REAL_SIP[2]) || (svr_addr[3] != DHCP_REAL_SIP[3]))) {
  536. #ifdef _DHCP_DEBUG_
  537. eth_printf("Another DHCP sever send a response message. This is ignored.");
  538. #endif
  539. return 0;
  540. }
  541. }
  542. p = (uint8_t*)(&pDHCPMSG->op);
  543. p = p +
  544. 240; // 240 = sizeof(RIP_MSG) + MAGIC_COOKIE size in RIP_MSG.opt - sizeof(RIP_MSG.opt)
  545. e = p + (len - 240);
  546. while(p < e) {
  547. switch(*p) {
  548. case endOption:
  549. p = e; // for break while(p < e)
  550. break;
  551. case padOption:
  552. p++;
  553. break;
  554. case dhcpMessageType:
  555. p++;
  556. p++;
  557. type = *p++;
  558. break;
  559. case subnetMask:
  560. p++;
  561. p++;
  562. DHCP_allocated_sn[0] = *p++;
  563. DHCP_allocated_sn[1] = *p++;
  564. DHCP_allocated_sn[2] = *p++;
  565. DHCP_allocated_sn[3] = *p++;
  566. break;
  567. case routersOnSubnet:
  568. p++;
  569. opt_len = *p++;
  570. DHCP_allocated_gw[0] = *p++;
  571. DHCP_allocated_gw[1] = *p++;
  572. DHCP_allocated_gw[2] = *p++;
  573. DHCP_allocated_gw[3] = *p++;
  574. p = p + (opt_len - 4);
  575. break;
  576. case dns:
  577. p++;
  578. opt_len = *p++;
  579. DHCP_allocated_dns[0] = *p++;
  580. DHCP_allocated_dns[1] = *p++;
  581. DHCP_allocated_dns[2] = *p++;
  582. DHCP_allocated_dns[3] = *p++;
  583. p = p + (opt_len - 4);
  584. break;
  585. case dhcpIPaddrLeaseTime:
  586. p++;
  587. opt_len = *p++;
  588. dhcp_lease_time = *p++;
  589. dhcp_lease_time = (dhcp_lease_time << 8) + *p++;
  590. dhcp_lease_time = (dhcp_lease_time << 8) + *p++;
  591. dhcp_lease_time = (dhcp_lease_time << 8) + *p++;
  592. #ifdef _DHCP_DEBUG_
  593. dhcp_lease_time = 10;
  594. #endif
  595. break;
  596. case dhcpServerIdentifier:
  597. p++;
  598. opt_len = *p++;
  599. DHCP_SIP[0] = *p++;
  600. DHCP_SIP[1] = *p++;
  601. DHCP_SIP[2] = *p++;
  602. DHCP_SIP[3] = *p++;
  603. DHCP_REAL_SIP[0] = svr_addr[0];
  604. DHCP_REAL_SIP[1] = svr_addr[1];
  605. DHCP_REAL_SIP[2] = svr_addr[2];
  606. DHCP_REAL_SIP[3] = svr_addr[3];
  607. break;
  608. default:
  609. p++;
  610. opt_len = *p++;
  611. p += opt_len;
  612. break;
  613. } // switch
  614. } // while
  615. } // if
  616. return type;
  617. }
  618. uint8_t DHCP_run(void) {
  619. uint8_t type;
  620. uint8_t ret;
  621. if(dhcp_state == STATE_DHCP_STOP) return DHCP_STOPPED;
  622. if(getSn_SR(DHCP_SOCKET) != SOCK_UDP) socket(DHCP_SOCKET, Sn_MR_UDP, DHCP_CLIENT_PORT, 0x00);
  623. ret = DHCP_RUNNING;
  624. type = parseDHCPMSG();
  625. switch(dhcp_state) {
  626. case STATE_DHCP_INIT:
  627. DHCP_allocated_ip[0] = 0;
  628. DHCP_allocated_ip[1] = 0;
  629. DHCP_allocated_ip[2] = 0;
  630. DHCP_allocated_ip[3] = 0;
  631. send_DHCP_DISCOVER();
  632. dhcp_state = STATE_DHCP_DISCOVER;
  633. break;
  634. case STATE_DHCP_DISCOVER:
  635. if(type == DHCP_OFFER) {
  636. #ifdef _DHCP_DEBUG_
  637. eth_printf("> Receive DHCP_OFFER");
  638. #endif
  639. DHCP_allocated_ip[0] = pDHCPMSG->yiaddr[0];
  640. DHCP_allocated_ip[1] = pDHCPMSG->yiaddr[1];
  641. DHCP_allocated_ip[2] = pDHCPMSG->yiaddr[2];
  642. DHCP_allocated_ip[3] = pDHCPMSG->yiaddr[3];
  643. send_DHCP_REQUEST();
  644. dhcp_state = STATE_DHCP_REQUEST;
  645. } else {
  646. ret = check_DHCP_timeout();
  647. }
  648. break;
  649. case STATE_DHCP_REQUEST:
  650. if(type == DHCP_ACK) {
  651. #ifdef _DHCP_DEBUG_
  652. eth_printf("> Receive DHCP_ACK");
  653. #endif
  654. if(check_DHCP_leasedIP()) {
  655. // Network info assignment from DHCP
  656. dhcp_ip_assign();
  657. reset_DHCP_timeout();
  658. dhcp_state = STATE_DHCP_LEASED;
  659. } else {
  660. // IP address conflict occurred
  661. reset_DHCP_timeout();
  662. dhcp_ip_conflict();
  663. dhcp_state = STATE_DHCP_INIT;
  664. }
  665. } else if(type == DHCP_NAK) {
  666. #ifdef _DHCP_DEBUG_
  667. eth_printf("> Receive DHCP_NACK");
  668. #endif
  669. reset_DHCP_timeout();
  670. dhcp_state = STATE_DHCP_DISCOVER;
  671. } else {
  672. ret = check_DHCP_timeout();
  673. }
  674. break;
  675. case STATE_DHCP_LEASED:
  676. ret = DHCP_IP_LEASED;
  677. if((dhcp_lease_time != INFINITE_LEASETIME) && ((dhcp_lease_time / 2) < dhcp_tick_1s)) {
  678. #ifdef _DHCP_DEBUG_
  679. eth_printf("> Maintains the IP address ");
  680. #endif
  681. type = 0;
  682. OLD_allocated_ip[0] = DHCP_allocated_ip[0];
  683. OLD_allocated_ip[1] = DHCP_allocated_ip[1];
  684. OLD_allocated_ip[2] = DHCP_allocated_ip[2];
  685. OLD_allocated_ip[3] = DHCP_allocated_ip[3];
  686. DHCP_XID++;
  687. send_DHCP_REQUEST();
  688. reset_DHCP_timeout();
  689. dhcp_state = STATE_DHCP_REREQUEST;
  690. }
  691. break;
  692. case STATE_DHCP_REREQUEST:
  693. ret = DHCP_IP_LEASED;
  694. if(type == DHCP_ACK) {
  695. dhcp_retry_count = 0;
  696. if(OLD_allocated_ip[0] != DHCP_allocated_ip[0] ||
  697. OLD_allocated_ip[1] != DHCP_allocated_ip[1] ||
  698. OLD_allocated_ip[2] != DHCP_allocated_ip[2] ||
  699. OLD_allocated_ip[3] != DHCP_allocated_ip[3]) {
  700. ret = DHCP_IP_CHANGED;
  701. dhcp_ip_update();
  702. #ifdef _DHCP_DEBUG_
  703. eth_printf(">IP changed.");
  704. #endif
  705. }
  706. #ifdef _DHCP_DEBUG_
  707. else
  708. eth_printf(">IP is continued.");
  709. #endif
  710. reset_DHCP_timeout();
  711. dhcp_state = STATE_DHCP_LEASED;
  712. } else if(type == DHCP_NAK) {
  713. #ifdef _DHCP_DEBUG_
  714. eth_printf("> Receive DHCP_NACK, Failed to maintain ip");
  715. #endif
  716. reset_DHCP_timeout();
  717. dhcp_state = STATE_DHCP_DISCOVER;
  718. } else
  719. ret = check_DHCP_timeout();
  720. break;
  721. default:
  722. break;
  723. }
  724. return ret;
  725. }
  726. void DHCP_stop(void) {
  727. close(DHCP_SOCKET);
  728. dhcp_state = STATE_DHCP_STOP;
  729. }
  730. uint8_t check_DHCP_timeout(void) {
  731. uint8_t ret = DHCP_RUNNING;
  732. if(dhcp_retry_count < MAX_DHCP_RETRY) {
  733. if(dhcp_tick_next < dhcp_tick_1s) {
  734. switch(dhcp_state) {
  735. case STATE_DHCP_DISCOVER:
  736. // eth_printf("<<timeout>> state : STATE_DHCP_DISCOVER");
  737. send_DHCP_DISCOVER();
  738. break;
  739. case STATE_DHCP_REQUEST:
  740. // eth_printf("<<timeout>> state : STATE_DHCP_REQUEST");
  741. send_DHCP_REQUEST();
  742. break;
  743. case STATE_DHCP_REREQUEST:
  744. // eth_printf("<<timeout>> state : STATE_DHCP_REREQUEST");
  745. send_DHCP_REQUEST();
  746. break;
  747. default:
  748. break;
  749. }
  750. dhcp_tick_1s = 0;
  751. dhcp_tick_next = dhcp_tick_1s + DHCP_WAIT_TIME;
  752. dhcp_retry_count++;
  753. }
  754. } else { // timeout occurred
  755. switch(dhcp_state) {
  756. case STATE_DHCP_DISCOVER:
  757. dhcp_state = STATE_DHCP_INIT;
  758. ret = DHCP_FAILED;
  759. break;
  760. case STATE_DHCP_REQUEST:
  761. case STATE_DHCP_REREQUEST:
  762. send_DHCP_DISCOVER();
  763. dhcp_state = STATE_DHCP_DISCOVER;
  764. break;
  765. default:
  766. break;
  767. }
  768. reset_DHCP_timeout();
  769. }
  770. return ret;
  771. }
  772. int8_t check_DHCP_leasedIP(void) {
  773. uint8_t tmp;
  774. int32_t ret;
  775. //WIZchip RCR value changed for ARP Timeout count control
  776. tmp = getRCR();
  777. setRCR(0x03);
  778. // IP conflict detection : ARP request - ARP reply
  779. // Broadcasting ARP Request for check the IP conflict using UDP sendto() function
  780. ret = sendto(DHCP_SOCKET, (uint8_t*)"CHECK_IP_CONFLICT", 17, DHCP_allocated_ip, 5000);
  781. // RCR value restore
  782. setRCR(tmp);
  783. if(ret == SOCKERR_TIMEOUT) {
  784. // UDP send Timeout occurred : allocated IP address is unique, DHCP Success
  785. #ifdef _DHCP_DEBUG_
  786. eth_printf("\r\n> Check leased IP - OK");
  787. #endif
  788. return 1;
  789. } else {
  790. // Received ARP reply or etc : IP address conflict occur, DHCP Failed
  791. send_DHCP_DECLINE();
  792. ret = dhcp_tick_1s;
  793. while((dhcp_tick_1s - ret) < 2)
  794. ; // wait for 1s over; wait to complete to send DECLINE message;
  795. return 0;
  796. }
  797. }
  798. void DHCP_init(uint8_t s, uint8_t* buf) {
  799. uint8_t zeroip[4] = {0, 0, 0, 0};
  800. getSHAR(DHCP_CHADDR);
  801. if((DHCP_CHADDR[0] | DHCP_CHADDR[1] | DHCP_CHADDR[2] | DHCP_CHADDR[3] | DHCP_CHADDR[4] |
  802. DHCP_CHADDR[5]) == 0x00) {
  803. // assigning temporary mac address, you should be set SHAR before call this function.
  804. DHCP_CHADDR[0] = 0x00;
  805. DHCP_CHADDR[1] = 0x08;
  806. DHCP_CHADDR[2] = 0xdc;
  807. DHCP_CHADDR[3] = 0x00;
  808. DHCP_CHADDR[4] = 0x00;
  809. DHCP_CHADDR[5] = 0x00;
  810. setSHAR(DHCP_CHADDR);
  811. }
  812. DHCP_SOCKET = s; // SOCK_DHCP
  813. pDHCPMSG = (RIP_MSG*)buf;
  814. DHCP_XID = 0x12345678;
  815. {
  816. DHCP_XID += DHCP_CHADDR[3];
  817. DHCP_XID += DHCP_CHADDR[4];
  818. DHCP_XID += DHCP_CHADDR[5];
  819. DHCP_XID += (DHCP_CHADDR[3] ^ DHCP_CHADDR[4] ^ DHCP_CHADDR[5]);
  820. }
  821. // WIZchip Netinfo Clear
  822. setSIPR(zeroip);
  823. setGAR(zeroip);
  824. reset_DHCP_timeout();
  825. dhcp_state = STATE_DHCP_INIT;
  826. }
  827. /* Reset the DHCP timeout count and retry count. */
  828. void reset_DHCP_timeout(void) {
  829. dhcp_tick_1s = 0;
  830. dhcp_tick_next = DHCP_WAIT_TIME;
  831. dhcp_retry_count = 0;
  832. }
  833. void DHCP_time_handler(void) {
  834. dhcp_tick_1s++;
  835. }
  836. void getIPfromDHCP(uint8_t* ip) {
  837. ip[0] = DHCP_allocated_ip[0];
  838. ip[1] = DHCP_allocated_ip[1];
  839. ip[2] = DHCP_allocated_ip[2];
  840. ip[3] = DHCP_allocated_ip[3];
  841. }
  842. void getGWfromDHCP(uint8_t* ip) {
  843. ip[0] = DHCP_allocated_gw[0];
  844. ip[1] = DHCP_allocated_gw[1];
  845. ip[2] = DHCP_allocated_gw[2];
  846. ip[3] = DHCP_allocated_gw[3];
  847. }
  848. void getSNfromDHCP(uint8_t* ip) {
  849. ip[0] = DHCP_allocated_sn[0];
  850. ip[1] = DHCP_allocated_sn[1];
  851. ip[2] = DHCP_allocated_sn[2];
  852. ip[3] = DHCP_allocated_sn[3];
  853. }
  854. void getDNSfromDHCP(uint8_t* ip) {
  855. ip[0] = DHCP_allocated_dns[0];
  856. ip[1] = DHCP_allocated_dns[1];
  857. ip[2] = DHCP_allocated_dns[2];
  858. ip[3] = DHCP_allocated_dns[3];
  859. }
  860. uint32_t getDHCPLeasetime(void) {
  861. return dhcp_lease_time;
  862. }
  863. char NibbleToHex(uint8_t nibble) {
  864. nibble &= 0x0F;
  865. if(nibble <= 9)
  866. return nibble + '0';
  867. else
  868. return nibble + ('A' - 0x0A);
  869. }