svn cleanup
[anytun.git] / Sockets / tests / echoserver.cpp
blob45cb6c2e316c0f6e975f288c672cf42755f919cb
1 /**
2 ** \file echoserver.cpp
3 ** \date 2006-10-02
4 ** \author grymse@alhem.net
5 **/
6 /*
7 Copyright (C) 2006 Anders Hedstrom
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #ifdef _WIN32
24 #pragma warning(disable:4786)
25 #endif
26 #include <StdoutLog.h>
27 #include <ListenSocket.h>
28 #include <SocketHandler.h>
29 #include <TcpSocket.h>
30 #ifndef _WIN32
31 #include <signal.h>
32 #endif
33 #include <HttpDebugSocket.h>
36 #ifdef ENABLE_DETACH
37 static bool g_b_detach = false;
38 #endif
39 #ifdef HAVE_OPENSSL
40 static bool g_b_ssl = false;
41 #endif
42 static std::string gFilename = "server.pem";
43 static std::string gPw;
44 static bool quit = false;
45 static bool g_b_http = false;
46 static bool g_b_nobuf = false;
47 #ifdef ENABLE_DETACH
48 static bool g_b_detach2 = false;
49 #endif
51 #define DEB(x)
54 class MySocket : public TcpSocket
56 public:
57 MySocket(ISocketHandler& h) : TcpSocket(h) {
58 SetLineProtocol();
59 #ifdef HAVE_OPENSSL
60 if (g_b_ssl)
61 EnableSSL();
62 #endif
63 if (g_b_nobuf)
64 DisableInputBuffer();
66 ~MySocket() {
69 void OnAccept() {
70 #ifdef ENABLE_DETACH
71 if (g_b_detach)
73 if (!Detach())
74 fprintf(stderr, "\nDetach failed\n");
76 #endif
79 void OnRawData(const char *buf,size_t len) {
80 if (g_b_nobuf)
82 SendBuf(buf, len);
86 void OnLine(const std::string& line) {
87 #ifdef ENABLE_DETACH
88 if (g_b_detach2 && !IsDetach())
90 m_line = line;
91 if (!Detach())
93 fprintf(stderr, "\nDetach failed\n");
95 return;
97 DEB(printf("fd %d OnLine; %s\n", GetSocket(), Handler().IsSlave() ? "slave" : "master");)
98 #endif
99 Send(line + "\n");
102 void OnDetached() {
103 DEB(printf("fd %d OnDetached; %s\n", GetSocket(), Handler().IsSlave() ? "slave" : "master");)
104 // fprintf(stderr, "-");
105 // fflush(stderr);
106 #ifdef ENABLE_DETACH
107 if (g_b_detach2)
109 Send(m_line + "\n");
111 #endif
114 #ifdef HAVE_OPENSSL
115 void InitSSLServer() {
116 InitializeContext("echoserver", gFilename, gPw, SSLv23_method());
118 #endif
120 private:
121 std::string m_line;
125 #ifndef _WIN32
126 void sigint(int)
128 quit = true;
130 #endif
132 int main(int argc,char *argv[])
134 #ifndef _WIN32
135 signal(SIGPIPE, SIG_IGN);
136 signal(SIGINT, sigint);
137 #endif
138 port_t port = 2222;
139 bool enableLog = false;
140 int queue = 20;
141 for (int i = 1; i < argc; i++)
143 #ifdef ENABLE_DETACH
144 if (!strcmp(argv[i], "-detach"))
145 g_b_detach = true;
146 if (!strcmp(argv[i], "-detach2"))
147 g_b_detach2 = true;
148 #endif
149 #ifdef HAVE_OPENSSL
150 if (!strcmp(argv[i], "-ssl"))
151 g_b_ssl = true;
152 if (!strcmp(argv[i], "-file") && i < argc - 1)
153 gFilename = argv[++i];
154 #endif
155 if (!strcmp(argv[i], "-port") && i < argc - 1)
156 port = atoi(argv[++i]);
157 if (!strcmp(argv[i], "-pw") && i < argc - 1)
158 gPw = argv[++i];
159 if (!strcmp(argv[i], "-log"))
160 enableLog = true;
161 if (!strcmp(argv[i], "-queue") && i < argc - 1)
162 queue = atoi(argv[++i]);
163 if (!strcmp(argv[i], "-http"))
164 g_b_http = true;
165 if (!strcmp(argv[i], "-nobuf"))
166 g_b_nobuf = true;
167 if (!strcmp(argv[i], "-h"))
169 printf("Usage: %s [ options ] [-ssl]\n", *argv);
170 printf(" -port nn listen on port nn\n");
171 #ifdef ENABLE_DETACH
172 printf(" -detach detach each socket on accept\n");
173 printf(" -detach2 detach when line received\n");
174 #endif
175 #ifdef HAVE_OPENSSL
176 printf(" -ssl run as ssl server, .pem file needed\n");
177 printf(" -file xx .pem filename, default is \"server.pem\"\n");
178 #endif
179 printf(" -pw xx private key password\n");
180 printf(" -log enable sockethandler debug log\n");
181 printf(" -queue nn listen queue size (default 20)\n");
182 printf(" -http enable http server with dummy response\n");
183 printf(" -nobuf echo raw data\n");
184 exit(-1);
187 if (g_b_http && g_b_nobuf)
189 printf("Nobuf and Http does not work together\n");
190 exit(-1);
192 StdoutLog *log = enableLog ? new StdoutLog() : NULL;
193 SocketHandler h(log);
194 ListenSocket<MySocket> l(h);
195 ListenSocket<HttpDebugSocket> l2(h);
196 if (!g_b_http)
198 if (l.Bind(port, queue))
200 fprintf(stderr, "Bind to port %d failed\n", port);
201 return -1;
203 fprintf(stderr, "Listening on port %d\n", port);
204 h.Add(&l);
206 else
208 printf("Enable HttpDebugSocket\n");
209 if (l2.Bind(port, queue))
211 fprintf(stderr, "Bind to port %d failed\n", port);
212 return -1;
214 fprintf(stderr, "Listening on port %d\n", port);
215 h.Add(&l2);
217 #ifdef ENABLE_DETACH
218 if (g_b_detach)
219 fprintf(stderr, "Will detach each incoming socket\n");
220 #endif
221 #ifdef HAVE_OPENSSL
222 if (g_b_ssl)
223 fprintf(stderr, "Using SSL\n");
224 #endif
225 while (!quit)
227 h.Select(1, 0);
229 fprintf(stderr, "\nExiting...\n");
230 if (log)
232 // delete log;
234 return 0;