Artifactspublic-v0

curl

master
ply4 git calls144 ms290 KiB2 cachedprotocol v2
354 lines8.6 KiBRaw
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/* <DESC>
25 * Show how CURLOPT_OPENSOCKETFUNCTION can be used to block IP addresses.
26 * </DESC>
27 */
28/* This is an advanced example that defines a whitelist or a blacklist to
29 * filter IP addresses.
30 */
32#ifdef __AMIGA__
33#include <stdio.h>
34int main(void)
36 printf("Platform not supported.\n");
37 return 1;
39#else
41#ifdef _WIN32 /* Requires Windows Vista+ */
42#include <winsock2.h>
43#include <ws2tcpip.h>
44#include <windows.h>
45#define strdup _strdup
46#else
47#include <sys/types.h>
48#include <sys/socket.h>
49#include <netinet/in.h>
50#include <arpa/inet.h>
51#endif
53#include <stdio.h>
54#include <stdlib.h>
55#include <string.h>
57#include <curl/curl.h>
59#ifndef TRUE
60#define TRUE 1
61#endif
63#ifndef FALSE
64#define FALSE 0
65#endif
67struct ip {
68 /* The user-provided IP address or network (use CIDR) to filter */
69 char *str;
70 /* IP address family AF_INET (IPv4) or AF_INET6 (IPv6) */
71 int family;
72 /* IP in network byte format */
73 union netaddr {
74 struct in_addr ipv4;
75#ifdef AF_INET6
76 struct in6_addr ipv6;
77#endif
78 } netaddr;
79 /* IP bits to match against.
80 * This is equal to the CIDR notation or max bits if no CIDR.
81 * For example if ip->str is 127.0.0.0/8 then ip->maskbits is 8.
82 */
83 int maskbits;
84 struct ip *next;
85};
87enum connection_filter_t {
88 CONNECTION_FILTER_BLACKLIST,
89 CONNECTION_FILTER_WHITELIST
90};
92struct connection_filter {
93 struct ip *list;
94 enum connection_filter_t type;
95 int verbose;
96#ifdef AF_INET6
97 /* If the address being filtered is an IPv4-mapped IPv6 address then it is
98 * checked against IPv4 list entries as well, unless ipv6_v6only is set TRUE.
99 */
100 int ipv6_v6only;
101#endif
102};
104static struct ip *ip_list_append(struct ip *list, const char *data)
106 struct ip *ip, *last;
107 char *cidr;
109 ip = calloc(1, sizeof(*ip));
110 if(!ip)
111 return NULL;
113 if(strchr(data, ':')) {
114#ifdef AF_INET6
115 ip->family = AF_INET6;
116#else
117 free(ip);
118 return NULL;
119#endif
120 }
121 else
122 ip->family = AF_INET;
124 ip->str = strdup(data);
125 if(!ip->str) {
126 free(ip);
127 return NULL;
128 }
130 /* determine the number of bits that this IP matches against */
131 cidr = strchr(ip->str, '/');
132 if(cidr) {
133 ip->maskbits = atoi(cidr + 1);
134 if(ip->maskbits <= 0 ||
135#ifdef AF_INET6
136 (ip->family == AF_INET6 && ip->maskbits > 128) ||
137#endif
138 (ip->family == AF_INET && ip->maskbits > 32)) {
139 free(ip->str);
140 free(ip);
141 return NULL;
142 }
143 /* ignore the CIDR notation when converting ip->str to ip->netaddr */
144 *cidr = '\0';
145 }
146 else if(ip->family == AF_INET)
147 ip->maskbits = 32;
148#ifdef AF_INET6
149 else if(ip->family == AF_INET6)
150 ip->maskbits = 128;
151#endif
153 /* !checksrc! disable BANNEDFUNC 1 */
154 if(inet_pton(ip->family, ip->str, &ip->netaddr) != 1) {
155 free(ip->str);
156 free(ip);
157 return NULL;
158 }
160 if(cidr)
161 *cidr = '/';
163 if(!list)
164 return ip;
165 for(last = list; last->next; last = last->next)
166 ;
167 last->next = ip;
168 return list;
171static void ip_list_free_all(struct ip *list)
173 struct ip *next;
174 while(list) {
175 next = list->next;
176 free(list->str);
177 free(list);
178 list = next;
179 }
182static void free_connection_filter(struct connection_filter *filter)
184 if(filter) {
185 ip_list_free_all(filter->list);
186 free(filter);
187 }
190static int ip_match(struct ip *ip, void *netaddr)
192 int bytes, tailbits;
193 const unsigned char *x, *y;
195 x = (const unsigned char *)&ip->netaddr;
196 y = (const unsigned char *)netaddr;
198 for(bytes = ip->maskbits / 8; bytes; --bytes) {
199 if(*x++ != *y++)
200 return FALSE;
201 }
203 tailbits = ip->maskbits % 8;
204 if(tailbits) {
205 unsigned char tailmask = (unsigned char)((0xFF << (8 - tailbits)) & 0xFF);
206 if((*x & tailmask) != (*y & tailmask))
207 return FALSE;
208 }
210 return TRUE;
213#ifdef AF_INET6
214static int is_ipv4_mapped_ipv6_address(int family, void *netaddr)
216 if(family == AF_INET6) {
217 int i;
218 const unsigned char *x = (const unsigned char *)netaddr;
219 for(i = 0; i < 12; ++i) {
220 if(x[i])
221 break;
222 }
223 /* support formats ::x.x.x.x (deprecated) and ::ffff:x.x.x.x */
224 if((i == 12 && (x[i] || x[i + 1] || x[i + 2] || x[i + 3])) ||
225 (i == 10 && (x[i] == 0xFF && x[i + 1] == 0xFF)))
226 return TRUE;
227 }
229 return FALSE;
231#endif /* AF_INET6 */
233static curl_socket_t opensocket(void *clientp, curlsocktype purpose,
234 struct curl_sockaddr *address)
236 /* filter the address */
237 if(purpose == CURLSOCKTYPE_IPCXN) {
238 void *cinaddr = NULL;
240 if(address->family == AF_INET)
241 cinaddr = &((struct sockaddr_in *)(void *)&address->addr)->sin_addr;
242#ifdef AF_INET6
243 else if(address->family == AF_INET6)
244 cinaddr = &((struct sockaddr_in6 *)(void *)&address->addr)->sin6_addr;
245#endif
247 if(cinaddr) {
248 struct ip *ip;
249 struct connection_filter *filter = (struct connection_filter *)clientp;
250#ifdef AF_INET6
251 int mapped = !filter->ipv6_v6only &&
252 is_ipv4_mapped_ipv6_address(address->family, cinaddr);
253#endif
255 for(ip = filter->list; ip; ip = ip->next) {
256 if(ip->family == address->family && ip_match(ip, cinaddr))
257 break;
258#ifdef AF_INET6
259 if(mapped && ip->family == AF_INET && address->family == AF_INET6 &&
260 ip_match(ip, (unsigned char *)cinaddr + 12))
261 break;
262#endif
263 }
265 if(ip && filter->type == CONNECTION_FILTER_BLACKLIST) {
266 if(filter->verbose) {
267 char buf[128] = { 0 };
268 /* !checksrc! disable BANNEDFUNC 1 */
269 inet_ntop(address->family, cinaddr, buf, sizeof(buf));
270 fprintf(stderr, "* Rejecting IP %s due to blacklist entry %s.\n",
271 buf, ip->str);
272 }
273 return CURL_SOCKET_BAD;
274 }
275 else if(!ip && filter->type == CONNECTION_FILTER_WHITELIST) {
276 if(filter->verbose) {
277 char buf[128] = { 0 };
278 /* !checksrc! disable BANNEDFUNC 1 */
279 inet_ntop(address->family, cinaddr, buf, sizeof(buf));
280 fprintf(stderr,
281 "* Rejecting IP %s due to missing whitelist entry.\n", buf);
282 }
283 return CURL_SOCKET_BAD;
284 }
285 }
286 }
288 return socket(address->family, address->socktype, address->protocol);
291int main(void)
293 CURL *curl;
294 CURLcode result;
295 struct connection_filter *filter;
297 filter = calloc(1, sizeof(*filter));
298 if(!filter)
299 return 1;
301 result = curl_global_init(CURL_GLOBAL_ALL);
302 if(result != CURLE_OK) {
303 free(filter);
304 return (int)result;
305 }
307 curl = curl_easy_init();
308 if(!curl) {
309 curl_global_cleanup();
310 free(filter);
311 return 1;
312 }
314 /* Set the target URL */
315 curl_easy_setopt(curl, CURLOPT_URL, "http://localhost");
317 /* Define an IP connection filter.
318 * If an address has CIDR notation then it matches the network.
319 * For example 74.6.143.25/24 matches 74.6.143.0 - 74.6.143.255.
320 */
321 filter->type = CONNECTION_FILTER_BLACKLIST;
322 filter->list = ip_list_append(filter->list, "98.137.11.164");
323 filter->list = ip_list_append(filter->list, "127.0.0.0/8");
324#ifdef AF_INET6
325 filter->list = ip_list_append(filter->list, "::1");
326#endif
328 /* Set the socket function which does the filtering */
329 curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, opensocket);
330 curl_easy_setopt(curl, CURLOPT_OPENSOCKETDATA, filter);
332 /* Verbose mode */
333 filter->verbose = TRUE;
334 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
336 /* Perform the request */
337 result = curl_easy_perform(curl);
339 /* Check for errors */
340 if(result != CURLE_OK) {
341 fprintf(stderr, "curl_easy_perform() failed: %s\n",
342 curl_easy_strerror(result));
343 }
345 /* Clean up */
346 curl_easy_cleanup(curl);
347 free_connection_filter(filter);
349 /* Clean up libcurl */
350 curl_global_cleanup();
352 return 0;
354#endif
Diagnostics4 git calls · 144 ms
CallStatusSizeTime0144 ms
discover200152 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=152
git.server.agent=gitty/1.0
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch object-format
git.server.fetch=shallow filter sideband-all
git.protocol.version=2
git.pkt.frames=8
git.pkt.data=6
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=152
git.pkt.payload.max=34
ls-refs200149 B · hit2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=149
git.response.cache=HIT
git.response.duration_ms=2
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=149
git.pkt.payload.max=78
git.ls_refs.count=2
git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=293470
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=blob:none
git.fetch.filter.requested=tree:0
git.fetch.filter.fallback=blob:none
git.fetch.filter.learned=false
git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=10
git.pkt.data=8
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=293470
git.pkt.payload.max=65516
git.sideband.band1.bytes=293354
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=87
git.pack.bytes=293354
git.pack.ofs_delta=0
git.pack.ref_delta=1
git.pack.chain.max=1
git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0
git.pack.full=86
Open pack map
fetch2003.2 KiB139 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=139
git.response.bytes=3230
git.response.cache=MISS
git.response.duration_ms=139
git.fetch.wants=69ffb251953daea518d1352f5cd4d45d6939df87
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=3230
git.pkt.payload.max=3209
git.sideband.band1.bytes=3208
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
Ref
refs/heads/master
Commit
31c911efe5652725e930f4d2e60c9060e5f6646d
cf-ray
a5e1a26d8c773cd0
Request
72f6bf08…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format