Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * General purpose functions. |
| 3 | * |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 4 | * Copyright 2000-2007 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Willy Tarreau | 2e74c3f | 2007-12-02 18:45:09 +0100 | [diff] [blame] | 13 | #include <ctype.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 14 | #include <netdb.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <string.h> |
Willy Tarreau | 127f966 | 2007-12-06 00:53:51 +0100 | [diff] [blame] | 17 | #include <sys/socket.h> |
| 18 | #include <sys/un.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 19 | #include <netinet/in.h> |
| 20 | #include <arpa/inet.h> |
| 21 | |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 22 | #include <common/config.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 23 | #include <common/standard.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 24 | #include <proto/log.h> |
| 25 | |
Willy Tarreau | 72d759c | 2007-10-25 12:14:10 +0200 | [diff] [blame] | 26 | /* enough to store 10 integers of : |
| 27 | * 2^64-1 = 18446744073709551615 or |
| 28 | * -2^63 = -9223372036854775808 |
Willy Tarreau | e7239b5 | 2009-03-29 13:41:58 +0200 | [diff] [blame] | 29 | * |
| 30 | * The HTML version needs room for adding the 25 characters |
| 31 | * '<span class="rls"></span>' around digits at positions 3N+1 in order |
| 32 | * to add spacing at up to 6 positions : 18 446 744 073 709 551 615 |
Willy Tarreau | 72d759c | 2007-10-25 12:14:10 +0200 | [diff] [blame] | 33 | */ |
Willy Tarreau | e7239b5 | 2009-03-29 13:41:58 +0200 | [diff] [blame] | 34 | char itoa_str[10][171]; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * copies at most <size-1> chars from <src> to <dst>. Last char is always |
| 38 | * set to 0, unless <size> is 0. The number of chars copied is returned |
| 39 | * (excluding the terminating zero). |
| 40 | * This code has been optimized for size and speed : on x86, it's 45 bytes |
| 41 | * long, uses only registers, and consumes only 4 cycles per char. |
| 42 | */ |
| 43 | int strlcpy2(char *dst, const char *src, int size) |
| 44 | { |
| 45 | char *orig = dst; |
| 46 | if (size) { |
| 47 | while (--size && (*dst = *src)) { |
| 48 | src++; dst++; |
| 49 | } |
| 50 | *dst = 0; |
| 51 | } |
| 52 | return dst - orig; |
| 53 | } |
| 54 | |
| 55 | /* |
Willy Tarreau | 72d759c | 2007-10-25 12:14:10 +0200 | [diff] [blame] | 56 | * This function simply returns a locally allocated string containing |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 57 | * the ascii representation for number 'n' in decimal. |
| 58 | */ |
Willy Tarreau | 72d759c | 2007-10-25 12:14:10 +0200 | [diff] [blame] | 59 | const char *ultoa_r(unsigned long n, char *buffer, int size) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 60 | { |
| 61 | char *pos; |
| 62 | |
Willy Tarreau | 72d759c | 2007-10-25 12:14:10 +0200 | [diff] [blame] | 63 | pos = buffer + size - 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 64 | *pos-- = '\0'; |
| 65 | |
| 66 | do { |
| 67 | *pos-- = '0' + n % 10; |
| 68 | n /= 10; |
Willy Tarreau | 72d759c | 2007-10-25 12:14:10 +0200 | [diff] [blame] | 69 | } while (n && pos >= buffer); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 70 | return pos + 1; |
| 71 | } |
| 72 | |
Willy Tarreau | 91092e5 | 2007-10-25 16:58:42 +0200 | [diff] [blame] | 73 | /* |
Willy Tarreau | e7239b5 | 2009-03-29 13:41:58 +0200 | [diff] [blame] | 74 | * This function simply returns a locally allocated string containing |
| 75 | * the ascii representation for number 'n' in decimal, formatted for |
| 76 | * HTML output with tags to create visual grouping by 3 digits. The |
| 77 | * output needs to support at least 171 characters. |
| 78 | */ |
| 79 | const char *ulltoh_r(unsigned long long n, char *buffer, int size) |
| 80 | { |
| 81 | char *start; |
| 82 | int digit = 0; |
| 83 | |
| 84 | start = buffer + size; |
| 85 | *--start = '\0'; |
| 86 | |
| 87 | do { |
| 88 | if (digit == 3 && start >= buffer + 7) |
| 89 | memcpy(start -= 7, "</span>", 7); |
| 90 | |
| 91 | if (start >= buffer + 1) { |
| 92 | *--start = '0' + n % 10; |
| 93 | n /= 10; |
| 94 | } |
| 95 | |
| 96 | if (digit == 3 && start >= buffer + 18) |
| 97 | memcpy(start -= 18, "<span class=\"rls\">", 18); |
| 98 | |
| 99 | if (digit++ == 3) |
| 100 | digit = 1; |
| 101 | } while (n && start > buffer); |
| 102 | return start; |
| 103 | } |
| 104 | |
| 105 | /* |
Willy Tarreau | 91092e5 | 2007-10-25 16:58:42 +0200 | [diff] [blame] | 106 | * This function simply returns a locally allocated string containing the ascii |
| 107 | * representation for number 'n' in decimal, unless n is 0 in which case it |
| 108 | * returns the alternate string (or an empty string if the alternate string is |
| 109 | * NULL). It use is intended for limits reported in reports, where it's |
| 110 | * desirable not to display anything if there is no limit. Warning! it shares |
| 111 | * the same vector as ultoa_r(). |
| 112 | */ |
| 113 | const char *limit_r(unsigned long n, char *buffer, int size, const char *alt) |
| 114 | { |
| 115 | return (n) ? ultoa_r(n, buffer, size) : (alt ? alt : ""); |
| 116 | } |
| 117 | |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 118 | /* |
| 119 | * converts <str> to a struct sockaddr_un* which is locally allocated. |
| 120 | * The format is "/path", where "/path" is a path to a UNIX domain socket. |
| 121 | */ |
Willy Tarreau | caf720d | 2008-03-07 10:07:04 +0100 | [diff] [blame] | 122 | struct sockaddr_un *str2sun(const char *str) |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 123 | { |
Willy Tarreau | 127f966 | 2007-12-06 00:53:51 +0100 | [diff] [blame] | 124 | static struct sockaddr_un su; |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 125 | int strsz; /* length included null */ |
| 126 | |
Willy Tarreau | 127f966 | 2007-12-06 00:53:51 +0100 | [diff] [blame] | 127 | memset(&su, 0, sizeof(su)); |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 128 | strsz = strlen(str) + 1; |
Willy Tarreau | 127f966 | 2007-12-06 00:53:51 +0100 | [diff] [blame] | 129 | if (strsz > sizeof(su.sun_path)) { |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 130 | Alert("Socket path '%s' too long (max %d)\n", |
Willy Tarreau | 5e4a6f1 | 2009-04-11 19:42:49 +0200 | [diff] [blame^] | 131 | str, (int)sizeof(su.sun_path) - 1); |
Willy Tarreau | caf720d | 2008-03-07 10:07:04 +0100 | [diff] [blame] | 132 | } else { |
| 133 | su.sun_family = AF_UNIX; |
| 134 | memcpy(su.sun_path, str, strsz); |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 135 | } |
Willy Tarreau | 127f966 | 2007-12-06 00:53:51 +0100 | [diff] [blame] | 136 | return &su; |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 137 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 138 | |
| 139 | /* |
| 140 | * Returns non-zero if character <s> is a hex digit (0-9, a-f, A-F), else zero. |
| 141 | * |
| 142 | * It looks like this one would be a good candidate for inlining, but this is |
| 143 | * not interesting because it around 35 bytes long and often called multiple |
| 144 | * times within the same function. |
| 145 | */ |
| 146 | int ishex(char s) |
| 147 | { |
| 148 | s -= '0'; |
| 149 | if ((unsigned char)s <= 9) |
| 150 | return 1; |
| 151 | s -= 'A' - '0'; |
| 152 | if ((unsigned char)s <= 5) |
| 153 | return 1; |
| 154 | s -= 'a' - 'A'; |
| 155 | if ((unsigned char)s <= 5) |
| 156 | return 1; |
| 157 | return 0; |
| 158 | } |
| 159 | |
Willy Tarreau | 2e74c3f | 2007-12-02 18:45:09 +0100 | [diff] [blame] | 160 | /* |
| 161 | * Checks <name> for invalid characters. Valid chars are [A-Za-z0-9_:.-]. If an |
| 162 | * invalid character is found, a pointer to it is returned. If everything is |
| 163 | * fine, NULL is returned. |
| 164 | */ |
| 165 | const char *invalid_char(const char *name) |
| 166 | { |
| 167 | if (!*name) |
| 168 | return name; |
| 169 | |
| 170 | while (*name) { |
Willy Tarreau | 127f966 | 2007-12-06 00:53:51 +0100 | [diff] [blame] | 171 | if (!isalnum((int)*name) && *name != '.' && *name != ':' && |
Willy Tarreau | 2e74c3f | 2007-12-02 18:45:09 +0100 | [diff] [blame] | 172 | *name != '_' && *name != '-') |
| 173 | return name; |
| 174 | name++; |
| 175 | } |
| 176 | return NULL; |
| 177 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 178 | |
| 179 | /* |
Krzysztof Piotr Oledzki | efe3b6f | 2008-05-23 23:49:32 +0200 | [diff] [blame] | 180 | * Checks <domainname> for invalid characters. Valid chars are [A-Za-z0-9_.-]. |
| 181 | * If an invalid character is found, a pointer to it is returned. |
| 182 | * If everything is fine, NULL is returned. |
| 183 | */ |
| 184 | const char *invalid_domainchar(const char *name) { |
| 185 | |
| 186 | if (!*name) |
| 187 | return name; |
| 188 | |
| 189 | while (*name) { |
| 190 | if (!isalnum((int)*name) && *name != '.' && |
| 191 | *name != '_' && *name != '-') |
| 192 | return name; |
| 193 | |
| 194 | name++; |
| 195 | } |
| 196 | |
| 197 | return NULL; |
| 198 | } |
| 199 | |
| 200 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 201 | * converts <str> to a struct sockaddr_in* which is locally allocated. |
| 202 | * The format is "addr:port", where "addr" can be a dotted IPv4 address, |
| 203 | * a host name, or empty or "*" to indicate INADDR_ANY. |
| 204 | */ |
| 205 | struct sockaddr_in *str2sa(char *str) |
| 206 | { |
| 207 | static struct sockaddr_in sa; |
| 208 | char *c; |
| 209 | int port; |
| 210 | |
| 211 | memset(&sa, 0, sizeof(sa)); |
| 212 | str = strdup(str); |
Willy Tarreau | c642348 | 2006-10-15 14:59:03 +0200 | [diff] [blame] | 213 | if (str == NULL) |
| 214 | goto out_nofree; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 215 | |
| 216 | if ((c = strrchr(str,':')) != NULL) { |
| 217 | *c++ = '\0'; |
| 218 | port = atol(c); |
| 219 | } |
| 220 | else |
| 221 | port = 0; |
| 222 | |
| 223 | if (*str == '*' || *str == '\0') { /* INADDR_ANY */ |
| 224 | sa.sin_addr.s_addr = INADDR_ANY; |
| 225 | } |
| 226 | else if (!inet_pton(AF_INET, str, &sa.sin_addr)) { |
| 227 | struct hostent *he; |
| 228 | |
| 229 | if ((he = gethostbyname(str)) == NULL) { |
| 230 | Alert("Invalid server name: '%s'\n", str); |
| 231 | } |
| 232 | else |
| 233 | sa.sin_addr = *(struct in_addr *) *(he->h_addr_list); |
| 234 | } |
| 235 | sa.sin_port = htons(port); |
| 236 | sa.sin_family = AF_INET; |
| 237 | |
| 238 | free(str); |
Willy Tarreau | c642348 | 2006-10-15 14:59:03 +0200 | [diff] [blame] | 239 | out_nofree: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 240 | return &sa; |
| 241 | } |
| 242 | |
| 243 | /* |
Willy Tarreau | d077a8e | 2007-05-08 18:28:09 +0200 | [diff] [blame] | 244 | * converts <str> to two struct in_addr* which must be pre-allocated. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 245 | * The format is "addr[/mask]", where "addr" cannot be empty, and mask |
| 246 | * is optionnal and either in the dotted or CIDR notation. |
| 247 | * Note: "addr" can also be a hostname. Returns 1 if OK, 0 if error. |
| 248 | */ |
Willy Tarreau | d077a8e | 2007-05-08 18:28:09 +0200 | [diff] [blame] | 249 | int str2net(const char *str, struct in_addr *addr, struct in_addr *mask) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 250 | { |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 251 | __label__ out_free, out_err; |
| 252 | char *c, *s; |
| 253 | int ret_val; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 254 | unsigned long len; |
| 255 | |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 256 | s = strdup(str); |
| 257 | if (!s) |
| 258 | return 0; |
| 259 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 260 | memset(mask, 0, sizeof(*mask)); |
| 261 | memset(addr, 0, sizeof(*addr)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 262 | |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 263 | if ((c = strrchr(s, '/')) != NULL) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 264 | *c++ = '\0'; |
| 265 | /* c points to the mask */ |
| 266 | if (strchr(c, '.') != NULL) { /* dotted notation */ |
| 267 | if (!inet_pton(AF_INET, c, mask)) |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 268 | goto out_err; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 269 | } |
| 270 | else { /* mask length */ |
| 271 | char *err; |
| 272 | len = strtol(c, &err, 10); |
| 273 | if (!*c || (err && *err) || (unsigned)len > 32) |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 274 | goto out_err; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 275 | if (len) |
| 276 | mask->s_addr = htonl(~0UL << (32 - len)); |
| 277 | else |
| 278 | mask->s_addr = 0; |
| 279 | } |
| 280 | } |
| 281 | else { |
Willy Tarreau | ebd6160 | 2006-12-30 11:54:15 +0100 | [diff] [blame] | 282 | mask->s_addr = ~0U; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 283 | } |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 284 | if (!inet_pton(AF_INET, s, addr)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 285 | struct hostent *he; |
| 286 | |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 287 | if ((he = gethostbyname(s)) == NULL) { |
| 288 | goto out_err; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 289 | } |
| 290 | else |
| 291 | *addr = *(struct in_addr *) *(he->h_addr_list); |
| 292 | } |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 293 | |
| 294 | ret_val = 1; |
| 295 | out_free: |
| 296 | free(s); |
| 297 | return ret_val; |
| 298 | out_err: |
| 299 | ret_val = 0; |
| 300 | goto out_free; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 301 | } |
| 302 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 303 | |
| 304 | /* |
| 305 | * Parse IP address found in url. |
| 306 | */ |
| 307 | static int url2ip(const char *addr, struct in_addr *dst) |
| 308 | { |
| 309 | int saw_digit, octets, ch; |
| 310 | u_char tmp[4], *tp; |
| 311 | const char *cp = addr; |
| 312 | |
| 313 | saw_digit = 0; |
| 314 | octets = 0; |
| 315 | *(tp = tmp) = 0; |
| 316 | |
| 317 | while (*addr) { |
| 318 | unsigned char digit = (ch = *addr++) - '0'; |
| 319 | if (digit > 9 && ch != '.') |
| 320 | break; |
| 321 | if (digit <= 9) { |
| 322 | u_int new = *tp * 10 + digit; |
| 323 | if (new > 255) |
| 324 | return 0; |
| 325 | *tp = new; |
| 326 | if (!saw_digit) { |
| 327 | if (++octets > 4) |
| 328 | return 0; |
| 329 | saw_digit = 1; |
| 330 | } |
| 331 | } else if (ch == '.' && saw_digit) { |
| 332 | if (octets == 4) |
| 333 | return 0; |
| 334 | *++tp = 0; |
| 335 | saw_digit = 0; |
| 336 | } else |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | if (octets < 4) |
| 341 | return 0; |
| 342 | |
| 343 | memcpy(&dst->s_addr, tmp, 4); |
| 344 | return addr-cp-1; |
| 345 | } |
| 346 | |
| 347 | /* |
| 348 | * Resolve destination server from URL. Convert <str> to a sockaddr_in*. |
| 349 | */ |
| 350 | int url2sa(const char *url, int ulen, struct sockaddr_in *addr) |
| 351 | { |
| 352 | const char *curr = url, *cp = url; |
| 353 | int ret, url_code = 0; |
| 354 | unsigned int http_code = 0; |
| 355 | |
| 356 | /* Cleanup the room */ |
| 357 | addr->sin_family = AF_INET; |
| 358 | addr->sin_addr.s_addr = 0; |
| 359 | addr->sin_port = 0; |
| 360 | |
| 361 | /* Firstly, try to find :// pattern */ |
| 362 | while (curr < url+ulen && url_code != 0x3a2f2f) { |
| 363 | url_code = ((url_code & 0xffff) << 8); |
| 364 | url_code += (unsigned char)*curr++; |
| 365 | } |
| 366 | |
| 367 | /* Secondly, if :// pattern is found, verify parsed stuff |
| 368 | * before pattern is matching our http pattern. |
| 369 | * If so parse ip address and port in uri. |
| 370 | * |
| 371 | * WARNING: Current code doesn't support dynamic async dns resolver. |
| 372 | */ |
| 373 | if (url_code == 0x3a2f2f) { |
| 374 | while (cp < curr - 3) |
| 375 | http_code = (http_code << 8) + *cp++; |
| 376 | http_code |= 0x20202020; /* Turn everything to lower case */ |
| 377 | |
| 378 | /* HTTP url matching */ |
| 379 | if (http_code == 0x68747470) { |
| 380 | /* We are looking for IP address. If you want to parse and |
| 381 | * resolve hostname found in url, you can use str2sa(), but |
| 382 | * be warned this can slow down global daemon performances |
| 383 | * while handling lagging dns responses. |
| 384 | */ |
| 385 | ret = url2ip(curr, &addr->sin_addr); |
| 386 | if (!ret) |
| 387 | return -1; |
| 388 | curr += ret; |
Willy Tarreau | d1cd276 | 2007-12-02 10:55:56 +0100 | [diff] [blame] | 389 | addr->sin_port = (*curr == ':') ? str2uic(++curr) : 80; |
| 390 | addr->sin_port = htons(addr->sin_port); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 391 | } |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | return -1; |
| 396 | } |
| 397 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 398 | /* will try to encode the string <string> replacing all characters tagged in |
| 399 | * <map> with the hexadecimal representation of their ASCII-code (2 digits) |
| 400 | * prefixed by <escape>, and will store the result between <start> (included) |
| 401 | * and <stop> (excluded), and will always terminate the string with a '\0' |
| 402 | * before <stop>. The position of the '\0' is returned if the conversion |
| 403 | * completes. If bytes are missing between <start> and <stop>, then the |
| 404 | * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0' |
| 405 | * cannot even be stored so we return <start> without writing the 0. |
| 406 | * The input string must also be zero-terminated. |
| 407 | */ |
| 408 | const char hextab[16] = "0123456789ABCDEF"; |
| 409 | char *encode_string(char *start, char *stop, |
| 410 | const char escape, const fd_set *map, |
| 411 | const char *string) |
| 412 | { |
| 413 | if (start < stop) { |
| 414 | stop--; /* reserve one byte for the final '\0' */ |
| 415 | while (start < stop && *string != '\0') { |
| 416 | if (!FD_ISSET((unsigned char)(*string), map)) |
| 417 | *start++ = *string; |
| 418 | else { |
| 419 | if (start + 3 >= stop) |
| 420 | break; |
| 421 | *start++ = escape; |
| 422 | *start++ = hextab[(*string >> 4) & 15]; |
| 423 | *start++ = hextab[*string & 15]; |
| 424 | } |
| 425 | string++; |
| 426 | } |
| 427 | *start = '\0'; |
| 428 | } |
| 429 | return start; |
| 430 | } |
| 431 | |
| 432 | |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 433 | unsigned int str2ui(const char *s) |
| 434 | { |
| 435 | return __str2ui(s); |
| 436 | } |
| 437 | |
| 438 | unsigned int str2uic(const char *s) |
| 439 | { |
| 440 | return __str2uic(s); |
| 441 | } |
| 442 | |
| 443 | unsigned int strl2ui(const char *s, int len) |
| 444 | { |
| 445 | return __strl2ui(s, len); |
| 446 | } |
| 447 | |
| 448 | unsigned int strl2uic(const char *s, int len) |
| 449 | { |
| 450 | return __strl2uic(s, len); |
| 451 | } |
| 452 | |
| 453 | /* This one is 7 times faster than strtol() on athlon with checks. |
| 454 | * It returns the value of the number composed of all valid digits read, |
| 455 | * and can process negative numbers too. |
| 456 | */ |
| 457 | int strl2ic(const char *s, int len) |
| 458 | { |
| 459 | int i = 0; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 460 | int j, k; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 461 | |
| 462 | if (len > 0) { |
| 463 | if (*s != '-') { |
| 464 | /* positive number */ |
| 465 | while (len-- > 0) { |
| 466 | j = (*s++) - '0'; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 467 | k = i * 10; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 468 | if (j > 9) |
| 469 | break; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 470 | i = k + j; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 471 | } |
| 472 | } else { |
| 473 | /* negative number */ |
| 474 | s++; |
| 475 | while (--len > 0) { |
| 476 | j = (*s++) - '0'; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 477 | k = i * 10; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 478 | if (j > 9) |
| 479 | break; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 480 | i = k - j; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 481 | } |
| 482 | } |
| 483 | } |
| 484 | return i; |
| 485 | } |
| 486 | |
| 487 | |
| 488 | /* This function reads exactly <len> chars from <s> and converts them to a |
| 489 | * signed integer which it stores into <ret>. It accurately detects any error |
| 490 | * (truncated string, invalid chars, overflows). It is meant to be used in |
| 491 | * applications designed for hostile environments. It returns zero when the |
| 492 | * number has successfully been converted, non-zero otherwise. When an error |
| 493 | * is returned, the <ret> value is left untouched. It is yet 5 to 40 times |
| 494 | * faster than strtol(). |
| 495 | */ |
| 496 | int strl2irc(const char *s, int len, int *ret) |
| 497 | { |
| 498 | int i = 0; |
| 499 | int j; |
| 500 | |
| 501 | if (!len) |
| 502 | return 1; |
| 503 | |
| 504 | if (*s != '-') { |
| 505 | /* positive number */ |
| 506 | while (len-- > 0) { |
| 507 | j = (*s++) - '0'; |
| 508 | if (j > 9) return 1; /* invalid char */ |
| 509 | if (i > INT_MAX / 10) return 1; /* check for multiply overflow */ |
| 510 | i = i * 10; |
| 511 | if (i + j < i) return 1; /* check for addition overflow */ |
| 512 | i = i + j; |
| 513 | } |
| 514 | } else { |
| 515 | /* negative number */ |
| 516 | s++; |
| 517 | while (--len > 0) { |
| 518 | j = (*s++) - '0'; |
| 519 | if (j > 9) return 1; /* invalid char */ |
| 520 | if (i < INT_MIN / 10) return 1; /* check for multiply overflow */ |
| 521 | i = i * 10; |
| 522 | if (i - j > i) return 1; /* check for subtract overflow */ |
| 523 | i = i - j; |
| 524 | } |
| 525 | } |
| 526 | *ret = i; |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | |
| 531 | /* This function reads exactly <len> chars from <s> and converts them to a |
| 532 | * signed integer which it stores into <ret>. It accurately detects any error |
| 533 | * (truncated string, invalid chars, overflows). It is meant to be used in |
| 534 | * applications designed for hostile environments. It returns zero when the |
| 535 | * number has successfully been converted, non-zero otherwise. When an error |
| 536 | * is returned, the <ret> value is left untouched. It is about 3 times slower |
| 537 | * than str2irc(). |
| 538 | */ |
| 539 | #ifndef LLONG_MAX |
| 540 | #define LLONG_MAX 9223372036854775807LL |
| 541 | #define LLONG_MIN (-LLONG_MAX - 1LL) |
| 542 | #endif |
| 543 | |
| 544 | int strl2llrc(const char *s, int len, long long *ret) |
| 545 | { |
| 546 | long long i = 0; |
| 547 | int j; |
| 548 | |
| 549 | if (!len) |
| 550 | return 1; |
| 551 | |
| 552 | if (*s != '-') { |
| 553 | /* positive number */ |
| 554 | while (len-- > 0) { |
| 555 | j = (*s++) - '0'; |
| 556 | if (j > 9) return 1; /* invalid char */ |
| 557 | if (i > LLONG_MAX / 10LL) return 1; /* check for multiply overflow */ |
| 558 | i = i * 10LL; |
| 559 | if (i + j < i) return 1; /* check for addition overflow */ |
| 560 | i = i + j; |
| 561 | } |
| 562 | } else { |
| 563 | /* negative number */ |
| 564 | s++; |
| 565 | while (--len > 0) { |
| 566 | j = (*s++) - '0'; |
| 567 | if (j > 9) return 1; /* invalid char */ |
| 568 | if (i < LLONG_MIN / 10LL) return 1; /* check for multiply overflow */ |
| 569 | i = i * 10LL; |
| 570 | if (i - j > i) return 1; /* check for subtract overflow */ |
| 571 | i = i - j; |
| 572 | } |
| 573 | } |
| 574 | *ret = i; |
| 575 | return 0; |
| 576 | } |
| 577 | |
Willy Tarreau | a0d37b6 | 2007-12-02 22:00:35 +0100 | [diff] [blame] | 578 | /* This function parses a time value optionally followed by a unit suffix among |
| 579 | * "d", "h", "m", "s", "ms" or "us". It converts the value into the unit |
| 580 | * expected by the caller. The computation does its best to avoid overflows. |
| 581 | * The value is returned in <ret> if everything is fine, and a NULL is returned |
| 582 | * by the function. In case of error, a pointer to the error is returned and |
| 583 | * <ret> is left untouched. Values are automatically rounded up when needed. |
| 584 | */ |
| 585 | const char *parse_time_err(const char *text, unsigned *ret, unsigned unit_flags) |
| 586 | { |
| 587 | unsigned imult, idiv; |
| 588 | unsigned omult, odiv; |
| 589 | unsigned value; |
| 590 | |
| 591 | omult = odiv = 1; |
| 592 | |
| 593 | switch (unit_flags & TIME_UNIT_MASK) { |
| 594 | case TIME_UNIT_US: omult = 1000000; break; |
| 595 | case TIME_UNIT_MS: omult = 1000; break; |
| 596 | case TIME_UNIT_S: break; |
| 597 | case TIME_UNIT_MIN: odiv = 60; break; |
| 598 | case TIME_UNIT_HOUR: odiv = 3600; break; |
| 599 | case TIME_UNIT_DAY: odiv = 86400; break; |
| 600 | default: break; |
| 601 | } |
| 602 | |
| 603 | value = 0; |
| 604 | |
| 605 | while (1) { |
| 606 | unsigned int j; |
| 607 | |
| 608 | j = *text - '0'; |
| 609 | if (j > 9) |
| 610 | break; |
| 611 | text++; |
| 612 | value *= 10; |
| 613 | value += j; |
| 614 | } |
| 615 | |
| 616 | imult = idiv = 1; |
| 617 | switch (*text) { |
| 618 | case '\0': /* no unit = default unit */ |
| 619 | imult = omult = idiv = odiv = 1; |
| 620 | break; |
| 621 | case 's': /* second = unscaled unit */ |
| 622 | break; |
| 623 | case 'u': /* microsecond : "us" */ |
| 624 | if (text[1] == 's') { |
| 625 | idiv = 1000000; |
| 626 | text++; |
| 627 | } |
| 628 | break; |
| 629 | case 'm': /* millisecond : "ms" or minute: "m" */ |
| 630 | if (text[1] == 's') { |
| 631 | idiv = 1000; |
| 632 | text++; |
| 633 | } else |
| 634 | imult = 60; |
| 635 | break; |
| 636 | case 'h': /* hour : "h" */ |
| 637 | imult = 3600; |
| 638 | break; |
| 639 | case 'd': /* day : "d" */ |
| 640 | imult = 86400; |
| 641 | break; |
| 642 | default: |
| 643 | return text; |
| 644 | break; |
| 645 | } |
| 646 | |
| 647 | if (omult % idiv == 0) { omult /= idiv; idiv = 1; } |
| 648 | if (idiv % omult == 0) { idiv /= omult; omult = 1; } |
| 649 | if (imult % odiv == 0) { imult /= odiv; odiv = 1; } |
| 650 | if (odiv % imult == 0) { odiv /= imult; imult = 1; } |
| 651 | |
| 652 | value = (value * (imult * omult) + (idiv * odiv - 1)) / (idiv * odiv); |
| 653 | *ret = value; |
| 654 | return NULL; |
| 655 | } |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 656 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 657 | /* |
| 658 | * Local variables: |
| 659 | * c-indent-level: 8 |
| 660 | * c-basic-offset: 8 |
| 661 | * End: |
| 662 | */ |