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 size_t b64decode_tinc(const char *src, void *dst, size_t length) {
87 unsigned char *udst = (unsigned char *)dst;
89 for(i = 0; i < length && src[i]; i++) {
90 triplet |= base64_decode[src[i] & 0xff] << (6 * (i & 3));
93 if(triplet & 0xff000000U) {
97 udst[0] = triplet & 0xff;
99 udst[1] = triplet & 0xff;
107 if(triplet & 0xff000000U) {
112 udst[0] = triplet & 0xff;
114 udst[1] = triplet & 0xff;
115 return i / 4 * 3 + 2;
116 } else if((i & 3) == 2) {
117 udst[0] = triplet & 0xff;
118 return i / 4 * 3 + 1;
124 bool is_decimal(const char *str) {
130 char *badchar = NULL;
131 strtol(str, &badchar, 10);
132 return !errno && badchar != str && !*badchar;
135 // itoa() conflicts with a similarly named function under MinGW.
136 char *int_to_str(int num) {
138 xasprintf(&str, "%d", num);
142 static size_t b64encode_tinc_internal(const void *src, char *dst, size_t length, const char *alphabet) {
144 const unsigned char *usrc = (unsigned char *)src;
145 size_t si = length / 3 * 3;
146 size_t di = length / 3 * 4;
150 triplet = usrc[si] | usrc[si + 1] << 8;
151 dst[di] = alphabet[triplet & 63];
153 dst[di + 1] = alphabet[triplet & 63];
155 dst[di + 2] = alphabet[triplet];
162 dst[di] = alphabet[triplet & 63];
164 dst[di + 1] = alphabet[triplet];
178 triplet = usrc[si] | usrc[si + 1] << 8 | usrc[si + 2] << 16;
179 dst[di] = alphabet[triplet & 63];
181 dst[di + 1] = alphabet[triplet & 63];
183 dst[di + 2] = alphabet[triplet & 63];
185 dst[di + 3] = alphabet[triplet];
191 size_t b64encode_tinc(const void *src, char *dst, size_t length) {
192 return b64encode_tinc_internal(src, dst, length, base64_original);
195 size_t b64encode_tinc_urlsafe(const void *src, char *dst, size_t length) {
196 return b64encode_tinc_internal(src, dst, length, base64_urlsafe);
200 const char *winerror(int err) {
201 static char buf[1024], *ptr;
203 ptr = buf + snprintf(buf, sizeof(buf), "(%d) ", err);
205 if(!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
206 NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), ptr, sizeof(buf) - (ptr - buf), NULL)) {
207 strncpy(buf, "(unable to format errormessage)", sizeof(buf));
210 if((ptr = strchr(buf, '\r'))) {
218 bool check_id(const char *id) {
224 if(!isalnum((uint8_t) *id) && *id != '_') {
231 bool check_netname(const char *netname, bool strict) {
232 if(!netname || !*netname || *netname == '.') {
236 for(const char *c = netname; *c; c++) {
237 if(iscntrl((uint8_t) *c)) {
241 if(*c == '/' || *c == '\\') {
245 if(strict && strchr(" $%<>:`\"|?*", *c)) {
253 /* Windows doesn't define HOST_NAME_MAX. */
254 #ifndef HOST_NAME_MAX
255 #define HOST_NAME_MAX 255
258 char *replace_name(const char *name) {
262 char *envname = getenv(name + 1);
263 char hostname[HOST_NAME_MAX + 1];
266 if(strcmp(name + 1, "HOST")) {
267 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid Name: environment variable %s does not exist\n", name + 1);
271 if(gethostname(hostname, sizeof(hostname)) || !*hostname) {
272 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get hostname: %s\n", sockstrerror(sockerrno));
276 hostname[HOST_NAME_MAX] = 0;
280 ret_name = xstrdup(envname);
282 for(char *c = ret_name; *c; c++)
283 if(!isalnum((uint8_t) *c)) {
287 ret_name = xstrdup(name);
290 if(!check_id(ret_name)) {
291 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid name for myself!");
299 /* Open a file with the desired permissions, minus the umask.
300 Also, if we want to create an executable file, we call fchmod()
301 to set the executable bits. */
303 FILE *fopenmask(const char *filename, const char *mode, mode_t perms) {
304 mode_t mask = umask(0);
306 umask(~perms & 0777);
307 FILE *f = fopen(filename, mode);
310 fprintf(stderr, "Could not open %s: %s\n", filename, strerror(errno));
317 fchmod(fileno(f), perms);