chiark / gitweb /
server/tripe.h: Fix commentary markup.
[tripe] / server / servutil.c
CommitLineData
410c8acf 1/* -*-c-*-
410c8acf 2 *
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 *
11ad66c2
MW
12 * TrIPE is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 3 of the License, or (at your
15 * option) any later version.
e04c2d50 16 *
11ad66c2
MW
17 * TrIPE is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
e04c2d50 21 *
410c8acf 22 * You should have received a copy of the GNU General Public License
11ad66c2 23 * along with TrIPE. If not, see <https://www.gnu.org/licenses/>.
410c8acf 24 */
25
410c8acf 26/*----- Header files ------------------------------------------------------*/
27
28#include "tripe.h"
29
df9dfccf 30/*----- Global variables --------------------------------------------------*/
31
a4b808b0 32octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ], buf_u[PKBUFSZ];
df9dfccf 33
410c8acf 34/*----- Main code ---------------------------------------------------------*/
35
a7880467 36/* --- @timestr@ --- *
37 *
38 * Arguments: @time_t t@ = a time to convert
39 *
40 * Returns: A pointer to a textual representation of the time.
41 *
42 * Use: Converts a time to a textual representation. Corrupts
a4b808b0 43 * @buf_u@.
a7880467 44 */
45
46const char *timestr(time_t t)
47{
48 struct tm *tm;
49 if (!t)
50 return ("NEVER");
51 tm = localtime(&t);
a4b808b0
MW
52 strftime((char *)buf_u, sizeof(buf_u), "%Y-%m-%dT%H:%M:%S", tm);
53 return ((const char *)buf_u);
410c8acf 54}
55
52b86648
MW
56/* --- @mystrieq@ --- *
57 *
58 * Arguments: @const char *x, *y@ = two strings
59 *
60 * Returns: True if @x@ and @y are equal, up to case.
61 */
62
63int mystrieq(const char *x, const char *y)
64{
65 for (;;) {
66 if (!*x && !*y) return (1);
67 if (tolower((unsigned char)*x) != tolower((unsigned char)*y))
68 return (0);
69 x++; y++;
70 }
71}
72
37941236 73/* --- @seq_reset@ --- *
74 *
75 * Arguments: @seqwin *s@ = sequence-checking window
76 *
77 * Returns: ---
78 *
79 * Use: Resets a sequence number window.
80 */
81
82void seq_reset(seqwin *s) { s->seq = 0; s->win = 0; }
83
84/* --- @seq_check@ --- *
85 *
86 * Arguments: @seqwin *s@ = sequence-checking window
87 * @uint32 q@ = sequence number to check
f43df819 88 * @const char *service@ = service to report message from
37941236 89 *
f43df819 90 * Returns: Zero on success, nonzero if the sequence number was bad.
37941236 91 *
92 * Use: Checks a sequence number against the window, updating things
93 * as necessary.
94 */
95
f43df819 96int seq_check(seqwin *s, uint32 q, const char *service)
37941236 97{
98 uint32 qbit;
99 uint32 n;
100
f43df819
MW
101 if (q < s->seq) {
102 a_warn(service, "replay", "old-sequence", A_END);
103 return (-1);
104 }
37941236 105 if (q >= s->seq + SEQ_WINSZ) {
106 n = q - (s->seq + SEQ_WINSZ - 1);
107 if (n < SEQ_WINSZ)
108 s->win >>= n;
109 else
110 s->win = 0;
111 s->seq += n;
112 }
113 qbit = 1 << (q - s->seq);
f43df819
MW
114 if (s->win & qbit) {
115 a_warn(service, "replay", "duplicated-sequence", A_END);
116 return (-1);
117 }
37941236 118 s->win |= qbit;
119 return (0);
120}
121
410c8acf 122/*----- That's all, folks -------------------------------------------------*/