ppkarwasz commented on issue #1740:
URL: 
https://github.com/apache/logging-log4j2/issues/1740#issuecomment-5630518029

   Hi @drTr0jan,
   
   Sorry, my earlier comment was ambiguous: by `gethostname()` (all lowercase) 
I meant the [POSIX C 
function](https://man7.org/linux/man-pages/man2/gethostname.2.html), not a Java 
method.
   
   On Linux 
[`InetAddress.getLocalHost()`](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/net/InetAddress.html#getLocalHost())
 does this:
   
   1. `impl.getLocalHostName()` calls `gethostname()`: that is *all* it does, 
so you get the kernel's UTS `nodename`, traditionally unqualified.
   2. That name goes to `lookupAllHostAddr()`, which calls 
[`getaddrinfo()`](https://man7.org/linux/man-pages/man3/getaddrinfo.3.html) 
with the `AI_CANONNAME` hint, so the C library does resolve a canonical name.
   3. The JNI code then stamps the **input** string onto every returned 
address, discarding `res->ai_canonname`:
   
   ```c
   setInetAddress_hostName(env, iaObj, host);
   ```
   
   
([`Inet4AddressImpl.c#L198`](https://github.com/openjdk/jdk/blob/da75f3c4ad5bdf25167a3ed80e51f567ab3dbd01/src/java.base/unix/native/libnet/Inet4AddressImpl.c#L198);
 `Inet6AddressImpl.c` does the same at lines 362 and 384, and that is the 
implementation actually used on a dual-stack host.)
   
   You are right that `InetAddress.getHostName()` performs a reverse 
[`getnameinfo()`](https://man7.org/linux/man-pages/man3/getnameinfo.3.html) 
lookup, but only when the name is not already known:
   
   ```java
   String getHostName(boolean check) {
       if (holder().getHostName() == null) {
           holder().hostName = InetAddress.getHostFromNameService(this, check);
       }
       return holder().getHostName();
   }
   ```
   
   Step 3 already filled that field in, so the lookup never happens. 
`getHostName()` does return an FQDN for addresses obtained other ways 
(`getByAddress()`, or from a socket), just not for this one.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to