| 169 | } |
| 170 | |
| 171 | static uint8_t get_ip(struct mac_addr_t *p_mac_addr) |
| 172 | { |
| 173 | static uint8_t next_client_ip = DHCPD_CLIENT_IP_MIN; |
| 174 | static struct mac_ip_item_t mac_table[MAC_TABLE_LEN]; |
| 175 | static int offset = 0; |
| 176 | |
| 177 | struct mac_addr_t bad_mac; |
| 178 | int i; |
| 179 | uint8_t ip_addr_3; |
| 180 | |
| 181 | rt_memset(&bad_mac, 0, sizeof(bad_mac)); |
| 182 | if (!rt_memcmp(&bad_mac, p_mac_addr, sizeof(bad_mac))) |
| 183 | { |
| 184 | DEBUG_PRINTF("mac address all zero"); |
| 185 | ip_addr_3 = DHCPD_CLIENT_IP_MAX; |
| 186 | goto _return; |
| 187 | } |
| 188 | |
| 189 | rt_memset(&bad_mac, 0xFF, sizeof(bad_mac)); |
| 190 | if (!rt_memcmp(&bad_mac, p_mac_addr, sizeof(bad_mac))) |
| 191 | { |
| 192 | DEBUG_PRINTF("mac address all one"); |
| 193 | ip_addr_3 = DHCPD_CLIENT_IP_MAX; |
| 194 | goto _return; |
| 195 | } |
| 196 | |
| 197 | for (i = 0; i < MAC_TABLE_LEN; i++) |
| 198 | { |
| 199 | if (!rt_memcmp(&mac_table[i].mac_addr, p_mac_addr, sizeof(bad_mac))) |
| 200 | { |
| 201 | //use old ip |
| 202 | ip_addr_3 = mac_table[i].ip_addr_3; |
| 203 | DEBUG_PRINTF("return old ip: %d\n", (int)ip_addr_3); |
| 204 | goto _return; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /* add new ip */ |
| 209 | mac_table[offset].mac_addr = *p_mac_addr; |
| 210 | mac_table[offset].ip_addr_3 = next_client_ip; |
| 211 | ip_addr_3 = mac_table[offset].ip_addr_3 ; |
| 212 | |
| 213 | offset++; |
| 214 | if (offset >= MAC_TABLE_LEN) |
| 215 | offset = 0; |
| 216 | |
| 217 | next_client_ip++; |
| 218 | if (next_client_ip > DHCPD_CLIENT_IP_MAX) |
| 219 | next_client_ip = DHCPD_CLIENT_IP_MIN; |
| 220 | |
| 221 | DEBUG_PRINTF("create new ip: %d\n", (int)ip_addr_3); |
| 222 | DEBUG_PRINTF("next_client_ip %d\n", next_client_ip); |
| 223 | |
| 224 | _return: |
| 225 | return ip_addr_3; |
| 226 | } |
| 227 | |
| 228 | static void dhcpd_thread_entry(void *parameter) |
no test coverage detected