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