add minimal logger

move some printf to use logger

remove warnings from bencode.h
pull/1/head
Jeff Becker 6 years ago
parent 720452770b
commit 39b1714f27
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -25,6 +25,7 @@ set(LIB_SRC
llarp/exit_info.c
llarp/exit_route.c
llarp/iwp_link.cpp
llarp/logger.cpp
llarp/link.c
llarp/link_intro.cpp
llarp/link_message.cpp
@ -33,7 +34,7 @@ set(LIB_SRC
llarp/mem_std.cpp
llarp/net.cpp
llarp/nodedb.cpp
llarp/router_contact.c
llarp/router_contact.cpp
llarp/router.cpp
llarp/router_identity.c
llarp/threadpool.cpp

@ -106,8 +106,8 @@ bdecode_read_integer(struct llarp_buffer_t* buffer, uint64_t* result)
static bool INLINE
bdecode_read_string(llarp_buffer_t* buffer, llarp_buffer_t* result)
{
size_t len;
int slen;
size_t len, slen;
int num;
char numbuf[10];
len =
@ -116,10 +116,12 @@ bdecode_read_string(llarp_buffer_t* buffer, llarp_buffer_t* result)
return false;
numbuf[len] = 0;
slen = atoi(numbuf);
if(slen < 0)
num = atoi(numbuf);
if(num < 0)
return false;
slen = num;
buffer->cur++;
len = llarp_buffer_size_left(buffer);

@ -232,9 +232,11 @@ llarp_ai_list_index(struct llarp_ai_list *l, ssize_t idx, struct llarp_ai *dst)
if(idx < 0)
return false;
if(l->list.size() > idx)
size_t i = idx;
if(l->list.size() > i)
{
llarp_ai_copy(dst, &l->list[idx]);
llarp_ai_copy(dst, &l->list[i]);
return true;
}
return false;

@ -6,6 +6,7 @@
#include <unistd.h>
#include <cstdio>
#include "ev.hpp"
#include "logger.hpp"
#include "net.hpp"
namespace llarp
@ -95,7 +96,7 @@ struct llarp_epoll_loop : public llarp_ev_loop
{
if(ev->read(readbuf, sizeof(readbuf)) == -1)
{
printf("close ev\n");
llarp::Info(__FILE__, "close ev");
close_ev(ev);
delete ev;
}
@ -145,7 +146,7 @@ struct llarp_epoll_loop : public llarp_ev_loop
}
}
llarp::Addr a(*addr);
printf("binding to %s\n", a.to_string().c_str());
llarp::Info(__FILE__, "bind to ", a.to_string());
if(bind(fd, addr, slen) == -1)
{
perror("bind()");

@ -1,6 +1,7 @@
#include <llarp/router_contact.h>
#include <llarp/link_message.hpp>
#include "buffer.hpp"
#include "logger.hpp"
#include "router.hpp"
namespace llarp
@ -28,19 +29,19 @@ namespace llarp
// we are expecting the first key to be 'a'
if(!llarp_buffer_eq(*key, "a"))
{
printf("message does not have message type\n");
llarp::Warn(__FILE__, "message has no message type");
return false;
}
if(!bdecode_read_string(r->buffer, &strbuf))
{
printf("could not value of message type");
llarp::Warn(__FILE__, "could not read value of message type");
return false;
}
// bad key size
if(strbuf.sz != 1)
{
printf("bad mesage type size: %ld\n", strbuf.sz);
llarp::Warn(__FILE__, "bad mesage type size: ", strbuf.sz);
return false;
}
handler->msgtype = *strbuf.cur;
@ -75,10 +76,9 @@ namespace llarp
{
if(!llarp_rc_bdecode(from->get_remote_router(from), buff))
{
printf("failed to decode RC\n");
llarp::Warn(__FILE__, "failed to decode RC");
return false;
}
printf("decoded rc\n");
return true;
}
else if(llarp_buffer_eq(key, "v"))
@ -87,14 +87,14 @@ namespace llarp
return false;
if(proto != LLARP_PROTO_VERSION)
{
printf("llarp protocol version missmatch\n");
llarp::Warn(__FILE__, "llarp protocol version missmatch ", proto);
return false;
}
return true;
}
else
{
printf("invalid LIM key: %c\n", *key.cur);
llarp::Warn(__FILE__, "invalid LIM key: ", *key.cur);
return false;
}
}
@ -136,7 +136,6 @@ namespace llarp
bool
InboundMessageHandler::FlushReplies()
{
printf("sending replies\n");
bool success = true;
while(sendq.size())
{

@ -0,0 +1,6 @@
#include "logger.hpp"
namespace llarp
{
LogLevel loglevel = eLogDebug;
}

@ -0,0 +1,96 @@
#ifndef LLARP_LOGGER_HPP
#define LLARP_LOGGER_HPP
#include <chrono>
#include <iostream>
#include <sstream>
namespace llarp
{
enum LogLevel
{
eLogDebug,
eLogInfo,
eLogWarn,
eLogError
};
extern LogLevel loglevel;
/** internal */
template < typename TArg >
void
LogAppend(std::stringstream& ss, TArg&& arg)
{
ss << std::forward< TArg >(arg);
}
/** internal */
template < typename TArg, typename... TArgs >
void
LogAppend(std::stringstream& ss, TArg&& arg, TArgs&&... args)
{
LogAppend(ss, std::forward< TArg >(arg));
LogAppend(ss, std::forward< TArgs >(args)...);
}
/** internal */
template < typename... TArgs >
void
Log(LogLevel lvl, const char* tag, TArgs&&... args)
{
if(loglevel < lvl)
return;
std::stringstream ss;
switch(lvl)
{
case eLogDebug:
ss << "[DBG] ";
break;
case eLogInfo:
ss << "[NFO] ";
break;
case eLogWarn:
ss << "[WRN] ";
break;
case eLogError:
ss << "[ERR] ";
break;
}
auto now = std::chrono::steady_clock::now().time_since_epoch();
ss << std::chrono::duration_cast< std::chrono::milliseconds >(now).count()
<< " ";
LogAppend(ss, std::forward< TArgs >(args)...);
std::cerr << ss.str() << std::endl;
}
template < typename... TArgs >
void
Debug(const char* tag, TArgs&&... args)
{
Log(eLogDebug, tag, std::forward< TArgs >(args)...);
}
template < typename... TArgs >
void
Info(const char* tag, TArgs&&... args)
{
Log(eLogInfo, tag, std::forward< TArgs >(args)...);
}
template < typename... TArgs >
void
Warn(const char* tag, TArgs&&... args)
{
Log(eLogWarn, tag, std::forward< TArgs >(args)...);
}
template < typename... TArgs >
void
Error(const char* tag, TArgs&&... args)
{
Log(eLogError, tag, std::forward< TArgs >(args)...);
}
}
#endif

@ -7,6 +7,7 @@
#include "buffer.hpp"
#include "encode.hpp"
#include "logger.hpp"
#include "net.hpp"
#include "str.hpp"
@ -68,11 +69,11 @@ llarp_router::try_connect(fs::path rcfile)
f.read((char *)buf.base, sz);
}
else
printf("file too large\n");
llarp::Error(__FILE__, rcfile, " too large");
}
else
{
printf("failed to open %s\n", rcfile.c_str());
llarp::Error(__FILE__, "failed to open ", rcfile);
return;
}
}
@ -80,21 +81,17 @@ llarp_router::try_connect(fs::path rcfile)
{
if(llarp_rc_verify_sig(&crypto, &remote))
{
printf("signature valided\n");
if(llarp_router_try_connect(this, &remote))
llarp::Info(__FILE__, "verified signature");
if(!llarp_router_try_connect(this, &remote))
{
printf("session attempt started\n");
}
else
{
printf("session already pending\n");
llarp::Warn(__FILE__, "session already made");
}
}
else
printf("failed to verify signature\n");
llarp::Error(__FILE__, "failed to verify signature of RC");
}
else
printf("failed to decode buffer, read=%ld\n", buf.cur - buf.base);
llarp::Error(__FILE__, "failed to decode RC");
llarp_rc_free(&remote);
}
@ -121,13 +118,12 @@ llarp_router::Ready()
bool
llarp_router::SaveRC()
{
printf("verify rc signature... ");
llarp::Info(__FILE__, "verify RC signature");
if(!llarp_rc_verify_sig(&crypto, &rc))
{
printf(" BAD!\n");
llarp::Error(__FILE__, "RC has bad signature not saving");
return false;
}
printf(" OK.\n");
byte_t tmp[MAX_RC_SIZE];
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
@ -138,9 +134,11 @@ llarp_router::SaveRC()
if(f.is_open())
{
f.write((char *)buf.base, buf.cur - buf.base);
llarp::Info(__FILE__, "RC saved to ", our_rc_file);
return true;
}
}
llarp::Error(__FILE__, "did not save RC to ", our_rc_file);
return false;
}
@ -156,7 +154,6 @@ llarp_router::Close()
void
llarp_router::on_try_connect_result(llarp_link_establish_job *job)
{
printf("on_try_connect_result\n");
if(job->session)
{
llarp_rc *remote = job->session->get_remote_router(job->session);
@ -166,12 +163,18 @@ llarp_router::on_try_connect_result(llarp_link_establish_job *job)
llarp::pubkey pubkey;
memcpy(&pubkey[0], remote->pubkey, 32);
char tmp[68] = {0};
printf("session made with %s\n",
llarp::HexEncode< decltype(pubkey), decltype(tmp) >(pubkey, tmp));
const char *pubkeystr =
llarp::HexEncode< decltype(pubkey), decltype(tmp) >(pubkey, tmp);
llarp::Info(__FILE__, "session established with ", pubkeystr);
auto itr = router->pendingMessages.find(pubkey);
if(itr != router->pendingMessages.end())
{
// flush pending
if(itr->second.size())
{
llarp::Info(__FILE__, pubkeystr, " flush ", itr->second.size(),
" pending messages");
}
for(auto &msg : itr->second)
{
auto buf = llarp::Buffer< decltype(msg) >(msg);
@ -182,7 +185,7 @@ llarp_router::on_try_connect_result(llarp_link_establish_job *job)
return;
}
}
printf("session not made\n");
llarp::Info(__FILE__, "session not established");
}
void
@ -205,33 +208,32 @@ llarp_router::Run()
if(!SaveRC())
{
printf("failed to save rc\n");
return;
}
printf("saved router contact\n");
char tmp[68] = {0};
llarp::pubkey ourPubkey;
memcpy(&ourPubkey[0], pubkey(), 32);
printf("we are %s\n",
llarp::HexEncode< llarp::pubkey, decltype(tmp) >(ourPubkey, tmp));
const char *us =
llarp::HexEncode< llarp::pubkey, decltype(tmp) >(ourPubkey, tmp);
llarp::Info(__FILE__, "we are ", us);
// start links
for(auto link : links)
{
int result = link->start_link(link, logic);
if(result == -1)
printf("link %s failed to start\n", link->name());
llarp::Warn(__FILE__, "Link ", link->name(), " failed to start");
else
printf("link %s started\n", link->name());
llarp::Info(__FILE__, "Link ", link->name(), " started");
}
printf("connecting to routers\n");
for(const auto &itr : connect)
{
printf("try connecting to %s\n", itr.first.c_str());
llarp::Info(__FILE__, "connecting to node ", itr.first);
try_connect(itr.second);
}
}
@ -253,9 +255,7 @@ llarp_router::iter_try_connect(llarp_router_link_iter *iter,
job->result = &llarp_router::on_try_connect_result;
// give router as user pointer
job->user = router;
printf("try_establish\n");
link->try_establish(link, job);
printf("return true\n");
return true;
}
@ -285,7 +285,6 @@ llarp_configure_router(struct llarp_router *router, struct llarp_config *conf)
llarp_config_iter(conf, &iter);
if(!router->Ready())
{
printf("router not ready\n");
return false;
}
return router->EnsureIdentity();
@ -304,13 +303,11 @@ llarp_router_try_connect(struct llarp_router *router, struct llarp_rc *remote)
llarp_ai addr;
if(llarp_ai_list_index(remote->addrs, 0, &addr))
{
printf("try connect to first address\n");
llarp_router_iterate_links(router,
{&addr, &llarp_router::iter_try_connect});
return true;
}
else
printf("router has no addresses?\n");
return false;
}
@ -479,12 +476,11 @@ namespace llarp
llarp_ai ai;
link->get_our_address(link, &ai);
llarp::Addr addr = ai;
printf("link %s bound to %s\n", key, addr.to_string().c_str());
self->AddLink(link);
return;
}
}
printf("link %s failed to configure\n", key);
llarp::Error(__FILE__, "link ", key, " failed to configure");
}
else if(StrEq(section, "iwp-connect"))
{

@ -2,6 +2,11 @@
#include <llarp/router_contact.h>
#include <llarp/version.h>
#include "buffer.hpp"
#include "logger.hpp"
extern "C" {
void
llarp_rc_free(struct llarp_rc *rc)
{
@ -25,7 +30,7 @@ llarp_rc_decode_dict(struct dict_reader *r, llarp_buffer_t *key)
{
uint64_t v;
llarp_buffer_t strbuf;
struct llarp_rc *rc = r->user;
llarp_rc *rc = static_cast< llarp_rc * >(r->user);
if(!key)
return true;
@ -90,7 +95,7 @@ llarp_rc_decode_dict(struct dict_reader *r, llarp_buffer_t *key)
bool
llarp_rc_bdecode(struct llarp_rc *rc, llarp_buffer_t *buff)
{
struct dict_reader r = {.user = rc, .on_key = &llarp_rc_decode_dict};
dict_reader r = {buff, rc, &llarp_rc_decode_dict};
return bdecode_read_dict(buff, &r);
}
@ -100,10 +105,8 @@ llarp_rc_verify_sig(struct llarp_crypto *crypto, struct llarp_rc *rc)
bool result = false;
llarp_sig_t sig;
byte_t tmp[MAX_RC_SIZE];
llarp_buffer_t buf;
buf.base = tmp;
buf.cur = tmp;
buf.sz = sizeof(tmp);
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
// copy sig
memcpy(sig, rc->signature, sizeof(llarp_sig_t));
// zero sig
@ -119,7 +122,7 @@ llarp_rc_verify_sig(struct llarp_crypto *crypto, struct llarp_rc *rc)
result = crypto->verify(rc->pubkey, buf, sig);
}
else
printf("llarp_rc_bencode() failed\n");
llarp::Warn(__FILE__, "RC encode failed");
// restore sig
memcpy(rc->signature, sig, sizeof(llarp_sig_t));
return result;
@ -172,3 +175,4 @@ llarp_rc_bencode(struct llarp_rc *rc, llarp_buffer_t *buff)
return false;
return bencode_end(buff);
}
}
Loading…
Cancel
Save