Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions RLib-core/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ add_executable(core-01-timer ltimer_exmaple.cpp)
add_executable(core-02-udp_echo_server ludpsocket_udpechoserver.cpp)
add_executable(core-03-udp_sender ludpsocket_udpsender.cpp)
add_executable(core-04-tcp_echo_server tcp_echo_server.cpp)
add_executable(core-05-icmp_ping licmpsocket_ping.cpp)

target_link_libraries(core-01-timer PRIVATE RLib)
target_link_libraries(core-02-udp_echo_server PRIVATE RLib)
target_link_libraries(core-03-udp_sender PRIVATE RLib)
target_link_libraries(core-04-tcp_echo_server PRIVATE RLib)
target_link_libraries(core-05-icmp_ping PRIVATE RLib)


if(CMAKE_BUILD_TYPE MATCHES "Debug")
Expand All @@ -35,4 +37,7 @@ if(CMAKE_BUILD_TYPE MATCHES "Debug")

target_compile_options(core-04-tcp_echo_server PRIVATE -fsanitize=address -g -fno-omit-frame-pointer)
target_link_options(core-04-tcp_echo_server PRIVATE -fsanitize=address)

target_compile_options(core-05-icmp_ping PRIVATE -fsanitize=address -g -fno-omit-frame-pointer)
target_link_options(core-05-icmp_ping PRIVATE -fsanitize=address)
endif()
80 changes: 80 additions & 0 deletions RLib-core/examples/licmpsocket_ping.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <cerrno>
#include <cstring>
#include <iostream>

#include "LEventLoop.hpp"
#include "LIcmpSocket.hpp"
#include "LTimer.hpp"

class Pinger
{
public:
Pinger()
: m_sequence(0)
{
if (socket.error() != LIcmpSocket::UnknownSocketError) {
std::cerr << "Failed to open ICMP socket: " << socket.errorString() << std::endl;
std::cerr << "(raw ICMP sockets usually require root privileges or CAP_NET_RAW)"
<< std::endl;
return;
}

socket.onReadyRead(this, &Pinger::readPendingReplies);

sendPing();
timer.onTimeout(this, &Pinger::sendPing);
timer.start(1000);
}

void sendPing()
{
++m_sequence;
std::string payload = "RLib ICMP ping test";

int64_t bytes = socket.ping("127.0.0.1",
0x4242,
m_sequence,
std::vector<uint8_t>(payload.begin(), payload.end()));

if (bytes > 0) {
std::cout << "Ping #" << m_sequence << " sent (" << bytes << " bytes)" << std::endl;
} else {
std::cerr << "Failed to send ping: " << socket.errorString() << std::endl;
}
}

void readPendingReplies()
{
LIcmpSocket::EchoReply reply;
while (socket.readEchoReply(&reply)) {
std::cout << "Reply from " << reply.address
<< ": id=" << reply.id
<< " seq=" << reply.sequence;

if (!reply.payload.empty()) {
std::string text(reply.payload.begin(), reply.payload.end());
std::cout << " payload=\"" << text << "\"";
}
std::cout << std::endl;
}
}

private:
LIcmpSocket socket;
LTimer timer;
uint16_t m_sequence;
};

// Sends ICMP Echo Requests to 127.0.0.1 every second and prints replies.
// Run with root privileges or CAP_NET_RAW:
//
// sudo ./core-05-icmp_ping
//
int main()
{
LEventLoop loop;

Pinger pinger;

return loop.exec();
}
2 changes: 2 additions & 0 deletions RLib-core/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ add_library(RLib STATIC
LTaskQueue.hpp
LUnbufferedTcpSocket.hpp
LUdpSocket.hpp
LIcmpSocket.hpp

LByteRingBuffer.cpp
LTimer.cpp
Expand All @@ -27,6 +28,7 @@ add_library(RLib STATIC
LTaskQueue.cpp
LUnbufferedTcpSocket.cpp
LUdpSocket.cpp
LIcmpSocket.cpp
)

target_include_directories(RLib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
Expand Down
Loading