getservbyname(3N)
NAME
getservbyname, getservbyport, getservent, setservent, endservent − get service entry
SYNOPSIS
cc [ flag ... ] file ... −lsocket -lnsl [ library ... ]
#include <netdb.h>
struct servent ∗getservbyname(const char ∗name, char ∗proto);
struct servent ∗getservbyport(int port, char ∗proto);
struct servent ∗getservent(void);
int setservent(int stayopen);
int endservent(void);
MT-LEVEL
Unsafe
DESCRIPTION
getservent(), getservbyname(), and getservbyport() each return a service entry.
The entry may come from one of the following sources: the services file (see services(4)), the NIS map “services.byname”, and the NIS+ table “services”.
getservbyname() searches for a service entry with a given service name.
getservbyport() searches for a service entry with a given port number and, if the protocol name is non-NULL, the protocol.
getservent() enumerates service entries: successive calls to getservent() will return either successive service entries or NULL. Enumeration may not be supported by some sources.
setservent() “rewinds” to the beginning of the enumeration of service entries. If the stayopen flag is non-zero, resources such as open file descriptors are not deallocated after each call to getservbyport() and getservbyname(). Calls to getservbyname() and getservbyport() may leave the enumeration in an indeterminate state, so setservent() should be called before the first getservent().
endservent() may be called to indicate that service processing is complete; the system may then close any open services file, deallocate storage, and so forth. It is legitimate, but possibly less efficient, to call more service routines after endservent().
The internal representation of a service entry is a servent structure defined in <netdb.h> with the following members:
char∗s_name;
char∗∗s_aliases;
ints_port;
char∗s_proto;
RETURN VALUES
getservbyname() and getservbyport() return a pointer to a struct servent if they successfully locate the requested entry; otherwise they return NULL.
getservent() returns a pointer to a struct servent if it successfully enumerates an entry; otherwise it returns NULL, indicating the end of the enumeration.
FILES
/etc/services
/etc/netconfig
/etc/nsswitch.conf
/usr/lib/switch.so
SEE ALSO
netdir(3N), netconfig(4), nsswitch.conf(4), services(4)
NOTES
All information is contained in a static area, so it must be copied if it is to be saved.
getservbyname() performs much the same function as netdir_getbyname(). To ensure that the two produce consistent results, getservbyname() is implemented in terms of netdir_getbyname(). Their behavior is controlled by the netconfig(4) entries for the “inet” family of transports. If these entries include “switch.so”, then the entry for “services” in nsswitch.conf will also be used; otherwise nsswitch.conf will not be consulted.
Similarly, getservbyport() performs much the same function as netdir_getbyaddr() and is implemented in terms of it.
There is no analogue of getservent() in the netdir routines, so getservent() goes straight to the “services” entry in nsswitch.conf. It may thus produce results that are inconsistent with the results of getservbyname() and getservbyport().
Even without this inconsistency, the use of getservent() is deprecated; enumeration is well-defined for the services file and is supported (albeit inefficiently) for NIS and NIS+, but in general may not be well-defined. The semantics of enumeration are discussed in nsswitch.conf(4).
These interfaces are unsafe in multithreaded applications. Unsafe interfaces should be called only from the main thread.
BUGS
Programs that call getservbyname() or getservbyport() routines cannot be linked statically since the implementation of these routines requires dynamic linker functionality to access shared objects at run time.
SunOS 5.2 — Last change: 22 Jan 1993