Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * General purpose functions. |
| 3 | * |
Willy Tarreau | 348238b | 2010-01-18 15:05:57 +0100 | [diff] [blame] | 4 | * Copyright 2000-2010 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 | 45cb4fb | 2009-10-26 21:10:04 +0100 | [diff] [blame] | 24 | #include <eb32tree.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 | */ |
Emeric Brun | 3a7fce5 | 2010-01-04 14:54:38 +0100 | [diff] [blame] | 60 | 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. |
Willy Tarreau | d5191e7 | 2010-02-09 20:50:45 +0100 | [diff] [blame] | 122 | * NULL is returned if the socket path is invalid (too long). |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 123 | */ |
Willy Tarreau | caf720d | 2008-03-07 10:07:04 +0100 | [diff] [blame] | 124 | struct sockaddr_un *str2sun(const char *str) |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 125 | { |
Willy Tarreau | 127f966 | 2007-12-06 00:53:51 +0100 | [diff] [blame] | 126 | static struct sockaddr_un su; |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 127 | int strsz; /* length included null */ |
| 128 | |
Willy Tarreau | 127f966 | 2007-12-06 00:53:51 +0100 | [diff] [blame] | 129 | memset(&su, 0, sizeof(su)); |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 130 | strsz = strlen(str) + 1; |
Willy Tarreau | 127f966 | 2007-12-06 00:53:51 +0100 | [diff] [blame] | 131 | if (strsz > sizeof(su.sun_path)) { |
Willy Tarreau | d5191e7 | 2010-02-09 20:50:45 +0100 | [diff] [blame] | 132 | return NULL; |
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 | /* |
Willy Tarreau | da3b7c3 | 2009-11-02 20:12:52 +0100 | [diff] [blame] | 162 | * Return integer equivalent of character <c> for a hex digit (0-9, a-f, A-F), |
| 163 | * otherwise -1. This compact form helps gcc produce efficient code. |
| 164 | */ |
| 165 | int hex2i(int c) |
| 166 | { |
| 167 | if ((unsigned char)(c -= '0') > 9) { |
| 168 | if ((unsigned char)(c -= 'A' - '0') > 5 && |
| 169 | (unsigned char)(c -= 'a' - 'A') > 5) |
| 170 | c = -11; |
| 171 | c += 10; |
| 172 | } |
| 173 | return c; |
| 174 | } |
| 175 | |
| 176 | /* |
Willy Tarreau | 2e74c3f | 2007-12-02 18:45:09 +0100 | [diff] [blame] | 177 | * Checks <name> for invalid characters. Valid chars are [A-Za-z0-9_:.-]. If an |
| 178 | * invalid character is found, a pointer to it is returned. If everything is |
| 179 | * fine, NULL is returned. |
| 180 | */ |
| 181 | const char *invalid_char(const char *name) |
| 182 | { |
| 183 | if (!*name) |
| 184 | return name; |
| 185 | |
| 186 | while (*name) { |
Willy Tarreau | 88e0581 | 2010-03-03 00:16:00 +0100 | [diff] [blame] | 187 | if (!isalnum((int)(unsigned char)*name) && *name != '.' && *name != ':' && |
Willy Tarreau | 2e74c3f | 2007-12-02 18:45:09 +0100 | [diff] [blame] | 188 | *name != '_' && *name != '-') |
| 189 | return name; |
| 190 | name++; |
| 191 | } |
| 192 | return NULL; |
| 193 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 194 | |
| 195 | /* |
Krzysztof Piotr Oledzki | efe3b6f | 2008-05-23 23:49:32 +0200 | [diff] [blame] | 196 | * Checks <domainname> for invalid characters. Valid chars are [A-Za-z0-9_.-]. |
| 197 | * If an invalid character is found, a pointer to it is returned. |
| 198 | * If everything is fine, NULL is returned. |
| 199 | */ |
| 200 | const char *invalid_domainchar(const char *name) { |
| 201 | |
| 202 | if (!*name) |
| 203 | return name; |
| 204 | |
| 205 | while (*name) { |
Willy Tarreau | 88e0581 | 2010-03-03 00:16:00 +0100 | [diff] [blame] | 206 | if (!isalnum((int)(unsigned char)*name) && *name != '.' && |
Krzysztof Piotr Oledzki | efe3b6f | 2008-05-23 23:49:32 +0200 | [diff] [blame] | 207 | *name != '_' && *name != '-') |
| 208 | return name; |
| 209 | |
| 210 | name++; |
| 211 | } |
| 212 | |
| 213 | return NULL; |
| 214 | } |
| 215 | |
| 216 | /* |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 217 | * converts <str> to a struct sockaddr_storage* which is locally allocated. The |
| 218 | * string is assumed to contain only an address, no port. The address can be a |
| 219 | * dotted IPv4 address, an IPv6 address, a host name, or empty or "*" to |
| 220 | * indicate INADDR_ANY. NULL is returned if the host part cannot be resolved. |
| 221 | * The return address will only have the address family and the address set, |
| 222 | * all other fields remain zero. The string is not supposed to be modified. |
| 223 | * The IPv6 '::' address is IN6ADDR_ANY. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 224 | */ |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 225 | struct sockaddr_storage *str2ip(const char *str) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 226 | { |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 227 | static struct sockaddr_storage sa; |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 228 | struct hostent *he; |
| 229 | |
| 230 | memset(&sa, 0, sizeof(sa)); |
| 231 | |
| 232 | /* Any IPv6 address */ |
| 233 | if (str[0] == ':' && str[1] == ':' && !str[2]) { |
| 234 | sa.ss_family = AF_INET6; |
| 235 | return &sa; |
| 236 | } |
| 237 | |
| 238 | /* Any IPv4 address */ |
| 239 | if (!str[0] || (str[0] == '*' && !str[1])) { |
| 240 | sa.ss_family = AF_INET; |
| 241 | return &sa; |
| 242 | } |
| 243 | |
| 244 | /* check for IPv6 first */ |
| 245 | if (inet_pton(AF_INET6, str, &((struct sockaddr_in6 *)&sa)->sin6_addr)) { |
| 246 | sa.ss_family = AF_INET6; |
| 247 | return &sa; |
| 248 | } |
| 249 | |
| 250 | /* then check for IPv4 */ |
| 251 | if (inet_pton(AF_INET, str, &((struct sockaddr_in *)&sa)->sin_addr)) { |
| 252 | sa.ss_family = AF_INET; |
| 253 | return &sa; |
| 254 | } |
| 255 | |
| 256 | /* try to resolve an IPv4/IPv6 hostname */ |
| 257 | he = gethostbyname(str); |
| 258 | if (he) { |
| 259 | sa.ss_family = he->h_addrtype; |
| 260 | switch (sa.ss_family) { |
| 261 | case AF_INET: |
| 262 | ((struct sockaddr_in *)&sa)->sin_addr = *(struct in_addr *) *(he->h_addr_list); |
| 263 | return &sa; |
| 264 | case AF_INET6: |
| 265 | ((struct sockaddr_in6 *)&sa)->sin6_addr = *(struct in6_addr *) *(he->h_addr_list); |
| 266 | return &sa; |
| 267 | } |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 268 | } |
| 269 | #ifdef USE_GETADDRINFO |
| 270 | else { |
| 271 | struct addrinfo hints, *result; |
| 272 | |
| 273 | memset(&result, 0, sizeof(result)); |
| 274 | memset(&hints, 0, sizeof(hints)); |
| 275 | hints.ai_family = AF_UNSPEC; |
| 276 | hints.ai_socktype = SOCK_DGRAM; |
| 277 | hints.ai_flags = AI_PASSIVE; |
| 278 | hints.ai_protocol = 0; |
| 279 | |
| 280 | if (getaddrinfo(str, NULL, &hints, &result) == 0) { |
| 281 | sa.ss_family = result->ai_family; |
| 282 | switch (result->ai_family) { |
| 283 | case AF_INET: |
| 284 | memcpy((struct sockaddr_in *)&sa, result->ai_addr, result->ai_addrlen); |
| 285 | return &sa; |
| 286 | case AF_INET6: |
| 287 | memcpy((struct sockaddr_in6 *)&sa, result->ai_addr, result->ai_addrlen); |
| 288 | return &sa; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | freeaddrinfo(result); |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 293 | } |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 294 | #endif |
| 295 | /* unsupported address family */ |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 296 | |
| 297 | return NULL; |
| 298 | } |
| 299 | |
| 300 | /* |
| 301 | * converts <str> to a locally allocated struct sockaddr_storage *. |
| 302 | * The format is "addr[:[port]]", where "addr" can be a dotted IPv4 address, an |
| 303 | * IPv6 address, a host name, or empty or "*" to indicate INADDR_ANY. If an IPv6 |
| 304 | * address wants to ignore port, it must be terminated by a trailing colon (':'). |
| 305 | * The IPv6 '::' address is IN6ADDR_ANY, so in order to bind to a given port on |
| 306 | * IPv6, use ":::port". NULL is returned if the host part cannot be resolved. |
| 307 | */ |
| 308 | struct sockaddr_storage *str2sa(const char *str) |
| 309 | { |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 310 | struct sockaddr_storage *ret = NULL; |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 311 | char *str2; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 312 | char *c; |
| 313 | int port; |
| 314 | |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 315 | str2 = strdup(str); |
| 316 | if (str2 == NULL) |
Willy Tarreau | d5191e7 | 2010-02-09 20:50:45 +0100 | [diff] [blame] | 317 | goto out; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 318 | |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 319 | if ((c = strrchr(str2, ':')) != NULL) { /* Port */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 320 | *c++ = '\0'; |
| 321 | port = atol(c); |
| 322 | } |
| 323 | else |
| 324 | port = 0; |
| 325 | |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 326 | ret = str2ip(str2); |
| 327 | if (!ret) |
| 328 | goto out; |
| 329 | |
| 330 | switch (ret->ss_family) { |
| 331 | case AF_INET: |
| 332 | ((struct sockaddr_in *)ret)->sin_port = htons(port); |
| 333 | break; |
| 334 | case AF_INET6: |
| 335 | ((struct sockaddr_in6 *)ret)->sin6_port = htons(port); |
| 336 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 337 | } |
Willy Tarreau | d5191e7 | 2010-02-09 20:50:45 +0100 | [diff] [blame] | 338 | out: |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 339 | free(str2); |
Willy Tarreau | d5191e7 | 2010-02-09 20:50:45 +0100 | [diff] [blame] | 340 | return ret; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /* |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 344 | * converts <str> to a locally allocated struct sockaddr_storage *, and a |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 345 | * port range consisting in two integers. The low and high end are always set |
| 346 | * even if the port is unspecified, in which case (0,0) is returned. The low |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 347 | * port is set in the sockaddr. Thus, it is enough to check the size of the |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 348 | * returned range to know if an array must be allocated or not. The format is |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 349 | * "addr[:[port[-port]]]", where "addr" can be a dotted IPv4 address, an IPv6 |
| 350 | * address, a host name, or empty or "*" to indicate INADDR_ANY. If an IPv6 |
| 351 | * address wants to ignore port, it must be terminated by a trailing colon (':'). |
| 352 | * The IPv6 '::' address is IN6ADDR_ANY, so in order to bind to a given port on |
| 353 | * IPv6, use ":::port". NULL is returned if the host part cannot be resolved. |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 354 | */ |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 355 | struct sockaddr_storage *str2sa_range(const char *str, int *low, int *high) |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 356 | { |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 357 | struct sockaddr_storage *ret = NULL; |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 358 | char *str2; |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 359 | char *c; |
| 360 | int portl, porth; |
| 361 | |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 362 | str2 = strdup(str); |
| 363 | if (str2 == NULL) |
Willy Tarreau | d5191e7 | 2010-02-09 20:50:45 +0100 | [diff] [blame] | 364 | goto out; |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 365 | |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 366 | if ((c = strrchr(str2,':')) != NULL) { /* Port */ |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 367 | char *sep; |
| 368 | *c++ = '\0'; |
| 369 | sep = strchr(c, '-'); |
| 370 | if (sep) |
| 371 | *sep++ = '\0'; |
| 372 | else |
| 373 | sep = c; |
| 374 | portl = atol(c); |
| 375 | porth = atol(sep); |
| 376 | } |
| 377 | else { |
| 378 | portl = 0; |
| 379 | porth = 0; |
| 380 | } |
| 381 | |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 382 | ret = str2ip(str2); |
| 383 | if (!ret) |
| 384 | goto out; |
| 385 | |
| 386 | switch (ret->ss_family) { |
| 387 | case AF_INET: |
| 388 | ((struct sockaddr_in *)ret)->sin_port = htons(portl); |
| 389 | break; |
| 390 | case AF_INET6: |
| 391 | ((struct sockaddr_in6 *)ret)->sin6_port = htons(portl); |
| 392 | break; |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 393 | } |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 394 | |
| 395 | *low = portl; |
| 396 | *high = porth; |
Willy Tarreau | d5191e7 | 2010-02-09 20:50:45 +0100 | [diff] [blame] | 397 | out: |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 398 | free(str2); |
Willy Tarreau | d5191e7 | 2010-02-09 20:50:45 +0100 | [diff] [blame] | 399 | return ret; |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 400 | } |
| 401 | |
Willy Tarreau | 2937c0d | 2010-01-26 17:36:17 +0100 | [diff] [blame] | 402 | /* converts <str> to a struct in_addr containing a network mask. It can be |
| 403 | * passed in dotted form (255.255.255.0) or in CIDR form (24). It returns 1 |
| 404 | * if the conversion succeeds otherwise non-zero. |
| 405 | */ |
| 406 | int str2mask(const char *str, struct in_addr *mask) |
| 407 | { |
| 408 | if (strchr(str, '.') != NULL) { /* dotted notation */ |
| 409 | if (!inet_pton(AF_INET, str, mask)) |
| 410 | return 0; |
| 411 | } |
| 412 | else { /* mask length */ |
| 413 | char *err; |
| 414 | unsigned long len = strtol(str, &err, 10); |
| 415 | |
| 416 | if (!*str || (err && *err) || (unsigned)len > 32) |
| 417 | return 0; |
| 418 | if (len) |
| 419 | mask->s_addr = htonl(~0UL << (32 - len)); |
| 420 | else |
| 421 | mask->s_addr = 0; |
| 422 | } |
| 423 | return 1; |
| 424 | } |
| 425 | |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 426 | /* |
Willy Tarreau | d077a8e | 2007-05-08 18:28:09 +0200 | [diff] [blame] | 427 | * converts <str> to two struct in_addr* which must be pre-allocated. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 428 | * The format is "addr[/mask]", where "addr" cannot be empty, and mask |
| 429 | * is optionnal and either in the dotted or CIDR notation. |
| 430 | * Note: "addr" can also be a hostname. Returns 1 if OK, 0 if error. |
| 431 | */ |
Willy Tarreau | d077a8e | 2007-05-08 18:28:09 +0200 | [diff] [blame] | 432 | 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] | 433 | { |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 434 | __label__ out_free, out_err; |
| 435 | char *c, *s; |
| 436 | int ret_val; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 437 | |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 438 | s = strdup(str); |
| 439 | if (!s) |
| 440 | return 0; |
| 441 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 442 | memset(mask, 0, sizeof(*mask)); |
| 443 | memset(addr, 0, sizeof(*addr)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 444 | |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 445 | if ((c = strrchr(s, '/')) != NULL) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 446 | *c++ = '\0'; |
| 447 | /* c points to the mask */ |
Willy Tarreau | 2937c0d | 2010-01-26 17:36:17 +0100 | [diff] [blame] | 448 | if (!str2mask(c, mask)) |
| 449 | goto out_err; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 450 | } |
| 451 | else { |
Willy Tarreau | ebd6160 | 2006-12-30 11:54:15 +0100 | [diff] [blame] | 452 | mask->s_addr = ~0U; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 453 | } |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 454 | if (!inet_pton(AF_INET, s, addr)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 455 | struct hostent *he; |
| 456 | |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 457 | if ((he = gethostbyname(s)) == NULL) { |
| 458 | goto out_err; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 459 | } |
| 460 | else |
| 461 | *addr = *(struct in_addr *) *(he->h_addr_list); |
| 462 | } |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 463 | |
| 464 | ret_val = 1; |
| 465 | out_free: |
| 466 | free(s); |
| 467 | return ret_val; |
| 468 | out_err: |
| 469 | ret_val = 0; |
| 470 | goto out_free; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 471 | } |
| 472 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 473 | |
| 474 | /* |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 475 | * Parse IPv4 address found in url. |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 476 | */ |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 477 | int url2ipv4(const char *addr, struct in_addr *dst) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 478 | { |
| 479 | int saw_digit, octets, ch; |
| 480 | u_char tmp[4], *tp; |
| 481 | const char *cp = addr; |
| 482 | |
| 483 | saw_digit = 0; |
| 484 | octets = 0; |
| 485 | *(tp = tmp) = 0; |
| 486 | |
| 487 | while (*addr) { |
| 488 | unsigned char digit = (ch = *addr++) - '0'; |
| 489 | if (digit > 9 && ch != '.') |
| 490 | break; |
| 491 | if (digit <= 9) { |
| 492 | u_int new = *tp * 10 + digit; |
| 493 | if (new > 255) |
| 494 | return 0; |
| 495 | *tp = new; |
| 496 | if (!saw_digit) { |
| 497 | if (++octets > 4) |
| 498 | return 0; |
| 499 | saw_digit = 1; |
| 500 | } |
| 501 | } else if (ch == '.' && saw_digit) { |
| 502 | if (octets == 4) |
| 503 | return 0; |
| 504 | *++tp = 0; |
| 505 | saw_digit = 0; |
| 506 | } else |
| 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | if (octets < 4) |
| 511 | return 0; |
| 512 | |
| 513 | memcpy(&dst->s_addr, tmp, 4); |
| 514 | return addr-cp-1; |
| 515 | } |
| 516 | |
| 517 | /* |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 518 | * Resolve destination server from URL. Convert <str> to a sockaddr_storage*. |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 519 | */ |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 520 | int url2sa(const char *url, int ulen, struct sockaddr_storage *addr) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 521 | { |
| 522 | const char *curr = url, *cp = url; |
| 523 | int ret, url_code = 0; |
| 524 | unsigned int http_code = 0; |
| 525 | |
| 526 | /* Cleanup the room */ |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 527 | |
| 528 | /* FIXME: assume IPv4 only for now */ |
| 529 | ((struct sockaddr_in *)addr)->sin_family = AF_INET; |
| 530 | ((struct sockaddr_in *)addr)->sin_addr.s_addr = 0; |
| 531 | ((struct sockaddr_in *)addr)->sin_port = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 532 | |
| 533 | /* Firstly, try to find :// pattern */ |
| 534 | while (curr < url+ulen && url_code != 0x3a2f2f) { |
| 535 | url_code = ((url_code & 0xffff) << 8); |
| 536 | url_code += (unsigned char)*curr++; |
| 537 | } |
| 538 | |
| 539 | /* Secondly, if :// pattern is found, verify parsed stuff |
| 540 | * before pattern is matching our http pattern. |
| 541 | * If so parse ip address and port in uri. |
| 542 | * |
| 543 | * WARNING: Current code doesn't support dynamic async dns resolver. |
| 544 | */ |
| 545 | if (url_code == 0x3a2f2f) { |
| 546 | while (cp < curr - 3) |
| 547 | http_code = (http_code << 8) + *cp++; |
| 548 | http_code |= 0x20202020; /* Turn everything to lower case */ |
| 549 | |
| 550 | /* HTTP url matching */ |
| 551 | if (http_code == 0x68747470) { |
| 552 | /* We are looking for IP address. If you want to parse and |
| 553 | * resolve hostname found in url, you can use str2sa(), but |
| 554 | * be warned this can slow down global daemon performances |
| 555 | * while handling lagging dns responses. |
| 556 | */ |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 557 | ret = url2ipv4(curr, &((struct sockaddr_in *)&addr)->sin_addr); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 558 | if (!ret) |
| 559 | return -1; |
| 560 | curr += ret; |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 561 | ((struct sockaddr_in *)addr)->sin_port = (*curr == ':') ? str2uic(++curr) : 80; |
| 562 | ((struct sockaddr_in *)addr)->sin_port = htons(((struct sockaddr_in *)&addr)->sin_port); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 563 | } |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | return -1; |
| 568 | } |
| 569 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 570 | /* will try to encode the string <string> replacing all characters tagged in |
| 571 | * <map> with the hexadecimal representation of their ASCII-code (2 digits) |
| 572 | * prefixed by <escape>, and will store the result between <start> (included) |
| 573 | * and <stop> (excluded), and will always terminate the string with a '\0' |
| 574 | * before <stop>. The position of the '\0' is returned if the conversion |
| 575 | * completes. If bytes are missing between <start> and <stop>, then the |
| 576 | * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0' |
| 577 | * cannot even be stored so we return <start> without writing the 0. |
| 578 | * The input string must also be zero-terminated. |
| 579 | */ |
| 580 | const char hextab[16] = "0123456789ABCDEF"; |
| 581 | char *encode_string(char *start, char *stop, |
| 582 | const char escape, const fd_set *map, |
| 583 | const char *string) |
| 584 | { |
| 585 | if (start < stop) { |
| 586 | stop--; /* reserve one byte for the final '\0' */ |
| 587 | while (start < stop && *string != '\0') { |
| 588 | if (!FD_ISSET((unsigned char)(*string), map)) |
| 589 | *start++ = *string; |
| 590 | else { |
| 591 | if (start + 3 >= stop) |
| 592 | break; |
| 593 | *start++ = escape; |
| 594 | *start++ = hextab[(*string >> 4) & 15]; |
| 595 | *start++ = hextab[*string & 15]; |
| 596 | } |
| 597 | string++; |
| 598 | } |
| 599 | *start = '\0'; |
| 600 | } |
| 601 | return start; |
| 602 | } |
| 603 | |
| 604 | |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 605 | unsigned int str2ui(const char *s) |
| 606 | { |
| 607 | return __str2ui(s); |
| 608 | } |
| 609 | |
| 610 | unsigned int str2uic(const char *s) |
| 611 | { |
| 612 | return __str2uic(s); |
| 613 | } |
| 614 | |
| 615 | unsigned int strl2ui(const char *s, int len) |
| 616 | { |
| 617 | return __strl2ui(s, len); |
| 618 | } |
| 619 | |
| 620 | unsigned int strl2uic(const char *s, int len) |
| 621 | { |
| 622 | return __strl2uic(s, len); |
| 623 | } |
| 624 | |
Willy Tarreau | 4ec83cd | 2010-10-15 23:19:55 +0200 | [diff] [blame] | 625 | unsigned int read_uint(const char **s, const char *end) |
| 626 | { |
| 627 | return __read_uint(s, end); |
| 628 | } |
| 629 | |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 630 | /* This one is 7 times faster than strtol() on athlon with checks. |
| 631 | * It returns the value of the number composed of all valid digits read, |
| 632 | * and can process negative numbers too. |
| 633 | */ |
| 634 | int strl2ic(const char *s, int len) |
| 635 | { |
| 636 | int i = 0; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 637 | int j, k; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 638 | |
| 639 | if (len > 0) { |
| 640 | if (*s != '-') { |
| 641 | /* positive number */ |
| 642 | while (len-- > 0) { |
| 643 | j = (*s++) - '0'; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 644 | k = i * 10; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 645 | if (j > 9) |
| 646 | break; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 647 | i = k + j; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 648 | } |
| 649 | } else { |
| 650 | /* negative number */ |
| 651 | s++; |
| 652 | while (--len > 0) { |
| 653 | j = (*s++) - '0'; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 654 | k = i * 10; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 655 | if (j > 9) |
| 656 | break; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 657 | i = k - j; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | } |
| 661 | return i; |
| 662 | } |
| 663 | |
| 664 | |
| 665 | /* This function reads exactly <len> chars from <s> and converts them to a |
| 666 | * signed integer which it stores into <ret>. It accurately detects any error |
| 667 | * (truncated string, invalid chars, overflows). It is meant to be used in |
| 668 | * applications designed for hostile environments. It returns zero when the |
| 669 | * number has successfully been converted, non-zero otherwise. When an error |
| 670 | * is returned, the <ret> value is left untouched. It is yet 5 to 40 times |
| 671 | * faster than strtol(). |
| 672 | */ |
| 673 | int strl2irc(const char *s, int len, int *ret) |
| 674 | { |
| 675 | int i = 0; |
| 676 | int j; |
| 677 | |
| 678 | if (!len) |
| 679 | return 1; |
| 680 | |
| 681 | if (*s != '-') { |
| 682 | /* positive number */ |
| 683 | while (len-- > 0) { |
| 684 | j = (*s++) - '0'; |
| 685 | if (j > 9) return 1; /* invalid char */ |
| 686 | if (i > INT_MAX / 10) return 1; /* check for multiply overflow */ |
| 687 | i = i * 10; |
| 688 | if (i + j < i) return 1; /* check for addition overflow */ |
| 689 | i = i + j; |
| 690 | } |
| 691 | } else { |
| 692 | /* negative number */ |
| 693 | s++; |
| 694 | while (--len > 0) { |
| 695 | j = (*s++) - '0'; |
| 696 | if (j > 9) return 1; /* invalid char */ |
| 697 | if (i < INT_MIN / 10) return 1; /* check for multiply overflow */ |
| 698 | i = i * 10; |
| 699 | if (i - j > i) return 1; /* check for subtract overflow */ |
| 700 | i = i - j; |
| 701 | } |
| 702 | } |
| 703 | *ret = i; |
| 704 | return 0; |
| 705 | } |
| 706 | |
| 707 | |
| 708 | /* This function reads exactly <len> chars from <s> and converts them to a |
| 709 | * signed integer which it stores into <ret>. It accurately detects any error |
| 710 | * (truncated string, invalid chars, overflows). It is meant to be used in |
| 711 | * applications designed for hostile environments. It returns zero when the |
| 712 | * number has successfully been converted, non-zero otherwise. When an error |
| 713 | * is returned, the <ret> value is left untouched. It is about 3 times slower |
| 714 | * than str2irc(). |
| 715 | */ |
| 716 | #ifndef LLONG_MAX |
| 717 | #define LLONG_MAX 9223372036854775807LL |
| 718 | #define LLONG_MIN (-LLONG_MAX - 1LL) |
| 719 | #endif |
| 720 | |
| 721 | int strl2llrc(const char *s, int len, long long *ret) |
| 722 | { |
| 723 | long long i = 0; |
| 724 | int j; |
| 725 | |
| 726 | if (!len) |
| 727 | return 1; |
| 728 | |
| 729 | if (*s != '-') { |
| 730 | /* positive number */ |
| 731 | while (len-- > 0) { |
| 732 | j = (*s++) - '0'; |
| 733 | if (j > 9) return 1; /* invalid char */ |
| 734 | if (i > LLONG_MAX / 10LL) return 1; /* check for multiply overflow */ |
| 735 | i = i * 10LL; |
| 736 | if (i + j < i) return 1; /* check for addition overflow */ |
| 737 | i = i + j; |
| 738 | } |
| 739 | } else { |
| 740 | /* negative number */ |
| 741 | s++; |
| 742 | while (--len > 0) { |
| 743 | j = (*s++) - '0'; |
| 744 | if (j > 9) return 1; /* invalid char */ |
| 745 | if (i < LLONG_MIN / 10LL) return 1; /* check for multiply overflow */ |
| 746 | i = i * 10LL; |
| 747 | if (i - j > i) return 1; /* check for subtract overflow */ |
| 748 | i = i - j; |
| 749 | } |
| 750 | } |
| 751 | *ret = i; |
| 752 | return 0; |
| 753 | } |
| 754 | |
Willy Tarreau | a0d37b6 | 2007-12-02 22:00:35 +0100 | [diff] [blame] | 755 | /* This function parses a time value optionally followed by a unit suffix among |
| 756 | * "d", "h", "m", "s", "ms" or "us". It converts the value into the unit |
| 757 | * expected by the caller. The computation does its best to avoid overflows. |
| 758 | * The value is returned in <ret> if everything is fine, and a NULL is returned |
| 759 | * by the function. In case of error, a pointer to the error is returned and |
| 760 | * <ret> is left untouched. Values are automatically rounded up when needed. |
| 761 | */ |
| 762 | const char *parse_time_err(const char *text, unsigned *ret, unsigned unit_flags) |
| 763 | { |
| 764 | unsigned imult, idiv; |
| 765 | unsigned omult, odiv; |
| 766 | unsigned value; |
| 767 | |
| 768 | omult = odiv = 1; |
| 769 | |
| 770 | switch (unit_flags & TIME_UNIT_MASK) { |
| 771 | case TIME_UNIT_US: omult = 1000000; break; |
| 772 | case TIME_UNIT_MS: omult = 1000; break; |
| 773 | case TIME_UNIT_S: break; |
| 774 | case TIME_UNIT_MIN: odiv = 60; break; |
| 775 | case TIME_UNIT_HOUR: odiv = 3600; break; |
| 776 | case TIME_UNIT_DAY: odiv = 86400; break; |
| 777 | default: break; |
| 778 | } |
| 779 | |
| 780 | value = 0; |
| 781 | |
| 782 | while (1) { |
| 783 | unsigned int j; |
| 784 | |
| 785 | j = *text - '0'; |
| 786 | if (j > 9) |
| 787 | break; |
| 788 | text++; |
| 789 | value *= 10; |
| 790 | value += j; |
| 791 | } |
| 792 | |
| 793 | imult = idiv = 1; |
| 794 | switch (*text) { |
| 795 | case '\0': /* no unit = default unit */ |
| 796 | imult = omult = idiv = odiv = 1; |
| 797 | break; |
| 798 | case 's': /* second = unscaled unit */ |
| 799 | break; |
| 800 | case 'u': /* microsecond : "us" */ |
| 801 | if (text[1] == 's') { |
| 802 | idiv = 1000000; |
| 803 | text++; |
| 804 | } |
| 805 | break; |
| 806 | case 'm': /* millisecond : "ms" or minute: "m" */ |
| 807 | if (text[1] == 's') { |
| 808 | idiv = 1000; |
| 809 | text++; |
| 810 | } else |
| 811 | imult = 60; |
| 812 | break; |
| 813 | case 'h': /* hour : "h" */ |
| 814 | imult = 3600; |
| 815 | break; |
| 816 | case 'd': /* day : "d" */ |
| 817 | imult = 86400; |
| 818 | break; |
| 819 | default: |
| 820 | return text; |
| 821 | break; |
| 822 | } |
| 823 | |
| 824 | if (omult % idiv == 0) { omult /= idiv; idiv = 1; } |
| 825 | if (idiv % omult == 0) { idiv /= omult; omult = 1; } |
| 826 | if (imult % odiv == 0) { imult /= odiv; odiv = 1; } |
| 827 | if (odiv % imult == 0) { odiv /= imult; imult = 1; } |
| 828 | |
| 829 | value = (value * (imult * omult) + (idiv * odiv - 1)) / (idiv * odiv); |
| 830 | *ret = value; |
| 831 | return NULL; |
| 832 | } |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 833 | |
Emeric Brun | 39132b2 | 2010-01-04 14:57:24 +0100 | [diff] [blame] | 834 | /* this function converts the string starting at <text> to an unsigned int |
| 835 | * stored in <ret>. If an error is detected, the pointer to the unexpected |
| 836 | * character is returned. If the conversio is succesful, NULL is returned. |
| 837 | */ |
| 838 | const char *parse_size_err(const char *text, unsigned *ret) { |
| 839 | unsigned value = 0; |
| 840 | |
| 841 | while (1) { |
| 842 | unsigned int j; |
| 843 | |
| 844 | j = *text - '0'; |
| 845 | if (j > 9) |
| 846 | break; |
| 847 | if (value > ~0U / 10) |
| 848 | return text; |
| 849 | value *= 10; |
| 850 | if (value > (value + j)) |
| 851 | return text; |
| 852 | value += j; |
| 853 | text++; |
| 854 | } |
| 855 | |
| 856 | switch (*text) { |
| 857 | case '\0': |
| 858 | break; |
| 859 | case 'K': |
| 860 | case 'k': |
| 861 | if (value > ~0U >> 10) |
| 862 | return text; |
| 863 | value = value << 10; |
| 864 | break; |
| 865 | case 'M': |
| 866 | case 'm': |
| 867 | if (value > ~0U >> 20) |
| 868 | return text; |
| 869 | value = value << 20; |
| 870 | break; |
| 871 | case 'G': |
| 872 | case 'g': |
| 873 | if (value > ~0U >> 30) |
| 874 | return text; |
| 875 | value = value << 30; |
| 876 | break; |
| 877 | default: |
| 878 | return text; |
| 879 | } |
| 880 | |
| 881 | *ret = value; |
| 882 | return NULL; |
| 883 | } |
| 884 | |
Willy Tarreau | 946ba59 | 2009-05-10 15:41:18 +0200 | [diff] [blame] | 885 | /* copies at most <n> characters from <src> and always terminates with '\0' */ |
| 886 | char *my_strndup(const char *src, int n) |
| 887 | { |
| 888 | int len = 0; |
| 889 | char *ret; |
| 890 | |
| 891 | while (len < n && src[len]) |
| 892 | len++; |
| 893 | |
| 894 | ret = (char *)malloc(len + 1); |
| 895 | if (!ret) |
| 896 | return ret; |
| 897 | memcpy(ret, src, len); |
| 898 | ret[len] = '\0'; |
| 899 | return ret; |
| 900 | } |
| 901 | |
Willy Tarreau | 482b00d | 2009-10-04 22:48:42 +0200 | [diff] [blame] | 902 | /* This function returns the first unused key greater than or equal to <key> in |
| 903 | * ID tree <root>. Zero is returned if no place is found. |
| 904 | */ |
| 905 | unsigned int get_next_id(struct eb_root *root, unsigned int key) |
| 906 | { |
| 907 | struct eb32_node *used; |
| 908 | |
| 909 | do { |
| 910 | used = eb32_lookup_ge(root, key); |
| 911 | if (!used || used->key > key) |
| 912 | return key; /* key is available */ |
| 913 | key++; |
| 914 | } while (key); |
| 915 | return key; |
| 916 | } |
| 917 | |
Willy Tarreau | 348238b | 2010-01-18 15:05:57 +0100 | [diff] [blame] | 918 | /* This function compares a sample word possibly followed by blanks to another |
| 919 | * clean word. The compare is case-insensitive. 1 is returned if both are equal, |
| 920 | * otherwise zero. This intends to be used when checking HTTP headers for some |
| 921 | * values. Note that it validates a word followed only by blanks but does not |
| 922 | * validate a word followed by blanks then other chars. |
| 923 | */ |
| 924 | int word_match(const char *sample, int slen, const char *word, int wlen) |
| 925 | { |
| 926 | if (slen < wlen) |
| 927 | return 0; |
| 928 | |
| 929 | while (wlen) { |
| 930 | char c = *sample ^ *word; |
| 931 | if (c && c != ('A' ^ 'a')) |
| 932 | return 0; |
| 933 | sample++; |
| 934 | word++; |
| 935 | slen--; |
| 936 | wlen--; |
| 937 | } |
| 938 | |
| 939 | while (slen) { |
| 940 | if (*sample != ' ' && *sample != '\t') |
| 941 | return 0; |
| 942 | sample++; |
| 943 | slen--; |
| 944 | } |
| 945 | return 1; |
| 946 | } |
Willy Tarreau | 482b00d | 2009-10-04 22:48:42 +0200 | [diff] [blame] | 947 | |
Willy Tarreau | d54bbdc | 2009-09-07 11:00:31 +0200 | [diff] [blame] | 948 | /* Converts any text-formatted IPv4 address to a host-order IPv4 address. It |
| 949 | * is particularly fast because it avoids expensive operations such as |
| 950 | * multiplies, which are optimized away at the end. It requires a properly |
| 951 | * formated address though (3 points). |
| 952 | */ |
| 953 | unsigned int inetaddr_host(const char *text) |
| 954 | { |
| 955 | const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0'; |
| 956 | register unsigned int dig100, dig10, dig1; |
| 957 | int s; |
| 958 | const char *p, *d; |
| 959 | |
| 960 | dig1 = dig10 = dig100 = ascii_zero; |
| 961 | s = 24; |
| 962 | |
| 963 | p = text; |
| 964 | while (1) { |
| 965 | if (((unsigned)(*p - '0')) <= 9) { |
| 966 | p++; |
| 967 | continue; |
| 968 | } |
| 969 | |
| 970 | /* here, we have a complete byte between <text> and <p> (exclusive) */ |
| 971 | if (p == text) |
| 972 | goto end; |
| 973 | |
| 974 | d = p - 1; |
| 975 | dig1 |= (unsigned int)(*d << s); |
| 976 | if (d == text) |
| 977 | goto end; |
| 978 | |
| 979 | d--; |
| 980 | dig10 |= (unsigned int)(*d << s); |
| 981 | if (d == text) |
| 982 | goto end; |
| 983 | |
| 984 | d--; |
| 985 | dig100 |= (unsigned int)(*d << s); |
| 986 | end: |
| 987 | if (!s || *p != '.') |
| 988 | break; |
| 989 | |
| 990 | s -= 8; |
| 991 | text = ++p; |
| 992 | } |
| 993 | |
| 994 | dig100 -= ascii_zero; |
| 995 | dig10 -= ascii_zero; |
| 996 | dig1 -= ascii_zero; |
| 997 | return ((dig100 * 10) + dig10) * 10 + dig1; |
| 998 | } |
| 999 | |
| 1000 | /* |
| 1001 | * Idem except the first unparsed character has to be passed in <stop>. |
| 1002 | */ |
| 1003 | unsigned int inetaddr_host_lim(const char *text, const char *stop) |
| 1004 | { |
| 1005 | const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0'; |
| 1006 | register unsigned int dig100, dig10, dig1; |
| 1007 | int s; |
| 1008 | const char *p, *d; |
| 1009 | |
| 1010 | dig1 = dig10 = dig100 = ascii_zero; |
| 1011 | s = 24; |
| 1012 | |
| 1013 | p = text; |
| 1014 | while (1) { |
| 1015 | if (((unsigned)(*p - '0')) <= 9 && p < stop) { |
| 1016 | p++; |
| 1017 | continue; |
| 1018 | } |
| 1019 | |
| 1020 | /* here, we have a complete byte between <text> and <p> (exclusive) */ |
| 1021 | if (p == text) |
| 1022 | goto end; |
| 1023 | |
| 1024 | d = p - 1; |
| 1025 | dig1 |= (unsigned int)(*d << s); |
| 1026 | if (d == text) |
| 1027 | goto end; |
| 1028 | |
| 1029 | d--; |
| 1030 | dig10 |= (unsigned int)(*d << s); |
| 1031 | if (d == text) |
| 1032 | goto end; |
| 1033 | |
| 1034 | d--; |
| 1035 | dig100 |= (unsigned int)(*d << s); |
| 1036 | end: |
| 1037 | if (!s || p == stop || *p != '.') |
| 1038 | break; |
| 1039 | |
| 1040 | s -= 8; |
| 1041 | text = ++p; |
| 1042 | } |
| 1043 | |
| 1044 | dig100 -= ascii_zero; |
| 1045 | dig10 -= ascii_zero; |
| 1046 | dig1 -= ascii_zero; |
| 1047 | return ((dig100 * 10) + dig10) * 10 + dig1; |
| 1048 | } |
| 1049 | |
| 1050 | /* |
| 1051 | * Idem except the pointer to first unparsed byte is returned into <ret> which |
| 1052 | * must not be NULL. |
| 1053 | */ |
Willy Tarreau | 7417275 | 2010-10-15 23:21:42 +0200 | [diff] [blame] | 1054 | unsigned int inetaddr_host_lim_ret(char *text, char *stop, char **ret) |
Willy Tarreau | d54bbdc | 2009-09-07 11:00:31 +0200 | [diff] [blame] | 1055 | { |
| 1056 | const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0'; |
| 1057 | register unsigned int dig100, dig10, dig1; |
| 1058 | int s; |
Willy Tarreau | 7417275 | 2010-10-15 23:21:42 +0200 | [diff] [blame] | 1059 | char *p, *d; |
Willy Tarreau | d54bbdc | 2009-09-07 11:00:31 +0200 | [diff] [blame] | 1060 | |
| 1061 | dig1 = dig10 = dig100 = ascii_zero; |
| 1062 | s = 24; |
| 1063 | |
| 1064 | p = text; |
| 1065 | while (1) { |
| 1066 | if (((unsigned)(*p - '0')) <= 9 && p < stop) { |
| 1067 | p++; |
| 1068 | continue; |
| 1069 | } |
| 1070 | |
| 1071 | /* here, we have a complete byte between <text> and <p> (exclusive) */ |
| 1072 | if (p == text) |
| 1073 | goto end; |
| 1074 | |
| 1075 | d = p - 1; |
| 1076 | dig1 |= (unsigned int)(*d << s); |
| 1077 | if (d == text) |
| 1078 | goto end; |
| 1079 | |
| 1080 | d--; |
| 1081 | dig10 |= (unsigned int)(*d << s); |
| 1082 | if (d == text) |
| 1083 | goto end; |
| 1084 | |
| 1085 | d--; |
| 1086 | dig100 |= (unsigned int)(*d << s); |
| 1087 | end: |
| 1088 | if (!s || p == stop || *p != '.') |
| 1089 | break; |
| 1090 | |
| 1091 | s -= 8; |
| 1092 | text = ++p; |
| 1093 | } |
| 1094 | |
| 1095 | *ret = p; |
| 1096 | dig100 -= ascii_zero; |
| 1097 | dig10 -= ascii_zero; |
| 1098 | dig1 -= ascii_zero; |
| 1099 | return ((dig100 * 10) + dig10) * 10 + dig1; |
| 1100 | } |
| 1101 | |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 1102 | /* Convert a fixed-length string to an IP address. Returns 0 in case of error, |
| 1103 | * or the number of chars read in case of success. Maybe this could be replaced |
| 1104 | * by one of the functions above. Also, apparently this function does not support |
| 1105 | * hosts above 255 and requires exactly 4 octets. |
| 1106 | */ |
| 1107 | int buf2ip(const char *buf, size_t len, struct in_addr *dst) |
| 1108 | { |
| 1109 | const char *addr; |
| 1110 | int saw_digit, octets, ch; |
| 1111 | u_char tmp[4], *tp; |
| 1112 | const char *cp = buf; |
| 1113 | |
| 1114 | saw_digit = 0; |
| 1115 | octets = 0; |
| 1116 | *(tp = tmp) = 0; |
| 1117 | |
| 1118 | for (addr = buf; addr - buf < len; addr++) { |
| 1119 | unsigned char digit = (ch = *addr) - '0'; |
| 1120 | |
| 1121 | if (digit > 9 && ch != '.') |
| 1122 | break; |
| 1123 | |
| 1124 | if (digit <= 9) { |
| 1125 | u_int new = *tp * 10 + digit; |
| 1126 | |
| 1127 | if (new > 255) |
| 1128 | return 0; |
| 1129 | |
| 1130 | *tp = new; |
| 1131 | |
| 1132 | if (!saw_digit) { |
| 1133 | if (++octets > 4) |
| 1134 | return 0; |
| 1135 | saw_digit = 1; |
| 1136 | } |
| 1137 | } else if (ch == '.' && saw_digit) { |
| 1138 | if (octets == 4) |
| 1139 | return 0; |
| 1140 | |
| 1141 | *++tp = 0; |
| 1142 | saw_digit = 0; |
| 1143 | } else |
| 1144 | return 0; |
| 1145 | } |
| 1146 | |
| 1147 | if (octets < 4) |
| 1148 | return 0; |
| 1149 | |
| 1150 | memcpy(&dst->s_addr, tmp, 4); |
| 1151 | return addr - cp; |
| 1152 | } |
| 1153 | |
Willy Tarreau | acf9577 | 2010-06-14 19:09:21 +0200 | [diff] [blame] | 1154 | /* To be used to quote config arg positions. Returns the short string at <ptr> |
| 1155 | * surrounded by simple quotes if <ptr> is valid and non-empty, or "end of line" |
| 1156 | * if ptr is NULL or empty. The string is locally allocated. |
| 1157 | */ |
| 1158 | const char *quote_arg(const char *ptr) |
| 1159 | { |
| 1160 | static char val[32]; |
| 1161 | int i; |
| 1162 | |
| 1163 | if (!ptr || !*ptr) |
| 1164 | return "end of line"; |
| 1165 | val[0] = '\''; |
| 1166 | for (i = 1; i < sizeof(val) - 1 && *ptr; i++) |
| 1167 | val[i] = *ptr++; |
| 1168 | val[i++] = '\''; |
| 1169 | val[i] = '\0'; |
| 1170 | return val; |
| 1171 | } |
| 1172 | |
Willy Tarreau | 5b18020 | 2010-07-18 10:40:48 +0200 | [diff] [blame] | 1173 | /* returns an operator among STD_OP_* for string <str> or < 0 if unknown */ |
| 1174 | int get_std_op(const char *str) |
| 1175 | { |
| 1176 | int ret = -1; |
| 1177 | |
| 1178 | if (*str == 'e' && str[1] == 'q') |
| 1179 | ret = STD_OP_EQ; |
| 1180 | else if (*str == 'n' && str[1] == 'e') |
| 1181 | ret = STD_OP_NE; |
| 1182 | else if (*str == 'l') { |
| 1183 | if (str[1] == 'e') ret = STD_OP_LE; |
| 1184 | else if (str[1] == 't') ret = STD_OP_LT; |
| 1185 | } |
| 1186 | else if (*str == 'g') { |
| 1187 | if (str[1] == 'e') ret = STD_OP_GE; |
| 1188 | else if (str[1] == 't') ret = STD_OP_GT; |
| 1189 | } |
| 1190 | |
| 1191 | if (ret == -1 || str[2] != '\0') |
| 1192 | return -1; |
| 1193 | return ret; |
| 1194 | } |
| 1195 | |
Willy Tarreau | 4c14eaa | 2010-11-24 14:01:45 +0100 | [diff] [blame] | 1196 | /* hash a 32-bit integer to another 32-bit integer */ |
| 1197 | unsigned int full_hash(unsigned int a) |
| 1198 | { |
| 1199 | return __full_hash(a); |
| 1200 | } |
| 1201 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1202 | /* |
| 1203 | * Local variables: |
| 1204 | * c-indent-level: 8 |
| 1205 | * c-basic-offset: 8 |
| 1206 | * End: |
| 1207 | */ |