Switch from epoll to libevent, to add OS X support.
This commit is contained in:
10
Makefile
10
Makefile
@@ -1,16 +1,16 @@
|
|||||||
all: auth-client auth-server gen-key gen-keypair
|
all: auth-client auth-server gen-key gen-keypair
|
||||||
|
|
||||||
%.o: %.cc
|
%.o: %.cc
|
||||||
g++ -std=c++11 -c -o $@ $<
|
g++ -I/usr/local/include -std=c++11 -c -o $@ $<
|
||||||
|
|
||||||
auth-client: auth-client.o crypto.o tlv.o
|
auth-client: auth-client.o crypto.o tlv.o
|
||||||
g++ -o auth-client auth-client.o crypto.o tlv.o -lsodium
|
g++ -L/usr/local/lib -o auth-client auth-client.o crypto.o tlv.o -lsodium -levent
|
||||||
|
|
||||||
auth-server: auth-server.o crypto.o tlv.o
|
auth-server: auth-server.o crypto.o tlv.o
|
||||||
g++ -o auth-server auth-server.o crypto.o tlv.o -lsodium
|
g++ -L/usr/local/lib -o auth-server auth-server.o crypto.o tlv.o -lsodium -levent
|
||||||
|
|
||||||
gen-key: gen-key.o crypto.o tlv.o
|
gen-key: gen-key.o crypto.o tlv.o
|
||||||
g++ -o gen-key gen-key.o crypto.o tlv.o -lsodium
|
g++ -L/usr/local/lib -o gen-key gen-key.o crypto.o tlv.o -lsodium -levent
|
||||||
|
|
||||||
gen-keypair: gen-keypair.o crypto.o tlv.o
|
gen-keypair: gen-keypair.o crypto.o tlv.o
|
||||||
g++ -o gen-keypair gen-keypair.o crypto.o tlv.o -lsodium
|
g++ -L/usr/local/lib -o gen-keypair gen-keypair.o crypto.o tlv.o -lsodium -levent
|
||||||
|
|||||||
@@ -37,26 +37,6 @@ int main(int argc, char *argv[]) {
|
|||||||
secret_key_file >> secret_key;
|
secret_key_file >> secret_key;
|
||||||
}
|
}
|
||||||
|
|
||||||
int fd = socket(PF_INET6, SOCK_STREAM, 0);
|
CryptoPubServer server(secret_key);
|
||||||
|
|
||||||
int optval = 1;
|
|
||||||
setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof(optval));
|
|
||||||
|
|
||||||
struct sockaddr_in6 server_addr = {0};
|
|
||||||
server_addr.sin6_family = AF_INET6;
|
|
||||||
server_addr.sin6_addr = in6addr_any;
|
|
||||||
server_addr.sin6_port = htons(4990);
|
|
||||||
|
|
||||||
if (bind(fd, (struct sockaddr*) &server_addr, sizeof(server_addr))) {
|
|
||||||
perror("bind");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (listen(fd, 256)) {
|
|
||||||
perror("listen");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
CryptoPubServer server(fd, secret_key);
|
|
||||||
server.Loop();
|
server.Loop();
|
||||||
}
|
}
|
||||||
|
|||||||
97
crypto.cc
97
crypto.cc
@@ -13,15 +13,6 @@
|
|||||||
#include "crypto.h"
|
#include "crypto.h"
|
||||||
|
|
||||||
|
|
||||||
CryptoBase::CryptoBase(const int fd)
|
|
||||||
: fd_(fd) {}
|
|
||||||
|
|
||||||
CryptoBase::~CryptoBase() {
|
|
||||||
if (close(fd_)) {
|
|
||||||
perror("close");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void CryptoBase::GenKey(std::string* key) {
|
void CryptoBase::GenKey(std::string* key) {
|
||||||
unsigned char buf[crypto_secretbox_KEYBYTES];
|
unsigned char buf[crypto_secretbox_KEYBYTES];
|
||||||
randombytes_buf(buf, crypto_secretbox_KEYBYTES);
|
randombytes_buf(buf, crypto_secretbox_KEYBYTES);
|
||||||
@@ -37,72 +28,64 @@ void CryptoBase::GenKeyPair(std::string* secret_key, std::string* public_key) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
CryptoPubServer::CryptoPubServer(const int fd, const std::string secret_key)
|
CryptoPubServer::CryptoPubServer(const std::string secret_key)
|
||||||
: CryptoBase(fd),
|
: secret_key_(secret_key),
|
||||||
secret_key_(secret_key),
|
event_base_(event_base_new()) {
|
||||||
epoll_fd_(epoll_create(256)) {
|
struct sockaddr_in6 server_addr = {0};
|
||||||
epoll_event event = {
|
server_addr.sin6_family = AF_INET6;
|
||||||
.events = EPOLLIN,
|
server_addr.sin6_addr = in6addr_any;
|
||||||
.data = {
|
server_addr.sin6_port = htons(4990);
|
||||||
.ptr = this,
|
|
||||||
},
|
listener_ = evconnlistener_new_bind(event_base_, &CryptoPubServer::OnNewConn, this, LEV_OPT_REUSEABLE|LEV_OPT_CLOSE_ON_FREE, -1, (struct sockaddr*)&server_addr, sizeof(server_addr));
|
||||||
};
|
|
||||||
epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, fd, &event);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CryptoPubServer::~CryptoPubServer() {
|
CryptoPubServer::~CryptoPubServer() {
|
||||||
if (close(epoll_fd_)) {
|
evconnlistener_free(listener_);
|
||||||
perror("close");
|
event_base_free(event_base_);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CryptoPubServer::OnReadable() {
|
void CryptoPubServer::OnNewConn(struct evconnlistener* listener, int client_fd, struct sockaddr* client_addr_, int client_addrlen, void* this__) {
|
||||||
struct sockaddr_in6 client;
|
auto this_ = (CryptoPubServer*)this__;
|
||||||
socklen_t client_len = sizeof(client);
|
|
||||||
auto client_fd = accept(fd_, (struct sockaddr*) &client, &client_len);
|
assert(client_addr_->sa_family == AF_INET6);
|
||||||
if (client_fd == -1) {
|
auto client_addr = (struct sockaddr_in6*)client_addr_;
|
||||||
perror("accept");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
char buf[128];
|
char buf[128];
|
||||||
inet_ntop(AF_INET6, &client.sin6_addr, buf, 128);
|
inet_ntop(AF_INET6, &client_addr->sin6_addr, buf, 128);
|
||||||
std::cerr << "New connection from [" << buf << "]:" << ntohs(client.sin6_port) << std::endl;
|
std::cerr << "New connection from [" << buf << "]:" << ntohs(client_addr->sin6_port) << std::endl;
|
||||||
auto peer = new CryptoPubServerConnection(client_fd, secret_key_);
|
|
||||||
|
|
||||||
epoll_event event = {
|
auto bev = bufferevent_socket_new(this_->event_base_, client_fd, BEV_OPT_CLOSE_ON_FREE);
|
||||||
.events = EPOLLIN,
|
bufferevent_enable(bev, EV_READ);
|
||||||
.data = {
|
bufferevent_disable(bev, EV_WRITE);
|
||||||
.ptr = peer,
|
auto peer = new CryptoPubServerConnection(bev, this_->secret_key_);
|
||||||
},
|
bufferevent_setcb(bev, &CryptoPubServerConnection::OnReadable, NULL, &CryptoPubServerConnection::OnError, peer);
|
||||||
};
|
|
||||||
epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, client_fd, &event);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CryptoPubServer::Loop() {
|
void CryptoPubServer::Loop() {
|
||||||
while (1) {
|
event_base_dispatch(event_base_);
|
||||||
epoll_event events[10];
|
|
||||||
auto num_events = epoll_wait(epoll_fd_, events, 10, -1);
|
|
||||||
for (int i = 0; i < num_events; i++) {
|
|
||||||
if (events[i].events & EPOLLIN) {
|
|
||||||
auto obj = (CryptoBase*) events[i].data.ptr;
|
|
||||||
obj->OnReadable();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
CryptoPubServerConnection::CryptoPubServerConnection(const int fd, const std::string secret_key)
|
CryptoPubServerConnection::CryptoPubServerConnection(struct bufferevent* bev, const std::string secret_key)
|
||||||
: CryptoBase(fd),
|
: bev_(bev),
|
||||||
secret_key_(secret_key),
|
secret_key_(secret_key),
|
||||||
state_(AWAITING_HANDSHAKE) {
|
state_(AWAITING_HANDSHAKE) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void CryptoPubServerConnection::OnReadable() {
|
CryptoPubServerConnection::~CryptoPubServerConnection() {
|
||||||
|
std::cerr << "Connection closed" << std::endl;
|
||||||
|
bufferevent_free(bev_);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CryptoPubServerConnection::OnReadable(struct bufferevent* bev, void* this__) {
|
||||||
|
std::cerr << "OnReadable" << std::endl;
|
||||||
|
auto this_ = (CryptoPubServerConnection*)this__;
|
||||||
char buf[128];
|
char buf[128];
|
||||||
if (read(fd_, buf, 128) == 0) {
|
bufferevent_read(bev, buf, 128);
|
||||||
delete this;
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CryptoPubServerConnection::OnError(struct bufferevent* bev, const short what, void* this__) {
|
||||||
|
std::cerr << "OnError" << std::endl;
|
||||||
|
auto this_ = (CryptoPubServerConnection*)this__;
|
||||||
|
delete this_;
|
||||||
}
|
}
|
||||||
|
|||||||
27
crypto.h
27
crypto.h
@@ -1,26 +1,27 @@
|
|||||||
#include <sys/epoll.h>
|
#include <event2/bufferevent.h>
|
||||||
|
#include <event2/event.h>
|
||||||
|
#include <event2/listener.h>
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
class CryptoBase {
|
class CryptoBase {
|
||||||
public:
|
public:
|
||||||
CryptoBase(const int fd);
|
virtual ~CryptoBase() {};
|
||||||
virtual ~CryptoBase();
|
|
||||||
|
|
||||||
static void GenKey(std::string* key);
|
static void GenKey(std::string* key);
|
||||||
static void GenKeyPair(std::string* secret_key, std::string* public_key);
|
static void GenKeyPair(std::string* secret_key, std::string* public_key);
|
||||||
virtual void OnReadable() = 0;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
const int fd_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class CryptoPubServerConnection : public CryptoBase {
|
class CryptoPubServerConnection : public CryptoBase {
|
||||||
public:
|
public:
|
||||||
CryptoPubServerConnection(const int fd, const std::string secret_key);
|
CryptoPubServerConnection(struct bufferevent* bev, const std::string secret_key);
|
||||||
void OnReadable();
|
~CryptoPubServerConnection();
|
||||||
|
static void OnReadable(struct bufferevent* bev, void* this__);
|
||||||
|
static void OnError(struct bufferevent* bev, const short what, void* this__);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
struct bufferevent* bev_;
|
||||||
|
|
||||||
const std::string secret_key_;
|
const std::string secret_key_;
|
||||||
const std::string ephemeral_secret_key_;
|
const std::string ephemeral_secret_key_;
|
||||||
const std::string client_public_key_;
|
const std::string client_public_key_;
|
||||||
@@ -32,12 +33,14 @@ class CryptoPubServerConnection : public CryptoBase {
|
|||||||
|
|
||||||
class CryptoPubServer : public CryptoBase {
|
class CryptoPubServer : public CryptoBase {
|
||||||
public:
|
public:
|
||||||
CryptoPubServer(const int fd, const std::string secret_key);
|
CryptoPubServer(const std::string secret_key);
|
||||||
~CryptoPubServer();
|
~CryptoPubServer();
|
||||||
void OnReadable();
|
static void OnNewConn(struct evconnlistener* listener, int fd, struct sockaddr* client_addr, int client_addrlen, void* this__);
|
||||||
void Loop();
|
void Loop();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
struct event_base *event_base_;
|
||||||
|
struct evconnlistener *listener_;
|
||||||
|
|
||||||
const std::string secret_key_;
|
const std::string secret_key_;
|
||||||
const int epoll_fd_;
|
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user