Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * General purpose functions. |
| 3 | * |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 4 | * Copyright 2000-2007 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 | |
| 13 | #include <netdb.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <string.h> |
| 16 | #include <netinet/in.h> |
| 17 | #include <arpa/inet.h> |
| 18 | |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 19 | #include <common/config.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 20 | #include <common/standard.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | #include <proto/log.h> |
| 22 | |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 23 | /* enough to store 2^64-1 = 18446744073709551615 */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 24 | static char itoa_str[21]; |
| 25 | |
| 26 | /* |
| 27 | * copies at most <size-1> chars from <src> to <dst>. Last char is always |
| 28 | * set to 0, unless <size> is 0. The number of chars copied is returned |
| 29 | * (excluding the terminating zero). |
| 30 | * This code has been optimized for size and speed : on x86, it's 45 bytes |
| 31 | * long, uses only registers, and consumes only 4 cycles per char. |
| 32 | */ |
| 33 | int strlcpy2(char *dst, const char *src, int size) |
| 34 | { |
| 35 | char *orig = dst; |
| 36 | if (size) { |
| 37 | while (--size && (*dst = *src)) { |
| 38 | src++; dst++; |
| 39 | } |
| 40 | *dst = 0; |
| 41 | } |
| 42 | return dst - orig; |
| 43 | } |
| 44 | |
| 45 | /* |
| 46 | * This function simply returns a statically allocated string containing |
| 47 | * the ascii representation for number 'n' in decimal. |
| 48 | */ |
| 49 | char *ultoa(unsigned long n) |
| 50 | { |
| 51 | char *pos; |
| 52 | |
| 53 | pos = itoa_str + sizeof(itoa_str) - 1; |
| 54 | *pos-- = '\0'; |
| 55 | |
| 56 | do { |
| 57 | *pos-- = '0' + n % 10; |
| 58 | n /= 10; |
| 59 | } while (n && pos >= itoa_str); |
| 60 | return pos + 1; |
| 61 | } |
| 62 | |
| 63 | |
| 64 | /* |
| 65 | * Returns non-zero if character <s> is a hex digit (0-9, a-f, A-F), else zero. |
| 66 | * |
| 67 | * It looks like this one would be a good candidate for inlining, but this is |
| 68 | * not interesting because it around 35 bytes long and often called multiple |
| 69 | * times within the same function. |
| 70 | */ |
| 71 | int ishex(char s) |
| 72 | { |
| 73 | s -= '0'; |
| 74 | if ((unsigned char)s <= 9) |
| 75 | return 1; |
| 76 | s -= 'A' - '0'; |
| 77 | if ((unsigned char)s <= 5) |
| 78 | return 1; |
| 79 | s -= 'a' - 'A'; |
| 80 | if ((unsigned char)s <= 5) |
| 81 | return 1; |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | /* |
| 87 | * converts <str> to a struct sockaddr_in* which is locally allocated. |
| 88 | * The format is "addr:port", where "addr" can be a dotted IPv4 address, |
| 89 | * a host name, or empty or "*" to indicate INADDR_ANY. |
| 90 | */ |
| 91 | struct sockaddr_in *str2sa(char *str) |
| 92 | { |
| 93 | static struct sockaddr_in sa; |
| 94 | char *c; |
| 95 | int port; |
| 96 | |
| 97 | memset(&sa, 0, sizeof(sa)); |
| 98 | str = strdup(str); |
Willy Tarreau | c642348 | 2006-10-15 14:59:03 +0200 | [diff] [blame] | 99 | if (str == NULL) |
| 100 | goto out_nofree; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 101 | |
| 102 | if ((c = strrchr(str,':')) != NULL) { |
| 103 | *c++ = '\0'; |
| 104 | port = atol(c); |
| 105 | } |
| 106 | else |
| 107 | port = 0; |
| 108 | |
| 109 | if (*str == '*' || *str == '\0') { /* INADDR_ANY */ |
| 110 | sa.sin_addr.s_addr = INADDR_ANY; |
| 111 | } |
| 112 | else if (!inet_pton(AF_INET, str, &sa.sin_addr)) { |
| 113 | struct hostent *he; |
| 114 | |
| 115 | if ((he = gethostbyname(str)) == NULL) { |
| 116 | Alert("Invalid server name: '%s'\n", str); |
| 117 | } |
| 118 | else |
| 119 | sa.sin_addr = *(struct in_addr *) *(he->h_addr_list); |
| 120 | } |
| 121 | sa.sin_port = htons(port); |
| 122 | sa.sin_family = AF_INET; |
| 123 | |
| 124 | free(str); |
Willy Tarreau | c642348 | 2006-10-15 14:59:03 +0200 | [diff] [blame] | 125 | out_nofree: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 126 | return &sa; |
| 127 | } |
| 128 | |
| 129 | /* |
Willy Tarreau | d077a8e | 2007-05-08 18:28:09 +0200 | [diff] [blame] | 130 | * converts <str> to two struct in_addr* which must be pre-allocated. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 131 | * The format is "addr[/mask]", where "addr" cannot be empty, and mask |
| 132 | * is optionnal and either in the dotted or CIDR notation. |
| 133 | * Note: "addr" can also be a hostname. Returns 1 if OK, 0 if error. |
| 134 | */ |
Willy Tarreau | d077a8e | 2007-05-08 18:28:09 +0200 | [diff] [blame] | 135 | 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] | 136 | { |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 137 | __label__ out_free, out_err; |
| 138 | char *c, *s; |
| 139 | int ret_val; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 140 | unsigned long len; |
| 141 | |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 142 | s = strdup(str); |
| 143 | if (!s) |
| 144 | return 0; |
| 145 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 146 | memset(mask, 0, sizeof(*mask)); |
| 147 | memset(addr, 0, sizeof(*addr)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 148 | |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 149 | if ((c = strrchr(s, '/')) != NULL) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 150 | *c++ = '\0'; |
| 151 | /* c points to the mask */ |
| 152 | if (strchr(c, '.') != NULL) { /* dotted notation */ |
| 153 | if (!inet_pton(AF_INET, c, mask)) |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 154 | goto out_err; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 155 | } |
| 156 | else { /* mask length */ |
| 157 | char *err; |
| 158 | len = strtol(c, &err, 10); |
| 159 | if (!*c || (err && *err) || (unsigned)len > 32) |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 160 | goto out_err; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 161 | if (len) |
| 162 | mask->s_addr = htonl(~0UL << (32 - len)); |
| 163 | else |
| 164 | mask->s_addr = 0; |
| 165 | } |
| 166 | } |
| 167 | else { |
Willy Tarreau | ebd6160 | 2006-12-30 11:54:15 +0100 | [diff] [blame] | 168 | mask->s_addr = ~0U; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 169 | } |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 170 | if (!inet_pton(AF_INET, s, addr)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 171 | struct hostent *he; |
| 172 | |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 173 | if ((he = gethostbyname(s)) == NULL) { |
| 174 | goto out_err; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 175 | } |
| 176 | else |
| 177 | *addr = *(struct in_addr *) *(he->h_addr_list); |
| 178 | } |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 179 | |
| 180 | ret_val = 1; |
| 181 | out_free: |
| 182 | free(s); |
| 183 | return ret_val; |
| 184 | out_err: |
| 185 | ret_val = 0; |
| 186 | goto out_free; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /* will try to encode the string <string> replacing all characters tagged in |
| 190 | * <map> with the hexadecimal representation of their ASCII-code (2 digits) |
| 191 | * prefixed by <escape>, and will store the result between <start> (included) |
| 192 | * and <stop> (excluded), and will always terminate the string with a '\0' |
| 193 | * before <stop>. The position of the '\0' is returned if the conversion |
| 194 | * completes. If bytes are missing between <start> and <stop>, then the |
| 195 | * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0' |
| 196 | * cannot even be stored so we return <start> without writing the 0. |
| 197 | * The input string must also be zero-terminated. |
| 198 | */ |
| 199 | const char hextab[16] = "0123456789ABCDEF"; |
| 200 | char *encode_string(char *start, char *stop, |
| 201 | const char escape, const fd_set *map, |
| 202 | const char *string) |
| 203 | { |
| 204 | if (start < stop) { |
| 205 | stop--; /* reserve one byte for the final '\0' */ |
| 206 | while (start < stop && *string != '\0') { |
| 207 | if (!FD_ISSET((unsigned char)(*string), map)) |
| 208 | *start++ = *string; |
| 209 | else { |
| 210 | if (start + 3 >= stop) |
| 211 | break; |
| 212 | *start++ = escape; |
| 213 | *start++ = hextab[(*string >> 4) & 15]; |
| 214 | *start++ = hextab[*string & 15]; |
| 215 | } |
| 216 | string++; |
| 217 | } |
| 218 | *start = '\0'; |
| 219 | } |
| 220 | return start; |
| 221 | } |
| 222 | |
| 223 | |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 224 | unsigned int str2ui(const char *s) |
| 225 | { |
| 226 | return __str2ui(s); |
| 227 | } |
| 228 | |
| 229 | unsigned int str2uic(const char *s) |
| 230 | { |
| 231 | return __str2uic(s); |
| 232 | } |
| 233 | |
| 234 | unsigned int strl2ui(const char *s, int len) |
| 235 | { |
| 236 | return __strl2ui(s, len); |
| 237 | } |
| 238 | |
| 239 | unsigned int strl2uic(const char *s, int len) |
| 240 | { |
| 241 | return __strl2uic(s, len); |
| 242 | } |
| 243 | |
| 244 | /* This one is 7 times faster than strtol() on athlon with checks. |
| 245 | * It returns the value of the number composed of all valid digits read, |
| 246 | * and can process negative numbers too. |
| 247 | */ |
| 248 | int strl2ic(const char *s, int len) |
| 249 | { |
| 250 | int i = 0; |
| 251 | int j; |
| 252 | |
| 253 | if (len > 0) { |
| 254 | if (*s != '-') { |
| 255 | /* positive number */ |
| 256 | while (len-- > 0) { |
| 257 | j = (*s++) - '0'; |
| 258 | i = i * 10; |
| 259 | if (j > 9) |
| 260 | break; |
| 261 | i += j; |
| 262 | } |
| 263 | } else { |
| 264 | /* negative number */ |
| 265 | s++; |
| 266 | while (--len > 0) { |
| 267 | j = (*s++) - '0'; |
| 268 | i = i * 10; |
| 269 | if (j > 9) |
| 270 | break; |
| 271 | i -= j; |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | return i; |
| 276 | } |
| 277 | |
| 278 | |
| 279 | /* This function reads exactly <len> chars from <s> and converts them to a |
| 280 | * signed integer which it stores into <ret>. It accurately detects any error |
| 281 | * (truncated string, invalid chars, overflows). It is meant to be used in |
| 282 | * applications designed for hostile environments. It returns zero when the |
| 283 | * number has successfully been converted, non-zero otherwise. When an error |
| 284 | * is returned, the <ret> value is left untouched. It is yet 5 to 40 times |
| 285 | * faster than strtol(). |
| 286 | */ |
| 287 | int strl2irc(const char *s, int len, int *ret) |
| 288 | { |
| 289 | int i = 0; |
| 290 | int j; |
| 291 | |
| 292 | if (!len) |
| 293 | return 1; |
| 294 | |
| 295 | if (*s != '-') { |
| 296 | /* positive number */ |
| 297 | while (len-- > 0) { |
| 298 | j = (*s++) - '0'; |
| 299 | if (j > 9) return 1; /* invalid char */ |
| 300 | if (i > INT_MAX / 10) return 1; /* check for multiply overflow */ |
| 301 | i = i * 10; |
| 302 | if (i + j < i) return 1; /* check for addition overflow */ |
| 303 | i = i + j; |
| 304 | } |
| 305 | } else { |
| 306 | /* negative number */ |
| 307 | s++; |
| 308 | while (--len > 0) { |
| 309 | j = (*s++) - '0'; |
| 310 | if (j > 9) return 1; /* invalid char */ |
| 311 | if (i < INT_MIN / 10) return 1; /* check for multiply overflow */ |
| 312 | i = i * 10; |
| 313 | if (i - j > i) return 1; /* check for subtract overflow */ |
| 314 | i = i - j; |
| 315 | } |
| 316 | } |
| 317 | *ret = i; |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | |
| 322 | /* This function reads exactly <len> chars from <s> and converts them to a |
| 323 | * signed integer which it stores into <ret>. It accurately detects any error |
| 324 | * (truncated string, invalid chars, overflows). It is meant to be used in |
| 325 | * applications designed for hostile environments. It returns zero when the |
| 326 | * number has successfully been converted, non-zero otherwise. When an error |
| 327 | * is returned, the <ret> value is left untouched. It is about 3 times slower |
| 328 | * than str2irc(). |
| 329 | */ |
| 330 | #ifndef LLONG_MAX |
| 331 | #define LLONG_MAX 9223372036854775807LL |
| 332 | #define LLONG_MIN (-LLONG_MAX - 1LL) |
| 333 | #endif |
| 334 | |
| 335 | int strl2llrc(const char *s, int len, long long *ret) |
| 336 | { |
| 337 | long long i = 0; |
| 338 | int j; |
| 339 | |
| 340 | if (!len) |
| 341 | return 1; |
| 342 | |
| 343 | if (*s != '-') { |
| 344 | /* positive number */ |
| 345 | while (len-- > 0) { |
| 346 | j = (*s++) - '0'; |
| 347 | if (j > 9) return 1; /* invalid char */ |
| 348 | if (i > LLONG_MAX / 10LL) return 1; /* check for multiply overflow */ |
| 349 | i = i * 10LL; |
| 350 | if (i + j < i) return 1; /* check for addition overflow */ |
| 351 | i = i + j; |
| 352 | } |
| 353 | } else { |
| 354 | /* negative number */ |
| 355 | s++; |
| 356 | while (--len > 0) { |
| 357 | j = (*s++) - '0'; |
| 358 | if (j > 9) return 1; /* invalid char */ |
| 359 | if (i < LLONG_MIN / 10LL) return 1; /* check for multiply overflow */ |
| 360 | i = i * 10LL; |
| 361 | if (i - j > i) return 1; /* check for subtract overflow */ |
| 362 | i = i - j; |
| 363 | } |
| 364 | } |
| 365 | *ret = i; |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 370 | /* |
| 371 | * Local variables: |
| 372 | * c-indent-level: 8 |
| 373 | * c-basic-offset: 8 |
| 374 | * End: |
| 375 | */ |