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