52 Commits

Author SHA1 Message Date
Viktor Szakats
85c841cb45 msvc: drop exception, make BIT() a bitfield with Visual Studio
Add casts to `bool`, or use `bit` type in local variables, where
neccessary to avoid MSVC compiler warnings C4242.

Note: There may remain places needing the above updates, where not
tested in CI, and missed in manual review.

Also:
- urldata: convert struct field `connect_only` to bitfield to match its
  counterpart in another struct.
- rename curl-specific `bit` type to `curl_bit`.

Closes #20142
2026-01-17 11:46:31 +01:00
Viktor Szakats
57ff2d6c91 dns: flatten the include tree
Closes #20106
2026-01-17 11:45:18 +01:00
Stefan Eissing
6c8956c1cb socketpair: cleaner interface
Declutter the ifdefs in socketpair.h. Introduce Curl_wakeup_*()
function that encapsulate the details about how the socketpair
is implemented.

This moves the EVENTFD specials from the using code into socketpair
implemenatation, avoiding duplications in three places.

Closes #20340
2026-01-16 16:43:43 +01:00
Viktor Szakats
84ff0f6474 lib: fix two CURLDEBUG guards to be DEBUGBUILD
Both guard the use of `CURL_DNS_SERVER` debug env.

Follow-up to df2b4ccc22 #18157
Follow-up to 02e9690c3e #17015
Follow-up to 59dc9f7e69 #13718

Closes #20328
2026-01-15 13:08:19 +01:00
Viktor Szakats
59a5fd8518 build: drop redundant unused variable suppressions
Also:
- digest_sspi: merge some `ifdef`s.

Closes #20310
2026-01-14 10:38:34 +01:00
Viktor Szakats
7032982896 tidy-up: miscellaneous
- asyn-thrdd.c: scope an include.
- apply more clang-format suggestions.
- tidy-up PP guard comments.
- delete empty line from the top of headers.
- add empty line after `curl_setup.h` include where missing.
- fix indent.
- CODE_STYLE.md: add `strcpy`.
  Follow-up to 8636ad55df #20088
- lib1901.c: drop unnecessary line.
  Follow-up to 436e67f65b #20076

Closes #20070
2025-12-26 22:06:09 +01:00
Viktor Szakats
e8415a8296 lib: drop, or replace sendf.h with curl_trc.h where possible
- replace `sendf.h` with `curl_trc.h` where it was included just for it.
- drop unused `curl_trc.h` includes.
- easy: delete obsolete comment about `send.h` include reason.

Also:
- move out `curl_trc.h` include from `sendf.h` and include it directly
  in users, where not done already. To flatten the include tree and
  to less rely on indirect includes.
- stop including `sendf.h` from other headers, replace it with forward
  declaration of `Curl_easy`, as done already elsewhere.

Verified with an all non-unity CI run.

Closes #20061
2025-12-21 12:39:25 +01:00
Viktor Szakats
a354cc8664 lib: drop includes unused or duplicate
Closes #20051
2025-12-20 22:02:20 +01:00
Viktor Szakats
951d778c33 build: drop unused curl_share.h includes
Closes #20022
2025-12-19 01:34:56 +01:00
Stefan Eissing
b4be1f271e time-keeping: keep timestamp in multi, always update
Always use curlx_now() when calling Curl_pgrs_now(data). Tests with the
"manual" updates to now proved differ more then 100ms in parallel testing.

Add `curlx_nowp()` to set current time into a struct curltime.
Add `curlx_ptimediff_ms() and friends, passing pointers.

Update documentation.

Closes #19998
2025-12-18 22:10:06 +01:00
Stefan Eissing
2de22a00c7 lib: keep timestamp in easy handle
Use `data->progress.now` as the timestamp of proecssing a transfer.
Update it on significant events and refrain from calling `curlx_now()`
in many places.

The problem this addresses is
a) calling curlx_now() has costs, depending on platform. Calling it
   every time results in 25% increase `./runtest` duration on macOS.
b) we used to pass a `struct curltime *` around to save on calls, but
   when some method directly use `curx_now()` and some use the passed
   pointer, the transfer experienes non-linear time. This results in
   timeline checks to report events in the wrong order.

By keeping a timestamp in the easy handle and updating it there, no
longer invoking `curlx_now()` in the "lower" methods, the transfer
can observer a steady clock progression.

Add documentation in docs/internals/TIME-KEEPING.md

Reported-by: Viktor Szakats
Fixes #19935
Closes #19961
2025-12-16 08:48:44 +01:00
Daniel Stenberg
f4b56f34ba asyn-thrdd: fix Curl_async_getaddrinfo() on systems without getaddrinfo
Follow-up to ce06fe7771
Bug: ce06fe7771 (r172215567)
Reported-by: Harry Sintonen
Closes #19859
2025-12-07 00:08:19 +01:00
Viktor Szakats
47a1ab2ebe lib: fix formatting nits (part 1)
From `lib/a*` to `lib/cs*`.

Closes #19764
2025-11-30 11:01:50 +01:00
Viktor Szakats
193cb00ce9 build: stop overriding standard memory allocation functions
Before this patch curl used the C preprocessor to override standard
memory allocation symbols: malloc, calloc, strdup, realloc, free.
The goal of these is to replace them with curl's debug wrappers in
`CURLDEBUG` builds, another was to replace them with the wrappers
calling user-defined allocators in libcurl. This solution needed a bunch
of workarounds to avoid breaking external headers: it relied on include
order to do the overriding last. For "unity" builds it needed to reset
overrides before external includes. Also in test apps, which are always
built as single source files. It also needed the `(symbol)` trick
to avoid overrides in some places. This would still not fix cases where
the standard symbols were macros. It was also fragile and difficult
to figure out which was the actual function behind an alloc or free call
in a specific piece of code. This in turn caused bugs where the wrong
allocator was accidentally called.

To avoid these problems, this patch replaces this solution with
`curlx_`-prefixed allocator macros, and mapping them _once_ to either
the libcurl wrappers, the debug wrappers or the standard ones, matching
the rest of the code in libtests.

This concludes the long journey to avoid redefining standard functions
in the curl codebase.

Note: I did not update `packages/OS400/*.c` sources. They did not
`#include` `curl_setup.h`, `curl_memory.h` or `memdebug.h`, meaning
the overrides were never applied to them. This may or may not have been
correct. For now I suppressed the direct use of standard allocators
via a local `.checksrc`. Probably they (except for `curlcl.c`) should be
updated to include `curl_setup.h` and use the `curlx_` macros.

This patch changes mappings in two places:
- `lib/curl_threads.c` in libtests: Before this patch it mapped to
  libcurl allocators. After, it maps to standard allocators, like
  the rest of libtests code.
- `units`: before this patch it mapped to standard allocators. After, it
  maps to libcurl allocators.

Also:
- drop all position-dependent `curl_memory.h` and `memdebug.h` includes,
  and delete the now unnecessary headers.
- rename `Curl_tcsdup` macro to `curlx_tcsdup` and define like the other
  allocators.
- map `curlx_strdup()` to `_strdup()` on Windows (was: `strdup()`).
  To fix warnings silenced via `_CRT_NONSTDC_NO_DEPRECATE`.
- multibyte: map `curlx_convert_*()` to `_strdup()` on Windows
  (was: `strdup()`).
- src: do not reuse the `strdup` name for the local replacement.
- lib509: call `_strdup()` on Windows (was: `strdup()`).
- test1132: delete test obsoleted by this patch.
- CHECKSRC.md: update text for `SNPRINTF`.
- checksrc: ban standard allocator symbols.

Follow-up to b12da22db1 #18866
Follow-up to db98daab05 #18844
Follow-up to 4deea9396b #18814
Follow-up to 9678ff5b1b #18776
Follow-up to 10bac43b87 #18774
Follow-up to 20142f5d06 #18634
Follow-up to bf7375ecc5 #18503
Follow-up to 9863599d69 #18502
Follow-up to 3bb5e58c10 #17827

Closes #19626
2025-11-28 10:44:26 +01:00
Daniel Stenberg
ce06fe7771 hostip: make more functions return CURLcode
- Curl_async_getaddrinfo() always returned NULL so it was pointless.
  Return proper curlcode instead to distinguish between errors. Same for
  Curl_doh().
- simplify the IP address handling
- make Curl_str2addr() function return CURLcode

Closes #19669
2025-11-25 09:13:34 +01:00
Viktor Szakats
3887069c66 lib: rename internal header share.h to curl_share.h to avoid collision
Windows CRTs have a `share.h`. Before this patch when trying to
`#include <share.h>` it, the compiler picked up curl's internal
`lib/share.h` instead. Rename it to avoid this issue.

CRT `share.h` has constants necessary for using safe open CRT functions.

Also rename `lib/share.c` to keep matching the header.

Ref: https://learn.microsoft.com/cpp/c-runtime-library/sharing-constants
Ref: 625f2c1644 #16949 #16991
Cherry-picked from #19643
Closes #19676
2025-11-25 00:26:50 +01:00
Viktor Szakats
554dfa5568 build: drop Windows CE / CeGCC support
Windows CE support was limited to successful builds with ming32ce
(a toolchain that hasn't seen an update since 2009, using an ancient gcc
version and "old mingw"-style SDK headers, that curl deprecated earlier).
Builds with MSVC were broken for a long time. mingw32ce builds were never
actually tested and runtime and unlikely to work due to missing stubs.
Windows CE toolchains also miss to comply with C89. Paired with lack of
demand and support for the platform, curl deprecated it earlier.

This patch removes support from the codebase to ease maintaining Windows
codepaths.

Follow-up to f98c0ba834 #17924
Follow-up to 8491e6574c #17379
Follow-up to 2a292c3984 #15975

Closes #17927
2025-11-15 15:35:23 +01:00
Stefan Eissing
78a610cb83 lib: rename curlx_timediff to curlx_timeleft_ms
Rename `Curl_timeleft()` to `Curl_timeleft_ms()` to make the units in
the returned `timediff_t` clear. (We used to always have ms there, but
with QUIC started to sometimes calc ns as well).

Rename some assigned vars without `_ms` suffix for clarity as well.

Closes #19486
2025-11-13 13:12:58 +01:00
x2018
1bddfe02d5 asyn-thrdd: release rrname if ares_init_options fails
Closes #19410
2025-11-09 11:35:04 +01:00
x2018
dd71f61ea2 lib: cleanup for some typos about spaces and code style
Closes #19370
2025-11-05 14:07:28 +01:00
Daniel Stenberg
8d0bfe74fb httpsrr: send HTTPS query to the right target
When the target host is on a different port than 443, the name
"_[port]._https.[name]" shall be used.

Fixes #19301
Reported-by: Gunni on github
Closes #19324
2025-11-01 23:13:30 +01:00
Viktor Szakats
a13d811044 lib: delete unused header includes
`escape.h`, `getinfo.h`, `strdup.h`.

Tested OK with full non-unity CI run.

Closes #19231
2025-10-25 00:19:00 +02:00
Viktor Szakats
b12da22db1 lib: stop overriding system printf symbols
After this patch, the codebase no longer overrides system printf
functions. Instead it explicitly calls either the curl printf functions
`curl_m*printf()` or the system ones using their original names.

Also:
- drop unused `curl_printf.h` includes.
- checksrc: ban system printf functions, allow where necessary.

Follow-up to db98daab05 #18844
Follow-up to 4deea9396b #18814

Closes #18866
2025-10-06 20:57:59 +02:00
Stefan Eissing
d8823e855c asyn-thrdd resolver: clear timeout when done
When the async threaded resolver thread returned, clear the
started EXPIRE_ASYNC_NAME timeout.

Closes #18769
2025-10-01 07:59:41 +02:00
Stefan Eissing
de3fc1d7ad asyn-thrdd: drop pthread_cancel
Remove use of pthread_cancel in asnyc threaded resolving. While there
are system where this works, others might leak to resource leakage
(memory, files, etc.). The popular nsswitch is one example where resolve
code can be dragged in that is not prepared.

The overall promise and mechanism of pthread_cancel() is just too
brittle and the historcal design of getaddrinfo() continues to haunt us.

Fixes #18532
Reported-by: Javier Blazquez
Closes #18540
2025-09-15 09:25:43 +02:00
Stefan Eissing
d57cfc1a8d asyn-thrdd: more simplifications
- use wakeup sockets non-locked.
- send wakeup notify only in normal control flow (not cancel). close
  wakeup sockets in unlink only.
- remove 5ms thread lifetime wait crutch before pthread_cancel().

Closes #18380
2025-08-23 16:25:39 +02:00
Viktor Szakats
03868ca714 asyn-thrdd: fix no HAVE_GETADDRINFO builds
mingw32ce, CM 4.4.0-arm schannel:
```
lib/asyn-thrdd.c: In function 'gethostbyname_thread':
lib/asyn-thrdd.c:349: error: too many arguments to function 'async_thrd_cleanup'
```
Ref: https://github.com/curl/curl/actions/runs/17158865566/job/48682687295?pr=18039#step:9:21

Follow-up to 88fc6c491f #18263
Closes #18371
2025-08-23 00:16:06 +02:00
Stefan Eissing
a8d20cd223 asyn-thrdd: manage DEFERRED and locks better
- cancel thread waits until thread start is at least 5ms in the past
  to give it some time to get its cancellation setup in place
- cancel thread without holding the mutex. It's supposed to be an
  async operation, but better be safe
- set DEFERRED cancel state explicitly, should be default in a pthread,
  but better be safe

Closes #18350
2025-08-22 16:26:11 +02:00
Stefan Eissing
cbd5d2507a asyn-thrdd: fix --disable-socketpair builds
Regression from #18339

Reported-by: Marcel Raad
Closes #18347
2025-08-22 10:36:43 +02:00
Stefan Eissing
6b111f0a8c ares: use ares_strerror() to retrieve error messages
Add optional detail to `Curl_resolver_error()` to add to failure message
where available. This makes, for c-ares, the reason for a failed
resource available to the user without extra trace config.

When "dns" tracing enabled, print the c-ares server config at the start
of a resolve.

Closes #18251
2025-08-22 10:01:58 +02:00
Stefan Eissing
8ebea37eb1 asyn-thrdd: remove condition variable
Add a flag `thrd_don` to assess if the resolving thread has finished and
only destroy the context when *both* ref_count reaches 0 and thrd_done
is true.

Closes #18345
2025-08-22 09:05:56 +02:00
Daniel Stenberg
c83fa990bd Revert "asyn-thrdd: use condition var more carefully"
This reverts commit bd4622bfaf.

Not intended for merge!
2025-08-22 09:05:19 +02:00
Stefan Eissing
bd4622bfaf asyn-thrdd: use condition var more carefully
When the thread started is too fast, the signal will come before the
wait. Add an additional check before the wait to catch the thread
having started or already ended.

Closes #18344
2025-08-22 08:40:40 +02:00
Stefan Eissing
b63cce7fee asyn-thrdd: addressing stalls in ci
Disable pthread_cancel in pushed cleanup function. Close wakeup socket
when shutting down resolving, since we no longer want wakeups.

Closes #18339
2025-08-21 17:00:45 +02:00
Stefan Eissing
88fc6c491f threaded-resolver: fix shutdown
Changed strategy to start up and terminate resolver thread.

When starting up:

Start the thread with mutex acquired, wait for signal from thread that
it started and has incremented the ref counter. Thread set
pthread_cancel() to disabled before that and only enables cancelling
during resolving itself. This assure that the ref counter is correct and
the unlinking of the resolve context always happens.

When shutting down resolving:

If ref counting shows thread has finished, join it, free everything. If
thread has not finished, try pthread_cancel() (non Windows), but keep
the thread handle around.

When destroying resolving:

Shutdown first, then, if the thread is still there and 'quick_exit' is
not set, join it and free everything. This might occur a delay if
getaddrinfo() hangs and cannot be interrupted by pthread_cancel().

Destroying resolving happens when another resolve is started on an
easy handle or when the easy handle is closed.

Add test795 to check that connect timeout triggers correctly
when resolving is delayed. Add debug env var `CURL_DNS_DELAY_MS`
to simulate delays in resolving.

Fix test1557 to set `quick_exit` and use `xxx.invalid` as domain
instead of `nothing` that was leading to hangers in CI.

Closes #18263
2025-08-21 09:26:49 +02:00
Marcel Raad
a559f94321 asyn-thrdd: silence unused parameter warning
Closes https://github.com/curl/curl/pull/18310
2025-08-18 20:04:53 +02:00
Daniel Stenberg
fc4ae23cc2 asyn-thrdd: fix Curl_async_pollset without socketpair
- remove a superfluous if()
- edit the #ifdef to only do the timeout checks when socketpair support
  is missing, as that code flow could not be reached otherways anyway

CodeSonar pointed out possible issue that led me to see this.

Closes #18306
2025-08-18 13:03:51 +02:00
Stefan Eissing
9cc4e24ad9 resolving: dns error tracing
* Add more tracing information to c-ares errors.
* remove CURL_ASYNC_SUCCESS, rename `ares->last_status` to
  `ares->ares_status`. Give trace explanation for "common"
  errors
* add ares "csv" information to tracing on failure
* DoH: invoke `Curl_resolver_error()` on failure to populate
  error buf

Closes #18247
2025-08-11 14:35:07 +02:00
Daniel Stenberg
df2b4ccc22 hostip: cache negative name resolves
Hold them for half the normal lifetime. Helps when told to transfer N
URLs in quick succession that all use the same non-resolving hostname.

Done by storing a DNS entry with a NULL pointer for 'addr'.

Previously an attempt was made in #12406 by Björn Stenberg that was
ultimately never merged.

Closes #18157
2025-08-05 08:05:31 +02:00
Stefan Eissing
5b80b4c012 lib: replace getsock() logic with pollsets
`getsock()` calls operated on a global limit that could
not be configure beyond 16 sockets. This is no longer adequate
with the new happy eyeballing strategy.

Instead, do the following:
- make `struct easy_pollset` dynamic. Starting with
  a minimal room for two sockets, the very common case,
  allow it to grow on demand.
- replace all protocol handler getsock() calls with pollsets
  and a CURLcode to return failures
- add CURLcode return for all connection filter `adjust_pollset()`
  callbacks, since they too can now fail.
- use appropriately in multi.c and multi_ev.c
- fix unit2600 to trigger pollset growth

Closes #18164
2025-08-04 23:43:13 +02:00
Viktor Szakats
4d977fe552 tidy-up: more whitespace/indent, comments
Also a couple of minor formatting updates in the root `CMakeLists.txt`.
One swap to `#ifdef`.

Closes #17929
2025-07-25 11:47:51 +02:00
Viktor Szakats
ee232917dc tidy-up: Curl_thread_create() callback return type
Replace repeat `#ifdef` code with a macro for the return type of
the thread function.

Also:
- always define `CURL_STDCALL`, allowing to use it without guards.
- lib1307: drop single-use macro `CAINFO`.

Closes #17889
2025-07-21 16:17:42 +02:00
Viktor Szakats
0260e8465a GHA/checksrc: expand spellcheck, fix issues found
- codespell: break logic out into its own runnable script. Allowing
  to run it on local machines.
- codespell: install via `pip`, bump to latest version.
- codespell: show version number in CI log.
- codespell: drop no longer needed word exception: `msdos`.
- codespell: include all curl source tree, except `packages` and
  `winbuild`. Drop an obsolete file exclusion.
- add new spellchecker job using the `typos` tool. It includes
  the codespell dictionary and a couple more. Use linuxbrew to install
  it. This takes 10 seconds, while installing via `cargo` from source
  would take over a minute.
- codespell: introduce an inline ignore filter compatible with `cspell`
  Make `typos` recognize it, too. Move single exceptions inline.

Fix new typos found. Also rename variables and words to keep
spellchecking exceptions at minumum. This involves touching some tests.
Also switch base64 strings to `%b64[]` to avoid false positives.

Ref: https://github.com/crate-ci/typos/blob/master/docs/reference.md
Ref: https://github.com/codespell-project/codespell?tab=readme-ov-file#inline-ignore
Ref: https://github.com/codespell-project/codespell/issues/1212#issuecomment-1721152455
Ref: https://cspell.org/docs/Configuration/document-settings

Closes #17905
2025-07-21 16:09:01 +02:00
Stefan Eissing
e9ae1bd404 connection: clarify transport
The `transport` to use for a transfer, e.g. TCP/QUIC/UNIX/UDP, is
initially selected by options and protocol used. This is set at the
`struct connectdata` as `transport` member.

During connection establishment, this transport may change due to
Alt-Svc or Happy-Eyeballing. Most common is the switch from TCP to QUIC.

Rename the connection member to `transport_wanted` and add a way to
query the connection for the transport in use via a new connection
filter query.

The filter query can also be used in the happy eyeballing attempts when
code needs to know which transport is used by the "filter below". This
happens in wolfssl initialization, as one example.

Closes #17923
2025-07-14 14:33:18 +02:00
Daniel Stenberg
c257753ee9 asyn-thrdd: free the previous name before strdup'ing the new
Fixes #17602
Reported-by: hiimmat on github
Closes #17604
2025-06-12 12:53:57 +02:00
Daniel Stenberg
1886260a95 lib: make curlx_inet_ntop()
move function to curlx/, change all callers

Closes #17560
2025-06-09 13:16:01 +02:00
Daniel Stenberg
f738093896 asyn-thrdd: fix cleanup when RR fails due to OOM
Fixes #17507
Closes #17514
2025-06-02 09:08:04 +02:00
Stefan Eissing
73b8912580 asnyc-thrdd: explain how this is okay with a comment
Coverity assess correctly that a variable write under mutex lock could
overwrite values from another thread - if the function were ever called
from multiple thread for the same transfer - which it is not.

Closes #17365
2025-05-16 12:55:17 +02:00
Stefan Eissing
2e1040fa5c asny-thrdd: fix detach from running thread
When cancelling a threaded resolve and the thread is
still running, detach from it under the mutex lock.

Otherwise, the detach might happen after the thread finished
and access already freed memory.

Fixes #17256
Reported-by: Mathieu Garaud
Closes #17320
2025-05-12 17:17:19 +02:00
Daniel Stenberg
255aac56f9 curlx: move into to curlx/
Move curlx_ functions into its own subdir.

The idea is to use the curlx_ prefix proper on these functions, and use
these same function names both in tool, lib and test suite source code.
Stop the previous special #define setup for curlx_ names.

The printf defines are now done for the library alone. Tests no longer
use the printf defines. The tool code sets its own defines. The printf
functions are not curlx, they are publicly available.

The strcase defines are not curlx_ functions and should not be used by
tool or server code.

dynbuf, warnless, base64, strparse, timeval, timediff are now proper
curlx functions.

When libcurl is built statically, the functions from the library can be
used as-is. The key is then that the functions must work as-is, without
having to be recompiled for use in tool/tests. This avoids symbol
collisions - when libcurl is built statically, we use those functions
directly when building the tool/tests. When libcurl is shared, we
build/link them separately for the tool/tests.

Assisted-by: Jay Satiro

Closes #17253
2025-05-07 11:01:15 +02:00