Main loop, epoll handling

This commit is contained in:
Ian Gulliver
2015-02-05 16:36:25 +00:00
parent b3a450226c
commit 3f7b086e24
4 changed files with 150 additions and 7 deletions

View File

@@ -1,9 +1,25 @@
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <unistd.h>
#include <iostream>
#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;
}
}