Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ setservent(3N) — SunOS 5.3

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

intro(2)

netdir(3N)

netconfig(4)

nsswitch.conf(4)

services(4)

getservbyname(3N)

NAME

getservbyname, getservbyname_r, getservbyport, getservbyport_r, getservent, getservent_r, 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 ∗getservbyname_r(const char ∗name, char ∗proto, struct servent ∗result,

char ∗buffer, intbuflen);

struct servent ∗getservbyport(int port, char ∗proto);

struct servent ∗getservbyport_r(int port, char ∗proto, struct servent ∗result,

char ∗buffer, int buflen);

struct servent ∗getservent(void);

struct servent ∗getservent_r(struct servent ∗result, char ∗buffer int buflen);

int setservent(int stayopen);

int endservent(void);

MT-LEVEL

See the subsection “Reentrant Interfaces” in the DESCRIPTION section of this page. 

DESCRIPTION

 

These functions are used to obtain entries for Internet services.  An entry may come from any of the sources for services specified in the /etc/nsswitch.conf file (see nsswitch.conf(4)). 

getservbyname() searches for an entry with the Internet service name specified by the parameter name.

getservbyport() searches for an entry with the Internet port number port and the protocol name given by the string proto. If the pointer proto is NULL, the search is for any entry with the given port number. 

The functions setservent(), getservent(), and endservent() are used to enumerate entries from the services database. 

setservent() sets (or resets) the enumeration to the beginning of the set of service entries.  This function should be called before the first call to getservent().  Calls to the functions getservbyname() and getservbyport() leave the enumeration position in an indeterminate state.  If the stayopen flag is non-zero, the system may keep allocated resources such as open file descriptors until a subsequent call to endservent(). 

Successive calls to getservent() return either successive entries or NULL, indicating the end of the enumeration. 

endservent() may be called to indicate that the caller expects to do no further service entry retrieval operations; the system may then deallocate resources it was using.  It is still allowed, but possibly less efficient, for the process to call more service entry retrieval functions after calling endservent(). 

Reentrant Interfaces

The functions getservbyname(), getservbyport(), and getservent() use static storage that is re-used in each call, making these routines unsafe for use in multithreaded applications. 

The functions:

getservbyname_r(),
getservbyport_r(),

and

getservent_r()

provide reentrant interfaces for these operations. 

Each reentrant interface performs the same operation as its non-reentrant counterpart, named by removing the “_r” suffix.  The reentrant interfaces, however, use buffers supplied by the caller to store returned results, and are safe for use in both single-threaded and multithreaded applications. 

Each reentrant interface takes the same parameters as its non-reentrant counterpart, as well as the following additional parameters.  The parameter result must be a pointer to a struct servent structure allocated by the caller.  On successful completion, the function returns the service entry in this structure.  The parameter buffer must be a pointer to a buffer supplied by the caller.  This buffer is used as storage space for the service entry data.  All of the pointers within the returned struct servent result point to data stored within this buffer (see RETURN VALUES).  The buffer must be large enough to hold all of the data associated with the service entry. The parameter buflen should give the size in bytes of the buffer indicated by buffer.

For enumeration in multithreaded applications, the position within the enumeration is a process-wide property shared by all threads.  setservent() may be used in a multithreaded application but resets the enumeration position for all threads.  If multiple threads interleave calls to getservent_r(), the threads will enumerate disjoint subsets of the service database. 

Like their non-reentrant counterparts, getservbyname_r() and getservbyport_r() leave the enumeration position in an indeterminate state. 

RETURN VALUES

Service entries are represented by the struct servent structure defined in <netdb.h>:

struct  servent {

     char ∗s_name;
     char ∗s_aliases;
     int s_port;
     char ∗s_proto;

};

The functions getservbyname(), getservbyname_r(), getservbyport(), and getservbyport_r() each return a pointer to a struct servent if they successfully locate the requested entry; otherwise they return NULL. 

The functions getservent() and getservent_r() each return a pointer to a struct servent if they successfully enumerate an entry; otherwise they return NULL, indicating the end of the enumeration. 

The functions getservbyname(), getservbyport(), and getservent() use static storage, so returned data must be copied before a subsequent call to any of these functions if the data is to be saved. 

When the pointer returned by the reentrant functions getservbyname_r(), getservbyport_r(), and getservent_r() is non-NULL, it is always equal to the result pointer that was supplied by the caller. 

ERRORS

The reentrant functions getservbyname_r(), getservbyport_r() and getservent_r() will return NULL and set errno to ERANGE if the length of the buffer supplied by caller is not large enough to store the result.  See intro(2) for the proper usage and interpretation of errno in multithreaded applications. 

FILES

/etc/services

/etc/netconfig

/etc/nsswitch.conf

SEE ALSO

intro(2), netdir(3N), netconfig(4), nsswitch.conf(4), services(4)

WARNINGS

The reentrant interfaces getservbyname_r(), getservbyport_r(), and getservent_r() are included in this release on an uncommitted basis only, and are subject to change or removal in future minor releases. 

NOTES

Programs that use the interfaces described in this manual page cannot be linked statically since the implementations of these functions employ dynamic loading and linking of shared objects at run time. 

When compiling multithreaded applications, see Intro(3), Notes On Multithread Applications, for information about the use of the _REENTRANT flag. 

Use of the enumeration interfaces getservent() and getservent_r() is discouraged; enumeration may not be supported for all database sources.  The semantics of enumeration are discussed further in nsswitch.conf(4). 

getservbyname_r() is implemented in terms of netdir_getbyname() in order to ensure that they return consistent results.  Similarly, getservbyport_r() is implemented in terms of netdir_getbyaddr(). 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. 

There is no analogue of getservent_r() in the netdir routines, so getservent_r() goes straight to the services entry in /etc/nsswitch.conf.  Thus the sources used for enumeration may not coincide exactly with those used by the search functions. 

Sun Microsystems  —  Last change: 29 Mar 1993

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026