diff --git a/Makefile b/Makefile index 4bcbe56bd..9a1df703a 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/include/sarp/config.h b/include/sarp/config.h index a79f73519..4262b1aef 100644 --- a/include/sarp/config.h +++ b/include/sarp/config.h @@ -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 diff --git a/include/sarp/ev.h b/include/sarp/ev.h new file mode 100644 index 000000000..eebac7658 --- /dev/null +++ b/include/sarp/ev.h @@ -0,0 +1,19 @@ +#ifndef SARP_EV_H +#define SARP_EV_H +#include + +#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 diff --git a/include/sarp/link.h b/include/sarp/link.h new file mode 100644 index 000000000..9f6965cb8 --- /dev/null +++ b/include/sarp/link.h @@ -0,0 +1,27 @@ +#ifndef SARP_LINK_H_ +#define SARP_LINK_H_ +#include +#include +#include +#include + +#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 diff --git a/libsarp/config.cpp b/libsarp/config.cpp index 4c407105f..d26e4e5d4 100644 --- a/libsarp/config.cpp +++ b/libsarp/config.cpp @@ -1,39 +1,38 @@ #include +#include "config.hpp" #include "ini.hpp" namespace sarp { - struct config - { - typedef std::map section_t; - section_t router; - section_t network; - section_t netdb; - bool Load(const char * fname) + template + 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) { diff --git a/libsarp/config.hpp b/libsarp/config.hpp new file mode 100644 index 000000000..5cf9941c2 --- /dev/null +++ b/libsarp/config.hpp @@ -0,0 +1,32 @@ +#ifndef LIBSARP_CONFIG_HPP +#define LIBSARP_CONFIG_HPP +#include +#include + +#include + +namespace sarp +{ + struct Config + { + typedef std::map 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 diff --git a/libsarp/link.cpp b/libsarp/link.cpp new file mode 100644 index 000000000..cc6497480 --- /dev/null +++ b/libsarp/link.cpp @@ -0,0 +1,34 @@ +#include "link.hpp" +#include + +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(crypto, remote); + } + sessions[remote]->RecvFrom(recvbuff, ret); + } + } + while(ret != -1); + return -1; + } +} diff --git a/libsarp/link.hpp b/libsarp/link.hpp new file mode 100644 index 000000000..53a10a95b --- /dev/null +++ b/libsarp/link.hpp @@ -0,0 +1,64 @@ +#ifndef LIBSARP_LINK_HPP +#define LIBSARP_LINK_HPP +#include +#include +#include +#include +#include + +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_ptr; + + struct Link + { + typedef std::map Sessions; + + int sockfd; + Sessions sessions; + sarp_seckey_t transportSecKey; + sarp_pubkey_t transportPubKey; + + sarp_crypto * crypto; + + int Run(); + }; +} + +#endif diff --git a/libsarp/router.cpp b/libsarp/router.cpp new file mode 100644 index 000000000..ba9f48193 --- /dev/null +++ b/libsarp/router.cpp @@ -0,0 +1,13 @@ +#include +#include +#include "link.hpp" +#include + +namespace sarp +{ + struct Router + { + std::vector activeLinks; + }; +} +