1#include "TlsAcceptor.h" 2 3#include "TlsStream.h" 4 5#include <stdio.h> 6#include <sys/socket.h> 7 8TlsAcceptor::TlsAcceptor(TlsConfig* config, const InetAddress& listenAddr) 9 : context_(TlsContext::kServer, config), 10 listenSock_(Socket::createTCP(AF_INET)) 11{ 12 listenSock_.setReuseAddr(true); 13 listenSock_.bindOrDie(listenAddr); 14 listenSock_.listenOrDie(); 15} 16 17TlsStreamPtr TlsAcceptor::accept() 18{ 19 // FIXME: use accept4 20 int sockfd = ::accept(listenSock_.fd(), NULL, NULL); 21 if (sockfd >= 0) 22 { 23 TlsContext context = context_.accept(sockfd); 24 if (context.handshake()) 25 { 26 LOG_ERROR << context.error(); 27 return TlsStreamPtr(); 28 } 29 return TlsStreamPtr(new TlsStream(std::move(context))); 30 } 31 else 32 { 33 perror("TlsAcceptor::accept"); 34 return TlsStreamPtr(); 35 } 36} 37 38