dhcp.c 28 KB

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