blob: 0b2ea8d8a524656b7c83b5af75df7ed62f87886b [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau2dd0d472006-06-29 17:53:05 +02002 include/common/standard.h
Willy Tarreaubaaee002006-06-26 02:48:02 +02003 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 Tarreau2dd0d472006-06-29 17:53:05 +020022#ifndef _COMMON_STANDARD_H
23#define _COMMON_STANDARD_H
Willy Tarreaubaaee002006-06-26 02:48:02 +020024
25#include <netinet/in.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020026#include <common/config.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020027
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 */
47extern 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 */
53extern 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 */
58extern 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 */
65struct 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 */
73int 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 */
85extern const char hextab[];
86char *encode_string(char *start, char *stop,
87 const char escape, const fd_set *map,
88 const char *string);
89
Willy Tarreau2dd0d472006-06-29 17:53:05 +020090#endif /* _COMMON_STANDARD_H */