]>
Commit | Line | Data |
---|---|---|
ca1a4bba MK |
1 | .\" Copyright 2008, Linux Foundation, written by Michael Kerrisk |
2 | .\" <mtk.manpages@gmail.com> | |
3 | .\" | |
4 | .\" Permission is granted to make and distribute verbatim copies of this | |
5 | .\" manual provided the copyright notice and this permission notice are | |
6 | .\" preserved on all copies. | |
7 | .\" | |
8 | .\" Permission is granted to copy and distribute modified versions of this | |
9 | .\" manual under the conditions for verbatim copying, provided that the | |
10 | .\" entire resulting derived work is distributed under the terms of a | |
11 | .\" permission notice identical to this one. | |
12 | .\" | |
13 | .\" Since the Linux kernel and libraries are constantly changing, this | |
14 | .\" manual page may be incorrect or out-of-date. The author(s) assume no | |
15 | .\" responsibility for errors or omissions, or for damages resulting from | |
16 | .\" the use of the information contained herein. The author(s) may not | |
17 | .\" have taken the same level of care in the production of this manual, | |
18 | .\" which is licensed free of charge, as they might when working | |
19 | .\" professionally. | |
20 | .\" | |
21 | .\" Formatted or processed versions of this manual, if unaccompanied by | |
22 | .\" the source, must acknowledge the copyright and authors of this work. | |
23 | .\" | |
535f0df5 | 24 | .TH GETPROTOENT_R 3 2010-09-10 "GNU" "Linux Programmer's Manual" |
ca1a4bba MK |
25 | .SH NAME |
26 | getprotoent_r, getprotobyname_r, getprotobynumber_r \- get | |
27 | protocol entry (reentrant) | |
28 | .SH SYNOPSIS | |
29 | .nf | |
30 | .B #include <netdb.h> | |
31 | .sp | |
32 | .BI "int getprotoent_r(struct protoent *" result_buf ", char *" buf , | |
33 | .BI " size_t " buflen ", struct protoent **" result ); | |
34 | .sp | |
35 | .BI "int getprotobyname_r(const char *" name , | |
36 | .BI " struct protoent *" result_buf ", char *" buf , | |
37 | .BI " size_t " buflen ", struct protoent **" result ); | |
38 | .sp | |
39 | .BI "int getprotobynumber_r(int " proto , | |
40 | .BI " struct protoent *" result_buf ", char *" buf , | |
41 | .BI " size_t " buflen ", struct protoent **" result ); | |
42 | .sp | |
43 | .fi | |
44 | .in -4n | |
45 | Feature Test Macro Requirements for glibc (see | |
46 | .BR feature_test_macros (7)): | |
47 | .ad l | |
48 | .in | |
49 | .sp | |
50 | .BR getprotoent_r (), | |
51 | .BR getprotobyname_r (), | |
52 | .BR getprotobynumber_r (): | |
6cee96e5 | 53 | .RS 4 |
ca1a4bba | 54 | _BSD_SOURCE || _SVID_SOURCE |
6cee96e5 | 55 | .RE |
ca1a4bba MK |
56 | .ad b |
57 | .SH DESCRIPTION | |
58 | The | |
59 | .BR getprotoent_r (), | |
60 | .BR getprotobyname_r (), | |
61 | and | |
62 | .BR getprotobynumber_r () | |
63 | functions are the reentrant equivalents of, respectively, | |
64 | .BR getprotoent (3), | |
65 | .BR getprotobyname (3), | |
66 | and | |
67 | .BR getprotobynumber (3). | |
68 | They differ in the way that the | |
69 | .I protoent | |
70 | structure is returned, | |
71 | and in the function calling signature and return value. | |
72 | This manual page describes just the differences from | |
54d75d6c | 73 | the nonreentrant functions. |
ca1a4bba MK |
74 | |
75 | Instead of returning a pointer to a statically allocated | |
76 | .I protoent | |
77 | structure as the function result, | |
78 | these functions copy the structure into the location pointed to by | |
79 | .IR result_buf . | |
80 | ||
81 | The | |
82 | .I buf | |
83 | array is used to store the string fields pointed to by the returned | |
84 | .I protoent | |
85 | structure. | |
54d75d6c | 86 | (The nonreentrant functions allocate these strings in static storage.) |
ca1a4bba MK |
87 | The size of this array is specified in |
88 | .IR buflen . | |
89 | If | |
90 | .I buf | |
91 | is too small, the call fails with the error | |
92 | .BR ERANGE , | |
93 | and the caller must try again with a larger buffer. | |
94 | (A buffer of length 1024 bytes should be sufficient for most applications.) | |
95 | .\" I can find no information on the required/recommended buffer size; | |
54d75d6c | 96 | .\" the nonreentrant functions use a 1024 byte buffer. |
ca1a4bba MK |
97 | .\" The 1024 byte value is also what the Solaris man page suggests. -- mtk |
98 | ||
99 | If the function call successfully obtains a protocol record, then | |
100 | .I *result | |
101 | is set pointing to | |
102 | .IR result_buf ; | |
103 | otherwise, | |
104 | .I *result | |
105 | is set to NULL. | |
47297adb | 106 | .SH RETURN VALUE |
ca1a4bba | 107 | On success, these functions return 0. |
535f0df5 | 108 | On error, they return one of the positive error numbers listed in ERRORS. |
ca1a4bba MK |
109 | |
110 | On error, record not found | |
111 | .RB ( getprotobyname_r (), | |
112 | .BR getprotobynumber_r ()), | |
113 | or end of input | |
114 | .RB ( getprotoent_r ()) | |
115 | .I result | |
116 | is set to NULL. | |
117 | .SH ERRORS | |
118 | .TP | |
119 | .B ENOENT | |
120 | .RB ( getprotoent_r ()) | |
121 | No more records in database. | |
122 | .TP | |
123 | .B ERANGE | |
124 | .I buf | |
125 | is too small. | |
126 | Try again with a larger buffer | |
127 | (and increased | |
128 | .IR buflen ). | |
47297adb | 129 | .SH CONFORMING TO |
ca1a4bba MK |
130 | These functions are GNU extensions. |
131 | Functions with similar names exist on some other systems, | |
132 | though typically with different calling signatures. | |
133 | .SH EXAMPLE | |
134 | The program below uses | |
135 | .BR getprotobyname_r () | |
136 | to retrieve the protocol record for the protocol named | |
137 | in its first command-line argument. | |
138 | If a second (integer) command-line argument is supplied, | |
139 | it is used as the initial value for | |
140 | .IR buflen ; | |
141 | if | |
142 | .BR getprotobyname_r () | |
143 | fails with the error | |
144 | .BR ERANGE , | |
145 | the program retries with larger buffer sizes. | |
146 | The following shell session shows a couple of sample runs: | |
147 | .in +4n | |
148 | .nf | |
149 | ||
b43a3b30 | 150 | .RB "$" " ./a.out tcp 1" |
ca1a4bba MK |
151 | ERANGE! Retrying with larger buffer |
152 | getprotobyname_r() returned: 0 (success) (buflen=78) | |
153 | p_name=tcp; p_proto=6; aliases=TCP | |
b43a3b30 | 154 | .RB "$" " ./a.out xxx 1" |
ca1a4bba MK |
155 | ERANGE! Retrying with larger buffer |
156 | getprotobyname_r() returned: 0 (success) (buflen=100) | |
157 | Call failed/record not found | |
ca1a4bba MK |
158 | .fi |
159 | .in | |
9c330504 | 160 | .SS Program source |
d84d0300 | 161 | \& |
ca1a4bba MK |
162 | .nf |
163 | #define _GNU_SOURCE | |
164 | #include <ctype.h> | |
165 | #include <netdb.h> | |
166 | #include <stdlib.h> | |
167 | #include <stdio.h> | |
168 | #include <errno.h> | |
169 | #include <string.h> | |
170 | ||
171 | #define MAX_BUF 10000 | |
172 | ||
173 | int | |
174 | main(int argc, char *argv[]) | |
175 | { | |
176 | int buflen, erange_cnt, s; | |
177 | struct protoent result_buf; | |
178 | struct protoent *result; | |
179 | char buf[MAX_BUF]; | |
180 | char **p; | |
181 | ||
182 | if (argc < 2) { | |
183 | printf("Usage: %s proto\-name [buflen]\\n", argv[0]); | |
184 | exit(EXIT_FAILURE); | |
185 | } | |
186 | ||
187 | buflen = 1024; | |
188 | if (argc > 2) | |
189 | buflen = atoi(argv[2]); | |
190 | ||
191 | if (buflen > MAX_BUF) { | |
192 | printf("Exceeded buffer limit (%d)\\n", MAX_BUF); | |
193 | exit(EXIT_FAILURE); | |
194 | } | |
195 | ||
196 | erange_cnt = 0; | |
197 | do { | |
198 | s = getprotobyname_r(argv[1], &result_buf, | |
199 | buf, buflen, &result); | |
200 | if (s == ERANGE) { | |
201 | if (erange_cnt == 0) | |
202 | printf("ERANGE! Retrying with larger buffer\\n"); | |
203 | erange_cnt++; | |
204 | ||
205 | /* Increment a byte at a time so we can see exactly | |
206 | what size buffer was required */ | |
207 | ||
208 | buflen++; | |
209 | ||
210 | if (buflen > MAX_BUF) { | |
211 | printf("Exceeded buffer limit (%d)\\n", MAX_BUF); | |
212 | exit(EXIT_FAILURE); | |
213 | } | |
214 | } | |
215 | } while (s == ERANGE); | |
216 | ||
217 | printf("getprotobyname_r() returned: %s (buflen=%d)\\n", | |
218 | (s == 0) ? "0 (success)" : (s == ENOENT) ? "ENOENT" : | |
219 | strerror(s), buflen); | |
220 | ||
221 | if (s != 0 || result == NULL) { | |
222 | printf("Call failed/record not found\\n"); | |
223 | exit(EXIT_FAILURE); | |
224 | } | |
225 | ||
226 | printf("p_name=%s; p_proto=%d; aliases=", | |
227 | result_buf.p_name, result_buf.p_proto); | |
228 | for (p = result_buf.p_aliases; *p != NULL; p++) | |
229 | printf("%s ", *p); | |
230 | printf("\\n"); | |
231 | ||
232 | exit(EXIT_SUCCESS); | |
233 | } | |
234 | .fi | |
47297adb | 235 | .SH SEE ALSO |
ca1a4bba | 236 | .BR getprotoent (3), |
ca1a4bba | 237 | .BR protocols (5) |