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 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.
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
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/>.
26 /*----- Header files ------------------------------------------------------*/
30 /*----- Global variables --------------------------------------------------*/
32 octet buf_i
[PKBUFSZ
], buf_o
[PKBUFSZ
], buf_t
[PKBUFSZ
], buf_u
[PKBUFSZ
];
34 /*----- Sequence numbers --------------------------------------------------*/
36 /* --- @seq_reset@ --- *
38 * Arguments: @seqwin *s@ = sequence-checking window
42 * Use: Resets a sequence number window.
45 void seq_reset(seqwin
*s
) { s
->seq
= 0; s
->win
= 0; }
47 /* --- @seq_check@ --- *
49 * Arguments: @seqwin *s@ = sequence-checking window
50 * @uint32 q@ = sequence number to check
51 * @const char *service@ = service to report message from
53 * Returns: Zero on success, nonzero if the sequence number was bad.
55 * Use: Checks a sequence number against the window, updating things
59 int seq_check(seqwin
*s
, uint32 q
, const char *service
)
65 a_warn(service
, "replay", "old-sequence", A_END
);
68 if (q
>= s
->seq
+ SEQ_WINSZ
) {
69 n
= q
- (s
->seq
+ SEQ_WINSZ
- 1);
76 qbit
= 1 << (q
- s
->seq
);
78 a_warn(service
, "replay", "duplicated-sequence", A_END
);
85 /*----- Random odds and sods ----------------------------------------------*/
87 /* --- @timestr@ --- *
89 * Arguments: @time_t t@ = a time to convert
91 * Returns: A pointer to a textual representation of the time.
93 * Use: Converts a time to a textual representation. Corrupts
97 const char *timestr(time_t t
)
103 strftime((char *)buf_u
, sizeof(buf_u
), "%Y-%m-%dT%H:%M:%S", tm
);
104 return ((const char *)buf_u
);
107 /* --- @mystrieq@ --- *
109 * Arguments: @const char *x, *y@ = two strings
111 * Returns: True if @x@ and @y are equal, up to case.
114 int mystrieq(const char *x
, const char *y
)
117 if (!*x
&& !*y
) return (1);
118 if (tolower((unsigned char)*x
) != tolower((unsigned char)*y
))
124 /*----- Address handling --------------------------------------------------*/
126 const struct addrfam aftab
[] = {
127 #define DEF(af) { AF_##af, #af },
134 * Arguments: @int af@ = an address family code
136 * Returns: The index of the address family's record in @aftab@, or @-1@.
143 for (i
= 0; i
< NADDRFAM
; i
++)
144 if (af
== aftab
[i
].af
) return (i
);
148 /* --- @addrsz@ --- *
150 * Arguments: @const addr *a@ = a network address
152 * Returns: The size of the address, for passing into the sockets API.
155 socklen_t
addrsz(const addr
*a
)
157 switch (a
->sa
.sa_family
) {
158 case AF_INET
: return (sizeof(a
->sin
));
163 /* --- @getport@, @setport@ --- *
165 * Arguments: @addr *a@ = a network address
166 * @unsigned port@ = port number to set
170 * Use: Retrieves or sets the port number in an address structure.
173 unsigned getport(addr
*a
)
175 switch (a
->sa
.sa_family
) {
176 case AF_INET
: return (ntohs(a
->sin
.sin_port
)); break;
181 void setport(addr
*a
, unsigned port
)
183 switch (a
->sa
.sa_family
) {
184 case AF_INET
: a
->sin
.sin_port
= htons(port
); break;
189 /*----- That's all, folks -------------------------------------------------*/