1GETPROTOENT_R(3) Linux Programmer's Manual GETPROTOENT_R(3)
2
3
4
6 getprotoent_r, getprotobyname_r, getprotobynumber_r - get protocol en‐
7 try (reentrant)
8
10 #include <netdb.h>
11
12 int getprotoent_r(struct protoent *result_buf, char *buf,
13 size_t buflen, struct protoent **result);
14
15 int getprotobyname_r(const char *name,
16 struct protoent *result_buf, char *buf,
17 size_t buflen, struct protoent **result);
18
19 int getprotobynumber_r(int proto,
20 struct protoent *result_buf, char *buf,
21 size_t buflen, struct protoent **result);
22
23 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
24
25 getprotoent_r(), getprotobyname_r(), getprotobynumber_r():
26 Since glibc 2.19:
27 _DEFAULT_SOURCE
28 Glibc 2.19 and earlier:
29 _BSD_SOURCE || _SVID_SOURCE
30
32 The getprotoent_r(), getprotobyname_r(), and getprotobynumber_r() func‐
33 tions are the reentrant equivalents of, respectively, getprotoent(3),
34 getprotobyname(3), and getprotobynumber(3). They differ in the way
35 that the protoent structure is returned, and in the function calling
36 signature and return value. This manual page describes just the dif‐
37 ferences from the nonreentrant functions.
38
39 Instead of returning a pointer to a statically allocated protoent
40 structure as the function result, these functions copy the structure
41 into the location pointed to by result_buf.
42
43 The buf array is used to store the string fields pointed to by the re‐
44 turned protoent structure. (The nonreentrant functions allocate these
45 strings in static storage.) The size of this array is specified in bu‐
46 flen. If buf is too small, the call fails with the error ERANGE, and
47 the caller must try again with a larger buffer. (A buffer of length
48 1024 bytes should be sufficient for most applications.)
49
50 If the function call successfully obtains a protocol record, then *re‐
51 sult is set pointing to result_buf; otherwise, *result is set to NULL.
52
54 On success, these functions return 0. On error, they return one of the
55 positive error numbers listed in ERRORS.
56
57 On error, record not found (getprotobyname_r(), getprotobynumber_r()),
58 or end of input (getprotoent_r()) result is set to NULL.
59
61 ENOENT (getprotoent_r()) No more records in database.
62
63 ERANGE buf is too small. Try again with a larger buffer (and increased
64 buflen).
65
67 For an explanation of the terms used in this section, see at‐
68 tributes(7).
69
70 ┌─────────────────────┬───────────────┬────────────────┐
71 │Interface │ Attribute │ Value │
72 ├─────────────────────┼───────────────┼────────────────┤
73 │getprotoent_r(), │ Thread safety │ MT-Safe locale │
74 │getprotobyname_r(), │ │ │
75 │getprotobynumber_r() │ │ │
76 └─────────────────────┴───────────────┴────────────────┘
77
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
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
174 getprotoent(3), protocols(5)
175
177 This page is part of release 5.10 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 2020-11-01 GETPROTOENT_R(3)