2015-02-05 16:36:25 +00:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
2015-02-05 12:55:48 +00:00
|
|
|
#include "crypto.h"
|
|
|
|
|
|
|
|
|
|
#include "nacl/build/instance1/include/amd64/crypto_box.h"
|
2015-02-05 13:02:42 +00:00
|
|
|
#include "nacl/build/instance1/include/amd64/crypto_secretbox.h"
|
|
|
|
|
#include "nacl/build/instance1/include/amd64/randombytes.h"
|
2015-02-05 12:55:48 +00:00
|
|
|
|
2015-02-05 16:36:25 +00:00
|
|
|
CryptoBase::CryptoBase(const int fd)
|
|
|
|
|
: fd_(fd) {}
|
|
|
|
|
|
|
|
|
|
CryptoBase::~CryptoBase() {
|
|
|
|
|
if (close(fd_)) {
|
|
|
|
|
perror("close");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-05 12:55:48 +00:00
|
|
|
std::string CryptoBase::BinToHex(const std::string& bin) {
|
|
|
|
|
static const char *hex = "0123456789ABCDEF";
|
|
|
|
|
std::string ret;
|
|
|
|
|
ret.reserve(bin.length() * 2);
|
|
|
|
|
for (size_t i = 0; i < bin.length(); i++) {
|
|
|
|
|
const char c = bin[i];
|
|
|
|
|
ret.push_back(hex[(c & 0xf0) >> 4]);
|
|
|
|
|
ret.push_back(hex[c & 0x0f]);
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-05 13:02:42 +00:00
|
|
|
void CryptoBase::GenKey(std::string* key) {
|
|
|
|
|
char buf[crypto_secretbox_KEYBYTES];
|
|
|
|
|
randombytes((unsigned char *)buf, crypto_secretbox_KEYBYTES);
|
|
|
|
|
*key = buf;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CryptoBase::GenKeyPair(std::string* secret_key, std::string* public_key) {
|
|
|
|
|
*public_key = crypto_box_keypair(secret_key);
|
2015-02-05 12:55:48 +00:00
|
|
|
}
|
2015-02-05 16:36:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|