2 .\" Copyright (c) 1983, Regents of the University of California. All rights reserved. The Berkeley software License Agreement specifies the terms and conditions for redistribution. Copyright 1989 AT&T. Copyright (c) 2007, Sun Microsystems, Inc.
3 .\" All Rights Reserved.
4 .TH GETSERVBYNAME 3SOCKET "Jan 31, 2007"
6 getservbyname, getservbyname_r, getservbyport, getservbyport_r, getservent,
7 getservent_r, setservent, endservent \- get service entry
11 \fBcc\fR [ \fIflag\fR ... ] \fIfile\fR ... \fB-lsocket\fR \fB -lnsl \fR [ \fIlibrary\fR ... ]
14 \fBstruct servent *\fR\fBgetservbyname\fR(\fBconst char *\fR\fIname\fR, \fBconst char *\fR\fIproto\fR);
19 \fBstruct servent *\fR\fBgetservbyname_r\fR(\fBconst char *\fR\fIname\fR, \fBconst char *\fR\fIproto\fR,
20 \fBstruct servent *\fR\fIresult\fR, \fBchar *\fR\fIbuffer\fR, \fBint\fR \fIbuflen\fR);
25 \fBstruct servent *\fR\fBgetservbyport\fR(\fBint\fR \fIport\fR, \fBconst char *\fR\fIproto\fR);
30 \fBstruct servent *\fR\fBgetservbyport_r\fR(\fBint\fR \fIport\fR, \fBconst char *\fR\fIproto\fR,
31 \fBstruct servent *\fR\fIresult\fR, \fBchar *\fR\fIbuffer\fR, \fBint\fR \fIbuflen\fR);
36 \fBstruct servent *\fR\fBgetservent\fR(\fBvoid\fR);
41 \fBstruct servent *\fR\fBgetservent_r\fR(\fBstruct servent *\fR\fIresult\fR, \fBchar *\fR\fIbuffer\fR,
42 \fBint\fR \fIbuflen\fR);
47 \fBint\fR \fBsetservent\fR(\fBint\fR \fIstayopen\fR);
52 \fBint\fR \fBendservent\fR(\fBvoid\fR);
58 These functions are used to obtain entries for Internet services. An entry may
59 come from any of the sources for \fBservices\fR specified in the
60 \fB/etc/nsswitch.conf\fR file. See \fBnsswitch.conf\fR(4).
63 The \fBgetservbyname()\fR and \fBgetservbyport()\fR functions sequentially
64 search from the beginning of the file until a matching protocol name or port
65 number is found, or until end-of-file is encountered. If a protocol name is
66 also supplied (non-null), searches must also match the protocol.
69 The \fBgetservbyname()\fR function searches for an entry with the Internet
70 service name specified by the \fIname\fR parameter.
73 The \fBgetservbyport()\fR function searches for an entry with the Internet port
77 All addresses are returned in network order. In order to interpret the
78 addresses, \fBbyteorder\fR(3SOCKET) must be used for byte order conversion. The
79 string \fIproto\fR is used by both \fBgetservbyname()\fR and
80 \fBgetservbyport()\fR to restrict the search to entries with the specified
81 protocol. If \fIproto\fR is \fINULL\fR, entries with any protocol can be
85 The functions \fBsetservent()\fR, \fBgetservent()\fR, and \fBendservent()\fR
86 are used to enumerate entries from the services database.
89 The \fBsetservent()\fR function sets (or resets) the enumeration to the
90 beginning of the set of service entries. This function should be called before
91 the first call to \fBgetservent()\fR. Calls to the functions
92 \fBgetservbyname()\fR and \fBgetservbyport()\fR leave the enumeration position
93 in an indeterminate state. If the \fIstayopen\fR flag is non-zero, the system
94 may keep allocated resources such as open file descriptors until a subsequent
95 call to \fBendservent()\fR.
98 The \fBgetservent()\fR function reads the next line of the file, opening the
99 file if necessary. \fBgetservent()\fR opens and rewinds the file. If the
100 \fIstayopen\fR flag is non-zero, the net data base will not be closed after
101 each call to \fBgetservent()\fR (either directly, or indirectly through one of
102 the other "getserv"calls).
105 Successive calls to \fBgetservent()\fR return either successive entries or
106 \fINULL\fR, indicating the end of the enumeration.
109 The \fBendservent()\fR function closes the file. The \fBendservent()\fR
110 function can be called to indicate that the caller expects to do no further
111 service entry retrieval operations; the system can then deallocate resources it
112 was using. It is still allowed, but possibly less efficient, for the process
113 to call more service entry retrieval functions after calling
115 .SS "Reentrant Interfaces"
118 The functions \fBgetservbyname()\fR, \fBgetservbyport()\fR, and
119 \fBgetservent()\fR use static storage that is re-used in each call, making
120 these functions unsafe for use in multithreaded applications.
123 The functions \fBgetservbyname_r()\fR, \fBgetservbyport_r()\fR, and
124 \fBgetservent_r()\fR provide reentrant interfaces for these operations.
127 Each reentrant interface performs the same operation as its non-reentrant
128 counterpart, named by removing the "\fB_r\fR" suffix. The reentrant
129 interfaces, however, use buffers supplied by the caller to store returned
130 results, and are safe for use in both single-threaded and multithreaded
134 Each reentrant interface takes the same parameters as its non-reentrant
135 counterpart, as well as the following additional parameters. The parameter
136 \fIresult\fR must be a pointer to a \fBstruct servent\fR structure allocated by
137 the caller. On successful completion, the function returns the service entry
138 in this structure. The parameter \fIbuffer\fR must be a pointer to a buffer
139 supplied by the caller. This buffer is used as storage space for the service
140 entry data. All of the pointers within the returned \fBstruct servent\fR
141 \fIresult\fR point to data stored within this buffer. See the RETURN VALUES
142 section of this manual page. The buffer must be large enough to hold all of the
143 data associated with the service entry. The parameter \fIbuflen\fR should give
144 the size in bytes of the buffer indicated by \fIbuffer\fR.
147 For enumeration in multithreaded applications, the position within the
148 enumeration is a process-wide property shared by all threads. The
149 \fBsetservent()\fR function can be used in a multithreaded application but
150 resets the enumeration position for all threads. If multiple threads
151 interleave calls to \fBgetservent_r()\fR, the threads will enumerate disjoint
152 subsets of the service database.
155 Like their non-reentrant counterparts, \fBgetservbyname_r()\fR and
156 \fBgetservbyport_r()\fR leave the enumeration position in an indeterminate
161 Service entries are represented by the \fBstruct servent\fR structure defined
167 char *s_name; /* official name of service */
168 char **s_aliases; /* alias list */
169 int s_port; /* port service resides at */
170 char *s_proto; /* protocol to use */
177 The members of this structure are:
184 The official name of the service.
190 \fB\fBs_aliases\fR\fR
193 A zero terminated list of alternate names for the service.
202 The port number at which the service resides. Port numbers are
203 returned in network byte order.
212 The name of the protocol to use when contacting the service
217 The functions \fBgetservbyname()\fR, \fBgetservbyname_r()\fR,
218 \fBgetservbyport()\fR, and \fBgetservbyport_r()\fR each return a pointer to a
219 \fBstruct servent\fR if they successfully locate the requested entry; otherwise
220 they return \fINULL\fR.
223 The functions \fBgetservent()\fR and \fBgetservent_r()\fR each return a pointer
224 to a \fBstruct servent\fR if they successfully enumerate an entry; otherwise
225 they return \fINULL,\fR indicating the end of the enumeration.
228 The functions \fBgetservbyname()\fR, \fBgetservbyport()\fR, and
229 \fBgetservent()\fR use static storage, so returned data must be copied before a
230 subsequent call to any of these functions if the data is to be saved.
233 When the pointer returned by the reentrant functions \fBgetservbyname_r()\fR,
234 \fBgetservbyport_r()\fR, and \fBgetservent_r()\fR is non-null, it is always
235 equal to the \fIresult\fR pointer that was supplied by the caller.
239 The reentrant functions \fBgetservbyname_r()\fR, \fBgetservbyport_r()\fR, and
240 \fBgetservent_r()\fR return \fINULL\fR and set \fBerrno\fR to \fBERANGE\fR if
241 the length of the buffer supplied by caller is not large enough to store the
242 result. See \fBIntro\fR(2) for the proper usage and interpretation of
243 \fBerrno\fR in multithreaded applications.
248 \fB\fB/etc/services\fR\fR
251 Internet network services
257 \fB\fB/etc/netconfig\fR\fR
260 network configuration file
266 \fB\fB/etc/nsswitch.conf\fR\fR
269 configuration file for the name-service switch
275 See \fBattributes\fR(5) for descriptions of the following attributes:
283 ATTRIBUTE TYPE ATTRIBUTE VALUE
286 See "Reentrant Interfaces" in \fBDESCRIPTION\fR.
293 \fBIntro\fR(2), \fBIntro\fR(3), \fBbyteorder\fR(3SOCKET), \fBnetdir\fR(3NSL),
294 \fBnetconfig\fR(4), \fBnsswitch.conf\fR(4), \fBservices\fR(4),
295 \fBattributes\fR(5), \fBnetdb.h\fR(3HEAD)
299 The reentrant interfaces \fBgetservbyname_r()\fR, \fBgetservbyport_r()\fR, and
300 \fBgetservent_r()\fR are included in this release on an uncommitted basis only,
301 and are subject to change or removal in future minor releases.
305 The functions that return \fBstruct servent\fR return the least significant
306 16-bits of the \fIs_port\fR field in \fInetwork byte order\fR.
307 \fBgetservbyport()\fR and \fBgetservbyport_r()\fR also expect the input
308 parameter \fIport\fR in the \fInetwork byte order\fR. See \fBhtons\fR(3SOCKET)
309 for more details on converting between host and network byte orders.
312 To ensure that they all return consistent results, \fBgetservbyname()\fR,
313 \fBgetservbyname_r()\fR, and \fBnetdir_getbyname()\fR are implemented in terms
314 of the same internal library function. This function obtains the system-wide
315 source lookup policy based on the \fBinet\fR family entries in
316 \fBnetconfig\fR(4) and the \fBservices:\fR entry in \fBnsswitch.conf\fR(4).
317 Similarly, \fBgetservbyport()\fR, \fBgetservbyport_r()\fR, and
318 \fBnetdir_getbyaddr()\fR are implemented in terms of the same internal library
319 function. If the \fBinet\fR family entries in \fBnetconfig\fR(4) have a ``-''
320 in the last column for nametoaddr libraries, then the entry for \fBservices\fR
321 in \fBnsswitch.conf\fR will be used; otherwise the nametoaddr libraries in that
322 column will be used, and \fBnsswitch.conf\fR will not be consulted.
325 There is no analogue of \fBgetservent()\fR and \fBgetservent_r()\fR in the
326 netdir functions, so these enumeration functions go straight to the
327 \fBservices\fR entry in \fBnsswitch.conf\fR. Thus enumeration may return
328 results from a different source than that used by \fBgetservbyname()\fR,
329 \fBgetservbyname_r()\fR, \fBgetservbyport()\fR, and \fBgetservbyport_r()\fR.
332 When compiling multithreaded applications, see \fBIntro\fR(3), \fINotes On
333 Multithread Applications\fR, for information about the use of the
334 \fB_REENTRANT\fR flag.
337 Use of the enumeration interfaces \fBgetservent()\fR and \fBgetservent_r()\fR
338 is discouraged; enumeration may not be supported for all database sources. The
339 semantics of enumeration are discussed further in \fBnsswitch.conf\fR(4).