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