elks-enhanced
public
Read
Owner: themaster
Branch: master
Commits: 6893
Updated: 2026-04-19 00:15
Git CLI clone URL
git clone https://www.xt-emporium.com/git/elks-enhanced.git
Fullscreen desktop URL
Code
Commits
History
Branches
Bug Reports
Discussions
Compare
Settings
elks-enhanced
/
elkscmd
/
ktcp
/
uip
/
apps
/
dhcpc
/
dhcpc.c
File editor
/* * Upstream uIP DHCP client. * * Vendored from adamdunkels/uip with ELKS integration changes limited to * coexistence with the existing unified UDP app dispatcher. */ #include <string.h> #include <stddef.h> #include "uip.h" #include "dhcpc.h" extern void uip_tracef(const char *fmt, ...); #define STATE_INITIAL 0 #define STATE_SENDING 1 #define STATE_OFFER_RECEIVED 2 #define STATE_CONFIG_RECEIVED 3 extern char dhcpc_appstate_tag; static struct dhcpc_state s; struct dhcp_msg { u8_t op, htype, hlen, hops; u8_t xid[4]; u16_t secs, flags; u8_t ciaddr[4]; u8_t yiaddr[4]; u8_t siaddr[4]; u8_t giaddr[4]; u8_t chaddr[16]; #ifndef UIP_CONF_DHCP_LIGHT u8_t sname[64]; u8_t file[128]; #endif u8_t options[312]; }; #define BOOTP_BROADCAST 0x8000 #define DHCP_REQUEST 1 #define DHCP_REPLY 2 #define DHCP_HTYPE_ETHERNET 1 #define DHCP_HLEN_ETHERNET 6 #define DHCP_CHADDR_LEN 16 #define DHCP_MSG_HEADER_LEN (offsetof(struct dhcp_msg, chaddr) + DHCP_CHADDR_LEN) #define DHCP_MSG_LEN 236 #define DHCPC_SERVER_PORT 67 #define DHCPC_CLIENT_PORT 68 #define DHCPDISCOVER 1 #define DHCPOFFER 2 #define DHCPREQUEST 3 #define DHCPACK 5 #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_REQ_LIST 55 #define DHCP_OPTION_END 255 static const u8_t xid[4] = {0xad, 0xde, 0x12, 0x23}; static const u8_t magic_cookie[4] = {99, 130, 83, 99}; static u8_t *msg_options(struct dhcp_msg *m) { return ((u8_t *)m) + DHCP_MSG_LEN; } static u8_t *add_msg_type(u8_t *optptr, u8_t type) { *optptr++ = DHCP_OPTION_MSG_TYPE; *optptr++ = 1; *optptr++ = type; return optptr; } static u8_t *add_server_id(u8_t *optptr) { *optptr++ = DHCP_OPTION_SERVER_ID; *optptr++ = 4; memcpy(optptr, s.serverid, 4); return optptr + 4; } static u8_t *add_req_ipaddr(u8_t *optptr) { *optptr++ = DHCP_OPTION_REQ_IPADDR; *optptr++ = 4; memcpy(optptr, s.ipaddr, 4); return optptr + 4; } static u8_t *add_req_options(u8_t *optptr) { *optptr++ = DHCP_OPTION_REQ_LIST; *optptr++ = 3; *optptr++ = DHCP_OPTION_SUBNET_MASK; *optptr++ = DHCP_OPTION_ROUTER; *optptr++ = DHCP_OPTION_DNS_SERVER; return optptr; } static u8_t *add_end(u8_t *optptr) { *optptr++ = DHCP_OPTION_END; return optptr; } static void create_msg(struct dhcp_msg *m) { u8_t *options; m->op = DHCP_REQUEST; m->htype = DHCP_HTYPE_ETHERNET; m->hlen = s.mac_len; m->hops = 0; memcpy(m->xid, xid, sizeof(m->xid)); m->secs = 0; m->flags = HTONS(BOOTP_BROADCAST); memcpy(m->ciaddr, uip_hostaddr, sizeof(m->ciaddr)); memset(m->yiaddr, 0, sizeof(m->yiaddr)); memset(m->siaddr, 0, sizeof(m->siaddr)); memset(m->giaddr, 0, sizeof(m->giaddr)); memcpy(m->chaddr, s.mac_addr, s.mac_len); memset(&m->chaddr[s.mac_len], 0, DHCP_CHADDR_LEN - s.mac_len); memset(((u8_t *)m) + DHCP_MSG_HEADER_LEN, 0, DHCP_MSG_LEN - DHCP_MSG_HEADER_LEN); options = msg_options(m); memcpy(options, magic_cookie, sizeof(magic_cookie)); } static void send_discover(void) { u8_t *end; struct dhcp_msg *m = (struct dhcp_msg *)uip_appdata; u8_t *options; create_msg(m); options = msg_options(m); end = add_msg_type(options + 4, DHCPDISCOVER); end = add_req_options(end); end = add_end(end); uip_send(uip_appdata, end - (u8_t *)uip_appdata); } static void send_request(void) { u8_t *end; struct dhcp_msg *m = (struct dhcp_msg *)uip_appdata; u8_t *options; create_msg(m); options = msg_options(m); end = add_msg_type(options + 4, DHCPREQUEST); end = add_server_id(end); end = add_req_ipaddr(end); end = add_end(end); uip_send(uip_appdata, end - (u8_t *)uip_appdata); } static u8_t parse_options(u8_t *optptr, int len) { u8_t *end = optptr + len; u8_t type = 0; while(optptr < end) { if(*optptr == 0) { ++optptr; continue; } switch(*optptr) { case DHCP_OPTION_SUBNET_MASK: memcpy(s.netmask, optptr + 2, 4); break; case DHCP_OPTION_ROUTER: memcpy(s.default_router, optptr + 2, 4); break; case DHCP_OPTION_DNS_SERVER: memcpy(s.dnsaddr, optptr + 2, 4); break; case DHCP_OPTION_MSG_TYPE: type = *(optptr + 2); break; case DHCP_OPTION_SERVER_ID: memcpy(s.serverid, optptr + 2, 4); break; case DHCP_OPTION_LEASE_TIME: memcpy(s.lease_time, optptr + 2, 4); break; case DHCP_OPTION_END: return type; } optptr += optptr[1] + 2; } return type; } static u8_t parse_msg(void) { struct dhcp_msg *m = (struct dhcp_msg *)uip_appdata; int len = uip_datalen(); u8_t *options; u8_t type; if(len < DHCP_MSG_LEN + (int)sizeof(magic_cookie)) { uip_tracef("dhcp short len=%d\n", len); return 0; } options = msg_options(m); if(m->op == DHCP_REPLY && memcmp(m->xid, xid, sizeof(xid)) == 0 && memcmp(m->chaddr, s.mac_addr, s.mac_len) == 0) { memcpy(s.ipaddr, m->yiaddr, 4); type = parse_options(options + 4, len - DHCP_MSG_LEN - sizeof(magic_cookie)); uip_tracef("dhcp msg type=%u len=%d\n", type, len); return type; } return 0; } static PT_THREAD(handle_dhcp(void)) { PT_BEGIN(&s.pt); s.state = STATE_SENDING; s.ticks = CLOCK_SECOND; do { send_discover(); timer_set(&s.timer, s.ticks); PT_WAIT_UNTIL(&s.pt, uip_newdata() || timer_expired(&s.timer)); if(uip_newdata() && parse_msg() == DHCPOFFER) { s.state = STATE_OFFER_RECEIVED; break; } if(s.ticks < CLOCK_SECOND * 60) { s.ticks *= 2; } } while(s.state != STATE_OFFER_RECEIVED); PT_YIELD(&s.pt); s.ticks = CLOCK_SECOND; do { send_request(); timer_set(&s.timer, s.ticks); PT_WAIT_UNTIL(&s.pt, uip_newdata() || timer_expired(&s.timer)); if(uip_newdata() && parse_msg() == DHCPACK) { s.state = STATE_CONFIG_RECEIVED; break; } if(s.ticks <= CLOCK_SECOND * 10) { s.ticks += CLOCK_SECOND; } else { PT_RESTART(&s.pt); } } while(s.state != STATE_CONFIG_RECEIVED); dhcpc_configured(&s); while(1) { PT_YIELD(&s.pt); } PT_END(&s.pt); } void dhcpc_init(const void *mac_addr, int mac_len) { uip_ipaddr_t addr; memset(&s, 0, sizeof(s)); s.mac_addr = mac_addr; s.mac_len = mac_len; s.state = STATE_INITIAL; uip_ipaddr(addr, 255,255,255,255); s.conn = uip_udp_new(&addr, HTONS(DHCPC_SERVER_PORT)); if(s.conn != NULL) { uip_udp_bind(s.conn, HTONS(DHCPC_CLIENT_PORT)); s.conn->appstate = &dhcpc_appstate_tag; } PT_INIT(&s.pt); } void dhcpc_appcall(void) { handle_dhcp(); } void dhcpc_request(void) { u16_t ipaddr[2]; if(s.state == STATE_INITIAL) { uip_ipaddr(ipaddr, 0,0,0,0); uip_sethostaddr(ipaddr); } }
Commit message
This repository is read-only for this account.
Repository snapshot
Current branch
master
Visibility
public
Your access
Read
Remote
Configured
File activity
View file history