1GETPROTOENT_R(3) Linux Programmer's Manual GETPROTOENT_R(3)
2
3
4
6 getprotoent_r, getprotobyname_r, getprotobynumber_r - get protocol
7 entry (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 _BSD_SOURCE || _SVID_SOURCE
27
29 The getprotoent_r(), getprotobyname_r(), and getprotobynumber_r() func‐
30 tions are the reentrant equivalents of, respectively, getprotoent(3),
31 getprotobyname(3), and getprotobynumber(3). They differ in the way
32 that the protoent structure is returned, and in the function calling
33 signature and return value. This manual page describes just the dif‐
34 ferences from the nonreentrant functions.
35
36 Instead of returning a pointer to a statically allocated protoent
37 structure as the function result, these functions copy the structure
38 into the location pointed to by result_buf.
39
40 The buf array is used to store the string fields pointed to by the
41 returned protoent structure. (The nonreentrant functions allocate
42 these strings in static storage.) The size of this array is specified
43 in buflen. If buf is too small, the call fails with the error ERANGE,
44 and the caller must try again with a larger buffer. (A buffer of
45 length 1024 bytes should be sufficient for most applications.)
46
47 If the function call successfully obtains a protocol record, then
48 *result is set pointing to result_buf; otherwise, *result is set to
49 NULL.
50
52 On success, these functions return 0. On error, they return one of the
53 positive error numbers listed in ERRORS.
54
55 On error, record not found (getprotobyname_r(), getprotobynumber_r()),
56 or end of input (getprotoent_r()) result is set to NULL.
57
59 ENOENT (getprotoent_r()) No more records in database.
60
61 ERANGE buf is too small. Try again with a larger buffer (and increased
62 buflen).
63
65 These functions are GNU extensions. Functions with similar names exist
66 on some other systems, though typically with different calling signa‐
67 tures.
68
70 The program below uses getprotobyname_r() to retrieve the protocol
71 record for the protocol named in its first command-line argument. If a
72 second (integer) command-line argument is supplied, it is used as the
73 initial value for buflen; if getprotobyname_r() fails with the error
74 ERANGE, the program retries with larger buffer sizes. The following
75 shell session shows a couple of sample runs:
76
77 $ ./a.out tcp 1
78 ERANGE! Retrying with larger buffer
79 getprotobyname_r() returned: 0 (success) (buflen=78)
80 p_name=tcp; p_proto=6; aliases=TCP
81 $ ./a.out xxx 1
82 ERANGE! Retrying with larger buffer
83 getprotobyname_r() returned: 0 (success) (buflen=100)
84 Call failed/record not found
85
86 Program source
87
88 #define _GNU_SOURCE
89 #include <ctype.h>
90 #include <netdb.h>
91 #include <stdlib.h>
92 #include <stdio.h>
93 #include <errno.h>
94 #include <string.h>
95
96 #define MAX_BUF 10000
97
98 int
99 main(int argc, char *argv[])
100 {
101 int buflen, erange_cnt, s;
102 struct protoent result_buf;
103 struct protoent *result;
104 char buf[MAX_BUF];
105 char **p;
106
107 if (argc < 2) {
108 printf("Usage: %s proto-name [buflen]\n", argv[0]);
109 exit(EXIT_FAILURE);
110 }
111
112 buflen = 1024;
113 if (argc > 2)
114 buflen = atoi(argv[2]);
115
116 if (buflen > MAX_BUF) {
117 printf("Exceeded buffer limit (%d)\n", MAX_BUF);
118 exit(EXIT_FAILURE);
119 }
120
121 erange_cnt = 0;
122 do {
123 s = getprotobyname_r(argv[1], &result_buf,
124 buf, buflen, &result);
125 if (s == ERANGE) {
126 if (erange_cnt == 0)
127 printf("ERANGE! Retrying with larger buffer\n");
128 erange_cnt++;
129
130 /* Increment a byte at a time so we can see exactly
131 what size buffer was required */
132
133 buflen++;
134
135 if (buflen > MAX_BUF) {
136 printf("Exceeded buffer limit (%d)\n", MAX_BUF);
137 exit(EXIT_FAILURE);
138 }
139 }
140 } while (s == ERANGE);
141
142 printf("getprotobyname_r() returned: %s (buflen=%d)\n",
143 (s == 0) ? "0 (success)" : (s == ENOENT) ? "ENOENT" :
144 strerror(s), buflen);
145
146 if (s != 0 || result == NULL) {
147 printf("Call failed/record not found\n");
148 exit(EXIT_FAILURE);
149 }
150
151 printf("p_name=%s; p_proto=%d; aliases=",
152 result_buf.p_name, result_buf.p_proto);
153 for (p = result_buf.p_aliases; *p != NULL; p++)
154 printf("%s ", *p);
155 printf("\n");
156
157 exit(EXIT_SUCCESS);
158 }
159
161 getprotoent(3), protocols(5)
162
164 This page is part of release 3.53 of the Linux man-pages project. A
165 description of the project, and information about reporting bugs, can
166 be found at http://www.kernel.org/doc/man-pages/.
167
168
169
170GNU 2010-09-10 GETPROTOENT_R(3)