blob: 64357a2b250be79f2eb0c90572b6698d9cc85448 [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);
99
100 if ((c = strrchr(str,':')) != NULL) {
101 *c++ = '\0';
102 port = atol(c);
103 }
104 else
105 port = 0;
106
107 if (*str == '*' || *str == '\0') { /* INADDR_ANY */
108 sa.sin_addr.s_addr = INADDR_ANY;
109 }
110 else if (!inet_pton(AF_INET, str, &sa.sin_addr)) {
111 struct hostent *he;
112
113 if ((he = gethostbyname(str)) == NULL) {
114 Alert("Invalid server name: '%s'\n", str);
115 }
116 else
117 sa.sin_addr = *(struct in_addr *) *(he->h_addr_list);
118 }
119 sa.sin_port = htons(port);
120 sa.sin_family = AF_INET;
121
122 free(str);
123 return &sa;
124}
125
126/*
127 * converts <str> to a two struct in_addr* which are locally allocated.
128 * The format is "addr[/mask]", where "addr" cannot be empty, and mask
129 * is optionnal and either in the dotted or CIDR notation.
130 * Note: "addr" can also be a hostname. Returns 1 if OK, 0 if error.
131 */
132int str2net(char *str, struct in_addr *addr, struct in_addr *mask)
133{
134 char *c;
135 unsigned long len;
136
137 memset(mask, 0, sizeof(*mask));
138 memset(addr, 0, sizeof(*addr));
139 str = strdup(str);
140
141 if ((c = strrchr(str, '/')) != NULL) {
142 *c++ = '\0';
143 /* c points to the mask */
144 if (strchr(c, '.') != NULL) { /* dotted notation */
145 if (!inet_pton(AF_INET, c, mask))
146 return 0;
147 }
148 else { /* mask length */
149 char *err;
150 len = strtol(c, &err, 10);
151 if (!*c || (err && *err) || (unsigned)len > 32)
152 return 0;
153 if (len)
154 mask->s_addr = htonl(~0UL << (32 - len));
155 else
156 mask->s_addr = 0;
157 }
158 }
159 else {
160 mask->s_addr = ~0UL;
161 }
162 if (!inet_pton(AF_INET, str, addr)) {
163 struct hostent *he;
164
165 if ((he = gethostbyname(str)) == NULL) {
166 return 0;
167 }
168 else
169 *addr = *(struct in_addr *) *(he->h_addr_list);
170 }
171 free(str);
172 return 1;
173}
174
175/* will try to encode the string <string> replacing all characters tagged in
176 * <map> with the hexadecimal representation of their ASCII-code (2 digits)
177 * prefixed by <escape>, and will store the result between <start> (included)
178 * and <stop> (excluded), and will always terminate the string with a '\0'
179 * before <stop>. The position of the '\0' is returned if the conversion
180 * completes. If bytes are missing between <start> and <stop>, then the
181 * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0'
182 * cannot even be stored so we return <start> without writing the 0.
183 * The input string must also be zero-terminated.
184 */
185const char hextab[16] = "0123456789ABCDEF";
186char *encode_string(char *start, char *stop,
187 const char escape, const fd_set *map,
188 const char *string)
189{
190 if (start < stop) {
191 stop--; /* reserve one byte for the final '\0' */
192 while (start < stop && *string != '\0') {
193 if (!FD_ISSET((unsigned char)(*string), map))
194 *start++ = *string;
195 else {
196 if (start + 3 >= stop)
197 break;
198 *start++ = escape;
199 *start++ = hextab[(*string >> 4) & 15];
200 *start++ = hextab[*string & 15];
201 }
202 string++;
203 }
204 *start = '\0';
205 }
206 return start;
207}
208
209
210/*
211 * Local variables:
212 * c-indent-level: 8
213 * c-basic-offset: 8
214 * End:
215 */