getservbyname Function

 < Day Day Up > 



getservbyname Function

Within the IP suite of protocols, a service sits on a unique port for a given Transport layer protocol. For example, an SMTP server resides on port 25 within the TCP layer. An SNMP agent resides on port 161 within the UDP layer. The getservbyname function takes a service name and protocol and yields among other things, the port to be used to either register the service or find the service. The getservbyname function has the following prototype:

#include <netdb.h> struct servent *getservbyname( const char *name,                                    const char *proto );

The caller provides a service name, such as “smtp” or “http” and a protocol such as “tcp” or “udp.” The function returns a pointer to a servent structure representing the requested protocol, or NULL representing either an error or service not found. The servent structure has the following format:

struct servent {     char *s_name;       /* The official service name */     char **s_aliases;   /* List of alias names       */     int  s_port;        /* Port number               */     char s_proto;       /* Required Protocol         */ };

The s_name field represents the official service name (such as “smtp”), whereas s_alias is a list of other names for the service. For example, service “smtp” has an alias “mail.” Field s_port is the port number for the service; in the case of “smtp,” the port number is 25 (the well-known reserved port for SMTP). Finally, the s_proto field is the protocol that is required by the particular service (in this case, “tcp”).

Let’s now look at a simple example of the getservbyname function. In Listing 4.12 is the simple case of an application identifying the port number of a given service. We provide the getservbyname function our service name (“http”) and the transport protocol over which it runs (“tcp”), and then store the result in sp. The service name (which may differ from the service name we passed in) is stored in sp->s_name, whereas the port number is stored in sp->s_port. The port number is stored in network byte order, so we use the ntohs function to convert this into host byte order for proper display.

Listing 4.12 Simple example of getservbyname.

start example
#include <netdb.h> struct servent *sp; ... sp = getservbyname( "http", "tcp" ); if (sp) {   printf( "Service %s is at port %d\n",            sp->s_name, ntohs( sp->s_port ) ); }
end example

More information is provided by getservbyname (as is illustrated by the contents of the servent structure). Listing 4.13 illustrates the use of the other fields within the servent structure. The protocol field (s_proto) identifies the protocol required for the particular service. The s_aliases list defines some other service names that can be used. If the application uses an alias in the getservbyname call, the official name still appears in the s_name field of the servent structure.

Listing 4.13 Complete example of getservbyname.

start example
#include <netdb.h> struct servent *sp; int i; sp = getservbyname( "smtp", "tcp" ); if (sp) {   printf( "s_name  = %s\n", sp->s_name );   printf( "s_port  = %d\n", ntohs(sp->s_port) );   printf( "s_proto = %s\n", sp->s_proto );   i = 0;   printf( "Aliases:\n" );   while (1) {     if ( sp->s_aliases[i] ) {       printf(" s_aliases[%d] = %s\n", i, sp->s_aliases[i] );       i++;     } else break;   } }
end example

An item to note with getservbyname is that although the application may know the service name of interest, it may not know the protocol over which it operates. In this case, the protocol argument of getservbyname may be passed as NULL. The application should then consult the s_proto field to know how to construct the socket for creating or connecting to the service. For example:

sp = getservbyname( "mail", NULL );

results in the same information as the prior example in Listing 4.13. Note that in addition to not specifying the protocol parameter, we also specify an alias for the “smtp” service.



 < Day Day Up > 



BSD Sockets Programming from a Multi-Language Perspective
Network Programming for Microsoft Windows , Second Edition (Microsoft Programming Series)
ISBN: 1584502681
EAN: 2147483647
Year: 2003
Pages: 225
Authors: Jim Ohlund

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net