Support non-default TRP and TID ports
[trust_router.git] / common / tr_util.c
1 /*
2  * Copyright (c) 2012, 2013, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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.
32  *
33  */
34
35 #include <assert.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <time.h>
39 #include <tr_name_internal.h>
40 #include <tr_util.h>
41 #include <stdlib.h>
42 #include <talloc.h>
43
44 void tr_bin_to_hex(const unsigned char * bin, size_t bin_len,
45                    char * hex_out, size_t hex_len)
46 {
47   assert(hex_len >= 2*bin_len);
48   while (bin_len >0) {
49     snprintf(hex_out, hex_len, "%.2x", bin[0]);
50     bin++, hex_out += 2;
51     bin_len--;
52     hex_len -= 2;
53   }
54 }
55
56 /**
57  * Compare two timespecs
58  *
59  * Assumes tv_nsec <= 1e9
60  *
61  * @param ts1
62  * @param ts2
63  * @return 0 if ts1==ts2, -1 if ts1<ts2, 1 if ts1>ts2.
64  */
65 int tr_cmp_timespec(const struct timespec *ts1, const struct timespec *ts2)
66 {
67   if (ts1->tv_sec > ts2->tv_sec)
68     return 1;
69
70   if (ts1->tv_sec < ts2->tv_sec)
71     return -1;
72
73   /* ts1->tv_sec==ts2->tv_sec */
74
75   if (ts1->tv_nsec > ts2->tv_nsec)
76     return 1;
77
78   if (ts1->tv_nsec < ts2->tv_nsec)
79     return -1;
80
81   return 0;
82 }
83
84 /**
85  * Compute ts1 + ts2
86  *
87  * @param ts1
88  * @param ts2
89  * @param sum ts1 + ts2
90  * @return 0 on success, nonzero on error
91  */
92 int tr_add_timespec(const struct timespec *ts1, const struct timespec *ts2, struct timespec *sum)
93 {
94   const time_t ONE_BILLION = 1000000000;
95
96   if (!ts1 || !ts2 || !sum)
97     return -1;
98
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;
104
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;
108     sum->tv_sec += 1;
109   }
110
111   return 0;
112 }
113
114 /**
115  * Compute ts1 - ts2
116  *
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.
119  *
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.)
124  *
125  * @param ts1
126  * @param ts2
127  * @param diff ts1 - ts2
128  * @return 0 on success, nonzero on error
129  */
130 int tr_sub_timespec(const struct timespec *ts1, const struct timespec *ts2, struct timespec *diff)
131 {
132   const time_t ONE_BILLION = 1000000000;
133   struct timespec ts1_copy = {0};
134   struct timespec ts2_copy = {0};
135   
136   if (!ts1 || !ts2 || !diff)
137     return -1;
138
139   ts1_copy = *ts1;
140   ts2_copy = *ts2;
141   
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;
147   }
148
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 */
151
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;
155     diff->tv_sec += 1;
156   }
157
158   return 0;
159 }
160
161 /**
162  * Convert a struct timespec to a string representation
163  * @param ts
164  * @return
165  */
166 char *timespec_to_str(const struct timespec *ts)
167 {
168   struct tm tm;
169   char *s=NULL;
170
171   if (gmtime_r(&(ts->tv_sec), &tm)==NULL)
172     return NULL;
173
174   s=malloc(40); /* long enough to contain strftime result */
175   if (s==NULL)
176     return NULL;
177
178   if (strftime(s, 40, "%F %T UTC", &tm)==0) {
179     free(s);
180     return NULL;
181   }
182   return s;
183 }
184
185 /**
186  * Convert a time from one clock to another
187  *
188  * Because this involves reading each clock, it is not exact.
189  *
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
195  */
196 struct timespec *tr_clock_convert(clockid_t from, const struct timespec *when,
197                                   clockid_t to, struct timespec *dst)
198 {
199   struct timespec now_from = {0};
200   struct timespec diff = {0}; /* difference between when and now_from */
201   struct timespec now_to = {0};
202
203   if ((clock_gettime(from, &now_from) != 0)
204       || (clock_gettime(to, &now_to) != 0)) {
205     return NULL;
206   }
207   if (tr_sub_timespec(when, &now_from, &diff) != 0) {
208     return NULL;
209   }
210   if (tr_add_timespec(&now_to, &diff, dst) != 0) {
211     return NULL;
212   }
213   return dst;
214 }
215
216 TR_NAME *tr_parse_hostname(const char *s)
217 {
218   const char *colon;
219   char *hostname;
220   size_t hostname_len;
221   TR_NAME *retval;
222
223   if (s == NULL)
224     return NULL;
225
226   /* find the colon */
227   colon = strchr(s, ':');
228   if (colon == NULL)
229     return tr_new_name(s); /* there was no colon, take the whole string */
230
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)
235     return NULL;
236
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);
241
242   /* clean up and return */
243   free(hostname);
244   return retval;
245 }
246
247 /**
248  * Parse the port from a hostname:port string
249  *
250  * @param s string to parse
251  * @return the specified port, 0 if none specified, -1 if invalid
252  */
253 int tr_parse_port(const char *s)
254 {
255   const char *s_port;
256   char *end_of_conversion;
257   long int port; /* long instead of int because we use strtol */
258
259   /* Find the first colon */
260   s_port = strchr(s, ':'); /* port starts at s_port + 1 */
261   if (s_port == NULL)
262     return 0; /* no port */
263
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*/
267
268   s_port += 1; /* port now starts at s_port */
269
270   /* Parse the port number */
271   port = strtol(s, &end_of_conversion, /* base */ 10);
272
273   /* validate */
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)) {
277     return -1;
278   }
279
280   return (int) port;
281 }
282
283 /**
284  * Parse hostname and port
285  *
286  * @param s
287  * @param hn_dest
288  * @param p_dest
289  * @return 0 on success, -1 on error
290  */
291 int tr_parse_hostname_and_port(const char *s, TR_NAME **hn_dest, int *p_dest)
292 {
293   if ((hn_dest == NULL) || (p_dest == NULL))
294     return -1;
295
296   *hn_dest = tr_parse_hostname(s);
297   if (*hn_dest == NULL)
298     return -1;
299
300   *p_dest = tr_parse_port(s);
301   if ((*p_dest < 0) || (*p_dest > 65535)) {
302     tr_free_name(*hn_dest);
303     *hn_dest = NULL;
304     return -1;
305   }
306
307   return 0;
308 }
309
310 TR_NAME *tr_hostname_and_port_to_name(TR_NAME *hn, int port)
311 {
312   TR_NAME *retval = NULL;
313   char *s = NULL;
314   char *hn_s = tr_name_strdup(hn);
315
316   if (!hn_s)
317     return NULL;
318
319   s = talloc_asprintf(NULL, "%s:%d", hn_s, port);
320   free(hn_s);
321
322   if (s) {
323     retval = tr_new_name(s);
324     talloc_free(s);
325   }
326
327   return retval;
328 }