]> git.ipfire.org Git - thirdparty/squid.git/blob - src/Parsing.cc
65776f8700abb92bd2455c434d394895e8162059
[thirdparty/squid.git] / src / Parsing.cc
1 /*
2 * $Id$
3 *
4 * DEBUG: section 3 Configuration File Parsing
5 * AUTHOR: Harvest Derived
6 *
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
9 *
10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
32 *
33 */
34
35 #include "config.h"
36 #include "compat/strtoll.h"
37 #include "Parsing.h"
38
39 /*
40 * These functions is the same as atoi/l/f, except that they check for errors
41 */
42
43 double
44 xatof(const char *token)
45 {
46 char *end;
47 double ret = strtod(token, &end);
48
49 if (ret == 0 && end == token)
50 self_destruct();
51
52 return ret;
53 }
54
55 int
56 xatoi(const char *token)
57 {
58 return xatol(token);
59 }
60
61 long
62 xatol(const char *token)
63 {
64 char *end;
65 long ret = strtol(token, &end, 10);
66
67 if (end == token || *end)
68 self_destruct();
69
70 return ret;
71 }
72
73 unsigned short
74 xatos(const char *token)
75 {
76 long port = xatol(token);
77
78 if (port & ~0xFFFF)
79 self_destruct();
80
81 return port;
82 }
83
84 int64_t
85 GetInteger64(void)
86 {
87 char *token = strtok(NULL, w_space);
88 int i;
89
90 if (token == NULL)
91 self_destruct();
92
93 i = strtoll(token, NULL, 10);
94
95 return i;
96 }
97
98 int
99 GetInteger(void)
100 {
101 char *token = strtok(NULL, w_space);
102 int i;
103
104 if (token == NULL)
105 self_destruct();
106
107 // %i honors 0 and 0x prefixes, which are important for things like umask
108 if (sscanf(token, "%i", &i) != 1)
109 self_destruct();
110
111 return i;
112 }
113
114 u_short
115 GetShort(void)
116 {
117 char *token = strtok(NULL, w_space);
118
119 if (token == NULL)
120 self_destruct();
121
122 return xatos(token);
123 }
124
125 bool
126 StringToInt(const char *s, int &result, const char **p, int base)
127 {
128 if (s) {
129 char *ptr = 0;
130 const int h = (int) strtol(s, &ptr, base);
131
132 if (ptr != s && ptr) {
133 result = h;
134
135 if (p)
136 *p = ptr;
137
138 return true;
139 }
140 }
141
142 return false;
143 }
144
145 bool
146 StringToInt64(const char *s, int64_t &result, const char **p, int base)
147 {
148 if (s) {
149 char *ptr = 0;
150 const int64_t h = (int64_t) strtoll(s, &ptr, base);
151
152 if (ptr != s && ptr) {
153 result = h;
154
155 if (p)
156 *p = ptr;
157
158 return true;
159 }
160 }
161
162 return false;
163 }
164
165 bool
166 GetHostWithPort(char *token, Ip::Address *ipa)
167 {
168 char *t;
169 char *host;
170 char *tmp;
171 unsigned short port;
172
173 host = NULL;
174 port = 0;
175
176 #if USE_IPV6
177 if (*token == '[') {
178 /* [host]:port */
179 host = token + 1;
180 t = strchr(host, ']');
181 if (!t)
182 return false;
183 *t++ = '\0';
184 if (*t != ':')
185 return false;
186 port = xatos(t + 1);
187 } else
188 #endif
189 if ((t = strchr(token, ':'))) {
190 /* host:port */
191 host = token;
192 *t = '\0';
193 port = xatos(t + 1);
194
195 if (0 == port)
196 return false;
197 } else if ((port = strtol(token, &tmp, 10)), !*tmp) {
198 /* port */
199 } else {
200 host = token;
201 port = 0;
202 }
203
204 if (NULL == host)
205 ipa->SetAnyAddr();
206 else if ( ipa->GetHostByName(host) ) /* dont use ipcache. Accept either FQDN or IPA. */
207 (void) 0;
208 else
209 return false;
210
211 /* port MUST be set after the IPA lookup/conversion is performed. */
212 ipa->SetPort(port);
213
214 return true;
215 }