From 88c3c69e524a7277c596a7a0d806586b057d2f61 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Sat, 7 Feb 2015 16:44:42 +0100 Subject: [PATCH] Switch from epoll to libevent, to add OS X support. --- Makefile | 10 ++--- auth-server.cc | 22 +---------- crypto.cc | 103 +++++++++++++++++++++---------------------------- crypto.h | 27 +++++++------ 4 files changed, 64 insertions(+), 98 deletions(-) diff --git a/Makefile b/Makefile index 1ceacb7..856b2df 100644 --- a/Makefile +++ b/Makefile @@ -1,16 +1,16 @@ all: auth-client auth-server gen-key gen-keypair %.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 - 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 - 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 - 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 - 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 diff --git a/auth-server.cc b/auth-server.cc index c02d965..8bdc090 100644 --- a/auth-server.cc +++ b/auth-server.cc @@ -37,26 +37,6 @@ int main(int argc, char *argv[]) { secret_key_file >> secret_key; } - int fd = socket(PF_INET6, SOCK_STREAM, 0); - - 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); + CryptoPubServer server(secret_key); server.Loop(); } diff --git a/crypto.cc b/crypto.cc index 6e14baf..a7d9736 100644 --- a/crypto.cc +++ b/crypto.cc @@ -13,15 +13,6 @@ #include "crypto.h" -CryptoBase::CryptoBase(const int fd) - : fd_(fd) {} - -CryptoBase::~CryptoBase() { - if (close(fd_)) { - perror("close"); - } -} - void CryptoBase::GenKey(std::string* key) { unsigned char 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) - : CryptoBase(fd), - secret_key_(secret_key), - epoll_fd_(epoll_create(256)) { - epoll_event event = { - .events = EPOLLIN, - .data = { - .ptr = this, - }, - }; - epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, fd, &event); +CryptoPubServer::CryptoPubServer(const std::string secret_key) + : secret_key_(secret_key), + event_base_(event_base_new()) { + struct sockaddr_in6 server_addr = {0}; + server_addr.sin6_family = AF_INET6; + server_addr.sin6_addr = in6addr_any; + server_addr.sin6_port = htons(4990); + + 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)); } CryptoPubServer::~CryptoPubServer() { - if (close(epoll_fd_)) { - perror("close"); - } + evconnlistener_free(listener_); + event_base_free(event_base_); } -void CryptoPubServer::OnReadable() { - struct sockaddr_in6 client; - socklen_t client_len = sizeof(client); - auto client_fd = accept(fd_, (struct sockaddr*) &client, &client_len); - if (client_fd == -1) { - perror("accept"); - return; - } +void CryptoPubServer::OnNewConn(struct evconnlistener* listener, int client_fd, struct sockaddr* client_addr_, int client_addrlen, void* this__) { + auto this_ = (CryptoPubServer*)this__; + + assert(client_addr_->sa_family == AF_INET6); + auto client_addr = (struct sockaddr_in6*)client_addr_; char buf[128]; - inet_ntop(AF_INET6, &client.sin6_addr, buf, 128); - std::cerr << "New connection from [" << buf << "]:" << ntohs(client.sin6_port) << std::endl; - auto peer = new CryptoPubServerConnection(client_fd, secret_key_); - - epoll_event event = { - .events = EPOLLIN, - .data = { - .ptr = peer, - }, - }; - epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, client_fd, &event); + inet_ntop(AF_INET6, &client_addr->sin6_addr, buf, 128); + std::cerr << "New connection from [" << buf << "]:" << ntohs(client_addr->sin6_port) << std::endl; + + auto bev = bufferevent_socket_new(this_->event_base_, client_fd, BEV_OPT_CLOSE_ON_FREE); + bufferevent_enable(bev, EV_READ); + bufferevent_disable(bev, EV_WRITE); + auto peer = new CryptoPubServerConnection(bev, this_->secret_key_); + bufferevent_setcb(bev, &CryptoPubServerConnection::OnReadable, NULL, &CryptoPubServerConnection::OnError, peer); } void CryptoPubServer::Loop() { - while (1) { - 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(); - } - } - } + event_base_dispatch(event_base_); } -CryptoPubServerConnection::CryptoPubServerConnection(const int fd, const std::string secret_key) - : CryptoBase(fd), +CryptoPubServerConnection::CryptoPubServerConnection(struct bufferevent* bev, const std::string secret_key) + : bev_(bev), secret_key_(secret_key), state_(AWAITING_HANDSHAKE) { } -void CryptoPubServerConnection::OnReadable() { - char buf[128]; - if (read(fd_, buf, 128) == 0) { - delete this; - return; - } +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]; + bufferevent_read(bev, buf, 128); +} + +void CryptoPubServerConnection::OnError(struct bufferevent* bev, const short what, void* this__) { + std::cerr << "OnError" << std::endl; + auto this_ = (CryptoPubServerConnection*)this__; + delete this_; } diff --git a/crypto.h b/crypto.h index e3a50a4..69d38cd 100644 --- a/crypto.h +++ b/crypto.h @@ -1,26 +1,27 @@ -#include +#include +#include +#include #include class CryptoBase { public: - CryptoBase(const int fd); - virtual ~CryptoBase(); + virtual ~CryptoBase() {}; static void GenKey(std::string* key); static void GenKeyPair(std::string* secret_key, std::string* public_key); - virtual void OnReadable() = 0; - - protected: - const int fd_; }; class CryptoPubServerConnection : public CryptoBase { public: - CryptoPubServerConnection(const int fd, const std::string secret_key); - void OnReadable(); + CryptoPubServerConnection(struct bufferevent* bev, const std::string secret_key); + ~CryptoPubServerConnection(); + static void OnReadable(struct bufferevent* bev, void* this__); + static void OnError(struct bufferevent* bev, const short what, void* this__); private: + struct bufferevent* bev_; + const std::string secret_key_; const std::string ephemeral_secret_key_; const std::string client_public_key_; @@ -32,12 +33,14 @@ class CryptoPubServerConnection : public CryptoBase { class CryptoPubServer : public CryptoBase { public: - CryptoPubServer(const int fd, const std::string secret_key); + CryptoPubServer(const std::string secret_key); ~CryptoPubServer(); - void OnReadable(); + static void OnNewConn(struct evconnlistener* listener, int fd, struct sockaddr* client_addr, int client_addrlen, void* this__); void Loop(); private: + struct event_base *event_base_; + struct evconnlistener *listener_; + const std::string secret_key_; - const int epoll_fd_; };