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 | |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 604 | /* Decode an URL-encoded string in-place. The resulting string might |
| 605 | * be shorter. If some forbidden characters are found, the conversion is |
| 606 | * aborted, the string is truncated before the issue and non-zero is returned, |
| 607 | * otherwise the operation returns non-zero indicating success. |
| 608 | */ |
| 609 | int url_decode(char *string) |
| 610 | { |
| 611 | char *in, *out; |
| 612 | int ret = 0; |
| 613 | |
| 614 | in = string; |
| 615 | out = string; |
| 616 | while (*in) { |
| 617 | switch (*in) { |
| 618 | case '+' : |
| 619 | *out++ = ' '; |
| 620 | break; |
| 621 | case '%' : |
| 622 | if (!ishex(in[1]) || !ishex(in[2])) |
| 623 | goto end; |
| 624 | *out++ = (hex2i(in[1]) << 4) + hex2i(in[2]); |
| 625 | in += 2; |
| 626 | break; |
| 627 | default: |
| 628 | *out++ = *in; |
| 629 | break; |
| 630 | } |
| 631 | in++; |
| 632 | } |
| 633 | ret = 1; /* success */ |
| 634 | end: |
| 635 | *out = 0; |
| 636 | return ret; |
| 637 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 638 | |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 639 | unsigned int str2ui(const char *s) |
| 640 | { |
| 641 | return __str2ui(s); |
| 642 | } |
| 643 | |
| 644 | unsigned int str2uic(const char *s) |
| 645 | { |
| 646 | return __str2uic(s); |
| 647 | } |
| 648 | |
| 649 | unsigned int strl2ui(const char *s, int len) |
| 650 | { |
| 651 | return __strl2ui(s, len); |
| 652 | } |
| 653 | |
| 654 | unsigned int strl2uic(const char *s, int len) |
| 655 | { |
| 656 | return __strl2uic(s, len); |
| 657 | } |
| 658 | |
Willy Tarreau | 4ec83cd | 2010-10-15 23:19:55 +0200 | [diff] [blame] | 659 | unsigned int read_uint(const char **s, const char *end) |
| 660 | { |
| 661 | return __read_uint(s, end); |
| 662 | } |
| 663 | |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 664 | /* This one is 7 times faster than strtol() on athlon with checks. |
| 665 | * It returns the value of the number composed of all valid digits read, |
| 666 | * and can process negative numbers too. |
| 667 | */ |
| 668 | int strl2ic(const char *s, int len) |
| 669 | { |
| 670 | int i = 0; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 671 | int j, k; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 672 | |
| 673 | if (len > 0) { |
| 674 | if (*s != '-') { |
| 675 | /* positive number */ |
| 676 | while (len-- > 0) { |
| 677 | j = (*s++) - '0'; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 678 | k = i * 10; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 679 | if (j > 9) |
| 680 | break; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 681 | i = k + j; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 682 | } |
| 683 | } else { |
| 684 | /* negative number */ |
| 685 | s++; |
| 686 | while (--len > 0) { |
| 687 | j = (*s++) - '0'; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 688 | k = i * 10; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 689 | if (j > 9) |
| 690 | break; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 691 | i = k - j; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 692 | } |
| 693 | } |
| 694 | } |
| 695 | return i; |
| 696 | } |
| 697 | |
| 698 | |
| 699 | /* This function reads exactly <len> chars from <s> and converts them to a |
| 700 | * signed integer which it stores into <ret>. It accurately detects any error |
| 701 | * (truncated string, invalid chars, overflows). It is meant to be used in |
| 702 | * applications designed for hostile environments. It returns zero when the |
| 703 | * number has successfully been converted, non-zero otherwise. When an error |
| 704 | * is returned, the <ret> value is left untouched. It is yet 5 to 40 times |
| 705 | * faster than strtol(). |
| 706 | */ |
| 707 | int strl2irc(const char *s, int len, int *ret) |
| 708 | { |
| 709 | int i = 0; |
| 710 | int j; |
| 711 | |
| 712 | if (!len) |
| 713 | return 1; |
| 714 | |
| 715 | if (*s != '-') { |
| 716 | /* positive number */ |
| 717 | while (len-- > 0) { |
| 718 | j = (*s++) - '0'; |
| 719 | if (j > 9) return 1; /* invalid char */ |
| 720 | if (i > INT_MAX / 10) return 1; /* check for multiply overflow */ |
| 721 | i = i * 10; |
| 722 | if (i + j < i) return 1; /* check for addition overflow */ |
| 723 | i = i + j; |
| 724 | } |
| 725 | } else { |
| 726 | /* negative number */ |
| 727 | s++; |
| 728 | while (--len > 0) { |
| 729 | j = (*s++) - '0'; |
| 730 | if (j > 9) return 1; /* invalid char */ |
| 731 | if (i < INT_MIN / 10) return 1; /* check for multiply overflow */ |
| 732 | i = i * 10; |
| 733 | if (i - j > i) return 1; /* check for subtract overflow */ |
| 734 | i = i - j; |
| 735 | } |
| 736 | } |
| 737 | *ret = i; |
| 738 | return 0; |
| 739 | } |
| 740 | |
| 741 | |
| 742 | /* This function reads exactly <len> chars from <s> and converts them to a |
| 743 | * signed integer which it stores into <ret>. It accurately detects any error |
| 744 | * (truncated string, invalid chars, overflows). It is meant to be used in |
| 745 | * applications designed for hostile environments. It returns zero when the |
| 746 | * number has successfully been converted, non-zero otherwise. When an error |
| 747 | * is returned, the <ret> value is left untouched. It is about 3 times slower |
| 748 | * than str2irc(). |
| 749 | */ |
| 750 | #ifndef LLONG_MAX |
| 751 | #define LLONG_MAX 9223372036854775807LL |
| 752 | #define LLONG_MIN (-LLONG_MAX - 1LL) |
| 753 | #endif |
| 754 | |
| 755 | int strl2llrc(const char *s, int len, long long *ret) |
| 756 | { |
| 757 | long long i = 0; |
| 758 | int j; |
| 759 | |
| 760 | if (!len) |
| 761 | return 1; |
| 762 | |
| 763 | if (*s != '-') { |
| 764 | /* positive number */ |
| 765 | while (len-- > 0) { |
| 766 | j = (*s++) - '0'; |
| 767 | if (j > 9) return 1; /* invalid char */ |
| 768 | if (i > LLONG_MAX / 10LL) return 1; /* check for multiply overflow */ |
| 769 | i = i * 10LL; |
| 770 | if (i + j < i) return 1; /* check for addition overflow */ |
| 771 | i = i + j; |
| 772 | } |
| 773 | } else { |
| 774 | /* negative number */ |
| 775 | s++; |
| 776 | while (--len > 0) { |
| 777 | j = (*s++) - '0'; |
| 778 | if (j > 9) return 1; /* invalid char */ |
| 779 | if (i < LLONG_MIN / 10LL) return 1; /* check for multiply overflow */ |
| 780 | i = i * 10LL; |
| 781 | if (i - j > i) return 1; /* check for subtract overflow */ |
| 782 | i = i - j; |
| 783 | } |
| 784 | } |
| 785 | *ret = i; |
| 786 | return 0; |
| 787 | } |
| 788 | |
Willy Tarreau | a0d37b6 | 2007-12-02 22:00:35 +0100 | [diff] [blame] | 789 | /* This function parses a time value optionally followed by a unit suffix among |
| 790 | * "d", "h", "m", "s", "ms" or "us". It converts the value into the unit |
| 791 | * expected by the caller. The computation does its best to avoid overflows. |
| 792 | * The value is returned in <ret> if everything is fine, and a NULL is returned |
| 793 | * by the function. In case of error, a pointer to the error is returned and |
| 794 | * <ret> is left untouched. Values are automatically rounded up when needed. |
| 795 | */ |
| 796 | const char *parse_time_err(const char *text, unsigned *ret, unsigned unit_flags) |
| 797 | { |
| 798 | unsigned imult, idiv; |
| 799 | unsigned omult, odiv; |
| 800 | unsigned value; |
| 801 | |
| 802 | omult = odiv = 1; |
| 803 | |
| 804 | switch (unit_flags & TIME_UNIT_MASK) { |
| 805 | case TIME_UNIT_US: omult = 1000000; break; |
| 806 | case TIME_UNIT_MS: omult = 1000; break; |
| 807 | case TIME_UNIT_S: break; |
| 808 | case TIME_UNIT_MIN: odiv = 60; break; |
| 809 | case TIME_UNIT_HOUR: odiv = 3600; break; |
| 810 | case TIME_UNIT_DAY: odiv = 86400; break; |
| 811 | default: break; |
| 812 | } |
| 813 | |
| 814 | value = 0; |
| 815 | |
| 816 | while (1) { |
| 817 | unsigned int j; |
| 818 | |
| 819 | j = *text - '0'; |
| 820 | if (j > 9) |
| 821 | break; |
| 822 | text++; |
| 823 | value *= 10; |
| 824 | value += j; |
| 825 | } |
| 826 | |
| 827 | imult = idiv = 1; |
| 828 | switch (*text) { |
| 829 | case '\0': /* no unit = default unit */ |
| 830 | imult = omult = idiv = odiv = 1; |
| 831 | break; |
| 832 | case 's': /* second = unscaled unit */ |
| 833 | break; |
| 834 | case 'u': /* microsecond : "us" */ |
| 835 | if (text[1] == 's') { |
| 836 | idiv = 1000000; |
| 837 | text++; |
| 838 | } |
| 839 | break; |
| 840 | case 'm': /* millisecond : "ms" or minute: "m" */ |
| 841 | if (text[1] == 's') { |
| 842 | idiv = 1000; |
| 843 | text++; |
| 844 | } else |
| 845 | imult = 60; |
| 846 | break; |
| 847 | case 'h': /* hour : "h" */ |
| 848 | imult = 3600; |
| 849 | break; |
| 850 | case 'd': /* day : "d" */ |
| 851 | imult = 86400; |
| 852 | break; |
| 853 | default: |
| 854 | return text; |
| 855 | break; |
| 856 | } |
| 857 | |
| 858 | if (omult % idiv == 0) { omult /= idiv; idiv = 1; } |
| 859 | if (idiv % omult == 0) { idiv /= omult; omult = 1; } |
| 860 | if (imult % odiv == 0) { imult /= odiv; odiv = 1; } |
| 861 | if (odiv % imult == 0) { odiv /= imult; imult = 1; } |
| 862 | |
| 863 | value = (value * (imult * omult) + (idiv * odiv - 1)) / (idiv * odiv); |
| 864 | *ret = value; |
| 865 | return NULL; |
| 866 | } |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 867 | |
Emeric Brun | 39132b2 | 2010-01-04 14:57:24 +0100 | [diff] [blame] | 868 | /* this function converts the string starting at <text> to an unsigned int |
| 869 | * stored in <ret>. If an error is detected, the pointer to the unexpected |
| 870 | * character is returned. If the conversio is succesful, NULL is returned. |
| 871 | */ |
| 872 | const char *parse_size_err(const char *text, unsigned *ret) { |
| 873 | unsigned value = 0; |
| 874 | |
| 875 | while (1) { |
| 876 | unsigned int j; |
| 877 | |
| 878 | j = *text - '0'; |
| 879 | if (j > 9) |
| 880 | break; |
| 881 | if (value > ~0U / 10) |
| 882 | return text; |
| 883 | value *= 10; |
| 884 | if (value > (value + j)) |
| 885 | return text; |
| 886 | value += j; |
| 887 | text++; |
| 888 | } |
| 889 | |
| 890 | switch (*text) { |
| 891 | case '\0': |
| 892 | break; |
| 893 | case 'K': |
| 894 | case 'k': |
| 895 | if (value > ~0U >> 10) |
| 896 | return text; |
| 897 | value = value << 10; |
| 898 | break; |
| 899 | case 'M': |
| 900 | case 'm': |
| 901 | if (value > ~0U >> 20) |
| 902 | return text; |
| 903 | value = value << 20; |
| 904 | break; |
| 905 | case 'G': |
| 906 | case 'g': |
| 907 | if (value > ~0U >> 30) |
| 908 | return text; |
| 909 | value = value << 30; |
| 910 | break; |
| 911 | default: |
| 912 | return text; |
| 913 | } |
| 914 | |
| 915 | *ret = value; |
| 916 | return NULL; |
| 917 | } |
| 918 | |
Willy Tarreau | 946ba59 | 2009-05-10 15:41:18 +0200 | [diff] [blame] | 919 | /* copies at most <n> characters from <src> and always terminates with '\0' */ |
| 920 | char *my_strndup(const char *src, int n) |
| 921 | { |
| 922 | int len = 0; |
| 923 | char *ret; |
| 924 | |
| 925 | while (len < n && src[len]) |
| 926 | len++; |
| 927 | |
| 928 | ret = (char *)malloc(len + 1); |
| 929 | if (!ret) |
| 930 | return ret; |
| 931 | memcpy(ret, src, len); |
| 932 | ret[len] = '\0'; |
| 933 | return ret; |
| 934 | } |
| 935 | |
Willy Tarreau | 482b00d | 2009-10-04 22:48:42 +0200 | [diff] [blame] | 936 | /* This function returns the first unused key greater than or equal to <key> in |
| 937 | * ID tree <root>. Zero is returned if no place is found. |
| 938 | */ |
| 939 | unsigned int get_next_id(struct eb_root *root, unsigned int key) |
| 940 | { |
| 941 | struct eb32_node *used; |
| 942 | |
| 943 | do { |
| 944 | used = eb32_lookup_ge(root, key); |
| 945 | if (!used || used->key > key) |
| 946 | return key; /* key is available */ |
| 947 | key++; |
| 948 | } while (key); |
| 949 | return key; |
| 950 | } |
| 951 | |
Willy Tarreau | 348238b | 2010-01-18 15:05:57 +0100 | [diff] [blame] | 952 | /* This function compares a sample word possibly followed by blanks to another |
| 953 | * clean word. The compare is case-insensitive. 1 is returned if both are equal, |
| 954 | * otherwise zero. This intends to be used when checking HTTP headers for some |
| 955 | * values. Note that it validates a word followed only by blanks but does not |
| 956 | * validate a word followed by blanks then other chars. |
| 957 | */ |
| 958 | int word_match(const char *sample, int slen, const char *word, int wlen) |
| 959 | { |
| 960 | if (slen < wlen) |
| 961 | return 0; |
| 962 | |
| 963 | while (wlen) { |
| 964 | char c = *sample ^ *word; |
| 965 | if (c && c != ('A' ^ 'a')) |
| 966 | return 0; |
| 967 | sample++; |
| 968 | word++; |
| 969 | slen--; |
| 970 | wlen--; |
| 971 | } |
| 972 | |
| 973 | while (slen) { |
| 974 | if (*sample != ' ' && *sample != '\t') |
| 975 | return 0; |
| 976 | sample++; |
| 977 | slen--; |
| 978 | } |
| 979 | return 1; |
| 980 | } |
Willy Tarreau | 482b00d | 2009-10-04 22:48:42 +0200 | [diff] [blame] | 981 | |
Willy Tarreau | d54bbdc | 2009-09-07 11:00:31 +0200 | [diff] [blame] | 982 | /* Converts any text-formatted IPv4 address to a host-order IPv4 address. It |
| 983 | * is particularly fast because it avoids expensive operations such as |
| 984 | * multiplies, which are optimized away at the end. It requires a properly |
| 985 | * formated address though (3 points). |
| 986 | */ |
| 987 | unsigned int inetaddr_host(const char *text) |
| 988 | { |
| 989 | const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0'; |
| 990 | register unsigned int dig100, dig10, dig1; |
| 991 | int s; |
| 992 | const char *p, *d; |
| 993 | |
| 994 | dig1 = dig10 = dig100 = ascii_zero; |
| 995 | s = 24; |
| 996 | |
| 997 | p = text; |
| 998 | while (1) { |
| 999 | if (((unsigned)(*p - '0')) <= 9) { |
| 1000 | p++; |
| 1001 | continue; |
| 1002 | } |
| 1003 | |
| 1004 | /* here, we have a complete byte between <text> and <p> (exclusive) */ |
| 1005 | if (p == text) |
| 1006 | goto end; |
| 1007 | |
| 1008 | d = p - 1; |
| 1009 | dig1 |= (unsigned int)(*d << s); |
| 1010 | if (d == text) |
| 1011 | goto end; |
| 1012 | |
| 1013 | d--; |
| 1014 | dig10 |= (unsigned int)(*d << s); |
| 1015 | if (d == text) |
| 1016 | goto end; |
| 1017 | |
| 1018 | d--; |
| 1019 | dig100 |= (unsigned int)(*d << s); |
| 1020 | end: |
| 1021 | if (!s || *p != '.') |
| 1022 | break; |
| 1023 | |
| 1024 | s -= 8; |
| 1025 | text = ++p; |
| 1026 | } |
| 1027 | |
| 1028 | dig100 -= ascii_zero; |
| 1029 | dig10 -= ascii_zero; |
| 1030 | dig1 -= ascii_zero; |
| 1031 | return ((dig100 * 10) + dig10) * 10 + dig1; |
| 1032 | } |
| 1033 | |
| 1034 | /* |
| 1035 | * Idem except the first unparsed character has to be passed in <stop>. |
| 1036 | */ |
| 1037 | unsigned int inetaddr_host_lim(const char *text, const char *stop) |
| 1038 | { |
| 1039 | const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0'; |
| 1040 | register unsigned int dig100, dig10, dig1; |
| 1041 | int s; |
| 1042 | const char *p, *d; |
| 1043 | |
| 1044 | dig1 = dig10 = dig100 = ascii_zero; |
| 1045 | s = 24; |
| 1046 | |
| 1047 | p = text; |
| 1048 | while (1) { |
| 1049 | if (((unsigned)(*p - '0')) <= 9 && p < stop) { |
| 1050 | p++; |
| 1051 | continue; |
| 1052 | } |
| 1053 | |
| 1054 | /* here, we have a complete byte between <text> and <p> (exclusive) */ |
| 1055 | if (p == text) |
| 1056 | goto end; |
| 1057 | |
| 1058 | d = p - 1; |
| 1059 | dig1 |= (unsigned int)(*d << s); |
| 1060 | if (d == text) |
| 1061 | goto end; |
| 1062 | |
| 1063 | d--; |
| 1064 | dig10 |= (unsigned int)(*d << s); |
| 1065 | if (d == text) |
| 1066 | goto end; |
| 1067 | |
| 1068 | d--; |
| 1069 | dig100 |= (unsigned int)(*d << s); |
| 1070 | end: |
| 1071 | if (!s || p == stop || *p != '.') |
| 1072 | break; |
| 1073 | |
| 1074 | s -= 8; |
| 1075 | text = ++p; |
| 1076 | } |
| 1077 | |
| 1078 | dig100 -= ascii_zero; |
| 1079 | dig10 -= ascii_zero; |
| 1080 | dig1 -= ascii_zero; |
| 1081 | return ((dig100 * 10) + dig10) * 10 + dig1; |
| 1082 | } |
| 1083 | |
| 1084 | /* |
| 1085 | * Idem except the pointer to first unparsed byte is returned into <ret> which |
| 1086 | * must not be NULL. |
| 1087 | */ |
Willy Tarreau | 7417275 | 2010-10-15 23:21:42 +0200 | [diff] [blame] | 1088 | unsigned int inetaddr_host_lim_ret(char *text, char *stop, char **ret) |
Willy Tarreau | d54bbdc | 2009-09-07 11:00:31 +0200 | [diff] [blame] | 1089 | { |
| 1090 | const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0'; |
| 1091 | register unsigned int dig100, dig10, dig1; |
| 1092 | int s; |
Willy Tarreau | 7417275 | 2010-10-15 23:21:42 +0200 | [diff] [blame] | 1093 | char *p, *d; |
Willy Tarreau | d54bbdc | 2009-09-07 11:00:31 +0200 | [diff] [blame] | 1094 | |
| 1095 | dig1 = dig10 = dig100 = ascii_zero; |
| 1096 | s = 24; |
| 1097 | |
| 1098 | p = text; |
| 1099 | while (1) { |
| 1100 | if (((unsigned)(*p - '0')) <= 9 && p < stop) { |
| 1101 | p++; |
| 1102 | continue; |
| 1103 | } |
| 1104 | |
| 1105 | /* here, we have a complete byte between <text> and <p> (exclusive) */ |
| 1106 | if (p == text) |
| 1107 | goto end; |
| 1108 | |
| 1109 | d = p - 1; |
| 1110 | dig1 |= (unsigned int)(*d << s); |
| 1111 | if (d == text) |
| 1112 | goto end; |
| 1113 | |
| 1114 | d--; |
| 1115 | dig10 |= (unsigned int)(*d << s); |
| 1116 | if (d == text) |
| 1117 | goto end; |
| 1118 | |
| 1119 | d--; |
| 1120 | dig100 |= (unsigned int)(*d << s); |
| 1121 | end: |
| 1122 | if (!s || p == stop || *p != '.') |
| 1123 | break; |
| 1124 | |
| 1125 | s -= 8; |
| 1126 | text = ++p; |
| 1127 | } |
| 1128 | |
| 1129 | *ret = p; |
| 1130 | dig100 -= ascii_zero; |
| 1131 | dig10 -= ascii_zero; |
| 1132 | dig1 -= ascii_zero; |
| 1133 | return ((dig100 * 10) + dig10) * 10 + dig1; |
| 1134 | } |
| 1135 | |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 1136 | /* Convert a fixed-length string to an IP address. Returns 0 in case of error, |
| 1137 | * or the number of chars read in case of success. Maybe this could be replaced |
| 1138 | * by one of the functions above. Also, apparently this function does not support |
| 1139 | * hosts above 255 and requires exactly 4 octets. |
| 1140 | */ |
| 1141 | int buf2ip(const char *buf, size_t len, struct in_addr *dst) |
| 1142 | { |
| 1143 | const char *addr; |
| 1144 | int saw_digit, octets, ch; |
| 1145 | u_char tmp[4], *tp; |
| 1146 | const char *cp = buf; |
| 1147 | |
| 1148 | saw_digit = 0; |
| 1149 | octets = 0; |
| 1150 | *(tp = tmp) = 0; |
| 1151 | |
| 1152 | for (addr = buf; addr - buf < len; addr++) { |
| 1153 | unsigned char digit = (ch = *addr) - '0'; |
| 1154 | |
| 1155 | if (digit > 9 && ch != '.') |
| 1156 | break; |
| 1157 | |
| 1158 | if (digit <= 9) { |
| 1159 | u_int new = *tp * 10 + digit; |
| 1160 | |
| 1161 | if (new > 255) |
| 1162 | return 0; |
| 1163 | |
| 1164 | *tp = new; |
| 1165 | |
| 1166 | if (!saw_digit) { |
| 1167 | if (++octets > 4) |
| 1168 | return 0; |
| 1169 | saw_digit = 1; |
| 1170 | } |
| 1171 | } else if (ch == '.' && saw_digit) { |
| 1172 | if (octets == 4) |
| 1173 | return 0; |
| 1174 | |
| 1175 | *++tp = 0; |
| 1176 | saw_digit = 0; |
| 1177 | } else |
| 1178 | return 0; |
| 1179 | } |
| 1180 | |
| 1181 | if (octets < 4) |
| 1182 | return 0; |
| 1183 | |
| 1184 | memcpy(&dst->s_addr, tmp, 4); |
| 1185 | return addr - cp; |
| 1186 | } |
| 1187 | |
Willy Tarreau | acf9577 | 2010-06-14 19:09:21 +0200 | [diff] [blame] | 1188 | /* To be used to quote config arg positions. Returns the short string at <ptr> |
| 1189 | * surrounded by simple quotes if <ptr> is valid and non-empty, or "end of line" |
| 1190 | * if ptr is NULL or empty. The string is locally allocated. |
| 1191 | */ |
| 1192 | const char *quote_arg(const char *ptr) |
| 1193 | { |
| 1194 | static char val[32]; |
| 1195 | int i; |
| 1196 | |
| 1197 | if (!ptr || !*ptr) |
| 1198 | return "end of line"; |
| 1199 | val[0] = '\''; |
| 1200 | for (i = 1; i < sizeof(val) - 1 && *ptr; i++) |
| 1201 | val[i] = *ptr++; |
| 1202 | val[i++] = '\''; |
| 1203 | val[i] = '\0'; |
| 1204 | return val; |
| 1205 | } |
| 1206 | |
Willy Tarreau | 5b18020 | 2010-07-18 10:40:48 +0200 | [diff] [blame] | 1207 | /* returns an operator among STD_OP_* for string <str> or < 0 if unknown */ |
| 1208 | int get_std_op(const char *str) |
| 1209 | { |
| 1210 | int ret = -1; |
| 1211 | |
| 1212 | if (*str == 'e' && str[1] == 'q') |
| 1213 | ret = STD_OP_EQ; |
| 1214 | else if (*str == 'n' && str[1] == 'e') |
| 1215 | ret = STD_OP_NE; |
| 1216 | else if (*str == 'l') { |
| 1217 | if (str[1] == 'e') ret = STD_OP_LE; |
| 1218 | else if (str[1] == 't') ret = STD_OP_LT; |
| 1219 | } |
| 1220 | else if (*str == 'g') { |
| 1221 | if (str[1] == 'e') ret = STD_OP_GE; |
| 1222 | else if (str[1] == 't') ret = STD_OP_GT; |
| 1223 | } |
| 1224 | |
| 1225 | if (ret == -1 || str[2] != '\0') |
| 1226 | return -1; |
| 1227 | return ret; |
| 1228 | } |
| 1229 | |
Willy Tarreau | 4c14eaa | 2010-11-24 14:01:45 +0100 | [diff] [blame] | 1230 | /* hash a 32-bit integer to another 32-bit integer */ |
| 1231 | unsigned int full_hash(unsigned int a) |
| 1232 | { |
| 1233 | return __full_hash(a); |
| 1234 | } |
| 1235 | |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 1236 | /* Return non-zero if IPv4 address is part of the network, |
| 1237 | * otherwise zero. |
| 1238 | */ |
| 1239 | int in_net_ipv4(struct in_addr *addr, struct in_addr *mask, struct in_addr *net) |
| 1240 | { |
| 1241 | return((addr->s_addr & mask->s_addr) == (net->s_addr & mask->s_addr)); |
| 1242 | } |
| 1243 | |
| 1244 | /* Return non-zero if IPv6 address is part of the network, |
| 1245 | * otherwise zero. |
| 1246 | */ |
| 1247 | int in_net_ipv6(struct in6_addr *addr, struct in6_addr *mask, struct in6_addr *net) |
| 1248 | { |
| 1249 | int i; |
| 1250 | |
| 1251 | for (i = 0; i < sizeof(struct in6_addr) / sizeof(int); i++) |
| 1252 | if (((((int *)addr)[i] & ((int *)mask)[i])) != |
| 1253 | (((int *)net)[i] & ((int *)mask)[i])) |
| 1254 | return 0; |
| 1255 | return 1; |
| 1256 | } |
| 1257 | |
| 1258 | /* RFC 4291 prefix */ |
| 1259 | const char rfc4291_pfx[] = { 0x00, 0x00, 0x00, 0x00, |
| 1260 | 0x00, 0x00, 0x00, 0x00, |
| 1261 | 0x00, 0x00, 0xFF, 0xFF }; |
| 1262 | |
| 1263 | /* Map IPv4 adress on IPv6 address, as specified in RFC 3513. */ |
| 1264 | void v4tov6(struct in6_addr *sin6_addr, struct in_addr *sin_addr) |
| 1265 | { |
| 1266 | memcpy(sin6_addr->s6_addr, rfc4291_pfx, sizeof(rfc4291_pfx)); |
| 1267 | memcpy(sin6_addr->s6_addr+12, &sin_addr->s_addr, 4); |
| 1268 | } |
| 1269 | |
| 1270 | /* Map IPv6 adress on IPv4 address, as specified in RFC 3513. |
| 1271 | * Return true if conversion is possible and false otherwise. |
| 1272 | */ |
| 1273 | int v6tov4(struct in_addr *sin_addr, struct in6_addr *sin6_addr) |
| 1274 | { |
| 1275 | if (memcmp(sin6_addr->s6_addr, rfc4291_pfx, sizeof(rfc4291_pfx)) == 0) { |
| 1276 | memcpy(&(sin_addr->s_addr), &(sin6_addr->s6_addr[12]), |
| 1277 | sizeof(struct in_addr)); |
| 1278 | return 1; |
| 1279 | } |
| 1280 | |
| 1281 | return 0; |
| 1282 | } |
| 1283 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1284 | /* |
| 1285 | * Local variables: |
| 1286 | * c-indent-level: 8 |
| 1287 | * c-basic-offset: 8 |
| 1288 | * End: |
| 1289 | */ |