From 3f7b086e249e394427b896c93ae0a6749b755f41 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Thu, 5 Feb 2015 16:36:25 +0000 Subject: [PATCH] Main loop, epoll handling --- Makefile | 2 +- auth-server.cc | 32 ++++++++++++++---- crypto.cc | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++ crypto.h | 31 +++++++++++++++++ 4 files changed, 150 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index c639b17..5020223 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ all: auth-server gen-key gen-keypair %.o: %.cc - g++ -c -o $@ $< + g++ -std=c++11 -c -o $@ $< auth-server: auth-server.o crypto.o g++ -o auth-server auth-server.o crypto.o nacl/build/instance1/lib/amd64/randombytes.o nacl/build/instance1/lib/amd64/libnacl.a diff --git a/auth-server.cc b/auth-server.cc index 4ffa1cb..5453578 100644 --- a/auth-server.cc +++ b/auth-server.cc @@ -1,14 +1,34 @@ #include -#include "nacl/build/instance1/include/amd64/crypto_box.h" +#include +#include +#include +#include +#include + +#include "crypto.h" int main() { - std::string pk; - std::string sk; + int fd = socket(PF_INET6, SOCK_STREAM, 0); - pk = crypto_box_keypair(&sk); + int optval = 1; + setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof(optval)); - std::cout << pk.size() << std::endl; + struct sockaddr_in6 server_addr = {0}; + server_addr.sin6_family = AF_INET6; + server_addr.sin6_addr = in6addr_any; + server_addr.sin6_port = htons(4990); - return 0; + 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, "abcde"); + server.Loop(); } diff --git a/crypto.cc b/crypto.cc index a0dc488..dabaecd 100644 --- a/crypto.cc +++ b/crypto.cc @@ -1,9 +1,25 @@ +#include +#include +#include +#include + +#include + #include "crypto.h" #include "nacl/build/instance1/include/amd64/crypto_box.h" #include "nacl/build/instance1/include/amd64/crypto_secretbox.h" #include "nacl/build/instance1/include/amd64/randombytes.h" +CryptoBase::CryptoBase(const int fd) + : fd_(fd) {} + +CryptoBase::~CryptoBase() { + if (close(fd_)) { + perror("close"); + } +} + std::string CryptoBase::BinToHex(const std::string& bin) { static const char *hex = "0123456789ABCDEF"; std::string ret; @@ -25,3 +41,79 @@ void CryptoBase::GenKey(std::string* key) { void CryptoBase::GenKeyPair(std::string* secret_key, std::string* public_key) { *public_key = crypto_box_keypair(secret_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() { + if (close(epoll_fd_)) { + perror("close"); + } +} + +int 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 -1; + } + + 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 CryptoPubPeer(client_fd, secret_key_); + + epoll_event event = { + .events = EPOLLIN, + .data = { + .ptr = peer, + }, + }; + epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, client_fd, &event); + + return -1; +} + +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; + auto fd = obj->OnReadable(); + if (fd >= 0) { + epoll_ctl(epoll_fd_, EPOLL_CTL_DEL, fd, &events[i]); + } + } + } + } +} + + +CryptoPubPeer::CryptoPubPeer(const int fd, const std::string secret_key) + : CryptoBase(fd), + secret_key_(secret_key) { +} + +int CryptoPubPeer::OnReadable() { + char buf[128]; + if (read(fd_, buf, 128) == 0) { + auto fd = fd_; + delete this; + return fd; + } +} diff --git a/crypto.h b/crypto.h index 6df3b98..9e11487 100644 --- a/crypto.h +++ b/crypto.h @@ -1,8 +1,39 @@ +#include + #include class CryptoBase { public: + CryptoBase(const int fd); + virtual ~CryptoBase(); + static std::string BinToHex(const std::string& bin); static void GenKey(std::string* key); static void GenKeyPair(std::string* secret_key, std::string* public_key); + virtual int OnReadable() = 0; + + protected: + const int fd_; +}; + +class CryptoPubPeer : public CryptoBase { + public: + CryptoPubPeer(const int fd, const std::string secret_key); + int OnReadable(); + + private: + const std::string secret_key_; + const std::string ephemeral_secret_key_; +}; + +class CryptoPubServer : public CryptoBase { + public: + CryptoPubServer(const int fd, const std::string secret_key); + ~CryptoPubServer(); + int OnReadable(); + void Loop(); + + private: + const std::string secret_key_; + const int epoll_fd_; };