From: Mark Wooding Date: Sun, 3 Sep 2017 23:18:41 +0000 (+0100) Subject: server/peer.c: Abstract out updating a peer's address. X-Git-Tag: 1.5.0~91 X-Git-Url: https://git.distorted.org.uk/~mdw/tripe/commitdiff_plain/23bbe6014c89ed239b25405a8b7a8725413620f7 server/peer.c: Abstract out updating a peer's address. --- diff --git a/server/peer.c b/server/peer.c index 891888b0..03ed2ae3 100644 --- a/server/peer.c +++ b/server/peer.c @@ -166,6 +166,51 @@ static int p_encrypt(peer *p, int ty, buf *bin, buf *bout) return (err); } +/* --- @p_updateaddr@ --- * + * + * Arguments: @peer *p@ = pointer to peer block + * @const addr *a@ = address to associate with this peer + * + * Returns: Zero if the address was changed; @+1@ if it was already + * right. + * + * Use: Updates our idea of @p@'s address. + */ + +int p_updateaddr(peer *p, const addr *a) +{ + peer *q; + peer_byaddr *pa, *qa; + unsigned f; + + /* --- Figure out how to proceed --- * + * + * If this address already belongs to a different peer, then swap the + * addresses over. This doesn't leave the displaced peer in an especially + * good state, but it ought to get sorted out soon enough. + */ + + pa = am_find(&byaddr, a, sizeof(peer_byaddr), &f); + if (f && pa->p == p) + return (+1); + else if (!f) { + T( trace(T_PEER, "peer: updating address for `%s'", p_name(p)); ) + am_remove(&byaddr, p->byaddr); + p->byaddr = pa; p->spec.sa = *a; pa->p = p; + a_notify("NEWADDR", "?PEER", p, "?ADDR", a, A_END); + return (0); + } else { + q = pa->p; qa = p->byaddr; + T( trace(T_PEER, "peer: swapping addresses for `%s' and `%s'", + p_name(p), p_name(q)); ) + q->byaddr = qa; qa->p = q; q->spec.sa = p->spec.sa; + p->byaddr = pa; pa->p = p; p->spec.sa = *a; + a_notify("NEWADDR", "?PEER", p, "?ADDR", a, A_END); + a_notify("NEWADDR", "?PEER", q, "?ADDR", &q->spec.sa, A_END); + return (0); + } +} + /* --- @p_decrypt@ --- * * * Arguments: @peer **pp@ = pointer to peer to decrypt message from @@ -188,9 +233,7 @@ static int p_decrypt(peer **pp, addr *a, size_t n, int ty, buf *bin, buf *bout) { peer *p, *q; - peer_byaddr *pa, *qa; int err = KSERR_DECRYPT; - unsigned f; /* --- If we have a match on the source address then try that first --- */ @@ -248,33 +291,11 @@ searched: return (-1); } - /* --- We found one that accepted, so update the peer's address --- * - * - * If we had an initial guess of which peer this packet came from -- i.e., - * @q@ is not null -- then swap the addresses over. This doesn't leave the - * evicted peer in an especially good state, but it ought to get sorted out - * soon enough. - */ + /* --- We found one that accepted, so update the peer's address --- */ if (!err) { *pp = p; - if (!q) { - T( trace(T_PEER, "peer: updating address for `%s'", p_name(p)); ) - pa = am_find(&byaddr, a, sizeof(peer_byaddr), &f); assert(!f); - am_remove(&byaddr, p->byaddr); - p->byaddr = pa; - pa->p = p; - p->spec.sa = *a; - a_notify("NEWADDR", "?PEER", p, "?ADDR", a, A_END); - } else { - T( trace(T_PEER, "peer: swapping addresses for `%s' and `%s'", - p_name(p), p_name(q)); ) - pa = p->byaddr; qa = q->byaddr; - pa->p = q; q->byaddr = pa; q->spec.sa = p->spec.sa; - qa->p = p; p->byaddr = qa; p->spec.sa = *a; - a_notify("NEWADDR", "?PEER", p, "?ADDR", a, A_END); - a_notify("NEWADDR", "?PEER", q, "?ADDR", &q->spec.sa, A_END); - } + p_updateaddr(p, a); } match: diff --git a/server/tripe.h b/server/tripe.h index fa5333bd..e7c47ecd 100644 --- a/server/tripe.h +++ b/server/tripe.h @@ -1409,6 +1409,19 @@ extern void ps_quit(void); /*----- Peer management ---------------------------------------------------*/ +/* --- @p_updateaddr@ --- * + * + * Arguments: @peer *p@ = pointer to peer block + * @const addr *a@ = address to associate with this peer + * + * Returns: Zero if the address was changed; @+1@ if it was already + * right. + * + * Use: Updates our idea of @p@'s address. + */ + +extern int p_updateaddr(peer */*p*/, const addr */*a*/); + /* --- @p_txstart@ --- * * * Arguments: @peer *p@ = pointer to peer block