From c91b64f892b9b8c34305c7eec7a4b06d732ce7d2 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Thu, 5 Feb 2015 20:57:01 +0000 Subject: [PATCH] It's not necessary to use EPOLL_CTL_DEL after close() --- auth-server.cc | 32 ++++++++++++++++++++++++++++++-- crypto.cc | 20 +++++++------------- crypto.h | 10 +++++----- 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/auth-server.cc b/auth-server.cc index dae2f19..c02d965 100644 --- a/auth-server.cc +++ b/auth-server.cc @@ -1,5 +1,7 @@ +#include #include +#include #include #include #include @@ -8,7 +10,33 @@ #include "crypto.h" -int main() { +static const struct option long_options[] = { + {"secret_key_filename", required_argument, NULL, 's'}, +}; + +int main(int argc, char *argv[]) { + std::string secret_key_filename; + { + int option, option_index; + while ((option = getopt_long(argc, argv, "s:", long_options, &option_index)) != -1) { + switch (option) { + case 's': + secret_key_filename = optarg; + break; + } + } + } + + std::string secret_key; + { + std::fstream secret_key_file(secret_key_filename, std::fstream::in); + if (secret_key_file.fail()) { + std::cerr << "Failed to open secret key file" << std::endl; + return 1; + } + secret_key_file >> secret_key; + } + int fd = socket(PF_INET6, SOCK_STREAM, 0); int optval = 1; @@ -29,6 +57,6 @@ int main() { return 1; } - CryptoPubServer server(fd, "abcde"); + CryptoPubServer server(fd, secret_key); server.Loop(); } diff --git a/crypto.cc b/crypto.cc index d5e74ae..f39cb15 100644 --- a/crypto.cc +++ b/crypto.cc @@ -50,19 +50,19 @@ CryptoPubServer::~CryptoPubServer() { } } -int CryptoPubServer::OnReadable() { +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 -1; + return; } 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_); + auto peer = new CryptoPubServerConnection(client_fd, secret_key_); epoll_event event = { .events = EPOLLIN, @@ -71,8 +71,6 @@ int CryptoPubServer::OnReadable() { }, }; epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, client_fd, &event); - - return -1; } void CryptoPubServer::Loop() { @@ -82,26 +80,22 @@ void CryptoPubServer::Loop() { 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]); - } + obj->OnReadable(); } } } } -CryptoPubPeer::CryptoPubPeer(const int fd, const std::string secret_key) +CryptoPubServerConnection::CryptoPubServerConnection(const int fd, const std::string secret_key) : CryptoBase(fd), secret_key_(secret_key) { } -int CryptoPubPeer::OnReadable() { +void CryptoPubServerConnection::OnReadable() { char buf[128]; if (read(fd_, buf, 128) == 0) { - auto fd = fd_; delete this; - return fd; + return; } } diff --git a/crypto.h b/crypto.h index bb29483..447a464 100644 --- a/crypto.h +++ b/crypto.h @@ -9,16 +9,16 @@ class CryptoBase { static void GenKey(std::string* key); static void GenKeyPair(std::string* secret_key, std::string* public_key); - virtual int OnReadable() = 0; + virtual void OnReadable() = 0; protected: const int fd_; }; -class CryptoPubPeer : public CryptoBase { +class CryptoPubServerConnection : public CryptoBase { public: - CryptoPubPeer(const int fd, const std::string secret_key); - int OnReadable(); + CryptoPubServerConnection(const int fd, const std::string secret_key); + void OnReadable(); private: const std::string secret_key_; @@ -29,7 +29,7 @@ class CryptoPubServer : public CryptoBase { public: CryptoPubServer(const int fd, const std::string secret_key); ~CryptoPubServer(); - int OnReadable(); + void OnReadable(); void Loop(); private: