Skip to content

Commit

Permalink
Update for DNS API changes
Browse files Browse the repository at this point in the history
Signed-off-by: Deomid "rojer" Ryabkov <rojer@rojer.me>
  • Loading branch information
rojer committed Aug 21, 2021
1 parent 110ccbe commit 1f2af44
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 4 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions include/mgos_lwip.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ extern "C" {
#endif

bool mgos_lwip_if_get_ip_info(const struct netif *nif,
const char *dns_override,
struct mgos_net_ip_info *ip_info);

#ifdef __cplusplus
Expand Down
4 changes: 4 additions & 0 deletions lwip/src/core/ipv4/dhcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,10 @@ dhcp_handle_ack(struct netif *netif, struct dhcp_msg *msg_in)
dns_setserver(n, &dns_addr);
}
#endif /* LWIP_DHCP_PROVIDE_DNS_SERVERS */
ip4_addr_set_zero(&dhcp->offered_dns_addr);
if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_DNS_SERVER)) {
ip4_addr_set_u32(&dhcp->offered_dns_addr, lwip_htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_DNS_SERVER)));
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions lwip/src/include/lwip/dhcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ struct dhcp
ip4_addr_t offered_ip_addr;
ip4_addr_t offered_sn_mask;
ip4_addr_t offered_gw_addr;
ip4_addr_t offered_dns_addr;

u32_t offered_t0_lease; /* lease period (in seconds) */
u32_t offered_t1_renew; /* recommended renew time (usually 50% of lease period) */
Expand Down
2 changes: 2 additions & 0 deletions lwip/src/include/netif/ppp/ppp.h
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,8 @@ struct ppp_pcb_s {
ipv6cp_options ipv6cp_allowoptions; /* Options we allow peer to request */
ipv6cp_options ipv6cp_hisoptions; /* Options that we ack'd */
#endif /* PPP_IPV6_SUPPORT */

u32_t dns_server;
};

/************************
Expand Down
2 changes: 2 additions & 0 deletions lwip/src/netif/ppp/ipcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1930,7 +1930,9 @@ static void ipcp_up(fsm *f) {
if (go->dnsaddr[1])
script_setenv("DNS2", ip_ntoa(go->dnsaddr[1]), 0);
#endif /* UNUSED */
pcb->dns_server = 0;
if (pcb->settings.usepeerdns && (go->dnsaddr[0] || go->dnsaddr[1])) {
pcb->dns_server = go->dnsaddr[0];
sdns(pcb, go->dnsaddr[0], go->dnsaddr[1]);
#if 0 /* UNUSED */
script_setenv("USEPEERDNS", "1", 0);
Expand Down
9 changes: 9 additions & 0 deletions mos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ build_vars:

conds:
# For esp32, SDK provides LwIP and this is basically an no-op library.
- when: mos.platform == "esp32"
apply:
sources:
- src
includes:
- include

- when: mos.platform == "esp8266"
apply:
conds:
Expand Down Expand Up @@ -80,6 +87,7 @@ conds:
- lwip/src/netif/ppp
includes:
- include
- include/lwip
- lwip/src/include

- when: mos.platform == "rs14100"
Expand All @@ -98,6 +106,7 @@ conds:
- lwip/src/netif/ppp
includes:
- include
- include/lwip
- lwip/src/include

cdefs:
Expand Down
4 changes: 3 additions & 1 deletion src/esp8266/sdk_lwip/include/lwip/dhcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ struct dhcp
ip_addr_t offered_ip_addr;
ip_addr_t offered_sn_mask;
ip_addr_t offered_gw_addr;

ip_addr_t offered_dns_addr;
ip_addr_t offered_ntp_addr;

u32_t offered_t0_lease; /* lease period (in seconds) */
u32_t offered_t1_renew; /* recommended renew time (usually 50% of lease period) */
u32_t offered_t2_rebind; /* recommended rebind time (usually 66% of lease period) */
Expand Down
4 changes: 4 additions & 0 deletions src/esp8266/sdk_lwip/src/core/dhcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,10 @@ dhcp_handle_ack(struct netif *netif)
}
}
#endif /* LWIP_DNS */
ip_addr_set_zero(&dhcp->offered_dns_addr);
if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_DNS_SERVER)) {
ip4_addr_set_u32(&dhcp->offered_dns_addr, htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_DNS_SERVER)));
}
}

/** Set a statically allocated struct dhcp to work with.
Expand Down
20 changes: 17 additions & 3 deletions src/mgos_lwip.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,39 @@

#include "common/platform.h"

#include "lwip/dhcp.h"
#include "lwip/netif.h"
#include "lwip/tcpip.h"

bool mgos_lwip_if_get_ip_info(const struct netif *nif,
const char *dns_override,
struct mgos_net_ip_info *ip_info) {
if (nif == NULL) return false;
if (nif == NULL || !(nif->flags & NETIF_FLAG_UP)) return false;
memset(ip_info, 0, sizeof(*ip_info));
ip_info->ip.sin_addr.s_addr = ip_addr_get_ip4_u32(&nif->ip_addr);
ip_info->netmask.sin_addr.s_addr = ip_addr_get_ip4_u32(&nif->netmask);
ip_info->gw.sin_addr.s_addr = ip_addr_get_ip4_u32(&nif->gw);
if (!mgos_conf_str_empty(dns_override)) {
if (!mgos_net_str_to_ip(dns_override, &ip_info->dns)) return false;
} else {
struct dhcp *dhcp = netif_dhcp_data(nif);
if (dhcp != NULL) {
ip_info->dns.sin_addr.s_addr = ip4_addr_get_u32(&dhcp->offered_dns_addr);
}
}
return true;
}

#if CS_PLATFORM != CS_P_ESP32 && CS_PLATFORM != CS_P_ESP8266
static void tcpip_init_done(void *arg) {
*((bool *) arg) = true;
*((bool *)arg) = true;
}
#endif

bool mgos_lwip_init(void) {
#if CS_PLATFORM != CS_P_ESP32 && CS_PLATFORM != CS_P_ESP8266
volatile bool lwip_inited = false;
tcpip_init(tcpip_init_done, (void *) &lwip_inited);
tcpip_init(tcpip_init_done, (void *)&lwip_inited);
while (!lwip_inited) {
}
#endif
Expand Down

0 comments on commit 1f2af44

Please sign in to comment.