diff --git a/examples/socket/ping.lua b/examples/socket/ping.lua index d27a155..1a13823 100755 --- a/examples/socket/ping.lua +++ b/examples/socket/ping.lua @@ -9,8 +9,6 @@ local socket = require 'eco.socket' local time = require 'eco.time' local ICMP_HEADER_LEN = 8 -local ICMP_ECHO = 8 -local ICMP_ECHOREPLY = 0 local dest_ip = '127.0.0.1' local local_id = math.random(0, 65535) @@ -19,7 +17,7 @@ local local_data = 'hello' local function build_icmp_req() local data = { - string.char(ICMP_ECHO), -- type + string.char(socket.ICMP_ECHO), -- type '\0', -- code '\0\0', -- checksum '\0\0', -- id: the kernel will assign it with local port @@ -81,7 +79,7 @@ while true do local icmp_type, id, seq, n = parse_icmp_resp(resp) if icmp_type then - if icmp_type == ICMP_ECHOREPLY then + if icmp_type == socket.ICMP_ECHOREPLY then if id == local_id then print(string.format('%d bytes from %s: icmp_seq=%d time=%.3f ms', n, dest_ip, seq, elapsed * 1000)) end diff --git a/examples/socket/ping6.lua b/examples/socket/ping6.lua index f9a324c..275f124 100755 --- a/examples/socket/ping6.lua +++ b/examples/socket/ping6.lua @@ -9,8 +9,6 @@ local socket = require 'eco.socket' local time = require 'eco.time' local ICMP6_HEADER_LEN = 8 -local ICMP6_ECHO = 128 -local ICMP6_ECHOREPLY = 129 local dest_ip = '::1' local local_id = math.random(0, 65535) @@ -19,7 +17,7 @@ local local_data = 'hello' local function build_icmp_req() local data = { - string.char(ICMP6_ECHO), -- type + string.char(socket.ICMPV6_ECHO_REQUEST), -- type '\0', -- code '\0\0', -- checksum '\0\0', -- id: the kernel will assign it with local port @@ -81,7 +79,7 @@ while true do local icmp_type, id, seq, n = parse_icmp_resp(resp) if icmp_type then - if icmp_type == ICMP6_ECHOREPLY then + if icmp_type == socket.ICMPV6_ECHO_REPLY then if id == local_id then print(string.format('%d bytes from %s: icmp_seq=%d time=%.3f ms', n, dest_ip, seq, elapsed * 1000)) end diff --git a/socket.c b/socket.c index 8b7e32e..c2b03d0 100644 --- a/socket.c +++ b/socket.c @@ -19,6 +19,8 @@ #include #include #include +#include +#include #include "eco.h" @@ -1125,5 +1127,11 @@ int luaopen_eco_core_socket(lua_State *L) lua_add_constant(L, "PACKET_MR_PROMISC", PACKET_MR_PROMISC); + lua_add_constant(L, "ICMP_ECHOREPLY", ICMP_ECHOREPLY); + lua_add_constant(L, "ICMP_ECHO", ICMP_ECHO); + + lua_add_constant(L, "ICMPV6_ECHO_REQUEST", ICMPV6_ECHO_REQUEST); + lua_add_constant(L, "ICMPV6_ECHO_REPLY", ICMPV6_ECHO_REPLY); + return 1; }