1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * SPDX-License-Identifier: curl
23 ***************************************************************************/
25 #include "curl_setup.h"
27 #ifndef CURL_DISABLE_PROXY
29 #include "inet_pton.h"
33 #ifdef HAVE_NETINET_IN_H
34 #include <netinet/in.h>
38 * Curl_cidr4_match() returns TRUE if the given IPv4 address is within the
39 * specified CIDR address range.
41 UNITTEST bool Curl_cidr4_match(const char *ipv4, /* 1.2.3.4 address */
42 const char *network, /* 1.2.3.4 address */
45 unsigned int address = 0;
46 unsigned int check = 0;
52 if(1 != Curl_inet_pton(AF_INET, ipv4, &address))
54 if(1 != Curl_inet_pton(AF_INET, network, &check))
57 if(bits && (bits != 32)) {
58 unsigned int mask = 0xffffffff << (32 - bits);
59 unsigned int haddr = htonl(address);
60 unsigned int hcheck = htonl(check);
62 fprintf(stderr, "Host %s (%x) network %s (%x) bits %u mask %x => %x\n",
63 ipv4, haddr, network, hcheck, bits, mask,
64 (haddr ^ hcheck) & mask);
66 if((haddr ^ hcheck) & mask)
70 return (address == check);
73 UNITTEST bool Curl_cidr6_match(const char *ipv6,
80 unsigned char address[16];
81 unsigned char check[16];
88 if(1 != Curl_inet_pton(AF_INET6, ipv6, address))
90 if(1 != Curl_inet_pton(AF_INET6, network, check))
92 if((bytes > 16) || ((bytes == 16) && rest))
94 if(bytes && memcmp(address, check, bytes))
96 if(rest && !((address[bytes] ^ check[bytes]) & (0xff << (8 - rest))))
114 /****************************************************************
115 * Checks if the host is in the noproxy list. returns TRUE if it matches and
116 * therefore the proxy should NOT be used.
117 ****************************************************************/
118 bool Curl_check_noproxy(const char *name, const char *no_proxy)
120 /* no_proxy=domain1.dom,host.domain2.dom
121 * (a comma-separated list of hosts which should
122 * not be proxied, or an asterisk to override
123 * all proxy variables)
125 if(no_proxy && no_proxy[0]) {
126 const char *p = no_proxy;
128 enum nametype type = TYPE_HOST;
130 if(!strcmp("*", no_proxy))
133 /* NO_PROXY was specified and it wasn't just an asterisk */
137 /* IPv6 numerical address */
138 endptr = strchr(name, ']');
142 namelen = endptr - name;
143 if(namelen >= sizeof(hostip))
145 memcpy(hostip, name, namelen);
151 unsigned int address;
152 if(1 == Curl_inet_pton(AF_INET, name, &address))
154 namelen = strlen(name);
163 while(*p && ISBLANK(*p))
167 /* pass over the pattern */
168 while(*p && !ISBLANK(*p) && (*p != ',')) {
180 match = (tokenlen <= namelen) &&
181 strncasecompare(token, name + (namelen - tokenlen), namelen);
184 match = (tokenlen == namelen) &&
185 strncasecompare(token, name, namelen);
190 const char *check = token;
191 char *slash = strchr(check, '/');
192 unsigned int bits = 0;
194 /* if the slash is part of this token, use it */
195 if(slash && (slash < &check[tokenlen])) {
196 bits = atoi(slash + 1);
197 /* copy the check name to a temp buffer */
198 if(tokenlen >= sizeof(checkip))
200 memcpy(checkip, check, tokenlen);
201 checkip[ slash - check ] = 0;
204 if(type == TYPE_IPV6)
205 match = Curl_cidr6_match(name, check, bits);
207 match = Curl_cidr4_match(name, check, bits);
217 } /* NO_PROXY was specified and it wasn't just an asterisk */
222 #endif /* CURL_DISABLE_PROXY */