9 static const char rcsid[] __attribute__((used)) =
"$Id: client.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::secure;
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(
"secure");
49 SET_OPTION(Log::WithStderrOption);
51 init_config(argc, argv);
54 INFO(
"Using %s v%s", libutilxx__package(), libutilxx__version());
57 INFO(
"Connecting to server...");
59 SecureTCPSocket::shared client = SecureTCPSocket::shared_ptr(AF_INET,
"127.0.0.1", 9876);
60 client->setBlock(
false);
62 INFO(
"Using %s", client->getSSLLibraryVersion());
63 INFO(
"Established connection to %s with protocol %s and cipher %s", client->getRemoteIPAddressPort().c_str(), client->getActiveProtocol(), client->getActiveCipher());
67 mux.insertRead(client);
69 while (running && client->isUsable()) {
70 std::string buf = su::String::random(64);
72 INFO(
"Send: %s", buf.c_str());
75 if (mux.rselect(750000) > 0) {
79 if ((n = client->recv(buf)) > 0) {
80 INFO(
"Recv: %s", buf.c_str());
84 }
catch (SocketException &e) {
85 INFO(
"Exception: %s", e.what());
90 INFO(
"Clean shutdown");
95 bool init_config(
int argc,
char **argv) {
99 cmdln.add(
'd',
"debug",
"set debug level to INT",
"INT", debug, 0, 9);
101 cmdln.add(
'h',
"help",
"display this help screen");
102 cmdln.add(
'v',
"version",
"display version information");
104 std::map<std::string, std::string> conf = cmdln.parse(stdin);
106 if (conf.find(
"help") != conf.end()) {
107 printf(
"%s v%s\n", libutilxx__package(), libutilxx__version());
108 cmdln.usage(stdout, EXIT_SUCCESS);
109 }
else if (conf.find(
"version") != conf.end()) {
110 printf(
"%s v%s\n", libutilxx__package(), libutilxx__version());
115 if (conf.find(
"debug") != conf.end()) {
116 debug = atoi(conf[
"debug"].c_str());
121 SET_LOGMASK(LOG_UPTO(LOG_DEBUG));
124 SET_TRACE(PACKAGE
"-detail");
137 bool init_signals(
void) {
138 struct sigaction act, old;
140 act.sa_handler = sighandler;
141 sigemptyset(&act.sa_mask);
142 act.sa_flags = SA_RESTART;
144 if (sigaction(SIGINT, NULL, &old) < 0) {
145 fprintf(stderr,
"Failed to retrieve signal handler for SIGINT.\n");
146 }
else if (old.sa_handler == SIG_DFL) {
147 if (sigaction(SIGINT, &act, NULL) < 0) {
148 fprintf(stderr,
"Failed to install signal handler for SIGINT.\n");
152 if (sigaction(SIGTERM, NULL, &old) < 0) {
153 fprintf(stderr,
"Failed to retrieve signal handler for SIGTERM.\n");
154 }
else if (old.sa_handler == SIG_DFL) {
155 if (sigaction(SIGTERM, &act, NULL) < 0) {
156 fprintf(stderr,
"Failed to install signal handler for SIGTERM.\n");
160 if (sigaction(SIGHUP, NULL, &old) < 0) {
161 fprintf(stderr,
"Failed to retrieve signal handler for SIGHUP.\n");
162 }
else if (old.sa_handler == SIG_DFL) {
163 if (sigaction(SIGHUP, &act, NULL) < 0) {
164 fprintf(stderr,
"Failed to install signal handler for SIGHUP.\n");
168 if (sigaction(SIGUSR1, NULL, &old) < 0) {
169 fprintf(stderr,
"Failed to retrieve signal handler for SIGUSR1.\n");
170 }
else if (old.sa_handler == SIG_DFL) {
171 if (sigaction(SIGUSR1, &act, NULL) < 0) {
172 fprintf(stderr,
"Failed to install signal handler for SIGUSR1.\n");
176 if (sigaction(SIGUSR2, NULL, &old) < 0) {
177 fprintf(stderr,
"Failed to retrieve signal handler for SIGUSR2.\n");
178 }
else if (old.sa_handler == SIG_DFL) {
179 if (sigaction(SIGUSR2, &act, NULL) < 0) {
180 fprintf(stderr,
"Failed to install signal handler for SIGUSR2.\n");
184 if (sigaction(SIGPIPE, NULL, &old) < 0) {
185 fprintf(stderr,
"Failed to retrieve signal handler for SIGPIPE.\n");
186 }
else if (old.sa_handler == SIG_DFL) {
187 if (sigaction(SIGPIPE, &act, NULL) < 0) {
188 fprintf(stderr,
"Failed to install signal handler for SIGPIPE.\n");
195 void sighandler(
int sig) {