Skip to content

Instantly share code, notes, and snippets.

@smcl
Last active February 7, 2016 11:41
Show Gist options
  • Save smcl/e2b22dbc8ae04fa15e87 to your computer and use it in GitHub Desktop.
Save smcl/e2b22dbc8ae04fa15e87 to your computer and use it in GitHub Desktop.
diff --git a/app/include/lwip/app/ping.h b/app/include/lwip/app/ping.h
index 21e26e9..161b7ab 100644
--- a/app/include/lwip/app/ping.h
+++ b/app/include/lwip/app/ping.h
@@ -75,6 +75,7 @@ struct ping_resp{
uint32 bytes;
uint32 total_bytes;
uint32 total_time;
+ uint8 ttl;
sint8 ping_err;
};
diff --git a/app/include/user_modules.h b/app/include/user_modules.h
index f3bb0fe..9dfbca8 100644
--- a/app/include/user_modules.h
+++ b/app/include/user_modules.h
@@ -17,33 +17,33 @@
#define LUA_USE_MODULES_BIT
//#define LUA_USE_MODULES_BMP085
#define LUA_USE_MODULES_CJSON
-#define LUA_USE_MODULES_COAP
+//#define LUA_USE_MODULES_COAP
#define LUA_USE_MODULES_CRYPTO
-#define LUA_USE_MODULES_DHT
+//#define LUA_USE_MODULES_DHT
//#define LUA_USE_MODULES_ENDUSER_SETUP // USE_DNS in dhcpserver.h needs to be enabled for this module to work.
#define LUA_USE_MODULES_FILE
#define LUA_USE_MODULES_GPIO
//#define LUA_USE_MODULES_HX711
-#define LUA_USE_MODULES_I2C
-#define LUA_USE_MODULES_MQTT
+//#define LUA_USE_MODULES_I2C
+//#define LUA_USE_MODULES_MQTT
#define LUA_USE_MODULES_NET
#define LUA_USE_MODULES_NODE
-#define LUA_USE_MODULES_OW
-#define LUA_USE_MODULES_PWM
-#define LUA_USE_MODULES_RC
-#define LUA_USE_MODULES_RTCFIFO
-#define LUA_USE_MODULES_RTCMEM
-#define LUA_USE_MODULES_RTCTIME
-#define LUA_USE_MODULES_SNTP
+//#define LUA_USE_MODULES_OW
+//#define LUA_USE_MODULES_PWM
+//#define LUA_USE_MODULES_RC
+//#define LUA_USE_MODULES_RTCFIFO
+//#define LUA_USE_MODULES_RTCMEM
+//#define LUA_USE_MODULES_RTCTIME
+//#define LUA_USE_MODULES_SNTP
#define LUA_USE_MODULES_SPI
#define LUA_USE_MODULES_TMR
-#define LUA_USE_MODULES_TSL2561
-#define LUA_USE_MODULES_U8G
+//#define LUA_USE_MODULES_TSL2561
+//#define LUA_USE_MODULES_U8G
#define LUA_USE_MODULES_UART
//#define LUA_USE_MODULES_UCG
#define LUA_USE_MODULES_WIFI
//#define LUA_USE_MODULES_WS2801
-#define LUA_USE_MODULES_WS2812
+//#define LUA_USE_MODULES_WS2812
#endif /* LUA_CROSS_COMPILER */
diff --git a/app/lwip/app/ping.c b/app/lwip/app/ping.c
index 6bd6f0e..674b1e6 100644
--- a/app/lwip/app/ping.c
+++ b/app/lwip/app/ping.c
@@ -164,6 +164,8 @@ ping_recv(void *arg, struct raw_pcb *pcb, struct pbuf *p, ip_addr_t *addr)
pingresp.resp_time = delay;
pingresp.seqno = ntohs(iecho->seqno);
pingresp.ping_err = 0;
+ //pingresp.ttl = pcb->ttl;
+ pingresp.ttl = iphdr->_ttl;
pingmsg->ping_opt->recv_function(pingmsg->ping_opt,(void*) &pingresp);
}
}
@@ -240,7 +242,7 @@ ping_coarse_tmr(void *arg)
} else {
uint32 delay = system_relative_time(pingmsg->ping_start);
delay /= PING_COARSE;
-// ping_seq_num = 0;
+ ping_seq_num = 0;
if (ping_opt->sent_function == NULL){
os_printf("ping %d, timeout %d, total payload %d bytes, %d ms\n",
pingmsg->max_count, pingmsg->timeout_count, PING_DATA_SIZE*(pingmsg->max_count - pingmsg->timeout_count),delay);
diff --git a/app/modules/net.c b/app/modules/net.c
index f0eee3b..84f8250 100644
--- a/app/modules/net.c
+++ b/app/modules/net.c
@@ -12,6 +12,9 @@
#include "lwip/ip_addr.h"
#include "espconn.h"
#include "lwip/dns.h"
+#include "lwip/app/ping.h"
+#include "lwip/raw.h"
+#include "c_stdio.h"
#ifdef CLIENT_SSL_ENABLE
unsigned char *default_certificate;
@@ -39,6 +42,10 @@ static uint16_t tcp_server_timeover = 30;
static struct espconn *pTcpServer = NULL;
static struct espconn *pUdpServer = NULL;
+static int ping_callback_ref;
+static int ping_host_count;
+static ip_addr_t ping_host_ip;
+
typedef struct lnet_userdata
{
struct espconn *pesp_conn;
@@ -1476,6 +1483,159 @@ static int net_getdnsserver( lua_State* L )
return 1;
}
+static int net_test(lua_State *L)
+{
+ if (lua_isstring(L, 1)) {
+ const char *str_input = luaL_checkstring(L, 1);
+ lua_pushstring(L, str_input);
+ } else {
+ lua_pushnil(L);
+ }
+
+ return 1;
+}
+
+static int net_pcall_test(lua_State *L)
+{
+ if (lua_isstring(L, 1)) {
+ const char *str_input = luaL_checkstring(L, 1);
+ lua_pushstring(L, str_input);
+ } else {
+ lua_pushnil(L);
+ }
+
+ return 1;
+}
+
+
+void ping_received(void *arg, void *data) {
+ struct ping_option *pingopt = (struct ping_option*)arg;
+ struct ping_resp *pingresp = (struct ping_resp*)data;
+
+ char ipaddrstr[16];
+ ip_addr_t source_ip;
+
+ source_ip.addr = pingopt->ip;
+ ipaddr_ntoa_r(&source_ip, ipaddrstr, sizeof(ipaddrstr));
+
+ // if we've registered a lua callback function, retrieve
+ // it from registry + call it, otherwise just print the
+ // ping response in a standard-ish way
+ if (ping_callback_ref != LUA_NOREF) {
+ lua_rawgeti(gL, LUA_REGISTRYINDEX, ping_callback_ref);
+ lua_pushinteger(gL, pingresp->bytes);
+ lua_pushstring(gL, ipaddrstr);
+ lua_pushinteger(gL, pingresp->seqno);
+ lua_pushinteger(gL, pingresp->ttl);
+ lua_pushinteger(gL, pingresp->resp_time);
+ lua_call(gL, 5, 0);
+ } else {
+ c_printf("%d bytes from %s, icmp_seq=%d ttl=%d time=%dms\n",
+ pingresp->bytes,
+ ipaddrstr,
+ pingresp->seqno,
+ pingresp->ttl,
+ pingresp->resp_time);
+ }
+}
+
+static void ping_by_hostname(const char *name, ip_addr_t *ipaddr, void *arg) {
+ struct ping_option *ping_opt = (struct ping_option *)c_zalloc(sizeof(struct ping_option));
+
+ if (ipaddr->addr == IPADDR_NONE) {
+ c_printf("problem resolving hostname\n");
+ return;
+ }
+
+ ping_opt->count = ping_host_count;
+ ping_opt->ip = ipaddr->addr;
+ ping_opt->coarse_time = 0;
+ ping_opt->recv_function = &ping_received;
+
+ ping_start(ping_opt);
+}
+
+/**
+ * net.ping()
+ * Description:
+ * Send ICMP ping request to address, optionally call callback when response received
+ * Syntax:
+ * wifi.sta.getconfig(ssid, password) --Set STATION configuration, Auto-connect by default, Connects to any BSSID
+ * net.ping(address) -- send 4 ping requests to target address
+ * net.ping(address, n) -- send n ping requests to target address
+ * net.ping(address, n, callback) -- send n ping requests to target address
+ * Parameters:
+ * address: string
+ * n: number of requests to send
+ * callback:
+ * Returns:
+ * Nothing.
+ *
+ * Example:
+ * net.ping("192.168.0.1") -- send 4 pings to 192.168.0.1
+ * net.ping("192.168.0.1", 10) -- send 10 pings to 192.168.0.1
+ * net.ping("192.168.0.1", 10, got_ping) -- send 10 pings to 192.168.0.1, call got_ping() with the
+ * -- ping results (delay, hops)
+ */
+static int net_ping(lua_State *L)
+{
+ const char *ping_target;
+ unsigned count = 4;
+
+ // retrieve address arg (mandatory)
+ if (lua_isstring(L, 1)) {
+ ping_target = luaL_checkstring(L, 1);
+ } else {
+ return luaL_error(L, "no address specified");
+ }
+
+ // retrieve count arg (optional)
+ if (lua_isnumber(L, 2)) {
+ count = luaL_checkinteger(L, 2);
+ }
+
+ // retrieve callback arg (optional)
+ if (lua_type(L, 3) == LUA_TFUNCTION || lua_type(L, 3) == LUA_TLIGHTFUNCTION) {
+ if (ping_callback_ref != LUA_NOREF)
+ luaL_unref(L, LUA_REGISTRYINDEX, ping_callback_ref);
+ ping_callback_ref = luaL_ref(L, LUA_REGISTRYINDEX);
+ } else {
+ ping_callback_ref = LUA_NOREF;
+ }
+
+ gL = L; // global L for net module.
+
+ // attempt to parse ping target as IP
+ uint32 ip = ipaddr_addr(ping_target);
+
+ if (ip != IPADDR_NONE) {
+ struct ping_option *ping_opt = (struct ping_option *)c_zalloc(sizeof(struct ping_option));
+
+ ping_opt->count = count;
+ ping_opt->ip = ip;
+ ping_opt->coarse_time = 0;
+ ping_opt->recv_function = &ping_received;
+
+ ping_start(ping_opt);
+ } else {
+ ping_host_count = count;
+
+ struct espconn *ping_dns_lookup;
+ espconn_create(ping_dns_lookup);
+ espconn_gethostbyname(ping_dns_lookup, ping_target, &ping_host_ip, ping_by_hostname);
+ }
+
+ return 0;
+}
+
+static int net_traceroute(lua_State *L) {
+ lua_pushnil(L);
+
+ return 1;
+}
+
+
+
#if 0
static int net_array_index( lua_State* L )
{
@@ -1545,11 +1705,15 @@ static const LUA_REG_TYPE net_dns_map[] = {
{ LNILKEY, LNILVAL }
};
-static const LUA_REG_TYPE net_map[] = {
+static const LUA_REG_TYPE net_map[] = {
{ LSTRKEY( "createServer" ), LFUNCVAL( net_createServer ) },
{ LSTRKEY( "createConnection" ), LFUNCVAL( net_createConnection ) },
{ LSTRKEY( "multicastJoin"), LFUNCVAL( net_multicastJoin ) },
{ LSTRKEY( "multicastLeave"), LFUNCVAL( net_multicastLeave ) },
+ { LSTRKEY( "test"), LFUNCVAL( net_test ) },
+ { LSTRKEY( "ping"), LFUNCVAL( net_ping ) },
+ { LSTRKEY( "pcall_test"), LFUNCVAL( net_pcall_test ) },
+ { LSTRKEY( "traceroute"), LFUNCVAL( net_traceroute ) },
{ LSTRKEY( "dns" ), LROVAL( net_dns_map ) },
{ LSTRKEY( "TCP" ), LNUMVAL( TCP ) },
{ LSTRKEY( "UDP" ), LNUMVAL( UDP ) },
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment