1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 1997-2020 ircd-hybrid development team |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
19 |
* USA |
20 |
*/ |
21 |
|
22 |
/*! \file misc.c |
23 |
* \brief Yet another miscellaneous functions file. |
24 |
* \version $Id$ |
25 |
*/ |
26 |
|
27 |
#include "stdinc.h" |
28 |
#include "misc.h" |
29 |
#include "irc_string.h" |
30 |
#include "ircd.h" |
31 |
|
32 |
|
33 |
const char * |
34 |
date(uintmax_t lclock) |
35 |
{ |
36 |
static char buf[80]; |
37 |
static uintmax_t lclock_last; |
38 |
|
39 |
if (lclock == 0) |
40 |
lclock = event_base->time.sec_real; |
41 |
|
42 |
if (lclock_last != lclock) |
43 |
{ |
44 |
lclock_last = lclock; /* Cache value to avoid repetitive strftime() calls. */ |
45 |
strftime(buf, sizeof(buf), "%A %B %-e %Y -- %T %z", localtime((time_t *)&lclock)); |
46 |
} |
47 |
|
48 |
return buf; |
49 |
} |
50 |
|
51 |
const char * |
52 |
date_iso8601(uintmax_t lclock) |
53 |
{ |
54 |
static char buf[MAX_DATE_STRING]; |
55 |
static uintmax_t lclock_last; |
56 |
|
57 |
if (lclock == 0) |
58 |
lclock = event_base->time.sec_real; |
59 |
|
60 |
if (lclock_last != lclock) |
61 |
{ |
62 |
lclock_last = lclock; /* Cache value to avoid repetitive strftime() calls. */ |
63 |
strftime(buf, sizeof(buf), "%FT%T%z", localtime((time_t *)&lclock)); |
64 |
} |
65 |
|
66 |
return buf; |
67 |
} |
68 |
|
69 |
/* |
70 |
* myctime - This is like standard ctime()-function, but it zaps away |
71 |
* the newline from the end of that string. Also, it takes |
72 |
* the time value as parameter, instead of pointer to it. |
73 |
* Note that it is necessary to copy the string to alternate |
74 |
* buffer (who knows how ctime() implements it, maybe it statically |
75 |
* has newline there and never 'refreshes' it -- zapping that |
76 |
* might break things in other places...) |
77 |
* |
78 |
* |
79 |
* Thu Nov 24 18:22:48 1986 |
80 |
*/ |
81 |
const char * |
82 |
date_ctime(uintmax_t lclock) |
83 |
{ |
84 |
static char buf[MAX_DATE_STRING]; |
85 |
static uintmax_t lclock_last; |
86 |
|
87 |
if (lclock == 0) |
88 |
lclock = event_base->time.sec_real; |
89 |
|
90 |
if (lclock_last != lclock) |
91 |
{ |
92 |
lclock_last = lclock; /* Cache value to avoid repetitive strftime() calls. */ |
93 |
strftime(buf, sizeof(buf), "%a %b %-e %T %Y", localtime((time_t *)&lclock)); |
94 |
} |
95 |
|
96 |
return buf; |
97 |
} |
98 |
|
99 |
const char * |
100 |
time_dissect(uintmax_t duration) |
101 |
{ |
102 |
static char buf[32]; /* 32 = sizeof("9999999999999999 days, 23:59:59") */ |
103 |
unsigned int days = 0, hours = 0, minutes = 0, seconds = 0; |
104 |
|
105 |
while (duration >= 60 * 60 * 24) |
106 |
{ |
107 |
duration -= 60 * 60 * 24; |
108 |
++days; |
109 |
} |
110 |
|
111 |
while (duration >= 60 * 60) |
112 |
{ |
113 |
duration -= 60 * 60; |
114 |
++hours; |
115 |
} |
116 |
|
117 |
while (duration >= 60) |
118 |
{ |
119 |
duration -= 60; |
120 |
++minutes; |
121 |
} |
122 |
|
123 |
seconds = duration; |
124 |
|
125 |
snprintf(buf, sizeof(buf), "%u day%s, %02u:%02u:%02u", |
126 |
days, days == 1 ? "" : "s", hours, minutes, seconds); |
127 |
return buf; |
128 |
} |
129 |
|
130 |
void |
131 |
binary_to_hex(const unsigned char *bin, char *hex, unsigned int length) |
132 |
{ |
133 |
static const char trans[] = "0123456789ABCDEF"; |
134 |
|
135 |
for (const unsigned char *const end = bin + length; bin < end; ++bin) |
136 |
{ |
137 |
*hex++ = trans[*bin >> 4]; |
138 |
*hex++ = trans[*bin & 0xf]; |
139 |
} |
140 |
|
141 |
*hex = '\0'; |
142 |
} |
143 |
|
144 |
bool |
145 |
address_compare(const void *p1, |
146 |
const void *p2, bool port) |
147 |
{ |
148 |
const struct irc_ssaddr *const addr1 = p1; |
149 |
const struct irc_ssaddr *const addr2 = p2; |
150 |
|
151 |
if (addr1->ss.ss_family != addr2->ss.ss_family) |
152 |
return false; |
153 |
|
154 |
if (addr1->ss.ss_family == AF_INET) |
155 |
{ |
156 |
const struct sockaddr_in *const sin1 = (const struct sockaddr_in *)addr1; |
157 |
const struct sockaddr_in *const sin2 = (const struct sockaddr_in *)addr2; |
158 |
|
159 |
if (port == true && (sin1->sin_port != sin2->sin_port)) |
160 |
return false; |
161 |
|
162 |
return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr; |
163 |
} |
164 |
else if (addr1->ss.ss_family == AF_INET6) |
165 |
{ |
166 |
const struct sockaddr_in6 *const sin1 = (const struct sockaddr_in6 *)addr1; |
167 |
const struct sockaddr_in6 *const sin2 = (const struct sockaddr_in6 *)addr2; |
168 |
|
169 |
if (port == true && (sin1->sin6_port != sin2->sin6_port)) |
170 |
return false; |
171 |
|
172 |
return memcmp(sin1->sin6_addr.s6_addr, |
173 |
sin2->sin6_addr.s6_addr, sizeof(struct in6_addr)) == 0; |
174 |
} |
175 |
|
176 |
return false; |
177 |
} |