Mistake on this page?
Report an issue in GitHub or email us
CyDhcpServer.h
1 /*
2  * Copyright (c) 2018-2019 ARM Limited
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef WHD_DHCP_SERVER_H
19 #define WHD_DHCP_SERVER_H
20 
21 #include "cy_result.h"
22 #include "cy_syslib.h"
23 #include "cynetwork_utils.h"
24 #include "UDPSocket.h"
26 #include "netsocket/NetworkStack.h"
27 #include "rtos.h"
28 
29 /* DHCP data structure */
30 typedef struct {
31  uint8_t Opcode; /* packet opcode type */
32  uint8_t HwType; /* hardware addr type */
33  uint8_t HwLen; /* hardware addr length */
34  uint8_t Hops; /* gateway hops */
35  uint32_t TransactionId; /* transaction ID */
36  uint16_t SecsElapsed; /* seconds since boot began */
37  uint16_t Flags;
38  uint32_t ClientIpAddr; /* client IP address */
39  uint32_t YourIpAddr; /* 'your' IP address */
40  uint32_t ServerIpAddr; /* server IP address */
41  uint32_t GatewayIpAddr; /* gateway IP address */
42  uint8_t ClientHwAddr[16]; /* client hardware address */
43  uint8_t Legacy[192]; /* SName, File */
44  uint32_t MagicCookie;
45  uint8_t Options[3]; /* options area */
46  /* as of RFC2131 it is variable length */
48 
49 #define DHCP_SUBNETMASK_OPTION_CODE (1)
50 #define DHCP_ROUTER_OPTION_CODE (3)
51 #define DHCP_DNS_SERVER_OPTION_CODE (6)
52 #define DHCP_HOST_NAME_OPTION_CODE (12)
53 #define DHCP_MTU_OPTION_CODE (26)
54 #define DHCP_REQUESTED_IP_ADDRESS_OPTION_CODE (50)
55 #define DHCP_LEASETIME_OPTION_CODE (51)
56 #define DHCP_MESSAGETYPE_OPTION_CODE (53)
57 #define DHCP_SERVER_IDENTIFIER_OPTION_CODE (54)
58 #define DHCP_PARAM_REQUEST_LIST_OPTION_CODE (55)
59 #define DHCP_WPAD_OPTION_CODE (252)
60 #define DHCP_END_OPTION_CODE (255)
61 
62 #define DHCP_IP_ADDRESS_CACHE_MAX (5)
63 #define ADDITIONAL_OPTION_BYTES (272)
64 #define DHCP_PACKET_SIZE (sizeof(dhcp_packet_t) + ADDITIONAL_OPTION_BYTES)
65 
66 /** DHCP thread could not be started */
67 #define CY_DHCP_THREAD_CREATION_FAILED CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_MIDDLEWARE_BASE, 0)
68 
69 /** Error while trying to stop the DHCP server */
70 #define CY_DHCP_STOP_FAILED CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_MIDDLEWARE_BASE, 1)
71 
72 /**
73  * Implementation of a DHCP sever
74  */
75 class CyDhcpServer {
76 public:
77  /**
78  * Create a DHCP server.
79  */
80  CyDhcpServer(NetworkStack *nstack, NetworkInterface *niface);
81 
82  /**
83  * Delete the DHCP server.
84  */
85  virtual ~CyDhcpServer();
86 
87  /**
88  * Start a DHCP server instance.
89  * @return CY_RSLT_SUCCESS on success otherwise error.
90  */
91  cy_rslt_t start(void);
92 
93  /**
94  * Stop a DHCP server instance.
95  * @return CY_RSLT_SUCCESS on success otherwise error.
96  */
97  cy_rslt_t stop(void);
98 
99 private:
100  NetworkStack *_nstack = NULL;
101  NetworkInterface *_niface = NULL;
102  UDPSocket _socket;
103  Thread _thread;
104  bool _running = false;
105 
106  cy_ip_addr_t _available_addr;
107  cy_ip_addr_t _server_addr;
108  cy_ip_addr_t _netmask;
109 
110  cy_mac_addr_t _mac_addr_cache[DHCP_IP_ADDRESS_CACHE_MAX];
111  cy_ip_addr_t _ip_addr_cache[DHCP_IP_ADDRESS_CACHE_MAX];
112  uint8_t _buff[DHCP_PACKET_SIZE];
113 
114  static void threadWrapper(CyDhcpServer *obj);
115  void runServer(void);
116 
117  void setAddress(const cy_mac_addr_t &mac_id, const cy_ip_addr_t &addr);
118  bool lookupAddress(const cy_mac_addr_t &mac_id, cy_ip_addr_t &addr);
119  void freeAddress(const cy_mac_addr_t &mac_id);
120 
121  void handleDiscover(dhcp_packet_t *dhcp);
122  void handleRequest(dhcp_packet_t *dhcp);
123 };
124 
125 #endif /* WHD_DHCP_SERVER_H */
The Thread class allow defining, creating, and controlling thread functions in the system...
Definition: Thread.h:92
CyDhcpServer(NetworkStack *nstack, NetworkInterface *niface)
Create a DHCP server.
NetworkStack class.
Definition: NetworkStack.h:42
Network Interface base class.
UDP socket implementation.
Definition: UDPSocket.h:34
cy_rslt_t start(void)
Start a DHCP server instance.
Common interface that is shared between network devices.
Implementation of a DHCP sever.
Definition: CyDhcpServer.h:75
virtual ~CyDhcpServer()
Delete the DHCP server.
cy_rslt_t stop(void)
Stop a DHCP server instance.
NetworkStack class.
uint8_t Options
Options flags.
Definition: shci.h:536
Structure for storing a MAC address (Wi-Fi Media Access Control address).
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.