xref: /openbsd-src/lib/libc/net/getprotoent.c (revision e3a1e9bf34eea155696e6eec761a1bf8eceb7daa)
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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #if defined(LIBC_SCCS) && !defined(lint)
31 static char rcsid[] = "$OpenBSD: getprotoent.c,v 1.7 2004/10/25 03:09:01 millert Exp $";
32 #endif /* LIBC_SCCS and not lint */
33 
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 
37 #include <errno.h>
38 #include <netdb.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 
43 void
44 setprotoent_r(int f, struct protoent_data *pd)
45 {
46 	if (pd->fp == NULL)
47 		pd->fp = fopen(_PATH_PROTOCOLS, "r" );
48 	else
49 		rewind(pd->fp);
50 	pd->stayopen |= f;
51 }
52 
53 void
54 endprotoent_r(struct protoent_data *pd)
55 {
56 	if (pd->fp) {
57 		fclose(pd->fp);
58 		pd->fp = NULL;
59 	}
60 	free(pd->aliases);
61 	pd->aliases = NULL;
62 	pd->maxaliases = 0;
63 	free(pd->line);
64 	pd->line = NULL;
65 	pd->stayopen = 0;
66 }
67 
68 int
69 getprotoent_r(struct protoent *pe, struct protoent_data *pd)
70 {
71 	char *p, *cp, **q, *endp;
72 	size_t len;
73 	long l;
74 	int serrno;
75 
76 	if (pd->fp == NULL && (pd->fp = fopen(_PATH_PROTOCOLS, "r" )) == NULL)
77 		return (-1);
78 again:
79 	if ((p = fgetln(pd->fp, &len)) == NULL)
80 		return (-1);
81 	if (len == 0 || *p == '#' || *p == '\n')
82 		goto again;
83 	if (p[len-1] == '\n')
84 		len--;
85 	if ((cp = memchr(p, '#', len)) != NULL)
86 		len = cp - p;
87 	cp = realloc(pd->line, len + 1);
88 	if (cp == NULL)
89 		return (-1);
90 	pd->line = pe->p_name = memcpy(cp, p, len);
91 	cp[len] = '\0';
92 	cp = strpbrk(cp, " \t");
93 	if (cp == NULL)
94 		goto again;
95 	*cp++ = '\0';
96 	while (*cp == ' ' || *cp == '\t')
97 		cp++;
98 	p = strpbrk(cp, " \t");
99 	if (p != NULL)
100 		*p++ = '\0';
101 	l = strtol(cp, &endp, 10);
102 	if (endp == cp || *endp != '\0' || l < 0 || l >= INT_MAX)
103 		goto again;
104 	pe->p_proto = l;
105 	if (pd->aliases == NULL) {
106 		pd->maxaliases = 5;
107 		pd->aliases = malloc(pd->maxaliases * sizeof(char *));
108 		if (pd->aliases == NULL) {
109 			serrno = errno;
110 			endprotoent_r(pd);
111 			errno = serrno;
112 			return (-1);
113 		}
114 	}
115 	q = pe->p_aliases = pd->aliases;
116 	if (p != NULL) {
117 		cp = p;
118 		while (cp && *cp) {
119 			if (*cp == ' ' || *cp == '\t') {
120 				cp++;
121 				continue;
122 			}
123 			if (q == &pe->p_aliases[pd->maxaliases - 1]) {
124 				p = realloc(pe->p_aliases,
125 				    2 * pd->maxaliases * sizeof(char *));
126 				if (p == NULL) {
127 					serrno = errno;
128 					endprotoent_r(pd);
129 					errno = serrno;
130 					return (-1);
131 				}
132 				pd->maxaliases *= 2;
133 				q = (char **)p + (q - pe->p_aliases);
134 				pe->p_aliases = pd->aliases = (char **)p;
135 			}
136 			*q++ = cp;
137 			cp = strpbrk(cp, " \t");
138 			if (cp != NULL)
139 				*cp++ = '\0';
140 		}
141 	}
142 	*q = NULL;
143 	return (0);
144 }
145 
146 struct protoent_data _protoent_data;	/* shared with getproto{,name}.c */
147 
148 void
149 setprotoent(int f)
150 {
151 	setprotoent_r(f, &_protoent_data);
152 }
153 
154 void
155 endprotoent(void)
156 {
157 	endprotoent_r(&_protoent_data);
158 }
159 
160 struct protoent *
161 getprotoent(void)
162 {
163 	static struct protoent proto;
164 
165 	if (getprotoent_r(&proto, &_protoent_data) != 0)
166 		return (NULL);
167 	return (&proto);
168 }
169