Page MenuHomeFreeBSD

getent: remove gethostby*() calls
Needs ReviewPublic

Authored by yanhaowang on Jul 11 2024, 7:05 AM.
Tags
None
Referenced Files
F109032384: D45949.diff
Thu, Jan 30, 9:41 PM
Unknown Object (File)
Sat, Jan 18, 8:07 PM
Unknown Object (File)
Sat, Jan 18, 3:12 AM
Unknown Object (File)
Dec 29 2024, 5:59 PM
Unknown Object (File)
Dec 26 2024, 11:55 AM
Unknown Object (File)
Dec 25 2024, 8:39 PM
Unknown Object (File)
Dec 25 2024, 2:56 AM
Unknown Object (File)
Dec 18 2024, 10:57 PM
Subscribers

Details

Reviewers
hrs
lwhsu
Summary
  1. Use "getaddrinfo" to replace the "gethostbyaddr" and "gethostbyname" functions.
  2. Add "addrprint" function to print "struct addrinfo".
Test Plan

Currently only live test. The below is my test result and there are some difference.

// Original
$ getent hosts 127.0.0.1
127.0.0.1         localhost localhost.my.domain

$ getent hosts ::1
::1               localhost localhost.my.domain

$ getent hosts 12321
0.0.48.33         12321

$ getent hosts 18472318947123894781239034534253425423
169.13.243.15     18472318947123894781239034534253425423

$ getent hosts www.google.com
2404:6800:4012:4::2004  www.google.com
142.251.42.228    www.google.com

$ getent hosts 142.251.43.4
142.251.43.4      tsa03s08-in-f4.1e100.net tsa03s08-in-f4.1e100.net
// Revised
$ getent hosts 127.0.0.1
127.0.0.1         localhost

$ getent hosts ::1
::1               localhost

$ getent hosts 12321
0.0.48.33         12321

$ getent hosts 18472318947123894781239034534253425423
169.13.243.15     18472318947123894781239034534253425423

$ getent hosts www.google.com
172.217.163.36    www.google.com
2404:6800:4012:3::2004  www.google.com

$ getent hosts 142.251.43.4
142.251.43.4      tsa03s08-in-f4.1e100.net

The revised code shows the output of getent hosts for various IP addresses and domain names, highlighting differences compared to the original output. Notably, the revised code does not include the alias "localhost.my.domain," which was present in the original output for localhost. It is because struct addrinfo don't have alias. And the order of IPv4 and IPv6 address is a revers.

There are other difference is like below.

//Orgin
$ getent hosts www.yahoo.com
2406:2000:a0:807::2  www.yahoo.com
180.222.106.12    www.yahoo.com

// Revised
kola@freebsd:/usr/src/usr.bin/getent $ getent hosts www.yahoo.com
180.222.106.12    www.yahoo.com
180.222.109.252   www.yahoo.com
180.222.106.11    www.yahoo.com
180.222.109.251   www.yahoo.com
2406:2000:a0:807::1  www.yahoo.com
2406:2000:9c:800::12  www.yahoo.com
2406:2000:a0:807::2  www.yahoo.com
2406:2000:9c:800::11  www.yahoo.com

It seem getaddrinfo will return more IP than the orgin gethostby* functions. I am not sure if this is correct?

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Passed
Unit
No Test Coverage
Build Status
Buildable 58866
Build 55753: arc lint + arc unit

Event Timeline

usr.bin/getent/Makefile
4 ↗(On Diff #140793)

Add "WARNS?= 3" in Makefile for avoid the strict aliasing rules error while convert "sockaddr *" to "sockaddr_in". But I am not sure the warning level is suitable or not. I look "arp" makefile and copy it.

usr.bin/getent/getent.c
284
  1. addrprint is similar with hostsprint function but used for addrinfo. But it seem because IPv6 don't use alias so addrinfo don't have alias member. So I leave it empty, not sure it is suitable or not.
usr.bin/getent/Makefile
4 ↗(On Diff #140793)

In general it's not recommended to decrease the warning level. Let's see if there is any possible way to avoid it. (But if there is no better choice it's also acceptable.)

yanhaowang edited the test plan for this revision. (Show Details)