X-Git-Url: https://git.distorted.org.uk/~mdw/tripe/blobdiff_plain/19dd2531cafe84927c845e548ec68587007f3ab2..5d06f63eaa34a4bf0e116f3e2a1e4ef4e6434224:/server/servutil.c diff --git a/server/servutil.c b/server/servutil.c index 92a5f775..70776dda 100644 --- a/server/servutil.c +++ b/server/servutil.c @@ -1,29 +1,26 @@ /* -*-c-*- * - * $Id$ - * * Various handy server-only utilities * * (c) 2001 Straylight/Edgeware */ -/*----- Licensing notice --------------------------------------------------* +/*----- Licensing notice --------------------------------------------------* * * 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 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 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. + * * 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,64 +29,9 @@ /*----- Global variables --------------------------------------------------*/ -octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ]; - -/*----- Main code ---------------------------------------------------------*/ - -/* --- @mpstr@ --- * - * - * Arguments: @mp *m@ = a multiprecision integer - * - * Returns: A pointer to the integer's textual representation. - * - * Use: Converts a multiprecision integer to a string. Corrupts - * @buf_t@. - */ - -const char *mpstr(mp *m) -{ - if (mp_writestring(m, (char *)buf_t, sizeof(buf_t), 10)) - return (""); - return ((const char *)buf_t); -} - -/* --- @gestr@ --- * - * - * Arguments: @group *g@ = a group - * @ge *x@ = a group element - * - * Returns: A pointer to the element's textual representation. - * - * Use: Converts a group element to a string. Corrupts - * @buf_t@. - */ - -const char *gestr(group *g, ge *x) -{ - if (group_writestring(g, x, (char *)buf_t, sizeof(buf_t))) - return (""); - return ((const char *)buf_t); -} - -/* --- @timestr@ --- * - * - * Arguments: @time_t t@ = a time to convert - * - * Returns: A pointer to a textual representation of the time. - * - * Use: Converts a time to a textual representation. Corrupts - * @buf_t@. - */ +octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ], buf_u[PKBUFSZ]; -const char *timestr(time_t t) -{ - struct tm *tm; - if (!t) - return ("NEVER"); - tm = localtime(&t); - strftime((char *)buf_t, sizeof(buf_t), "%Y-%m-%dT%H:%M:%S", tm); - return ((const char *)buf_t); -} +/*----- Sequence numbers --------------------------------------------------*/ /* --- @seq_reset@ --- * * @@ -140,4 +82,82 @@ int seq_check(seqwin *s, uint32 q, const char *service) return (0); } +/*----- Random odds and sods ----------------------------------------------*/ + +/* --- @timestr@ --- * + * + * Arguments: @time_t t@ = a time to convert + * + * Returns: A pointer to a textual representation of the time. + * + * Use: Converts a time to a textual representation. Corrupts + * @buf_u@. + */ + +const char *timestr(time_t t) +{ + struct tm *tm; + if (!t) + return ("NEVER"); + tm = localtime(&t); + strftime((char *)buf_u, sizeof(buf_u), "%Y-%m-%dT%H:%M:%S", tm); + return ((const char *)buf_u); +} + +/* --- @mystrieq@ --- * + * + * Arguments: @const char *x, *y@ = two strings + * + * Returns: True if @x@ and @y are equal, up to case. + */ + +int mystrieq(const char *x, const char *y) +{ + for (;;) { + if (!*x && !*y) return (1); + if (tolower((unsigned char)*x) != tolower((unsigned char)*y)) + return (0); + x++; y++; + } +} + +/*----- Address handling --------------------------------------------------*/ + +const struct addrfam aftab[] = { +#define DEF(af) { AF_##af, #af }, + ADDRFAM(DEF) +#undef DEF +}; + +/* --- @afix@ --- * + * + * Arguments: @int af@ = an address family code + * + * Returns: The index of the address family's record in @aftab@, or @-1@. + */ + +int afix(int af) +{ + int i; + + for (i = 0; i < NADDRFAM; i++) + if (af == aftab[i].af) return (i); + return (-1); +} + +/* --- @addrsz@ --- * + * + * Arguments: @const addr *a@ = a network address + * + * Returns: The size of the address, for passing into the sockets API. + */ + +socklen_t addrsz(const addr *a) +{ + switch (a->sa.sa_family) { + case AF_INET: return (sizeof(a->sin)); + default: abort(); + } +} + /*----- That's all, folks -------------------------------------------------*/