From 0567908a05823f9eaab230e15f84f0ca135c30f2 Mon Sep 17 00:00:00 2001 From: Baptiste Wicht Date: Sun, 11 Sep 2016 20:08:41 +0200 Subject: [PATCH] Reformat code --- programs/ping/src/main.cpp | 22 +++---- tlib/include/tlib/net.hpp | 6 +- tlib/src/net.cpp | 124 +++++++++++++++++++------------------ 3 files changed, 77 insertions(+), 75 deletions(-) diff --git a/programs/ping/src/main.cpp b/programs/ping/src/main.cpp index 3e5a1b91..e8cd8414 100644 --- a/programs/ping/src/main.cpp +++ b/programs/ping/src/main.cpp @@ -33,14 +33,14 @@ int main(int argc, char* argv[]) { tlib::socket sock(tlib::socket_domain::AF_INET, tlib::socket_type::RAW, tlib::socket_protocol::ICMP); - if(!sock){ + if (!sock) { tlib::printf("ls: socket error: %s\n", std::error_message(sock.error())); return 1; } sock.listen(true); - if(!sock){ + if (!sock) { tlib::printf("ls: socket error: %s\n", std::error_message(sock.error())); return 1; } @@ -51,11 +51,11 @@ int main(int argc, char* argv[]) { desc.type = tlib::icmp::type::ECHO_REQUEST; desc.code = 0; - for(size_t i = 0; i < N; ++i){ + for (size_t i = 0; i < N; ++i) { auto packet = sock.prepare_packet(&desc); if (!sock) { - if(sock.error() == std::ERROR_SOCKET_TIMEOUT){ + if (sock.error() == std::ERROR_SOCKET_TIMEOUT) { tlib::printf("Unable to resolve MAC address for target IP\n"); return 1; } @@ -77,11 +77,11 @@ int main(int argc, char* argv[]) { } auto before = tlib::ms_time(); - auto after = before; + auto after = before; - while(true){ + while (true) { // Make sure we don't wait for more than the timeout - if(after > before + timeout_ms){ + if (after > before + timeout_ms) { break; } @@ -91,7 +91,7 @@ int main(int argc, char* argv[]) { auto p = sock.wait_for_packet(remaining); if (!sock) { - if(sock.error() == std::ERROR_SOCKET_TIMEOUT){ + if (sock.error() == std::ERROR_SOCKET_TIMEOUT) { tlib::printf("%s unreachable\n", ip.c_str()); handled = true; sock.clear(); @@ -104,7 +104,7 @@ int main(int argc, char* argv[]) { auto command_type = static_cast(icmp_header->type); - if(command_type == tlib::icmp::type::ECHO_REPLY){ + if (command_type == tlib::icmp::type::ECHO_REPLY) { tlib::printf("Reply received from %s\n", ip.c_str()); handled = true; } @@ -114,14 +114,14 @@ int main(int argc, char* argv[]) { tlib::release_packet(p); } - if(handled){ + if (handled) { break; } after = tlib::ms_time(); } - if(i < N - 1){ + if (i < N - 1) { tlib::sleep_ms(1000); } } diff --git a/tlib/include/tlib/net.hpp b/tlib/include/tlib/net.hpp index 98f2bcda..944f9ffd 100644 --- a/tlib/include/tlib/net.hpp +++ b/tlib/include/tlib/net.hpp @@ -41,13 +41,13 @@ struct socket { * \brief Indicates if the socket is open or not * \return true if the socket is open, false otherwise */ - bool open() const ; + bool open() const; /*! * \brief Indicates if everything is in order * \return true if everything is good, false otherwise */ - bool good() const ; + bool good() const; /*! * \brief Indicates if everything is in order @@ -59,7 +59,7 @@ struct socket { * \brief Returns the error code, if any * \return the error code if any, 0 otherwise */ - size_t error() const ; + size_t error() const; /*! * \brief Clear the error code diff --git a/tlib/src/net.cpp b/tlib/src/net.cpp index c6879ee5..32a7a6ab 100644 --- a/tlib/src/net.cpp +++ b/tlib/src/net.cpp @@ -4,61 +4,62 @@ // (See accompanying file LICENSE or copy at // http://www.opensource.org/licenses/MIT) //======================================================================= + #include "tlib/net.hpp" #include "tlib/malloc.hpp" -std::expected tlib::socket_open(socket_domain domain, socket_type type, socket_protocol protocol){ +std::expected tlib::socket_open(socket_domain domain, socket_type type, socket_protocol protocol) { int64_t fd; asm volatile("mov rax, 0x3000; mov rbx, %[type]; mov rcx, %[type]; mov rdx, %[protocol]; int 50; mov %[fd], rax" - : [fd] "=m" (fd) - : [domain] "g" (static_cast(domain)), [type] "g" (static_cast(type)), [protocol] "g" (static_cast(protocol)) - : "rax", "rbx", "rcx", "rdx"); + : [fd] "=m"(fd) + : [domain] "g"(static_cast(domain)), [type] "g"(static_cast(type)), [protocol] "g"(static_cast(protocol)) + : "rax", "rbx", "rcx", "rdx"); - if(fd < 0){ + if (fd < 0) { return std::make_expected_from_error(-fd); } else { return std::make_expected(fd); } } -void tlib::socket_close(size_t fd){ +void tlib::socket_close(size_t fd) { asm volatile("mov rax, 0x3001; mov rbx, %[fd]; int 50;" - : /* No outputs */ - : [fd] "g" (fd) - : "rax", "rbx"); + : /* No outputs */ + : [fd] "g"(fd) + : "rax", "rbx"); } -std::expected tlib::prepare_packet(size_t socket_fd, void* desc){ +std::expected tlib::prepare_packet(size_t socket_fd, void* desc) { auto buffer = malloc(2048); int64_t fd; uint64_t index; asm volatile("mov rax, 0x3002; mov rbx, %[socket]; mov rcx, %[desc]; mov rdx, %[buffer]; int 50; mov %[fd], rax; mov %[index], rbx;" - : [fd] "=m" (fd), [index] "=m" (index) - : [socket] "g" (socket_fd), [desc] "g" (reinterpret_cast(desc)), [buffer] "g" (reinterpret_cast(buffer)) - : "rax", "rbx", "rcx", "rdx"); + : [fd] "=m"(fd), [index] "=m"(index) + : [socket] "g"(socket_fd), [desc] "g"(reinterpret_cast(desc)), [buffer] "g"(reinterpret_cast(buffer)) + : "rax", "rbx", "rcx", "rdx"); - if(fd < 0){ + if (fd < 0) { return std::make_expected_from_error(-fd); } else { tlib::packet p; - p.fd = fd; - p.index = index; + p.fd = fd; + p.index = index; p.payload = static_cast(buffer); return std::make_expected(p); } } -std::expected tlib::finalize_packet(size_t socket_fd, tlib::packet p){ +std::expected tlib::finalize_packet(size_t socket_fd, tlib::packet p) { auto packet_fd = p.fd; int64_t code; asm volatile("mov rax, 0x3003; mov rbx, %[socket]; mov rcx, %[packet]; int 50; mov %[code], rax" - : [code] "=m" (code) - : [socket] "g" (socket_fd), [packet] "g" (packet_fd) - : "rax", "rbx", "rcx"); + : [code] "=m"(code) + : [socket] "g"(socket_fd), [packet] "g"(packet_fd) + : "rax", "rbx", "rcx"); - if(code < 0){ + if (code < 0) { return std::make_expected_from_error(-code); } else { return std::make_expected(); @@ -67,80 +68,81 @@ std::expected tlib::finalize_packet(size_t socket_fd, tlib::packet p){ free(p.payload); } -std::expected tlib::listen(size_t socket_fd, bool l){ +std::expected tlib::listen(size_t socket_fd, bool l) { int64_t code; asm volatile("mov rax, 0x3004; mov rbx, %[socket]; mov rcx, %[listen]; int 50; mov %[code], rax" - : [code] "=m" (code) - : [socket] "g" (socket_fd), [listen] "g" (size_t(l)) - : "rax", "rbx", "rcx"); + : [code] "=m"(code) + : [socket] "g"(socket_fd), [listen] "g"(size_t(l)) + : "rax", "rbx", "rcx"); - if(code < 0){ + if (code < 0) { return std::make_expected_from_error(-code); } else { return std::make_expected(); } } -std::expected tlib::wait_for_packet(size_t socket_fd){ +std::expected tlib::wait_for_packet(size_t socket_fd) { auto buffer = malloc(2048); int64_t code; uint64_t payload; asm volatile("mov rax, 0x3005; mov rbx, %[socket]; mov rcx, %[buffer]; int 50; mov %[code], rax; mov %[payload], rbx;" - : [payload] "=m" (payload), [code] "=m" (code) - : [socket] "g" (socket_fd), [buffer] "g" (reinterpret_cast(buffer)) - : "rax", "rbx", "rcx"); + : [payload] "=m"(payload), [code] "=m"(code) + : [socket] "g"(socket_fd), [buffer] "g"(reinterpret_cast(buffer)) + : "rax", "rbx", "rcx"); - if(code < 0){ + if (code < 0) { free(buffer); return std::make_expected_from_error(-code); } else { tlib::packet p; - p.index = code; + p.index = code; p.payload = reinterpret_cast(payload); return std::make_expected(p); } } -std::expected tlib::wait_for_packet(size_t socket_fd, size_t ms){ +std::expected tlib::wait_for_packet(size_t socket_fd, size_t ms) { auto buffer = malloc(2048); int64_t code; uint64_t payload; asm volatile("mov rax, 0x3006; mov rbx, %[socket]; mov rcx, %[buffer]; mov rdx, %[ms]; int 50; mov %[code], rax; mov %[payload], rbx;" - : [payload] "=m" (payload), [code] "=m" (code) - : [socket] "g" (socket_fd), [buffer] "g" (reinterpret_cast(buffer)), [ms] "g" (ms) - : "rax", "rbx", "rcx"); + : [payload] "=m"(payload), [code] "=m"(code) + : [socket] "g"(socket_fd), [buffer] "g"(reinterpret_cast(buffer)), [ms] "g"(ms) + : "rax", "rbx", "rcx"); - if(code < 0){ + if (code < 0) { free(buffer); return std::make_expected_from_error(-code); } else { tlib::packet p; - p.index = code; + p.index = code; p.payload = reinterpret_cast(payload); return std::make_expected(p); } } -void tlib::release_packet(packet& packet){ - if(packet.payload){ +void tlib::release_packet(packet& packet) { + if (packet.payload) { free(packet.payload); } } -tlib::socket::socket(socket_domain domain, socket_type type, socket_protocol protocol) : domain(domain), type(type), protocol(protocol), fd(0), error_code(0) { +tlib::socket::socket(socket_domain domain, socket_type type, socket_protocol protocol) + : domain(domain), type(type), protocol(protocol), fd(0), error_code(0) { auto open_status = tlib::socket_open(domain, type, protocol); - if(open_status.valid()){ + if (open_status.valid()) { fd = *open_status; } else { error_code = open_status.error(); } } -tlib::socket::~socket(){ - if(fd){ +tlib::socket::~socket() { + if (fd) { tlib::socket_close(fd); } } @@ -153,7 +155,7 @@ bool tlib::socket::good() const { return error_code == 0; } -tlib::socket::operator bool(){ +tlib::socket::operator bool() { return good(); } @@ -161,29 +163,29 @@ size_t tlib::socket::error() const { return error_code; } -void tlib::socket::clear(){ +void tlib::socket::clear() { error_code = 0; } -void tlib::socket::listen(bool l){ - if(!good() || !open()){ +void tlib::socket::listen(bool l) { + if (!good() || !open()) { return; } auto status = tlib::listen(fd, l); - if(!status){ + if (!status) { error_code = status.error(); } } -tlib::packet tlib::socket::prepare_packet(void* desc){ - if(!good() || !open()){ +tlib::packet tlib::socket::prepare_packet(void* desc) { + if (!good() || !open()) { return tlib::packet(); } auto packet = tlib::prepare_packet(fd, desc); - if(!packet){ + if (!packet) { error_code = packet.error(); return tlib::packet(); } else { @@ -191,25 +193,25 @@ tlib::packet tlib::socket::prepare_packet(void* desc){ } } -void tlib::socket::finalize_packet(tlib::packet p){ - if(!good() || !open()){ +void tlib::socket::finalize_packet(tlib::packet p) { + if (!good() || !open()) { return; } auto status = tlib::finalize_packet(fd, p); - if(!status){ + if (!status) { error_code = status.error(); } } -tlib::packet tlib::socket::wait_for_packet(){ - if(!good() || !open()){ +tlib::packet tlib::socket::wait_for_packet() { + if (!good() || !open()) { return tlib::packet(); } auto p = tlib::wait_for_packet(fd); - if(!p){ + if (!p) { error_code = p.error(); return tlib::packet(); } else { @@ -217,14 +219,14 @@ tlib::packet tlib::socket::wait_for_packet(){ } } -tlib::packet tlib::socket::wait_for_packet(size_t ms){ - if(!good() || !open()){ +tlib::packet tlib::socket::wait_for_packet(size_t ms) { + if (!good() || !open()) { return tlib::packet(); } auto p = tlib::wait_for_packet(fd, ms); - if(!p){ + if (!p) { error_code = p.error(); return tlib::packet(); } else {