Monday, August 15, 2011

Examining the /etc/services File


Your Linux system has a text file, usually named /etc/services. This file is described in the man page services(5). This file maps the user-friendly names of certain Internet services to a port number and protocol. The precise pathname for this file is given by the C language macro _PATH_SERVICES. A simple example of its use follows:

Example

#include <netdb.h>
printf("File is path '%s'\n", _PATH_SERVICES);

The preceding code shows the necessary include file netdb.h and a printf(3) statement, which prints out the pathname for the services file. Each text line in the /etc/services file describes one Internet service. It has the following general format:

service-name port/protocol [alias . . .]

The square brackets shown indicate that the one or more alias entries are optional. The /etc/services text line is described in detail in Table below

Field
Description
service-name
The case-sensitive use-friendly name of the service is described by this
table entry.
Port
The port number precedes the slash, and is the decimal port number for
the service.
/
This separates the port number from the protocol field
Protocol
This specifies the type of the protocol to be used. This should be a
protocol that can be found in the protocols(5) file. Common
examples are udp or tcp.
alias
Other names for the "service-name." Additional aliases can be
separated by tabs or spaces. There is a maximum of 35 aliases
permitted, due to restrictions in getservent(3).

Following is a pair of well-known service entries:

ftp        21/tcp
telnet     23/tcp

The first entry shown lists the ftp service as being available on TCP/IP port 21. The second entry shows the telnet service being available on TCP/IP port 23.

Working with the /etc/services file directly is neither convenient nor wise for your program. Consequently, Linux provides you with some routines to make things easier.

Now after having some understanding we need to look on some of the functions like getservent, setservent and endservent. First I will present getservent function.




No comments:

Post a Comment