1GETPROTOENT_R(3)           Linux Programmer's Manual          GETPROTOENT_R(3)
2
3
4

NAME

6       getprotoent_r,  getprotobyname_r, getprotobynumber_r - get protocol en‐
7       try (reentrant)
8

SYNOPSIS

10       #include <netdb.h>
11
12       int getprotoent_r(struct protoent *restrict result_buf,
13                         char *restrict buf, size_t buflen,
14                         struct protoent **restrict result);
15       int getprotobyname_r(const char *restrict name,
16                         struct protoent *restrict result_buf,
17                         char *restrict buf, size_t buflen,
18                         struct protoent **restrict result);
19       int getprotobynumber_r(int proto,
20                         struct protoent *restrict result_buf,
21                         char *restrict buf, size_t buflen,
22                         struct protoent **restrict result);
23
24   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
25
26       getprotoent_r(), getprotobyname_r(), getprotobynumber_r():
27           Since glibc 2.19:
28               _DEFAULT_SOURCE
29           Glibc 2.19 and earlier:
30               _BSD_SOURCE || _SVID_SOURCE
31

DESCRIPTION

33       The getprotoent_r(), getprotobyname_r(), and getprotobynumber_r() func‐
34       tions  are  the reentrant equivalents of, respectively, getprotoent(3),
35       getprotobyname(3), and getprotobynumber(3).  They  differ  in  the  way
36       that  the  protoent  structure is returned, and in the function calling
37       signature and return value.  This manual page describes just  the  dif‐
38       ferences from the nonreentrant functions.
39
40       Instead  of  returning  a  pointer  to  a statically allocated protoent
41       structure as the function result, these functions  copy  the  structure
42       into the location pointed to by result_buf.
43
44       The  buf array is used to store the string fields pointed to by the re‐
45       turned protoent structure.  (The nonreentrant functions allocate  these
46       strings in static storage.)  The size of this array is specified in bu‐
47       flen.  If buf is too small, the call fails with the error  ERANGE,  and
48       the  caller  must  try again with a larger buffer.  (A buffer of length
49       1024 bytes should be sufficient for most applications.)
50
51       If the function call successfully obtains a protocol record, then  *re‐
52       sult is set pointing to result_buf; otherwise, *result is set to NULL.
53

RETURN VALUE

55       On success, these functions return 0.  On error, they return one of the
56       positive error numbers listed in ERRORS.
57
58       On error, record not found (getprotobyname_r(),  getprotobynumber_r()),
59       or end of input (getprotoent_r()) result is set to NULL.
60

ERRORS

62       ENOENT (getprotoent_r()) No more records in database.
63
64       ERANGE buf is too small.  Try again with a larger buffer (and increased
65              buflen).
66

ATTRIBUTES

68       For an  explanation  of  the  terms  used  in  this  section,  see  at‐
69       tributes(7).
70
71       ┌─────────────────────────────────────┬───────────────┬────────────────┐
72Interface                            Attribute     Value          
73       ├─────────────────────────────────────┼───────────────┼────────────────┤
74getprotoent_r(), getprotobyname_r(), │ Thread safety │ MT-Safe locale │
75getprotobynumber_r()                 │               │                │
76       └─────────────────────────────────────┴───────────────┴────────────────┘
77

CONFORMING TO

79       These functions are GNU extensions.  Functions with similar names exist
80       on  some  other systems, though typically with different calling signa‐
81       tures.
82

EXAMPLES

84       The program below uses  getprotobyname_r()  to  retrieve  the  protocol
85       record for the protocol named in its first command-line argument.  If a
86       second (integer) command-line argument is supplied, it is used  as  the
87       initial  value  for  buflen; if getprotobyname_r() fails with the error
88       ERANGE, the program retries with larger buffer  sizes.   The  following
89       shell session shows a couple of sample runs:
90
91           $ ./a.out tcp 1
92           ERANGE! Retrying with larger buffer
93           getprotobyname_r() returned: 0 (success)  (buflen=78)
94           p_name=tcp; p_proto=6; aliases=TCP
95           $ ./a.out xxx 1
96           ERANGE! Retrying with larger buffer
97           getprotobyname_r() returned: 0 (success)  (buflen=100)
98           Call failed/record not found
99
100   Program source
101
102       #define _GNU_SOURCE
103       #include <ctype.h>
104       #include <netdb.h>
105       #include <stdlib.h>
106       #include <stdio.h>
107       #include <errno.h>
108       #include <string.h>
109
110       #define MAX_BUF 10000
111
112       int
113       main(int argc, char *argv[])
114       {
115           int buflen, erange_cnt, s;
116           struct protoent result_buf;
117           struct protoent *result;
118           char buf[MAX_BUF];
119
120           if (argc < 2) {
121               printf("Usage: %s proto-name [buflen]\n", argv[0]);
122               exit(EXIT_FAILURE);
123           }
124
125           buflen = 1024;
126           if (argc > 2)
127               buflen = atoi(argv[2]);
128
129           if (buflen > MAX_BUF) {
130               printf("Exceeded buffer limit (%d)\n", MAX_BUF);
131               exit(EXIT_FAILURE);
132           }
133
134           erange_cnt = 0;
135           do {
136               s = getprotobyname_r(argv[1], &result_buf,
137                            buf, buflen, &result);
138               if (s == ERANGE) {
139                   if (erange_cnt == 0)
140                       printf("ERANGE! Retrying with larger buffer\n");
141                   erange_cnt++;
142
143                   /* Increment a byte at a time so we can see exactly
144                      what size buffer was required. */
145
146                   buflen++;
147
148                   if (buflen > MAX_BUF) {
149                       printf("Exceeded buffer limit (%d)\n", MAX_BUF);
150                       exit(EXIT_FAILURE);
151                   }
152               }
153           } while (s == ERANGE);
154
155           printf("getprotobyname_r() returned: %s  (buflen=%d)\n",
156                   (s == 0) ? "0 (success)" : (s == ENOENT) ? "ENOENT" :
157                   strerror(s), buflen);
158
159           if (s != 0 || result == NULL) {
160               printf("Call failed/record not found\n");
161               exit(EXIT_FAILURE);
162           }
163
164           printf("p_name=%s; p_proto=%d; aliases=",
165                       result_buf.p_name, result_buf.p_proto);
166           for (char **p = result_buf.p_aliases; *p != NULL; p++)
167               printf("%s ", *p);
168           printf("\n");
169
170           exit(EXIT_SUCCESS);
171       }
172

SEE ALSO

174       getprotoent(3), protocols(5)
175

COLOPHON

177       This  page  is  part of release 5.12 of the Linux man-pages project.  A
178       description of the project, information about reporting bugs,  and  the
179       latest     version     of     this    page,    can    be    found    at
180       https://www.kernel.org/doc/man-pages/.
181
182
183
184GNU                               2021-03-22                  GETPROTOENT_R(3)
Impressum