blob: a32267bb6972b0b94873fe3241e08867ea395ba4 [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
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020019#include <common/config.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020020#include <common/standard.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021#include <proto/log.h>
22
23/* enough to store 2^63=18446744073709551615 */
24static 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 */
33int 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 */
49char *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 */
71int 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 */
91struct 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 Tarreauc6423482006-10-15 14:59:03 +020099 if (str == NULL)
100 goto out_nofree;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101
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 Tarreauc6423482006-10-15 14:59:03 +0200125 out_nofree:
Willy Tarreaubaaee002006-06-26 02:48:02 +0200126 return &sa;
127}
128
129/*
130 * converts <str> to a two struct in_addr* which are locally allocated.
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 */
135int str2net(char *str, struct in_addr *addr, struct in_addr *mask)
136{
137 char *c;
138 unsigned long len;
139
140 memset(mask, 0, sizeof(*mask));
141 memset(addr, 0, sizeof(*addr));
142 str = strdup(str);
Willy Tarreauc6423482006-10-15 14:59:03 +0200143 if (str == NULL)
144 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200145
146 if ((c = strrchr(str, '/')) != NULL) {
147 *c++ = '\0';
148 /* c points to the mask */
149 if (strchr(c, '.') != NULL) { /* dotted notation */
150 if (!inet_pton(AF_INET, c, mask))
151 return 0;
152 }
153 else { /* mask length */
154 char *err;
155 len = strtol(c, &err, 10);
156 if (!*c || (err && *err) || (unsigned)len > 32)
157 return 0;
158 if (len)
159 mask->s_addr = htonl(~0UL << (32 - len));
160 else
161 mask->s_addr = 0;
162 }
163 }
164 else {
165 mask->s_addr = ~0UL;
166 }
167 if (!inet_pton(AF_INET, str, addr)) {
168 struct hostent *he;
169
170 if ((he = gethostbyname(str)) == NULL) {
171 return 0;
172 }
173 else
174 *addr = *(struct in_addr *) *(he->h_addr_list);
175 }
176 free(str);
177 return 1;
178}
179
180/* will try to encode the string <string> replacing all characters tagged in
181 * <map> with the hexadecimal representation of their ASCII-code (2 digits)
182 * prefixed by <escape>, and will store the result between <start> (included)
183 * and <stop> (excluded), and will always terminate the string with a '\0'
184 * before <stop>. The position of the '\0' is returned if the conversion
185 * completes. If bytes are missing between <start> and <stop>, then the
186 * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0'
187 * cannot even be stored so we return <start> without writing the 0.
188 * The input string must also be zero-terminated.
189 */
190const char hextab[16] = "0123456789ABCDEF";
191char *encode_string(char *start, char *stop,
192 const char escape, const fd_set *map,
193 const char *string)
194{
195 if (start < stop) {
196 stop--; /* reserve one byte for the final '\0' */
197 while (start < stop && *string != '\0') {
198 if (!FD_ISSET((unsigned char)(*string), map))
199 *start++ = *string;
200 else {
201 if (start + 3 >= stop)
202 break;
203 *start++ = escape;
204 *start++ = hextab[(*string >> 4) & 15];
205 *start++ = hextab[*string & 15];
206 }
207 string++;
208 }
209 *start = '\0';
210 }
211 return start;
212}
213
214
215/*
216 * Local variables:
217 * c-indent-level: 8
218 * c-basic-offset: 8
219 * End:
220 */