Skip to content

Commit 10c226f

Browse files
author
vsky
committed
Ping as a part of net module
1 parent 0fe0952 commit 10c226f

File tree

7 files changed

+72
-70
lines changed

7 files changed

+72
-70
lines changed

app/include/user_config.h

+3
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@
136136
//#define TIMER_SUSPEND_ENABLE
137137
//#define PMSLEEP_ENABLE
138138

139+
// The net module optionally offers net info functionnality. Uncomment the following
140+
// to enable the functionnality.
141+
//#define NET_INFO_ENABLE
139142

140143
// The WiFi module optionally offers an enhanced level of WiFi connection
141144
// management, using internal timer callbacks. Whilst many Lua developers

app/include/user_modules.h

-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
//#define LUA_USE_MODULES_MDNS
4040
#define LUA_USE_MODULES_MQTT
4141
#define LUA_USE_MODULES_NET
42-
//#define LUA_USE_MODULES_NET_INFO
4342
#define LUA_USE_MODULES_NODE
4443
#define LUA_USE_MODULES_OW
4544
//#define LUA_USE_MODULES_PCM

app/modules/net.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include "lwip/udp.h"
2121
#include "lwip/dhcp.h"
2222

23+
#include "net_info.h"
24+
2325
typedef enum net_type {
2426
TYPE_TCP_SERVER = 0,
2527
TYPE_TCP_CLIENT,
@@ -1004,7 +1006,6 @@ static int net_ifinfo( lua_State* L ) {
10041006
field_from_ipaddr(L, "ntp_server", &nif->dhcp->offered_ntp_addr);
10051007
}
10061008
lua_setfield(L, -2, "dhcp");
1007-
10081009
return 1;
10091010
}
10101011

@@ -1070,6 +1071,9 @@ LROT_BEGIN(net, NULL, 0)
10701071
LROT_FUNCENTRY( ifinfo, net_ifinfo )
10711072
LROT_FUNCENTRY( multicastJoin, net_multicastJoin )
10721073
LROT_FUNCENTRY( multicastLeave, net_multicastLeave )
1074+
#ifdef NET_INFO_ENABLE
1075+
LROT_FUNCENTRY( ping, net_info_ping )
1076+
#endif
10731077
LROT_TABENTRY( dns, net_dns_map )
10741078
#ifdef TLS_MODULE_PRESENT
10751079
LROT_TABENTRY( cert, tls_cert )

app/modules/net_info.c

+6-11
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
// ***************************************************************************
66

77
// #define NODE_DEBUG
8-
#define LUA_USE_MODULES_NET_INFO
98

9+
#include "net_info.h"
10+
1011
#include "module.h"
1112
#include "lauxlib.h"
1213

@@ -103,8 +104,8 @@ static void net_info_ping_raw(const char *name, ip_addr_t *ipaddr, ping_t nip) {
103104
}
104105
}
105106

106-
// Lua: net_info.ping(target, [count], responseCB)
107-
static int net_info_ping(lua_State *L)
107+
// Lua: net.ping(domain, [count], callback)
108+
int net_info_ping(lua_State *L)
108109
{
109110
ip_addr_t addr;
110111

@@ -118,7 +119,7 @@ static int net_info_ping(lua_State *L)
118119
ping_t nip = (ping_t) memset(lua_newuserdata(L, sizeof(*nip)), 0, sizeof(*nip));
119120

120121
/* Register C closure with 2 Upvals: (1) Lua CB function; (2) nip Userdata */
121-
lua_pushcclosure(L, ping_received_sent, 2); // stack has nip UD, responseCB; [-n, +1, m]
122+
lua_pushcclosure(L, ping_received_sent, 2); // stack has nip UD, callback; [-n, +1, m]
122123

123124
nip->ping_callback_ref = luaL_ref(L, LUA_REGISTRYINDEX); // registers the closure to registry [-1, +0, m]
124125
nip->ping_opt.count = l_count;
@@ -127,7 +128,7 @@ static int net_info_ping(lua_State *L)
127128
nip->ping_opt.sent_function = (ping_sent_function) &ping_CB;
128129

129130
NODE_DBG("[net_info_ping] nip = %p, nip->ping_callback_ref = %p\n", nip, nip->ping_callback_ref);
130-
131+
131132
err_t err = dns_gethostbyname(ping_target, &addr, (dns_found_callback) net_info_ping_raw, nip);
132133
if (err != ERR_OK && err != ERR_INPROGRESS) {
133134
luaL_unref(L, LUA_REGISTRYINDEX, nip->ping_callback_ref);
@@ -139,9 +140,3 @@ static int net_info_ping(lua_State *L)
139140
}
140141
return 0;
141142
}
142-
143-
LROT_BEGIN(net_info, NULL, 0)
144-
LROT_FUNCENTRY( ping, net_info_ping )
145-
LROT_END( net_info, NULL, 0 )
146-
147-
NODEMCU_MODULE(NET_INFO, "net_info", net_info, NULL);

app/modules/net_info.h

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#ifdef NET_INFO_ENABLE
2+
int net_info_ping(lua_State *L);
3+
#endif

docs/modules/net.md

+55
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,61 @@ Sets the IP of the DNS server used to resolve hostnames. Default: resolver1.open
626626
#### See also
627627
[`net.dns:getdnsserver()`](#netdnsgetdnsserver)
628628

629+
630+
### net.ping()
631+
632+
This is a function to ping a server. A callback function is called when response is or is not received.
633+
634+
The function is disabled by default to enable this functionnality define `NET_INFO_ENABLE` macro needs to be defined in `user_config.h`.
635+
636+
#### Syntax
637+
`net.ping(domain, [count], callback)`
638+
639+
#### Parameters
640+
- `domain` destination domain or IP address
641+
- `count` number of ping packets to be sent (optional parameter, default value is 4)
642+
- `callback(bytes, ipaddr, seqno, rtt)` callback function which is invoked when response is received where
643+
- `bytes` number of bytes received from destination server (0 means no response)
644+
- `ipaddr` destination serve IP address
645+
- `seqno` ICMP sequence number
646+
- `rtt` round trip time in ms
647+
648+
If domain name cannot be resolved callback is invoked with `bytes` parameter equal to 0 (i.e. no response) and `nil` values for all other parameters.
649+
650+
#### Returns
651+
`nil`
652+
653+
#### Example
654+
```lua
655+
net.ping("www.nodemcu.com", function (b, ip, sq, tm)
656+
if ip then print(("%d bytes from %s, icmp_seq=%d time=%dms"):format(b, ip, sq, tm)) else print("Invalid IP address") end
657+
end)
658+
```
659+
660+
Multiple pings can start in short sequence thought if the new ping overlaps with the previous one the first stops receiving answers, i.e.
661+
```lua
662+
function ping_resp(b, ip, sq, tm)
663+
print(string.format("%d bytes from %s, icmp_seq=%d time=%dms", b, ip, sq, tm))
664+
end
665+
666+
net.ping("8.8.8.8", 4, ping_resp)
667+
tmr.create():alarm(1000, tmr.ALARM_SINGLE, function() net.ping("8.8.4.4", 4, ping_resp) end)
668+
```
669+
gives
670+
```
671+
32 bytes from 8.8.8.8, icmp_seq=9 time=14ms
672+
32 bytes from 8.8.8.8, icmp_seq=10 time=9ms
673+
32 bytes from 8.8.4.4, icmp_seq=11 time=6ms
674+
32 bytes from 8.8.4.4, icmp_seq=13 time=12ms
675+
0 bytes from 8.8.8.8, icmp_seq=0 time=0ms
676+
32 bytes from 8.8.4.4, icmp_seq=15 time=16ms
677+
0 bytes from 8.8.8.8, icmp_seq=0 time=0ms
678+
32 bytes from 8.8.4.4, icmp_seq=16 time=7ms
679+
```
680+
681+
682+
683+
629684
# net.cert Module
630685

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

docs/modules/net_info.md

-57
This file was deleted.

0 commit comments

Comments
 (0)