Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DHCPClientApp.c 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2014.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. /** \file
  27. *
  28. * DHCP Client Application. When connected to the uIP stack, this will retrieve IP configuration settings from the
  29. * DHCP server on the network.
  30. */
  31. #define INCLUDE_FROM_DHCPCLIENTAPP_C
  32. #include "DHCPClientApp.h"
  33. #if defined(ENABLE_DHCP_CLIENT) || defined(__DOXYGEN__)
  34. /** Initialization function for the DHCP client. */
  35. void DHCPClientApp_Init(void)
  36. {
  37. /* Create a new UDP connection to the DHCP server port for the DHCP solicitation */
  38. struct uip_udp_conn* Connection = uip_udp_new(&uip_broadcast_addr, HTONS(DHCP_SERVER_PORT));
  39. /* If the connection was successfully created, bind it to the local DHCP client port */
  40. if (Connection != NULL)
  41. {
  42. uip_udp_appstate_t* const AppState = &Connection->appstate;
  43. uip_udp_bind(Connection, HTONS(DHCP_CLIENT_PORT));
  44. /* Set the initial client state */
  45. AppState->DHCPClient.CurrentState = DHCP_STATE_SendDiscover;
  46. /* Set timeout period to half a second for a DHCP server to respond */
  47. timer_set(&AppState->DHCPClient.Timeout, CLOCK_SECOND / 2);
  48. }
  49. }
  50. /** uIP stack application callback for the DHCP client. This function must be called each time the TCP/IP stack
  51. * needs a UDP packet to be processed.
  52. */
  53. void DHCPClientApp_Callback(void)
  54. {
  55. uip_udp_appstate_t* const AppState = &uip_udp_conn->appstate;
  56. DHCP_Header_t* const AppData = (DHCP_Header_t*)uip_appdata;
  57. uint16_t AppDataSize = 0;
  58. switch (AppState->DHCPClient.CurrentState)
  59. {
  60. case DHCP_STATE_SendDiscover:
  61. /* Clear all DHCP settings, reset client IP address */
  62. memset(&AppState->DHCPClient.DHCPOffer_Data, 0x00, sizeof(AppState->DHCPClient.DHCPOffer_Data));
  63. uip_sethostaddr((uip_ipaddr_t*)&AppState->DHCPClient.DHCPOffer_Data.AllocatedIP);
  64. /* Fill out the DHCP response header */
  65. AppDataSize += DHCPClientApp_FillDHCPHeader(AppData, DHCP_DISCOVER, AppState);
  66. /* Add the required DHCP options list to the packet */
  67. uint8_t RequiredOptionList[] = {DHCP_OPTION_SUBNET_MASK, DHCP_OPTION_ROUTER, DHCP_OPTION_DNS_SERVER};
  68. AppDataSize += DHCPCommon_SetOption(AppData->Options, DHCP_OPTION_REQ_LIST, sizeof(RequiredOptionList),
  69. RequiredOptionList);
  70. /* Send the DHCP DISCOVER packet */
  71. uip_udp_send(AppDataSize);
  72. /* Reset the timeout timer, progress to next state */
  73. timer_reset(&AppState->DHCPClient.Timeout);
  74. AppState->DHCPClient.CurrentState = DHCP_STATE_WaitForOffer;
  75. break;
  76. case DHCP_STATE_WaitForOffer:
  77. if (!(uip_newdata()))
  78. {
  79. /* Check if the DHCP timeout period has expired while waiting for a response */
  80. if (timer_expired(&AppState->DHCPClient.Timeout))
  81. AppState->DHCPClient.CurrentState = DHCP_STATE_SendDiscover;
  82. break;
  83. }
  84. uint8_t OfferResponse_MessageType;
  85. if ((AppData->TransactionID == DHCP_TRANSACTION_ID) &&
  86. DHCPCommon_GetOption(AppData->Options, DHCP_OPTION_MSG_TYPE, &OfferResponse_MessageType) &&
  87. (OfferResponse_MessageType == DHCP_OFFER))
  88. {
  89. /* Received a DHCP offer for an IP address, copy over values for later request */
  90. memcpy(&AppState->DHCPClient.DHCPOffer_Data.AllocatedIP, &AppData->YourIP, sizeof(uip_ipaddr_t));
  91. DHCPCommon_GetOption(AppData->Options, DHCP_OPTION_SUBNET_MASK, &AppState->DHCPClient.DHCPOffer_Data.Netmask);
  92. DHCPCommon_GetOption(AppData->Options, DHCP_OPTION_ROUTER, &AppState->DHCPClient.DHCPOffer_Data.GatewayIP);
  93. DHCPCommon_GetOption(AppData->Options, DHCP_OPTION_SERVER_ID, &AppState->DHCPClient.DHCPOffer_Data.ServerIP);
  94. timer_reset(&AppState->DHCPClient.Timeout);
  95. AppState->DHCPClient.CurrentState = DHCP_STATE_SendRequest;
  96. }
  97. break;
  98. case DHCP_STATE_SendRequest:
  99. /* Fill out the DHCP response header */
  100. AppDataSize += DHCPClientApp_FillDHCPHeader(AppData, DHCP_REQUEST, AppState);
  101. /* Add the DHCP REQUESTED IP ADDRESS option to the packet */
  102. AppDataSize += DHCPCommon_SetOption(AppData->Options, DHCP_OPTION_REQ_IPADDR, sizeof(uip_ipaddr_t),
  103. &AppState->DHCPClient.DHCPOffer_Data.AllocatedIP);
  104. /* Add the DHCP SERVER IP ADDRESS option to the packet */
  105. AppDataSize += DHCPCommon_SetOption(AppData->Options, DHCP_OPTION_SERVER_ID, sizeof(uip_ipaddr_t),
  106. &AppState->DHCPClient.DHCPOffer_Data.ServerIP);
  107. /* Send the DHCP REQUEST packet */
  108. uip_udp_send(AppDataSize);
  109. /* Reset the timeout timer, progress to next state */
  110. timer_reset(&AppState->DHCPClient.Timeout);
  111. AppState->DHCPClient.CurrentState = DHCP_STATE_WaitForACK;
  112. break;
  113. case DHCP_STATE_WaitForACK:
  114. if (!(uip_newdata()))
  115. {
  116. /* Check if the DHCP timeout period has expired while waiting for a response */
  117. if (timer_expired(&AppState->DHCPClient.Timeout))
  118. AppState->DHCPClient.CurrentState = DHCP_STATE_SendDiscover;
  119. break;
  120. }
  121. uint8_t RequestResponse_MessageType;
  122. if ((AppData->TransactionID == DHCP_TRANSACTION_ID) &&
  123. DHCPCommon_GetOption(AppData->Options, DHCP_OPTION_MSG_TYPE, &RequestResponse_MessageType) &&
  124. (RequestResponse_MessageType == DHCP_ACK))
  125. {
  126. /* Set the new network parameters from the DHCP server */
  127. uip_sethostaddr((uip_ipaddr_t*)&AppState->DHCPClient.DHCPOffer_Data.AllocatedIP);
  128. uip_setnetmask((uip_ipaddr_t*)&AppState->DHCPClient.DHCPOffer_Data.Netmask);
  129. uip_setdraddr((uip_ipaddr_t*)&AppState->DHCPClient.DHCPOffer_Data.GatewayIP);
  130. AppState->DHCPClient.CurrentState = DHCP_STATE_AddressLeased;
  131. }
  132. break;
  133. }
  134. }
  135. /** Fills the DHCP packet response with the appropriate BOOTP header for DHCP. This fills out all the required
  136. * fields, leaving only the additional DHCP options to be added to the packet before it is sent to the DHCP server.
  137. *
  138. * \param[out] DHCPHeader Location in the packet buffer where the BOOTP header should be written to
  139. * \param[in] DHCPMessageType DHCP Message type, such as DHCP_DISCOVER
  140. * \param[in] AppState Application state of the current UDP connection
  141. *
  142. * \return Size in bytes of the created DHCP packet
  143. */
  144. static uint16_t DHCPClientApp_FillDHCPHeader(DHCP_Header_t* const DHCPHeader,
  145. const uint8_t DHCPMessageType,
  146. uip_udp_appstate_t* const AppState)
  147. {
  148. /* Erase existing packet data so that we start will all 0x00 DHCP header data */
  149. memset(DHCPHeader, 0, sizeof(DHCP_Header_t));
  150. /* Fill out the DHCP packet header */
  151. DHCPHeader->Operation = DHCP_OP_BOOTREQUEST;
  152. DHCPHeader->HardwareType = DHCP_HTYPE_ETHERNET;
  153. DHCPHeader->HardwareAddressLength = sizeof(MACAddress);
  154. DHCPHeader->Hops = 0;
  155. DHCPHeader->TransactionID = DHCP_TRANSACTION_ID;
  156. DHCPHeader->ElapsedSeconds = 0;
  157. DHCPHeader->Flags = HTONS(BOOTP_BROADCAST);
  158. memcpy(&DHCPHeader->ClientIP, &uip_hostaddr, sizeof(uip_ipaddr_t));
  159. memcpy(&DHCPHeader->YourIP, &AppState->DHCPClient.DHCPOffer_Data.AllocatedIP, sizeof(uip_ipaddr_t));
  160. memcpy(&DHCPHeader->NextServerIP, &AppState->DHCPClient.DHCPOffer_Data.ServerIP, sizeof(uip_ipaddr_t));
  161. memcpy(&DHCPHeader->ClientHardwareAddress, &MACAddress, sizeof(struct uip_eth_addr));
  162. DHCPHeader->Cookie = DHCP_MAGIC_COOKIE;
  163. /* Add a DHCP message type and terminator options to the start of the DHCP options field */
  164. DHCPHeader->Options[0] = DHCP_OPTION_MSG_TYPE;
  165. DHCPHeader->Options[1] = 1;
  166. DHCPHeader->Options[2] = DHCPMessageType;
  167. DHCPHeader->Options[3] = DHCP_OPTION_END;
  168. /* Calculate the total number of bytes added to the outgoing packet */
  169. return (sizeof(DHCP_Header_t) + 4);
  170. }
  171. #endif