master
1/*
2 * Copyright (C) 1996-2022 Internet Software Consortium.
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
9 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
10 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
11 * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
13 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
14 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
15 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 *
17 * SPDX-License-Identifier: ISC
18 */
19#include "curl_setup.h"
21#ifdef HAVE_SYS_PARAM_H
22#include <sys/param.h>
23#endif
24#ifdef HAVE_NETINET_IN_H
25#include <netinet/in.h>
26#endif
27#ifdef HAVE_ARPA_INET_H
28#include <arpa/inet.h>
29#endif
31#include "curlx/inet_ntop.h"
32#include "curlx/snprintf.h"
33#include "curlx/strcopy.h"
35#define IN6ADDRSZ 16
36/* #define INADDRSZ 4 */
37#define INT16SZ 2
39/*
40 * If USE_IPV6 is disabled, we still want to parse IPv6 addresses, so make
41 * sure we have _some_ value for AF_INET6 without polluting our fake value
42 * everywhere.
43 */
44#if !defined(USE_IPV6) && !defined(AF_INET6)
45#define AF_INET6 (AF_INET + 1)
46#endif
48/*
49 * Format an IPv4 address, more or less like inet_ntop().
50 *
51 * Returns CURLcode.
52 * Note:
53 * - uses no static variables
54 * - takes an unsigned char* not an in_addr as input
55 */
56static CURLcode inet_ntop4(const unsigned char *src, char *dst, size_t size)
57{
58 char tmp[sizeof("255.255.255.255")];
59 size_t len;
61 DEBUGASSERT(size >= 16);
63 /* this snprintf() does not overflow the buffer. */
64 SNPRINTF(tmp, sizeof(tmp), "%d.%d.%d.%d",
65 ((int)((unsigned char)src[0])) & 0xff,
66 ((int)((unsigned char)src[1])) & 0xff,
67 ((int)((unsigned char)src[2])) & 0xff,
68 ((int)((unsigned char)src[3])) & 0xff);
70 len = strlen(tmp);
71 if(len == 0 || len >= size)
72 return CURLE_TOO_LARGE;
73 curlx_strcopy(dst, size, tmp, len);
74 return CURLE_OK;
75}
77/*
78 * Convert IPv6 binary address into presentation (printable) format.
79 */
80static CURLcode inet_ntop6(const unsigned char *src, char *dst, size_t size)
81{
82 /*
83 * Note that int32_t and int16_t need only be "at least" large enough
84 * to contain a value of the specified size. On some systems, like
85 * Crays, there is no such thing as an integer variable with 16 bits.
86 * Keep this in mind if you think this function should have been coded
87 * to use pointer overlays. All the world's not a VAX.
88 */
89 char tmp[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
90 char *tp;
91 struct {
92 int base;
93 int len;
94 } best, cur;
95 unsigned int words[IN6ADDRSZ / INT16SZ];
96 int i;
98 /* Preprocess:
99 * Copy the input (bytewise) array into a wordwise array.
100 * Find the longest run of 0x00's in src[] for :: shorthanding.
101 */
102 memset(words, '\0', sizeof(words));
103 for(i = 0; i < IN6ADDRSZ; i++)
104 words[i / 2] |= ((unsigned int)src[i] << ((1 - (i % 2)) << 3));
106 best.base = -1;
107 cur.base = -1;
108 best.len = 0;
109 cur.len = 0;
111 for(i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
112 if(words[i] == 0) {
113 if(cur.base == -1) {
114 cur.base = i;
115 cur.len = 1;
116 }
117 else
118 cur.len++;
119 }
120 else if(cur.base != -1) {
121 if(best.base == -1 || cur.len > best.len)
122 best = cur;
123 cur.base = -1;
124 }
125 }
126 if((cur.base != -1) && (best.base == -1 || cur.len > best.len))
127 best = cur;
128 if(best.base != -1 && best.len < 2)
129 best.base = -1;
130 /* Format the result. */
131 tp = tmp;
132 for(i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
133 /* Are we inside the best run of 0x00's? */
134 if(best.base != -1 && i >= best.base && i < (best.base + best.len)) {
135 if(i == best.base)
136 *tp++ = ':';
137 continue;
138 }
140 /* Are we following an initial run of 0x00s or any real hex?
141 */
142 if(i)
143 *tp++ = ':';
145 /* Is this address an encapsulated IPv4?
146 */
147 if(i == 6 && best.base == 0 &&
148 (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
149 CURLcode result = inet_ntop4(src + 12, tp, sizeof(tmp) - (tp - tmp));
150 if(result)
151 return result;
152 tp += strlen(tp);
153 break;
154 }
155 else {
156 /* Lower-case digits. Cannot use the set from mprintf.c since this
157 needs to work as a curlx function */
158 static const unsigned char ldigits[] = "0123456789abcdef";
160 unsigned int w = words[i];
161 /* output lowercase 16-bit hex number but ignore leading zeroes */
162 if(w & 0xf000)
163 *tp++ = ldigits[(w & 0xf000) >> 12];
164 if(w & 0xff00)
165 *tp++ = ldigits[(w & 0x0f00) >> 8];
166 if(w & 0xfff0)
167 *tp++ = ldigits[(w & 0x00f0) >> 4];
168 *tp++ = ldigits[(w & 0x000f)];
169 }
170 }
172 /* Was it a trailing run of 0x00's?
173 */
174 if(best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
175 *tp++ = ':';
177 /* Check for overflow, copy, and we are done. */
178 if((size_t)(tp - tmp) >= size)
179 return CURLE_TOO_LARGE;
180 curlx_strcopy(dst, size, tmp, tp - tmp);
181 return CURLE_OK;
182}
184/*
185 * Convert a network format address to presentation format.
186 *
187 * Copies result to 'buf' and returns CURLcode.
188 */
189CURLcode curlx_inet_ntop(int af, const void *src, char *buf, size_t size)
190{
191 switch(af) {
192 case AF_INET:
193 return inet_ntop4((const unsigned char *)src, buf, size);
194 case AF_INET6:
195 return inet_ntop6((const unsigned char *)src, buf, size);
196 default:
197 return CURLE_UNSUPPORTED_PROTOCOL;
198 }
199}
Diagnostics4 git calls · 22 ms
CallStatusSizeTime022 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=2 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=86Open pack map
fetch2002.5 KiB · hit17 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=17 git.response.bytes=2565 git.response.cache=HIT git.response.duration_ms=17 git.fetch.wants=ed06314520e5d0170928e5d1ec203f85426dc546 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=2565 git.pkt.payload.max=2544 git.sideband.band1.bytes=2543 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
- a5e34a426d8c4652
- Request
- 4a873d93…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format