X-Git-Url: https://git.distorted.org.uk/~mdw/tripe/blobdiff_plain/cb2c2bfcff87c4d6959fe3ccf47a4b509f933e9c..0451e0e42fbba844a4cd307f5a133b50d8f438f7:/server/peer.c diff --git a/server/peer.c b/server/peer.c index 891888b0..ba8d7baf 100644 --- a/server/peer.c +++ b/server/peer.c @@ -27,11 +27,14 @@ #include "tripe.h" +/*----- Global state ------------------------------------------------------*/ + +sel_file udpsock[NADDRFAM]; + /*----- Static variables --------------------------------------------------*/ static sym_table byname; static addrmap byaddr; -static sel_file sock; static unsigned nmobile; /*----- Tunnel table ------------------------------------------------------*/ @@ -166,6 +169,54 @@ 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; + int ix; + 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; + p->afix = afix(p->spec.sa.sa.sa_family); assert(p->afix >= 0); + 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; + ix = p->afix; p->afix = q->afix; q->afix = ix; + 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 +239,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 +297,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: @@ -489,7 +516,7 @@ static int p_dotxend(peer *p) } IF_TRACING(T_PEER, trace_block(T_PACKET, "peer: sending packet", BBASE(&p->b), BLEN(&p->b)); ) - if (sendto(sock.fd, BBASE(&p->b), BLEN(&p->b), + if (sendto(udpsock[p->afix].fd, BBASE(&p->b), BLEN(&p->b), 0, &p->spec.sa.sa, sasz) < 0) { a_warn("PEER", "?PEER", p, "socket-write-error", "?ERRNO", A_END); return (0); @@ -781,8 +808,8 @@ void p_init(struct in_addr addr, unsigned port) strerror(errno)); } fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC); - sel_initfile(&sel, &sock, fd, SEL_READ, p_read, 0); - sel_addfile(&sock); + sel_initfile(&sel, &udpsock[AFIX_INET], fd, SEL_READ, p_read, 0); + sel_addfile(&udpsock[AFIX_INET]); T( trace(T_PEER, "peer: created socket"); ) sym_create(&byname); @@ -791,17 +818,17 @@ void p_init(struct in_addr addr, unsigned port) /* --- @p_port@ --- * * - * Arguments: --- + * Arguments: @int i@ = address family index to retrieve * * Returns: Port number used for socket. */ -unsigned p_port(void) +unsigned p_port(int i) { addr a; socklen_t sz = sizeof(addr); - if (getsockname(sock.fd, &a.sa, &sz)) + if (getsockname(udpsock[i].fd, &a.sa, &sz)) die(EXIT_FAILURE, "couldn't read port number: %s", strerror(errno)); assert(a.sa.sa_family == AF_INET); return (ntohs(a.sin.sin_port)); @@ -877,6 +904,7 @@ peer *p_create(peerspec *spec) p->ks = 0; p->pings = 0; p->ifname = 0; + p->afix = afix(p->spec.sa.sa.sa_family); assert(p->afix >= 0); memset(&p->st, 0, sizeof(stats)); p->st.t_start = time(0); if (!(tops->flags & TUNF_PRIVOPEN))