Skip to content

Commit 2aa0f6e

Browse files
committed
feat(socket): Add some constants for ICMP and ICMPv6
Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
1 parent 8b29131 commit 2aa0f6e

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

examples/socket/ping.lua

+2-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ local socket = require 'eco.socket'
99
local time = require 'eco.time'
1010

1111
local ICMP_HEADER_LEN = 8
12-
local ICMP_ECHO = 8
13-
local ICMP_ECHOREPLY = 0
1412

1513
local dest_ip = '127.0.0.1'
1614
local local_id = math.random(0, 65535)
@@ -19,7 +17,7 @@ local local_data = 'hello'
1917

2018
local function build_icmp_req()
2119
local data = {
22-
string.char(ICMP_ECHO), -- type
20+
string.char(socket.ICMP_ECHO), -- type
2321
'\0', -- code
2422
'\0\0', -- checksum
2523
'\0\0', -- id: the kernel will assign it with local port
@@ -81,7 +79,7 @@ while true do
8179
local icmp_type, id, seq, n = parse_icmp_resp(resp)
8280

8381
if icmp_type then
84-
if icmp_type == ICMP_ECHOREPLY then
82+
if icmp_type == socket.ICMP_ECHOREPLY then
8583
if id == local_id then
8684
print(string.format('%d bytes from %s: icmp_seq=%d time=%.3f ms', n, dest_ip, seq, elapsed * 1000))
8785
end

examples/socket/ping6.lua

+2-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ local socket = require 'eco.socket'
99
local time = require 'eco.time'
1010

1111
local ICMP6_HEADER_LEN = 8
12-
local ICMP6_ECHO = 128
13-
local ICMP6_ECHOREPLY = 129
1412

1513
local dest_ip = '::1'
1614
local local_id = math.random(0, 65535)
@@ -19,7 +17,7 @@ local local_data = 'hello'
1917

2018
local function build_icmp_req()
2119
local data = {
22-
string.char(ICMP6_ECHO), -- type
20+
string.char(socket.ICMPV6_ECHO_REQUEST), -- type
2321
'\0', -- code
2422
'\0\0', -- checksum
2523
'\0\0', -- id: the kernel will assign it with local port
@@ -81,7 +79,7 @@ while true do
8179
local icmp_type, id, seq, n = parse_icmp_resp(resp)
8280

8381
if icmp_type then
84-
if icmp_type == ICMP6_ECHOREPLY then
82+
if icmp_type == socket.ICMPV6_ECHO_REPLY then
8583
if id == local_id then
8684
print(string.format('%d bytes from %s: icmp_seq=%d time=%.3f ms', n, dest_ip, seq, elapsed * 1000))
8785
end

socket.c

+8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include <linux/if_ether.h>
2020
#include <linux/if_arp.h>
2121
#include <netinet/tcp.h>
22+
#include <linux/icmpv6.h>
23+
#include <linux/icmp.h>
2224

2325
#include "eco.h"
2426

@@ -1125,5 +1127,11 @@ int luaopen_eco_core_socket(lua_State *L)
11251127

11261128
lua_add_constant(L, "PACKET_MR_PROMISC", PACKET_MR_PROMISC);
11271129

1130+
lua_add_constant(L, "ICMP_ECHOREPLY", ICMP_ECHOREPLY);
1131+
lua_add_constant(L, "ICMP_ECHO", ICMP_ECHO);
1132+
1133+
lua_add_constant(L, "ICMPV6_ECHO_REQUEST", ICMPV6_ECHO_REQUEST);
1134+
lua_add_constant(L, "ICMPV6_ECHO_REPLY", ICMPV6_ECHO_REPLY);
1135+
11281136
return 1;
11291137
}

0 commit comments

Comments
 (0)