Skip to content

improve: prepare fetch_posix.c for platforms that miss getaddrinfo()#131

Merged
sergio-nsk merged 2 commits into
masterfrom
sergio-nsk/fetch/1
Apr 1, 2026
Merged

improve: prepare fetch_posix.c for platforms that miss getaddrinfo()#131
sergio-nsk merged 2 commits into
masterfrom
sergio-nsk/fetch/1

Conversation

@sergio-nsk
Copy link
Copy Markdown
Collaborator

@sergio-nsk sergio-nsk commented Mar 31, 2026

This reduces amount of #if blocks.

Summary by CodeRabbit

  • Bug Fixes
    • More reliable network connection handling to reduce connection failures.
    • Improved socket cleanup to prevent lingering/invalid socket state and potential resource leaks.

@sergio-nsk sergio-nsk requested a review from nmoinvaz March 31, 2026 07:13
@sergio-nsk sergio-nsk added the cleanup Housekeeping tasks label Mar 31, 2026
Copilot AI review requested due to automatic review settings March 31, 2026 07:13
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 31, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: a7daa53d-59ee-49f1-b4e9-6798cd848201

📥 Commits

Reviewing files that changed from the base of the PR and between 7cc601b and 2940b23.

📒 Files selected for processing (1)
  • fetch_posix.c
🚧 Files skipped from review as they are similar to previous changes (1)
  • fetch_posix.c

📝 Walkthrough

Walkthrough

fetch_get in fetch_posix.c now copies address_info->ai_addr/ai_addrlen into local addr/addrlen, uses socket(addr->sa_family, SOCK_STREAM, IPPROTO_TCP) and connect(sfd, addr, addrlen), and initializes sfd = -1 with cleanup checking if (sfd != -1).

Changes

Cohort / File(s) Summary
Socket setup and cleanup refactor
fetch_posix.c
Extracted address_info->ai_addr and address_info->ai_addrlen into local addr/addrlen; create socket with socket(addr->sa_family, SOCK_STREAM, IPPROTO_TCP) and call connect(sfd, addr, addrlen); initialize sfd = -1 and change close guard to if (sfd != -1).

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Suggested labels

enhancement

Suggested reviewers

  • nmoinvaz

Poem

🐰 I hopped through pointers, quick and bright,
I tucked ai_addr in snug and tight.
I opened sockets, then closed with care—
sfd set -1, all tidy there! 🥕

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the primary change: refactoring fetch_posix.c to prepare for platforms without getaddrinfo() by reducing conditional compilation blocks.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch sergio-nsk/fetch/1

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Mar 31, 2026

Codecov Report

❌ Patch coverage is 91.66667% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 63.84%. Comparing base (df114f1) to head (2940b23).
⚠️ Report is 8 commits behind head on master.

Files with missing lines Patch % Lines
fetch_posix.c 91.66% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #131      +/-   ##
==========================================
+ Coverage   58.57%   63.84%   +5.27%     
==========================================
  Files          32       34       +2     
  Lines        2607     2918     +311     
  Branches      526      546      +20     
==========================================
+ Hits         1527     1863     +336     
+ Misses        745      709      -36     
- Partials      335      346      +11     
Flag Coverage Δ
macos 60.93% <91.66%> (+5.85%) ⬆️
macos_duktape 66.08% <91.66%> (+6.12%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@sergio-nsk sergio-nsk changed the title Sergio nsk/fetch/1 improve: prepare fetch_posix.c for platforms that miss getaddrinfo() Mar 31, 2026
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves portability of the POSIX networking code by adding a CMake-detected HAVE_NETINET_IN_H config define and including <netinet/in.h> where needed, and it refactors fetch_get() to use a cached sockaddr/length when creating and connecting the socket.

Changes:

  • Add HAVE_NETINET_IN_H detection in CMake and expose it via proxyres_config.h.
  • Include <netinet/in.h> conditionally in POSIX networking sources that rely on networking constants/types.
  • Refactor fetch_get() socket/connect argument handling.

Reviewed changes

Copilot reviewed 1 out of 1 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
wpad_dhcp_posix.c Conditionally includes <netinet/in.h> based on new config define.
proxyres_config.h.in Adds HAVE_NETINET_IN_H to the generated config header template.
net_util.c Conditionally includes <netinet/in.h> for improved portability.
fetch_posix.c Refactors address/length handling for socket()/connect().
CMakeLists.txt Adds check_include_file(netinet/in.h ...) for non-Windows builds.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread fetch_posix.c Outdated
Comment thread fetch_posix.c Outdated

// Create communication socket
sfd = socket(address_info->ai_family, address_info->ai_socktype, address_info->ai_protocol);
sfd = socket(addr->sa_family, SOCK_STREAM, IPPROTO_TCP);
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The socket is created with hard-coded SOCK_STREAM/IPPROTO_TCP instead of using address_info->ai_socktype / address_info->ai_protocol from getaddrinfo(). This can reduce portability and risks mismatches if the resolver returns an entry with different parameters; prefer using the values returned by getaddrinfo() (or hints) when calling socket().

Suggested change
sfd = socket(addr->sa_family, SOCK_STREAM, IPPROTO_TCP);
sfd = socket(address_info->ai_family, address_info->ai_socktype, address_info->ai_protocol);

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hint is initialized with these constants, so it's ok.

    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hint is initialized with these constants, so it's ok.

True, but I think the existing code is more semantically correct. The new code duplicates assumptions already encoded in the getaddrinfo() hints, so it’s a bit less idiomatic and slightly more brittle if those hints ever change.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The target platform SDK does not have getaddrinfo and struct addrinfo.

Comment thread fetch_posix.c Outdated
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
fetch_posix.c (1)

40-41: Use an API-matched type for addrlen.

Line 41 copies ai_addrlen into uint32_t, but that value is defined by the socket API and then fed straight into connect(). Since this refactor is about portability, I’d keep it behind a small platform alias (socklen_t on POSIX, int on Winsock) instead of baking in a 32-bit type.

♻️ Possible follow-up

Near the platform shims:

 `#ifdef` _WIN32
 #  define socketerr WSAGetLastError()
 #  define ssize_t   int
+typedef int socket_addrlen_t;
 `#else`
 #  define socketerr   errno
 #  define SOCKET      int
 #  define closesocket close
+typedef socklen_t socket_addrlen_t;
 `#endif`

At the declaration site:

-    uint32_t addrlen = 0;
+    socket_addrlen_t addrlen = 0;
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@fetch_posix.c` around lines 40 - 41, The variable addrlen should use the
socket-API-matching type rather than uint32_t: replace the declaration of
addrlen (and any copies from ai_addrlen) with the platform alias (socklen_t on
POSIX, int on Winsock) so the value passed to connect() matches the OS API;
update the declaration near "struct sockaddr *addr" and any assignments from
ai_addrlen to use that alias (or socklen_t) and adjust any casts or calls (e.g.,
connect()) accordingly to remove the incorrect uint32_t usage.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@fetch_posix.c`:
- Around line 40-41: The variable addrlen should use the socket-API-matching
type rather than uint32_t: replace the declaration of addrlen (and any copies
from ai_addrlen) with the platform alias (socklen_t on POSIX, int on Winsock) so
the value passed to connect() matches the OS API; update the declaration near
"struct sockaddr *addr" and any assignments from ai_addrlen to use that alias
(or socklen_t) and adjust any casts or calls (e.g., connect()) accordingly to
remove the incorrect uint32_t usage.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ffc6ebfc-ac63-4966-9f13-2ef873603129

📥 Commits

Reviewing files that changed from the base of the PR and between 4454fb3 and d298a89.

📒 Files selected for processing (1)
  • fetch_posix.c

@sergio-nsk sergio-nsk changed the title improve: prepare fetch_posix.c for platforms that miss getaddrinfo() improve: prepare fetch_posix.c for platforms that miss getaddrinfo() Mar 31, 2026
@sergio-nsk
Copy link
Copy Markdown
Collaborator Author

Maybe I should make constants.

@sergio-nsk sergio-nsk force-pushed the sergio-nsk/fetch/1 branch from 7cc601b to 2940b23 Compare April 1, 2026 01:15
@sergio-nsk sergio-nsk merged commit 85d8713 into master Apr 1, 2026
16 checks passed
@sergio-nsk sergio-nsk deleted the sergio-nsk/fetch/1 branch April 1, 2026 01:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cleanup Housekeeping tasks

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants