xref: /freebsd/lib/libc/net/getprotoent.c (revision 9dba3024c3f1a2df6f42689aac5a2ab4acc7561d)
1 /*
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid[] = "@(#)getprotoent.c	8.1 (Berkeley) 6/4/93";
36 #endif /* LIBC_SCCS and not lint */
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <netdb.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include "namespace.h"
47 #include "reentrant.h"
48 #include "un-namespace.h"
49 #include "netdb_private.h"
50 
51 static struct protodata protodata;
52 static thread_key_t protodata_key;
53 static once_t protodata_init_once = ONCE_INITIALIZER;
54 static int protodata_thr_keycreated = 0;
55 
56 static void
57 protoent_data_clear(struct protoent_data *ped)
58 {
59 	if (ped->fp) {
60 		fclose(ped->fp);
61 		ped->fp = NULL;
62 	}
63 }
64 
65 static void
66 protodata_free(void *ptr)
67 {
68 	struct protodata *pd = ptr;
69 
70 	if (pd == NULL)
71 		return;
72 	protoent_data_clear(&pd->data);
73 	free(pd);
74 }
75 
76 static void
77 protodata_keycreate(void)
78 {
79 	protodata_thr_keycreated =
80 	    (thr_keycreate(&protodata_key, protodata_free) == 0);
81 }
82 
83 struct protodata *
84 __protodata_init(void)
85 {
86 	struct protodata *pd;
87 
88 	if (thr_main() != 0)
89 		return (&protodata);
90 	if (thr_once(&protodata_init_once, protodata_keycreate) != 0 ||
91 	    !protodata_thr_keycreated)
92 		return (NULL);
93 	if ((pd = thr_getspecific(protodata_key)) != NULL)
94 		return (pd);
95 	if ((pd = calloc(1, sizeof(*pd))) == NULL)
96 		return (NULL);
97 	if (thr_setspecific(protodata_key, pd) == 0)
98 		return (pd);
99 	free(pd);
100 	return (NULL);
101 }
102 
103 void
104 setprotoent_r(int f, struct protoent_data *ped)
105 {
106 	if (ped->fp == NULL)
107 		ped->fp = fopen(_PATH_PROTOCOLS, "r");
108 	else
109 		rewind(ped->fp);
110 	ped->stayopen |= f;
111 }
112 
113 void
114 endprotoent_r(struct protoent_data *ped)
115 {
116 	if (ped->fp) {
117 		fclose(ped->fp);
118 		ped->fp = NULL;
119 	}
120 	ped->stayopen = 0;
121 }
122 
123 int
124 getprotoent_r(struct protoent *pe, struct protoent_data *ped)
125 {
126 	char *p;
127 	char *cp, **q, *endp;
128 	long l;
129 
130 	if (ped->fp == NULL && (ped->fp = fopen(_PATH_PROTOCOLS, "r")) == NULL)
131 		return (-1);
132 again:
133 	if ((p = fgets(ped->line, sizeof ped->line, ped->fp)) == NULL)
134 		return (-1);
135 	if (*p == '#')
136 		goto again;
137 	cp = strpbrk(p, "#\n");
138 	if (cp != NULL)
139 		*cp = '\0';
140 	pe->p_name = p;
141 	cp = strpbrk(p, " \t");
142 	if (cp == NULL)
143 		goto again;
144 	*cp++ = '\0';
145 	while (*cp == ' ' || *cp == '\t')
146 		cp++;
147 	p = strpbrk(cp, " \t");
148 	if (p != NULL)
149 		*p++ = '\0';
150 	l = strtol(cp, &endp, 10);
151 	if (endp == cp || *endp != '\0' || l < 0 || l > USHRT_MAX)
152 		goto again;
153 	pe->p_proto = l;
154 	q = pe->p_aliases = ped->aliases;
155 	if (p != NULL) {
156 		cp = p;
157 		while (cp && *cp) {
158 			if (*cp == ' ' || *cp == '\t') {
159 				cp++;
160 				continue;
161 			}
162 			if (q < &ped->aliases[_MAXALIASES - 1])
163 				*q++ = cp;
164 			cp = strpbrk(cp, " \t");
165 			if (cp != NULL)
166 				*cp++ = '\0';
167 		}
168 	}
169 	*q = NULL;
170 	return (0);
171 }
172 
173 void
174 setprotoent(int f)
175 {
176 	struct protodata *pd;
177 
178 	if ((pd = __protodata_init()) == NULL)
179 		return;
180 	setprotoent_r(f, &pd->data);
181 }
182 
183 void
184 endprotoent(void)
185 {
186 	struct protodata *pd;
187 
188 	if ((pd = __protodata_init()) == NULL)
189 		return;
190 	endprotoent_r(&pd->data);
191 }
192 
193 struct protoent *
194 getprotoent(void)
195 {
196 	struct protodata *pd;
197 
198 	if ((pd = __protodata_init()) == NULL)
199 		return (NULL);
200 	if (getprotoent_r(&pd->proto, &pd->data) != 0)
201 		return (NULL);
202 	return (&pd->proto);
203 }
204