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 | { |
| 137 | char *c; |
| 138 | unsigned long len; |
| 139 | |
| 140 | memset(mask, 0, sizeof(*mask)); |
| 141 | memset(addr, 0, sizeof(*addr)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 142 | |
| 143 | if ((c = strrchr(str, '/')) != NULL) { |
| 144 | *c++ = '\0'; |
| 145 | /* c points to the mask */ |
| 146 | if (strchr(c, '.') != NULL) { /* dotted notation */ |
| 147 | if (!inet_pton(AF_INET, c, mask)) |
| 148 | return 0; |
| 149 | } |
| 150 | else { /* mask length */ |
| 151 | char *err; |
| 152 | len = strtol(c, &err, 10); |
| 153 | if (!*c || (err && *err) || (unsigned)len > 32) |
| 154 | return 0; |
| 155 | if (len) |
| 156 | mask->s_addr = htonl(~0UL << (32 - len)); |
| 157 | else |
| 158 | mask->s_addr = 0; |
| 159 | } |
| 160 | } |
| 161 | else { |
Willy Tarreau | ebd6160 | 2006-12-30 11:54:15 +0100 | [diff] [blame] | 162 | mask->s_addr = ~0U; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 163 | } |
| 164 | if (!inet_pton(AF_INET, str, addr)) { |
| 165 | struct hostent *he; |
| 166 | |
| 167 | if ((he = gethostbyname(str)) == NULL) { |
| 168 | return 0; |
| 169 | } |
| 170 | else |
| 171 | *addr = *(struct in_addr *) *(he->h_addr_list); |
| 172 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 173 | return 1; |
| 174 | } |
| 175 | |
| 176 | /* will try to encode the string <string> replacing all characters tagged in |
| 177 | * <map> with the hexadecimal representation of their ASCII-code (2 digits) |
| 178 | * prefixed by <escape>, and will store the result between <start> (included) |
| 179 | * and <stop> (excluded), and will always terminate the string with a '\0' |
| 180 | * before <stop>. The position of the '\0' is returned if the conversion |
| 181 | * completes. If bytes are missing between <start> and <stop>, then the |
| 182 | * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0' |
| 183 | * cannot even be stored so we return <start> without writing the 0. |
| 184 | * The input string must also be zero-terminated. |
| 185 | */ |
| 186 | const char hextab[16] = "0123456789ABCDEF"; |
| 187 | char *encode_string(char *start, char *stop, |
| 188 | const char escape, const fd_set *map, |
| 189 | const char *string) |
| 190 | { |
| 191 | if (start < stop) { |
| 192 | stop--; /* reserve one byte for the final '\0' */ |
| 193 | while (start < stop && *string != '\0') { |
| 194 | if (!FD_ISSET((unsigned char)(*string), map)) |
| 195 | *start++ = *string; |
| 196 | else { |
| 197 | if (start + 3 >= stop) |
| 198 | break; |
| 199 | *start++ = escape; |
| 200 | *start++ = hextab[(*string >> 4) & 15]; |
| 201 | *start++ = hextab[*string & 15]; |
| 202 | } |
| 203 | string++; |
| 204 | } |
| 205 | *start = '\0'; |
| 206 | } |
| 207 | return start; |
| 208 | } |
| 209 | |
| 210 | |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 211 | unsigned int str2ui(const char *s) |
| 212 | { |
| 213 | return __str2ui(s); |
| 214 | } |
| 215 | |
| 216 | unsigned int str2uic(const char *s) |
| 217 | { |
| 218 | return __str2uic(s); |
| 219 | } |
| 220 | |
| 221 | unsigned int strl2ui(const char *s, int len) |
| 222 | { |
| 223 | return __strl2ui(s, len); |
| 224 | } |
| 225 | |
| 226 | unsigned int strl2uic(const char *s, int len) |
| 227 | { |
| 228 | return __strl2uic(s, len); |
| 229 | } |
| 230 | |
| 231 | /* This one is 7 times faster than strtol() on athlon with checks. |
| 232 | * It returns the value of the number composed of all valid digits read, |
| 233 | * and can process negative numbers too. |
| 234 | */ |
| 235 | int strl2ic(const char *s, int len) |
| 236 | { |
| 237 | int i = 0; |
| 238 | int j; |
| 239 | |
| 240 | if (len > 0) { |
| 241 | if (*s != '-') { |
| 242 | /* positive number */ |
| 243 | while (len-- > 0) { |
| 244 | j = (*s++) - '0'; |
| 245 | i = i * 10; |
| 246 | if (j > 9) |
| 247 | break; |
| 248 | i += j; |
| 249 | } |
| 250 | } else { |
| 251 | /* negative number */ |
| 252 | s++; |
| 253 | while (--len > 0) { |
| 254 | j = (*s++) - '0'; |
| 255 | i = i * 10; |
| 256 | if (j > 9) |
| 257 | break; |
| 258 | i -= j; |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | return i; |
| 263 | } |
| 264 | |
| 265 | |
| 266 | /* This function reads exactly <len> chars from <s> and converts them to a |
| 267 | * signed integer which it stores into <ret>. It accurately detects any error |
| 268 | * (truncated string, invalid chars, overflows). It is meant to be used in |
| 269 | * applications designed for hostile environments. It returns zero when the |
| 270 | * number has successfully been converted, non-zero otherwise. When an error |
| 271 | * is returned, the <ret> value is left untouched. It is yet 5 to 40 times |
| 272 | * faster than strtol(). |
| 273 | */ |
| 274 | int strl2irc(const char *s, int len, int *ret) |
| 275 | { |
| 276 | int i = 0; |
| 277 | int j; |
| 278 | |
| 279 | if (!len) |
| 280 | return 1; |
| 281 | |
| 282 | if (*s != '-') { |
| 283 | /* positive number */ |
| 284 | while (len-- > 0) { |
| 285 | j = (*s++) - '0'; |
| 286 | if (j > 9) return 1; /* invalid char */ |
| 287 | if (i > INT_MAX / 10) return 1; /* check for multiply overflow */ |
| 288 | i = i * 10; |
| 289 | if (i + j < i) return 1; /* check for addition overflow */ |
| 290 | i = i + j; |
| 291 | } |
| 292 | } else { |
| 293 | /* negative number */ |
| 294 | s++; |
| 295 | while (--len > 0) { |
| 296 | j = (*s++) - '0'; |
| 297 | if (j > 9) return 1; /* invalid char */ |
| 298 | if (i < INT_MIN / 10) return 1; /* check for multiply overflow */ |
| 299 | i = i * 10; |
| 300 | if (i - j > i) return 1; /* check for subtract overflow */ |
| 301 | i = i - j; |
| 302 | } |
| 303 | } |
| 304 | *ret = i; |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | |
| 309 | /* This function reads exactly <len> chars from <s> and converts them to a |
| 310 | * signed integer which it stores into <ret>. It accurately detects any error |
| 311 | * (truncated string, invalid chars, overflows). It is meant to be used in |
| 312 | * applications designed for hostile environments. It returns zero when the |
| 313 | * number has successfully been converted, non-zero otherwise. When an error |
| 314 | * is returned, the <ret> value is left untouched. It is about 3 times slower |
| 315 | * than str2irc(). |
| 316 | */ |
| 317 | #ifndef LLONG_MAX |
| 318 | #define LLONG_MAX 9223372036854775807LL |
| 319 | #define LLONG_MIN (-LLONG_MAX - 1LL) |
| 320 | #endif |
| 321 | |
| 322 | int strl2llrc(const char *s, int len, long long *ret) |
| 323 | { |
| 324 | long long i = 0; |
| 325 | int j; |
| 326 | |
| 327 | if (!len) |
| 328 | return 1; |
| 329 | |
| 330 | if (*s != '-') { |
| 331 | /* positive number */ |
| 332 | while (len-- > 0) { |
| 333 | j = (*s++) - '0'; |
| 334 | if (j > 9) return 1; /* invalid char */ |
| 335 | if (i > LLONG_MAX / 10LL) return 1; /* check for multiply overflow */ |
| 336 | i = i * 10LL; |
| 337 | if (i + j < i) return 1; /* check for addition overflow */ |
| 338 | i = i + j; |
| 339 | } |
| 340 | } else { |
| 341 | /* negative number */ |
| 342 | s++; |
| 343 | while (--len > 0) { |
| 344 | j = (*s++) - '0'; |
| 345 | if (j > 9) return 1; /* invalid char */ |
| 346 | if (i < LLONG_MIN / 10LL) return 1; /* check for multiply overflow */ |
| 347 | i = i * 10LL; |
| 348 | if (i - j > i) return 1; /* check for subtract overflow */ |
| 349 | i = i - j; |
| 350 | } |
| 351 | } |
| 352 | *ret = i; |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 357 | /* |
| 358 | * Local variables: |
| 359 | * c-indent-level: 8 |
| 360 | * c-basic-offset: 8 |
| 361 | * End: |
| 362 | */ |