Skip to content

fix(network): port bsdsocket from latest Amiberry - #39

Merged
midwan merged 3 commits into
masterfrom
fix/28-bsdsocket-port
Sep 15, 2026
Merged

midwan merged 3 commits into
masterfrom
fix/28-bsdsocket-port

Conversation

@midwan

@midwan midwan commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

Summary

Ports the bsdsocket.library emulation from the latest Amiberry to fix the crash-on-connect reported in #28 (AmiTradeCenter 1.4, OS3.9/Pimiga 5).

Files changed:

  • src/bsdsocket.cpp — identical to current Amiberry
  • src/include/bsdsocket.h — identical to current Amiberry
  • src/osdep/bsdsocket_host.cpp — upstream POSIX section; only local adaptation is SDL2 mutex types (SDL_mutex/<SDL_mutex.h> instead of SDL3)

No SLIRP/PCAP code involved (both remain compiled out).

Key fixes carried over

  • select()/FD_SET guarded against fd >= FD_SETSIZE (host-side crash)
  • post_socket_event() bounds-checks ftable writes; stale SET_* flags cleared on close/unregister
  • Event monitor thread torn down on cleanup/reset — no more dangling socketbase* after host_sbreset() (was an empty stub here)
  • errno preserved across logging in the blocking send/recv/connect loop
  • setsockopt/getsockopt argument-size validation; SO_EVENTMASK query support
  • sendmsg reads tolen from guest memory instead of passing a guest pointer
  • host_sendto checks the looked-up socket, not a stale sb->s
  • WaitSelect clamps nfds to the descriptor table size
  • dup2socket returns the new descriptor; EBADF on invalid fd
  • gethostname, Inet_LnaOf, Inet_NetOf, Inet_MakeAddr implemented correctly
  • Per-call logging gated behind log_bsd (BSDLOG/BSDTRACE) to stop log flooding

Verification

  • Clean build on macOS (Apple Silicon), zero compiler warnings
  • AROS boot with socket_emu=true: ~90s run, no errors, clean exit on SIGTERM
  • Delta vs upstream audited: bsdsocket.cpp/.h byte-identical; host file differs by exactly the 2 SDL2 lines

The literal AmiTradeCenter-on-OS3.9 repro needs the reporter's environment — @RWXecute a retest with a build from this branch would confirm.

Fixes #28

Old host layer crashed Amiga apps on connect (#28, AmiTradeCenter).
Port bsdsocket.cpp/.h and the POSIX half of bsdsocket_host.cpp from
current Amiberry; only local adaptation is SDL2 mutex types.

Key fixes carried over:
- select()/FD_SET guarded against fd >= FD_SETSIZE
- ftable writes bounds-checked; stale SET_* flags cleared on close
- event monitor unregistered on cleanup/reset (no dangling socketbase)
- errno preserved across logging in blocking send/recv/connect paths
- setsockopt/getsockopt arg validation, SO_EVENTMASK query support
- sendmsg reads tolen from guest memory, not guest pointer
- WaitSelect nfds clamped to descriptor table size
- dup2socket returns new fd; EBADF on invalid descriptor
- gethostname/Inet_LnaOf/Inet_NetOf/Inet_MakeAddr implemented
- per-call logging gated behind log_bsd (BSDLOG/BSDTRACE)

Refs #28
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 15, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-09-15T18:52:18.395617Z fe7e476 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@midwan midwan self-assigned this Sep 15, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f254b521a2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/osdep/bsdsocket_host.cpp Outdated
Comment thread src/osdep/bsdsocket_host.cpp Outdated
Comment thread src/osdep/bsdsocket_host.cpp
- bsdthr_blockingstuff: keep EINTR when a blocking call was aborted
  via sockabort; the unconditional saved_errno restore reported
  EAGAIN/EINPROGRESS so callers retried aborted operations
- event monitor: treat unconnected datagram sockets as ready for
  read/write readiness polling (getpeername fails with ENOTCONN on
  bound UDP sockets, which blocked all event delivery)
- event monitor: recv() returning 0 is EOF only on stream sockets;
  on UDP it is a valid zero-length datagram and must not fire
  REP_CLOSE
@midwan

midwan commented Sep 15, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

Marking unconnected datagram sockets connected=true let the monitor's
REP_CONNECT fallback post a false connect-completed event when a broad
mask (e.g. REP_ALL) included REP_CONNECT. dgram now relaxes only the
read/write readiness gates; connected keeps the getpeername() result.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fe7e476233

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/bsdsocket.cpp
@midwan
midwan merged commit 5b19f4b into master Sep 15, 2026
10 checks passed
@midwan
midwan deleted the fix/28-bsdsocket-port branch September 15, 2026 18:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash on connection in AmiTradeCentre

1 participant