From dd93f201e31f5a4d00f55325caf1460b152f91c2 Mon Sep 17 00:00:00 2001 From: Jeremy Lakeman Date: Wed, 14 Nov 2012 11:49:03 +1030 Subject: [PATCH] Add separate reverse lookup command --- commandline.c | 102 +++++++++++++++++++++++++++++++++++++++++++++- tests/dnaprotocol | 7 ++++ 2 files changed, 107 insertions(+), 2 deletions(-) diff --git a/commandline.c b/commandline.c index e21307e3..6d39e2f9 100644 --- a/commandline.c +++ b/commandline.c @@ -1719,8 +1719,8 @@ int app_node_info(int argc, const char *const *argv, struct command_line_option continue; } - time_ms_t timeout_ms = (next_send>timeout?timeout:next_send) - now; - if (overlay_mdp_client_poll(timeout_ms)<=0) + time_ms_t poll_timeout = (next_send>timeout?timeout:next_send) - now; + if (overlay_mdp_client_poll(poll_timeout)<=0) continue; int ttl=-1; @@ -1791,6 +1791,102 @@ int app_node_info(int argc, const char *const *argv, struct command_line_option return 0; } +int app_reverse_lookup(int argc, const char *const *argv, struct command_line_option *o, void *context) +{ + const char *sid, *delay; + + if (cli_arg(argc, argv, o, "sid", &sid, str_is_subscriber_id, "") == -1) + return -1; + if (cli_arg(argc, argv, o, "timeout", &delay, NULL, "3000") == -1) + return -1; + + + int port=32768+(random()&0xffff); + + unsigned char srcsid[SID_SIZE]; + unsigned char dstsid[SID_SIZE]; + + stowSid(dstsid,0,(char *)sid); + + if (overlay_mdp_getmyaddr(0,srcsid)) + return WHY("Unable to get my address"); + if (overlay_mdp_bind(srcsid,port)) + return WHY("Unable to bind port"); + + time_ms_t now = gettime_ms(); + time_ms_t timeout = now + atoi(delay); + time_ms_t next_send = now; + overlay_mdp_frame mdp_reply; + + while(now < timeout){ + now=gettime_ms(); + + if (now >= next_send){ + /* Send a unicast packet to this node, asking for any did */ + lookup_send_request(srcsid, port, dstsid, ""); + next_send+=125; + continue; + } + + time_ms_t poll_timeout = (next_send>timeout?timeout:next_send) - now; + if (overlay_mdp_client_poll(poll_timeout)<=0) + continue; + + int ttl=-1; + if (overlay_mdp_recv(&mdp_reply, port, &ttl)) + continue; + + if ((mdp_reply.packetTypeAndFlags&MDP_TYPE_MASK)==MDP_ERROR){ + // TODO log error? + continue; + } + + if (mdp_reply.packetTypeAndFlags!=MDP_TX) { + WHYF("MDP returned an unexpected message (type=0x%x)", + mdp_reply.packetTypeAndFlags); + + if (mdp_reply.packetTypeAndFlags==MDP_ERROR) + WHYF("MDP message is return/error: %d:%s", + mdp_reply.error.error,mdp_reply.error.message); + continue; + } + + // we might receive a late response from an ealier request on the same socket, ignore it + if (memcmp(mdp_reply.in.src.sid, dstsid, SID_SIZE)){ + WHYF("Unexpected result from SID %s", alloca_tohex_sid(mdp_reply.in.src.sid)); + continue; + } + + { + char sidhex[SID_STRLEN + 1]; + char did[DID_MAXSIZE + 1]; + char name[64]; + char uri[512]; + if ( !parseDnaReply((char *)mdp_reply.in.payload, mdp_reply.in.payload_length, sidhex, did, name, uri, NULL) + || !str_is_subscriber_id(sidhex) + || !str_is_did(did) + || !str_is_uri(uri) + ) { + WHYF("Received malformed DNA reply: %s", + alloca_toprint(160, (const char *)mdp_reply.in.payload, mdp_reply.in.payload_length)); + continue; + } + + /* Got a good DNA reply, copy it into place and stop polling */ + cli_puts("did"); + cli_delim(":"); + cli_puts(did); + cli_delim("\n"); + cli_puts("name"); + cli_delim(":"); + cli_puts(name); + cli_delim("\n"); + break; + } + } + return 0; +} + /* NULL marks ends of command structure. "" marks an arg that can take any value. "[]" marks an optional arg that can take any value. @@ -1882,6 +1978,8 @@ struct command_line_option command_line_options[]={ "Return information about SID, and optionally ask for DID resolution via network"}, {app_count_peers,{"peer","count",NULL},0, "Return a count of routable peers on the network"}, + {app_reverse_lookup, {"reverse", "lookup", "", "[]", NULL}, 0, + "Lookup the phone number and name of a given subscriber"}, {app_test_rfs,{"test","rfs",NULL},0, "Test RFS field calculation"}, {app_monitor_cli,{"monitor",NULL},0, diff --git a/tests/dnaprotocol b/tests/dnaprotocol index 6d024916..cf3665d1 100755 --- a/tests/dnaprotocol +++ b/tests/dnaprotocol @@ -107,6 +107,13 @@ test_LookupRemote() { assertStdoutGrep --matches=1 "^sid://$SIDB/local/$DIDB:$DIDB:$NAMEB\$" } +doc_ReverseLookup="Resolve a remote identity" +test_ReverseLookup() { + executeOk_servald reverse lookup $SIDB + assertStdoutGrep --matches=1 ":$DIDB" + assertStdoutGrep --matches=1 ":$NAMEB" +} + doc_NodeinfoLocal="Node info auto-resolves for local identities" test_NodeinfoLocal() { # node info for a local identity returns DID/Name since it is free, even