xref: /freebsd/lib/libc/net/getservent.3 (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
1.\" Copyright (c) 1983, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.Dd December 7, 2020
29.Dt GETSERVENT 3
30.Os
31.Sh NAME
32.Nm getservent ,
33.Nm getservbyport ,
34.Nm getservbyname ,
35.Nm setservent ,
36.Nm endservent
37.Nd get service entry
38.Sh LIBRARY
39.Lb libc
40.Sh SYNOPSIS
41.In netdb.h
42.Ft struct servent *
43.Fn getservent
44.Ft struct servent *
45.Fn getservbyname "const char *name" "const char *proto"
46.Ft struct servent *
47.Fn getservbyport "int port" "const char *proto"
48.Ft void
49.Fn setservent "int stayopen"
50.Ft void
51.Fn endservent void
52.Sh DESCRIPTION
53The
54.Fn getservent ,
55.Fn getservbyname ,
56and
57.Fn getservbyport
58functions
59each return a pointer to an object with the
60following structure
61containing the broken-out
62fields of a line in the network services data base,
63.Pa /etc/services .
64.Bd -literal -offset indent
65struct servent {
66	char	*s_name;	/* official name of service */
67	char	**s_aliases;	/* alias list */
68	int	s_port;		/* port service resides at */
69	char	*s_proto;	/* protocol to use */
70};
71.Ed
72.Pp
73The members of this structure are:
74.Bl -tag -width s_aliases
75.It Fa s_name
76The official name of the service.
77.It Fa s_aliases
78A zero terminated list of alternate names for the service.
79.It Fa s_port
80The port number at which the service resides.
81Port numbers are returned in network byte order.
82.It Fa s_proto
83The name of the protocol to use when contacting the
84service.
85.El
86.Pp
87The
88.Fn getservent
89function
90reads the next line of the file, opening the file if necessary.
91.Pp
92The
93.Fn setservent
94function
95opens and rewinds the file.
96If the
97.Fa stayopen
98flag is non-zero,
99the net data base will not be closed after each call to
100.Fn getservbyname
101or
102.Fn getservbyport .
103.Pp
104The
105.Fn endservent
106function
107closes the file.
108.Pp
109The
110.Fn getservbyname
111and
112.Fn getservbyport
113functions
114sequentially search from the beginning
115of the file until a matching
116protocol name or
117port number (which must be specified in
118network byte order) is found,
119or until
120.Dv EOF
121is encountered.
122If a protocol name is also supplied (non-
123.Dv NULL ) ,
124searches must also match the protocol.
125.Sh FILES
126.Bl -tag -width /etc/services -compact
127.It Pa /etc/services
128.It Pa /var/db/services.db
129.El
130.Sh DIAGNOSTICS
131Null pointer returned on
132.Dv EOF
133or error.
134.Sh SEE ALSO
135.Xr getprotoent 3 ,
136.Xr services 5 ,
137.Xr services_mkdb 8
138.Sh HISTORY
139The
140.Fn getservent ,
141.Fn getservbyport ,
142.Fn getservbyname ,
143.Fn setservent ,
144and
145.Fn endservent
146functions appeared in
147.Bx 4.2 .
148.Sh BUGS
149These functions use a thread-specific data storage;
150if the data is needed for future use, it should be
151copied before any subsequent calls overwrite it.
152Expecting port numbers to fit in a 32 bit
153quantity is probably naive.
154