18
18
#include "lwip/igmp.h"
19
19
#include "lwip/tcp.h"
20
20
#include "lwip/udp.h"
21
+ #include "lwip/dhcp.h"
21
22
22
23
#if defined(CLIENT_SSL_ENABLE ) && defined(LUA_USE_MODULES_NET ) && defined(LUA_USE_MODULES_TLS )
23
24
#define TLS_MODULE_PRESENT
@@ -966,6 +967,62 @@ static int net_getdnsserver( lua_State* L ) {
966
967
return 1 ;
967
968
}
968
969
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
+
969
1026
#pragma mark - Tables
970
1027
971
1028
#ifdef TLS_MODULE_PRESENT
@@ -1017,6 +1074,9 @@ LROT_BEGIN(net_dns)
1017
1074
LROT_FUNCENTRY ( resolve , net_dns_static )
1018
1075
LROT_END ( net_dns , net_dns , 0 )
1019
1076
1077
+ LROT_BEGIN (net_if )
1078
+ LROT_FUNCENTRY ( info , net_if_info )
1079
+ LROT_END (net_if , net_if , 0 )
1020
1080
1021
1081
LROT_BEGIN (net )
1022
1082
LROT_FUNCENTRY ( createServer , net_createServer )
@@ -1025,6 +1085,7 @@ LROT_BEGIN(net)
1025
1085
LROT_FUNCENTRY ( multicastJoin , net_multicastJoin )
1026
1086
LROT_FUNCENTRY ( multicastLeave , net_multicastLeave )
1027
1087
LROT_TABENTRY ( dns , net_dns )
1088
+ LROT_TABENTRY ( if , net_if )
1028
1089
#ifdef TLS_MODULE_PRESENT
1029
1090
LROT_TABENTRY ( cert , tls_cert )
1030
1091
#endif
0 commit comments