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