17db0aea6SShuo Chen#include "TlsAcceptor.h" 27db0aea6SShuo Chen 37db0aea6SShuo Chen#include "TlsStream.h" 47db0aea6SShuo Chen 57db0aea6SShuo Chen#include <stdio.h> 67db0aea6SShuo Chen#include <sys/socket.h> 77db0aea6SShuo Chen 87db0aea6SShuo ChenTlsAcceptor::TlsAcceptor(TlsConfig* config, const InetAddress& listenAddr) 97db0aea6SShuo Chen : context_(TlsContext::kServer, config), 10cc4062ffSShuo Chen listenSock_(Socket::createTCP(AF_INET)) 117db0aea6SShuo Chen{ 127db0aea6SShuo Chen listenSock_.setReuseAddr(true); 137db0aea6SShuo Chen listenSock_.bindOrDie(listenAddr); 147db0aea6SShuo Chen listenSock_.listenOrDie(); 157db0aea6SShuo Chen} 167db0aea6SShuo Chen 177db0aea6SShuo ChenTlsStreamPtr TlsAcceptor::accept() 187db0aea6SShuo Chen{ 197db0aea6SShuo Chen // FIXME: use accept4 207db0aea6SShuo Chen int sockfd = ::accept(listenSock_.fd(), NULL, NULL); 217db0aea6SShuo Chen if (sockfd >= 0) 227db0aea6SShuo Chen { 237db0aea6SShuo Chen TlsContext context = context_.accept(sockfd); 247db0aea6SShuo Chen if (context.handshake()) 257db0aea6SShuo Chen { 267db0aea6SShuo Chen LOG_ERROR << context.error(); 277db0aea6SShuo Chen return TlsStreamPtr(); 287db0aea6SShuo Chen } 297db0aea6SShuo Chen return TlsStreamPtr(new TlsStream(std::move(context))); 307db0aea6SShuo Chen } 317db0aea6SShuo Chen else 327db0aea6SShuo Chen { 337db0aea6SShuo Chen perror("TlsAcceptor::accept"); 347db0aea6SShuo Chen return TlsStreamPtr(); 357db0aea6SShuo Chen } 367db0aea6SShuo Chen} 377db0aea6SShuo Chen 38