Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 2 | include/common/standard.h |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3 | This files contains some general purpose functions and macros. |
| 4 | |
| 5 | Copyright (C) 2000-2006 Willy Tarreau - w@1wt.eu |
| 6 | |
| 7 | This library is free software; you can redistribute it and/or |
| 8 | modify it under the terms of the GNU Lesser General Public |
| 9 | License as published by the Free Software Foundation, version 2.1 |
| 10 | exclusively. |
| 11 | |
| 12 | This library is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | Lesser General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU Lesser General Public |
| 18 | License along with this library; if not, write to the Free Software |
| 19 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 22 | #ifndef _COMMON_STANDARD_H |
| 23 | #define _COMMON_STANDARD_H |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 24 | |
| 25 | #include <netinet/in.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 26 | #include <common/config.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 27 | |
| 28 | /****** string-specific macros and functions ******/ |
| 29 | /* if a > max, then bound <a> to <max>. The macro returns the new <a> */ |
| 30 | #define UBOUND(a, max) ({ typeof(a) b = (max); if ((a) > b) (a) = b; (a); }) |
| 31 | |
| 32 | /* if a < min, then bound <a> to <min>. The macro returns the new <a> */ |
| 33 | #define LBOUND(a, min) ({ typeof(a) b = (min); if ((a) < b) (a) = b; (a); }) |
| 34 | |
| 35 | /* returns 1 only if only zero or one bit is set in X, which means that X is a |
| 36 | * power of 2, and 0 otherwise */ |
| 37 | #define POWEROF2(x) (((x) & ((x)-1)) == 0) |
| 38 | |
| 39 | |
| 40 | /* |
| 41 | * copies at most <size-1> chars from <src> to <dst>. Last char is always |
| 42 | * set to 0, unless <size> is 0. The number of chars copied is returned |
| 43 | * (excluding the terminating zero). |
| 44 | * This code has been optimized for size and speed : on x86, it's 45 bytes |
| 45 | * long, uses only registers, and consumes only 4 cycles per char. |
| 46 | */ |
| 47 | extern int strlcpy2(char *dst, const char *src, int size); |
| 48 | |
| 49 | /* |
| 50 | * This function simply returns a statically allocated string containing |
| 51 | * the ascii representation for number 'n' in decimal. |
| 52 | */ |
| 53 | extern char *ultoa(unsigned long n); |
| 54 | |
| 55 | /* |
| 56 | * Returns non-zero if character <s> is a hex digit (0-9, a-f, A-F), else zero. |
| 57 | */ |
| 58 | extern int ishex(char s); |
| 59 | |
| 60 | /* |
| 61 | * converts <str> to a struct sockaddr_in* which is locally allocated. |
| 62 | * The format is "addr:port", where "addr" can be a dotted IPv4 address, |
| 63 | * a host name, or empty or "*" to indicate INADDR_ANY. |
| 64 | */ |
| 65 | struct sockaddr_in *str2sa(char *str); |
| 66 | |
| 67 | /* |
| 68 | * converts <str> to a two struct in_addr* which are locally allocated. |
| 69 | * The format is "addr[/mask]", where "addr" cannot be empty, and mask |
| 70 | * is optionnal and either in the dotted or CIDR notation. |
| 71 | * Note: "addr" can also be a hostname. Returns 1 if OK, 0 if error. |
| 72 | */ |
| 73 | int str2net(char *str, struct in_addr *addr, struct in_addr *mask); |
| 74 | |
| 75 | /* will try to encode the string <string> replacing all characters tagged in |
| 76 | * <map> with the hexadecimal representation of their ASCII-code (2 digits) |
| 77 | * prefixed by <escape>, and will store the result between <start> (included) |
| 78 | * and <stop> (excluded), and will always terminate the string with a '\0' |
| 79 | * before <stop>. The position of the '\0' is returned if the conversion |
| 80 | * completes. If bytes are missing between <start> and <stop>, then the |
| 81 | * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0' |
| 82 | * cannot even be stored so we return <start> without writing the 0. |
| 83 | * The input string must also be zero-terminated. |
| 84 | */ |
| 85 | extern const char hextab[]; |
| 86 | char *encode_string(char *start, char *stop, |
| 87 | const char escape, const fd_set *map, |
| 88 | const char *string); |
| 89 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 90 | #endif /* _COMMON_STANDARD_H */ |