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], buf_u[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_u, sizeof(buf_u), 10))
51 return ((const char *)buf_u);
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_u, sizeof(buf_u)))
69 return ((const char *)buf_u);
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_u, sizeof(buf_u), "%Y-%m-%dT%H:%M:%S", tm);
89 return ((const char *)buf_u);
92 /* --- @mystrieq@ --- *
94 * Arguments: @const char *x, *y@ = two strings
96 * Returns: True if @x@ and @y are equal, up to case.
99 int mystrieq(const char *x, const char *y)
102 if (!*x && !*y) return (1);
103 if (tolower((unsigned char)*x) != tolower((unsigned char)*y))
109 /* --- @seq_reset@ --- *
111 * Arguments: @seqwin *s@ = sequence-checking window
115 * Use: Resets a sequence number window.
118 void seq_reset(seqwin *s) { s->seq = 0; s->win = 0; }
120 /* --- @seq_check@ --- *
122 * Arguments: @seqwin *s@ = sequence-checking window
123 * @uint32 q@ = sequence number to check
124 * @const char *service@ = service to report message from
126 * Returns: Zero on success, nonzero if the sequence number was bad.
128 * Use: Checks a sequence number against the window, updating things
132 int seq_check(seqwin *s, uint32 q, const char *service)
138 a_warn(service, "replay", "old-sequence", A_END);
141 if (q >= s->seq + SEQ_WINSZ) {
142 n = q - (s->seq + SEQ_WINSZ - 1);
149 qbit = 1 << (q - s->seq);
151 a_warn(service, "replay", "duplicated-sequence", A_END);
158 /*----- That's all, folks -------------------------------------------------*/