Commit | Line | Data |
---|---|---|
410c8acf | 1 | /* -*-c-*- |
2 | * | |
410c8acf | 3 | * Various handy server-only utilities |
4 | * | |
5 | * (c) 2001 Straylight/Edgeware | |
6 | */ | |
7 | ||
e04c2d50 | 8 | /*----- Licensing notice --------------------------------------------------* |
410c8acf | 9 | * |
10 | * This file is part of Trivial IP Encryption (TrIPE). | |
11 | * | |
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. | |
e04c2d50 | 16 | * |
410c8acf | 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. | |
e04c2d50 | 21 | * |
410c8acf | 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. | |
25 | */ | |
26 | ||
410c8acf | 27 | /*----- Header files ------------------------------------------------------*/ |
28 | ||
29 | #include "tripe.h" | |
30 | ||
df9dfccf | 31 | /*----- Global variables --------------------------------------------------*/ |
32 | ||
33 | octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ]; | |
34 | ||
410c8acf | 35 | /*----- Main code ---------------------------------------------------------*/ |
36 | ||
37 | /* --- @mpstr@ --- * | |
38 | * | |
39 | * Arguments: @mp *m@ = a multiprecision integer | |
40 | * | |
41 | * Returns: A pointer to the integer's textual representation. | |
42 | * | |
43 | * Use: Converts a multiprecision integer to a string. Corrupts | |
a7880467 | 44 | * @buf_t@. |
410c8acf | 45 | */ |
46 | ||
47 | const char *mpstr(mp *m) | |
48 | { | |
a7880467 | 49 | if (mp_writestring(m, (char *)buf_t, sizeof(buf_t), 10)) |
410c8acf | 50 | return ("<failed>"); |
a7880467 | 51 | return ((const char *)buf_t); |
52 | } | |
53 | ||
52c03a2a | 54 | /* --- @gestr@ --- * |
55 | * | |
56 | * Arguments: @group *g@ = a group | |
57 | * @ge *x@ = a group element | |
58 | * | |
59 | * Returns: A pointer to the element's textual representation. | |
60 | * | |
61 | * Use: Converts a group element to a string. Corrupts | |
62 | * @buf_t@. | |
63 | */ | |
64 | ||
65 | const char *gestr(group *g, ge *x) | |
66 | { | |
67 | if (group_writestring(g, x, (char *)buf_t, sizeof(buf_t))) | |
68 | return ("<failed>"); | |
69 | return ((const char *)buf_t); | |
70 | } | |
71 | ||
a7880467 | 72 | /* --- @timestr@ --- * |
73 | * | |
74 | * Arguments: @time_t t@ = a time to convert | |
75 | * | |
76 | * Returns: A pointer to a textual representation of the time. | |
77 | * | |
78 | * Use: Converts a time to a textual representation. Corrupts | |
79 | * @buf_t@. | |
80 | */ | |
81 | ||
82 | const char *timestr(time_t t) | |
83 | { | |
84 | struct tm *tm; | |
85 | if (!t) | |
86 | return ("NEVER"); | |
87 | tm = localtime(&t); | |
88 | strftime((char *)buf_t, sizeof(buf_t), "%Y-%m-%dT%H:%M:%S", tm); | |
89 | return ((const char *)buf_t); | |
410c8acf | 90 | } |
91 | ||
37941236 | 92 | /* --- @seq_reset@ --- * |
93 | * | |
94 | * Arguments: @seqwin *s@ = sequence-checking window | |
95 | * | |
96 | * Returns: --- | |
97 | * | |
98 | * Use: Resets a sequence number window. | |
99 | */ | |
100 | ||
101 | void seq_reset(seqwin *s) { s->seq = 0; s->win = 0; } | |
102 | ||
103 | /* --- @seq_check@ --- * | |
104 | * | |
105 | * Arguments: @seqwin *s@ = sequence-checking window | |
106 | * @uint32 q@ = sequence number to check | |
f43df819 | 107 | * @const char *service@ = service to report message from |
37941236 | 108 | * |
f43df819 | 109 | * Returns: Zero on success, nonzero if the sequence number was bad. |
37941236 | 110 | * |
111 | * Use: Checks a sequence number against the window, updating things | |
112 | * as necessary. | |
113 | */ | |
114 | ||
f43df819 | 115 | int seq_check(seqwin *s, uint32 q, const char *service) |
37941236 | 116 | { |
117 | uint32 qbit; | |
118 | uint32 n; | |
119 | ||
f43df819 MW |
120 | if (q < s->seq) { |
121 | a_warn(service, "replay", "old-sequence", A_END); | |
122 | return (-1); | |
123 | } | |
37941236 | 124 | if (q >= s->seq + SEQ_WINSZ) { |
125 | n = q - (s->seq + SEQ_WINSZ - 1); | |
126 | if (n < SEQ_WINSZ) | |
127 | s->win >>= n; | |
128 | else | |
129 | s->win = 0; | |
130 | s->seq += n; | |
131 | } | |
132 | qbit = 1 << (q - s->seq); | |
f43df819 MW |
133 | if (s->win & qbit) { |
134 | a_warn(service, "replay", "duplicated-sequence", A_END); | |
135 | return (-1); | |
136 | } | |
37941236 | 137 | s->win |= qbit; |
138 | return (0); | |
139 | } | |
140 | ||
410c8acf | 141 | /*----- That's all, folks -------------------------------------------------*/ |