9 static const char rcsid[] __attribute__((used)) =
"$Id: server.cpp 1653 2016-02-28 19:54:59Z sella $";
20 #include <condition_variable>
24 #include "sella/util/Log.h"
25 #include "sella/util/String.h"
26 #include "sella/util/CommandLine.h"
27 #include "sella/net/IPAddressPort.h"
28 #include "sella/net/SocketMux.h"
29 #include "sella/net/SecureTCPSocket.h"
38 using namespace sella::server;
39 using namespace sella::util;
40 using namespace sella::net;
42 bool init_config(
int argc,
char **argv);
43 bool init_signals(
void);
44 void sighandler(
int sig);
46 int main(
int argc,
char **argv) {
48 SET_IDENTIFIER(
"server");
49 SET_OPTION(Log::WithStderrOption);
51 init_config(argc, argv);
54 INFO(
"Using %s v%s", libutilxx__package(), libutilxx__version());
57 INFO(
"Setting up server...");
59 SecureTCPSocket::shared server = SecureTCPSocket::shared_ptr(AF_INET);
61 server->setCertificateFile(
"../contrib/cert.pem");
62 server->setPrivateKeyFile(
"../contrib/key.pem");
64 server->setProtocol(SecureTCPSocket::SSLv3,
false);
65 server->setCiphers(SecureTCPSocket::PerformanceCiphers);
66 server->setPreferServerCiphers();
68 server->setSoReuseAddr();
69 server->setBlock(
false);
70 server->bind(
"127.0.0.1", 9876);
71 server->setSoReuseAddr();
76 mux.insertRead(server);
78 INFO(
"Using %s", server->getSSLLibraryVersion());
79 INFO(
"Waiting for a connection on %s", server->getLocalIPAddressPort().c_str());
83 if ((c = mux.rselect()) > 0) {
84 auto &pending = mux.getReadList();
86 for (
auto p = pending.begin(); p != pending.end(); ++p) {
88 SecureTCPSocket::shared client = std::static_pointer_cast<
SecureTCPSocket>(server->accept());
90 INFO(
"Established connection %s using protocol %s with cipher %s", client->getRemoteIPAddressPort().c_str(), client->getActiveProtocol(), client->getActiveCipher());
92 mux.insertRead(client);
94 if ((*p)->isUsable()) {
96 if ((n = (*p)->recv(buf)) > 0) {
97 INFO(
"Recv: %s", buf.c_str());
99 if (rand() % 5 == 0) {
103 (*p)->send(
"Hail to the king, baby.");
105 (*p)->send(
"I got it, I got it! I know your damn words, alright?.");
107 (*p)->send(
"Good. Bad. I'm the guy with the gun.");
109 (*p)->send(
"Alright you primitive screwheads, listen up!");
111 (*p)->send(
"It's a trick, get an axe.");
117 if (!(*p)->isUsable()) {
118 INFO(
"Client disconnected");
126 }
catch (SocketException &e) {
127 INFO(
"Exception: %s", e.what());
132 INFO(
"Clean shutdwn");
137 bool init_config(
int argc,
char **argv) {
141 cmdln.add(
'd',
"debug",
"set debug level to INT",
"INT", debug, 0, 9);
143 cmdln.add(
'h',
"help",
"display this help screen");
144 cmdln.add(
'v',
"version",
"display version information");
146 std::map<std::string, std::string> conf = cmdln.parse(stdin);
148 if (conf.find(
"help") != conf.end()) {
149 printf(
"%s v%s\n", libutilxx__package(), libutilxx__version());
150 cmdln.usage(stdout, EXIT_SUCCESS);
151 }
else if (conf.find(
"version") != conf.end()) {
152 printf(
"%s v%s\n", libutilxx__package(), libutilxx__version());
157 if (conf.find(
"debug") != conf.end()) {
158 debug = atoi(conf[
"debug"].c_str());
163 SET_LOGMASK(LOG_UPTO(LOG_DEBUG));
166 SET_TRACE(PACKAGE
"-detail");
179 bool init_signals(
void) {
180 struct sigaction act, old;
182 act.sa_handler = sighandler;
183 sigemptyset(&act.sa_mask);
184 act.sa_flags = SA_RESTART;
186 if (sigaction(SIGINT, NULL, &old) < 0) {
187 fprintf(stderr,
"Failed to retrieve signal handler for SIGINT.\n");
188 }
else if (old.sa_handler == SIG_DFL) {
189 if (sigaction(SIGINT, &act, NULL) < 0) {
190 fprintf(stderr,
"Failed to install signal handler for SIGINT.\n");
194 if (sigaction(SIGTERM, NULL, &old) < 0) {
195 fprintf(stderr,
"Failed to retrieve signal handler for SIGTERM.\n");
196 }
else if (old.sa_handler == SIG_DFL) {
197 if (sigaction(SIGTERM, &act, NULL) < 0) {
198 fprintf(stderr,
"Failed to install signal handler for SIGTERM.\n");
202 if (sigaction(SIGHUP, NULL, &old) < 0) {
203 fprintf(stderr,
"Failed to retrieve signal handler for SIGHUP.\n");
204 }
else if (old.sa_handler == SIG_DFL) {
205 if (sigaction(SIGHUP, &act, NULL) < 0) {
206 fprintf(stderr,
"Failed to install signal handler for SIGHUP.\n");
210 if (sigaction(SIGUSR1, NULL, &old) < 0) {
211 fprintf(stderr,
"Failed to retrieve signal handler for SIGUSR1.\n");
212 }
else if (old.sa_handler == SIG_DFL) {
213 if (sigaction(SIGUSR1, &act, NULL) < 0) {
214 fprintf(stderr,
"Failed to install signal handler for SIGUSR1.\n");
218 if (sigaction(SIGUSR2, NULL, &old) < 0) {
219 fprintf(stderr,
"Failed to retrieve signal handler for SIGUSR2.\n");
220 }
else if (old.sa_handler == SIG_DFL) {
221 if (sigaction(SIGUSR2, &act, NULL) < 0) {
222 fprintf(stderr,
"Failed to install signal handler for SIGUSR2.\n");
226 if (sigaction(SIGPIPE, NULL, &old) < 0) {
227 fprintf(stderr,
"Failed to retrieve signal handler for SIGPIPE.\n");
228 }
else if (old.sa_handler == SIG_DFL) {
229 if (sigaction(SIGPIPE, &act, NULL) < 0) {
230 fprintf(stderr,
"Failed to install signal handler for SIGPIPE.\n");
237 void sighandler(
int sig) {