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_info.c
llarp/exit_route.c llarp/exit_route.c
llarp/iwp_link.cpp llarp/iwp_link.cpp
llarp/logger.cpp
llarp/link.c llarp/link.c
llarp/link_intro.cpp llarp/link_intro.cpp
llarp/link_message.cpp llarp/link_message.cpp
@ -33,7 +34,7 @@ set(LIB_SRC
llarp/mem_std.cpp llarp/mem_std.cpp
llarp/net.cpp llarp/net.cpp
llarp/nodedb.cpp llarp/nodedb.cpp
llarp/router_contact.c llarp/router_contact.cpp
llarp/router.cpp llarp/router.cpp
llarp/router_identity.c llarp/router_identity.c
llarp/threadpool.cpp llarp/threadpool.cpp

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

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

@ -1,6 +1,7 @@
#include <llarp/router_contact.h> #include <llarp/router_contact.h>
#include <llarp/link_message.hpp> #include <llarp/link_message.hpp>
#include "buffer.hpp" #include "buffer.hpp"
#include "logger.hpp"
#include "router.hpp" #include "router.hpp"
namespace llarp namespace llarp
@ -28,19 +29,19 @@ namespace llarp
// we are expecting the first key to be 'a' // we are expecting the first key to be 'a'
if(!llarp_buffer_eq(*key, "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; return false;
} }
if(!bdecode_read_string(r->buffer, &strbuf)) 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; return false;
} }
// bad key size // bad key size
if(strbuf.sz != 1) if(strbuf.sz != 1)
{ {
printf("bad mesage type size: %ld\n", strbuf.sz); llarp::Warn(__FILE__, "bad mesage type size: ", strbuf.sz);
return false; return false;
} }
handler->msgtype = *strbuf.cur; handler->msgtype = *strbuf.cur;
@ -75,10 +76,9 @@ namespace llarp
{ {
if(!llarp_rc_bdecode(from->get_remote_router(from), buff)) 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; return false;
} }
printf("decoded rc\n");
return true; return true;
} }
else if(llarp_buffer_eq(key, "v")) else if(llarp_buffer_eq(key, "v"))
@ -87,14 +87,14 @@ namespace llarp
return false; return false;
if(proto != LLARP_PROTO_VERSION) if(proto != LLARP_PROTO_VERSION)
{ {
printf("llarp protocol version missmatch\n"); llarp::Warn(__FILE__, "llarp protocol version missmatch ", proto);
return false; return false;
} }
return true; return true;
} }
else else
{ {
printf("invalid LIM key: %c\n", *key.cur); llarp::Warn(__FILE__, "invalid LIM key: ", *key.cur);
return false; return false;
} }
} }
@ -136,7 +136,6 @@ namespace llarp
bool bool
InboundMessageHandler::FlushReplies() InboundMessageHandler::FlushReplies()
{ {
printf("sending replies\n");
bool success = true; bool success = true;
while(sendq.size()) 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 "buffer.hpp"
#include "encode.hpp" #include "encode.hpp"
#include "logger.hpp"
#include "net.hpp" #include "net.hpp"
#include "str.hpp" #include "str.hpp"
@ -68,11 +69,11 @@ llarp_router::try_connect(fs::path rcfile)
f.read((char *)buf.base, sz); f.read((char *)buf.base, sz);
} }
else else
printf("file too large\n"); llarp::Error(__FILE__, rcfile, " too large");
} }
else else
{ {
printf("failed to open %s\n", rcfile.c_str()); llarp::Error(__FILE__, "failed to open ", rcfile);
return; return;
} }
} }
@ -80,21 +81,17 @@ llarp_router::try_connect(fs::path rcfile)
{ {
if(llarp_rc_verify_sig(&crypto, &remote)) if(llarp_rc_verify_sig(&crypto, &remote))
{ {
printf("signature valided\n"); llarp::Info(__FILE__, "verified signature");
if(llarp_router_try_connect(this, &remote)) if(!llarp_router_try_connect(this, &remote))
{ {
printf("session attempt started\n"); llarp::Warn(__FILE__, "session already made");
}
else
{
printf("session already pending\n");
} }
} }
else else
printf("failed to verify signature\n"); llarp::Error(__FILE__, "failed to verify signature of RC");
} }
else else
printf("failed to decode buffer, read=%ld\n", buf.cur - buf.base); llarp::Error(__FILE__, "failed to decode RC");
llarp_rc_free(&remote); llarp_rc_free(&remote);
} }
@ -121,13 +118,12 @@ llarp_router::Ready()
bool bool
llarp_router::SaveRC() llarp_router::SaveRC()
{ {
printf("verify rc signature... "); llarp::Info(__FILE__, "verify RC signature");
if(!llarp_rc_verify_sig(&crypto, &rc)) if(!llarp_rc_verify_sig(&crypto, &rc))
{ {
printf(" BAD!\n"); llarp::Error(__FILE__, "RC has bad signature not saving");
return false; return false;
} }
printf(" OK.\n");
byte_t tmp[MAX_RC_SIZE]; byte_t tmp[MAX_RC_SIZE];
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp); auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
@ -138,9 +134,11 @@ llarp_router::SaveRC()
if(f.is_open()) if(f.is_open())
{ {
f.write((char *)buf.base, buf.cur - buf.base); f.write((char *)buf.base, buf.cur - buf.base);
llarp::Info(__FILE__, "RC saved to ", our_rc_file);
return true; return true;
} }
} }
llarp::Error(__FILE__, "did not save RC to ", our_rc_file);
return false; return false;
} }
@ -156,7 +154,6 @@ llarp_router::Close()
void void
llarp_router::on_try_connect_result(llarp_link_establish_job *job) llarp_router::on_try_connect_result(llarp_link_establish_job *job)
{ {
printf("on_try_connect_result\n");
if(job->session) if(job->session)
{ {
llarp_rc *remote = job->session->get_remote_router(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; llarp::pubkey pubkey;
memcpy(&pubkey[0], remote->pubkey, 32); memcpy(&pubkey[0], remote->pubkey, 32);
char tmp[68] = {0}; char tmp[68] = {0};
printf("session made with %s\n", const char *pubkeystr =
llarp::HexEncode< decltype(pubkey), decltype(tmp) >(pubkey, tmp)); llarp::HexEncode< decltype(pubkey), decltype(tmp) >(pubkey, tmp);
llarp::Info(__FILE__, "session established with ", pubkeystr);
auto itr = router->pendingMessages.find(pubkey); auto itr = router->pendingMessages.find(pubkey);
if(itr != router->pendingMessages.end()) if(itr != router->pendingMessages.end())
{ {
// flush pending // flush pending
if(itr->second.size())
{
llarp::Info(__FILE__, pubkeystr, " flush ", itr->second.size(),
" pending messages");
}
for(auto &msg : itr->second) for(auto &msg : itr->second)
{ {
auto buf = llarp::Buffer< decltype(msg) >(msg); auto buf = llarp::Buffer< decltype(msg) >(msg);
@ -182,7 +185,7 @@ llarp_router::on_try_connect_result(llarp_link_establish_job *job)
return; return;
} }
} }
printf("session not made\n"); llarp::Info(__FILE__, "session not established");
} }
void void
@ -205,33 +208,32 @@ llarp_router::Run()
if(!SaveRC()) if(!SaveRC())
{ {
printf("failed to save rc\n");
return; return;
} }
printf("saved router contact\n");
char tmp[68] = {0}; char tmp[68] = {0};
llarp::pubkey ourPubkey; llarp::pubkey ourPubkey;
memcpy(&ourPubkey[0], pubkey(), 32); memcpy(&ourPubkey[0], pubkey(), 32);
printf("we are %s\n", const char *us =
llarp::HexEncode< llarp::pubkey, decltype(tmp) >(ourPubkey, tmp)); llarp::HexEncode< llarp::pubkey, decltype(tmp) >(ourPubkey, tmp);
llarp::Info(__FILE__, "we are ", us);
// start links // start links
for(auto link : links) for(auto link : links)
{ {
int result = link->start_link(link, logic); int result = link->start_link(link, logic);
if(result == -1) if(result == -1)
printf("link %s failed to start\n", link->name()); llarp::Warn(__FILE__, "Link ", link->name(), " failed to start");
else 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) 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); 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; job->result = &llarp_router::on_try_connect_result;
// give router as user pointer // give router as user pointer
job->user = router; job->user = router;
printf("try_establish\n");
link->try_establish(link, job); link->try_establish(link, job);
printf("return true\n");
return true; return true;
} }
@ -285,7 +285,6 @@ llarp_configure_router(struct llarp_router *router, struct llarp_config *conf)
llarp_config_iter(conf, &iter); llarp_config_iter(conf, &iter);
if(!router->Ready()) if(!router->Ready())
{ {
printf("router not ready\n");
return false; return false;
} }
return router->EnsureIdentity(); return router->EnsureIdentity();
@ -304,13 +303,11 @@ llarp_router_try_connect(struct llarp_router *router, struct llarp_rc *remote)
llarp_ai addr; llarp_ai addr;
if(llarp_ai_list_index(remote->addrs, 0, &addr)) if(llarp_ai_list_index(remote->addrs, 0, &addr))
{ {
printf("try connect to first address\n");
llarp_router_iterate_links(router, llarp_router_iterate_links(router,
{&addr, &llarp_router::iter_try_connect}); {&addr, &llarp_router::iter_try_connect});
return true; return true;
} }
else
printf("router has no addresses?\n");
return false; return false;
} }
@ -479,12 +476,11 @@ namespace llarp
llarp_ai ai; llarp_ai ai;
link->get_our_address(link, &ai); link->get_our_address(link, &ai);
llarp::Addr addr = ai; llarp::Addr addr = ai;
printf("link %s bound to %s\n", key, addr.to_string().c_str());
self->AddLink(link); self->AddLink(link);
return; return;
} }
} }
printf("link %s failed to configure\n", key); llarp::Error(__FILE__, "link ", key, " failed to configure");
} }
else if(StrEq(section, "iwp-connect")) else if(StrEq(section, "iwp-connect"))
{ {

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