Add BIND 9.2.4rc7.
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / bind / irs / getprotoent_r.c
1 /*
2  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (c) 1998-1999 by Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static const char rcsid[] = "$Id: getprotoent_r.c,v 1.3.2.1 2004/03/09 09:17:30 marka Exp $";
20 #endif /* LIBC_SCCS and not lint */
21
22 #include <port_before.h>
23 #if !defined(_REENTRANT) || !defined(DO_PTHREADS)
24         static int getprotoent_r_not_required = 0;
25 #else
26 #include <errno.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <sys/types.h>
30 #include <netinet/in.h>
31 #include <netdb.h>
32 #include <port_after.h>
33
34 #ifdef PROTO_R_RETURN
35
36 static PROTO_R_RETURN 
37 copy_protoent(struct protoent *, struct protoent *, PROTO_R_COPY_ARGS);
38
39 PROTO_R_RETURN
40 getprotobyname_r(const char *name, struct protoent *pptr, PROTO_R_ARGS) {
41         struct protoent *pe = getprotobyname(name);
42 #ifdef PROTO_R_SETANSWER
43         int n = 0;
44
45         if (pe == NULL || (n = copy_protoent(pe, pptr, PROTO_R_COPY)) != 0)
46                 *answerp = NULL;
47         else
48                 *answerp = pptr;
49         
50         return (n);
51 #else
52         if (pe == NULL)
53                 return (PROTO_R_BAD);
54
55         return (copy_protoent(pe, pptr, PROTO_R_COPY));
56 #endif
57 }
58
59 PROTO_R_RETURN
60 getprotobynumber_r(int proto, struct protoent *pptr, PROTO_R_ARGS) {
61         struct protoent *pe = getprotobynumber(proto);
62 #ifdef PROTO_R_SETANSWER
63         int n = 0;
64
65         if (pe == NULL || (n = copy_protoent(pe, pptr, PROTO_R_COPY)) != 0)
66                 *answerp = NULL;
67         else
68                 *answerp = pptr;
69         
70         return (n);
71 #else
72         if (pe == NULL)
73                 return (PROTO_R_BAD);
74
75         return (copy_protoent(pe, pptr, PROTO_R_COPY));
76 #endif
77 }
78
79 /*
80  *      These assume a single context is in operation per thread.
81  *      If this is not the case we will need to call irs directly
82  *      rather than through the base functions.
83  */
84
85 PROTO_R_RETURN
86 getprotoent_r(struct protoent *pptr, PROTO_R_ARGS) {
87         struct protoent *pe = getprotoent();
88 #ifdef PROTO_R_SETANSWER
89         int n = 0;
90
91         if (pe == NULL || (n = copy_protoent(pe, pptr, PROTO_R_COPY)) != 0)
92                 *answerp = NULL;
93         else
94                 *answerp = pptr;
95         
96         return (n);
97 #else
98         if (pe == NULL)
99                 return (PROTO_R_BAD);
100
101         return (copy_protoent(pe, pptr, PROTO_R_COPY));
102 #endif
103 }
104
105 PROTO_R_SET_RETURN
106 #ifdef PROTO_R_ENT_ARGS
107 setprotoent_r(int stay_open, PROTO_R_ENT_ARGS)
108 #else
109 setprotoent_r(int stay_open)
110 #endif
111 {
112         setprotoent(stay_open);
113 #ifdef PROTO_R_SET_RESULT
114         return (PROTO_R_SET_RESULT);
115 #endif
116 }
117
118 PROTO_R_END_RETURN
119 #ifdef PROTO_R_ENT_ARGS
120 endprotoent_r(PROTO_R_ENT_ARGS)
121 #else
122 endprotoent_r()
123 #endif
124 {
125         endprotoent();
126         PROTO_R_END_RESULT(PROTO_R_OK);
127 }
128
129 /* Private */
130
131 #ifndef PROTOENT_DATA
132 static PROTO_R_RETURN
133 copy_protoent(struct protoent *pe, struct protoent *pptr, PROTO_R_COPY_ARGS) {
134         char *cp;
135         int i, n;
136         int numptr, len;
137
138         /* Find out the amount of space required to store the answer. */
139         numptr = 1; /* NULL ptr */
140         len = (char *)ALIGN(buf) - buf;
141         for (i = 0; pe->p_aliases[i]; i++, numptr++) {
142                 len += strlen(pe->p_aliases[i]) + 1;
143         }
144         len += strlen(pe->p_name) + 1;
145         len += numptr * sizeof(char*);
146         
147         if (len > (int)buflen) {
148                 errno = ERANGE;
149                 return (PROTO_R_BAD);
150         }
151
152         /* copy protocol value*/
153         pptr->p_proto = pe->p_proto;
154
155         cp = (char *)ALIGN(buf) + numptr * sizeof(char *);
156
157         /* copy official name */
158         n = strlen(pe->p_name) + 1;
159         strcpy(cp, pe->p_name);
160         pptr->p_name = cp;
161         cp += n;
162
163         /* copy aliases */
164         pptr->p_aliases = (char **)ALIGN(buf);
165         for (i = 0 ; pe->p_aliases[i]; i++) {
166                 n = strlen(pe->p_aliases[i]) + 1;
167                 strcpy(cp, pe->p_aliases[i]);
168                 pptr->p_aliases[i] = cp;
169                 cp += n;
170         }
171         pptr->p_aliases[i] = NULL;
172
173         return (PROTO_R_OK);
174 }
175 #else /* !PROTOENT_DATA */
176 static int
177 copy_protoent(struct protoent *pe, struct protoent *pptr, PROTO_R_COPY_ARGS) {
178         char *cp, *eob;
179         int i, n;
180
181         /* copy protocol value */
182         pptr->p_proto = pe->p_proto;
183
184         /* copy official name */
185         cp = pdptr->line;
186         eob = pdptr->line + sizeof(pdptr->line);
187         if ((n = strlen(pe->p_name) + 1) < (eob - cp)) {
188                 strcpy(cp, pe->p_name);
189                 pptr->p_name = cp;
190                 cp += n;
191         } else {
192                 return (-1);
193         }
194
195         /* copy aliases */
196         i = 0;
197         pptr->p_aliases = pdptr->proto_aliases;
198         while (pe->p_aliases[i] && i < (_MAXALIASES-1)) {
199                 if ((n = strlen(pe->p_aliases[i]) + 1) < (eob - cp)) {
200                         strcpy(cp, pe->p_aliases[i]);
201                         pptr->p_aliases[i] = cp;
202                         cp += n;
203                 } else {
204                         break;
205                 }
206                 i++;
207         }
208         pptr->p_aliases[i] = NULL;
209
210         return (PROTO_R_OK);
211 }
212 #endif /* PROTOENT_DATA */
213 #else /* PROTO_R_RETURN */
214         static int getprotoent_r_unknown_system = 0;
215 #endif /* PROTO_R_RETURN */
216 #endif /* !defined(_REENTRANT) || !defined(DO_PTHREADS) */