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>
44 void tr_bin_to_hex(const unsigned char * bin, size_t bin_len,
45 char * hex_out, size_t hex_len)
47 assert(hex_len >= 2*bin_len);
49 snprintf(hex_out, hex_len, "%.2x", bin[0]);
57 * Compare two timespecs
59 * Assumes tv_nsec <= 1e9
63 * @return 0 if ts1==ts2, -1 if ts1<ts2, 1 if ts1>ts2.
65 int tr_cmp_timespec(const struct timespec *ts1, const struct timespec *ts2)
67 if (ts1->tv_sec > ts2->tv_sec)
70 if (ts1->tv_sec < ts2->tv_sec)
73 /* ts1->tv_sec==ts2->tv_sec */
75 if (ts1->tv_nsec > ts2->tv_nsec)
78 if (ts1->tv_nsec < ts2->tv_nsec)
89 * @param sum ts1 + ts2
90 * @return 0 on success, nonzero on error
92 int tr_add_timespec(const struct timespec *ts1, const struct timespec *ts2, struct timespec *sum)
94 const time_t ONE_BILLION = 1000000000;
96 if (!ts1 || !ts2 || !sum)
99 /* would be nice to do range checking, but I don't know a portable way to get the
100 * max value of a time_t. Figure that nsec <= 1e9 and seconds are unlikely to go off
101 * too close to infinity, so overflow is unlikely */
102 sum->tv_nsec = ts1->tv_nsec + ts2->tv_nsec;
103 sum->tv_sec = ts1->tv_sec + ts2->tv_sec;
105 /* make sure that we have no more than a second worth of nsec */
106 while (sum->tv_nsec >= ONE_BILLION) {
107 sum->tv_nsec -= ONE_BILLION;
117 * Allows negative results, which will be represented as a negative tv_sec.
118 * The returned tv_nsec is always positive and less than 1e9.
120 * (The value represented is tv_sec + tv_nsec/1e9 seconds - this is a little
121 * counterintuitive when tv_sec is negative. E.g., -1.5 seconds is represented
122 * as {-2, 500000000). Negative time_t values are not guaranteed to be supported
123 * anyway, so it's probably best to stay away from them.)
127 * @param diff ts1 - ts2
128 * @return 0 on success, nonzero on error
130 int tr_sub_timespec(const struct timespec *ts1, const struct timespec *ts2, struct timespec *diff)
132 const time_t ONE_BILLION = 1000000000;
133 struct timespec ts1_copy = {0};
134 struct timespec ts2_copy = {0};
136 if (!ts1 || !ts2 || !diff)
142 while (ts2_copy.tv_nsec > ts1_copy.tv_nsec) {
143 /* Reduce ts2 by one second worth of nsec, balanced by removing a second
144 * from ts1. Repeat until ts2->tv_nsec <= ts1->tv_nsec. */
145 ts2_copy.tv_nsec -= ONE_BILLION;
146 ts1_copy.tv_sec -= 1;
149 diff->tv_nsec = ts1_copy.tv_nsec - ts2_copy.tv_nsec; /* >= 0 */
150 diff->tv_sec = ts1_copy.tv_sec - ts2_copy.tv_sec; /* sign indeterminate */
152 /* make sure we have no more than 1 sec worth of nsec */
153 while (diff->tv_nsec > ONE_BILLION) {
154 diff->tv_nsec -= ONE_BILLION;
162 * Convert a struct timespec to a string representation
166 char *timespec_to_str(const struct timespec *ts)
171 if (gmtime_r(&(ts->tv_sec), &tm)==NULL)
174 s=malloc(40); /* long enough to contain strftime result */
178 if (strftime(s, 40, "%F %T UTC", &tm)==0) {
186 * Convert a time from one clock to another
188 * Because this involves reading each clock, it is not exact.
190 * @param from clock to convert from
191 * @param when time to convert, measured on the 'from' clock
192 * @param to clock to convert to
193 * @param dst destination, measured on the 'to' clock
194 * @return dst or null on error
196 struct timespec *tr_clock_convert(clockid_t from, const struct timespec *when,
197 clockid_t to, struct timespec *dst)
199 struct timespec now_from = {0};
200 struct timespec diff = {0}; /* difference between when and now_from */
201 struct timespec now_to = {0};
203 if ((clock_gettime(from, &now_from) != 0)
204 || (clock_gettime(to, &now_to) != 0)) {
207 if (tr_sub_timespec(when, &now_from, &diff) != 0) {
210 if (tr_add_timespec(&now_to, &diff, dst) != 0) {
216 TR_NAME *tr_parse_hostname(const char *s)
227 colon = strchr(s, ':');
229 return tr_new_name(s); /* there was no colon, take the whole string */
231 /* make a copy of the hostname portion of the string */
232 hostname_len = colon - s;
233 hostname = malloc(hostname_len + 1); /* +1 for the null termination */
234 if (hostname == NULL)
237 /* copy up to the colon, add a null termination, and make a TR_NAME */
238 strncpy(hostname, s, hostname_len);
239 hostname[hostname_len] = '\0';
240 retval = tr_new_name(hostname);
242 /* clean up and return */
248 * Parse the port from a hostname:port string
250 * @param s string to parse
251 * @return the specified port, 0 if none specified, -1 if invalid
253 int tr_parse_port(const char *s)
256 char *end_of_conversion;
257 long int port; /* long instead of int because we use strtol */
259 /* Find the first colon */
260 s_port = strchr(s, ':'); /* port starts at s_port + 1 */
262 return 0; /* no port */
264 /* Check that the last colon is the same as the first */
265 if (strrchr(s, ':') != s_port)
266 return -1; /* multiple colons are invalid*/
268 s_port += 1; /* port now starts at s_port */
270 /* Parse the port number */
271 port = strtol(s, &end_of_conversion, /* base */ 10);
274 if ((end_of_conversion == s_port) /* there was no port, just a colon */
275 || (*end_of_conversion != '\0') /* did not reach the end of the string */
276 || (port <= 0) || (port > 65535)) {
284 * Parse hostname and port
289 * @return 0 on success, -1 on error
291 int tr_parse_hostname_and_port(const char *s, TR_NAME **hn_dest, int *p_dest)
293 if ((hn_dest == NULL) || (p_dest == NULL))
296 *hn_dest = tr_parse_hostname(s);
297 if (*hn_dest == NULL)
300 *p_dest = tr_parse_port(s);
301 if ((*p_dest < 0) || (*p_dest > 65535)) {
302 tr_free_name(*hn_dest);
310 TR_NAME *tr_hostname_and_port_to_name(TR_NAME *hn, int port)
312 TR_NAME *retval = NULL;
314 char *hn_s = tr_name_strdup(hn);
319 s = talloc_asprintf(NULL, "%s:%d", hn_s, port);
323 retval = tr_new_name(s);