mirror of https://github.com/oxen-io/lokinet
more
parent
78879db964
commit
f557027692
@ -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
|
@ -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…
Reference in New Issue