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 | | #include "curl_trc.h" |
27 | | #include "urldata.h" |
28 | | #include "cfilters.h" |
29 | | #include "multiif.h" |
30 | | |
31 | | #include "cf-socket.h" |
32 | | #include "connect.h" |
33 | | #include "http2.h" |
34 | | #include "http_proxy.h" |
35 | | #include "cf-h1-proxy.h" |
36 | | #include "cf-h2-proxy.h" |
37 | | #include "cf-haproxy.h" |
38 | | #include "cf-https-connect.h" |
39 | | #include "cf-ip-happy.h" |
40 | | #include "progress.h" |
41 | | #include "socks.h" |
42 | | #include "curlx/strparse.h" |
43 | | #include "vtls/vtls.h" |
44 | | #include "vquic/vquic.h" |
45 | | #include "curlx/strcopy.h" |
46 | | |
47 | | static void trc_write(struct Curl_easy *data, curl_infotype type, |
48 | | const char *ptr, size_t size) |
49 | 0 | { |
50 | 0 | if(data->set.verbose) { |
51 | 0 | if(data->set.fdebug) { |
52 | 0 | bool inCallback = Curl_is_in_callback(data); |
53 | 0 | Curl_set_in_callback(data, TRUE); |
54 | 0 | (void)(*data->set.fdebug)(data, type, CURL_UNCONST(ptr), size, |
55 | 0 | data->set.debugdata); |
56 | 0 | Curl_set_in_callback(data, inCallback); |
57 | 0 | } |
58 | 0 | else { |
59 | 0 | static const char s_infotype[CURLINFO_END][3] = { |
60 | 0 | "* ", "< ", "> ", "{ ", "} ", "{ ", "} " }; |
61 | 0 | switch(type) { |
62 | 0 | case CURLINFO_TEXT: |
63 | 0 | case CURLINFO_HEADER_OUT: |
64 | 0 | case CURLINFO_HEADER_IN: |
65 | 0 | fwrite(s_infotype[type], 2, 1, data->set.err); |
66 | 0 | fwrite(ptr, size, 1, data->set.err); |
67 | 0 | break; |
68 | 0 | default: /* nada */ |
69 | 0 | break; |
70 | 0 | } |
71 | 0 | } |
72 | 0 | } |
73 | 0 | } |
74 | | |
75 | | /* max length we trace before ending in '...' */ |
76 | 0 | #define TRC_LINE_MAX 2048 |
77 | | |
78 | 0 | #define CURL_TRC_FMT_IDSC "[x-%" CURL_FORMAT_CURL_OFF_T "] " |
79 | 0 | #define CURL_TRC_FMT_IDSD "[%" CURL_FORMAT_CURL_OFF_T "-x] " |
80 | 0 | #define CURL_TRC_FMT_IDSDC "[%" CURL_FORMAT_CURL_OFF_T "-%" \ |
81 | 0 | CURL_FORMAT_CURL_OFF_T "] " |
82 | | |
83 | | static struct curl_trc_feat Curl_trc_feat_ids = { |
84 | | "LIB-IDS", |
85 | | CURL_LOG_LVL_NONE, |
86 | | }; |
87 | | #define CURL_TRC_IDS(data) \ |
88 | 0 | (Curl_trc_is_verbose(data) && \ |
89 | 0 | Curl_trc_feat_ids.log_level >= CURL_LOG_LVL_INFO) |
90 | | |
91 | | static size_t trc_print_ids(struct Curl_easy *data, char *buf, size_t maxlen) |
92 | 0 | { |
93 | 0 | curl_off_t cid = data->conn ? |
94 | 0 | data->conn->connection_id : data->state.recent_conn_id; |
95 | 0 | if(data->id >= 0) { |
96 | 0 | if(cid >= 0) |
97 | 0 | return curl_msnprintf(buf, maxlen, CURL_TRC_FMT_IDSDC, data->id, cid); |
98 | 0 | else |
99 | 0 | return curl_msnprintf(buf, maxlen, CURL_TRC_FMT_IDSD, data->id); |
100 | 0 | } |
101 | 0 | else if(cid >= 0) |
102 | 0 | return curl_msnprintf(buf, maxlen, CURL_TRC_FMT_IDSC, cid); |
103 | 0 | else { |
104 | 0 | return curl_msnprintf(buf, maxlen, "[x-x] "); |
105 | 0 | } |
106 | 0 | } |
107 | | |
108 | | static size_t trc_end_buf(char *buf, size_t len, size_t maxlen, bool addnl) |
109 | 0 | { |
110 | | /* make sure we end the trace line in `buf` properly. It needs |
111 | | * to end with a terminating '\0' or '\n\0' */ |
112 | 0 | if(len >= (maxlen - (addnl ? 2 : 1))) { |
113 | 0 | len = maxlen - 5; |
114 | 0 | buf[len++] = '.'; |
115 | 0 | buf[len++] = '.'; |
116 | 0 | buf[len++] = '.'; |
117 | 0 | buf[len++] = '\n'; |
118 | 0 | } |
119 | 0 | else if(addnl) |
120 | 0 | buf[len++] = '\n'; |
121 | 0 | buf[len] = '\0'; |
122 | 0 | return len; |
123 | 0 | } |
124 | | |
125 | | void Curl_debug(struct Curl_easy *data, curl_infotype type, |
126 | | const char *ptr, size_t size) |
127 | 3.17M | { |
128 | 3.17M | if(data->set.verbose) { |
129 | 0 | static const char s_infotype[CURLINFO_END][3] = { |
130 | 0 | "* ", "< ", "> ", "{ ", "} ", "{ ", "} " }; |
131 | 0 | char buf[TRC_LINE_MAX]; |
132 | 0 | size_t len; |
133 | 0 | if(data->set.fdebug) { |
134 | 0 | bool inCallback = Curl_is_in_callback(data); |
135 | |
|
136 | 0 | if(CURL_TRC_IDS(data) && (size < TRC_LINE_MAX)) { |
137 | 0 | len = trc_print_ids(data, buf, TRC_LINE_MAX); |
138 | 0 | len += curl_msnprintf(buf + len, TRC_LINE_MAX - len, "%.*s", |
139 | 0 | (int)size, ptr); |
140 | 0 | len = trc_end_buf(buf, len, TRC_LINE_MAX, FALSE); |
141 | 0 | Curl_set_in_callback(data, TRUE); |
142 | 0 | (void)(*data->set.fdebug)(data, type, buf, len, data->set.debugdata); |
143 | 0 | Curl_set_in_callback(data, inCallback); |
144 | 0 | } |
145 | 0 | else { |
146 | 0 | Curl_set_in_callback(data, TRUE); |
147 | 0 | (void)(*data->set.fdebug)(data, type, CURL_UNCONST(ptr), |
148 | 0 | size, data->set.debugdata); |
149 | 0 | Curl_set_in_callback(data, inCallback); |
150 | 0 | } |
151 | 0 | } |
152 | 0 | else { |
153 | 0 | switch(type) { |
154 | 0 | case CURLINFO_TEXT: |
155 | 0 | case CURLINFO_HEADER_OUT: |
156 | 0 | case CURLINFO_HEADER_IN: |
157 | 0 | #ifndef CURL_DISABLE_VERBOSE_STRINGS |
158 | 0 | if(CURL_TRC_IDS(data)) { |
159 | 0 | len = trc_print_ids(data, buf, TRC_LINE_MAX); |
160 | 0 | fwrite(buf, len, 1, data->set.err); |
161 | 0 | } |
162 | 0 | #endif |
163 | 0 | fwrite(s_infotype[type], 2, 1, data->set.err); |
164 | 0 | fwrite(ptr, size, 1, data->set.err); |
165 | 0 | break; |
166 | 0 | default: /* nada */ |
167 | 0 | break; |
168 | 0 | } |
169 | 0 | } |
170 | 0 | } |
171 | 3.17M | } |
172 | | |
173 | | /* Curl_failf() is for messages stating why we failed. |
174 | | * The message SHALL NOT include any LF or CR. |
175 | | */ |
176 | | void Curl_failf(struct Curl_easy *data, const char *fmt, ...) |
177 | 223k | { |
178 | 223k | DEBUGASSERT(!strchr(fmt, '\n')); |
179 | 223k | if(data->set.verbose || data->set.errorbuffer) { |
180 | 0 | va_list ap; |
181 | 0 | size_t len; |
182 | 0 | char error[CURL_ERROR_SIZE + 2]; |
183 | 0 | va_start(ap, fmt); |
184 | 0 | len = curl_mvsnprintf(error, CURL_ERROR_SIZE, fmt, ap); |
185 | |
|
186 | 0 | if(data->set.errorbuffer && !data->state.errorbuf) { |
187 | 0 | curlx_strcopy(data->set.errorbuffer, CURL_ERROR_SIZE, error, len); |
188 | 0 | data->state.errorbuf = TRUE; /* wrote error string */ |
189 | 0 | } |
190 | 0 | error[len++] = '\n'; |
191 | 0 | error[len] = '\0'; |
192 | 0 | trc_write(data, CURLINFO_TEXT, error, len); |
193 | 0 | va_end(ap); |
194 | 0 | } |
195 | 223k | } |
196 | | |
197 | | #ifdef CURLVERBOSE |
198 | | struct curl_trc_feat Curl_trc_feat_multi = { |
199 | | "MULTI", |
200 | | CURL_LOG_LVL_NONE, |
201 | | }; |
202 | | struct curl_trc_feat Curl_trc_feat_read = { |
203 | | "READ", |
204 | | CURL_LOG_LVL_NONE, |
205 | | }; |
206 | | struct curl_trc_feat Curl_trc_feat_write = { |
207 | | "WRITE", |
208 | | CURL_LOG_LVL_NONE, |
209 | | }; |
210 | | struct curl_trc_feat Curl_trc_feat_dns = { |
211 | | "DNS", |
212 | | CURL_LOG_LVL_NONE, |
213 | | }; |
214 | | struct curl_trc_feat Curl_trc_feat_timer = { |
215 | | "TIMER", |
216 | | CURL_LOG_LVL_NONE, |
217 | | }; |
218 | | #endif |
219 | | |
220 | | #ifndef CURL_DISABLE_VERBOSE_STRINGS |
221 | | |
222 | | static void trc_infof(struct Curl_easy *data, |
223 | | struct curl_trc_feat *feat, |
224 | | const char *opt_id, int opt_id_idx, |
225 | | const char * const fmt, va_list ap) CURL_PRINTF(5, 0); |
226 | | |
227 | | static void trc_infof(struct Curl_easy *data, |
228 | | struct curl_trc_feat *feat, |
229 | | const char *opt_id, int opt_id_idx, |
230 | | const char * const fmt, va_list ap) |
231 | 0 | { |
232 | 0 | size_t len = 0; |
233 | 0 | char buf[TRC_LINE_MAX]; |
234 | |
|
235 | 0 | if(CURL_TRC_IDS(data)) |
236 | 0 | len += trc_print_ids(data, buf + len, TRC_LINE_MAX - len); |
237 | 0 | if(feat) |
238 | 0 | len += curl_msnprintf(buf + len, TRC_LINE_MAX - len, "[%s] ", feat->name); |
239 | 0 | if(opt_id) { |
240 | 0 | if(opt_id_idx > 0) |
241 | 0 | len += curl_msnprintf(buf + len, TRC_LINE_MAX - len, "[%s-%d] ", |
242 | 0 | opt_id, opt_id_idx); |
243 | 0 | else |
244 | 0 | len += curl_msnprintf(buf + len, TRC_LINE_MAX - len, "[%s] ", opt_id); |
245 | 0 | } |
246 | 0 | len += curl_mvsnprintf(buf + len, TRC_LINE_MAX - len, fmt, ap); |
247 | 0 | len = trc_end_buf(buf, len, TRC_LINE_MAX, TRUE); |
248 | 0 | trc_write(data, CURLINFO_TEXT, buf, len); |
249 | 0 | } |
250 | | |
251 | | void Curl_infof(struct Curl_easy *data, const char *fmt, ...) |
252 | 0 | { |
253 | 0 | DEBUGASSERT(!strchr(fmt, '\n')); |
254 | 0 | if(Curl_trc_is_verbose(data)) { |
255 | 0 | va_list ap; |
256 | 0 | va_start(ap, fmt); |
257 | 0 | trc_infof(data, data->state.feat, NULL, 0, fmt, ap); |
258 | 0 | va_end(ap); |
259 | 0 | } |
260 | 0 | } |
261 | | |
262 | | void Curl_trc_cf_infof(struct Curl_easy *data, const struct Curl_cfilter *cf, |
263 | | const char *fmt, ...) |
264 | 0 | { |
265 | 0 | DEBUGASSERT(cf); |
266 | 0 | if(Curl_trc_cf_is_verbose(cf, data)) { |
267 | 0 | va_list ap; |
268 | 0 | va_start(ap, fmt); |
269 | 0 | trc_infof(data, data->state.feat, cf->cft->name, cf->sockindex, fmt, ap); |
270 | 0 | va_end(ap); |
271 | 0 | } |
272 | 0 | } |
273 | | |
274 | | static const char * const Curl_trc_timer_names[] = { |
275 | | "100_TIMEOUT", |
276 | | "ASYNC_NAME", |
277 | | "CONNECTTIMEOUT", |
278 | | "DNS_PER_NAME", |
279 | | "DNS_PER_NAME2", |
280 | | "HAPPY_EYEBALLS_DNS", |
281 | | "HAPPY_EYEBALLS", |
282 | | "MULTI_PENDING", |
283 | | "SPEEDCHECK", |
284 | | "TIMEOUT", |
285 | | "TOOFAST", |
286 | | "QUIC", |
287 | | "FTP_ACCEPT", |
288 | | "ALPN_EYEBALLS", |
289 | | "SHUTDOWN", |
290 | | }; |
291 | | |
292 | | static const char *trc_timer_name(int tid) |
293 | 0 | { |
294 | 0 | if((tid >= 0) && ((size_t)tid < CURL_ARRAYSIZE(Curl_trc_timer_names))) |
295 | 0 | return Curl_trc_timer_names[(size_t)tid]; |
296 | 0 | return "UNKNOWN?"; |
297 | 0 | } |
298 | | |
299 | | void Curl_trc_timer(struct Curl_easy *data, int tid, const char *fmt, ...) |
300 | 0 | { |
301 | 0 | DEBUGASSERT(!strchr(fmt, '\n')); |
302 | 0 | if(Curl_trc_ft_is_verbose(data, &Curl_trc_feat_timer)) { |
303 | 0 | const char *tname = trc_timer_name(tid); |
304 | 0 | va_list ap; |
305 | 0 | va_start(ap, fmt); |
306 | 0 | trc_infof(data, &Curl_trc_feat_timer, tname, 0, fmt, ap); |
307 | 0 | va_end(ap); |
308 | 0 | } |
309 | 0 | } |
310 | | |
311 | | void Curl_trc_easy_timers(struct Curl_easy *data) |
312 | 0 | { |
313 | 0 | if(CURL_TRC_TIMER_is_verbose(data)) { |
314 | 0 | struct Curl_llist_node *e = Curl_llist_head(&data->state.timeoutlist); |
315 | 0 | if(e) { |
316 | 0 | const struct curltime *pnow = Curl_pgrs_now(data); |
317 | 0 | while(e) { |
318 | 0 | struct time_node *n = Curl_node_elem(e); |
319 | 0 | e = Curl_node_next(e); |
320 | 0 | CURL_TRC_TIMER(data, n->eid, "expires in %" FMT_TIMEDIFF_T "ns", |
321 | 0 | curlx_ptimediff_us(&n->time, pnow)); |
322 | 0 | } |
323 | 0 | } |
324 | 0 | } |
325 | 0 | } |
326 | | |
327 | | static const char * const Curl_trc_mstate_names[] = { |
328 | | "INIT", |
329 | | "PENDING", |
330 | | "SETUP", |
331 | | "CONNECT", |
332 | | "RESOLVING", |
333 | | "CONNECTING", |
334 | | "PROTOCONNECT", |
335 | | "PROTOCONNECTING", |
336 | | "DO", |
337 | | "DOING", |
338 | | "DOING_MORE", |
339 | | "DID", |
340 | | "PERFORMING", |
341 | | "RATELIMITING", |
342 | | "DONE", |
343 | | "COMPLETED", |
344 | | "MSGSENT", |
345 | | }; |
346 | | |
347 | | const char *Curl_trc_mstate_name(int state) |
348 | 0 | { |
349 | 0 | if((state >= 0) && ((size_t)state < CURL_ARRAYSIZE(Curl_trc_mstate_names))) |
350 | 0 | return Curl_trc_mstate_names[(size_t)state]; |
351 | 0 | return "?"; |
352 | 0 | } |
353 | | |
354 | | void Curl_trc_multi(struct Curl_easy *data, const char *fmt, ...) |
355 | 0 | { |
356 | 0 | DEBUGASSERT(!strchr(fmt, '\n')); |
357 | 0 | if(Curl_trc_ft_is_verbose(data, &Curl_trc_feat_multi)) { |
358 | 0 | const char *sname = (data->id >= 0) ? |
359 | 0 | Curl_trc_mstate_name(data->mstate) : NULL; |
360 | 0 | va_list ap; |
361 | 0 | va_start(ap, fmt); |
362 | 0 | trc_infof(data, &Curl_trc_feat_multi, sname, 0, fmt, ap); |
363 | 0 | va_end(ap); |
364 | 0 | } |
365 | 0 | } |
366 | | |
367 | | void Curl_trc_read(struct Curl_easy *data, const char *fmt, ...) |
368 | 0 | { |
369 | 0 | DEBUGASSERT(!strchr(fmt, '\n')); |
370 | 0 | if(Curl_trc_ft_is_verbose(data, &Curl_trc_feat_read)) { |
371 | 0 | va_list ap; |
372 | 0 | va_start(ap, fmt); |
373 | 0 | trc_infof(data, &Curl_trc_feat_read, NULL, 0, fmt, ap); |
374 | 0 | va_end(ap); |
375 | 0 | } |
376 | 0 | } |
377 | | |
378 | | void Curl_trc_write(struct Curl_easy *data, const char *fmt, ...) |
379 | 0 | { |
380 | 0 | DEBUGASSERT(!strchr(fmt, '\n')); |
381 | 0 | if(Curl_trc_ft_is_verbose(data, &Curl_trc_feat_write)) { |
382 | 0 | va_list ap; |
383 | 0 | va_start(ap, fmt); |
384 | 0 | trc_infof(data, &Curl_trc_feat_write, NULL, 0, fmt, ap); |
385 | 0 | va_end(ap); |
386 | 0 | } |
387 | 0 | } |
388 | | |
389 | | void Curl_trc_dns(struct Curl_easy *data, const char *fmt, ...) |
390 | 0 | { |
391 | 0 | DEBUGASSERT(!strchr(fmt, '\n')); |
392 | 0 | if(Curl_trc_ft_is_verbose(data, &Curl_trc_feat_dns)) { |
393 | 0 | va_list ap; |
394 | 0 | va_start(ap, fmt); |
395 | 0 | trc_infof(data, &Curl_trc_feat_dns, NULL, 0, fmt, ap); |
396 | 0 | va_end(ap); |
397 | 0 | } |
398 | 0 | } |
399 | | |
400 | | #ifndef CURL_DISABLE_FTP |
401 | | struct curl_trc_feat Curl_trc_feat_ftp = { |
402 | | "FTP", |
403 | | CURL_LOG_LVL_NONE, |
404 | | }; |
405 | | |
406 | | void Curl_trc_ftp(struct Curl_easy *data, const char *fmt, ...) |
407 | 0 | { |
408 | 0 | DEBUGASSERT(!strchr(fmt, '\n')); |
409 | 0 | if(Curl_trc_ft_is_verbose(data, &Curl_trc_feat_ftp)) { |
410 | 0 | va_list ap; |
411 | 0 | va_start(ap, fmt); |
412 | 0 | trc_infof(data, &Curl_trc_feat_ftp, NULL, 0, fmt, ap); |
413 | 0 | va_end(ap); |
414 | 0 | } |
415 | 0 | } |
416 | | #endif /* !CURL_DISABLE_FTP */ |
417 | | |
418 | | #ifndef CURL_DISABLE_SMTP |
419 | | struct curl_trc_feat Curl_trc_feat_smtp = { |
420 | | "SMTP", |
421 | | CURL_LOG_LVL_NONE, |
422 | | }; |
423 | | |
424 | | void Curl_trc_smtp(struct Curl_easy *data, const char *fmt, ...) |
425 | 0 | { |
426 | 0 | DEBUGASSERT(!strchr(fmt, '\n')); |
427 | 0 | if(Curl_trc_ft_is_verbose(data, &Curl_trc_feat_smtp)) { |
428 | 0 | va_list ap; |
429 | 0 | va_start(ap, fmt); |
430 | 0 | trc_infof(data, &Curl_trc_feat_smtp, NULL, 0, fmt, ap); |
431 | 0 | va_end(ap); |
432 | 0 | } |
433 | 0 | } |
434 | | #endif /* !CURL_DISABLE_SMTP */ |
435 | | |
436 | | #ifdef USE_SSL |
437 | | struct curl_trc_feat Curl_trc_feat_ssls = { |
438 | | "SSLS", |
439 | | CURL_LOG_LVL_NONE, |
440 | | }; |
441 | | |
442 | | void Curl_trc_ssls(struct Curl_easy *data, const char *fmt, ...) |
443 | 0 | { |
444 | 0 | DEBUGASSERT(!strchr(fmt, '\n')); |
445 | 0 | if(Curl_trc_ft_is_verbose(data, &Curl_trc_feat_ssls)) { |
446 | 0 | va_list ap; |
447 | 0 | va_start(ap, fmt); |
448 | 0 | trc_infof(data, &Curl_trc_feat_ssls, NULL, 0, fmt, ap); |
449 | 0 | va_end(ap); |
450 | 0 | } |
451 | 0 | } |
452 | | #endif /* USE_SSL */ |
453 | | |
454 | | #ifdef USE_SSH |
455 | | struct curl_trc_feat Curl_trc_feat_ssh = { |
456 | | "SSH", |
457 | | CURL_LOG_LVL_NONE, |
458 | | }; |
459 | | |
460 | | void Curl_trc_ssh(struct Curl_easy *data, const char *fmt, ...) |
461 | | { |
462 | | DEBUGASSERT(!strchr(fmt, '\n')); |
463 | | if(Curl_trc_ft_is_verbose(data, &Curl_trc_feat_ssh)) { |
464 | | va_list ap; |
465 | | va_start(ap, fmt); |
466 | | trc_infof(data, &Curl_trc_feat_ssh, NULL, 0, fmt, ap); |
467 | | va_end(ap); |
468 | | } |
469 | | } |
470 | | #endif /* USE_SSH */ |
471 | | |
472 | | #if !defined(CURL_DISABLE_WEBSOCKETS) && !defined(CURL_DISABLE_HTTP) |
473 | | struct curl_trc_feat Curl_trc_feat_ws = { |
474 | | "WS", |
475 | | CURL_LOG_LVL_NONE, |
476 | | }; |
477 | | |
478 | | void Curl_trc_ws(struct Curl_easy *data, const char *fmt, ...) |
479 | 0 | { |
480 | 0 | DEBUGASSERT(!strchr(fmt, '\n')); |
481 | 0 | if(Curl_trc_ft_is_verbose(data, &Curl_trc_feat_ws)) { |
482 | 0 | va_list ap; |
483 | 0 | va_start(ap, fmt); |
484 | 0 | trc_infof(data, &Curl_trc_feat_ws, NULL, 0, fmt, ap); |
485 | 0 | va_end(ap); |
486 | 0 | } |
487 | 0 | } |
488 | | #endif /* !CURL_DISABLE_WEBSOCKETS && !CURL_DISABLE_HTTP */ |
489 | | |
490 | 0 | #define TRC_CT_NONE (0) |
491 | 0 | #define TRC_CT_PROTOCOL (1 << 0) |
492 | 0 | #define TRC_CT_NETWORK (1 << 1) |
493 | 0 | #define TRC_CT_PROXY (1 << 2) |
494 | | #define TRC_CT_INTERNALS (1 << 3) |
495 | | |
496 | | struct trc_feat_def { |
497 | | struct curl_trc_feat *feat; |
498 | | unsigned int category; |
499 | | }; |
500 | | |
501 | | static struct trc_feat_def trc_feats[] = { |
502 | | { &Curl_trc_feat_ids, TRC_CT_INTERNALS }, |
503 | | { &Curl_trc_feat_multi, TRC_CT_NETWORK }, |
504 | | { &Curl_trc_feat_read, TRC_CT_NONE }, |
505 | | { &Curl_trc_feat_write, TRC_CT_NONE }, |
506 | | { &Curl_trc_feat_dns, TRC_CT_NETWORK }, |
507 | | { &Curl_trc_feat_timer, TRC_CT_NETWORK }, |
508 | | #ifndef CURL_DISABLE_FTP |
509 | | { &Curl_trc_feat_ftp, TRC_CT_PROTOCOL }, |
510 | | #endif |
511 | | #ifndef CURL_DISABLE_SMTP |
512 | | { &Curl_trc_feat_smtp, TRC_CT_PROTOCOL }, |
513 | | #endif |
514 | | #ifdef USE_SSL |
515 | | { &Curl_trc_feat_ssls, TRC_CT_NETWORK }, |
516 | | #endif |
517 | | #ifdef USE_SSH |
518 | | { &Curl_trc_feat_ssh, TRC_CT_PROTOCOL }, |
519 | | #endif |
520 | | #if !defined(CURL_DISABLE_WEBSOCKETS) && !defined(CURL_DISABLE_HTTP) |
521 | | { &Curl_trc_feat_ws, TRC_CT_PROTOCOL }, |
522 | | #endif |
523 | | }; |
524 | | |
525 | | struct trc_cft_def { |
526 | | struct Curl_cftype *cft; |
527 | | unsigned int category; |
528 | | }; |
529 | | |
530 | | static struct trc_cft_def trc_cfts[] = { |
531 | | { &Curl_cft_tcp, TRC_CT_NETWORK }, |
532 | | { &Curl_cft_udp, TRC_CT_NETWORK }, |
533 | | { &Curl_cft_unix, TRC_CT_NETWORK }, |
534 | | { &Curl_cft_tcp_accept, TRC_CT_NETWORK }, |
535 | | { &Curl_cft_ip_happy, TRC_CT_NETWORK }, |
536 | | { &Curl_cft_setup, TRC_CT_PROTOCOL }, |
537 | | #if !defined(CURL_DISABLE_HTTP) && defined(USE_NGHTTP2) |
538 | | { &Curl_cft_nghttp2, TRC_CT_PROTOCOL }, |
539 | | #endif |
540 | | #ifdef USE_SSL |
541 | | { &Curl_cft_ssl, TRC_CT_NETWORK }, |
542 | | #ifndef CURL_DISABLE_PROXY |
543 | | { &Curl_cft_ssl_proxy, TRC_CT_PROXY }, |
544 | | #endif |
545 | | #endif |
546 | | #ifndef CURL_DISABLE_PROXY |
547 | | #ifndef CURL_DISABLE_HTTP |
548 | | { &Curl_cft_h1_proxy, TRC_CT_PROXY }, |
549 | | #ifdef USE_NGHTTP2 |
550 | | { &Curl_cft_h2_proxy, TRC_CT_PROXY }, |
551 | | #endif |
552 | | { &Curl_cft_http_proxy, TRC_CT_PROXY }, |
553 | | #endif /* !CURL_DISABLE_HTTP */ |
554 | | { &Curl_cft_haproxy, TRC_CT_PROXY }, |
555 | | { &Curl_cft_socks_proxy, TRC_CT_PROXY }, |
556 | | #endif /* !CURL_DISABLE_PROXY */ |
557 | | #if !defined(CURL_DISABLE_HTTP) && defined(USE_HTTP3) |
558 | | { &Curl_cft_http3, TRC_CT_PROTOCOL }, |
559 | | #endif |
560 | | #ifndef CURL_DISABLE_HTTP |
561 | | { &Curl_cft_http_connect, TRC_CT_PROTOCOL }, |
562 | | #endif |
563 | | }; |
564 | | |
565 | | static void trc_apply_level_by_name(struct Curl_str *token, int lvl) |
566 | 0 | { |
567 | 0 | size_t i; |
568 | |
|
569 | 0 | for(i = 0; i < CURL_ARRAYSIZE(trc_cfts); ++i) { |
570 | 0 | if(curlx_str_casecompare(token, trc_cfts[i].cft->name)) { |
571 | 0 | trc_cfts[i].cft->log_level = lvl; |
572 | 0 | break; |
573 | 0 | } |
574 | 0 | } |
575 | 0 | for(i = 0; i < CURL_ARRAYSIZE(trc_feats); ++i) { |
576 | 0 | if(curlx_str_casecompare(token, trc_feats[i].feat->name)) { |
577 | 0 | trc_feats[i].feat->log_level = lvl; |
578 | 0 | break; |
579 | 0 | } |
580 | 0 | } |
581 | 0 | } |
582 | | |
583 | | static void trc_apply_level_by_category(int category, int lvl) |
584 | 0 | { |
585 | 0 | size_t i; |
586 | |
|
587 | 0 | for(i = 0; i < CURL_ARRAYSIZE(trc_cfts); ++i) { |
588 | 0 | if(!category || (trc_cfts[i].category & category)) |
589 | 0 | trc_cfts[i].cft->log_level = lvl; |
590 | 0 | } |
591 | 0 | for(i = 0; i < CURL_ARRAYSIZE(trc_feats); ++i) { |
592 | 0 | if(!category || (trc_feats[i].category & category)) |
593 | 0 | trc_feats[i].feat->log_level = lvl; |
594 | 0 | } |
595 | 0 | } |
596 | | |
597 | | static CURLcode trc_opt(const char *config) |
598 | 0 | { |
599 | 0 | struct Curl_str out; |
600 | 0 | while(!curlx_str_until(&config, &out, 32, ',')) { |
601 | 0 | int lvl = CURL_LOG_LVL_INFO; |
602 | 0 | const char *token = curlx_str(&out); |
603 | |
|
604 | 0 | if(*token == '-') { |
605 | 0 | lvl = CURL_LOG_LVL_NONE; |
606 | 0 | curlx_str_nudge(&out, 1); |
607 | 0 | } |
608 | 0 | else if(*token == '+') |
609 | 0 | curlx_str_nudge(&out, 1); |
610 | |
|
611 | 0 | if(curlx_str_casecompare(&out, "all")) |
612 | 0 | trc_apply_level_by_category(TRC_CT_NONE, lvl); |
613 | 0 | else if(curlx_str_casecompare(&out, "protocol")) |
614 | 0 | trc_apply_level_by_category(TRC_CT_PROTOCOL, lvl); |
615 | 0 | else if(curlx_str_casecompare(&out, "network")) |
616 | 0 | trc_apply_level_by_category(TRC_CT_NETWORK, lvl); |
617 | 0 | else if(curlx_str_casecompare(&out, "proxy")) |
618 | 0 | trc_apply_level_by_category(TRC_CT_PROXY, lvl); |
619 | 0 | else if(curlx_str_casecompare(&out, "doh")) { |
620 | 0 | struct Curl_str dns = { "dns", 3 }; |
621 | 0 | trc_apply_level_by_name(&dns, lvl); |
622 | 0 | } |
623 | 0 | else |
624 | 0 | trc_apply_level_by_name(&out, lvl); |
625 | |
|
626 | 0 | if(curlx_str_single(&config, ',')) |
627 | 0 | break; |
628 | 0 | } |
629 | 0 | return CURLE_OK; |
630 | 0 | } |
631 | | |
632 | | CURLcode Curl_trc_opt(const char *config) |
633 | 16 | { |
634 | 16 | CURLcode result = config ? trc_opt(config) : CURLE_OK; |
635 | 16 | #ifdef DEBUGBUILD |
636 | | /* CURL_DEBUG can override anything */ |
637 | 16 | if(!result) { |
638 | 16 | const char *dbg_config = getenv("CURL_DEBUG"); |
639 | 16 | if(dbg_config) |
640 | 0 | result = trc_opt(dbg_config); |
641 | 16 | } |
642 | 16 | #endif /* DEBUGBUILD */ |
643 | 16 | return result; |
644 | 16 | } |
645 | | |
646 | | CURLcode Curl_trc_init(void) |
647 | 16 | { |
648 | 16 | #ifdef DEBUGBUILD |
649 | 16 | return Curl_trc_opt(NULL); |
650 | | #else |
651 | | return CURLE_OK; |
652 | | #endif |
653 | 16 | } |
654 | | |
655 | | #else /* CURL_DISABLE_VERBOSE_STRINGS */ |
656 | | |
657 | | CURLcode Curl_trc_init(void) |
658 | | { |
659 | | return CURLE_OK; |
660 | | } |
661 | | |
662 | | void Curl_infof(struct Curl_easy *data, const char *fmt, ...) |
663 | | { |
664 | | (void)data; |
665 | | (void)fmt; |
666 | | } |
667 | | |
668 | | void Curl_trc_cf_infof(struct Curl_easy *data, const struct Curl_cfilter *cf, |
669 | | const char *fmt, ...) |
670 | | { |
671 | | (void)data; |
672 | | (void)cf; |
673 | | (void)fmt; |
674 | | } |
675 | | |
676 | | void Curl_trc_multi(struct Curl_easy *data, const char *fmt, ...) |
677 | | { |
678 | | (void)data; |
679 | | (void)fmt; |
680 | | } |
681 | | |
682 | | void Curl_trc_write(struct Curl_easy *data, const char *fmt, ...) |
683 | | { |
684 | | (void)data; |
685 | | (void)fmt; |
686 | | } |
687 | | |
688 | | void Curl_trc_dns(struct Curl_easy *data, const char *fmt, ...) |
689 | | { |
690 | | (void)data; |
691 | | (void)fmt; |
692 | | } |
693 | | |
694 | | void Curl_trc_timer(struct Curl_easy *data, int tid, const char *fmt, ...) |
695 | | { |
696 | | (void)data; |
697 | | (void)tid; |
698 | | (void)fmt; |
699 | | } |
700 | | |
701 | | void Curl_trc_read(struct Curl_easy *data, const char *fmt, ...) |
702 | | { |
703 | | (void)data; |
704 | | (void)fmt; |
705 | | } |
706 | | |
707 | | #ifndef CURL_DISABLE_FTP |
708 | | void Curl_trc_ftp(struct Curl_easy *data, const char *fmt, ...) |
709 | | { |
710 | | (void)data; |
711 | | (void)fmt; |
712 | | } |
713 | | #endif |
714 | | #ifndef CURL_DISABLE_SMTP |
715 | | void Curl_trc_smtp(struct Curl_easy *data, const char *fmt, ...) |
716 | | { |
717 | | (void)data; |
718 | | (void)fmt; |
719 | | } |
720 | | #endif |
721 | | #if !defined(CURL_DISABLE_WEBSOCKETS) && !defined(CURL_DISABLE_HTTP) |
722 | | void Curl_trc_ws(struct Curl_easy *data, const char *fmt, ...) |
723 | | { |
724 | | (void)data; |
725 | | (void)fmt; |
726 | | } |
727 | | #endif |
728 | | #ifdef USE_SSH |
729 | | void Curl_trc_ssh(struct Curl_easy *data, const char *fmt, ...) |
730 | | { |
731 | | (void)data; |
732 | | (void)fmt; |
733 | | } |
734 | | #endif |
735 | | #ifdef USE_SSL |
736 | | void Curl_trc_ssls(struct Curl_easy *data, const char *fmt, ...) |
737 | | { |
738 | | (void)data; |
739 | | (void)fmt; |
740 | | } |
741 | | #endif |
742 | | |
743 | | #endif /* !CURL_DISABLE_VERBOSE_STRINGS */ |