2

A DNS server resolves a hostname to an IP address. A program can resolve a hostname to an IP address by calling getaddrinfo() which in turn asks a DNS server to do the resolution, if I am correct.

In SysV init, is there some daemon which resolves a service name to a port, just like a DNS server? Does it do that by reading /etc/services? Does getaddrinfo() also invoke the daemon to perform the resolution?

I know that inetd reads /etc/services to decide which sockets to listen to. But inetd doesn't seem to resolve service name to port, does it?

Thanks.

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
Tim
  • 98,580
  • 191
  • 570
  • 977

1 Answers1

4

Note that getaddrinfo() only calls DNS if nsswitch.conf defines it. The files entry reads, directly, from /etc/hosts.

Basically, the "name service switch" functions (NSS) look at /etc/nsswitch.conf and then may dynamically load "libnss" routines. So, for example, you might have /lib/x86_64-linux-gnu/libnss_files.so.2, which is the library loaded when files is mentioned in nsswitch.conf.

For hosts, the nsswitch.conf line may read something like

hosts: files dns

This will tell the name service resolver to load the "nss_files" library (which will look in /etc/hosts) and if that fails, load the "nss_dns" library. It's that library that calls out to a DNS server.

For services, the nsswitch.conf line may read something like

services:       files ldap

This will load the "nss_files" library (which will look in /etc/services), and if that fails then load the "nss_ldap" library.

There are various different backends (files, db, ldap, nis, compat, dns...) and they determine how names are resolved.

For an inet entry such as

service time
{
...
}

the time value is looked up in the NSS map for services. If files is used in nsswitch.conf then it will see a line

time        37/tcp      timserver

which tells inetd to bind to TCP port 37.

Stephen Harris
  • 42,369
  • 5
  • 94
  • 123