Tuesday, August 16, 2011

socket - /etc/protocols File


Earlier in my post there was mention made that the protocol used there must appear in the protocols(5) table. The text file /etc/protocols acts as a mini-database of various defined internet protocol values. There is a set of functions, which perform in a very similar manner to the service entry functions that were just covered. These act as convenience functions, should you need them. These functions are so similar, in fact, that they do not need to be covered in detail. The function synopsis of getprotoent(3) is as follows:

#include <netdb.h>
struct protoent *getprotoent
(void);

The getprotoent(3) function returns one /etc/protocols entry with each call. A NULL pointer is returned when end-of-file or an error has been encountered. Listing below shows the protoent structure that is returned by the function call.

CAUTION

The pointer returned by getprotoent(3) is only valid until the next call to the same function.

Example

The struct protoent Structure

struct protoent {
   char *p_name;     // official protocol name
   char **p_aliases; // alias list
   int p_proto;      // protocol number
}

The structure members are more fully described as follows:
  • The structure member p_name contains a pointer to a C string that names the protocol (for example "tcp").
  • The member p_aliases is a pointer to an array of C string pointers, of which the last entry is a NULL pointer. If pp points to this structure, then pp->p_aliases[0] contains the first C string (or is NULL when there are no aliases). An example of an alias might be "TCP" (the uppercase name of the protocol is often specified as an alias).
  • The member p_proto contains the protocol number. For example, the protocol number found in /etc/protocols for entry "tcp" should agree with the C macro constant IPPROTO_TCP.

If you check with /usr/include/netinet/in.h and with the value in /etc/protocols, you will indeed see that they both have the value 6.

CAUTION
The getprotoent(3) function suffers from the same flaw as the getservent (3) function under Linux. Even when the value of errno is zeroed prior to calling getprotoent(3), when end-of-file is reached and indicated by a NULL return pointer, the errno value for Red Hat Linux 6.0 is ENOENT. Under other UNIX operating systems, such as HP-UX 10.2 and Sun Solaris 5.5.1, the errno value is left at zero when end-of-file is returned. This leads the author to speculate that this behavior is a bug, which might be corrected in a later release of Linux.

The getprotoent(3) function returns a NULL pointer when end-of-file is reached or when an error has been encountered. Listing below shows a demonstration program that iterates through all of the /etc/protocols database entries.

Example


Below I am presenting some of the important function api's that are very important in socket programming. I have putted the function signature. It will be better if you write the program by your self. Otherwise post a comment I will present that for you on your request.



No comments:

Post a Comment