2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
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
) :
31 void WvStreamsDebuggerServer::Connection::result_cb(WvStringParm
,
32 WvStringList
&results
)
38 void WvStreamsDebuggerServer::Connection::send(WvStringParm code
,
42 results
.append(result
);
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
),
62 WvIStreamList::globallist
.append(&streams
, false);
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
);
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
);
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,
105 streams
.append(conn
, true);
109 void WvStreamsDebuggerServer::unix_listener_close_cb()
111 log("Listener on %s closing\n", *unix_listener
->src());
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,
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
)
134 s
.send("AUTH", s
.salt
);
136 s
.setcallback(wv::bind(&WvStreamsDebuggerServer::auth_response_cb
, this,
141 void WvStreamsDebuggerServer::auth_response_cb(Connection
&s
)
143 const char *line
= s
.getline();
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
,
162 s
.send("OK", "Authenticated");
163 s
.setcallback(wv::bind(&WvStreamsDebuggerServer::ready_cb
, this,
169 void WvStreamsDebuggerServer::ready_cb(Connection
&s
)
171 const char *line
= s
.getline();
176 wvtcl_decode(args
, line
);
178 WvString cmd
= args
.popstr();
181 s
.send("ERROR", "Empty command");
185 WvString result
= s
.debugger
.run(cmd
, args
,
186 wv::bind(&Connection::result_cb
, &s
,
189 s
.send("ERROR", result
);
191 s
.send("OK", "Command successful");