1.\" $OpenBSD: getservent.3,v 1.21 2013/06/05 03:39:23 tedu Exp $ 2.\" 3.\" Copyright (c) 1983, 1991, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. Neither the name of the University nor the names of its contributors 15.\" may be used to endorse or promote products derived from this software 16.\" without specific prior written permission. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28.\" SUCH DAMAGE. 29.\" 30.Dd $Mdocdate: June 5 2013 $ 31.Dt GETSERVENT 3 32.Os 33.Sh NAME 34.Nm getservent , 35.Nm getservent_r , 36.Nm getservbyport , 37.Nm getservbyport_r , 38.Nm getservbyname , 39.Nm getservbyname_r , 40.Nm setservent , 41.Nm setservent_r , 42.Nm endservent , 43.Nm endservent_r 44.Nd get service entry 45.Sh SYNOPSIS 46.In netdb.h 47.Ft struct servent * 48.Fn getservent "void" 49.Ft int 50.Fn getservent_r "struct servent *servent" "struct servent_data *servent_data" 51.Ft struct servent * 52.Fn getservbyname "const char *name" "const char *proto" 53.Ft int 54.Fn getservbyname_r "const char *name" "const char *proto" "struct servent *servent" "struct servent_data *servent_data" 55.Ft struct servent * 56.Fn getservbyport "int port" "const char *proto" 57.Ft int 58.Fn getservbyport_r "int port" "const char *proto" "struct servent *servent" "struct servent_data *servent_data" 59.Ft void 60.Fn setservent "int stayopen" 61.Ft void 62.Fn setservent_r "int stayopen" "struct servent_data *servent_data" 63.Ft void 64.Fn endservent "void" 65.Ft void 66.Fn endservent_r "struct servent_data *servent_data" 67.Sh DESCRIPTION 68The 69.Fn getservent , 70.Fn getservbyname , 71and 72.Fn getservbyport 73functions each return a pointer to an object with the following structure 74containing the broken-out fields of a line in the network services database, 75.Pa /etc/services . 76.Bd -literal -offset indent 77struct servent { 78 char *s_name; /* official name of service */ 79 char **s_aliases; /* alias list */ 80 int s_port; /* port service resides at */ 81 char *s_proto; /* protocol to use */ 82}; 83.Ed 84.Pp 85The members of this structure are: 86.Bl -tag -width s_aliases 87.It Fa s_name 88The official name of the service. 89.It Fa s_aliases 90A null-terminated list of alternate names for the service. 91.It Fa s_port 92The port number at which the service resides. 93Port numbers are returned in network byte order. 94.It Fa s_proto 95The name of the protocol to use when contacting the service. 96.El 97.Pp 98The 99.Fn getservent 100function reads the next line of the file, opening the file if necessary. 101.Pp 102The 103.Fn setservent 104function opens and rewinds the file. 105If the 106.Fa stayopen 107flag is non-zero, 108the services database will not be closed after each call to 109.Fn getservbyname 110or 111.Fn getservbyport . 112.Pp 113The 114.Fn endservent 115function closes the file. 116.Pp 117The 118.Fn getservbyname 119and 120.Fn getservbyport 121functions sequentially search from the beginning of the file until a 122matching protocol name or port number (specified in network byte order) 123is found, or until 124.Dv EOF 125is encountered. 126If a protocol name is also supplied (non-null), 127searches must also match the protocol. 128.Pp 129The 130.Fn getservent_r , 131.Fn getservbyport_r , 132.Fn getservbyname_r , 133.Fn setservent_r , 134and 135.Fn endservent_r 136functions are reentrant versions of the above functions that take a 137pointer to a 138.Fa servent_data 139structure which is used to store state information. 140The structure must be zero-filled before it is used 141and should be considered opaque for the sake of portability. 142.Pp 143The 144.Fn getservent_r , 145.Fn getservbyport_r , 146and 147.Fn getservbyname_r 148functions 149also take a pointer to a 150.Fa servent 151structure which is used to store the results of the database lookup. 152.Sh RETURN VALUES 153The 154.Fn getservent , 155.Fn getservbyport , 156and 157.Fn getservbyname 158functions return a pointer to a 159.Fa servent 160structure on success or a null pointer if end-of-file 161is reached or an error occurs. 162.Pp 163The 164.Fn getservent_r , 165.Fn getservbyport_r , 166and 167.Fn getservbyname_r 168functions return 0 on success or \-1 if end-of-file 169is reached or an error occurs. 170.Sh FILES 171.Bl -tag -width /etc/services -compact 172.It Pa /etc/services 173.El 174.Sh SEE ALSO 175.Xr getprotoent 3 , 176.Xr services 5 177.Sh STANDARDS 178The 179.Fn getservent , 180.Fn getservbynumber , 181.Fn getservbyname , 182.Fn setservent , 183and 184.Fn endservent 185functions conform to 186.St -p1003.1-2004 . 187.Pp 188The 189.Fn getservent_r , 190.Fn getservbyport_r , 191.Fn getservbyname_r , 192.Fn setservent_r , 193and 194.Fn endservent_r 195functions are not currently standardized. 196This implementation follows the API used by HP, IBM, and Digital. 197.Sh HISTORY 198The 199.Fn getservent , 200.Fn getservbyport , 201.Fn getservbyname , 202.Fn setservent , 203and 204.Fn endservent 205functions appeared in 206.Bx 4.2 . 207.Pp 208The 209.Fn getservent_r , 210.Fn getservbyport_r , 211.Fn getservbyname_r , 212.Fn setservent_r , 213and 214.Fn endservent_r 215functions appeared in 216.Ox 3.7 . 217.Sh BUGS 218The non-reentrant functions use static data storage; if the data is needed 219for future use, it should be copied before any subsequent calls overwrite it. 220Expecting port numbers to fit in a 32-bit quantity is probably naive. 221