From a09ab6b4841031b18be88b9b32d89d6be881daef Mon Sep 17 00:00:00 2001 From: polistern Date: Thu, 9 Jun 2022 10:30:29 +0000 Subject: [PATCH] style: Style fixes. --- Makefile | 4 ++-- README.md | 2 ++ compat.h | 31 +++++++++++++++------------- eepget.cpp | 38 ++++++++++++++++++---------------- i2psam-c.h | 5 +++-- i2psam.cpp | 60 +++++++++++++++++++++++++++++------------------------- i2psam.h | 10 ++++----- 7 files changed, 82 insertions(+), 68 deletions(-) diff --git a/Makefile b/Makefile index 25133f5..bc1912c 100644 --- a/Makefile +++ b/Makefile @@ -5,11 +5,11 @@ OBJS=$(SRCS:.cpp=.o) TARGET=libi2psam.a $(TARGET): $(OBJS) - $(AR) $(ARFLAGS) $(TARGET) $(OBJS) + $(AR) $(ARFLAGS) $(TARGET) $(OBJS) LOADLIBES=-L./ -li2psam eepget: eepget.cpp $(TARGET) clean: - $(RM) $(TARGET) $(OBJS) eepget + $(RM) $(TARGET) $(OBJS) eepget diff --git a/README.md b/README.md index 4db9b45..f9f1b96 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +[![License](https://img.shields.io/github/license/i2p/i2psam.svg)](https://github.com/i2p/i2psam/blob/master/LICENSE) + # i2psam A C++ library for the [SAM v3 API](https://geti2p.net/en/docs/api/samv3). diff --git a/compat.h b/compat.h index 619852b..d8c9bf3 100644 --- a/compat.h +++ b/compat.h @@ -6,7 +6,7 @@ * Distributed under the MIT software license, see the accompanying * file COPYING or http://www.opensource.org/licenses/mit-license.php. * - * see full documentation about SAM at http://www.i2p2.i2p/samv3.html + * See full documentation about SAM at http://www.i2p2.i2p/samv3.html */ #ifndef I2PSAM_COMPAT_H @@ -16,21 +16,20 @@ #ifdef _WIN32_WINNT #undef _WIN32_WINNT - -#endif +#endif // _WIN32_WINNT #define _WIN32_WINNT 0x0501 #ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN 1 -#endif +#endif // WIN32_LEAN_AND_MEAN #ifndef NOMINMAX #define NOMINMAX -#endif +#endif // NOMINMAX #ifdef FD_SETSIZE #undef FD_SETSIZE // prevent redefinition compiler warning -#endif +#endif // FD_SETSIZE #define FD_SETSIZE 1024 // max number of fds in fd_set @@ -38,7 +37,9 @@ #include #include #include -#else // NO WIN32 + +#else // WIN32 + #include #include #include @@ -54,7 +55,7 @@ #ifdef WIN32 #define MSG_DONTWAIT 0 -#else +#else // WIN32 typedef u_int SOCKET; #include "errno.h" #define WSAGetLastError() errno @@ -68,16 +69,16 @@ typedef u_int SOCKET; #define WSAENOTSOCK EBADF #define INVALID_SOCKET (SOCKET)(~0) #define SOCKET_ERROR -1 -#endif +#endif // WIN32 #ifdef WIN32 #ifndef S_IRUSR #define S_IRUSR 0400 #define S_IWUSR 0200 -#endif -#else +#endif // S_IRUSR +#else // WIN32 #define MAX_PATH 1024 -#endif +#endif // WIN32 // As Solaris does not have the MSG_NOSIGNAL flag for send(2) syscall, it is defined as 0 #if !defined(HAVE_MSG_NOSIGNAL) && !defined(MSG_NOSIGNAL) @@ -85,15 +86,17 @@ typedef u_int SOCKET; #endif #ifndef WIN32 + // PRIO_MAX is not defined on Solaris #ifndef PRIO_MAX #define PRIO_MAX 20 -#endif +#endif // PRIO_MAX + #define THREAD_PRIORITY_LOWEST PRIO_MAX #define THREAD_PRIORITY_BELOW_NORMAL 2 #define THREAD_PRIORITY_NORMAL 0 #define THREAD_PRIORITY_ABOVE_NORMAL (-2) -#endif +#endif // WIN32 size_t strnlen_int(const char *start, size_t max_len); diff --git a/eepget.cpp b/eepget.cpp index ef6b783..0e4b3d4 100644 --- a/eepget.cpp +++ b/eepget.cpp @@ -4,29 +4,33 @@ * Distributed under the MIT software license, see the accompanying * file LICENSE or http://www.opensource.org/licenses/mit-license.php. * - * see full documentation about SAM at http://www.i2p2.i2p/samv3.html + * See full documentation about SAM at http://www.i2p2.i2p/samv3.html */ #include "i2psam.h" -#include - int main(int argc, char **argv) { - if (argc < 2) { - std::cerr << "Usage: eepget " << std::endl; - return 1; + if (argc < 2) + { + std::cerr << "Usage: eepget " << std::endl; + return 1; } - std::string target(argv[1]); - SAM::StreamSession s("eepget"); - auto lookupResult = s.namingLookup(target); - auto connResult = s.connect(lookupResult.value, false); - auto conn = connResult.value.get(); - conn->write("GET / HTTP/1.1\r\n\r\n"); - auto reply = conn->read(); - while (!reply.empty()) { - std::cout << reply << std::flush; - reply = conn->read(); + + std::string target(argv[1]); + SAM::StreamSession s("eepget"); + auto lookupResult = s.namingLookup(target); + auto connResult = s.connect(lookupResult.value, false); + auto conn = connResult.value.get(); + conn->write("GET / HTTP/1.1\r\n\r\n"); + auto reply = conn->read(); + + while (!reply.empty()) + { + std::cout << reply << std::flush; + reply = conn->read(); } - conn->close(); + + std::cout << std::endl; + conn->close(); } diff --git a/i2psam-c.h b/i2psam-c.h index 5cd4070..b253e81 100644 --- a/i2psam-c.h +++ b/i2psam-c.h @@ -1,5 +1,5 @@ /** - * c wrapper for i2psam + * C wrapper for i2psam * Author: jeff * License: MIT * probably contains bugs :-DDDD @@ -15,7 +15,8 @@ struct i2psam_destination; struct i2psam_stream_session; struct i2psam_socket; -struct i2psam_stream_settings { +struct i2psam_stream_settings +{ /** * hostname of sam interface */ diff --git a/i2psam.cpp b/i2psam.cpp index 2776dce..2d5806a 100644 --- a/i2psam.cpp +++ b/i2psam.cpp @@ -7,7 +7,7 @@ * Distributed under the MIT software license, see the accompanying * file LICENSE or http://www.opensource.org/licenses/mit-license.php. * - * see full documentation about SAM at http://www.i2p2.i2p/samv3.html + * See full documentation about SAM at http://www.i2p2.i2p/samv3.html */ #include @@ -192,7 +192,7 @@ void I2pSocket::close() ::closesocket(socket_); #else ::close(socket_); -#endif +#endif // WIN32 socket_ = INVALID_SOCKET; } } @@ -223,7 +223,7 @@ SAMSession::SAMSession( { #ifdef DEBUG_ON_STDOUT std::cout << "Created a brand new SAM stream session (" << sessionID_ << ")" << std::endl; -#endif +#endif // DEBUG_ON_STDOUT } SAMSession::SAMSession(SAMSession& rhs) @@ -238,7 +238,7 @@ SAMSession::SAMSession(SAMSession& rhs) rhs.socket_.close(); #ifdef DEBUG_ON_STDOUT std::cout << "Created a new SAM session (" << sessionID_ << ") from another (" << rhs.sessionID_ << ")" << std::endl; -#endif +#endif // DEBUG_ON_STDOUT } /*static*/ @@ -260,7 +260,7 @@ std::string SAMSession::generateSessionID() #ifdef DEBUG_ON_STDOUT std::cout << "Generated session ID: " << result << std::endl; -#endif +#endif // DEBUG_ON_STDOUT return result; } @@ -308,12 +308,12 @@ RequestResult SAMSession::destGenerate() const FullDestination SAMSession::createSession(const std::string& destination) { - return createSession(destination, SAM_SIGNATURE_TYPE); + return createSession(destination, SAM_SIGNATURE_TYPE); } FullDestination SAMSession::createSession(const std::string& destination, const std::string& sigType) { - return createSession(destination, sigType, i2pOptions_); + return createSession(destination, sigType, i2pOptions_); } void SAMSession::fallSick() const { isSick_ = true; } @@ -462,7 +462,7 @@ StreamSession::StreamSession( myDestination_ = createStreamSession(destination); #ifdef DEBUG_ON_STDOUT std::cout << "Created a brand new SAM stream session (" << sessionID_ << ")" << std::endl; -#endif +#endif // DEBUG_ON_STDOUT } StreamSession::StreamSession(StreamSession &rhs) @@ -477,7 +477,7 @@ StreamSession::StreamSession(StreamSession &rhs) #ifdef DEBUG_ON_STDOUT std::cout << "Created a new SAM stream session (" << sessionID_ << ") from another (" << rhs.sessionID_ << ")" << std::endl; -#endif +#endif // DEBUG_ON_STDOUT } StreamSession::~StreamSession() @@ -485,7 +485,7 @@ StreamSession::~StreamSession() stopForwardingAll(); #ifdef DEBUG_ON_STDOUT std::cout << "Closing SAM stream session (" << sessionID_ << ") ..." << std::endl; -#endif +#endif // DEBUG_ON_STDOUT } RequestResult> StreamSession::accept(bool silent) @@ -678,7 +678,7 @@ DatagramSession::DatagramSession( myDestination_ = createDatagramSession(destination); #ifdef DEBUG_ON_STDOUT std::cout << "Created a brand new SAM datagram session (" << sessionID_ << ")" << std::endl; -#endif +#endif // DEBUG_ON_STDOUT } DatagramSession::DatagramSession(DatagramSession &rhs) @@ -692,24 +692,24 @@ DatagramSession::DatagramSession(DatagramSession &rhs) #ifdef DEBUG_ON_STDOUT std::cout << "Created a new SAM datagram session (" << sessionID_ << ") from another (" << rhs.sessionID_ << ")" << std::endl; -#endif +#endif // DEBUG_ON_STDOUT } DatagramSession::~DatagramSession() { #ifdef DEBUG_ON_STDOUT std::cout << "Closing SAM datagram session (" << sessionID_ << ") ..." << std::endl; -#endif +#endif // DEBUG_ON_STDOUT } FullDestination DatagramSession::createDatagramSession(const std::string& destination) { - return createDatagramSession(destination, SAM_SIGNATURE_TYPE); + return createDatagramSession(destination, SAM_SIGNATURE_TYPE); } FullDestination DatagramSession::createDatagramSession(const std::string& destination, const std::string& sigType) { - return createDatagramSession(destination, sigType, i2pOptions_); + return createDatagramSession(destination, sigType, i2pOptions_); } FullDestination DatagramSession::createDatagramSession(const std::string& destination, const std::string& sigType, const std::string& i2pOptions) @@ -778,7 +778,7 @@ RawSession::RawSession( myDestination_ = createRawSession(destination); #ifdef DEBUG_ON_STDOUT std::cout << "Created a brand new SAM datagram session (" << sessionID_ << ")" << std::endl; -#endif +#endif // DEBUG_ON_STDOUT } RawSession::RawSession(RawSession &rhs) @@ -791,14 +791,14 @@ RawSession::RawSession(RawSession &rhs) (void) createRawSession(myDestination_.priv); #ifdef DEBUG_ON_STDOUT std::cout << "Created a new SAM datagram session (" << sessionID_ << ") from another (" << rhs.sessionID_ << ")" << std::endl; -#endif +#endif // DEBUG_ON_STDOUT } RawSession::~RawSession() { #ifdef DEBUG_ON_STDOUT std::cout << "Closing SAM datagram session (" << sessionID_ << ") ..." << std::endl; -#endif +#endif // DEBUG_ON_STDOUT } FullDestination RawSession::createRawSession(const std::string& destination) @@ -1221,22 +1221,26 @@ int i2psam_is_sick(struct i2psam_stream_session *session) struct i2psam_socket *i2psam_accept(struct i2psam_stream_session *session, int silent) { auto result = session->impl->accept(silent); - if (result.isOk) { - struct i2psam_socket *socket = new i2psam_socket; - socket->impl = std::move(result.value); - return socket; - } else + if (result.isOk) + { + struct i2psam_socket *socket = new i2psam_socket; + socket->impl = std::move(result.value); + return socket; + } + else return nullptr; } struct i2psam_socket *i2psam_connect(struct i2psam_stream_session *session, const char *destination, int silent) { auto result = session->impl->connect(destination, silent != 0); - if (result.isOk) { - struct i2psam_socket *socket = new i2psam_socket; - socket->impl = std::move(result.value); - return socket; - } else + if (result.isOk) + { + struct i2psam_socket *socket = new i2psam_socket; + socket->impl = std::move(result.value); + return socket; + } + else return nullptr; } diff --git a/i2psam.h b/i2psam.h index 6e8e55b..16b2d43 100644 --- a/i2psam.h +++ b/i2psam.h @@ -7,7 +7,7 @@ * Distributed under the MIT software license, see the accompanying * file LICENSE or http://www.opensource.org/licenses/mit-license.php. * - * see full documentation about SAM at http://www.i2p2.i2p/samv3.html + * See full documentation about SAM at http://www.i2p2.i2p/samv3.html */ #ifndef I2PSAM_H__ @@ -54,7 +54,7 @@ #define SAM_DEFAULT_I2CP_LEASESET_ENC_TYPE "0,4" // Define this, if you want more of the original standard output diagnostics -#define DEBUG_ON_STDOUT +//#define DEBUG_ON_STDOUT #ifdef __cplusplus // __cplusplus #include "compat.h" @@ -79,9 +79,9 @@ static void print_error(const std::string &err) #ifdef DEBUG_ON_STDOUT #ifdef WIN32 std::cout << err << "(" << WSAGetLastError() << ")" << std::endl; -#else +#else // WIN32 std::cout << err << "(" << errno << ")" << std::endl; -#endif +#endif // WIN32 #endif // DEBUG_ON_STDOUT } @@ -335,7 +335,7 @@ class I2pSocket static int instances_; static void initWSA(); static void freeWSA(); -#endif +#endif // WIN32 void handshake(); void init();