diff --git a/include/espressif/esp_misc.h b/include/espressif/esp_misc.h index 168c8003..881670da 100644 --- a/include/espressif/esp_misc.h +++ b/include/espressif/esp_misc.h @@ -23,4 +23,19 @@ void os_delay_us(uint16 us); void os_install_putc1(void (*p)(char c)); void os_putc(char c); +enum dhcp_status{ + DHCP_STOPPED, + DHCP_STARTED +}; + +struct dhcps_lease { + struct ip_addr start_ip; + struct ip_addr end_ip; +}; + +enum dhcps_offer_option{ + OFFER_START = 0x00, + OFFER_ROUTER = 0x01, + OFFER_END +}; #endif diff --git a/include/espressif/esp_softap.h b/include/espressif/esp_softap.h index 4d3a4346..e6f2a723 100644 --- a/include/espressif/esp_softap.h +++ b/include/espressif/esp_softap.h @@ -18,6 +18,18 @@ struct softap_config { }; bool wifi_softap_get_config(struct softap_config *config); +bool wifi_softap_get_config_default(struct softap_config *config); bool wifi_softap_set_config(struct softap_config *config); +bool wifi_softap_set_config_current(struct softap_config *config); + +uint8 wifi_softap_get_station_num(void); +struct station_info * wifi_softap_get_station_info(void); +void wifi_softap_free_station_info(void); + +bool wifi_softap_dhcps_start(void); +bool wifi_softap_dhcps_stop(void); +enum dhcp_status wifi_softap_dhcps_status(void); +bool wifi_softap_set_dhcps_lease(struct dhcps_lease *please); +bool wifi_softap_set_dhcps_offer_option(uint8 level, void* optarg); #endif diff --git a/include/espressif/esp_sta.h b/include/espressif/esp_sta.h index 06ea2880..d4bf0af0 100644 --- a/include/espressif/esp_sta.h +++ b/include/espressif/esp_sta.h @@ -16,7 +16,9 @@ struct station_config { }; bool wifi_station_get_config(struct station_config *config); +bool wifi_station_get_config_default(struct station_config *config); bool wifi_station_set_config(struct station_config *config); +bool wifi_station_set_config_current(struct station_config *config); bool wifi_station_connect(void); bool wifi_station_disconnect(void); @@ -46,6 +48,9 @@ bool wifi_station_scan(struct scan_config *config, scan_done_cb_t cb); uint8 wifi_station_get_auto_connect(void); bool wifi_station_set_auto_connect(uint8 set); +bool wifi_station_set_reconnect_policy(bool set); +bool wifi_station_get_reconnect_policy(void); + enum { STATION_IDLE = 0, STATION_CONNECTING, @@ -57,4 +62,13 @@ enum { uint8 wifi_station_get_connect_status(void); +uint8 wifi_station_get_current_ap_id(void); +bool wifi_station_ap_change(uint8 current_ap_id); +bool wifi_station_ap_number_set(uint8 ap_number); +uint8 wifi_station_get_ap_info(struct station_config config[]); + +bool wifi_station_dhcpc_start(void); +bool wifi_station_dhcpc_stop(void); +enum dhcp_status wifi_station_dhcpc_status(void); + #endif diff --git a/include/espressif/esp_system.h b/include/espressif/esp_system.h index f1283e48..e9e42f0e 100644 --- a/include/espressif/esp_system.h +++ b/include/espressif/esp_system.h @@ -9,12 +9,12 @@ #include "c_types.h" enum rst_reason { - DEFAULT_RST_FLAG = 0, - WDT_RST_FLAG, - EXCEPTION_RST_FLAG, - SOFT_WDT_RST_FLAG, - SOFT_RESTART_FLAG, - DEEP_SLEEP_AWAKE_FLAG + REASON_DEFAULT_RST = 0, + REASON_WDT_RST, + REASON_EXCEPTION_RST, + REASON_SOFT_WDT_RST, + REASON_SOFT_RESTART, + REASON_DEEP_SLEEP_AWAKE }; struct rst_info{ @@ -38,6 +38,22 @@ void system_restart(void); void system_deep_sleep(uint32 time_in_us); bool system_deep_sleep_set_option(uint8 option); +#define SYS_BOOT_ENHANCE_MODE 0 +#define SYS_BOOT_NORMAL_MODE 1 + +#define SYS_BOOT_NORMAL_BIN 0 +#define SYS_BOOT_TEST_BIN 1 + +uint8 system_get_boot_version(void); +uint32 system_get_userbin_addr(void); +uint8 system_get_boot_mode(void); +bool system_restart_enhance(uint8 bin_type, uint32 bin_addr); + +uint8 system_upgrade_userbin_check(void); +void system_upgrade_reboot(void); +uint8 system_upgrade_flag_check(); +void system_upgrade_flag_set(uint8 flag); + uint32 system_get_time(void); void system_print_meminfo(void); @@ -72,4 +88,8 @@ enum flash_size_map system_get_flash_size_map(void); bool system_param_save_with_protect(uint16 start_sec, void *param, uint16 len); bool system_param_load(uint16 start_sec, uint16 offset, void *param, uint16 len); +void system_phy_set_max_tpw(uint8 max_tpw); +void system_phy_set_tpw_via_vdd33(uint16 vdd33); +void system_phy_set_rfoption(uint8 option); + #endif diff --git a/include/espressif/esp_timer.h b/include/espressif/esp_timer.h index 82677fd2..42f9785c 100644 --- a/include/espressif/esp_timer.h +++ b/include/espressif/esp_timer.h @@ -11,7 +11,7 @@ typedef void os_timer_func_t(void *timer_arg); typedef struct _os_timer_t { struct _os_timer_t *timer_next; - void *freerots_handle; + void *timer_handle; uint32 timer_expire; uint32 timer_period; os_timer_func_t *timer_func; @@ -19,4 +19,8 @@ typedef struct _os_timer_t { void *timer_arg; } os_timer_t; +void os_timer_setfn(os_timer_t *ptimer, os_timer_func_t *pfunction, void *parg); +void os_timer_arm(os_timer_t *ptimer, uint32_t msec, bool repeat_flag); +void os_timer_disarm(os_timer_t *ptimer); + #endif diff --git a/include/espressif/esp_wifi.h b/include/espressif/esp_wifi.h index 3a65d870..331cdc1a 100644 --- a/include/espressif/esp_wifi.h +++ b/include/espressif/esp_wifi.h @@ -24,7 +24,9 @@ typedef enum _auth_mode { } AUTH_MODE; uint8 wifi_get_opmode(void); +uint8 wifi_get_opmode_default(void); bool wifi_set_opmode(uint8 opmode); +bool wifi_set_opmode_current(uint8 opmode); enum { STATION_IF = 0, @@ -48,6 +50,7 @@ uint8 wifi_get_channel(void); bool wifi_set_channel(uint8 channel); void wifi_status_led_install(uint8 gpio_id, uint32 gpio_name, uint8 gpio_func); +void wifi_status_led_uninstall(void); bool wifi_promiscuous_set_mac(const uint8_t *address); diff --git a/include/lwip/arch/cc.h b/include/lwip/arch/cc.h index 9de537d6..0bbf7cc6 100644 --- a/include/lwip/arch/cc.h +++ b/include/lwip/arch/cc.h @@ -38,6 +38,7 @@ #define EFAULT 14 +#define ERRNO #define LWIP_PROVIDE_ERRNO #if (1) diff --git a/include/lwip/lwip/dhcpserver.h b/include/lwip/lwip/dhcpserver.h new file mode 100644 index 00000000..8e68e3e0 --- /dev/null +++ b/include/lwip/lwip/dhcpserver.h @@ -0,0 +1,99 @@ +#ifndef __DHCPS_H__ +#define __DHCPS_H__ + +#define USE_DNS + +typedef struct dhcps_state{ + sint16_t state; +} dhcps_state; + +// ����dhcpclient�Զ����һ��DHCP msg�ṹ�� +typedef struct dhcps_msg { + uint8_t op, htype, hlen, hops; + uint8_t xid[4]; + uint16_t secs, flags; + uint8_t ciaddr[4]; + uint8_t yiaddr[4]; + uint8_t siaddr[4]; + uint8_t giaddr[4]; + uint8_t chaddr[16]; + uint8_t sname[64]; + uint8_t file[128]; + uint8_t options[312]; +}dhcps_msg; + +#ifndef LWIP_OPEN_SRC +struct dhcps_lease { + struct ip_addr start_ip; + struct ip_addr end_ip; +}; + +enum dhcps_offer_option{ + OFFER_START = 0x00, + OFFER_ROUTER = 0x01, + OFFER_END +}; +#endif + +struct dhcps_pool{ + struct ip_addr ip; + uint8 mac[6]; + uint32 lease_timer; +}; + +typedef struct _list_node{ + void *pnode; + struct _list_node *pnext; +}list_node; + +#define DHCPS_LEASE_TIMER 0x05A0 +#define DHCPS_MAX_LEASE 0x64 +#define BOOTP_BROADCAST 0x8000 + +#define DHCP_REQUEST 1 +#define DHCP_REPLY 2 +#define DHCP_HTYPE_ETHERNET 1 +#define DHCP_HLEN_ETHERNET 6 +#define DHCP_MSG_LEN 236 + +#define DHCPS_SERVER_PORT 67 +#define DHCPS_CLIENT_PORT 68 + +#define DHCPDISCOVER 1 +#define DHCPOFFER 2 +#define DHCPREQUEST 3 +#define DHCPDECLINE 4 +#define DHCPACK 5 +#define DHCPNAK 6 +#define DHCPRELEASE 7 + +#define DHCP_OPTION_SUBNET_MASK 1 +#define DHCP_OPTION_ROUTER 3 +#define DHCP_OPTION_DNS_SERVER 6 +#define DHCP_OPTION_REQ_IPADDR 50 +#define DHCP_OPTION_LEASE_TIME 51 +#define DHCP_OPTION_MSG_TYPE 53 +#define DHCP_OPTION_SERVER_ID 54 +#define DHCP_OPTION_INTERFACE_MTU 26 +#define DHCP_OPTION_PERFORM_ROUTER_DISCOVERY 31 +#define DHCP_OPTION_BROADCAST_ADDRESS 28 +#define DHCP_OPTION_REQ_LIST 55 +#define DHCP_OPTION_END 255 + +//#define USE_CLASS_B_NET 1 +#define DHCPS_DEBUG 0 +#define MAX_STATION_NUM 8 + +#define DHCPS_STATE_OFFER 1 +#define DHCPS_STATE_DECLINE 2 +#define DHCPS_STATE_ACK 3 +#define DHCPS_STATE_NAK 4 +#define DHCPS_STATE_IDLE 5 + +#define dhcps_router_enabled(offer) ((offer & OFFER_ROUTER) != 0) + +void dhcps_start(struct ip_info *info); +void dhcps_stop(void); + +#endif + diff --git a/include/lwip/lwip/netif.h b/include/lwip/lwip/netif.h index a039b1dc..d28d8bed 100644 --- a/include/lwip/lwip/netif.h +++ b/include/lwip/lwip/netif.h @@ -207,6 +207,7 @@ struct netif { #if LWIP_DHCP /** the DHCP client state information for this netif */ struct dhcp *dhcp; + struct udp_pcb *dhcps_pcb; //dhcps #endif /* LWIP_DHCP */ #if LWIP_AUTOIP /** the AutoIP client state information for this netif */ diff --git a/lib/libfreertos.a b/lib/libfreertos.a index 81138ae4..ef3ad901 100644 Binary files a/lib/libfreertos.a and b/lib/libfreertos.a differ diff --git a/lib/libjson.a b/lib/libjson.a index d286e032..4aec12f1 100644 Binary files a/lib/libjson.a and b/lib/libjson.a differ diff --git a/lib/liblwip.a b/lib/liblwip.a index f764d730..7529eb4a 100644 Binary files a/lib/liblwip.a and b/lib/liblwip.a differ diff --git a/lib/libmain.a b/lib/libmain.a index d6333d65..1e9e46ae 100644 Binary files a/lib/libmain.a and b/lib/libmain.a differ diff --git a/lib/libnet80211.a b/lib/libnet80211.a index 402a63e1..598fd48a 100644 Binary files a/lib/libnet80211.a and b/lib/libnet80211.a differ diff --git a/lib/libphy.a b/lib/libphy.a index f38bee97..5bbd7d0c 100644 Binary files a/lib/libphy.a and b/lib/libphy.a differ diff --git a/lib/libpp.a b/lib/libpp.a index d21619f1..2dd87eed 100644 Binary files a/lib/libpp.a and b/lib/libpp.a differ diff --git a/lib/libsmartconfig.a b/lib/libsmartconfig.a index 4cb357c7..a770dce6 100644 Binary files a/lib/libsmartconfig.a and b/lib/libsmartconfig.a differ diff --git a/lib/libssl.a b/lib/libssl.a index 6f772360..e81cf91f 100644 Binary files a/lib/libssl.a and b/lib/libssl.a differ diff --git a/lib/libudhcp.a b/lib/libudhcp.a deleted file mode 100644 index 6194070f..00000000 Binary files a/lib/libudhcp.a and /dev/null differ diff --git a/lib/libwpa.a b/lib/libwpa.a index a3899b02..e59cc511 100644 Binary files a/lib/libwpa.a and b/lib/libwpa.a differ diff --git a/third_party/udhcp/Makefile b/third_party/udhcp/Makefile deleted file mode 100644 index 86ea96fa..00000000 --- a/third_party/udhcp/Makefile +++ /dev/null @@ -1,46 +0,0 @@ - -############################################################# -# Required variables for each makefile -# Discard this section from all parent makefiles -# Expected variables (with automatic defaults): -# CSRCS (all "C" files in the dir) -# SUBDIRS (all subdirs with a Makefile) -# GEN_LIBS - list of libs to be generated () -# GEN_IMAGES - list of images to be generated () -# COMPONENTS_xxx - a list of libs/objs in the form -# subdir/lib to be extracted and rolled up into -# a generated lib/image xxx.a () -# -ifndef PDIR - -GEN_LIBS = libudhcp.a - -endif - - -############################################################# -# Configuration i.e. compile options etc. -# Target specific stuff (defines etc.) goes in here! -# Generally values applying to a tree are captured in the -# makefile at its root level - these are then overridden -# for a subtree within the makefile rooted therein -# -#DEFINES += - -############################################################# -# Recursion Magic - Don't touch this!! -# -# Each subtree potentially has an include directory -# corresponding to the common APIs applicable to modules -# rooted at that subtree. Accordingly, the INCLUDE PATH -# of a module can only contain the include directories up -# its parent path, and not its siblings -# -# Required for each makefile to inherit from the parent -# - -INCLUDES := $(INCLUDES) -I $(PDIR)include -INCLUDES += -I ./ -PDIR := ../$(PDIR) -sinclude $(PDIR)Makefile - diff --git a/third_party/udhcp/arpping.c b/third_party/udhcp/arpping.c deleted file mode 100644 index 5dff7c80..00000000 --- a/third_party/udhcp/arpping.c +++ /dev/null @@ -1,132 +0,0 @@ -/* vi: set sw=4 ts=4: */ -/* - * Mostly stolen from: dhcpcd - DHCP client daemon - * by Yoichi Hariguchi - * - * Licensed under GPLv2, see file LICENSE in this source tree. - */ -#include "udhcp/common.h" -#include "udhcp/dhcpd.h" - -#define HWTYPE_ETHERNET 1 - -struct arpMsg { - /* Ethernet header */ - uint8_t h_dest[6]; /* 00 destination ether addr */ - uint8_t h_source[6]; /* 06 source ether addr */ - uint16_t h_proto; /* 0c packet type ID field */ - - /* ARP packet */ - uint16_t htype; /* 0e hardware type (must be ARPHRD_ETHER) */ - uint16_t ptype; /* 10 protocol type (must be ETH_P_IP) */ - uint8_t hlen; /* 12 hardware address length (must be 6) */ - uint8_t plen; /* 13 protocol address length (must be 4) */ - uint16_t operation; /* 14 ARP opcode */ - uint8_t sHaddr[6]; /* 16 sender's hardware address */ - uint8_t sInaddr[4]; /* 1c sender's IP address */ - uint8_t tHaddr[6]; /* 20 target's hardware address */ - uint8_t tInaddr[4]; /* 26 target's IP address */ - uint8_t pad[18]; /* 2a pad for min. ethernet payload (60 bytes) */ -}; - -enum { - ARP_MSG_SIZE = 0x2a -}; - -/* Returns 1 if no reply received */ -int ICACHE_FLASH_ATTR arpping(uint32_t test_nip, - const uint8_t *safe_mac, - uint32_t from_ip, - uint8_t *from_mac, - const char *interface) -{ - return 1; -#if 0 - int timeout_ms; - int s; - - int rv = 1; /* "no reply received" yet */ - struct sockaddr addr; /* for interface name */ - struct arpMsg arp; - - s = socket(AF_INET, SOCK_RAW, IPPROTO_IP); - if (s == -1) { - UDHCP_DEBUG("can not create raw_socket\n"); - return -1; - } - - if (setsockopt_broadcast(s) == -1) { - UDHCP_DEBUG("can't enable bcast on raw socket\n"); - goto ret; - } - - /* send arp request */ - memset(&arp, 0, sizeof(arp)); - memset(arp.h_dest, 0xff, 6); /* MAC DA */ - memcpy(arp.h_source, from_mac, 6); /* MAC SA */ - arp.h_proto = htons(ETHTYPE_IP); /* protocol type (Ethernet) */ - arp.htype = htons(HWTYPE_ETHERNET); /* hardware type */ - arp.ptype = htons(ETHTYPE_ARP); /* protocol type (ARP message) */ - arp.hlen = 6; /* hardware address length */ - arp.plen = 4; /* protocol address length */ - arp.operation = htons(ARP_REQUEST); /* ARP op code */ - memcpy(arp.sHaddr, from_mac, 6); /* source hardware address */ - memcpy(arp.sInaddr, &from_ip, sizeof(from_ip)); /* source IP address */ - /* tHaddr is zero-filled */ /* target hardware address */ - memcpy(arp.tInaddr, &test_nip, sizeof(test_nip));/* target IP address */ - - memset(&addr, 0, sizeof(addr)); - - strncpy(addr.sa_data, interface, sizeof(addr.sa_data)); - if (sendto(s, &arp, sizeof(arp), 0, &addr, sizeof(addr)) < 0) { - // TODO: error message? caller didn't expect us to fail, - // just returning 1 "no reply received" misleads it. - goto ret; - } - - /* wait for arp reply, and check it */ - timeout_ms = 2000; - do { - typedef uint32_t aliased_uint32_t; - int r; - unsigned prevTime = monotonic_ms(); - - r = safe_read(s, &arp, sizeof(arp)); - if (r < 0) - break; - - UDHCP_DEBUG("sHaddr %02x:%02x:%02x:%02x:%02x:%02x\n", - arp.sHaddr[0], arp.sHaddr[1], arp.sHaddr[2], - arp.sHaddr[3], arp.sHaddr[4], arp.sHaddr[5]); - - if (r >= ARP_MSG_SIZE - && arp.operation == htons(ARP_REPLY) - /* don't check it: Linux doesn't return proper tHaddr (fixed in 2.6.24?) */ - /* && memcmp(arp.tHaddr, from_mac, 6) == 0 */ - && *(aliased_uint32_t*)arp.sInaddr == test_nip - ) { - /* if ARP source MAC matches safe_mac - * (which is client's MAC), then it's not a conflict - * (client simply already has this IP and replies to ARPs!) - */ - if (!safe_mac || memcmp(safe_mac, arp.sHaddr, 6) != 0) - rv = 0; - else - UDHCP_DEBUG("sHaddr == safe_mac\n"); - break; - } - - timeout_ms -= (unsigned)monotonic_ms() - prevTime + 1; - - /* We used to check "timeout_ms > 0", but - * this is more under/overflow-resistant - * (people did see overflows here when system time jumps): - */ - } while ((unsigned)timeout_ms <= 2000); - - ret: - close(s); - UDHCP_DEBUG("%srp reply received for this address\n", rv ? "No a" : "A"); - return rv; -#endif -} diff --git a/third_party/udhcp/common.c b/third_party/udhcp/common.c deleted file mode 100644 index 48a3a9bc..00000000 --- a/third_party/udhcp/common.c +++ /dev/null @@ -1,594 +0,0 @@ -/* vi: set sw=4 ts=4: */ -/* - * Rewrite by Russ Dill July 2001 - * - * Licensed under GPLv2, see file LICENSE in this source tree. - */ -#include - -#include "udhcp/common.h" - -#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1 -unsigned dhcp_verbose; -#endif - -/* We use it for "global" data via *(struct global*)&bb_common_bufsiz1. - * Since gcc insists on aligning struct global's members, it would be a pity - * (and an alignment fault on some CPUs) to mess it up. */ -char bb_common_bufsiz1[COMMON_BUFSIZE]; - -const uint8_t MAC_BCAST_ADDR[6] = { - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff -}; - -/* Supported options are easily added here. - * See RFC2132 for more options. - * OPTION_REQ: these options are requested by udhcpc (unless -o). - */ -const struct dhcp_optflag dhcp_optflags[] = { - /* flags code */ - { OPTION_IP | OPTION_REQ, 0x01 }, /* DHCP_SUBNET */ - { OPTION_S32 , 0x02 }, /* DHCP_TIME_OFFSET */ - { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x03 }, /* DHCP_ROUTER */ -// { OPTION_IP | OPTION_LIST , 0x04 }, /* DHCP_TIME_SERVER */ -// { OPTION_IP | OPTION_LIST , 0x05 }, /* DHCP_NAME_SERVER */ - { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x06 }, /* DHCP_DNS_SERVER */ -// { OPTION_IP | OPTION_LIST , 0x07 }, /* DHCP_LOG_SERVER */ -// { OPTION_IP | OPTION_LIST , 0x08 }, /* DHCP_COOKIE_SERVER */ - { OPTION_IP | OPTION_LIST , 0x09 }, /* DHCP_LPR_SERVER */ - { OPTION_STRING_HOST | OPTION_REQ, 0x0c }, /* DHCP_HOST_NAME */ - { OPTION_U16 , 0x0d }, /* DHCP_BOOT_SIZE */ - { OPTION_STRING_HOST | OPTION_REQ, 0x0f }, /* DHCP_DOMAIN_NAME */ - { OPTION_IP , 0x10 }, /* DHCP_SWAP_SERVER */ - { OPTION_STRING , 0x11 }, /* DHCP_ROOT_PATH */ - { OPTION_U8 , 0x17 }, /* DHCP_IP_TTL */ - { OPTION_U16 , 0x1a }, /* DHCP_MTU */ -//TODO: why do we request DHCP_BROADCAST? Can't we assume that -//in the unlikely case it is different from typical N.N.255.255, -//server would let us know anyway? - { OPTION_IP | OPTION_REQ, 0x1c }, /* DHCP_BROADCAST */ - { OPTION_IP_PAIR | OPTION_LIST , 0x21 }, /* DHCP_ROUTES */ - { OPTION_STRING_HOST , 0x28 }, /* DHCP_NIS_DOMAIN */ - { OPTION_IP | OPTION_LIST , 0x29 }, /* DHCP_NIS_SERVER */ - { OPTION_IP | OPTION_LIST | OPTION_REQ, 0x2a }, /* DHCP_NTP_SERVER */ - { OPTION_IP | OPTION_LIST , 0x2c }, /* DHCP_WINS_SERVER */ - { OPTION_U32 , 0x33 }, /* DHCP_LEASE_TIME */ - { OPTION_IP , 0x36 }, /* DHCP_SERVER_ID */ - { OPTION_STRING , 0x38 }, /* DHCP_ERR_MESSAGE */ -//TODO: must be combined with 'sname' and 'file' handling: - { OPTION_STRING_HOST , 0x42 }, /* DHCP_TFTP_SERVER_NAME */ - { OPTION_STRING , 0x43 }, /* DHCP_BOOT_FILE */ -//TODO: not a string, but a set of LASCII strings: -// { OPTION_STRING , 0x4D }, /* DHCP_USER_CLASS */ -#if ENABLE_FEATURE_UDHCP_RFC3397 - { OPTION_DNS_STRING | OPTION_LIST , 0x77 }, /* DHCP_DOMAIN_SEARCH */ - { OPTION_SIP_SERVERS , 0x78 }, /* DHCP_SIP_SERVERS */ -#endif - { OPTION_STATIC_ROUTES | OPTION_LIST , 0x79 }, /* DHCP_STATIC_ROUTES */ -#if ENABLE_FEATURE_UDHCP_8021Q - { OPTION_U16 , 0x84 }, /* DHCP_VLAN_ID */ - { OPTION_U8 , 0x85 }, /* DHCP_VLAN_PRIORITY */ -#endif - { OPTION_STRING , 0xd1 }, /* DHCP_PXE_CONF_FILE */ - { OPTION_6RD , 0xd4 }, /* DHCP_6RD */ - { OPTION_STATIC_ROUTES | OPTION_LIST , 0xf9 }, /* DHCP_MS_STATIC_ROUTES */ - { OPTION_STRING , 0xfc }, /* DHCP_WPAD */ - - /* Options below have no match in dhcp_option_strings[], - * are not passed to dhcpc scripts, and cannot be specified - * with "option XXX YYY" syntax in dhcpd config file. - * These entries are only used internally by udhcp[cd] - * to correctly encode options into packets. - */ - - { OPTION_IP , 0x32 }, /* DHCP_REQUESTED_IP */ - { OPTION_U8 , 0x35 }, /* DHCP_MESSAGE_TYPE */ - { OPTION_U16 , 0x39 }, /* DHCP_MAX_SIZE */ -//looks like these opts will work just fine even without these defs: -// { OPTION_STRING , 0x3c }, /* DHCP_VENDOR */ -// /* not really a string: */ -// { OPTION_STRING , 0x3d }, /* DHCP_CLIENT_ID */ - { 0, 0 } /* zeroed terminating entry */ -}; - -/* Used for converting options from incoming packets to env variables - * for udhcpc stript, and for setting options for udhcpd via - * "opt OPTION_NAME OPTION_VALUE" directives in udhcpd.conf file. - */ -/* Must match dhcp_optflags[] order */ -const char dhcp_option_strings[] = - "subnet" "\0" /* DHCP_SUBNET */ - "timezone" "\0" /* DHCP_TIME_OFFSET */ - "router" "\0" /* DHCP_ROUTER */ -// "timesrv" "\0" /* DHCP_TIME_SERVER */ -// "namesrv" "\0" /* DHCP_NAME_SERVER */ - "dns" "\0" /* DHCP_DNS_SERVER */ -// "logsrv" "\0" /* DHCP_LOG_SERVER */ -// "cookiesrv" "\0" /* DHCP_COOKIE_SERVER */ - "lprsrv" "\0" /* DHCP_LPR_SERVER */ - "hostname" "\0" /* DHCP_HOST_NAME */ - "bootsize" "\0" /* DHCP_BOOT_SIZE */ - "domain" "\0" /* DHCP_DOMAIN_NAME */ - "swapsrv" "\0" /* DHCP_SWAP_SERVER */ - "rootpath" "\0" /* DHCP_ROOT_PATH */ - "ipttl" "\0" /* DHCP_IP_TTL */ - "mtu" "\0" /* DHCP_MTU */ - "broadcast" "\0" /* DHCP_BROADCAST */ - "routes" "\0" /* DHCP_ROUTES */ - "nisdomain" "\0" /* DHCP_NIS_DOMAIN */ - "nissrv" "\0" /* DHCP_NIS_SERVER */ - "ntpsrv" "\0" /* DHCP_NTP_SERVER */ - "wins" "\0" /* DHCP_WINS_SERVER */ - "lease" "\0" /* DHCP_LEASE_TIME */ - "serverid" "\0" /* DHCP_SERVER_ID */ - "message" "\0" /* DHCP_ERR_MESSAGE */ - "tftp" "\0" /* DHCP_TFTP_SERVER_NAME */ - "bootfile" "\0" /* DHCP_BOOT_FILE */ -// "userclass" "\0" /* DHCP_USER_CLASS */ -#if ENABLE_FEATURE_UDHCP_RFC3397 - "search" "\0" /* DHCP_DOMAIN_SEARCH */ -// doesn't work in udhcpd.conf since OPTION_SIP_SERVERS -// is not handled yet by "string->option" conversion code: - "sipsrv" "\0" /* DHCP_SIP_SERVERS */ -#endif - "staticroutes" "\0"/* DHCP_STATIC_ROUTES */ -#if ENABLE_FEATURE_UDHCP_8021Q - "vlanid" "\0" /* DHCP_VLAN_ID */ - "vlanpriority" "\0"/* DHCP_VLAN_PRIORITY */ -#endif - "pxeconffile" "\0" /* DHCP_PXE_CONF_FILE */ - "ip6rd" "\0" /* DHCP_6RD */ - "msstaticroutes""\0"/* DHCP_MS_STATIC_ROUTES */ - "wpad" "\0" /* DHCP_WPAD */ - ; - -/* Lengths of the option types in binary form. - * Used by: - * udhcp_str2optset: to determine how many bytes to allocate. - * xmalloc_optname_optval: to estimate string length - * from binary option length: (option[LEN] / dhcp_option_lengths[opt_type]) - * is the number of elements, multiply in by one element's string width - * (len_of_option_as_string[opt_type]) and you know how wide string you need. - */ -const uint8_t dhcp_option_lengths[] = { - [OPTION_IP] = 4, - [OPTION_IP_PAIR] = 8, -// [OPTION_BOOLEAN] = 1, - [OPTION_STRING] = 1, /* ignored by udhcp_str2optset */ - [OPTION_STRING_HOST] = 1, /* ignored by udhcp_str2optset */ -#if ENABLE_FEATURE_UDHCP_RFC3397 - [OPTION_DNS_STRING] = 1, /* ignored by both udhcp_str2optset and xmalloc_optname_optval */ - [OPTION_SIP_SERVERS] = 1, -#endif - [OPTION_U8] = 1, - [OPTION_U16] = 2, -// [OPTION_S16] = 2, - [OPTION_U32] = 4, - [OPTION_S32] = 4, - /* Just like OPTION_STRING, we use minimum length here */ - [OPTION_STATIC_ROUTES] = 5, - [OPTION_6RD] = 22, /* ignored by udhcp_str2optset */ -}; - - -#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2 -static void log_option(const char *pfx, const uint8_t *opt) -{ - if (dhcp_verbose >= 2) { - char buf[256 * 2 + 2]; - *bin2hex(buf, (void*) (opt + OPT_DATA), opt[OPT_LEN]) = '\0'; - UDHCP_DEBUG("%s: 0x%02x %s", pfx, opt[OPT_CODE], buf); - } -} -#else -# define log_option(pfx, opt) ((void)0) -#endif -#if 0 -unsigned ICACHE_FLASH_ATTR udhcp_option_idx(const char *name) -{ - int n = index_in_strings(dhcp_option_strings, name); - if (n >= 0) - return n; - - { - char buf[sizeof(dhcp_option_strings)]; - char *d = buf; - const char *s = dhcp_option_strings; - while (s < dhcp_option_strings + sizeof(dhcp_option_strings) - 2) { - *d++ = (*s == '\0' ? ' ' : *s); - s++; - } - *d = '\0'; - UDHCP_DEBUG("unknown option '%s', known options: %s", name, buf); - } -} -#endif -/* Get an option with bounds checking (warning, result is not aligned) */ -uint8_t* ICACHE_FLASH_ATTR udhcp_get_option(struct dhcp_packet *packet, int code) -{ - uint8_t *optionptr; - int len; - int rem; - int overload = 0; - enum { - FILE_FIELD101 = FILE_FIELD * 0x101, - SNAME_FIELD101 = SNAME_FIELD * 0x101, - }; - - /* option bytes: [code][len][data1][data2]..[dataLEN] */ - optionptr = packet->options; - rem = sizeof(packet->options); - while (1) { - if (rem <= 0) { - UDHCP_DEBUG("bad packet, malformed option field\n"); - return NULL; - } - if (optionptr[OPT_CODE] == DHCP_PADDING) { - rem--; - optionptr++; - continue; - } - if (optionptr[OPT_CODE] == DHCP_END) { - if ((overload & FILE_FIELD101) == FILE_FIELD) { - /* can use packet->file, and didn't look at it yet */ - overload |= FILE_FIELD101; /* "we looked at it" */ - optionptr = packet->file; - rem = sizeof(packet->file); - continue; - } - if ((overload & SNAME_FIELD101) == SNAME_FIELD) { - /* can use packet->sname, and didn't look at it yet */ - overload |= SNAME_FIELD101; /* "we looked at it" */ - optionptr = packet->sname; - rem = sizeof(packet->sname); - continue; - } - break; - } - len = 2 + optionptr[OPT_LEN]; - rem -= len; - if (rem < 0) - continue; /* complain and return NULL */ - - if (optionptr[OPT_CODE] == code) { - UDHCP_DEBUG("Option found 0x%02x\n", optionptr[OPT_CODE]); - return optionptr + OPT_DATA; - } - - if (optionptr[OPT_CODE] == DHCP_OPTION_OVERLOAD) { - overload |= optionptr[OPT_DATA]; - /* fall through */ - } - optionptr += len; - } - - /* log3 because udhcpc uses it a lot - very noisy */ - UDHCP_DEBUG("Option 0x%02x not found\n", code); - return NULL; -} - -/* Return the position of the 'end' option (no bounds checking) */ -int ICACHE_FLASH_ATTR udhcp_end_option(uint8_t *optionptr) -{ - int i = 0; - - while (optionptr[i] != DHCP_END) { - if (optionptr[i] != DHCP_PADDING) - i += optionptr[i + OPT_LEN] + OPT_DATA-1; - i++; - } - return i; -} - -/* Add an option (supplied in binary form) to the options. - * Option format: [code][len][data1][data2]..[dataLEN] - */ -void ICACHE_FLASH_ATTR udhcp_add_binary_option(struct dhcp_packet *packet, uint8_t *addopt) -{ - unsigned len; - uint8_t *optionptr = packet->options; - unsigned end = udhcp_end_option(optionptr); - - len = OPT_DATA + addopt[OPT_LEN]; - /* end position + (option code/length + addopt length) + end option */ - if (end + len + 1 >= DHCP_OPTIONS_BUFSIZE) { -//TODO: learn how to use overflow option if we exhaust packet->options[] - UDHCP_DEBUG("option 0x%02x did not fit into the packet", - addopt[OPT_CODE]); - return; - } - log_option("Adding option", addopt); - memcpy(optionptr + end, addopt, len); - optionptr[end + len] = DHCP_END; -} - -/* Add an one to four byte option to a packet */ -void ICACHE_FLASH_ATTR udhcp_add_simple_option(struct dhcp_packet *packet, uint8_t code, uint32_t data) -{ - const struct dhcp_optflag *dh; - - for (dh = dhcp_optflags; dh->code; dh++) { - if (dh->code == code) { - uint8_t option[6], len; - - option[OPT_CODE] = code; - len = dhcp_option_lengths[dh->flags & OPTION_TYPE_MASK]; - option[OPT_LEN] = len; - //if (BB_BIG_ENDIAN) - // data <<= 8 * (4 - len); - /* Assignment is unaligned! */ - move_to_unaligned32(&option[OPT_DATA], data); - udhcp_add_binary_option(packet, option); - return; - } - } - - UDHCP_DEBUG("can't add option 0x%02x", code); -} - -/* Find option 'code' in opt_list */ -struct option_set* ICACHE_FLASH_ATTR udhcp_find_option(struct option_set *opt_list, uint8_t code) -{ - while (opt_list && opt_list->data[OPT_CODE] < code) - opt_list = opt_list->next; - - if (opt_list && opt_list->data[OPT_CODE] == code) - return opt_list; - return NULL; -} - -/* Parse string to IP in network order */ -int ICACHE_FLASH_ATTR udhcp_str2nip(const char *str, void *arg) -{ - ip_addr_t lsa; - - ipaddr_aton(str, &lsa); -// if (!lsa) -// return 0; - /* arg maybe unaligned */ - move_to_unaligned32((u32_t*)arg, lsa.addr); -// free(lsa); - return 1; -} - -/* udhcp_str2optset: - * Parse string option representation to binary form and add it to opt_list. - * Called to parse "udhcpc -x OPTNAME:OPTVAL" - * and to parse udhcpd.conf's "opt OPTNAME OPTVAL" directives. - */ -/* helper for the helper */ -static char *ICACHE_FLASH_ATTR allocate_tempopt_if_needed( - const struct dhcp_optflag *optflag, - char *buffer, - int *length_p) -{ - char *allocated = NULL; - if ((optflag->flags & OPTION_TYPE_MASK) == OPTION_BIN) { - const char *end; -// allocated = xstrdup(buffer); /* more than enough */ -// end = hex2bin(allocated, buffer, 255); - if (errno) - UDHCP_DEBUG("malformed hex string '%s'", buffer); - *length_p = end - allocated; - } - return allocated; -} -/* helper: add an option to the opt_list */ -static void ICACHE_FLASH_ATTR attach_option( - struct option_set **opt_list, - const struct dhcp_optflag *optflag, - char *buffer, - int length) -{ - struct option_set *existing; - char *allocated; - - allocated = allocate_tempopt_if_needed(optflag, buffer, &length); -#if ENABLE_FEATURE_UDHCP_RFC3397 - if ((optflag->flags & OPTION_TYPE_MASK) == OPTION_DNS_STRING) { - /* reuse buffer and length for RFC1035-formatted string */ - allocated = buffer = (char *)dname_enc(NULL, 0, buffer, &length); - } -#endif - - existing = udhcp_find_option(*opt_list, optflag->code); - if (!existing) { - struct option_set *new, **curr; - - /* make a new option */ - UDHCP_DEBUG("Attaching option %02x to list", optflag->code); - new = (struct option_set *)zalloc(sizeof(*new)); - new->data = (uint8_t *)zalloc(length + OPT_DATA); - new->data[OPT_CODE] = optflag->code; - new->data[OPT_LEN] = length; - memcpy(new->data + OPT_DATA, (allocated ? allocated : buffer), length); - - curr = opt_list; - while (*curr && (*curr)->data[OPT_CODE] < optflag->code) - curr = &(*curr)->next; - - new->next = *curr; - *curr = new; - goto ret; - } - - if (optflag->flags & OPTION_LIST) { - unsigned old_len; - - /* add it to an existing option */ - UDHCP_DEBUG("Attaching option %02x to existing member of list", optflag->code); - old_len = existing->data[OPT_LEN]; - if (old_len + length < 255) { - /* actually 255 is ok too, but adding a space can overlow it */ - - existing->data = (uint8_t *)realloc(existing->data, OPT_DATA + 1 + old_len + length); - if ((optflag->flags & OPTION_TYPE_MASK) == OPTION_STRING - || (optflag->flags & OPTION_TYPE_MASK) == OPTION_STRING_HOST - ) { - /* add space separator between STRING options in a list */ - existing->data[OPT_DATA + old_len] = ' '; - old_len++; - } - memcpy(existing->data + OPT_DATA + old_len, (allocated ? allocated : buffer), length); - existing->data[OPT_LEN] = old_len + length; - } /* else, ignore the data, we could put this in a second option in the future */ - } /* else, ignore the new data */ - - ret: - free(allocated); -} - -int ICACHE_FLASH_ATTR udhcp_str2optset(const char *const_str, void *arg) -{ - UDHCP_DEBUG("udhcp_str2optset %s %p\n", const_str, arg); - return 0; -#if 0 - struct option_set **opt_list = arg; - char *opt, *val; - char *str; - const struct dhcp_optflag *optflag; - struct dhcp_optflag bin_optflag; - unsigned optcode; - int retval, length; - /* IP_PAIR needs 8 bytes, STATIC_ROUTES needs 9 max */ - char buffer[9]; - uint16_t *result_u16 = (uint16_t *) buffer; - uint32_t *result_u32 = (uint32_t *) buffer; - - /* Cheat, the only *const* str possible is "" */ - str = (char *) const_str; - opt = strtok(str, " \t="); - if (!opt) - return 0; - - optcode = bb_strtou(opt, NULL, 0); - if (!errno && optcode < 255) { - /* Raw (numeric) option code */ - bin_optflag.flags = OPTION_BIN; - bin_optflag.code = optcode; - optflag = &bin_optflag; - } else { - optflag = &dhcp_optflags[udhcp_option_idx(opt)]; - } - - retval = 0; - do { - val = strtok(NULL, ", \t"); - if (!val) - break; - length = dhcp_option_lengths[optflag->flags & OPTION_TYPE_MASK]; - retval = 0; - opt = buffer; /* new meaning for variable opt */ - switch (optflag->flags & OPTION_TYPE_MASK) { - case OPTION_IP: - retval = udhcp_str2nip(val, buffer); - break; - case OPTION_IP_PAIR: - retval = udhcp_str2nip(val, buffer); - val = strtok(NULL, ", \t/-"); - if (!val) - retval = 0; - if (retval) - retval = udhcp_str2nip(val, buffer + 4); - break; - case OPTION_STRING: - case OPTION_STRING_HOST: -#if ENABLE_FEATURE_UDHCP_RFC3397 - case OPTION_DNS_STRING: -#endif - length = strnlen(val, 254); - if (length > 0) { - opt = val; - retval = 1; - } - break; -// case OPTION_BOOLEAN: { -// static const char no_yes[] ALIGN1 = "no\0yes\0"; -// buffer[0] = retval = index_in_strings(no_yes, val); -// retval++; /* 0 - bad; 1: "no" 2: "yes" */ -// break; -// } - case OPTION_U8: - buffer[0] = bb_strtou32(val, NULL, 0); - retval = (errno == 0); - break; - /* htonX are macros in older libc's, using temp var - * in code below for safety */ - /* TODO: use bb_strtoX? */ - case OPTION_U16: { - uint32_t tmp;// = bb_strtou32(val, NULL, 0); - *result_u16 = htons(tmp); - retval = (errno == 0 /*&& tmp < 0x10000*/); - break; - } -// case OPTION_S16: { -// long tmp = bb_strtoi32(val, NULL, 0); -// *result_u16 = htons(tmp); -// retval = (errno == 0); -// break; -// } - case OPTION_U32: { - uint32_t tmp = bb_strtou32(val, NULL, 0); - *result_u32 = htonl(tmp); - retval = (errno == 0); - break; - } - case OPTION_S32: { - sint32_t tmp = bb_strtoi32(val, NULL, 0); - *result_u32 = htonl(tmp); - retval = (errno == 0); - break; - } - case OPTION_STATIC_ROUTES: { - /* Input: "a.b.c.d/m" */ - /* Output: mask(1 byte),pfx(0-4 bytes),gw(4 bytes) */ - unsigned mask; - char *slash = strchr(val, '/'); - if (slash) { - *slash = '\0'; - retval = udhcp_str2nip(val, buffer + 1); - buffer[0] = mask = bb_strtou(slash + 1, NULL, 10); - val = strtok(NULL, ", \t/-"); - if (!val || mask > 32 || errno) - retval = 0; - if (retval) { - length = ((mask + 7) >> 3) + 5; - retval = udhcp_str2nip(val, buffer + (length - 4)); - } - } - break; - } - case OPTION_BIN: /* handled in attach_option() */ - opt = val; - retval = 1; - break; - default: - break; - } - if (retval) - attach_option(opt_list, optflag, opt, length); - } while (retval && (optflag->flags & OPTION_LIST)); - - return retval; -#endif -} - -/* note: ip is a pointer to an IPv6 in network order, possibly misaliged */ -int ICACHE_FLASH_ATTR sprint_nip6(char *dest, /*const char *pre,*/ const uint8_t *ip) -{ - char hexstrbuf[16 * 2]; -// bin2hex(hexstrbuf, (void*)ip, 16); -// return sprintf(dest, /* "%s" */ -// "%.4s:%.4s:%.4s:%.4s:%.4s:%.4s:%.4s:%.4s", -// /* pre, */ -// hexstrbuf + 0 * 4, -// hexstrbuf + 1 * 4, -// hexstrbuf + 2 * 4, -// hexstrbuf + 3 * 4, -// hexstrbuf + 4 * 4, -// hexstrbuf + 5 * 4, -// hexstrbuf + 6 * 4, -// hexstrbuf + 7 * 4 -// ); - return 0; -} diff --git a/third_party/udhcp/dhcpd.c b/third_party/udhcp/dhcpd.c deleted file mode 100644 index 80d7710e..00000000 --- a/third_party/udhcp/dhcpd.c +++ /dev/null @@ -1,778 +0,0 @@ -/* vi: set sw=4 ts=4: */ -/* - * udhcp server - * Copyright (C) 1999 Matthew Ramsay - * Chris Trew - * - * Rewrite by Russ Dill July 2001 - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - */ - -//usage:#define udhcpd_trivial_usage -//usage: "[-fS] [-I ADDR]" IF_FEATURE_UDHCP_PORT(" [-P N]") " [CONFFILE]" -//usage:#define udhcpd_full_usage "\n\n" -//usage: "DHCP server\n" -//usage: "\n -f Run in foreground" -//usage: "\n -S Log to syslog too" -//usage: "\n -I ADDR Local address" -//usage: IF_FEATURE_UDHCP_PORT( -//usage: "\n -P N Use port N (default 67)" -//usage: ) - -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/queue.h" - -#include "udhcp/common.h" -#include "udhcp/dhcpc.h" -#include "udhcp/dhcpd.h" - -int errno; -#define UDHCPD 0 - -extern struct sockaddr dest_sin; -extern socklen_t dest_length; -static struct dhcp_packet pdhcp_pkt; -static uint32 dhcp_pkt_len; - -static xQueueHandle QueueStop = NULL; - -enum logtype { - LOGHEX, - LOGSTR -}; - -void ICACHE_FLASH_ATTR log_info(uint8* infobuf, uint32 length, enum logtype log_type) -{ - uint32 i = 0; - if (log_type == LOGHEX){ - for (i = 0; i < length; i ++){ - UDHCP_DEBUG("%2x ", infobuf[i]); - if ((i+1)%16==0) - UDHCP_DEBUG("\n"); - } - UDHCP_DEBUG("\n"); - } else if (log_type == LOGSTR){ - UDHCP_DEBUG("%s\n", infobuf); - } -} - -/* Send a packet to a specific mac address and ip address by creating our own ip packet */ -static void ICACHE_FLASH_ATTR send_packet_to_client(int s, struct dhcp_packet *dhcp_pkt, int force_broadcast) -{ - const uint8_t *chaddr; - uint32_t ciaddr; - unsigned padding; - int fd; - int result = -1; - const char *msg; - struct sockaddr_in dest_addr; - - bzero(&dest_addr, sizeof(dest_addr)); - dest_addr.sin_family = AF_INET; - dest_addr.sin_port = htons(CLIENT_PORT); - - if (force_broadcast - || (dhcp_pkt->flags & htons(BROADCAST_FLAG)) - || dhcp_pkt->ciaddr == 0 - ) { - UDHCP_DEBUG("Broadcasting packet to client\n"); - ciaddr = INADDR_BROADCAST; - chaddr = MAC_BCAST_ADDR; - dest_addr.sin_addr.s_addr = INADDR_BROADCAST; - } else { - UDHCP_DEBUG("Unicasting packet to client ciaddr\n"); - ciaddr = dhcp_pkt->ciaddr; - chaddr = dhcp_pkt->chaddr; - dest_addr.sin_addr.s_addr = htons(ciaddr); - } - - memcpy(&dest_sin, &dest_addr, dest_length); - padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(dhcp_pkt->options); - if (padding > DHCP_SIZE - 300) - padding = DHCP_SIZE - 300; - - dhcp_pkt_len = DHCP_SIZE - padding; - log_info((uint8*)dhcp_pkt, dhcp_pkt_len, LOGHEX); - - memset(&pdhcp_pkt, 0, DHCP_SIZE); - memcpy(&pdhcp_pkt , dhcp_pkt, dhcp_pkt_len); -// result = safe_write(s, dhcp_pkt, DHCP_SIZE - padding); -// if (result < 0) -// printf("safe_write function send data failed\n"); -// else -// printf("safe_write function send data size %d\n", result); -} - -/* Send a packet to gateway_nip using the kernel ip stack */ -static void ICACHE_FLASH_ATTR send_packet_to_relay(struct dhcp_packet *dhcp_pkt) -{ - UDHCP_DEBUG("Forwarding packet to relay\n"); - - udhcp_send_kernel_packet(dhcp_pkt, - server_config.server_nip, SERVER_PORT, - dhcp_pkt->gateway_nip, SERVER_PORT); -} - -static void ICACHE_FLASH_ATTR send_packet(int s, struct dhcp_packet *dhcp_pkt, int force_broadcast) -{ - if (dhcp_pkt->gateway_nip) - send_packet_to_relay(dhcp_pkt); - else - send_packet_to_client(s, dhcp_pkt, force_broadcast); -} - -static void ICACHE_FLASH_ATTR init_packet(struct dhcp_packet *packet, struct dhcp_packet *oldpacket, char type) -{ - /* Sets op, htype, hlen, cookie fields - * and adds DHCP_MESSAGE_TYPE option */ - udhcp_init_header(packet, type); - - packet->xid = oldpacket->xid; - memcpy(packet->chaddr, oldpacket->chaddr, sizeof(oldpacket->chaddr)); - packet->flags = oldpacket->flags; - packet->gateway_nip = oldpacket->gateway_nip; - packet->ciaddr = oldpacket->ciaddr; - udhcp_add_simple_option(packet, DHCP_SERVER_ID, server_config.server_nip); -} - -/* Fill options field, siaddr_nip, and sname and boot_file fields. - * TODO: teach this code to use overload option. - */ -static void ICACHE_FLASH_ATTR add_server_options(struct dhcp_packet *packet) -{ - struct option_set *curr = server_config.options; - - while (curr) { - if (curr->data[OPT_CODE] != DHCP_LEASE_TIME) - udhcp_add_binary_option(packet, curr->data); - curr = curr->next; - } - - packet->siaddr_nip = server_config.siaddr_nip; - - if (server_config.sname) - strncpy((char*)packet->sname, server_config.sname, sizeof(packet->sname) - 1); - if (server_config.boot_file) - strncpy((char*)packet->file, server_config.boot_file, sizeof(packet->file) - 1); -} - -static uint32_t ICACHE_FLASH_ATTR select_lease_time(struct dhcp_packet *packet) -{ - uint32_t lease_time_sec = server_config.max_lease_sec; - uint8_t *lease_time_opt = udhcp_get_option(packet, DHCP_LEASE_TIME); - if (lease_time_opt) { - move_from_unaligned32(lease_time_sec, lease_time_opt); - lease_time_sec = ntohl(lease_time_sec); - if (lease_time_sec > server_config.max_lease_sec) - lease_time_sec = server_config.max_lease_sec; - if (lease_time_sec < server_config.min_lease_sec) - lease_time_sec = server_config.min_lease_sec; - } - return lease_time_sec; -} - -/* We got a DHCP DISCOVER. Send an OFFER. */ -/* NOINLINE: limit stack usage in caller */ -static void ICACHE_FLASH_ATTR send_offer(int s, struct dhcp_packet *oldpacket, - uint32_t static_lease_nip, - struct dyn_lease *lease, - uint8_t *requested_ip_opt) -{ - struct dhcp_packet packet; - uint32_t lease_time_sec; - struct in_addr addr; - - init_packet(&packet, oldpacket, DHCPOFFER); - - /* If it is a static lease, use its IP */ - packet.yiaddr = static_lease_nip; - /* Else: */ - if (!static_lease_nip) { - /* We have no static lease for client's chaddr */ - uint32_t req_nip; - const char *p_host_name; - - if (lease) { - /* We have a dynamic lease for client's chaddr. - * Reuse its IP (even if lease is expired). - * Note that we ignore requested IP in this case. - */ - packet.yiaddr = lease->lease_nip; - } - /* Or: if client has requested an IP */ - else if (requested_ip_opt != NULL - /* (read IP) */ - && (move_from_unaligned32(req_nip, requested_ip_opt), 1) - /* and the IP is in the lease range */ - && ntohl(req_nip) >= server_config.start_ip - && ntohl(req_nip) <= server_config.end_ip - /* and */ - && ( !(lease = find_lease_by_nip(req_nip)) /* is not already taken */ - || is_expired_lease(lease) /* or is taken, but expired */ - ) - ) { - packet.yiaddr = req_nip; - } - else { - /* Otherwise, find a free IP */ - packet.yiaddr = find_free_or_expired_nip(oldpacket->chaddr); - } - - if (!packet.yiaddr) { - UDHCP_DEBUG("no free IP addresses. OFFER abandoned\n"); - return; - } - /* Reserve the IP for a short time hoping to get DHCPREQUEST soon */ - p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME); - lease = add_lease(packet.chaddr, packet.yiaddr, - server_config.offer_time, - p_host_name, - p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0 - ); - if (!lease) { - UDHCP_DEBUG("no free IP addresses. OFFER abandoned\n"); - return; - } - } - - lease_time_sec = select_lease_time(oldpacket); - udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec)); - add_server_options(&packet); - - addr.s_addr = packet.yiaddr; - UDHCP_DEBUG("Sending OFFER of %s\n", inet_ntoa(addr)); - /* send_packet emits error message itself if it detects failure */ - send_packet(s, &packet, /*force_bcast:*/ 0); -} - -/* NOINLINE: limit stack usage in caller */ -static void ICACHE_FLASH_ATTR send_NAK(int s, struct dhcp_packet *oldpacket) -{ - struct dhcp_packet packet; - - init_packet(&packet, oldpacket, DHCPNAK); - - UDHCP_DEBUG("Sending NAK\n"); - send_packet(s, &packet, /*force_bcast:*/ 1); -} - -/* NOINLINE: limit stack usage in caller */ -static void ICACHE_FLASH_ATTR send_ACK(int s, struct dhcp_packet *oldpacket, uint32_t yiaddr) -{ - struct dhcp_packet packet; - uint32_t lease_time_sec; - struct in_addr addr; - const char *p_host_name; - - init_packet(&packet, oldpacket, DHCPACK); - packet.yiaddr = yiaddr; - - lease_time_sec = select_lease_time(oldpacket); - udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec)); - - add_server_options(&packet); - - addr.s_addr = yiaddr; - UDHCP_DEBUG("Sending ACK to %s\n", inet_ntoa(addr)); - send_packet(s, &packet, /*force_bcast:*/ 0); - - p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME); - add_lease(packet.chaddr, packet.yiaddr, - lease_time_sec, - p_host_name, - p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0 - ); - if (ENABLE_FEATURE_UDHCPD_WRITE_LEASES_EARLY) { - /* rewrite the file with leases at every new acceptance */ - write_leases(); - } -} - -/* NOINLINE: limit stack usage in caller */ -static void ICACHE_FLASH_ATTR send_inform(int s, struct dhcp_packet *oldpacket) -{ - struct dhcp_packet packet; - - /* "If a client has obtained a network address through some other means - * (e.g., manual configuration), it may use a DHCPINFORM request message - * to obtain other local configuration parameters. Servers receiving a - * DHCPINFORM message construct a DHCPACK message with any local - * configuration parameters appropriate for the client without: - * allocating a new address, checking for an existing binding, filling - * in 'yiaddr' or including lease time parameters. The servers SHOULD - * unicast the DHCPACK reply to the address given in the 'ciaddr' field - * of the DHCPINFORM message. - * ... - * The server responds to a DHCPINFORM message by sending a DHCPACK - * message directly to the address given in the 'ciaddr' field - * of the DHCPINFORM message. The server MUST NOT send a lease - * expiration time to the client and SHOULD NOT fill in 'yiaddr'." - */ -//TODO: do a few sanity checks: is ciaddr set? -//Better yet: is ciaddr == IP source addr? - init_packet(&packet, oldpacket, DHCPACK); - add_server_options(&packet); - - send_packet(s, &packet, /*force_bcast:*/ 0); -} - -/* globals */ -struct dyn_lease *g_leases = NULL; -/* struct server_config_t server_config is in bb_common_bufsiz1 */ - -#if UDHCPD - -static void ICACHE_FLASH_ATTR udhcpd_start_task(void *pvParameters) -{ - int server_socket = -1, retval, max_sock; - uint8_t *state; - unsigned timeout_end; - unsigned num_ips; - unsigned opt; - struct option_set *option; - char *str_I = str_I; - char* config_file; - char *str_P; - struct dhcp_packet *packet; - int bytes; - struct timeval tv; - uint8_t *server_id_opt; - uint8_t *requested_ip_opt; - uint32_t requested_nip = requested_nip; /* for compiler */ - uint32_t static_lease_nip; - struct dyn_lease *lease, fake_lease; - - /* Would rather not do read_config before daemonization - - * otherwise NOMMU machines will parse config twice */ -// read_config(config_file); - config_init(&server_config); - - option = udhcp_find_option(server_config.options, DHCP_LEASE_TIME); - - if (option) { - UDHCP_DEBUG("udhcp_find_option function return value %x\n", option); - move_from_unaligned32(server_config.max_lease_sec, option->data + OPT_DATA); - server_config.max_lease_sec = ntohl(server_config.max_lease_sec); - } else - server_config.max_lease_sec = DEFAULT_LEASE_TIME; - - /* Sanity check */ - num_ips = server_config.end_ip - server_config.start_ip + 1; - if (server_config.max_leases > num_ips) { - UDHCP_DEBUG("max_leases=%u is too big, setting to %u", - (unsigned)server_config.max_leases, num_ips); - server_config.max_leases = num_ips; - } - - g_leases = (struct dyn_lease *)zalloc(server_config.max_leases * sizeof(g_leases[0])); -// read_leases(server_config.lease_file); - - if (udhcp_read_interface(server_config.interface, - &server_config.ifindex, - (server_config.server_nip == 0 ? &server_config.server_nip : NULL), - server_config.server_mac) - ) { - retval = 1; - return; - } - - /* Setup the socket */ - server_socket = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT, - server_config.interface); - - if (server_socket < 0) { - printf("set up socket fail\n"); - } else { - packet = (struct dhcp_packet *)zalloc(sizeof(struct dhcp_packet)); - while (1) { /* loop until universe collapses */ - - UDHCP_DEBUG("dhcp handsahke start %d.\n", server_socket); - printf("udhcpd_task %d\n", uxTaskGetStackHighWaterMark(NULL)); - bytes = udhcp_recv_kernel_packet(packet, server_socket); - log_info((uint8*)packet, bytes, LOGHEX); - - if (bytes < 0) { - /* bytes can also be -2 ("bad packet data") */ - if (bytes == -1 && errno != EINTR) { - // UDHCP_DEBUG("Read error: %s, reopening socket", strerror(errno)); - close(server_socket); - server_socket = -1; - } - } - if (packet->hlen != 6) { - UDHCP_DEBUG("MAC length != 6, ignoring packet"); - } - if (packet->op != BOOTREQUEST) { - UDHCP_DEBUG("not a REQUEST, ignoring packet"); - } - - /* Get message type if present */ - state = udhcp_get_option(packet, DHCP_MESSAGE_TYPE); - if (state == NULL || state[0] < DHCP_MINTYPE || state[0] > DHCP_MAXTYPE) { - UDHCP_DEBUG("no or bad message type option, ignoring packet"); - } - - /* Get SERVER_ID if present */ - server_id_opt = udhcp_get_option(packet, DHCP_SERVER_ID); - if (server_id_opt) { - uint32_t server_id_network_order; - move_from_unaligned32(server_id_network_order, server_id_opt); - if (server_id_network_order != server_config.server_nip) { - /* client talks to somebody else */ - UDHCP_DEBUG("server ID doesn't match, ignoring"); - } - } - - /* Look for a static/dynamic lease */ - static_lease_nip = get_static_nip_by_mac(server_config.static_leases, packet->chaddr); - if (static_lease_nip) { - UDHCP_DEBUG("Found static lease: %x", static_lease_nip); - memcpy(&fake_lease.lease_mac, packet->chaddr, 6); - fake_lease.lease_nip = static_lease_nip; - fake_lease.expires = 0; - lease = &fake_lease; - } else { - UDHCP_DEBUG("static lease not found, Find the lease that matches MAC: %02x:%02x:%02x:%02x:%02x:%02x\n", - packet->chaddr[0], packet->chaddr[1],packet->chaddr[2], - packet->chaddr[3],packet->chaddr[4],packet->chaddr[5]); - lease = find_lease_by_mac(packet->chaddr); - } - - /* Get REQUESTED_IP if present */ - requested_ip_opt = udhcp_get_option(packet, DHCP_REQUESTED_IP); - if (requested_ip_opt) { - move_from_unaligned32(requested_nip, requested_ip_opt); - } - - switch (state[0]) { - - case DHCPDISCOVER: - UDHCP_DEBUG("Received DISCOVER\n"); - - send_offer(server_socket, packet, static_lease_nip, lease, requested_ip_opt); - break; - - case DHCPREQUEST: - UDHCP_DEBUG("Received REQUEST\n"); - - if (!requested_ip_opt) { - requested_nip = packet->ciaddr; - if (requested_nip == 0) { - UDHCP_DEBUG("no requested IP and no ciaddr, ignoring\n"); - break; - } - } - if (lease && requested_nip == lease->lease_nip) { - /* client requested or configured IP matches the lease. - * ACK it, and bump lease expiration time. */ - send_ACK(server_socket, packet, lease->lease_nip); - break; - } - /* No lease for this MAC, or lease IP != requested IP */ - - if (server_id_opt /* client is in SELECTING state */ - || requested_ip_opt /* client is in INIT-REBOOT state */ - ) { - /* "No, we don't have this IP for you" */ - send_NAK(server_socket, packet); - } /* else: client is in RENEWING or REBINDING, do not answer */ - - break; - - case DHCPDECLINE: - - UDHCP_DEBUG("Received DECLINE\n"); - if (server_id_opt - && requested_ip_opt - && lease /* chaddr matches this lease */ - && requested_nip == lease->lease_nip - ) { - memset(lease->lease_mac, 0, sizeof(lease->lease_mac)); - lease->expires = time(NULL) + server_config.decline_time; - } - break; - - case DHCPRELEASE: - /* "Upon receipt of a DHCPRELEASE message, the server - * marks the network address as not allocated." - * - * SERVER_ID must be present, - * REQUESTED_IP must not be present (we do not check this), - * chaddr must be filled in, - * ciaddr must be filled in - */ - UDHCP_DEBUG("Received RELEASE\n"); - if (server_id_opt - && lease /* chaddr matches this lease */ - && packet->ciaddr == lease->lease_nip - ) { - lease->expires = time(NULL); - } - break; - - case DHCPINFORM: - UDHCP_DEBUG("Received INFORM\n"); - send_inform(server_socket, packet); - break; - } - UDHCP_DEBUG("message processing fineshed.\n"); - } - free(packet); - } - -} - -#else - -static void ICACHE_FLASH_ATTR udhcpd_start_task(void *pvParameters) -{ - bool ValueToSend = true; - bool ValueFromReceive = false; - portBASE_TYPE xStatus; - const portTickType xTickToWait = 100 / portTICK_RATE_MS; -// struct sockaddr_in server_addr; - int send_size; - int recv_size; - struct option_set *option = NULL; - unsigned num_ips; - uint8_t *state = NULL; - uint8_t *requested_ip_opt = NULL; - uint32_t requested_nip = 0; - uint8_t *server_id_opt = NULL; - uint32_t static_lease_nip; - struct dyn_lease *lease, fake_lease; - - config_init(&server_config); - - /*setup the maximum lease time*/ - option = udhcp_find_option(server_config.options, DHCP_LEASE_TIME); - if (option) { - UDHCP_DEBUG("udhcp_find_option function return value %x\n", option); - move_from_unaligned32(server_config.max_lease_sec, option->data + OPT_DATA); - server_config.max_lease_sec = ntohl(server_config.max_lease_sec); - } else - server_config.max_lease_sec = DEFAULT_LEASE_TIME; - - /* Sanity check */ - num_ips = server_config.end_ip - server_config.start_ip + 1; - if (server_config.max_leases > num_ips) { - UDHCP_DEBUG("max_leases=%u is too big, setting to %u", - (unsigned)server_config.max_leases, num_ips); - server_config.max_leases = num_ips; - } - - /*setup the server_mac and server_ip*/ - if (udhcp_read_interface(server_config.interface, - &server_config.ifindex, - (server_config.server_nip == 0 ? &server_config.server_nip : NULL), - server_config.server_mac) - ) { - return; - } - - g_leases = (struct dyn_lease *)zalloc(server_config.max_leases * sizeof(g_leases[0])); - - int server_socket = udhcp_listen_socket(SERVER_PORT,NULL); - - if (server_socket < 0){ - - } else { - struct dhcp_packet *packet = (struct dhcp_packet *)zalloc(sizeof(struct dhcp_packet)); - for(;;) { - xStatus = xQueueReceive(QueueStop,&ValueFromReceive,0); - if (xStatus != pdPASS){ - UDHCP_DEBUG("Could not receive from the queue!\n"); - } else if (ValueFromReceive){ - break; - } - - recv_size = udhcp_recv_kernel_packet(packet, server_socket); - if (recv_size < 0){ - UDHCP_DEBUG("Server Recieve Data Failed!\n"); -// break; - } else { - UDHCP_DEBUG("recvfrom function recv data size %d %d\n", recv_size, sizeof(struct dhcp_packet)); - if (packet->hlen != 6) { - UDHCP_DEBUG("MAC length != 6, ignoring packet"); - } - if (packet->op != BOOTREQUEST) { - UDHCP_DEBUG("not a REQUEST, ignoring packet"); - } - - /* Get message type if present */ - state = udhcp_get_option(packet, DHCP_MESSAGE_TYPE); - if (state == NULL || state[0] < DHCP_MINTYPE || state[0] > DHCP_MAXTYPE) { - UDHCP_DEBUG("no or bad message type option, ignoring packet"); - } - - /* Get SERVER_ID if present */ - server_id_opt = udhcp_get_option(packet, DHCP_SERVER_ID); - if (server_id_opt) { - uint32_t server_id_network_order; - move_from_unaligned32(server_id_network_order, server_id_opt); - if (server_id_network_order != server_config.server_nip) { - /* client talks to somebody else */ - UDHCP_DEBUG("server ID doesn't match, ignoring\n"); - } - } - - /* Look for a static/dynamic lease */ - static_lease_nip = get_static_nip_by_mac(server_config.static_leases, packet->chaddr); - if (static_lease_nip) { - UDHCP_DEBUG("Found static lease: %x", static_lease_nip); - memcpy(&fake_lease.lease_mac, packet->chaddr, 6); - fake_lease.lease_nip = static_lease_nip; - fake_lease.expires = 0; - lease = &fake_lease; - } else { - UDHCP_DEBUG("static lease not found, Find the lease that matches MAC: %02x:%02x:%02x:%02x:%02x:%02x\n", - packet->chaddr[0], packet->chaddr[1],packet->chaddr[2], - packet->chaddr[3],packet->chaddr[4],packet->chaddr[5]); - lease = find_lease_by_mac(packet->chaddr); - } - - /* Get REQUESTED_IP if present */ - requested_ip_opt = udhcp_get_option(packet, DHCP_REQUESTED_IP); - if (requested_ip_opt) { - move_from_unaligned32(requested_nip, requested_ip_opt); - } - - switch(state[0]){ - case DHCPDISCOVER: - UDHCP_DEBUG("Received DISCOVER\n"); - send_offer(server_socket, packet, static_lease_nip, lease, requested_ip_opt); - send_size = safe_write(server_socket, &pdhcp_pkt, dhcp_pkt_len); - if (send_size < 0) - UDHCP_DEBUG("safe_write function send data failed\n"); - else - UDHCP_DEBUG("safe_write function send data size %d\n", send_size); - break; - - case DHCPREQUEST: - UDHCP_DEBUG("Received REQUEST\n"); - if (!requested_ip_opt) { - requested_nip = packet->ciaddr; - if (requested_nip == 0) { - UDHCP_DEBUG("no requested IP and no ciaddr, ignoring\n"); - break; - } - } - if (lease && requested_nip == lease->lease_nip) { - /* client requested or configured IP matches the lease. - * ACK it, and bump lease expiration time. */ - send_ACK(server_socket, packet, lease->lease_nip); - send_size = safe_write(server_socket, &pdhcp_pkt, dhcp_pkt_len); - if (send_size < 0) - UDHCP_DEBUG("safe_write function send data failed\n"); - else - UDHCP_DEBUG("safe_write function send data size %d\n", send_size); - break; - } - - /* No lease for this MAC, or lease IP != requested IP */ - if (server_id_opt /* client is in SELECTING state */ - || requested_ip_opt /* client is in INIT-REBOOT state */ - ) { - /* "No, we don't have this IP for you" */ - send_NAK(server_socket, packet); - send_size = safe_write(server_socket, &pdhcp_pkt, dhcp_pkt_len); - if (send_size < 0) - UDHCP_DEBUG("safe_write function send data failed\n"); - else - UDHCP_DEBUG("safe_write function send data size %d\n", send_size); - } /* else: client is in RENEWING or REBINDING, do not answer */ - - break; - - case DHCPDECLINE: - UDHCP_DEBUG("Received DECLINE\n"); - if (server_id_opt - && requested_ip_opt - && lease /* chaddr matches this lease */ - && requested_nip == lease->lease_nip - ) { - memset(lease->lease_mac, 0, sizeof(lease->lease_mac)); - lease->expires = time(NULL) + server_config.decline_time; - } - - break; - - case DHCPRELEASE: - UDHCP_DEBUG("Received RELEASE\n"); - if (server_id_opt - && lease /* chaddr matches this lease */ - && packet->ciaddr == lease->lease_nip - ) { - lease->expires = time(NULL); - } - - break; - - case DHCPINFORM: - UDHCP_DEBUG("Received INFORM\n"); - send_inform(server_socket, packet); - send_size = safe_write(server_socket, &pdhcp_pkt, dhcp_pkt_len); - if (send_size < 0) - UDHCP_DEBUG("safe_write function send data failed\n"); - else - UDHCP_DEBUG("safe_write function send data size %d\n", send_size); - - break; - - default : - break; - } - - } - - } - free(packet); - } - - free(g_leases); - close(server_socket); - vQueueDelete(QueueStop); - QueueStop = NULL; - vTaskDelete(NULL); -} -#endif - -void ICACHE_FLASH_ATTR udhcpd_start(void) -{ - if (QueueStop == NULL) - QueueStop = xQueueCreate(1,1); - - if (QueueStop != NULL) - xTaskCreate(udhcpd_start_task, "udhcpd_start", 512, NULL, 2, NULL); -} - -sint8 ICACHE_FLASH_ATTR udhcpd_stop(void) -{ - bool ValueToSend = true; - portBASE_TYPE xStatus; - if (QueueStop == NULL) - return -1; - - xStatus = xQueueSend(QueueStop,&ValueToSend,0); - if (xStatus != pdPASS){ - UDHCP_DEBUG("Could not send to the queue!\n"); - return -1; - } else { - taskYIELD(); - return pdPASS; - } -} diff --git a/third_party/udhcp/files.c b/third_party/udhcp/files.c deleted file mode 100644 index 0c1a51a6..00000000 --- a/third_party/udhcp/files.c +++ /dev/null @@ -1,320 +0,0 @@ -/* vi: set sw=4 ts=4: */ -/* - * DHCP server config and lease file manipulation - * - * Rewrite by Russ Dill July 2001 - * - * Licensed under GPLv2, see file LICENSE in this source tree. - */ -#include - -#include "udhcp/common.h" -#include "udhcp/dhcpd.h" - -static struct server_config_t *please_config = NULL; - -/* on these functions, make sure your datatype matches */ -static int ICACHE_FLASH_ATTR read_str(const char *line, void *arg) -{ - char **dest = arg; - - free(*dest); - *dest = (char *)strdup(line); - return 1; -} - -static int ICACHE_FLASH_ATTR read_u32(const char *line, void *arg) -{ -// *(uint32_t*)arg = strtoul(line, NULL, 10); - return errno == 0; -} - -static int ICACHE_FLASH_ATTR read_staticlease(const char *const_line, void *arg) -{ - char *line; - char *mac_string; - char *ip_string; -// struct ether_addr mac_bytes; /* it's "struct { uint8_t mac[6]; }" */ - uint8_t mac_bytes[6]; - uint32_t nip; - - /* Read mac */ - line = (char *) const_line; -// mac_string = strtok_r(line, " \t", &line); -// if (!mac_string || !ether_aton_r(mac_string, &mac_bytes)) -// return 0; - - /* Read ip */ -// ip_string = strtok_r(NULL, " \t", &line); - if (!ip_string || !udhcp_str2nip(ip_string, &nip)) - return 0; - - add_static_lease(arg, (uint8_t*) &mac_bytes, nip); - - log_static_leases(arg); - - return 1; -} - -#if 0 -struct config_keyword { - const char *keyword; - int (*handler)(const char *line, void *var); - void *var; - const char *def; -}; - -static const struct config_keyword keywords[] = { - /* keyword handler variable address default */ - {"start" , udhcp_str2nip , &server_config.start_ip , "192.168.9.100"}, - {"end" , udhcp_str2nip , &server_config.end_ip , "192.168.9.135"}, - {"interface" , read_str , &server_config.interface , "eth1"}, - /* Avoid "max_leases value not sane" warning by setting default - * to default_end_ip - default_start_ip + 1: */ - {"max_leases" , read_u32 , &server_config.max_leases , "35"}, - {"auto_time" , read_u32 , &server_config.auto_time , "7200"}, - {"decline_time" , read_u32 , &server_config.decline_time , "3600"}, - {"conflict_time", read_u32 , &server_config.conflict_time, "3600"}, - {"offer_time" , read_u32 , &server_config.offer_time , "60"}, - {"min_lease" , read_u32 , &server_config.min_lease_sec, "60"}, -// {"lease_file" , read_str , &server_config.lease_file , LEASES_FILE}, - {"pidfile" , read_str , &server_config.pidfile , "/var/run/udhcpd.pid"}, - {"siaddr" , udhcp_str2nip , &server_config.siaddr_nip , "0.0.0.0"}, - /* keywords with no defaults must be last! */ - {"option" , udhcp_str2optset, &server_config.options , ""}, - {"opt" , udhcp_str2optset, &server_config.options , ""}, - {"notify_file" , read_str , &server_config.notify_file , NULL}, - {"sname" , read_str , &server_config.sname , NULL}, - {"boot_file" , read_str , &server_config.boot_file , NULL}, - {"static_lease" , read_staticlease, &server_config.static_leases, ""}, -}; -enum { KWS_WITH_DEFAULTS = ARRAY_SIZE(keywords) - 6 }; -#endif -void ICACHE_FLASH_ATTR config_init(struct server_config_t* psconfig) -{ - uint32 softap_ip = 0,local_ip = 0; - struct ip_info info; - if (psconfig == NULL) - return; - - bzero(psconfig, sizeof(struct server_config_t)); - if (please_config == NULL){ - wifi_get_ip_info(SOFTAP_IF,&info); - local_ip = softap_ip = htonl(info.ip.addr); - softap_ip &= 0xFFFFFF00; - local_ip &= 0xFF; - if (local_ip >= 0x80) - local_ip -= 0x64; - else - local_ip += 0x64; - - psconfig->max_leases = 3; - psconfig->start_ip = softap_ip | local_ip; - psconfig->end_ip = softap_ip | (local_ip + psconfig->max_leases); - - psconfig->interface = NULL; - psconfig->auto_time = 7200; - psconfig->decline_time = 3600; - psconfig->conflict_time = 3600; - psconfig->offer_time = 60; - psconfig->min_lease_sec = 60; - - psconfig->pidfile = NULL; - psconfig->siaddr_nip = 0; - psconfig->static_leases = NULL; - } else { - psconfig->start_ip = please_config->start_ip; - psconfig->end_ip = please_config->end_ip; - psconfig->max_leases = please_config->max_leases; - psconfig->auto_time = please_config->auto_time; - psconfig->decline_time = please_config->decline_time; - psconfig->conflict_time = please_config->conflict_time; - psconfig->offer_time = please_config->offer_time; - psconfig->min_lease_sec = please_config->min_lease_sec; - free(please_config); - please_config = NULL; - } - UDHCP_DEBUG("start_ip = 0x%x, end_ip = 0x%x\n",psconfig->start_ip, psconfig->end_ip); -} - -bool ICACHE_FLASH_ATTR dhcp_set_info(struct dhcp_info *if_dhcp) -{ - struct ip_info info; - uint32 softap_ip = 0; - if (if_dhcp ==NULL) - return false; - - wifi_get_ip_info(SOFTAP_IF,&info); - softap_ip = htonl(info.ip.addr); - if_dhcp->start_ip = htonl(if_dhcp->start_ip); - if_dhcp->end_ip = htonl(if_dhcp->end_ip); - - /*config ip information can't contain local ip*/ - if ((if_dhcp->start_ip <= softap_ip) && (softap_ip<= if_dhcp->end_ip)) - return false; - - /*config ip information must be in the same segment as the local ip*/ - softap_ip >>= 8; - if ((if_dhcp->start_ip >> 8 != softap_ip) || - (if_dhcp->end_ip >> 8 != softap_ip)){ - return false; - } - - /*config ip information can't exceed the maximum number of leases*/ - if (if_dhcp ->end_ip - if_dhcp->start_ip > if_dhcp->max_leases){ - return false; - } - - please_config= (struct server_config_t*)zalloc(sizeof(struct server_config_t)); - if (please_config == NULL) - return false; - - please_config->start_ip = if_dhcp ->start_ip; - please_config->end_ip = if_dhcp ->end_ip; - please_config->max_leases = if_dhcp ->max_leases; - please_config->auto_time = if_dhcp ->auto_time; - please_config->decline_time = if_dhcp ->decline_time; - please_config->conflict_time = if_dhcp ->conflict_time; - please_config->offer_time = if_dhcp ->offer_time; - please_config->min_lease_sec = if_dhcp ->min_lease_sec; - UDHCP_DEBUG("start_ip = %x end_ip = %x\n", if_dhcp->start_ip, if_dhcp->end_ip); - return true; -} - -#if 0 -void ICACHE_FLASH_ATTR read_config(const char *file) -{ - unsigned i; - - for (i = 0; i < KWS_WITH_DEFAULTS; i++) - keywords[i].handler(keywords[i].def, keywords[i].var); - -#if 0 - parser_t *parser; - const struct config_keyword *k; - char *token[2]; - - parser = config_open(file); - while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) { - for (k = keywords, i = 0; i < ARRAY_SIZE(keywords); k++, i++) { - if (strcasecmp(token[0], k->keyword) == 0) { - if (!k->handler(token[1], k->var)) { - bb_error_msg("can't parse line %u in %s", - parser->lineno, file); - /* reset back to the default value */ - k->handler(k->def, k->var); - } - break; - } - } - } - config_close(parser); -#endif - server_config.start_ip = ntohl(server_config.start_ip); - server_config.end_ip = ntohl(server_config.end_ip); -} -#endif -void ICACHE_FLASH_ATTR write_leases(void) -{ - return; -#if 0 - int fd; - unsigned i; - leasetime_t curr; - sint64_t written_at; - -// fd = open_or_warn(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC); - if (fd < 0) - return; - - curr = written_at = time(NULL); - -// written_at = SWAP_BE64(written_at); - full_write(fd, &written_at, sizeof(written_at)); - - for (i = 0; i < server_config.max_leases; i++) { - leasetime_t tmp_time; - - if (g_leases[i].lease_nip == 0) - continue; - - /* Screw with the time in the struct, for easier writing */ - tmp_time = g_leases[i].expires; - - g_leases[i].expires -= curr; - if ((signed_leasetime_t) g_leases[i].expires < 0) - g_leases[i].expires = 0; - g_leases[i].expires = htonl(g_leases[i].expires); - - /* No error check. If the file gets truncated, - * we lose some leases on restart. Oh well. */ - full_write(fd, &g_leases[i], sizeof(g_leases[i])); - - /* Then restore it when done */ - g_leases[i].expires = tmp_time; - } - close(fd); - - if (server_config.notify_file) { - char *argv[3]; - argv[0] = server_config.notify_file; - argv[1] = server_config.lease_file; - argv[2] = NULL; -// spawn_and_wait(argv); - } -#endif -} - -void ICACHE_FLASH_ATTR read_leases(const char *file) -{ - return ; -#if 0 - struct dyn_lease lease; - sint64_t written_at, time_passed; - int fd; -#ifdef UDHCP_DBG - unsigned i = 0; -#endif - -// fd = open_or_warn(file, O_RDONLY); - if (fd < 0) - return; - - if (full_read(fd, &written_at, sizeof(written_at)) != sizeof(written_at)) - goto ret; -// written_at = SWAP_BE64(written_at); - - time_passed = time(NULL) - written_at; - /* Strange written_at, or lease file from old version of udhcpd - * which had no "written_at" field? */ - if ((uint64_t)time_passed > 12 * 60 * 60) - goto ret; - - while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) { -//FIXME: what if it matches some static lease? - uint32_t y = ntohl(lease.lease_nip); - if (y >= server_config.start_ip && y <= server_config.end_ip) { - signed_leasetime_t expires = ntohl(lease.expires) - (signed_leasetime_t)time_passed; - if (expires <= 0) - continue; - /* NB: add_lease takes "relative time", IOW, - * lease duration, not lease deadline. */ - if (add_lease(lease.lease_mac, lease.lease_nip, - expires,lease.hostname, sizeof(lease.hostname) - ) == 0 - ) { - UDHCP_DEBUG("too many leases while loading %s", file); - break; - } -#ifdef UDHCP_DBG - i++; -#endif - } - } -#ifdef UDHCP_DBG - UDHCP_DEBUG("Read %d leases", i); -#endif - ret: - close(fd); -#endif -} diff --git a/third_party/udhcp/leases.c b/third_party/udhcp/leases.c deleted file mode 100644 index 3c193495..00000000 --- a/third_party/udhcp/leases.c +++ /dev/null @@ -1,204 +0,0 @@ -/* vi: set sw=4 ts=4: */ -/* - * Russ Dill July 2001 - * - * Licensed under GPLv2, see file LICENSE in this source tree. - */ - -#include "udhcp/common.h" -#include "udhcp/dhcpd.h" - -/* Find the oldest expired lease, NULL if there are no expired leases */ -static struct dyn_lease *ICACHE_FLASH_ATTR oldest_expired_lease(void) -{ - struct dyn_lease *oldest_lease = NULL; - leasetime_t oldest_time = time(NULL); - unsigned i; - - /* Unexpired leases have g_leases[i].expires >= current time - * and therefore can't ever match */ - for (i = 0; i < server_config.max_leases; i++) { - if (g_leases[i].expires < oldest_time) { - oldest_time = g_leases[i].expires; - oldest_lease = &g_leases[i]; - } - } - return oldest_lease; -} - -/* Clear out all leases with matching nonzero chaddr OR yiaddr. - * If chaddr == NULL, this is a conflict lease. - */ -static void ICACHE_FLASH_ATTR clear_leases(const uint8_t *chaddr, uint32_t yiaddr) -{ - unsigned i; - - for (i = 0; i < server_config.max_leases; i++) { - if ((chaddr && memcmp(g_leases[i].lease_mac, chaddr, 6) == 0) - || (yiaddr && g_leases[i].lease_nip == yiaddr) - ) { - memset(&g_leases[i], 0, sizeof(g_leases[i])); - } - } -} - -/* Add a lease into the table, clearing out any old ones. - * If chaddr == NULL, this is a conflict lease. - */ -struct dyn_lease* ICACHE_FLASH_ATTR add_lease( - const uint8_t *chaddr, uint32_t yiaddr, - leasetime_t leasetime, - const char *hostname, int hostname_len) -{ - struct dyn_lease *oldest; - - /* clean out any old ones */ - clear_leases(chaddr, yiaddr); - - oldest = oldest_expired_lease(); - - if (oldest) { - memset(oldest, 0, sizeof(*oldest)); - if (hostname) { - char *p; - - hostname_len++; /* include NUL */ - if (hostname_len > sizeof(oldest->hostname)) - hostname_len = sizeof(oldest->hostname); - p = (char *)strncpy(oldest->hostname, hostname, hostname_len); - /* sanitization (s/non-ASCII/^/g) */ - while (*p) { - if (*p < ' ' || *p > 126) - *p = '^'; - p++; - } - } - if (chaddr) - memcpy(oldest->lease_mac, chaddr, 6); - oldest->lease_nip = yiaddr; - oldest->expires = time(NULL) + leasetime; - } - - return oldest; -} - -/* True if a lease has expired */ -int ICACHE_FLASH_ATTR is_expired_lease(struct dyn_lease *lease) -{ - return (lease->expires < (leasetime_t) time(NULL)); -} - -/* Find the first lease that matches MAC, NULL if no match */ -struct dyn_lease* ICACHE_FLASH_ATTR find_lease_by_mac(const uint8_t *mac) -{ - unsigned i; - - for (i = 0; i < server_config.max_leases; i++) - if (memcmp(g_leases[i].lease_mac, mac, 6) == 0) - return &g_leases[i]; - - return NULL; -} - -/* Find the first lease that matches IP, NULL is no match */ -struct dyn_lease* ICACHE_FLASH_ATTR find_lease_by_nip(uint32_t nip) -{ - unsigned i; - - for (i = 0; i < server_config.max_leases; i++) - if (g_leases[i].lease_nip == nip) - return &g_leases[i]; - - return NULL; -} - -/* Check if the IP is taken; if it is, add it to the lease table */ -static int ICACHE_FLASH_ATTR nobody_responds_to_arp(uint32_t nip, const uint8_t *safe_mac) -{ - struct in_addr temp; - int r; - - r = arpping(nip, safe_mac, - server_config.server_nip, - server_config.server_mac, - server_config.interface); - if (r) - return r; - - temp.s_addr = nip; - UDHCP_DEBUG("%s belongs to someone, reserving it for %u seconds\n", - inet_ntoa(temp), (unsigned)server_config.conflict_time); - add_lease(NULL, nip, server_config.conflict_time, NULL, 0); - return 0; -} - -/* Find a new usable (we think) address */ -uint32_t ICACHE_FLASH_ATTR find_free_or_expired_nip(const uint8_t *safe_mac) -{ - uint32_t addr; - struct dyn_lease *oldest_lease = NULL; - -#if ENABLE_FEATURE_UDHCPD_BASE_IP_ON_MAC - uint32_t stop; - unsigned i, hash; - - /* hash hwaddr: use the SDBM hashing algorithm. Seems to give good - * dispersal even with similarly-valued "strings". - */ - hash = 0; - for (i = 0; i < 6; i++) - hash += safe_mac[i] + (hash << 6) + (hash << 16) - hash; - - /* pick a seed based on hwaddr then iterate until we find a free address. */ - addr = server_config.start_ip - + (hash % (1 + server_config.end_ip - server_config.start_ip)); - stop = addr; -#else - addr = server_config.start_ip; -#define stop (server_config.end_ip + 1) -#endif - do { - uint32_t nip; - struct dyn_lease *lease; - - /* ie, 192.168.55.0 */ - if ((addr & 0xff) == 0) - goto next_addr; - /* ie, 192.168.55.255 */ - if ((addr & 0xff) == 0xff) - goto next_addr; - nip = htonl(addr); - /* skip our own address */ - if (nip == server_config.server_nip) - goto next_addr; - /* is this a static lease addr? */ - if (is_nip_reserved(server_config.static_leases, nip)) - goto next_addr; - - lease = find_lease_by_nip(nip); - if (!lease) { -//TODO: DHCP servers do not always sit on the same subnet as clients: should *ping*, not arp-ping! - if (nobody_responds_to_arp(nip, safe_mac)) - return nip; - } else { - if (!oldest_lease || lease->expires < oldest_lease->expires) - oldest_lease = lease; - } - - next_addr: - addr++; -#if ENABLE_FEATURE_UDHCPD_BASE_IP_ON_MAC - if (addr > server_config.end_ip) - addr = server_config.start_ip; -#endif - } while (addr != stop); - - if (oldest_lease - && is_expired_lease(oldest_lease) - && nobody_responds_to_arp(oldest_lease->lease_nip, safe_mac) - ) { - return oldest_lease->lease_nip; - } - - return 0; -} diff --git a/third_party/udhcp/packet.c b/third_party/udhcp/packet.c deleted file mode 100644 index 25f03649..00000000 --- a/third_party/udhcp/packet.c +++ /dev/null @@ -1,236 +0,0 @@ -/* vi: set sw=4 ts=4: */ -/* - * Packet ops - * - * Rewrite by Russ Dill July 2001 - * - * Licensed under GPLv2, see file LICENSE in this source tree. - */ - -#include "udhcp/common.h" -#include "udhcp/dhcpd.h" - -void ICACHE_FLASH_ATTR udhcp_init_header(struct dhcp_packet *packet, char type) -{ - memset(packet, 0, sizeof(*packet)); - packet->op = BOOTREQUEST; /* if client to a server */ - switch (type) { - case DHCPOFFER: - case DHCPACK: - case DHCPNAK: - packet->op = BOOTREPLY; /* if server to client */ - break; - } - packet->htype = 1; /* ethernet */ - packet->hlen = 6; - packet->cookie = htonl(DHCP_MAGIC); - if (DHCP_END != 0) - packet->options[0] = DHCP_END; - udhcp_add_simple_option(packet, DHCP_MESSAGE_TYPE, type); -} - -#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2 -void udhcp_dump_packet(struct dhcp_packet *packet) -{ - char buf[sizeof(packet->chaddr)*2 + 1]; - - if (dhcp_verbose < 2) - return; - - bb_info_msg( - //" op %x" - //" htype %x" - " hlen %x" - //" hops %x" - " xid %x" - //" secs %x" - //" flags %x" - " ciaddr %x" - " yiaddr %x" - " siaddr %x" - " giaddr %x" - //" chaddr %s" - //" sname %s" - //" file %s" - //" cookie %x" - //" options %s" - //, packet->op - //, packet->htype - , packet->hlen - //, packet->hops - , packet->xid - //, packet->secs - //, packet->flags - , packet->ciaddr - , packet->yiaddr - , packet->siaddr_nip - , packet->gateway_nip - //, packet->chaddr[16] - //, packet->sname[64] - //, packet->file[128] - //, packet->cookie - //, packet->options[] - ); - *bin2hex(buf, (void *) packet->chaddr, sizeof(packet->chaddr)) = '\0'; - bb_info_msg(" chaddr %s", buf); -} -#endif - -/* Read a packet from socket fd, return -1 on read error, -2 on packet error */ -int ICACHE_FLASH_ATTR udhcp_recv_kernel_packet(struct dhcp_packet *packet, int fd) -{ - int bytes; - - memset(packet, 0, sizeof(*packet)); - bytes = safe_read(fd, packet, sizeof(*packet)); - if (bytes < 0) { - UDHCP_DEBUG("Packet read error, ignoring\n"); - return bytes; /* returns -1 */ - } - - if (//bytes < offsetof(struct dhcp_packet, options) - //|| packet->cookie != htonl(DHCP_MAGIC) - packet->cookie != htonl(DHCP_MAGIC) - ) { - UDHCP_DEBUG("Packet with bad magic, ignoring\n"); - return -2; - } - UDHCP_DEBUG("Received a packet\n"); - udhcp_dump_packet(packet); - - return bytes; -} - -///* Construct a ip/udp header for a packet, send packet */ -//int ICACHE_FLASH_ATTR udhcp_send_raw_packet(struct dhcp_packet *dhcp_pkt, -// uint32_t source_nip, int source_port, -// uint32_t dest_nip, int dest_port, const uint8_t *dest_arp, -// int ifindex) -//{ -//// struct sockaddr_ll dest_sll; -// struct ip_udp_dhcp_packet packet; -// unsigned padding; -// int fd; -// int result = -1; -// const char *msg; -// -//// fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP)); -// if (fd < 0) { -// msg = "socket(%s)\n"; -// goto ret_msg; -// } -// -//// memset(&dest_sll, 0, sizeof(dest_sll)); -//// memset(&packet, 0, offsetof(struct ip_udp_dhcp_packet, data)); -// packet.data = *dhcp_pkt; /* struct copy */ -// -//// dest_sll.sll_family = AF_PACKET; -//// dest_sll.sll_protocol = htons(ETH_P_IP); -//// dest_sll.sll_ifindex = ifindex; -//// dest_sll.sll_halen = 6; -//// memcpy(dest_sll.sll_addr, dest_arp, 6); -// -//// if (bind(fd, (struct sockaddr *)&dest_sll, sizeof(dest_sll)) < 0) { -//// msg = "bind(%s)"; -//// goto ret_close; -//// } -// -// /* We were sending full-sized DHCP packets (zero padded), -// * but some badly configured servers were seen dropping them. -// * Apparently they drop all DHCP packets >576 *ethernet* octets big, -// * whereas they may only drop packets >576 *IP* octets big -// * (which for typical Ethernet II means 590 octets: 6+6+2 + 576). -// * -// * In order to work with those buggy servers, -// * we truncate packets after end option byte. -// * -// * However, RFC 1542 says "The IP Total Length and UDP Length -// * must be large enough to contain the minimal BOOTP header of 300 octets". -// * Thus, we retain enough padding to not go below 300 BOOTP bytes. -// * Some devices have filters which drop DHCP packets shorter than that. -// */ -// padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(packet.data.options); -// if (padding > DHCP_SIZE - 300) -// padding = DHCP_SIZE - 300; -// -// packet.ip._proto = IPPROTO_UDP; -// packet.ip.src.addr = source_nip; -// packet.ip.dest.addr = dest_nip; -// packet.udp.src = htons(source_port); -// packet.udp.dest = htons(dest_port); -// /* size, excluding IP header: */ -// packet.udp.len = htons(UDP_DHCP_SIZE - padding); -// /* for UDP checksumming, ip.len is set to UDP packet len */ -// packet.ip._len = packet.udp.len; -// packet.udp.chksum = inet_chksum((uint16_t *)&packet, -// IP_UDP_DHCP_SIZE - padding); -// /* but for sending, it is set to IP packet len */ -// packet.ip._len = htons(IP_UDP_DHCP_SIZE - padding); -// packet.ip._v_hl = sizeof(packet.ip) >> 2; -//// packet.ip.version = IPVERSION; -//// packet.ip._ttl = IPDEFTTL; -// packet.ip._chksum = inet_chksum((uint16_t *)&packet.ip, sizeof(packet.ip)); -// -// udhcp_dump_packet(dhcp_pkt); -//// result = sendto(fd, &packet, IP_UDP_DHCP_SIZE - padding, /*flags:*/ 0, -//// (struct sockaddr *) &dest_sll, sizeof(dest_sll)); -// msg = "sendto(%s)\n"; -// ret_close: -// close(fd); -// if (result < 0) { -// ret_msg: -// UDHCP_DEBUG(msg, "PACKET"); -// } -// return result; -//} - -/* Let the kernel do all the work for packet generation */ -int ICACHE_FLASH_ATTR udhcp_send_kernel_packet(struct dhcp_packet *dhcp_pkt, - uint32_t source_nip, int source_port, - uint32_t dest_nip, int dest_port) -{ - struct sockaddr_in sa; - unsigned padding; - int fd; - int result = -1; - const char *msg; - - fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); - if (fd < 0) { - msg = "socket(%s)\n"; - goto ret_msg; - } -// setsockopt_reuseaddr(fd); - - memset(&sa, 0, sizeof(sa)); - sa.sin_family = AF_INET; - sa.sin_port = htons(source_port); - sa.sin_addr.s_addr = source_nip; - if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) { - msg = "bind(%s)\n"; - goto ret_close; - } - - memset(&sa, 0, sizeof(sa)); - sa.sin_family = AF_INET; - sa.sin_port = htons(dest_port); - sa.sin_addr.s_addr = dest_nip; - if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) { - msg = "connect(%s)\n"; - goto ret_close; - } - - udhcp_dump_packet(dhcp_pkt); - padding = DHCP_OPTIONS_BUFSIZE - 1 - udhcp_end_option(dhcp_pkt->options); - if (padding > DHCP_SIZE - 300) - padding = DHCP_SIZE - 300; - result = safe_write(fd, dhcp_pkt, DHCP_SIZE - padding); - msg = "write(%s)\n"; - ret_close: - close(fd); - if (result < 0) { - ret_msg: - UDHCP_DEBUG(msg, "UDP"); - } - return result; -} diff --git a/third_party/udhcp/read.c b/third_party/udhcp/read.c deleted file mode 100644 index bbf75cb5..00000000 --- a/third_party/udhcp/read.c +++ /dev/null @@ -1,122 +0,0 @@ -/* vi: set sw=4 ts=4: */ -/* - * Utility routines. - * - * Copyright (C) 1999-2004 by Erik Andersen - * - * Licensed under GPLv2 or later, see file LICENSE in this source tree. - */ - -#include "udhcp/common.h" - -struct sockaddr dest_sin; -socklen_t dest_length = sizeof(struct sockaddr); - -ssize_t ICACHE_FLASH_ATTR safe_read(int fd, void *buf, size_t count) -{ - ssize_t n; - - do { -// n = read(fd, buf, count); - n = recvfrom(fd, buf, count, 0, &dest_sin, &dest_length); - } while (n < 0 && errno == EINTR); - - return n; -} - -/* - * Read all of the supplied buffer from a file. - * This does multiple reads as necessary. - * Returns the amount read, or -1 on an error. - * A short read is returned on an end of file. - */ -ssize_t ICACHE_FLASH_ATTR full_read(int fd, void *buf, size_t len) -{ - ssize_t cc; - ssize_t total; - - total = 0; - - while (len) { - cc = safe_read(fd, buf, len); - - if (cc < 0) { - if (total) { - /* we already have some! */ - /* user can do another read to know the error code */ - return total; - } - return cc; /* read() returns -1 on failure. */ - } - if (cc == 0) - break; - buf = ((char *)buf) + cc; - total += cc; - len -= cc; - } - - return total; -} - -ssize_t ICACHE_FLASH_ATTR read_close(int fd, void *buf, size_t size) -{ - /*int e;*/ - size = full_read(fd, buf, size); - /*e = errno;*/ - close(fd); - /*errno = e;*/ - return size; -} - -/*ssize_t open_read_close(const char *filename, void *buf, size_t size) -{ - int fd = open(filename, O_RDONLY); - if (fd < 0) - return fd; - return read_close(fd, buf, size); -}*/ - -ssize_t ICACHE_FLASH_ATTR safe_write(int fd, const void *buf, size_t count) -{ - ssize_t n; - - do { -// n = write(fd, buf, count); - n = sendto(fd, buf, count, 0, &dest_sin, dest_length); - } while (n < 0 && errno == EINTR); - - return n; -} - -/* - * Write all of the supplied buffer out to a file. - * This does multiple writes as necessary. - * Returns the amount written, or -1 on an error. - */ -ssize_t ICACHE_FLASH_ATTR full_write(int fd, const void *buf, size_t len) -{ - ssize_t cc; - ssize_t total; - - total = 0; - - while (len) { - cc = safe_write(fd, buf, len); - - if (cc < 0) { - if (total) { - /* we already wrote some! */ - /* user can do another write to know the error code */ - return total; - } - return cc; /* write() returns -1 on failure. */ - } - - total += cc; - buf = ((const char *)buf) + cc; - len -= cc; - } - - return total; -} - diff --git a/third_party/udhcp/socket.c b/third_party/udhcp/socket.c deleted file mode 100644 index ef2a69b4..00000000 --- a/third_party/udhcp/socket.c +++ /dev/null @@ -1,86 +0,0 @@ -/* vi: set sw=4 ts=4: */ -/* - * DHCP server client/server socket creation - * - * udhcp client/server - * Copyright (C) 1999 Matthew Ramsay - * Chris Trew - * - * Rewrite by Russ Dill July 2001 - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - */ - -#include "udhcp/common.h" - -int ICACHE_FLASH_ATTR udhcp_read_interface(const char *interface, int *ifindex, uint32_t *nip, uint8_t *mac) -{ - struct ip_info info; - if (wifi_get_opmode() == NULL_MODE) { - UDHCP_DEBUG("wifi's opmode is invalid\n"); - return 1; - } - - if (nip) { - wifi_get_ip_info(SOFTAP_IF,&info); - *nip = info.ip.addr; - UDHCP_DEBUG("IP %s\n", inet_ntoa(info.ip)); - } - - if (ifindex) { - - if (wifi_get_opmode() == SOFTAP_MODE || wifi_get_opmode() == STATIONAP_MODE) { - *ifindex = SOFTAP_IF; - } else { - *ifindex = STATION_IF; - } - } - - if (mac) { - - if (wifi_get_opmode() == SOFTAP_MODE || wifi_get_opmode() == STATIONAP_MODE) { - wifi_get_macaddr(SOFTAP_IF, mac); - } - UDHCP_DEBUG("MAC %02x:%02x:%02x:%02x:%02x:%02x\n", - mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); - } - - return 0; -} - -/* 1. None of the callers expects it to ever fail */ -/* 2. ip was always INADDR_ANY */ -int ICACHE_FLASH_ATTR udhcp_listen_socket(/*uint32_t ip,*/ int port, const char *inf) -{ - int fd; - int recv_timeout = 10; - struct sockaddr_in addr; - - UDHCP_DEBUG("Opening listen socket on *:%d %s\n", port, inf); - - fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); - - bzero(&addr, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = htons(INADDR_ANY); - addr.sin_port = htons(port); - /* addr.sin_addr.s_addr = ip; - all-zeros is INADDR_ANY */ - if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) - UDHCP_DEBUG("bind port %d failed\n", port); - - setsockopt(fd,SOL_SOCKET,SO_RCVTIMEO,(void *)&recv_timeout,sizeof(recv_timeout)); - - return fd; -} diff --git a/third_party/udhcp/static_leases.c b/third_party/udhcp/static_leases.c deleted file mode 100644 index 1d281113..00000000 --- a/third_party/udhcp/static_leases.c +++ /dev/null @@ -1,77 +0,0 @@ -/* vi: set sw=4 ts=4: */ -/* - * Storing and retrieving data for static leases - * - * Wade Berrier September 2004 - * - * Licensed under GPLv2, see file LICENSE in this source tree. - */ - -#include "udhcp/common.h" -#include "udhcp/dhcpd.h" - -/* Takes the address of the pointer to the static_leases linked list, - * address to a 6 byte mac address, - * 4 byte IP address */ -void ICACHE_FLASH_ATTR add_static_lease(struct static_lease **st_lease_pp, - uint8_t *mac, - uint32_t nip) -{ - struct static_lease *st_lease; - - /* Find the tail of the list */ - while ((st_lease = *st_lease_pp) != NULL) { - st_lease_pp = &st_lease->next; - } - - /* Add new node */ - *st_lease_pp = st_lease = (struct static_lease *)zalloc(sizeof(*st_lease)); - memcpy(st_lease->mac, mac, 6); - st_lease->nip = nip; - /*st_lease->next = NULL;*/ -} - -/* Find static lease IP by mac */ -uint32_t ICACHE_FLASH_ATTR get_static_nip_by_mac(struct static_lease *st_lease, void *mac) -{ - while (st_lease) { - if (memcmp(st_lease->mac, mac, 6) == 0) - return st_lease->nip; - st_lease = st_lease->next; - } - - return 0; -} - -/* Check to see if an IP is reserved as a static IP */ -int ICACHE_FLASH_ATTR is_nip_reserved(struct static_lease *st_lease, uint32_t nip) -{ - while (st_lease) { - if (st_lease->nip == nip) - return 1; - st_lease = st_lease->next; - } - - return 0; -} - -#ifdef UDHCP_DBG -/* Print out static leases just to check what's going on */ -/* Takes the address of the pointer to the static_leases linked list */ -void ICACHE_FLASH_ATTR log_static_leases(struct static_lease **st_lease_pp) -{ - struct static_lease *cur; - -// if (dhcp_verbose < 2) -// return; - - cur = *st_lease_pp; - while (cur) { - UDHCP_DEBUG("static lease: mac:%02x:%02x:%02x:%02x:%02x:%02x nip:%x", - cur->mac[0], cur->mac[1], cur->mac[2], - cur->mac[3], cur->mac[4], cur->mac[5], - cur->nip); - cur = cur->next; - } -} -#endif diff --git a/third_party/udhcp/time.c b/third_party/udhcp/time.c deleted file mode 100644 index fcfaf513..00000000 --- a/third_party/udhcp/time.c +++ /dev/null @@ -1,44 +0,0 @@ -/* vi: set sw=4 ts=4: */ -/* - * Utility routines. - * - * Copyright (C) 2007 Denys Vlasenko - * - * Licensed under GPLv2, see file LICENSE in this source tree. - */ - -#include "udhcp/common.h" - -unsigned long long ICACHE_FLASH_ATTR monotonic_ns(void) -{ - struct timeval tv; -// gettimeofday(&tv, NULL); - return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000; -} -unsigned long long ICACHE_FLASH_ATTR monotonic_us(void) -{ - struct timeval tv; -// gettimeofday(&tv, NULL); - return tv.tv_sec * 1000000ULL + tv.tv_usec; -} -unsigned long long ICACHE_FLASH_ATTR monotonic_ms(void) -{ - struct timeval tv; -// gettimeofday(&tv, NULL); - return tv.tv_sec * 1000ULL + tv.tv_usec / 1000; -} - -unsigned ICACHE_FLASH_ATTR monotonic_sec(void) -{ - unsigned monotonic_sec = 0; - monotonic_sec = system_get_time(); - if (monotonic_sec < 0) - monotonic_sec = rand()-monotonic_sec; - - return monotonic_sec; -} - -uint32_t ICACHE_FLASH_ATTR time(void *arg) -{ - return system_get_time(); -}