peer: make id a pointer, NULL until we know peer's ID.

Much better than undefined, and testing for NULL is better than
testing for STATE_INIT.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2016-07-01 11:27:57 +09:30
parent 7e6dc28f70
commit a613d8d1fb
5 changed files with 23 additions and 12 deletions

View File

@@ -13,6 +13,7 @@
#include <ccan/endian/endian.h> #include <ccan/endian/endian.h>
#include <ccan/mem/mem.h> #include <ccan/mem/mem.h>
#include <ccan/short_types/short_types.h> #include <ccan/short_types/short_types.h>
#include <ccan/structeq/structeq.h>
#include <inttypes.h> #include <inttypes.h>
#include <secp256k1.h> #include <secp256k1.h>
#include <secp256k1_ecdh.h> #include <secp256k1_ecdh.h>
@@ -351,6 +352,7 @@ static struct io_plan *check_proof(struct io_conn *conn, struct peer *peer)
struct signature sig; struct signature sig;
struct io_plan *(*cb)(struct io_conn *, struct peer *); struct io_plan *(*cb)(struct io_conn *, struct peer *);
Authenticate *auth; Authenticate *auth;
struct pubkey id;
auth = pkt_unwrap(peer, PKT__PKT_AUTH); auth = pkt_unwrap(peer, PKT__PKT_AUTH);
if (!auth) if (!auth)
@@ -362,16 +364,24 @@ static struct io_plan *check_proof(struct io_conn *conn, struct peer *peer)
return io_close(conn); return io_close(conn);
} }
if (!proto_to_pubkey(peer->dstate->secpctx, auth->node_id, &peer->id)) { if (!proto_to_pubkey(peer->dstate->secpctx, auth->node_id, &id)) {
log_unusual(peer->log, "Invalid auth id"); log_unusual(peer->log, "Invalid auth id");
return io_close(conn); return io_close(conn);
} }
/* Did we expect a specific ID? */
if (!peer->id)
peer->id = tal_dup(peer, struct pubkey, &id);
else if (!structeq(&id, peer->id)) {
log_unusual(peer->log, "Incorrect auth id");
return io_close(conn);
}
/* Signature covers *our* session key. */ /* Signature covers *our* session key. */
sha256_double(&sha, sha256_double(&sha,
neg->our_sessionpubkey, sizeof(neg->our_sessionpubkey)); neg->our_sessionpubkey, sizeof(neg->our_sessionpubkey));
if (!check_signed_hash(peer->dstate->secpctx, &sha, &sig, &peer->id)) { if (!check_signed_hash(peer->dstate->secpctx, &sha, &sig, peer->id)) {
log_unusual(peer->log, "Bad auth signature"); log_unusual(peer->log, "Bad auth signature");
return io_close(conn); return io_close(conn);
} }

View File

@@ -324,7 +324,7 @@ static char *to_string_(const tal_t *ctx,
h->r ? tal_hexstr(ctx, h->r, sizeof(*h->r)) h->r ? tal_hexstr(ctx, h->r, sizeof(*h->r))
: "UNKNOWN", : "UNKNOWN",
h->src ? to_string(ctx, lr, struct pubkey, h->src ? to_string(ctx, lr, struct pubkey,
&h->src->peer->id) h->src->peer->id)
: "local"); : "local");
} }

View File

@@ -56,7 +56,7 @@ struct peer *find_peer(struct lightningd_state *dstate, const struct pubkey *id)
struct peer *peer; struct peer *peer;
list_for_each(&dstate->peers, peer, list) { list_for_each(&dstate->peers, peer, list) {
if (peer->state != STATE_INIT && pubkey_eq(&peer->id, id)) if (peer->id && pubkey_eq(peer->id, id))
return peer; return peer;
} }
return NULL; return NULL;
@@ -115,9 +115,9 @@ void peer_open_complete(struct peer *peer, const char *problem)
log_debug(peer->log, "peer open complete"); log_debug(peer->log, "peer open complete");
assert(!peer->nc); assert(!peer->nc);
n = get_node(dstate, &peer->id); n = get_node(dstate, peer->id);
if (!n) if (!n)
n = new_node(dstate, &peer->id); n = new_node(dstate, peer->id);
peer->nc = add_connection(dstate, peer->nc = add_connection(dstate,
get_node(dstate, &dstate->id), n, get_node(dstate, &dstate->id), n,
dstate->config.fee_base, dstate->config.fee_base,
@@ -939,6 +939,7 @@ static struct peer *new_peer(struct lightningd_state *dstate,
list_add(&dstate->peers, &peer->list); list_add(&dstate->peers, &peer->list);
peer->state = STATE_INIT; peer->state = STATE_INIT;
peer->id = NULL;
peer->dstate = dstate; peer->dstate = dstate;
peer->addr.type = addr_type; peer->addr.type = addr_type;
peer->addr.protocol = addr_protocol; peer->addr.protocol = addr_protocol;
@@ -2611,7 +2612,8 @@ static void route_htlc_onwards(struct peer *peer,
return; return;
} }
log_debug_struct(peer->log, "HTLC forward to %s", struct pubkey, &next->id); log_debug_struct(peer->log, "HTLC forward to %s",
struct pubkey, next->id);
/* This checks the HTLC itself is possible. */ /* This checks the HTLC itself is possible. */
if (!command_htlc_add(next, msatoshis, if (!command_htlc_add(next, msatoshis,
@@ -2898,10 +2900,9 @@ static void json_getpeers(struct command *cmd,
json_add_string(response, "name", log_prefix(p->log)); json_add_string(response, "name", log_prefix(p->log));
json_add_string(response, "state", state_name(p->state)); json_add_string(response, "state", state_name(p->state));
/* This is only valid after crypto setup. */ if (p->id)
if (p->state != STATE_INIT)
json_add_pubkey(response, cmd->dstate->secpctx, json_add_pubkey(response, cmd->dstate->secpctx,
"peerid", &p->id); "peerid", p->id);
json_add_bool(response, "connected", p->conn && !p->fake_close); json_add_bool(response, "connected", p->conn && !p->fake_close);

View File

@@ -137,7 +137,7 @@ struct peer {
struct netaddr addr; struct netaddr addr;
/* Their ID. */ /* Their ID. */
struct pubkey id; struct pubkey *id;
/* Current received packet. */ /* Current received packet. */
Pkt *inpkt; Pkt *inpkt;

View File

@@ -218,7 +218,7 @@ struct peer *find_route(struct lightningd_state *dstate,
msatoshi += *fee; msatoshi += *fee;
log_info(dstate->base_log, "find_route:"); log_info(dstate->base_log, "find_route:");
log_add_struct(dstate->base_log, "via %s", struct pubkey, &first->id); log_add_struct(dstate->base_log, "via %s", struct pubkey, first->id);
for (i = 0; i < best; i++) { for (i = 0; i < best; i++) {
log_add_struct(dstate->base_log, " %s", log_add_struct(dstate->base_log, " %s",
struct pubkey, &(*route)[i]->dst->id); struct pubkey, &(*route)[i]->dst->id);