server/, mon/: Introduce transport of TrIPE over IPv6.
[tripe] / server / servutil.c
1 /* -*-c-*-
2 *
3 * Various handy server-only utilities
4 *
5 * (c) 2001 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Trivial IP Encryption (TrIPE).
11 *
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.
16 *
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.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE. If not, see <https://www.gnu.org/licenses/>.
24 */
25
26 /*----- Header files ------------------------------------------------------*/
27
28 #include "tripe.h"
29
30 /*----- Global variables --------------------------------------------------*/
31
32 octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ], buf_u[PKBUFSZ];
33
34 /*----- Sequence numbers --------------------------------------------------*/
35
36 /* --- @seq_reset@ --- *
37 *
38 * Arguments: @seqwin *s@ = sequence-checking window
39 *
40 * Returns: ---
41 *
42 * Use: Resets a sequence number window.
43 */
44
45 void seq_reset(seqwin *s) { s->seq = 0; s->win = 0; }
46
47 /* --- @seq_check@ --- *
48 *
49 * Arguments: @seqwin *s@ = sequence-checking window
50 * @uint32 q@ = sequence number to check
51 * @const char *service@ = service to report message from
52 *
53 * Returns: Zero on success, nonzero if the sequence number was bad.
54 *
55 * Use: Checks a sequence number against the window, updating things
56 * as necessary.
57 */
58
59 int seq_check(seqwin *s, uint32 q, const char *service)
60 {
61 uint32 qbit;
62 uint32 n;
63
64 if (q < s->seq) {
65 a_warn(service, "replay", "old-sequence", A_END);
66 return (-1);
67 }
68 if (q >= s->seq + SEQ_WINSZ) {
69 n = q - (s->seq + SEQ_WINSZ - 1);
70 if (n < SEQ_WINSZ)
71 s->win >>= n;
72 else
73 s->win = 0;
74 s->seq += n;
75 }
76 qbit = 1 << (q - s->seq);
77 if (s->win & qbit) {
78 a_warn(service, "replay", "duplicated-sequence", A_END);
79 return (-1);
80 }
81 s->win |= qbit;
82 return (0);
83 }
84
85 /*----- Random odds and sods ----------------------------------------------*/
86
87 /* --- @timestr@ --- *
88 *
89 * Arguments: @time_t t@ = a time to convert
90 *
91 * Returns: A pointer to a textual representation of the time.
92 *
93 * Use: Converts a time to a textual representation. Corrupts
94 * @buf_u@.
95 */
96
97 const char *timestr(time_t t)
98 {
99 struct tm *tm;
100 if (!t)
101 return ("NEVER");
102 tm = localtime(&t);
103 strftime((char *)buf_u, sizeof(buf_u), "%Y-%m-%dT%H:%M:%S", tm);
104 return ((const char *)buf_u);
105 }
106
107 /* --- @mystrieq@ --- *
108 *
109 * Arguments: @const char *x, *y@ = two strings
110 *
111 * Returns: True if @x@ and @y are equal, up to case.
112 */
113
114 int mystrieq(const char *x, const char *y)
115 {
116 for (;;) {
117 if (!*x && !*y) return (1);
118 if (tolower((unsigned char)*x) != tolower((unsigned char)*y))
119 return (0);
120 x++; y++;
121 }
122 }
123
124 /*----- Address handling --------------------------------------------------*/
125
126 const struct addrfam aftab[] = {
127 #ifdef HAVE_LIBADNS
128 # define DEF(af, qf) { AF_##af, #af, adns_qf_##qf },
129 #else
130 # define DEF(af, qf) { AF_##af, #af },
131 #endif
132 ADDRFAM(DEF)
133 #undef DEF
134 };
135
136 /* --- @afix@ --- *
137 *
138 * Arguments: @int af@ = an address family code
139 *
140 * Returns: The index of the address family's record in @aftab@, or @-1@.
141 */
142
143 int afix(int af)
144 {
145 int i;
146
147 for (i = 0; i < NADDRFAM; i++)
148 if (af == aftab[i].af) return (i);
149 return (-1);
150 }
151
152 /* --- @addrsz@ --- *
153 *
154 * Arguments: @const addr *a@ = a network address
155 *
156 * Returns: The size of the address, for passing into the sockets API.
157 */
158
159 socklen_t addrsz(const addr *a)
160 {
161 switch (a->sa.sa_family) {
162 case AF_INET: return (sizeof(a->sin));
163 case AF_INET6: return (sizeof(a->sin6));
164 default: abort();
165 }
166 }
167
168 /* --- @getport@, @setport@ --- *
169 *
170 * Arguments: @addr *a@ = a network address
171 * @unsigned port@ = port number to set
172 *
173 * Returns: ---
174 *
175 * Use: Retrieves or sets the port number in an address structure.
176 */
177
178 unsigned getport(addr *a)
179 {
180 switch (a->sa.sa_family) {
181 case AF_INET: return (ntohs(a->sin.sin_port)); break;
182 case AF_INET6: return (ntohs(a->sin6.sin6_port)); break;
183 default: abort();
184 }
185 }
186
187 void setport(addr *a, unsigned port)
188 {
189 switch (a->sa.sa_family) {
190 case AF_INET: a->sin.sin_port = htons(port); break;
191 case AF_INET6: a->sin6.sin6_port = htons(port); break;
192 default: abort();
193 }
194 }
195
196 /*----- That's all, folks -------------------------------------------------*/