# # # patch "cmd_netsync.cc" # from [3fc803802fa21cc1e8e35faa705c749fa284c841] # to [f1f2ecbdc31ebece21d44885bfc47ee43b60622e] # # patch "netcmd.hh" # from [902079e85a426929649f70aea179a7d706939711] # to [10527907b0ef2b48afc16182cc998211ba879d08] # # patch "netsync.cc" # from [e52a6b004ef68b64510c8949ddaf90109050f458] # to [2dc3f80b0fd6c7d168827ef49ce7de88d7452624] # ============================================================ --- cmd_netsync.cc 3fc803802fa21cc1e8e35faa705c749fa284c841 +++ cmd_netsync.cc f1f2ecbdc31ebece21d44885bfc47ee43b60622e @@ -67,47 +67,28 @@ find_key(options & opts, lua_hooks & lua, database & db, key_store & keys, - utf8 const & addr, - globish const & include, - globish const & exclude, - bool needed = true) + netsync_connection_info const & info, + bool need_key = true) { if (!opts.signing_key().empty()) return; rsa_keypair_id key; - uri u; - utf8 host(addr); + + utf8 host(info.client.unparsed); + if (!info.client.u.host.empty()) + host = utf8(info.client.u.host); - parse_uri(addr(), u); - if (!u.host.empty()) - host = utf8(u.host); - if (needed - && (key().empty() - || !lua.hook_get_netsync_key(host, include, exclude, key))) + if (!lua.hook_get_netsync_key(host, + info.client.include_pattern, + info.client.exclude_pattern, key) + && need_key) get_user_key(opts, lua, db, keys, key); opts.signing_key = key; } static void -find_key_if_needed(options & opts, - lua_hooks & lua, - database & db, - key_store & keys, - utf8 const & addr, - globish const & include, - globish const & exclude, - bool needed = true) -{ - uri u; - parse_uri(addr(), u); - - if (lua.hook_use_transport_auth(u)) - find_key(opts, lua, db, keys, addr, include, exclude, needed); -} - -static void extract_patterns(options & opts, database & db, args_vector const & args, globish & include_pattern, globish & exclude_pattern) { @@ -150,6 +131,58 @@ extract_patterns(options & opts, databas } } +static void +build_client_connection_info(options & opts, + lua_hooks & lua, + database & db, + key_store & keys, + utf8 const & addr, + globish const & include, + globish const & exclude, + netsync_connection_info & info, + bool need_key = true) +{ + info.client.include_pattern = include; + info.client.exclude_pattern = exclude; + info.client.unparsed = addr; + parse_uri(info.client.unparsed(), info.client.u); + if (lua.hook_get_netsync_connect_command(info.client.u, + info.client.include_pattern, + info.client.exclude_pattern, + global_sanity.debug_p(), + info.client.argv)) + { + info.client.use_argv = true; + opts.use_transport_auth = lua.hook_use_transport_auth(info.client.u); + } + else + { + info.client.use_argv = false; + } + if (opts.use_transport_auth) + { + find_key(opts, lua, db, keys, info, need_key); + } +} + +static void +extract_client_connection_info(options & opts, + lua_hooks & lua, + database & db, + key_store & keys, + args_vector const & args, + netsync_connection_info & info, + bool need_key = true) +{ + utf8 addr; + globish inc; + globish exc; + extract_address(opts, db, args, addr); + extract_patterns(opts, db, args, inc, exc); + build_client_connection_info(opts, lua, db, keys, + addr, inc, exc, info, need_key); +} + CMD(push, "push", "", CMD_REF(network), N_("[ADDRESS[:PORTNUMBER] [PATTERN ...]]"), N_("Pushes branches to a netsync server"), @@ -162,19 +195,11 @@ CMD(push, "push", "", CMD_REF(network), key_store keys(app); project_t project(db); - utf8 addr; - globish include_pattern, exclude_pattern; - extract_address(app.opts, db, args, addr); - extract_patterns(app.opts, db, args, include_pattern, exclude_pattern); - find_key_if_needed(app.opts, app.lua, db, keys, - addr, include_pattern, exclude_pattern); + netsync_connection_info info; + extract_client_connection_info(app.opts, app.lua, db, keys, args, info); - std::list uris; - uris.push_back(addr); - run_netsync_protocol(app.opts, app.lua, project, keys, - client_voice, source_role, uris, - include_pattern, exclude_pattern); + client_voice, source_role, info); } CMD(pull, "pull", "", CMD_REF(network), @@ -188,22 +213,15 @@ CMD(pull, "pull", "", CMD_REF(network), key_store keys(app); project_t project(db); - utf8 addr; - globish include_pattern, exclude_pattern; - extract_address(app.opts, db, args, addr); - extract_patterns(app.opts, db, args, include_pattern, exclude_pattern); - find_key_if_needed(app.opts, app.lua, db, keys, - addr, include_pattern, exclude_pattern, false); + netsync_connection_info info; + extract_client_connection_info(app.opts, app.lua, db, keys, + args, info, false); if (app.opts.signing_key() == "") P(F("doing anonymous pull; use -kKEYNAME if you need authentication")); - std::list uris; - uris.push_back(addr); - run_netsync_protocol(app.opts, app.lua, project, keys, - client_voice, sink_role, uris, - include_pattern, exclude_pattern); + client_voice, sink_role, info); } CMD(sync, "sync", "", CMD_REF(network), @@ -218,19 +236,11 @@ CMD(sync, "sync", "", CMD_REF(network), key_store keys(app); project_t project(db); - utf8 addr; - globish include_pattern, exclude_pattern; - extract_address(app.opts, db, args, addr); - extract_patterns(app.opts, db, args, include_pattern, exclude_pattern); - find_key_if_needed(app.opts, app.lua, db, keys, - addr, include_pattern, exclude_pattern, false); + netsync_connection_info info; + extract_client_connection_info(app.opts, app.lua, db, keys, args, info); - std::list uris; - uris.push_back(addr); - run_netsync_protocol(app.opts, app.lua, project, keys, - client_voice, source_and_sink_role, uris, - include_pattern, exclude_pattern); + client_voice, source_and_sink_role, info); } class dir_cleanup_helper @@ -327,8 +337,10 @@ CMD(clone, "clone", "", CMD_REF(network) globish include_pattern(app.opts.branchname()); globish exclude_pattern(app.opts.exclude_patterns); - find_key_if_needed(app.opts, app.lua, db, keys, - addr, include_pattern, exclude_pattern, false); + netsync_connection_info info; + build_client_connection_info(app.opts, app.lua, db, keys, + addr, include_pattern, exclude_pattern, + info); if (app.opts.signing_key() == "") P(F("doing anonymous pull; use -kKEYNAME if you need authentication")); @@ -353,12 +365,8 @@ CMD(clone, "clone", "", CMD_REF(network) // make sure we're back in the original dir so that file: URIs work change_current_working_dir(start_dir); - std::list uris; - uris.push_back(addr); - run_netsync_protocol(app.opts, app.lua, project, keys, - client_voice, sink_role, uris, - include_pattern, exclude_pattern); + client_voice, sink_role, info); change_current_working_dir(workspace_dir); @@ -468,6 +476,9 @@ CMD_NO_WORKSPACE(serve, "serve", "", CMD pid_file pid(app.opts.pidfile); db.ensure_open(); + + netsync_connection_info info; + info.server.addrs = app.opts.bind_uris; if (app.opts.use_transport_auth) { @@ -475,20 +486,18 @@ CMD_NO_WORKSPACE(serve, "serve", "", CMD F("need permission to store persistent passphrase " "(see hook persist_phrase_ok())")); + info.client.include_pattern = globish("*"); + info.client.exclude_pattern = globish(""); if (!app.opts.bind_uris.empty()) - find_key(app.opts, app.lua, db, keys, - *app.opts.bind_uris.begin(), globish("*"), globish("")); - else - find_key(app.opts, app.lua, db, keys, - utf8(), globish("*"), globish("")); + info.client.unparsed = *app.opts.bind_uris.begin(); + find_key(app.opts, app.lua, db, keys, info); } else if (!app.opts.bind_stdio) W(F("The --no-transport-auth option is usually only used " "in combination with --stdio")); run_netsync_protocol(app.opts, app.lua, project, keys, - server_voice, source_and_sink_role, app.opts.bind_uris, - globish("*"), globish("")); + server_voice, source_and_sink_role, info); } // Local Variables: ============================================================ --- netcmd.hh 902079e85a426929649f70aea179a7d706939711 +++ netcmd.hh 10527907b0ef2b48afc16182cc998211ba879d08 @@ -14,8 +14,10 @@ #include #include +#include "globish.hh" #include "merkle_tree.hh" #include "numeric_vocab.hh" +#include "uri.hh" #include "vocab.hh" #include "hmac.hh" #include "string_queue.hh" @@ -176,13 +178,28 @@ public: }; +struct netsync_connection_info +{ + struct + { + std::list addrs; + } server; + struct + { + globish include_pattern; + globish exclude_pattern; + uri u; + utf8 unparsed; + std::vector argv; + bool use_argv; + } client; +}; + void run_netsync_protocol(options & opts, lua_hooks & lua, project_t & project, key_store & keys, protocol_voice voice, protocol_role role, - std::list const & addrs, - globish const & include_pattern, - globish const & exclude_pattern); + netsync_connection_info const & info); // Local Variables: // mode: C++ ============================================================ --- netsync.cc e52a6b004ef68b64510c8949ddaf90109050f458 +++ netsync.cc 2dc3f80b0fd6c7d168827ef49ce7de88d7452624 @@ -2360,30 +2360,19 @@ build_stream_to_server(options & opts, l static shared_ptr build_stream_to_server(options & opts, lua_hooks & lua, - globish const & include_pattern, - globish const & exclude_pattern, - utf8 const & address, + netsync_connection_info info, Netxx::port_type default_port, Netxx::Timeout timeout) { shared_ptr server; - uri u; - vector argv; - - parse_uri(address(), u); - if (lua.hook_get_netsync_connect_command(u, - include_pattern, - exclude_pattern, - global_sanity.debug_p(), - argv)) + + if (info.client.use_argv) { - I(argv.size() > 0); - string cmd = argv[0]; - argv.erase(argv.begin()); - opts.use_transport_auth = lua.hook_use_transport_auth(u); + I(info.client.argv.size() > 0); + string cmd = info.client.argv[0]; + info.client.argv.erase(info.client.argv.begin()); return shared_ptr - (new Netxx::PipeStream(cmd, argv)); - + (new Netxx::PipeStream(cmd, info.client.argv)); } else { @@ -2392,7 +2381,12 @@ build_stream_to_server(options & opts, l #else bool use_ipv6=false; #endif - Netxx::Address addr(address().c_str(), + string host(info.client.u.host); + if (host.empty()) + host = info.client.unparsed(); + if (!info.client.u.port.empty()) + default_port = lexical_cast(info.client.u.port); + Netxx::Address addr(info.client.unparsed().c_str(), default_port, use_ipv6); return shared_ptr (new Netxx::Stream(addr, timeout)); @@ -2405,26 +2399,20 @@ call_server(options & opts, project_t & project, key_store & keys, protocol_role role, - globish const & include_pattern, - globish const & exclude_pattern, - std::list const & addresses, + netsync_connection_info const & info, Netxx::port_type default_port, unsigned long timeout_seconds) { Netxx::PipeCompatibleProbe probe; transaction_guard guard(project.db); - I(addresses.size() == 1); - utf8 address(*addresses.begin()); Netxx::Timeout timeout(static_cast(timeout_seconds)), instant(0,1); - P(F("connecting to %s") % address); + P(F("connecting to %s") % info.client.unparsed); shared_ptr server = build_stream_to_server(opts, lua, - include_pattern, - exclude_pattern, - address, default_port, + info, default_port, timeout); @@ -2435,9 +2423,9 @@ call_server(options & opts, session sess(opts, lua, project, keys, role, client_voice, - include_pattern, - exclude_pattern, - address(), server); + info.client.include_pattern, + info.client.exclude_pattern, + info.client.unparsed(), server); while (true) { @@ -2603,8 +2591,6 @@ handle_new_connection(options & opts, Netxx::StreamServer & server, Netxx::Timeout & timeout, protocol_role role, - globish const & include_pattern, - globish const & exclude_pattern, map > & sessions) { L(FL("accepting new connection on %s : %s") @@ -2631,7 +2617,7 @@ handle_new_connection(options & opts, shared_ptr sess(new session(opts, lua, project, keys, role, server_voice, - include_pattern, exclude_pattern, + globish("*"), globish(""), lexical_cast(client), str)); sess->begin_service(); sessions.insert(make_pair(client.get_socketfd(), sess)); @@ -2775,8 +2761,6 @@ serve_connections(options & opts, project_t & project, key_store & keys, protocol_role role, - globish const & include_pattern, - globish const & exclude_pattern, std::list const & addresses, Netxx::port_type default_port, unsigned long timeout_seconds, @@ -2878,17 +2862,19 @@ serve_connections(options & opts, = server_initiated_sync_requests.front(); server_initiated_sync_requests.pop_front(); - utf8 addr(request.address); - globish inc(request.include); - globish exc(request.exclude); - + netsync_connection_info info; + info.client.unparsed = utf8(request.address); + info.client.include_pattern = globish(request.include); + info.client.exclude_pattern = globish(request.exclude); + info.client.use_argv = false; + parse_uri(info.client.unparsed(), info.client.u); + try { - P(F("connecting to %s") % addr()); + P(F("connecting to %s") % info.client.unparsed); shared_ptr server = build_stream_to_server(opts, lua, - inc, exc, - addr, default_port, + info, default_port, timeout); // 'false' here means not to revert changes when @@ -2907,8 +2893,10 @@ serve_connections(options & opts, shared_ptr sess(new session(opts, lua, project, keys, role, client_voice, - inc, exc, - addr(), server, true)); + info.client.include_pattern, + info.client.exclude_pattern, + info.client.unparsed(), + server, true)); sessions.insert(make_pair(server->get_socketfd(), sess)); } @@ -2947,7 +2935,6 @@ serve_connections(options & opts, else if (fd == server) handle_new_connection(opts, lua, project, keys, addr, server, timeout, role, - include_pattern, exclude_pattern, sessions); // or an existing session woke up @@ -3302,20 +3289,18 @@ run_netsync_protocol(options & opts, lua project_t & project, key_store & keys, protocol_voice voice, protocol_role role, - std::list const & addrs, - globish const & include_pattern, - globish const & exclude_pattern) + netsync_connection_info const & info) { - if (include_pattern().find_first_of("'\"") != string::npos) + if (info.client.include_pattern().find_first_of("'\"") != string::npos) { W(F("include branch pattern contains a quote character:\n" - "%s") % include_pattern()); + "%s") % info.client.include_pattern()); } - if (exclude_pattern().find_first_of("'\"") != string::npos) + if (info.client.exclude_pattern().find_first_of("'\"") != string::npos) { W(F("exclude branch pattern contains a quote character:\n" - "%s") % exclude_pattern()); + "%s") % info.client.exclude_pattern()); } // We do not want to be killed by SIGPIPE from a network disconnect. @@ -3330,14 +3315,15 @@ run_netsync_protocol(options & opts, lua shared_ptr str(new Netxx::PipeStream(0,1)); shared_ptr sess(new session(opts, lua, project, keys, role, server_voice, - include_pattern, exclude_pattern, + globish("*"), globish(""), "stdio", str)); serve_single_connection(sess,constants::netsync_timeout_seconds); } else serve_connections(opts, lua, project, keys, - role, include_pattern, exclude_pattern, - addrs, static_cast(constants::netsync_default_port), + role, + info.server.addrs, + static_cast(constants::netsync_default_port), static_cast(constants::netsync_timeout_seconds), static_cast(constants::netsync_connection_limit)); } @@ -3345,8 +3331,8 @@ run_netsync_protocol(options & opts, lua { I(voice == client_voice); call_server(opts, lua, project, keys, - role, include_pattern, exclude_pattern, - addrs, static_cast(constants::netsync_default_port), + role, info, + static_cast(constants::netsync_default_port), static_cast(constants::netsync_timeout_seconds)); } }