Main loop, epoll handling
This commit is contained in:
2
Makefile
2
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
|
||||
|
||||
@@ -1,14 +1,34 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "nacl/build/instance1/include/amd64/crypto_box.h"
|
||||
#include <netinet/in.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
#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();
|
||||
}
|
||||
|
||||
92
crypto.cc
92
crypto.cc
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
31
crypto.h
31
crypto.h
@@ -1,8 +1,39 @@
|
||||
#include <sys/epoll.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
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_;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user