3 * Various handy server-only utilities
5 * (c) 2001 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Trivial IP Encryption (TrIPE).
12 * TrIPE is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * TrIPE is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /*----- Header files ------------------------------------------------------*/
31 /*----- Global variables --------------------------------------------------*/
33 octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ];
35 /*----- Main code ---------------------------------------------------------*/
39 * Arguments: @mp *m@ = a multiprecision integer
41 * Returns: A pointer to the integer's textual representation.
43 * Use: Converts a multiprecision integer to a string. Corrupts
47 const char *mpstr(mp *m)
49 if (mp_writestring(m, (char *)buf_t, sizeof(buf_t), 10))
51 return ((const char *)buf_t);
56 * Arguments: @group *g@ = a group
57 * @ge *x@ = a group element
59 * Returns: A pointer to the element's textual representation.
61 * Use: Converts a group element to a string. Corrupts
65 const char *gestr(group *g, ge *x)
67 if (group_writestring(g, x, (char *)buf_t, sizeof(buf_t)))
69 return ((const char *)buf_t);
72 /* --- @timestr@ --- *
74 * Arguments: @time_t t@ = a time to convert
76 * Returns: A pointer to a textual representation of the time.
78 * Use: Converts a time to a textual representation. Corrupts
82 const char *timestr(time_t t)
88 strftime((char *)buf_t, sizeof(buf_t), "%Y-%m-%dT%H:%M:%S", tm);
89 return ((const char *)buf_t);
92 /* --- @seq_reset@ --- *
94 * Arguments: @seqwin *s@ = sequence-checking window
98 * Use: Resets a sequence number window.
101 void seq_reset(seqwin *s) { s->seq = 0; s->win = 0; }
103 /* --- @seq_check@ --- *
105 * Arguments: @seqwin *s@ = sequence-checking window
106 * @uint32 q@ = sequence number to check
107 * @const char *service@ = service to report message from
109 * Returns: Zero on success, nonzero if the sequence number was bad.
111 * Use: Checks a sequence number against the window, updating things
115 int seq_check(seqwin *s, uint32 q, const char *service)
121 a_warn(service, "replay", "old-sequence", A_END);
124 if (q >= s->seq + SEQ_WINSZ) {
125 n = q - (s->seq + SEQ_WINSZ - 1);
132 qbit = 1 << (q - s->seq);
134 a_warn(service, "replay", "duplicated-sequence", A_END);
141 /*----- That's all, folks -------------------------------------------------*/