pull/1/head
Jon Titor 7 years ago
parent 78879db964
commit f557027692
No known key found for this signature in database
GPG Key ID: DFEA3A904352EFF7

@ -9,13 +9,12 @@ STATIC_OBJ = $(STATIC_SRC:.cpp=.cpp.o)
DAEMON_SRC = $(wildcard $(REPO)/daemon/*.c)
DAEMON_OBJ = $(DAEMON_SRC:.c=.o)
PKG = pkg-config
SODIUM_FLAGS = $(shell pkg-config --cflags libsodium)
SODIUM_LIBS = $(shell pkg-config --libs libsodium)
PKGS = libsodium
REQUIRED_CFLAGS = $(shell $(PKG) --cflags $(PKGS)) -I$(REPO)/include -std=c99
REQUIRED_CXXFLAGS = $(shell $(PKG) --cflags $(PKGS)) -I$(REPO)/include -std=c++17
REQUIRED_LDFLAGS = $(LDFLAGS) $(shell $(PKG) --libs $(PKGS)) -ljemalloc
REQUIRED_CFLAGS = $(SODIUM_FLAGS) -I$(REPO)/include -std=c99
REQUIRED_CXXFLAGS = $(SODIUM_FLAGS) -I$(REPO)/include -std=c++17
REQUIRED_LDFLAGS = $(LDFLAGS) -ljemalloc $(SODIUM_LIBS)
CXXFLAGS := $(REQUIRED_CXXFLAGS)
CFLAGS := $(REQUIRED_CFLAGS)

@ -12,7 +12,7 @@ void sarp_new_config(struct sarp_config ** conf, struct sarp_alloc * mem);
void sarp_free_config(struct sarp_config ** conf);
int sarp_load_config(struct sarp_config * conf, const char * fname);
#ifdef __cplusplus
}
#endif

@ -0,0 +1,19 @@
#ifndef SARP_EV_H
#define SARP_EV_H
#include <sarp/mem.h>
#ifdef __cplusplus
extern "C" {
#endif
struct sarp_ev_loop;
void sarp_ev_loop_alloc(struct sarp_ev_loop ** ev, struct sarp_alloc * mem);
void sarp_ev_loop_free(struct sarp_ev_loop ** ev);
int sarp_ev_loop_run(struct sarp_ev_loop * ev);
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,27 @@
#ifndef SARP_LINK_H_
#define SARP_LINK_H_
#include <sarp/config.h>
#include <sarp/mem.h>
#include <sarp/router.h>
#include <sarp/ev.h>
#ifdef __cplusplus
extern "C" {
#endif
struct sarp_inet_link;
void sarp_inet_link_alloc(struct sarp_inet_link ** link, struct sarp_alloc * mem);
void sarp_inet_link_free(struct sarp_inet_link ** link);
void sarp_inet_link_configure(struct sarp_inet_link * link, struct sarp_config * conf);
void sarp_inet_link_start(struct sarp_inet_link * link, struct sarp_router * router);
void sarp_inet_link_stop(struct sarp_inet_link * link);
#ifdef __cplusplus
}
#endif
#endif

@ -1,39 +1,38 @@
#include <sarp/config.h>
#include "config.hpp"
#include "ini.hpp"
namespace sarp
{
struct config
{
typedef std::map<std::string, std::string> section_t;
section_t router;
section_t network;
section_t netdb;
bool Load(const char * fname)
template<typename Config, typename Section>
static Section find_section(Config & c, const std::string & name, const Section & sect)
{
if(c.sections.find(name) == c.sections.end())
return sect;
return c.sections[name].values;
}
bool Config::Load(const char * fname)
{
std::ifstream f;
f.open(fname);
if(f.is_open())
{
std::ifstream f;
f.open(fname);
if(f.is_open())
{
ini::Parser parser(f);
auto & top = parser.top();
return true;
}
return false;
};
ini::Parser parser(f);
auto & top = parser.top();
router = find_section(top, "router", section_t{});
network = find_section(top, "network", section_t{});
netdb = find_section(top, "netdb", section_t{});
return true;
}
return false;
};
}
extern "C" {
struct sarp_config
{
sarp::config impl;
sarp_alloc * mem;
};
void sarp_new_config(struct sarp_config ** conf, struct sarp_alloc * mem)
{

@ -0,0 +1,32 @@
#ifndef LIBSARP_CONFIG_HPP
#define LIBSARP_CONFIG_HPP
#include <map>
#include <string>
#include <sarp/config.h>
namespace sarp
{
struct Config
{
typedef std::map<std::string, std::string> section_t;
section_t router;
section_t network;
section_t netdb;
bool Load(const char * fname);
};
}
extern "C" {
struct sarp_config
{
sarp::Config impl;
sarp_alloc * mem;
};
}
#endif

@ -0,0 +1,34 @@
#include "link.hpp"
#include <cstring>
bool operator < (const sockaddr_in6 addr0, const sockaddr_in6 addr1)
{
return memcmp(addr0.sin6_addr.s6_addr, addr1.sin6_addr.s6_addr, sizeof(addr0.sin6_addr)) && addr0.sin6_port < addr1.sin6_port;
}
namespace sarp
{
int Link::Run()
{
uint8_t recvbuff[1500];
sockaddr_in6 remote;
socklen_t remotelen;
ssize_t ret = 0;
do
{
ret = recvfrom(sockfd, recvbuff, sizeof(recvbuff),0, (sockaddr *) &remote, &remotelen);
if(ret > 0)
{
auto itr = sessions.find(remote);
if(itr == sessions.end())
{
sessions[remote] = std::make_unique<PeerSession>(crypto, remote);
}
sessions[remote]->RecvFrom(recvbuff, ret);
}
}
while(ret != -1);
return -1;
}
}

@ -0,0 +1,64 @@
#ifndef LIBSARP_LINK_HPP
#define LIBSARP_LINK_HPP
#include <netinet/in.h>
#include <sarp/crypto.h>
#include <cstdint>
#include <map>
#include <memory>
namespace sarp
{
struct PeerSession
{
sockaddr_in6 remoteAddr;
sarp_pubkey_t remotePubkey;
sarp_sharedkey_t sessionKey;
uint64_t lastRX;
sarp_crypto * _crypto;
enum State
{
eHandshake0,
eHandshake1,
eHandshake2,
eHandshake3,
eEstablished,
eTimeout
};
State state;
/** inbound session */
PeerSession(sarp_crypto * crypto, sockaddr_in6 remote);
/** outbound session */
PeerSession(sarp_crypto * crypto, sockaddr_in6 remote, sarp_pubkey_t remotePubkey);
PeerSession & operator=(const PeerSession & other);
void SendTo(int sockfd, const uint8_t * buff, std::size_t sz);
void RecvFrom(const uint8_t * buff, std::size_t sz);
};
typedef std::unique_ptr<PeerSession> PeerSession_ptr;
struct Link
{
typedef std::map<sockaddr_in6, PeerSession_ptr> Sessions;
int sockfd;
Sessions sessions;
sarp_seckey_t transportSecKey;
sarp_pubkey_t transportPubKey;
sarp_crypto * crypto;
int Run();
};
}
#endif

@ -0,0 +1,13 @@
#include <sarp/router.h>
#include <sarp/link.h>
#include "link.hpp"
#include <vector>
namespace sarp
{
struct Router
{
std::vector<Link> activeLinks;
};
}
Loading…
Cancel
Save