X-Git-Url: https://git.distorted.org.uk/~mdw/tripe/blobdiff_plain/5b9f3d3788bafcba79c893b1afc6a1c77bc77d20..813d2de98ea1bc45a68c3262c1df87f0834bd0ab:/server/servutil.c?ds=sidebyside diff --git a/server/servutil.c b/server/servutil.c index f19ce531..70776dda 100644 --- a/server/servutil.c +++ b/server/servutil.c @@ -9,19 +9,18 @@ * * This file is part of Trivial IP Encryption (TrIPE). * - * TrIPE is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. + * TrIPE is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 3 of the License, or (at your + * option) any later version. * - * TrIPE is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * TrIPE is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. * * You should have received a copy of the GNU General Public License - * along with TrIPE; if not, write to the Free Software Foundation, - * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * along with TrIPE. If not, see . */ /*----- Header files ------------------------------------------------------*/ @@ -32,7 +31,58 @@ octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ], buf_u[PKBUFSZ]; -/*----- Main code ---------------------------------------------------------*/ +/*----- Sequence numbers --------------------------------------------------*/ + +/* --- @seq_reset@ --- * + * + * Arguments: @seqwin *s@ = sequence-checking window + * + * Returns: --- + * + * Use: Resets a sequence number window. + */ + +void seq_reset(seqwin *s) { s->seq = 0; s->win = 0; } + +/* --- @seq_check@ --- * + * + * Arguments: @seqwin *s@ = sequence-checking window + * @uint32 q@ = sequence number to check + * @const char *service@ = service to report message from + * + * Returns: Zero on success, nonzero if the sequence number was bad. + * + * Use: Checks a sequence number against the window, updating things + * as necessary. + */ + +int seq_check(seqwin *s, uint32 q, const char *service) +{ + uint32 qbit; + uint32 n; + + if (q < s->seq) { + a_warn(service, "replay", "old-sequence", A_END); + return (-1); + } + if (q >= s->seq + SEQ_WINSZ) { + n = q - (s->seq + SEQ_WINSZ - 1); + if (n < SEQ_WINSZ) + s->win >>= n; + else + s->win = 0; + s->seq += n; + } + qbit = 1 << (q - s->seq); + if (s->win & qbit) { + a_warn(service, "replay", "duplicated-sequence", A_END); + return (-1); + } + s->win |= qbit; + return (0); +} + +/*----- Random odds and sods ----------------------------------------------*/ /* --- @timestr@ --- * * @@ -71,53 +121,43 @@ int mystrieq(const char *x, const char *y) } } -/* --- @seq_reset@ --- * - * - * Arguments: @seqwin *s@ = sequence-checking window +/*----- Address handling --------------------------------------------------*/ + +const struct addrfam aftab[] = { +#define DEF(af) { AF_##af, #af }, + ADDRFAM(DEF) +#undef DEF +}; + +/* --- @afix@ --- * * - * Returns: --- + * Arguments: @int af@ = an address family code * - * Use: Resets a sequence number window. + * Returns: The index of the address family's record in @aftab@, or @-1@. */ -void seq_reset(seqwin *s) { s->seq = 0; s->win = 0; } +int afix(int af) +{ + int i; -/* --- @seq_check@ --- * - * - * Arguments: @seqwin *s@ = sequence-checking window - * @uint32 q@ = sequence number to check - * @const char *service@ = service to report message from + for (i = 0; i < NADDRFAM; i++) + if (af == aftab[i].af) return (i); + return (-1); +} + +/* --- @addrsz@ --- * * - * Returns: Zero on success, nonzero if the sequence number was bad. + * Arguments: @const addr *a@ = a network address * - * Use: Checks a sequence number against the window, updating things - * as necessary. + * Returns: The size of the address, for passing into the sockets API. */ -int seq_check(seqwin *s, uint32 q, const char *service) +socklen_t addrsz(const addr *a) { - uint32 qbit; - uint32 n; - - if (q < s->seq) { - a_warn(service, "replay", "old-sequence", A_END); - return (-1); - } - if (q >= s->seq + SEQ_WINSZ) { - n = q - (s->seq + SEQ_WINSZ - 1); - if (n < SEQ_WINSZ) - s->win >>= n; - else - s->win = 0; - s->seq += n; + switch (a->sa.sa_family) { + case AF_INET: return (sizeof(a->sin)); + default: abort(); } - qbit = 1 << (q - s->seq); - if (s->win & qbit) { - a_warn(service, "replay", "duplicated-sequence", A_END); - return (-1); - } - s->win |= qbit; - return (0); } /*----- That's all, folks -------------------------------------------------*/