2 utils.c -- gathering of some stupid small functions
3 Copyright (C) 1999-2005 Ivo Timmermans
4 2000-2013 Guus Sliepen <guus@tinc-vpn.org>
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.
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.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 static const char hexadecimals[] = "0123456789ABCDEF";
29 static const char base64_original[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
30 static const char base64_urlsafe[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
31 static const char base64_decode[256] = {
32 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
33 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
34 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, 62, -1, 63,
35 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
36 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
37 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, 63,
38 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
39 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
40 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
41 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
42 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
43 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
44 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
45 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
46 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
47 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
50 static uint8_t charhex2bin(char c) {
51 uint8_t cu = (uint8_t) c;
56 return toupper(cu) - 'A' + 10;
60 size_t hex2bin(const char *src, void *vdst, size_t length) {
64 for(i = 0; i < length && isxdigit((uint8_t) src[i * 2]) && isxdigit((uint8_t) src[i * 2 + 1]); i++) {
65 dst[i] = charhex2bin(src[i * 2]) * 16 + charhex2bin(src[i * 2 + 1]);
71 size_t bin2hex(const void *vsrc, char *dst, size_t length) {
72 const char *src = vsrc;
74 for(size_t i = length; i > 0;) {
76 dst[i * 2 + 1] = hexadecimals[(unsigned char) src[i] & 15];
77 dst[i * 2] = hexadecimals[(unsigned char) src[i] >> 4];
84 char *absolute_path(const char *path) {
86 // Works for nonexistent paths
87 return _fullpath(NULL, path, 0);
94 // If an absolute path was passed, return its copy
99 // Try using realpath. If it fails for any reason
100 // other than that the file was not found, bail out.
101 char *abs = realpath(path, NULL);
103 if(abs || errno != ENOENT) {
107 // Since the file does not exist, we're forced to use a fallback.
108 // Get current working directory and concatenate it with the argument.
111 if(!getcwd(cwd, sizeof(cwd))) {
115 // Remove trailing slash if present since we'll be adding our own
116 size_t cwdlen = strlen(cwd);
118 if(cwdlen && cwd[cwdlen - 1] == '/') {
119 cwd[cwdlen - 1] = '\0';
122 // We don't do any normalization because it's complicated, and the payoff is small.
123 // If user passed something like '.././../foo' — that's their choice; fopen works either way.
124 xasprintf(&abs, "%s/%s", cwd, path);
126 if(strlen(abs) >= PATH_MAX) {
135 size_t b64decode_tinc(const char *src, void *dst, size_t length) {
137 uint32_t triplet = 0;
138 unsigned char *udst = (unsigned char *)dst;
140 for(i = 0; i < length && src[i]; i++) {
141 triplet |= (uint32_t)(base64_decode[src[i] & 0xff] << (6 * (i & 3)));
144 if(triplet & 0xff000000U) {
148 udst[0] = triplet & 0xff;
150 udst[1] = triplet & 0xff;
158 if(triplet & 0xff000000U) {
163 udst[0] = triplet & 0xff;
165 udst[1] = triplet & 0xff;
166 return i / 4 * 3 + 2;
167 } else if((i & 3) == 2) {
168 udst[0] = triplet & 0xff;
169 return i / 4 * 3 + 1;
175 bool is_decimal(const char *str) {
181 char *badchar = NULL;
182 strtol(str, &badchar, 10);
183 return !errno && badchar != str && !*badchar;
186 // itoa() conflicts with a similarly named function under MinGW.
187 char *int_to_str(int num) {
189 xasprintf(&str, "%d", num);
193 static size_t b64encode_tinc_internal(const void *src, char *dst, size_t length, const char *alphabet) {
195 const unsigned char *usrc = (unsigned char *)src;
196 size_t si = length / 3 * 3;
197 size_t di = length / 3 * 4;
201 triplet = usrc[si] | usrc[si + 1] << 8;
202 dst[di] = alphabet[triplet & 63];
204 dst[di + 1] = alphabet[triplet & 63];
206 dst[di + 2] = alphabet[triplet];
213 dst[di] = alphabet[triplet & 63];
215 dst[di + 1] = alphabet[triplet];
229 triplet = usrc[si] | usrc[si + 1] << 8 | usrc[si + 2] << 16;
230 dst[di] = alphabet[triplet & 63];
232 dst[di + 1] = alphabet[triplet & 63];
234 dst[di + 2] = alphabet[triplet & 63];
236 dst[di + 3] = alphabet[triplet];
242 size_t b64encode_tinc(const void *src, char *dst, size_t length) {
243 return b64encode_tinc_internal(src, dst, length, base64_original);
246 size_t b64encode_tinc_urlsafe(const void *src, char *dst, size_t length) {
247 return b64encode_tinc_internal(src, dst, length, base64_urlsafe);
251 const char *winerror(int err) {
252 static char buf[1024], *ptr;
254 ptr = buf + snprintf(buf, sizeof(buf), "(%d) ", err);
256 if(!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
257 NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), ptr, sizeof(buf) - (ptr - buf), NULL)) {
258 strncpy(buf, "(unable to format errormessage)", sizeof(buf));
261 if((ptr = strchr(buf, '\r'))) {
269 bool check_id(const char *id) {
275 if(!isalnum((uint8_t) *id) && *id != '_') {
282 bool check_netname(const char *netname, bool strict) {
283 if(!netname || !*netname || *netname == '.') {
287 for(const char *c = netname; *c; c++) {
288 if(iscntrl((uint8_t) *c)) {
292 if(*c == '/' || *c == '\\') {
296 if(strict && strchr(" $%<>:`\"|?*", *c)) {
304 /* Windows doesn't define HOST_NAME_MAX. */
305 #ifndef HOST_NAME_MAX
306 #define HOST_NAME_MAX 255
309 char *replace_name(const char *name) {
313 char *envname = getenv(name + 1);
314 char hostname[HOST_NAME_MAX + 1];
317 if(strcmp(name + 1, "HOST")) {
318 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid Name: environment variable %s does not exist\n", name + 1);
322 if(gethostname(hostname, sizeof(hostname)) || !*hostname) {
323 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get hostname: %s\n", sockstrerror(sockerrno));
327 hostname[HOST_NAME_MAX] = 0;
331 ret_name = xstrdup(envname);
333 for(char *c = ret_name; *c; c++)
334 if(!isalnum((uint8_t) *c)) {
338 ret_name = xstrdup(name);
341 if(!check_id(ret_name)) {
342 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid name for myself!");
350 /* Open a file with the desired permissions, minus the umask.
351 Also, if we want to create an executable file, we call fchmod()
352 to set the executable bits. */
354 FILE *fopenmask(const char *filename, const char *mode, mode_t perms) {
355 mode_t mask = umask(0);
357 umask(~perms & 0777);
358 FILE *f = fopen(filename, mode);
361 fprintf(stderr, "Could not open %s: %s\n", filename, strerror(errno));
368 fchmod(fileno(f), perms);
376 bool string_eq(const char *first, const char *second) {
377 return !first == !second &&
378 !(first && second && strcmp(first, second));