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