# # # patch "administrator.cc" # from [9dad369694bb93586422be021b9e0c97cb6cf073] # to [df93336d3ff1e1ef1d441d84ed1e243937d9bd6d] # # patch "administrator.hh" # from [f44bcbb5a1ed11d650be9000a67b1c9ba15c7509] # to [f410edff6f46b8029139835d12c2bea8e25c53c7] # # patch "basic_io_serverlist_reader.cc" # from [1e4823fd4213a8a1bef5bb35bcfe31562e737d64] # to [a8b0c7c1f1f0e7a17921b96b458d69c4aa21500b] # # patch "basic_io_serverlist_reader.hh" # from [eec996e24c5eb8cea9fb675b2f61f096716f6b15] # to [19f9f608cf418f7662b957a68649771294711bac] # # patch "buffer.cc" # from [6930ee89bbdc297f11453ade805115110116acd1] # to [430ce3374bb6938b832e57e9636a8cfcbe67d0b9] # # patch "channel.cc" # from [484866adc662ada029b74fe057c4f02a2d4149ce] # to [74dcb51d9ec09c7163c9a35f4c1e6051681af55b] # # patch "channel.hh" # from [f6bce5e76eca88fd407e1bc87ded2647010cb3f6] # to [5ab9c1f07c07884cb05aa4a5bedc451d19881e05] # # patch "err.hh" # from [6b90b83b476c2210c867fecfedefa65a40d7b31c] # to [7880a96e6e31043b0079d2641107f20afbbcb016] # # patch "query_client.cc" # from [7c175e09611051d71fedb7f282747361c586f367] # to [0173bfeca26f746ee592ade7c743c03d59611550] # # patch "query_client.hh" # from [cac3f7e03cfce0e544195e02f157f087ee3eefd3] # to [147311b466079c93ba80c3e564bc8a126ec03ccf] # # patch "server.cc" # from [eb4cde556d8eae36e8a9e1edd19729a515a294fa] # to [f75bf400b10f5036210cd997b0b7b344876b104c] # # patch "server.hh" # from [ea20a68b9e2148f3a6ed3d2ec6c840ea908aa98c] # to [0266eae7db0a66205c6036f4d5c60ca81d57b80b] # # patch "server_manager.cc" # from [000bcbace0880393e118a66f7d14d5200c5f3e2f] # to [b440ed62ee4963c0d737cfc5768a4b91b2fe81ec] # # patch "server_manager.hh" # from [770b74f3f1c11a26a8ab38d6f7b97f780606beda] # to [295bbc931e3c7acd0fa21589ca509b26812381fa] # # patch "serverlist_reader.hh" # from [08600cfed47d72207bd084f9d1ab9d9cb5642fab] # to [9613a97c615bfb147dd4910eedc8cb1d9b9150f7] # # patch "sock.cc" # from [f85a25c1743784ab60f9d75a3265cc276f745725] # to [3aa1ae7006abe004eaecbeafcf526c1a98abb3e8] # # patch "sock.hh" # from [e36fa1b42d04630450db64278ff939d4c72011fc] # to [169a677fc3106efa3d122247a6b8054f1c82d0c7] # # patch "usher.cc" # from [28fa8fca02016c89988ba5a670dc3756843ef658] # to [aec6d75a4cb98164e903cbda973b0b20de53859d] # ============================================================ --- administrator.cc 9dad369694bb93586422be021b9e0c97cb6cf073 +++ administrator.cc df93336d3ff1e1ef1d441d84ed1e243937d9bd6d @@ -1,14 +1,29 @@ #include "administrator.hh" +#include "server.hh" +#include "err.hh" +#include "io.hh" -#include +#include +#include +using std::cerr; +#include +using std::make_pair; +using std::max; +#include +using std::ifstream; +#include +using boost::lexical_cast; + +#include + administrator::cstate::cstate() : auth(false), rdone(false) { } -administrator::administrator() - : port(-1) +administrator::administrator(server_manager &sm, string const &cf) + : port(-1), manager(sm), conffile(cf) { } @@ -42,14 +57,14 @@ std::ostringstream oss; if (srv.empty()) { serverstate ss; - ss.num = total_connections; - if (connections_allowed) { - if (total_connections) + ss.num = manager.total_connections; + if (manager.connections_allowed) { + if (manager.total_connections) ss.state = serverstate::active; else ss.state = serverstate::waiting; } else { - if (total_connections) + if (manager.total_connections) ss.state = serverstate::shuttingdown; else ss.state = serverstate::shutdown; @@ -57,8 +72,8 @@ oss< >::iterator i; - i = server::servers_by_name.find(srv); - if (i != server::servers_by_name.end()) + i = manager.by_name.find(srv); + if (i != manager.by_name.end()) oss<second->get_state()<<"\n"; else oss<<"No such server.\n"; @@ -69,8 +84,8 @@ iss>>srv; std::ostringstream oss; map >::iterator i; - i = server::servers_by_name.find(srv); - if (i != server::servers_by_name.end()) { + i = manager.by_name.find(srv); + if (i != manager.by_name.end()) { i->second->enabled = true; oss<second->get_state()<<"\n"; } else @@ -81,8 +96,8 @@ iss>>srv; std::ostringstream oss; map >::iterator i; - i = server::servers_by_name.find(srv); - if (i != server::servers_by_name.end()) { + i = manager.by_name.find(srv); + if (i != manager.by_name.end()) { i->second->enabled = false; i->second->maybekill(); oss<second->get_state()<<"\n"; @@ -93,23 +108,23 @@ string state; iss>>state; map >::iterator i; - for (i = server::servers_by_name.begin(); - i != server::servers_by_name.end(); ++i) { + for (i = manager.by_name.begin(); + i != manager.by_name.end(); ++i) { if (state.empty() || i->second->get_state() == state) cs.buf += (cs.buf.empty()?"":" ") + i->first; } cs.buf += "\n"; } else if (cmd == "SHUTDOWN") { - connections_allowed = false; - kill_old_servers(); + manager.connections_allowed = false; + manager.kill_old_servers(); cs.buf = "ok\n"; } else if (cmd == "CONNECTIONS") { - cs.buf = lexical_cast(total_connections) + "\n"; + cs.buf = lexical_cast(manager.total_connections) + "\n"; } else if (cmd == "RELOAD") { - reload_conffile(conffile); + reload_conffile(); cs.buf = "ok\n"; } else if (cmd == "STARTUP") { - connections_allowed = true; + manager.connections_allowed = true; cs.buf = "ok\n"; } else { return true; @@ -208,3 +223,27 @@ conns.erase(*i); } } + +void +administrator::reload_conffile() +{ + std::ifstream ifs(conffile.c_str()); + vector delims; + delims.push_back("server"); + basic_io::stanza_reader reader(ifs, delims); + basic_io::stanza st; + bool ok = reader.get(st); + if (!ok) + return; + for (vector::iterator i = st.items.begin(); + i != st.items.end(); ++i) + { + if (i->key == "userpass") + { + if (i->values.size() != 2) + return; + admins.insert(make_pair(i->values[0].parsed, i->values[1].parsed)); + } + } + manager.reload_servers(); +} ============================================================ --- administrator.hh f44bcbb5a1ed11d650be9000a67b1c9ba15c7509 +++ administrator.hh f410edff6f46b8029139835d12c2bea8e25c53c7 @@ -1,14 +1,19 @@ #ifndef __ADMINISTRATOR_HH_ #define __ADMINISTRATOR_HH_ #include "sock.hh" +#include "server_manager.hh" #include using std::list; #include using std::pair; +#include +using std::string; +#include +using std::map; -struct fd_set; +#include struct administrator { @@ -21,11 +26,15 @@ cstate(); }; list > conns; - administrator(); + map admins; + server_manager &manager; + string conffile; + administrator(server_manager &sm, string const &cf); bool process(cstate & cs); void initialize(string const & ap); void add_to_select(int & maxfd, fd_set & rd, fd_set & wr, fd_set & er); void process_selected(fd_set & rd, fd_set & wr, fd_set & er); + void reload_conffile(); }; #endif ============================================================ --- basic_io_serverlist_reader.cc 1e4823fd4213a8a1bef5bb35bcfe31562e737d64 +++ basic_io_serverlist_reader.cc a8b0c7c1f1f0e7a17921b96b458d69c4aa21500b @@ -1,7 +1,13 @@ #include "basic_io_serverlist_reader.hh" #include +using std::string; +using std::vector; +using std::set; + +using namespace basic_io; + basic_io_serverlist_reader::basic_io_serverlist_reader(string const &fn) : filename(fn) { @@ -68,7 +74,7 @@ string str = i->values[0].parsed; unsigned int c = str.find(":"); ss.remote_host = str.substr(0, c); - if (c != lp.npos) + if (c != str.npos) ss.remote_port = boost::lexical_cast(str.substr(c+1)); } else if (i->key == "local") ============================================================ --- basic_io_serverlist_reader.hh eec996e24c5eb8cea9fb675b2f61f096716f6b15 +++ basic_io_serverlist_reader.hh 19f9f608cf418f7662b957a68649771294711bac @@ -1,5 +1,5 @@ -#ifndef __SERVERLIST_READER_HH_ -#define __SERVERLIST_READER_HH_ +#ifndef __BASIC_IO_SERVERLIST_READER_HH_ +#define __BASIC_IO_SERVERLIST_READER_HH_ #include "serverlist_reader.hh" #include "io.hh" @@ -13,7 +13,7 @@ std::string filename; std::ifstream ifs; boost::scoped_ptr reader; - basic_io_serverlist_reader(string const &fn); + basic_io_serverlist_reader(std::string const &fn); ~basic_io_serverlist_reader(); virtual serverspec get_next(); virtual bool begin_reading(); ============================================================ --- buffer.cc 6930ee89bbdc297f11453ade805115110116acd1 +++ buffer.cc 430ce3374bb6938b832e57e9636a8cfcbe67d0b9 @@ -1,5 +1,5 @@ #include "buffer.hh" -#include "errstr.hh" +#include "err.hh" #include ============================================================ --- channel.cc 484866adc662ada029b74fe057c4f02a2d4149ce +++ channel.cc 74dcb51d9ec09c7163c9a35f4c1e6051681af55b @@ -1,10 +1,20 @@ #include "channel.hh" +#include "query_client.hh" +#include "server_manager.hh" +#include "err.hh" -#include +#include +using std::max; +#include -channel(sock & c): num(++counter), +#include +#include + +channel::channel(sock & c, server_manager &sm) + : num(++counter), cli(c), srv(-1), - have_routed(false), no_server(false) + have_routed(false), no_server(false), + manager(sm) { char * dat; int size; @@ -19,16 +29,21 @@ cli.write_from(sbuf); } -~channel() + +channel::~channel() { if (who && !no_server) who->disconnect(); } -bool is_finished() + +bool +channel::is_finished() { return (cli == -1) && (srv == -1); } -void add_to_select(int & maxfd, fd_set & rd, fd_set & wr, fd_set & er) + +void +channel::add_to_select(int & maxfd, fd_set & rd, fd_set & wr, fd_set & er) { int c = cli; int s = srv; @@ -50,7 +65,9 @@ maxfd = max(maxfd, s); } } -bool process_selected(fd_set & rd, fd_set & wr, fd_set & er) + +bool +channel::process_selected(fd_set & rd, fd_set & wr, fd_set & er) { int c = cli; int s = srv; @@ -80,24 +97,17 @@ if (!have_routed) { string reply_srv, reply_pat; if (extract_reply(cbuf, reply_srv, reply_pat)) { - who = get_server(reply_srv, reply_pat); - if (who && who->enabled) { - try { - srv = who->connect(); - have_routed = true; - s = srv; - } catch (errstr & e) { - cerr<<"cannot contact server "<name()<<"\n"; - no_server = true; - } - } else { + try { + serversock ss = manager.connect_to_server(reply_srv, reply_pat); + who = ss.srv; + srv = ss; + have_routed = true; + s = srv; + } catch (errstr & e) { char * dat; int size; sbuf.getwrite(p, n); - if (who) - make_packet(srvdisabled, dat, size); - else - make_packet(notfound, dat, size); + make_packet(e.name, dat, size); if (n < size) size = n; memcpy(p, dat, size); sbuf.fixwrite(size); ============================================================ --- channel.hh f6bce5e76eca88fd407e1bc87ded2647010cb3f6 +++ channel.hh 5ab9c1f07c07884cb05aa4a5bedc451d19881e05 @@ -8,8 +8,10 @@ #include using boost::shared_ptr; -struct fd_set; +#include +struct server_manager; + struct channel { static int counter; @@ -21,7 +23,8 @@ buffer cbuf; buffer sbuf; shared_ptr who; - channel(sock & c); + server_manager &manager; + channel(sock & c, server_manager &sm); ~channel(); bool is_finished(); void add_to_select(int & maxfd, fd_set & rd, fd_set & wr, fd_set & er); ============================================================ --- err.hh 6b90b83b476c2210c867fecfedefa65a40d7b31c +++ err.hh 7880a96e6e31043b0079d2641107f20afbbcb016 @@ -1,6 +1,8 @@ #ifndef __ERR_HH_ #define __ERR_HH_ +#include + struct errstr { std::string name; ============================================================ --- query_client.cc 7c175e09611051d71fedb7f282747361c586f367 +++ query_client.cc 0173bfeca26f746ee592ade7c743c03d59611550 @@ -1,8 +1,18 @@ #include "buffer.hh" #include using std::string; +#include +using std::cerr; +string const greeting = " Hello! This is the monotone usher at localhost. What would you like?"; + +string const notfound = "!Sorry, I don't know where to find that."; + +string const disabled = "!Sorry, this usher is not currently accepting connections."; + +string const srvdisabled = "!Sorry, that server is currently disabled."; + // packet format is: // byte version // byte cmd {100 if we send, 101 if we receive} ============================================================ --- query_client.hh cac3f7e03cfce0e544195e02f157f087ee3eefd3 +++ query_client.hh 147311b466079c93ba80c3e564bc8a126ec03ccf @@ -1,11 +1,15 @@ #ifndef __QUERY_CLIENT_HH_ #define __QUERY_CLIENT_HH_ +#include + struct buffer; -namespace std { - struct string; -} +extern std::string const greeting; +//extern std::string const notfound; +//extern std::string const disabled; +//extern std::string const srvdisabled; + void make_packet(std::string const & msg, char * & pkt, int & size); bool extract_reply(buffer & buf, std::string & host, std::string & pat); ============================================================ --- server.cc eb4cde556d8eae36e8a9e1edd19729a515a294fa +++ server.cc f75bf400b10f5036210cd997b0b7b344876b104c @@ -1,5 +1,28 @@ #include "server.hh" +#include "err.hh" +#include +using std::cerr; + +#include +#include +#include + +#include +using boost::lexical_cast; + +// keep local servers around for this many seconds after the last +// client disconnects from them (only accurate to ~10 seconds) +int const server_idle_timeout = 60; + +// ranges that dynamic (local) servers can be put on +int const minport = 15000; +int const maxport = 65000; +int const minaddr[] = {127, 0, 1, 1}; +int const maxaddr[] = {127, 254, 254, 254}; +int currport = 0; +int curraddr[] = {0, 0, 0, 0}; + int fork_server(vector const & args); serverstate::serverstate(): state(unknown), num(0) {} @@ -175,20 +198,13 @@ args.push_back(monotone); args.push_back("serve"); args.push_back("--bind=" + addr + ":" + lexical_cast(port)); - unsigned int n = 0, m = 0; - n = arguments.find_first_not_of(" \t"); - while (n != string::npos && m != string::npos) { - m = arguments.find_first_of(" ", n); - args.push_back(arguments.substr(n, m-n)); - n = arguments.find_first_not_of(" ", m); - } + for (vector::iterator j = arguments.begin(); + j != arguments.end(); ++j) + args.push_back(*j); pid = fork_server(args); } } sock s = make_outgoing(port, addr); - if (local && !connection_count) { - live_servers.insert(by_name->second); - } ++connection_count; return s; } ============================================================ --- server.hh ea20a68b9e2148f3a6ed3d2ec6c840ea908aa98c +++ server.hh 0266eae7db0a66205c6036f4d5c60ca81d57b80b @@ -1,10 +1,12 @@ #ifndef __SERVER_HH_ #define __SERVER_HH_ -] + #include "sock.hh" #include using std::string; +#include +using std::vector; struct serverstate { ============================================================ --- server_manager.cc 000bcbace0880393e118a66f7d14d5200c5f3e2f +++ server_manager.cc b440ed62ee4963c0d737cfc5768a4b91b2fe81ec @@ -166,6 +166,10 @@ serversock ss(s); ss.server = srv; ++total_connections; + if (srv->local && srv->connection_count == 1) + { + live_servers.insert(srv); + } return ss; } else ============================================================ --- server_manager.hh 770b74f3f1c11a26a8ab38d6f7b97f780606beda +++ server_manager.hh 295bbc931e3c7acd0fa21589ca509b26812381fa @@ -1,7 +1,8 @@ #ifndef __SERVER_MANAGER_HH_ #define __SERVER_MANAGER_HH_ #include "sock.hh" +#include "server.hh" #include using std::string; ============================================================ --- serverlist_reader.hh 08600cfed47d72207bd084f9d1ab9d9cb5642fab +++ serverlist_reader.hh 9613a97c615bfb147dd4910eedc8cb1d9b9150f7 @@ -1,14 +1,18 @@ #ifndef __SERVERLIST_READER_HH_ #define __SERVERLIST_READER_HH_ +#include +#include +#include + struct serverspec { - string name; - set hosts, patterns; + std::string name; + std::set hosts, patterns; bool local; bool valid; - vector local_args; - string remote_host; + std::vector local_args; + std::string remote_host; int remote_port; }; ============================================================ --- sock.cc f85a25c1743784ab60f9d75a3265cc276f745725 +++ sock.cc 3aa1ae7006abe004eaecbeafcf526c1a98abb3e8 @@ -107,7 +107,7 @@ buf.fixread(n); return true; } -set sock::all_socks; +std::set sock::all_socks; sock start(string addr, int port) ============================================================ --- sock.hh e36fa1b42d04630450db64278ff939d4c72011fc +++ sock.hh 169a677fc3106efa3d122247a6b8054f1c82d0c7 @@ -1,12 +1,15 @@ #ifndef __SOCK_HH_ #define __SOCK_HH_ +#include +#include + struct buffer; struct sock { int *s; - static set all_socks; + static std::set all_socks; operator int(); sock(int ss); sock(sock const & ss); @@ -20,6 +23,6 @@ bool write_from(buffer & buf); }; -sock start(string addr, int port); +sock start(std::string addr, int port); #endif ============================================================ --- usher.cc 28fa8fca02016c89988ba5a670dc3756843ef658 +++ usher.cc aec6d75a4cb98164e903cbda973b0b20de53859d @@ -12,7 +12,6 @@ #include #include #include -#include #include #include #include @@ -46,28 +45,10 @@ string listenaddr = "0.0.0.0"; string monotone = "mtn"; -// keep local servers around for this many seconds after the last -// client disconnects from them (only accurate to ~10 seconds) -int const server_idle_timeout = 60; -// ranges that dynamic (local) servers can be put on -int const minport = 15000; -int const maxport = 65000; -int const minaddr[] = {127, 0, 1, 1}; -int const maxaddr[] = {127, 254, 254, 254}; -int currport = 0; -int curraddr[] = {0, 0, 0, 0}; -string const greeting = " Hello! This is the monotone usher at localhost. What would you like?"; -string const notfound = "!Sorry, I don't know where to find that."; -string const disabled = "!Sorry, this usher is not currently accepting connections."; - -string const srvdisabled = "!Sorry, that server is currently disabled."; - - - string getline(std::istream & in) { string out; @@ -83,27 +64,10 @@ -bool reload_pending; -map admins; +bool reload_pending(false); string conffile; -void reload_conffile(string const & file) -{ - reload_pending = false; - cerr<<"Reloading config file...\n"; - std::ifstream cf(file.c_str()); - string line = getline(cf); - while (!line.empty()) { - std::istringstream iss(line); - string a, b, c; - iss>>a>>b>>c; - if (a == "userpass") - admins.insert(make_pair(b, c)); - line = getline(cf); - } - -} void sched_reload(int sig) { reload_pending = true; @@ -252,7 +216,10 @@ } kill_old_servers(); if (reload_pending) - reload_conffile(conffile); + { + reload_pendinf = false; + reload_conffile(conffile); + } admin.process_selected(rd, wr, er); }