Skip to content

Commit 948325a

Browse files
nwfmarcelstoer
authored andcommitted
New net.if.info call to show LwIP information (#2862)
* Remove app/include/netif/wlan_lwip_if.h This file appears to be unused in our tree. * 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 #2819 has landed, now that #2709 is in the tree. But rather than exposing just that information, a more general interface seems useful.
1 parent 1b4b442 commit 948325a

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
@@ -980,6 +981,62 @@ static int net_getdnsserver( lua_State* L ) {
980981
return 1;
981982
}
982983

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

9851042
#ifdef TLS_MODULE_PRESENT
@@ -1031,6 +1088,9 @@ LROT_BEGIN(net_dns)
10311088
LROT_FUNCENTRY( resolve, net_dns_static )
10321089
LROT_END( net_dns, net_dns, 0 )
10331090

1091+
LROT_BEGIN(net_if)
1092+
LROT_FUNCENTRY( info, net_if_info )
1093+
LROT_END(net_if, net_if, 0)
10341094

10351095
LROT_BEGIN(net)
10361096
LROT_FUNCENTRY( createServer, net_createServer )
@@ -1039,6 +1099,7 @@ LROT_BEGIN(net)
10391099
LROT_FUNCENTRY( multicastJoin, net_multicastJoin )
10401100
LROT_FUNCENTRY( multicastLeave, net_multicastLeave )
10411101
LROT_TABENTRY( dns, net_dns )
1102+
LROT_TABENTRY( if, net_if )
10421103
#ifdef TLS_MODULE_PRESENT
10431104
LROT_TABENTRY( cert, tls_cert )
10441105
#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)