--- /dev/null
+/* -*-c-*-
+ *
+ * $Id$
+ *
+ * Various handy server-only utilities
+ *
+ * (c) 2001 Straylight/Edgeware
+ */
+
+/*----- 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.
+ *
+ * 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.
+ */
+
+/*----- Header files ------------------------------------------------------*/
+
+#include "tripe.h"
+
+/*----- 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 ("<failed>");
+ 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 ("<failed>");
+ 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@.
+ */
+
+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);
+}
+
+/* --- @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);
+}
+
+/*----- That's all, folks -------------------------------------------------*/