getservbyport(3) — Subroutines
NAME
getservbyport, getservbyport_r − Get a service entry by port number
LIBRARY
Standard C Library (libc.a)
SYNOPSIS
#include <netdb.h>
struct servent ∗getservbyport(
int port,
const char ∗proto);
int getservbyport_r(
int port,
const char ∗proto,
struct servent ∗serv,
struct servent_data ∗serv_data);
PARAMETERS
portSpecifies the port number where the service is located. This argument must be converted from host-byte order to a 2-byte Inernet network integer through a call to the htons function.
protoSpecifies the protocol name to use when contacting the service.
servPoints to the servent structure.
serv_dataIs data for the services database.
DESCRIPTION
The getservbyport() function returns a pointer to a structure of type servent. Its members specify data in fields from a record line in the /etc/services network services database file. The netdb.h header file defines the servent structure.
The getservbyport() function searches the /etc/services file sequentially until a match with the port parameter and with the proto parameter occurs. When used, the proto parameter must specify the /etc/services file protocol name. When the protocol name is not specified (the proto parameter is NULL), the proto parameter need not be matched during the /etc/services file record search. When EOF (End-of-File) is reached without a match, an error value is returned by this function.
The getservbyport() function searches the /etc/services file sequentially until one of the following occurs:
•A service port number and protocol name match.
•A service port number matches while the proto parameter is set to NULL.
•The end of the file is reached.
Use the endservent() function to close the /etc/services file.
The getservbyport_r() is the reentrant version of the getservbyport() function. When using the getservbyport_r() function, use the endservent_r() function to close the services file. Upon successful completion, the returned structure is pointed to by serv. The netdb.h header file defines the servent and serv_data structures.
The system searches either the local /etc/services file or the NIS distributed networks file for the requested information. To determine which file or files to search, and in which order, the system uses the switches in the /etc/svc.conf file.
NOTES
The getservbyport() return value points to static data, which is overwritten by any subsequently called functions using the same structure.
Before its first access by either the setservent_r() or getservbyport_r() function, you must zero-fill the servent_data structure.
RETURN VALUES
Upon successful completion, the getservbyport() function returns a pointer to a servent structure. A NULL pointer is returned when an error occurs or whenever the end of the /etc/services file is reached.
Upon successful completion for getservbyport_r(), a value of 0 (zero) is returned. Otherwise, a value of -1 is returned.
ERRORS
If any of the following conditions occurs, the getservbyport_r() function sets errno to the corresponding value:
[EINVAL]The serv or serv_data parameter is invalid.
[ESRCH]The search failed.
In addition, if the function fails to open the file, it sets errno to indicate the cause of the failure.
FILES
/etc/services
The DARPA Internet network service name database file. Each record in the file occupies a single line and has four fields: the official service name, the port number, the protocol name, and aliases.
RELATED INFORMATION
Functions: getservbyname(3), getservent(3), setservent(3), endservent(3).