123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- #define INCLUDE_FROM_DHCPSERVERAPP_C
- #include "DHCPServerApp.h"
- #if defined(ENABLE_DHCP_SERVER) || defined(__DOXYGEN__)
- struct uip_conn* BroadcastConnection;
- uint8_t LeasedIPs[255 / 8];
- void DHCPServerApp_Init(void)
- {
-
- uip_listen(HTONS(DHCP_SERVER_PORT));
-
- struct uip_udp_conn* BroadcastConnection = uip_udp_new(&uip_broadcast_addr, HTONS(DHCP_CLIENT_PORT));
-
- if (BroadcastConnection != NULL)
- uip_udp_bind(BroadcastConnection, HTONS(DHCP_SERVER_PORT));
-
- memset(LeasedIPs, 0x00, sizeof(LeasedIPs));
- }
- void DHCPServerApp_Callback(void)
- {
- DHCP_Header_t* const AppData = (DHCP_Header_t*)uip_appdata;
- uint16_t AppDataSize = 0;
-
- if (uip_newdata())
- {
-
- uint8_t DHCPMessageType;
- if (!(DHCPCommon_GetOption(AppData->Options, DHCP_OPTION_MSG_TYPE, &DHCPMessageType)))
- return;
- uip_ipaddr_t Netmask, GatewayIPAddress, PreferredClientIP;
- struct uip_eth_addr RemoteMACAddress;
- uint32_t TransactionID;
-
- uip_getnetmask(&Netmask);
- uip_getdraddr(&GatewayIPAddress);
- memcpy(&RemoteMACAddress, &AppData->ClientHardwareAddress, sizeof(struct uip_eth_addr));
- TransactionID = AppData->TransactionID;
-
- if (!(DHCPCommon_GetOption(AppData->Options, DHCP_OPTION_REQ_IPADDR, &PreferredClientIP)))
- memcpy(&PreferredClientIP, &uip_all_zeroes_addr, sizeof(uip_ipaddr_t));
- switch (DHCPMessageType)
- {
- case DHCP_DISCOVER:
-
- if (DHCPServerApp_CheckIfIPLeased(&PreferredClientIP))
- DHCPServerApp_GetUnleasedIP(&PreferredClientIP);
-
- AppDataSize += DHCPServerApp_FillDHCPHeader(AppData, DHCP_OFFER, &RemoteMACAddress, &PreferredClientIP, TransactionID);
-
- AppDataSize += DHCPCommon_SetOption(AppData->Options, DHCP_OPTION_SUBNET_MASK,
- sizeof(uip_ipaddr_t), &Netmask);
- AppDataSize += DHCPCommon_SetOption(AppData->Options, DHCP_OPTION_ROUTER,
- sizeof(uip_ipaddr_t), &GatewayIPAddress);
-
- uip_poll_conn(BroadcastConnection);
- memcpy(&uip_udp_conn->ripaddr, &uip_broadcast_addr, sizeof(uip_ipaddr_t));
- uip_udp_send(AppDataSize);
- break;
- case DHCP_REQUEST:
-
- if (!(DHCPServerApp_CheckIfIPLeased(&PreferredClientIP)))
- {
-
- AppDataSize += DHCPServerApp_FillDHCPHeader(AppData, DHCP_ACK, &RemoteMACAddress, &PreferredClientIP, TransactionID);
-
- AppDataSize += DHCPCommon_SetOption(AppData->Options, DHCP_OPTION_SUBNET_MASK,
- sizeof(uip_ipaddr_t), &Netmask);
- AppDataSize += DHCPCommon_SetOption(AppData->Options, DHCP_OPTION_ROUTER,
- sizeof(uip_ipaddr_t), &GatewayIPAddress);
-
- DHCPServerApp_LeaseIP(&PreferredClientIP);
- }
- else
- {
-
- AppDataSize += DHCPServerApp_FillDHCPHeader(AppData, DHCP_NAK, &RemoteMACAddress, &uip_all_zeroes_addr, TransactionID);
- }
-
- uip_poll_conn(BroadcastConnection);
- memcpy(&uip_udp_conn->ripaddr, &uip_broadcast_addr, sizeof(uip_ipaddr_t));
- uip_udp_send(AppDataSize);
- break;
- case DHCP_RELEASE:
-
- DHCPServerApp_UnleaseIP(&uip_udp_conn->ripaddr);
- break;
- }
- }
- }
- static uint16_t DHCPServerApp_FillDHCPHeader(DHCP_Header_t* const DHCPHeader,
- const uint8_t DHCPMessageType,
- const struct uip_eth_addr* const ClientHardwareAddress,
- const uip_ipaddr_t* const PreferredClientIP,
- const uint32_t TransactionID)
- {
-
- memset(DHCPHeader, 0, sizeof(DHCP_Header_t));
- DHCPHeader->Operation = DHCPMessageType;
- DHCPHeader->HardwareType = DHCP_HTYPE_ETHERNET;
- DHCPHeader->HardwareAddressLength = sizeof(MACAddress);
- DHCPHeader->Hops = 0;
- DHCPHeader->TransactionID = TransactionID;
- DHCPHeader->ElapsedSeconds = 0;
- DHCPHeader->Flags = 0;
- memcpy(&DHCPHeader->NextServerIP, &uip_hostaddr, sizeof(uip_ipaddr_t));
- memcpy(&DHCPHeader->YourIP, PreferredClientIP, sizeof(uip_ipaddr_t));
- memcpy(&DHCPHeader->ClientHardwareAddress, ClientHardwareAddress, sizeof(struct uip_eth_addr));
- DHCPHeader->Cookie = DHCP_MAGIC_COOKIE;
-
- DHCPHeader->Options[0] = DHCP_OPTION_MSG_TYPE;
- DHCPHeader->Options[1] = 1;
- DHCPHeader->Options[2] = DHCPMessageType;
- DHCPHeader->Options[3] = DHCP_OPTION_END;
-
- return (sizeof(DHCP_Header_t) + 4);
- }
- static bool DHCPServerApp_CheckIfIPLeased(const uip_ipaddr_t* const IPAddress)
- {
- uint8_t Byte = (IPAddress->u8[3] / 8);
- uint8_t Mask = (1 << (IPAddress->u8[3] % 8));
-
- if (IPAddress->u8[3] && !(IPAddress->u8[3] == uip_hostaddr.u8[3]) && !(LeasedIPs[Byte] & Mask))
- return false;
- else
- return true;
- }
- static void DHCPServerApp_GetUnleasedIP(uip_ipaddr_t* const NewIPAddress)
- {
- uip_ipaddr_copy(NewIPAddress, &uip_hostaddr);
-
- for (uint8_t IP = 1; IP < 254; IP++)
- {
-
- NewIPAddress->u8[3] = IP;
-
- if (!(DHCPServerApp_CheckIfIPLeased(NewIPAddress)))
- return;
- }
- }
- static void DHCPServerApp_LeaseIP(const uip_ipaddr_t* const IPAddress)
- {
- uint8_t Byte = (IPAddress->u8[3] / 8);
- uint8_t Mask = (1 << (IPAddress->u8[3] % 8));
-
- LeasedIPs[Byte] |= Mask;
- }
- static void DHCPServerApp_UnleaseIP(const uip_ipaddr_t* const IPAddress)
- {
- uint8_t Byte = (IPAddress->u8[3] / 8);
- uint8_t Mask = (1 << (IPAddress->u8[3] % 8));
-
- LeasedIPs[Byte] &= ~Mask;
- }
- #endif
|