Protocols Database (The GNU C Library)

#### 16.6.6 Protocols Database ¶

The communications protocol used with a socket controls low-level details of how data are exchanged. For example, the protocol implements things like checksums to detect errors in transmissions, and routing instructions for messages. Normal user programs have little reason to mess with these details directly.

The default communications protocol for the Internet namespace depends on the communication style. For stream communication, the default is TCP (“transmission control protocol”). For datagram communication, the default is UDP (“user datagram protocol”). For reliable datagram communication, the default is RDP (“reliable datagram protocol”). You should nearly always use the default.

Internet protocols are generally specified by a name instead of a number. The network protocols that a host knows about are stored in a database. This is usually either derived from the file `/etc/protocols`, or it may be an equivalent provided by a name server. You look up the protocol number associated with a named protocol in the database using the `getprotobyname` function.

Here are detailed descriptions of the utilities for accessing the protocols database. These are declared in `netdb.h`.

Data Type: struct protoent ¶

  This data type is used to represent entries in the network protocols database. It has the following members:

  `char *p_name`

    This is the official name of the protocol.

  `char **p_aliases`

    These are alternate names for the protocol, specified as an array of strings. The last element of the array is a null pointer.

  `int p_proto`

    This is the protocol number (in host byte order); use this member as the protocol argument to `socket`.

You can use `getprotobyname` and `getprotobynumber` to search the protocols database for a specific protocol. The information is returned in a statically-allocated structure; you must copy the information if you need to save it across calls.

Function: `struct protoent *` getprotobyname `(const char *name)` ¶

  Preliminary: | MT-Unsafe race:protobyname locale | AS-Unsafe dlopen plugin heap lock | AC-Unsafe corrupt lock fd mem | See POSIX Safety Concepts.

  The `getprotobyname` function returns information about the network protocol named name. If there is no such protocol, it returns a null pointer.

Function: `struct protoent *` getprotobynumber `(int protocol)` ¶

  Preliminary: | MT-Unsafe race:protobynumber locale | AS-Unsafe dlopen plugin heap lock | AC-Unsafe corrupt lock fd mem | See POSIX Safety Concepts.

  The `getprotobynumber` function returns information about the network protocol with number protocol. If there is no such protocol, it returns a null pointer.

You can also scan the whole protocols database one protocol at a time by using `setprotoent`, `getprotoent` and `endprotoent`. Be careful when using these functions because they are not reentrant.

Function: `void` setprotoent `(int stayopen)` ¶

  Preliminary: | MT-Unsafe race:protoent locale | AS-Unsafe dlopen plugin heap lock | AC-Unsafe corrupt lock fd mem | See POSIX Safety Concepts.

  This function opens the protocols database to begin scanning it.

  If the stayopen argument is nonzero, this sets a flag so that subsequent calls to `getprotobyname` or `getprotobynumber` will not close the database (as they usually would). This makes for more efficiency if you call those functions several times, by avoiding reopening the database for each call.

Function: `struct protoent *` getprotoent `(void)` ¶

  Preliminary: | MT-Unsafe race:protoent race:protoentbuf locale | AS-Unsafe dlopen plugin heap lock | AC-Unsafe corrupt lock fd mem | See POSIX Safety Concepts.

  This function returns the next entry in the protocols database. It returns a null pointer if there are no more entries.

Function: `void` endprotoent `(void)` ¶

  Preliminary: | MT-Unsafe race:protoent locale | AS-Unsafe dlopen plugin heap lock | AC-Unsafe corrupt lock fd mem | See POSIX Safety Concepts.

  This function closes the protocols database.