Skip to content

cpnet: use gethostbyname_r() only when its declaration is known - #74

Closed
duncanmcqueen wants to merge 1 commit into
ingelabs:masterfrom
duncanmcqueen:fix-gethostbyname-r
Closed

duncanmcqueen wants to merge 1 commit into
ingelabs:masterfrom
duncanmcqueen:fix-gethostbyname-r

Conversation

@duncanmcqueen

@duncanmcqueen duncanmcqueen commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Split from #60.

Problem and fix

configure checks if gethostbyname_r() exists (HAVE_GETHOSTBYNAME_R), and then how many arguments it takes (HAVE_FUNC_GETHOSTBYNAME_R_6, _5 or _3). The second check reads the declaration in <netdb.h>.

On Haiku, libnetwork has gethostbyname_r(), but no header declares it. Thus the first check finds the function and the second check fails. Then cpnet.c stops with #error unknown number of arguments for gethostbyname_r.

Now cpnet.c uses gethostbyname_r() only if configure found the number of arguments. Otherwise it uses gethostbyname(), as it does when the function does not exist.

The first check finds the function only when libnetwork is in LIBS, which #73 does. Thus the error occurs on Haiku after #73, not with master alone. Please merge this pull request before #73 or together with it.

How to reproduce

On Haiku, with #73:

../configure --enable-jni ...
grep -n gethostbyname_r config.log
make

Results

Haiku R1/beta6 x86_64 (hrev59866), javac from OpenJDK 8, Kaffe 1.1.10-pre. I ran autoreconf on Linux (autoconf 2.73, automake 1.19) and built on Haiku out of tree with --enable-jni --disable-examples --disable-plugin --disable-gjdoc --disable-gtk-peer --disable-gconf-peer --disable-alsa --disable-dssi.

Before (master with #73):
checking for gethostbyname_r... yes
checking how many arguments gethostbyname_r() takes... cannot find function declaration in netdb.h
../../../../src/native/jni/native-lib/cpnet.c:656:4: error: #error unknown number of arguments for gethostbyname_r
make: *** [Makefile:617: cpnet.lo] Error 1

After (master with this pull request and #73):
cpnet.c compiles, and host name lookups work (see the NetBasics test in #73).

Platforms

Not specific to Haiku: it occurs on each system that has gethostbyname_r() in its libraries but does not declare it. Haiku is the system that I know. On Linux, configure gives checking for library containing inet_pton... none required and checking how many arguments gethostbyname_r() takes... six, so the result does not change on Linux.

AI assistance

An AI model (Claude Code, by Anthropic) wrote this change, the test and this text. The repository owner directed the work and checked the results. Please review it as you review other contributions.

🤖 Generated with Claude Code

On Haiku, gethostbyname_r() is in libnetwork but no header declares
it, so configure finds the function but not the number of arguments,
and cpnet.c stops with #error. Use gethostbyname() in that case.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: duncanmcqueen <dwmcqueen@gmail.com>
@guillerodriguez

Copy link
Copy Markdown
Contributor

Hi @duncanmcqueen ,

If possible, when a PR falls behind master, prefer rebasing it rather than merging master into it, this makes it easier to keep a linear history :-)

@guillerodriguez

Copy link
Copy Markdown
Contributor

I am looking into this but I am not sure this is the right fix, for two reasons:

  1. On Haiku: gethostbyname() is a thin wrapper around gethostbyname_r() that uses one shared static buffer:

    static struct hostent h_ent;
    static char h_buf[16384];
    
    struct hostent *
    gethostbyname(const char *name) {
    	return gethostbyname_r(name, &h_ent, h_buf, sizeof(h_buf), &h_errno);
    }

    This is not thread-safe: two Java threads resolving names at the same time can overwrite each other’s results. (Note that this isn’t true everywhere: on macOS, for example, gethostbyname() uses thread-local storage.)

  2. On other systems, it would hide a real problem. If configure finds gethostbyname_r() but can’t work out its signature, we would rather the build stop with an error, so that we can look at the platform and handle it properly. With this change, it would silently switch to gethostbyname() without any notice, and that may not be thread-safe on that system.

We're looking into possible alternatives; will come back to you.

@guillerodriguez

Copy link
Copy Markdown
Contributor

Please have a look at #76. This switches to getnameinfo() / getaddrinfo() for name and address resolution. These functions are POSIX compliants (POSIX.1-2001), thread-safe and widely available, and should be well supported in Haiku if I am correct.

@duncanmcqueen

Copy link
Copy Markdown
Contributor Author

I close this pull request, because #76 replaces it. #76 uses getaddrinfo() and getnameinfo(), which are thread-safe on Haiku. The test results are in #76.

@guillerodriguez

Copy link
Copy Markdown
Contributor

Thank you. #76 is now merged.

@duncanmcqueen
duncanmcqueen deleted the fix-gethostbyname-r branch October 19, 2026 11:26
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.

2 participants