Fix compilation with old g++ 3.3.5 and debian-sarge.
[wvstreams.git] / ipstreams / wvstreamsdebuggerserver.cc
blobb4c360dbe06bb90305474e875403ed80f851ecfb
1 /*
2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
4 *
5 */
6 #include "wvstreamsdebuggerserver.h"
7 #include "wvunixsocket.h"
8 #include "wvtcplistener.h"
9 #include "wvunixlistener.h"
11 void WvStreamsDebuggerServer::Connection::choose_salt()
13 const int salt_size = 8;
14 const int salt_alphabet_size = 26+26+10;
15 const char salt_chars[salt_alphabet_size+1] =
16 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
18 salt.setsize(salt_size+1);
19 for (int i=0; i<salt_size; ++i)
20 salt.edit()[i] = salt_chars[rand() % salt_alphabet_size];
21 salt.edit()[salt_size] = '\0';
25 WvStreamsDebuggerServer::Connection::Connection(IWvStream *s) :
26 WvStreamClone(s)
31 void WvStreamsDebuggerServer::Connection::result_cb(WvStringParm,
32 WvStringList &results)
34 send("-", results);
38 void WvStreamsDebuggerServer::Connection::send(WvStringParm code,
39 WvStringParm result)
41 WvStringList results;
42 results.append(result);
43 send(code, results);
47 void WvStreamsDebuggerServer::Connection::send(WvStringParm code,
48 WvStringList &results)
50 print("%s %s\n", wvtcl_escape(code), wvtcl_encode(results));
54 WvStreamsDebuggerServer::WvStreamsDebuggerServer(const WvUnixAddr &unix_addr,
55 AuthCallback _auth_cb,
56 const WvIPPortAddr &tcp_addr) :
57 log("WvStreamsDebuggerServer", WvLog::Debug3),
58 unix_listener(NULL),
59 tcp_listener(NULL),
60 auth_cb(_auth_cb)
62 WvIStreamList::globallist.append(&streams, false);
64 #ifndef _WIN32
65 if (true)
67 unix_listener = new WvUnixListener(unix_addr, 0700);
68 unix_listener->set_wsname("wsd listener on %s", unix_addr);
69 unix_listener->onaccept(
70 wv::bind(&WvStreamsDebuggerServer::unix_listener_cb, this, _1));
71 unix_listener->setclosecallback(
72 wv::bind(&WvStreamsDebuggerServer::unix_listener_close_cb, this));
73 streams.append(unix_listener, true);
74 log("Listening on %s\n", unix_addr);
76 #endif
78 if (tcp_addr != WvIPPortAddr())
80 tcp_listener = new WvTCPListener(tcp_addr);
81 tcp_listener->set_wsname("wsd listener on %s", tcp_addr);
82 tcp_listener->onaccept(
83 wv::bind(&WvStreamsDebuggerServer::tcp_listener_cb, this, _1));
84 tcp_listener->setclosecallback(
85 wv::bind(&WvStreamsDebuggerServer::tcp_listener_close_cb, this));
86 streams.append(tcp_listener, true);
87 log("Listening on %s\n", tcp_addr);
92 WvStreamsDebuggerServer::~WvStreamsDebuggerServer()
94 WvIStreamList::globallist.unlink(&streams);
98 #ifndef _WIN32
99 void WvStreamsDebuggerServer::unix_listener_cb(IWvStream *unix_conn)
101 log("Accepted connection from %s\n", *unix_conn->src());
102 Connection *conn = new Connection(unix_conn);
103 conn->setcallback(wv::bind(&WvStreamsDebuggerServer::ready_cb, this,
104 wv::ref(*conn)));
105 streams.append(conn, true);
109 void WvStreamsDebuggerServer::unix_listener_close_cb()
111 log("Listener on %s closing\n", *unix_listener->src());
113 #endif
115 void WvStreamsDebuggerServer::tcp_listener_cb(IWvStream *tcp_conn)
117 log("Accepted connection from %s\n", *tcp_conn->src());
118 Connection *conn = new Connection(tcp_conn);
119 conn->setcallback(wv::bind(&WvStreamsDebuggerServer::ready_cb, this,
120 wv::ref(*conn)));
121 streams.append(conn, true);
125 void WvStreamsDebuggerServer::tcp_listener_close_cb()
127 log("Listener on %s closing\n", *tcp_listener->src());
131 void WvStreamsDebuggerServer::auth_request_cb(Connection &s)
133 s.choose_salt();
134 s.send("AUTH", s.salt);
136 s.setcallback(wv::bind(&WvStreamsDebuggerServer::auth_response_cb, this,
137 wv::ref(s)));
141 void WvStreamsDebuggerServer::auth_response_cb(Connection &s)
143 const char *line = s.getline();
144 if (line == NULL)
145 return;
147 WvStringList args;
148 wvtcl_decode(args, line);
150 WvString username = args.popstr();
151 WvString encoded_salted_password = args.popstr();
153 if (!auth_cb || !username || !encoded_salted_password
154 || !auth_cb(username, s.salt, encoded_salted_password))
156 s.send("ERROR", "Authentication failure");
157 s.setcallback(wv::bind(&WvStreamsDebuggerServer::auth_request_cb,
158 this, wv::ref(s)));
160 else
162 s.send("OK", "Authenticated");
163 s.setcallback(wv::bind(&WvStreamsDebuggerServer::ready_cb, this,
164 wv::ref(s)));
169 void WvStreamsDebuggerServer::ready_cb(Connection &s)
171 const char *line = s.getline();
172 if (line == NULL)
173 return;
175 WvStringList args;
176 wvtcl_decode(args, line);
178 WvString cmd = args.popstr();
179 if (!cmd)
181 s.send("ERROR", "Empty command");
182 return;
185 WvString result = s.debugger.run(cmd, args,
186 wv::bind(&Connection::result_cb, &s,
187 _1, _2));
188 if (!!result)
189 s.send("ERROR", result);
190 else
191 s.send("OK", "Command successful");