#include <netdb.h>
int getprotoent_r(size_t size;
struct protoent *restrict result_buf,
char buf[restrict size], size_t size,
struct protoent **restrict result);
int getprotobyname_r(size_t size;
const char *restrict name,
struct protoent *restrict result_buf,
char buf[restrict size], size_t size,
struct protoent **restrict result);
int getprotobynumber_r(size_t size;
int proto,
struct protoent *restrict result_buf,
char buf[restrict size], size_t size,
struct protoent **restrict result);
glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):
getprotoent_r(), getprotobyname_r(), getprotobynumber_r():
glibc 2.19 以降:
_DEFAULT_SOURCE
glibc 2.19 以前:
_BSD_SOURCE || _SVID_SOURCE
これらの関数は、関数の結果として静的に割り当てられた protoent 構造体 へのポインターを返すのではなく、 protoent 構造体を result_buf が 指す場所にコピーする。
The buf array is used to store the string fields pointed to by the returned protoent structure. (The nonreentrant functions allocate these strings in static storage.) The size of this array is specified in size. If buf is too small, the call fails with the error ERANGE, and the caller must try again with a larger buffer. (A buffer of size 1024 bytes should be sufficient for most applications.)
関数呼び出しでプロトコルレコードの取得に成功すると、 *result は result_buf を指すように設定される。 それ以外の場合は *result に NULL が設定される。
エラーの場合、レコードが見つからなかった場合 (getprotobyname_r(), getprotobyaddr_r()) やこれ以上レコードがない場合 (getprotoent_r())、result には NULL が設定される。
| Interface | Attribute | Value |
| getprotoent_r(), getprotobyname_r(), getprotobynumber_r() | Thread safety | MT-Safe locale |
$ ./a.out tcp 1 ERANGE! Retrying with larger buffer getprotobyname_r() returned: 0 (success) (size=78) p_name=tcp; p_proto=6; aliases=TCP $ ./a.out xxx 1 ERANGE! Retrying with larger buffer getprotobyname_r() returned: 0 (success) (size=100) Call failed/record not found
#define _GNU_SOURCE
#include <ctype.h>
#include <errno.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BUF 10000
int
main(int argc, char *argv[])
{
int size, erange_cnt, s;
struct protoent result_buf;
struct protoent *result;
char buf[MAX_BUF];
if (argc < 2) {
printf("Usage: %s proto-name [size]\n", argv[0]);
exit(EXIT_FAILURE);
}
size = 1024;
if (argc > 2)
size = atoi(argv[2]);
if (size > MAX_BUF) {
printf("Exceeded buffer limit (%d)\n", MAX_BUF);
exit(EXIT_FAILURE);
}
erange_cnt = 0;
do {
s = getprotobyname_r(argv[1], &result_buf,
buf, size, &result);
if (s == ERANGE) {
if (erange_cnt == 0)
printf("ERANGE! Retrying with larger buffer\n");
erange_cnt++;
/* Increment a byte at a time so we can see exactly
what size buffer was required. */
size++;
if (size > MAX_BUF) {
printf("Exceeded buffer limit (%d)\n", MAX_BUF);
exit(EXIT_FAILURE);
}
}
} while (s == ERANGE);
printf("getprotobyname_r() returned: %s (size=%d)\n",
(s == 0) ? "0 (success)" : (s == ENOENT) ? "ENOENT" :
strerror(s), size);
if (s != 0 || result == NULL) {
printf("Call failed/record not found\n");
exit(EXIT_FAILURE);
}
printf("p_name=%s; p_proto=%d; aliases=",
result_buf.p_name, result_buf.p_proto);
for (char **p = result_buf.p_aliases; *p != NULL; p++)
printf("%s ", *p);
printf("\n");
exit(EXIT_SUCCESS);
}