getservbyport_r Subroutine

Purpose

Gets service entry by port.

Library

Standard C Library (libc.a)

Syntax

#include <netdb.h>

int getservbyport_r(Port, Proto, servent, serv_data)
int Port;
const char *Proto;
struct servent *servent;
struct servent_data *serv_data;

Description

The getservbyport_r subroutine retrieves an entry from the /etc/services file using a port number as a search key. An application program can use the getservbyport_r subroutine to access a service, service aliases, the protocol for the service, and a protocol port number for the service.

The getservbyport_r subroutine searches the services file sequentially from the beginning of the file until it finds one of the following:
  • Matching protocol and port number
  • Matching protocol when the Port parameter value equals 0
  • End of the file

Upon locating a matching protocol and port number or upon locating a matching protocol where the Port parameter value equals 0, the getservbyport_r subroutine returns a pointer to a servent structure, which contains fields for a line of information in the /etc/services file. The netdb.h file defines the servent structure, the servert_data structure, and their fields.

The getservbyport routine overwrites static data returned on subsequent calls. The getservbyport_r routine does not.

Use the endservent_r subroutine to close the /etc/services file.

Parameters

Item Description
Port Specifies the port where a service resides.
Proto Specifies a protocol for use with the service.
servent Points to the servent structure.
serv_data Points to the serv_data structure.

Return Values

The function returns a 0 if successful and a -1 if unsuccessful.

Note: If an error occurs or the end of the file is reached, the getservbyport_r subroutine returns a -1 to indicate error.

Files

Item Description
/etc/services Contains service names.