1getprotoent_r(3) Library Functions Manual getprotoent_r(3)
2
3
4
6 getprotoent_r, getprotobyname_r, getprotobynumber_r - get protocol en‐
7 try (reentrant)
8
10 Standard C library (libc, -lc)
11
13 #include <netdb.h>
14
15 int getprotoent_r(struct protoent *restrict result_buf,
16 char buf[restrict .buflen], size_t buflen,
17 struct protoent **restrict result);
18 int getprotobyname_r(const char *restrict name,
19 struct protoent *restrict result_buf,
20 char buf[restrict .buflen], size_t buflen,
21 struct protoent **restrict result);
22 int getprotobynumber_r(int proto,
23 struct protoent *restrict result_buf,
24 char buf[restrict .buflen], size_t buflen,
25 struct protoent **restrict result);
26
27 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
28
29 getprotoent_r(), getprotobyname_r(), getprotobynumber_r():
30 Since glibc 2.19:
31 _DEFAULT_SOURCE
32 glibc 2.19 and earlier:
33 _BSD_SOURCE || _SVID_SOURCE
34
36 The getprotoent_r(), getprotobyname_r(), and getprotobynumber_r() func‐
37 tions are the reentrant equivalents of, respectively, getprotoent(3),
38 getprotobyname(3), and getprotobynumber(3). They differ in the way
39 that the protoent structure is returned, and in the function calling
40 signature and return value. This manual page describes just the dif‐
41 ferences from the nonreentrant functions.
42
43 Instead of returning a pointer to a statically allocated protoent
44 structure as the function result, these functions copy the structure
45 into the location pointed to by result_buf.
46
47 The buf array is used to store the string fields pointed to by the re‐
48 turned protoent structure. (The nonreentrant functions allocate these
49 strings in static storage.) The size of this array is specified in bu‐
50 flen. If buf is too small, the call fails with the error ERANGE, and
51 the caller must try again with a larger buffer. (A buffer of length
52 1024 bytes should be sufficient for most applications.)
53
54 If the function call successfully obtains a protocol record, then *re‐
55 sult is set pointing to result_buf; otherwise, *result is set to NULL.
56
58 On success, these functions return 0. On error, they return one of the
59 positive error numbers listed in ERRORS.
60
61 On error, record not found (getprotobyname_r(), getprotobynumber_r()),
62 or end of input (getprotoent_r()) result is set to NULL.
63
65 ENOENT (getprotoent_r()) No more records in database.
66
67 ERANGE buf is too small. Try again with a larger buffer (and increased
68 buflen).
69
71 For an explanation of the terms used in this section, see at‐
72 tributes(7).
73
74 ┌─────────────────────────────────────┬───────────────┬────────────────┐
75 │Interface │ Attribute │ Value │
76 ├─────────────────────────────────────┼───────────────┼────────────────┤
77 │getprotoent_r(), getprotobyname_r(), │ Thread safety │ MT-Safe locale │
78 │getprotobynumber_r() │ │ │
79 └─────────────────────────────────────┴───────────────┴────────────────┘
80
82 Functions with similar names exist on some other systems, though
83 typically with different calling signatures.
84
86 GNU.
87
89 The program below uses getprotobyname_r() to retrieve the protocol
90 record for the protocol named in its first command-line argument. If a
91 second (integer) command-line argument is supplied, it is used as the
92 initial value for buflen; if getprotobyname_r() fails with the error
93 ERANGE, the program retries with larger buffer sizes. The following
94 shell session shows a couple of sample runs:
95
96 $ ./a.out tcp 1
97 ERANGE! Retrying with larger buffer
98 getprotobyname_r() returned: 0 (success) (buflen=78)
99 p_name=tcp; p_proto=6; aliases=TCP
100 $ ./a.out xxx 1
101 ERANGE! Retrying with larger buffer
102 getprotobyname_r() returned: 0 (success) (buflen=100)
103 Call failed/record not found
104
105 Program source
106
107 #define _GNU_SOURCE
108 #include <ctype.h>
109 #include <errno.h>
110 #include <netdb.h>
111 #include <stdio.h>
112 #include <stdlib.h>
113 #include <string.h>
114
115 #define MAX_BUF 10000
116
117 int
118 main(int argc, char *argv[])
119 {
120 int buflen, erange_cnt, s;
121 struct protoent result_buf;
122 struct protoent *result;
123 char buf[MAX_BUF];
124
125 if (argc < 2) {
126 printf("Usage: %s proto-name [buflen]\n", argv[0]);
127 exit(EXIT_FAILURE);
128 }
129
130 buflen = 1024;
131 if (argc > 2)
132 buflen = atoi(argv[2]);
133
134 if (buflen > MAX_BUF) {
135 printf("Exceeded buffer limit (%d)\n", MAX_BUF);
136 exit(EXIT_FAILURE);
137 }
138
139 erange_cnt = 0;
140 do {
141 s = getprotobyname_r(argv[1], &result_buf,
142 buf, buflen, &result);
143 if (s == ERANGE) {
144 if (erange_cnt == 0)
145 printf("ERANGE! Retrying with larger buffer\n");
146 erange_cnt++;
147
148 /* Increment a byte at a time so we can see exactly
149 what size buffer was required. */
150
151 buflen++;
152
153 if (buflen > MAX_BUF) {
154 printf("Exceeded buffer limit (%d)\n", MAX_BUF);
155 exit(EXIT_FAILURE);
156 }
157 }
158 } while (s == ERANGE);
159
160 printf("getprotobyname_r() returned: %s (buflen=%d)\n",
161 (s == 0) ? "0 (success)" : (s == ENOENT) ? "ENOENT" :
162 strerror(s), buflen);
163
164 if (s != 0 || result == NULL) {
165 printf("Call failed/record not found\n");
166 exit(EXIT_FAILURE);
167 }
168
169 printf("p_name=%s; p_proto=%d; aliases=",
170 result_buf.p_name, result_buf.p_proto);
171 for (char **p = result_buf.p_aliases; *p != NULL; p++)
172 printf("%s ", *p);
173 printf("\n");
174
175 exit(EXIT_SUCCESS);
176 }
177
179 getprotoent(3), protocols(5)
180
181
182
183Linux man-pages 6.05 2023-07-20 getprotoent_r(3)