Skip to content

Commit

Permalink
feat(socket): Add some constants for ICMP and ICMPv6
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <[email protected]>
  • Loading branch information
zhaojh329 committed Aug 2, 2024
1 parent 8b29131 commit 2aa0f6e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
6 changes: 2 additions & 4 deletions examples/socket/ping.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions examples/socket/ping6.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include <linux/if_ether.h>
#include <linux/if_arp.h>
#include <netinet/tcp.h>
#include <linux/icmpv6.h>
#include <linux/icmp.h>

#include "eco.h"

Expand Down Expand Up @@ -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;
}

0 comments on commit 2aa0f6e

Please sign in to comment.