Coverage Report

Created: 2026-06-15 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Utilities/cmcurl/lib/noproxy.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
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.
13
 *
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.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
#include "curl_setup.h"
25
26
#ifndef CURL_DISABLE_PROXY
27
28
#include "curlx/inet_pton.h"
29
#include "noproxy.h"
30
#include "curlx/strparse.h"
31
32
#ifdef HAVE_NETINET_IN_H
33
#include <netinet/in.h>
34
#endif
35
36
#ifdef HAVE_ARPA_INET_H
37
#include <arpa/inet.h>
38
#endif
39
40
/*
41
 * cidr4_match() returns TRUE if the given IPv4 address is within the
42
 * specified CIDR address range.
43
 *
44
 * @unittest 1614
45
 */
46
UNITTEST bool cidr4_match(const char *ipv4,    /* 1.2.3.4 address */
47
                          const char *network, /* 1.2.3.4 address */
48
                          unsigned int bits);
49
UNITTEST bool cidr4_match(const char *ipv4,    /* 1.2.3.4 address */
50
                          const char *network, /* 1.2.3.4 address */
51
                          unsigned int bits)
52
0
{
53
0
  unsigned int address = 0;
54
0
  unsigned int check = 0;
55
56
0
  if(bits > 32)
57
    /* strange input */
58
0
    return FALSE;
59
60
0
  if(curlx_inet_pton(AF_INET, ipv4, &address) != 1)
61
0
    return FALSE;
62
0
  if(curlx_inet_pton(AF_INET, network, &check) != 1)
63
0
    return FALSE;
64
65
0
  if(bits && (bits != 32)) {
66
0
    unsigned int mask = 0xffffffff << (32 - bits);
67
0
    unsigned int haddr = htonl(address);
68
0
    unsigned int hcheck = htonl(check);
69
#if 0
70
    curl_mfprintf(stderr, "Host %s (%x) network %s (%x) "
71
                  "bits %u mask %x => %x\n",
72
                  ipv4, haddr, network, hcheck, bits, mask,
73
                  (haddr ^ hcheck) & mask);
74
#endif
75
0
    if((haddr ^ hcheck) & mask)
76
0
      return FALSE;
77
0
    return TRUE;
78
0
  }
79
0
  return address == check;
80
0
}
81
82
/* @unittest 1614 */
83
UNITTEST bool cidr6_match(const char *ipv6, const char *network,
84
                          unsigned int bits);
85
UNITTEST bool cidr6_match(const char *ipv6, const char *network,
86
                          unsigned int bits)
87
0
{
88
0
#ifdef USE_IPV6
89
0
  unsigned int bytes;
90
0
  unsigned int rest;
91
0
  unsigned char address[16];
92
0
  unsigned char check[16];
93
94
0
  if(!bits)
95
0
    bits = 128;
96
97
0
  bytes = bits / 8;
98
0
  rest = bits & 0x07;
99
0
  if((bytes > 16) || ((bytes == 16) && rest))
100
0
    return FALSE;
101
0
  if(curlx_inet_pton(AF_INET6, ipv6, address) != 1)
102
0
    return FALSE;
103
0
  if(curlx_inet_pton(AF_INET6, network, check) != 1)
104
0
    return FALSE;
105
0
  if(bytes && memcmp(address, check, bytes))
106
0
    return FALSE;
107
0
  if(rest && ((address[bytes] ^ check[bytes]) & (0xff << (8 - rest))))
108
0
    return FALSE;
109
110
0
  return TRUE;
111
#else
112
  (void)ipv6;
113
  (void)network;
114
  (void)bits;
115
  return FALSE;
116
#endif
117
0
}
118
119
enum nametype {
120
  TYPE_HOST,
121
  TYPE_IPV4,
122
  TYPE_IPV6
123
};
124
125
static bool match_host(const char *token, size_t tokenlen,
126
                       const char *name, size_t namelen)
127
0
{
128
0
  bool match = FALSE;
129
130
  /* ignore trailing dots in the token to check */
131
0
  if(token[tokenlen - 1] == '.')
132
0
    tokenlen--;
133
134
0
  if(tokenlen && (*token == '.')) {
135
    /* ignore leading token dot as well */
136
0
    token++;
137
0
    tokenlen--;
138
0
  }
139
  /* A: example.com matches 'example.com'
140
     B: www.example.com matches 'example.com'
141
     C: nonexample.com DOES NOT match 'example.com'
142
  */
143
0
  if(tokenlen == namelen)
144
    /* case A, exact match */
145
0
    match = curl_strnequal(token, name, namelen);
146
0
  else if(tokenlen < namelen) {
147
    /* case B, tailmatch domain */
148
0
    match = (name[namelen - tokenlen - 1] == '.') &&
149
0
            curl_strnequal(token, name + (namelen - tokenlen), tokenlen);
150
0
  }
151
  /* case C passes through, not a match */
152
0
  return match;
153
0
}
154
155
static bool match_ip(int type, const char *token, size_t tokenlen,
156
                     const char *name)
157
0
{
158
0
  char *slash;
159
0
  unsigned int bits = 0;
160
0
  char checkip[128];
161
0
  if(tokenlen >= sizeof(checkip))
162
    /* this cannot match */
163
0
    return FALSE;
164
  /* copy the check name to a temp buffer */
165
0
  memcpy(checkip, token, tokenlen);
166
0
  checkip[tokenlen] = 0;
167
168
0
  slash = strchr(checkip, '/');
169
  /* if the slash is part of this token, use it */
170
0
  if(slash) {
171
0
    curl_off_t value;
172
0
    const char *p = &slash[1];
173
0
    if(curlx_str_number(&p, &value, 128) || *p)
174
0
      return FALSE;
175
    /* a too large value is rejected in the cidr function below */
176
0
    bits = (unsigned int)value;
177
0
    *slash = 0; /* null-terminate there */
178
0
  }
179
0
  if(type == TYPE_IPV6)
180
0
    return cidr6_match(name, checkip, bits);
181
0
  else
182
0
    return cidr4_match(name, checkip, bits);
183
0
}
184
185
/****************************************************************
186
 * Checks if the host is in the noproxy list. returns TRUE if it matches and
187
 * therefore the proxy should NOT be used.
188
 ****************************************************************/
189
bool Curl_check_noproxy(const char *name, const char *no_proxy)
190
0
{
191
  /*
192
   * If we do not have a hostname at all, like for example with a FILE
193
   * transfer, we have nothing to interrogate the noproxy list with.
194
   */
195
0
  if(!name || name[0] == '\0')
196
0
    return FALSE;
197
198
  /* no_proxy=domain1.dom,host.domain2.dom
199
   *   (a comma-separated list of hosts which should
200
   *   not be proxied, or an asterisk to override
201
   *   all proxy variables)
202
   */
203
0
  if(no_proxy && no_proxy[0]) {
204
0
    const char *p = no_proxy;
205
0
    size_t namelen;
206
0
    char address[16];
207
0
    enum nametype type = TYPE_HOST;
208
0
    if(!strcmp("*", no_proxy))
209
0
      return TRUE;
210
211
    /* NO_PROXY was specified and it was not only an asterisk */
212
213
    /* Check if name is an IP address; if not, assume it being a hostname. */
214
0
    namelen = strlen(name);
215
0
    if(curlx_inet_pton(AF_INET, name, &address) == 1)
216
0
      type = TYPE_IPV4;
217
0
#ifdef USE_IPV6
218
0
    else if(curlx_inet_pton(AF_INET6, name, &address) == 1)
219
0
      type = TYPE_IPV6;
220
0
#endif
221
0
    else {
222
      /* ignore trailing dots in the hostname */
223
0
      if(name[namelen - 1] == '.')
224
0
        namelen--;
225
0
    }
226
227
0
    while(*p) {
228
0
      const char *token;
229
0
      size_t tokenlen = 0;
230
231
      /* pass blanks */
232
0
      curlx_str_passblanks(&p);
233
234
0
      token = p;
235
      /* pass over the pattern */
236
0
      while(*p && !ISBLANK(*p) && (*p != ',')) {
237
0
        p++;
238
0
        tokenlen++;
239
0
      }
240
241
0
      if(tokenlen) {
242
0
        bool match = FALSE;
243
0
        if(type == TYPE_HOST)
244
0
          match = match_host(token, tokenlen, name, namelen);
245
0
        else
246
0
          match = match_ip(type, token, tokenlen, name);
247
248
0
        if(match)
249
0
          return TRUE;
250
0
      }
251
252
      /* pass blanks after pattern */
253
0
      curlx_str_passblanks(&p);
254
      /* if not a comma, this ends the loop */
255
0
      if(*p != ',')
256
0
        break;
257
      /* pass any number of commas */
258
0
      while(*p == ',')
259
0
        p++;
260
0
    } /* while(*p) */
261
0
  } /* NO_PROXY was specified and it was not only an asterisk */
262
263
0
  return FALSE;
264
0
}
265
266
#endif /* CURL_DISABLE_PROXY */