2 * Copyright (c) 2012, 2013, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <tr_name_internal.h>
43 void tr_bin_to_hex(const unsigned char * bin, size_t bin_len,
44 char * hex_out, size_t hex_len)
46 assert(hex_len >= 2*bin_len);
48 snprintf(hex_out, hex_len, "%.2x", bin[0]);
56 * Compare two timespecs
58 * Assumes tv_nsec <= 1e9
62 * @return 0 if ts1==ts2, -1 if ts1<ts2, 1 if ts1>ts2.
64 int tr_cmp_timespec(const struct timespec *ts1, const struct timespec *ts2)
66 if (ts1->tv_sec > ts2->tv_sec)
69 if (ts1->tv_sec < ts2->tv_sec)
72 /* ts1->tv_sec==ts2->tv_sec */
74 if (ts1->tv_nsec > ts2->tv_nsec)
77 if (ts1->tv_nsec < ts2->tv_nsec)
88 * @param sum ts1 + ts2
89 * @return 0 on success, nonzero on error
91 int tr_add_timespec(const struct timespec *ts1, const struct timespec *ts2, struct timespec *sum)
93 const time_t ONE_BILLION = 1000000000;
95 if (!ts1 || !ts2 || !sum)
98 /* would be nice to do range checking, but I don't know a portable way to get the
99 * max value of a time_t. Figure that nsec <= 1e9 and seconds are unlikely to go off
100 * too close to infinity, so overflow is unlikely */
101 sum->tv_nsec = ts1->tv_nsec + ts2->tv_nsec;
102 sum->tv_sec = ts1->tv_sec + ts2->tv_sec;
104 /* make sure that we have no more than a second worth of nsec */
105 while (sum->tv_nsec >= ONE_BILLION) {
106 sum->tv_nsec -= ONE_BILLION;
116 * Allows negative results, which will be represented as a negative tv_sec.
117 * The returned tv_nsec is always positive and less than 1e9.
119 * (The value represented is tv_sec + tv_nsec/1e9 seconds - this is a little
120 * counterintuitive when tv_sec is negative. E.g., -1.5 seconds is represented
121 * as {-2, 500000000). Negative time_t values are not guaranteed to be supported
122 * anyway, so it's probably best to stay away from them.)
126 * @param diff ts1 - ts2
127 * @return 0 on success, nonzero on error
129 int tr_sub_timespec(const struct timespec *ts1, const struct timespec *ts2, struct timespec *diff)
131 const time_t ONE_BILLION = 1000000000;
132 struct timespec ts1_copy = {0};
133 struct timespec ts2_copy = {0};
135 if (!ts1 || !ts2 || !diff)
141 while (ts2_copy.tv_nsec > ts1_copy.tv_nsec) {
142 /* Reduce ts2 by one second worth of nsec, balanced by removing a second
143 * from ts1. Repeat until ts2->tv_nsec <= ts1->tv_nsec. */
144 ts2_copy.tv_nsec -= ONE_BILLION;
145 ts1_copy.tv_sec -= 1;
148 diff->tv_nsec = ts1_copy.tv_nsec - ts2_copy.tv_nsec; /* >= 0 */
149 diff->tv_sec = ts1_copy.tv_sec - ts2_copy.tv_sec; /* sign indeterminate */
151 /* make sure we have no more than 1 sec worth of nsec */
152 while (diff->tv_nsec > ONE_BILLION) {
153 diff->tv_nsec -= ONE_BILLION;
161 * Convert a struct timespec to a string representation
165 char *timespec_to_str(const struct timespec *ts)
170 if (gmtime_r(&(ts->tv_sec), &tm)==NULL)
173 s=malloc(40); /* long enough to contain strftime result */
177 if (strftime(s, 40, "%F %T UTC", &tm)==0) {
185 * Convert a time from one clock to another
187 * Because this involves reading each clock, it is not exact.
189 * @param from clock to convert from
190 * @param when time to convert, measured on the 'from' clock
191 * @param to clock to convert to
192 * @param dst destination, measured on the 'to' clock
193 * @return dst or null on error
195 struct timespec *tr_clock_convert(clockid_t from, const struct timespec *when,
196 clockid_t to, struct timespec *dst)
198 struct timespec now_from = {0};
199 struct timespec diff = {0}; /* difference between when and now_from */
200 struct timespec now_to = {0};
202 if ((clock_gettime(from, &now_from) != 0)
203 || (clock_gettime(to, &now_to) != 0)) {
206 if (tr_sub_timespec(when, &now_from, &diff) != 0) {
209 if (tr_add_timespec(&now_to, &diff, dst) != 0) {
215 TR_NAME *tr_parse_hostname(const char *s)
226 colon = strchr(s, ':');
228 return tr_new_name(s); /* there was no colon, take the whole string */
230 /* make a copy of the hostname portion of the string */
231 hostname_len = colon - s;
232 hostname = malloc(hostname_len + 1); /* +1 for the null termination */
233 if (hostname == NULL)
236 /* copy up to the colon, add a null termination, and make a TR_NAME */
237 strncpy(hostname, s, hostname_len);
238 hostname[hostname_len] = '\0';
239 retval = tr_new_name(hostname);
241 /* clean up and return */
247 * Parse the port from a hostname:port string
249 * @param s string to parse
250 * @return the specified port, 0 if none specified, -1 if invalid
252 int tr_parse_port(const char *s)
255 char *end_of_conversion;
256 long int port; /* long instead of int because we use strtol */
258 /* Find the first colon */
259 s_port = strchr(s, ':'); /* port starts at s_port + 1 */
261 return 0; /* no port */
263 /* Check that the last colon is the same as the first */
264 if (strrchr(s, ':') != s_port)
265 return -1; /* multiple colons are invalid*/
267 s_port += 1; /* port now starts at s_port */
269 /* Parse the port number */
270 port = strtol(s, &end_of_conversion, /* base */ 10);
273 if ((end_of_conversion == s_port) /* there was no port, just a colon */
274 || (*end_of_conversion != '\0') /* did not reach the end of the string */
275 || (port <= 0) || (port > 65535)) {
283 * Parse hostname and port
288 * @return 0 on success, -1 on error
290 int tr_parse_hostname_and_port(const char *s, TR_NAME **hn_dest, int *p_dest)
292 if ((hn_dest == NULL) || (p_dest == NULL))
295 *hn_dest = tr_parse_hostname(s);
296 if (*hn_dest == NULL)
299 *p_dest = tr_parse_port(s);
300 if ((*p_dest < 0) || (*p_dest > 65535)) {
301 tr_free_name(*hn_dest);