Skip to content

Commit 83fd5e5

Browse files
committedJul 27, 2019
New net.if.info call to show LwIP information
This is a generalization of `wifi.sta`'s and `wifi.ap`'s `getip` and `getmac` calls. I don't propose to deprecate those, but perhaps we should, in the documentation, point users at this function instead. The direct motivation is to permit continued use of DHCP-provided NTP servers in a future where nodemcu#2819 has landed, now that nodemcu#2709 is in the tree. But rather than exposing just that information, a more general interface seems useful.
1 parent 49ac968 commit 83fd5e5

File tree

3 files changed

+98
-25
lines changed

3 files changed

+98
-25
lines changed
 

‎app/include/netif/wlan_lwip_if.h

-25
This file was deleted.

‎app/modules/net.c

+61
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "lwip/igmp.h"
1919
#include "lwip/tcp.h"
2020
#include "lwip/udp.h"
21+
#include "lwip/dhcp.h"
2122

2223
#if defined(CLIENT_SSL_ENABLE) && defined(LUA_USE_MODULES_NET) && defined(LUA_USE_MODULES_TLS)
2324
#define TLS_MODULE_PRESENT
@@ -966,6 +967,62 @@ static int net_getdnsserver( lua_State* L ) {
966967
return 1;
967968
}
968969

970+
#pragma mark - netif info
971+
972+
/*
973+
* XXX This is internal to Espressif's SDK, but it's called from several places
974+
* in the NodeMCU tree. It would be nicer if there were a LwIP export for this
975+
* rather than this not-so-secret symbol.
976+
*/
977+
extern struct netif *eagle_lwip_getif(uint8);
978+
979+
static void
980+
push_ipaddr(lua_State *L, ip_addr_t *addr) {
981+
char temp[20];
982+
ssize_t ipl = ets_snprintf(temp, sizeof temp, IPSTR, IP2STR(&addr->addr));
983+
lua_assert (ipl >= 0 && ipl < 20);
984+
lua_pushlstring( L, temp, ipl );
985+
}
986+
987+
static void
988+
field_from_ipaddr(lua_State *L, const char * field_name, ip_addr_t* addr) {
989+
if ( ip_addr_isany(addr) ) {
990+
lua_pushnil(L);
991+
} else {
992+
push_ipaddr(L, addr);
993+
}
994+
lua_setfield(L, -2, field_name);
995+
}
996+
997+
static int net_if_info( lua_State* L ) {
998+
int ifidx = luaL_optint(L, 1, 0);
999+
1000+
struct netif * nif = eagle_lwip_getif(ifidx);
1001+
if (nif == NULL) {
1002+
return luaL_error( L, "unknown network interface index %d", ifidx);
1003+
}
1004+
1005+
lua_createtable(L, 0,
1006+
4 + (nif->dhcp == NULL ? 0 : 1));
1007+
1008+
lua_pushlstring(L, nif->hwaddr, nif->hwaddr_len);
1009+
lua_setfield(L, -2, "hwaddr");
1010+
1011+
field_from_ipaddr(L, "ip" , &nif->ip_addr);
1012+
field_from_ipaddr(L, "netmask", &nif->netmask);
1013+
field_from_ipaddr(L, "gateway", &nif->gw);
1014+
1015+
if (nif->dhcp != NULL) {
1016+
lua_createtable(L, 0, 3);
1017+
field_from_ipaddr(L, "server_ip" , &nif->dhcp->server_ip_addr );
1018+
field_from_ipaddr(L, "client_ip" , &nif->dhcp->offered_ip_addr );
1019+
field_from_ipaddr(L, "ntp_server", &nif->dhcp->offered_ntp_addr);
1020+
}
1021+
lua_setfield(L, -2, "dhcp");
1022+
1023+
return 1;
1024+
}
1025+
9691026
#pragma mark - Tables
9701027

9711028
#ifdef TLS_MODULE_PRESENT
@@ -1017,6 +1074,9 @@ LROT_BEGIN(net_dns)
10171074
LROT_FUNCENTRY( resolve, net_dns_static )
10181075
LROT_END( net_dns, net_dns, 0 )
10191076

1077+
LROT_BEGIN(net_if)
1078+
LROT_FUNCENTRY( info, net_if_info )
1079+
LROT_END(net_if, net_if, 0)
10201080

10211081
LROT_BEGIN(net)
10221082
LROT_FUNCENTRY( createServer, net_createServer )
@@ -1025,6 +1085,7 @@ LROT_BEGIN(net)
10251085
LROT_FUNCENTRY( multicastJoin, net_multicastJoin )
10261086
LROT_FUNCENTRY( multicastLeave, net_multicastLeave )
10271087
LROT_TABENTRY( dns, net_dns )
1088+
LROT_TABENTRY( if, net_if )
10281089
#ifdef TLS_MODULE_PRESENT
10291090
LROT_TABENTRY( cert, tls_cert )
10301091
#endif

‎docs/modules/net.md

+37
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,43 @@ Sets the IP of the DNS server used to resolve hostnames. Default: resolver1.open
618618
#### See also
619619
[`net.dns:getdnsserver()`](#netdnsgetdnsserver)
620620

621+
# net.if Module
622+
623+
## net.if.info()
624+
625+
Return information about a network interface, specified by index.
626+
627+
#### Syntax
628+
`net.if.info(if_index)`
629+
630+
#### Parameters
631+
- `if_index` the interface index; on ESP8266, `0` is the wifi client (STA) and `1`
632+
is the wifi AP.
633+
634+
#### Returns
635+
`nil` if the given `if_index` does not correspond to an interface. Otherwise,
636+
a table containing ...
637+
638+
* `ip`, `netmask`, and `gateway` configured for this interface, as dotted quad strings
639+
or `nil` if none is set.
640+
641+
* if DHCP was used to configure the interface, then `dhcp` will be a table containing...
642+
643+
* `server_ip` -- the DHCP server itself, as a dotted quad
644+
645+
* `client_ip` -- the IP address suggested for the client; likely, this equals `ip`
646+
above, unless the configuration has been overridden.
647+
648+
* `ntp_server` -- the NTP server suggested by the DHCP server.
649+
650+
DNS servers are not tracked per-interface in LwIP and, as such, are not
651+
reported here; use [`net.dns:getdnsserver()`](#netdnsgetdnsserver).
652+
653+
#### Example
654+
655+
`print(net.if.info(0).dhcp.ntp_server)` will show the NTP server suggested by
656+
the DHCP server.
657+
621658
# net.cert Module
622659

623660
This part gone to the [TLS](tls.md) module, link kept for backward compatibility.

0 commit comments

Comments
 (0)
Failed to load comments.