style: Style fixes.

This commit is contained in:
polistern
2022-06-09 10:30:29 +00:00
parent f7728cf681
commit a09ab6b484
7 changed files with 82 additions and 68 deletions

View File

@ -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).

View File

@ -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 <mswsock.h>
#include <windows.h>
#include <ws2tcpip.h>
#else // NO WIN32
#else // WIN32
#include <sys/fcntl.h>
#include <sys/mman.h>
#include <sys/socket.h>
@ -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);

View File

@ -4,19 +4,19 @@
* 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 <iostream>
int main(int argc, char **argv)
{
if (argc < 2) {
if (argc < 2)
{
std::cerr << "Usage: eepget <hostname.i2p>" << std::endl;
return 1;
}
std::string target(argv[1]);
SAM::StreamSession s("eepget");
auto lookupResult = s.namingLookup(target);
@ -24,9 +24,13 @@ int main(int argc, char **argv)
auto conn = connResult.value.get();
conn->write("GET / HTTP/1.1\r\n\r\n");
auto reply = conn->read();
while (!reply.empty()) {
while (!reply.empty())
{
std::cout << reply << std::flush;
reply = conn->read();
}
std::cout << std::endl;
conn->close();
}

View File

@ -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
*/

View File

@ -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 <ctime>
@ -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;
}
@ -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<std::unique_ptr<I2pSocket>> 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,14 +692,14 @@ 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)
@ -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) {
if (result.isOk)
{
struct i2psam_socket *socket = new i2psam_socket;
socket->impl = std::move(result.value);
return socket;
} else
}
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) {
if (result.isOk)
{
struct i2psam_socket *socket = new i2psam_socket;
socket->impl = std::move(result.value);
return socket;
} else
}
else
return nullptr;
}

View File

@ -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();