Page MenuHomeFreeBSD

arp: remove gethostby*() calls
Needs ReviewPublic

Authored by yanhaowang on Jun 14 2024, 11:26 AM.
Tags
None
Referenced Files
Unknown Object (File)
Thu, Dec 26, 2:55 PM
Unknown Object (File)
Tue, Dec 24, 10:30 PM
Unknown Object (File)
Tue, Dec 10, 7:50 PM
Unknown Object (File)
Thu, Dec 5, 11:36 AM
Unknown Object (File)
Fri, Nov 29, 10:02 PM
Unknown Object (File)
Nov 23 2024, 12:52 PM
Unknown Object (File)
Nov 20 2024, 8:24 PM
Unknown Object (File)
Nov 20 2024, 3:11 AM
Subscribers

Details

Reviewers
hrs
lwhsu
Summary
  1. Use getaddrinfo to replace gethostbyname.
  2. Use getnameinfo with NI_NAMEREQD flag to replace gethostbyaddr.
Test Plan

print_entry function in "arp.c" will be call when WITHOUT_NETLINK is define and print_entry function in "arp_netlink.c" will be call in opsite. I run arp -a in both case on revise and not revised code. it output same like below.

// Original
$ arp -a  // WITHOUT_NETLINK is defined
? (192.168.42.2) at 00:50:56:ef:d2:41 on le0 expires in 1020 seconds [ethernet]
? (192.168.42.128) at 00:0c:29:02:ba:a1 on le0 permanent [ethernet]

$ arp -a // WITHOUT_NETLINK is not defined
? (192.168.42.2) at 00:50:56:ef:d2:41 on le0 expires in 961 seconds [ethernet]
? (192.168.42.128) at 00:0c:29:02:ba:a1 on le0 permanent [ethernet]

// Revised
$ arp -a // WITHOUT_NETLINK is defined
? (192.168.42.2) at 00:50:56:ef:d2:41 on le0 expires in 1093 seconds [ethernet]
? (192.168.42.128) at 00:0c:29:02:ba:a1 on le0 permanent [ethernet]

$ arp -a // WITHOUT_NETLINK is not defined
? (192.168.42.2) at 00:50:56:ef:d2:41 on le0 expires in 1175 seconds [ethernet]
? (192.168.42.128) at 00:0c:29:02:ba:a1 on le0 permanent [ethernet]

As for the getaddr function I use sudo arp -d kola (kola is a hostname of my private IP, for testing the getaddrinfo function work) and sudo arp -d -a to test and it work fine (successfully delete the entry).

Diff Detail

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

Event Timeline

usr.sbin/arp/arp.c
262–265

Should I use memset to replace the bzero function?

This comment was removed by yanhaowang.
yanhaowang retitled this revision from arp: remove "gethostby*" function to arp: remove gethostby*() calls.Jun 16 2024, 3:03 AM
yanhaowang edited the summary of this revision. (Show Details)

Revise error code

We should use EAI_AGAIN instead of TRY_AGAIN error code while using getnameinfo.

yanhaowang marked an inline comment as not done.Jun 25 2024, 1:05 AM
hrs requested changes to this revision.Jun 28 2024, 12:38 AM

The change basically looks good to me, but I added some comments.

usr.sbin/arp/arp.c
263
hints = (struct addrinfo){
    .ai_family = AF_INET
};

is enough for memset() + assignments.

611

sizeof() should be replaced with addr->sin_len.

I would recommend to change the type of addr to struct sockaddr, and define struct sockaddr_in *sin = (struct sockaddr_in *)addr at the top of this function to use it as sockaddr_in. This requires additional changes in search() and action_fn, though.

usr.sbin/arp/arp_netlink.c
188

The same as arp.c

This revision now requires changes to proceed.Jun 28 2024, 12:38 AM

Revise according Hiroki advise

  1. Use sin_len member.
  2. Change function prototype from sockaddr_in to sockaddr. In this case we change action_fn, print_entry and nuke_entry function.
  3. Use struct initializer to replace memset and assignments.
usr.sbin/arp/arp.c
265–270

Removing "hints" and use of compound literals will give a more compact idiom like this:

error = getaddrinfo(host, NULL, &(struct addrinfo){
    .ai_family = AF_INET
}, &res);

I would recommend to use "error" instead of "status" as the variable name because the conditional can be written like the following. I believe it is more intuitive:

if (error) {
 ...
}
266

Check whether the return value is EAI_SYSTEM or not. If it is EAI_SYSTEM, the error code must be extracted from errno, not the return value.

if (status != 0) {
    xo_warnx("%s: %s", host, (status == EAI_SYSTEM)
        ? strerror(errno)  : gai_strerror(status));
    return (NULL);
}
612

Use sizeof(hostbuf) instead of NI_MAXHOST.

usr.sbin/arp/arp_netlink.c
189

Same as L.613 in arp.c.

Update according Hiroki advise

  1. Use "error" instead of "status" name to store the value of getnameinfo() function.
  2. Use compound literals instead of "hints" variable.
  3. Check EAI_SYSTEM while using gai_strerror() function.
  4. Use sizoef(hostbuf) instead of NI_MAXHOST.
yanhaowang edited the test plan for this revision. (Show Details)

Fix getaddr function