blob: e218055faaa1482689198b8a6d61f2b1fc50004b [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * General purpose functions.
3 *
4 * Copyright 2000-2006 Willy Tarreau <w@1wt.eu>
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
19#include <haproxy/standard.h>
20#include <proto/log.h>
21
22/* enough to store 2^63=18446744073709551615 */
23static char itoa_str[21];
24
25/*
26 * copies at most <size-1> chars from <src> to <dst>. Last char is always
27 * set to 0, unless <size> is 0. The number of chars copied is returned
28 * (excluding the terminating zero).
29 * This code has been optimized for size and speed : on x86, it's 45 bytes
30 * long, uses only registers, and consumes only 4 cycles per char.
31 */
32int strlcpy2(char *dst, const char *src, int size)
33{
34 char *orig = dst;
35 if (size) {
36 while (--size && (*dst = *src)) {
37 src++; dst++;
38 }
39 *dst = 0;
40 }
41 return dst - orig;
42}
43
44/*
45 * This function simply returns a statically allocated string containing
46 * the ascii representation for number 'n' in decimal.
47 */
48char *ultoa(unsigned long n)
49{
50 char *pos;
51
52 pos = itoa_str + sizeof(itoa_str) - 1;
53 *pos-- = '\0';
54
55 do {
56 *pos-- = '0' + n % 10;
57 n /= 10;
58 } while (n && pos >= itoa_str);
59 return pos + 1;
60}
61
62
63/*
64 * Returns non-zero if character <s> is a hex digit (0-9, a-f, A-F), else zero.
65 *
66 * It looks like this one would be a good candidate for inlining, but this is
67 * not interesting because it around 35 bytes long and often called multiple
68 * times within the same function.
69 */
70int ishex(char s)
71{
72 s -= '0';
73 if ((unsigned char)s <= 9)
74 return 1;
75 s -= 'A' - '0';
76 if ((unsigned char)s <= 5)
77 return 1;
78 s -= 'a' - 'A';
79 if ((unsigned char)s <= 5)
80 return 1;
81 return 0;
82}
83
84
85/*
86 * converts <str> to a struct sockaddr_in* which is locally allocated.
87 * The format is "addr:port", where "addr" can be a dotted IPv4 address,
88 * a host name, or empty or "*" to indicate INADDR_ANY.
89 */
90struct sockaddr_in *str2sa(char *str)
91{
92 static struct sockaddr_in sa;
93 char *c;
94 int port;
95
96 memset(&sa, 0, sizeof(sa));
97 str = strdup(str);
98
99 if ((c = strrchr(str,':')) != NULL) {
100 *c++ = '\0';
101 port = atol(c);
102 }
103 else
104 port = 0;
105
106 if (*str == '*' || *str == '\0') { /* INADDR_ANY */
107 sa.sin_addr.s_addr = INADDR_ANY;
108 }
109 else if (!inet_pton(AF_INET, str, &sa.sin_addr)) {
110 struct hostent *he;
111
112 if ((he = gethostbyname(str)) == NULL) {
113 Alert("Invalid server name: '%s'\n", str);
114 }
115 else
116 sa.sin_addr = *(struct in_addr *) *(he->h_addr_list);
117 }
118 sa.sin_port = htons(port);
119 sa.sin_family = AF_INET;
120
121 free(str);
122 return &sa;
123}
124
125/*
126 * converts <str> to a two struct in_addr* which are locally allocated.
127 * The format is "addr[/mask]", where "addr" cannot be empty, and mask
128 * is optionnal and either in the dotted or CIDR notation.
129 * Note: "addr" can also be a hostname. Returns 1 if OK, 0 if error.
130 */
131int str2net(char *str, struct in_addr *addr, struct in_addr *mask)
132{
133 char *c;
134 unsigned long len;
135
136 memset(mask, 0, sizeof(*mask));
137 memset(addr, 0, sizeof(*addr));
138 str = strdup(str);
139
140 if ((c = strrchr(str, '/')) != NULL) {
141 *c++ = '\0';
142 /* c points to the mask */
143 if (strchr(c, '.') != NULL) { /* dotted notation */
144 if (!inet_pton(AF_INET, c, mask))
145 return 0;
146 }
147 else { /* mask length */
148 char *err;
149 len = strtol(c, &err, 10);
150 if (!*c || (err && *err) || (unsigned)len > 32)
151 return 0;
152 if (len)
153 mask->s_addr = htonl(~0UL << (32 - len));
154 else
155 mask->s_addr = 0;
156 }
157 }
158 else {
159 mask->s_addr = ~0UL;
160 }
161 if (!inet_pton(AF_INET, str, addr)) {
162 struct hostent *he;
163
164 if ((he = gethostbyname(str)) == NULL) {
165 return 0;
166 }
167 else
168 *addr = *(struct in_addr *) *(he->h_addr_list);
169 }
170 free(str);
171 return 1;
172}
173
174/* will try to encode the string <string> replacing all characters tagged in
175 * <map> with the hexadecimal representation of their ASCII-code (2 digits)
176 * prefixed by <escape>, and will store the result between <start> (included)
177 * and <stop> (excluded), and will always terminate the string with a '\0'
178 * before <stop>. The position of the '\0' is returned if the conversion
179 * completes. If bytes are missing between <start> and <stop>, then the
180 * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0'
181 * cannot even be stored so we return <start> without writing the 0.
182 * The input string must also be zero-terminated.
183 */
184const char hextab[16] = "0123456789ABCDEF";
185char *encode_string(char *start, char *stop,
186 const char escape, const fd_set *map,
187 const char *string)
188{
189 if (start < stop) {
190 stop--; /* reserve one byte for the final '\0' */
191 while (start < stop && *string != '\0') {
192 if (!FD_ISSET((unsigned char)(*string), map))
193 *start++ = *string;
194 else {
195 if (start + 3 >= stop)
196 break;
197 *start++ = escape;
198 *start++ = hextab[(*string >> 4) & 15];
199 *start++ = hextab[*string & 15];
200 }
201 string++;
202 }
203 *start = '\0';
204 }
205 return start;
206}
207
208
209/*
210 * Local variables:
211 * c-indent-level: 8
212 * c-basic-offset: 8
213 * End:
214 */