1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * Copyright (c) 1997-2000 by Sun Microsystems, Inc. 3*0Sstevel@tonic-gate * All rights reserved. 4*0Sstevel@tonic-gate */ 5*0Sstevel@tonic-gate 6*0Sstevel@tonic-gate /* 7*0Sstevel@tonic-gate * Copyright (c) 1989, 1993, 1995 8*0Sstevel@tonic-gate * The Regents of the University of California. All rights reserved. 9*0Sstevel@tonic-gate * 10*0Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 11*0Sstevel@tonic-gate * modification, are permitted provided that the following conditions 12*0Sstevel@tonic-gate * are met: 13*0Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 14*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 15*0Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 16*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 17*0Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 18*0Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software 19*0Sstevel@tonic-gate * must display the following acknowledgement: 20*0Sstevel@tonic-gate * This product includes software developed by the University of 21*0Sstevel@tonic-gate * California, Berkeley and its contributors. 22*0Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors 23*0Sstevel@tonic-gate * may be used to endorse or promote products derived from this software 24*0Sstevel@tonic-gate * without specific prior written permission. 25*0Sstevel@tonic-gate * 26*0Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27*0Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28*0Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29*0Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30*0Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31*0Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32*0Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33*0Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34*0Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35*0Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36*0Sstevel@tonic-gate * SUCH DAMAGE. 37*0Sstevel@tonic-gate */ 38*0Sstevel@tonic-gate 39*0Sstevel@tonic-gate /* 40*0Sstevel@tonic-gate * Portions Copyright (c) 1996,1999 by Internet Software Consortium. 41*0Sstevel@tonic-gate * 42*0Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any 43*0Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above 44*0Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies. 45*0Sstevel@tonic-gate * 46*0Sstevel@tonic-gate * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS 47*0Sstevel@tonic-gate * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES 48*0Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE 49*0Sstevel@tonic-gate * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 50*0Sstevel@tonic-gate * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 51*0Sstevel@tonic-gate * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 52*0Sstevel@tonic-gate * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 53*0Sstevel@tonic-gate * SOFTWARE. 54*0Sstevel@tonic-gate */ 55*0Sstevel@tonic-gate 56*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 57*0Sstevel@tonic-gate 58*0Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint) 59*0Sstevel@tonic-gate static const char rcsid[] = "$Id: lcl_pr.c,v 1.18 1999/10/13 17:11:20 vixie Exp $"; 60*0Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */ 61*0Sstevel@tonic-gate 62*0Sstevel@tonic-gate /* extern */ 63*0Sstevel@tonic-gate 64*0Sstevel@tonic-gate #include "port_before.h" 65*0Sstevel@tonic-gate 66*0Sstevel@tonic-gate #include <sys/types.h> 67*0Sstevel@tonic-gate #include <netinet/in.h> 68*0Sstevel@tonic-gate #include <arpa/nameser.h> 69*0Sstevel@tonic-gate #include <resolv.h> 70*0Sstevel@tonic-gate 71*0Sstevel@tonic-gate #include <errno.h> 72*0Sstevel@tonic-gate #include <fcntl.h> 73*0Sstevel@tonic-gate #include <string.h> 74*0Sstevel@tonic-gate #include <stdio.h> 75*0Sstevel@tonic-gate #include <stdlib.h> 76*0Sstevel@tonic-gate 77*0Sstevel@tonic-gate #include <irs.h> 78*0Sstevel@tonic-gate #include <isc/memcluster.h> 79*0Sstevel@tonic-gate 80*0Sstevel@tonic-gate #include "port_after.h" 81*0Sstevel@tonic-gate 82*0Sstevel@tonic-gate #include "irs_p.h" 83*0Sstevel@tonic-gate #include "lcl_p.h" 84*0Sstevel@tonic-gate 85*0Sstevel@tonic-gate #ifndef _PATH_PROTOCOLS 86*0Sstevel@tonic-gate #define _PATH_PROTOCOLS "/etc/protocols" 87*0Sstevel@tonic-gate #endif 88*0Sstevel@tonic-gate #define MAXALIASES 35 89*0Sstevel@tonic-gate 90*0Sstevel@tonic-gate /* Types */ 91*0Sstevel@tonic-gate 92*0Sstevel@tonic-gate struct pvt { 93*0Sstevel@tonic-gate FILE * fp; 94*0Sstevel@tonic-gate char line[BUFSIZ+1]; 95*0Sstevel@tonic-gate struct protoent proto; 96*0Sstevel@tonic-gate char * proto_aliases[MAXALIASES]; 97*0Sstevel@tonic-gate }; 98*0Sstevel@tonic-gate 99*0Sstevel@tonic-gate /* Forward */ 100*0Sstevel@tonic-gate 101*0Sstevel@tonic-gate static void pr_close(struct irs_pr *); 102*0Sstevel@tonic-gate static struct protoent * pr_next(struct irs_pr *); 103*0Sstevel@tonic-gate static struct protoent * pr_byname(struct irs_pr *, const char *); 104*0Sstevel@tonic-gate static struct protoent * pr_bynumber(struct irs_pr *, int); 105*0Sstevel@tonic-gate static void pr_rewind(struct irs_pr *); 106*0Sstevel@tonic-gate static void pr_minimize(struct irs_pr *); 107*0Sstevel@tonic-gate 108*0Sstevel@tonic-gate /* Portability. */ 109*0Sstevel@tonic-gate 110*0Sstevel@tonic-gate #ifndef SEEK_SET 111*0Sstevel@tonic-gate # define SEEK_SET 0 112*0Sstevel@tonic-gate #endif 113*0Sstevel@tonic-gate 114*0Sstevel@tonic-gate /* Public */ 115*0Sstevel@tonic-gate 116*0Sstevel@tonic-gate struct irs_pr * 117*0Sstevel@tonic-gate irs_lcl_pr(struct irs_acc *this) { 118*0Sstevel@tonic-gate struct irs_pr *pr; 119*0Sstevel@tonic-gate struct pvt *pvt; 120*0Sstevel@tonic-gate 121*0Sstevel@tonic-gate if (!(pr = memget(sizeof *pr))) { 122*0Sstevel@tonic-gate errno = ENOMEM; 123*0Sstevel@tonic-gate return (NULL); 124*0Sstevel@tonic-gate } 125*0Sstevel@tonic-gate if (!(pvt = memget(sizeof *pvt))) { 126*0Sstevel@tonic-gate memput(pr, sizeof *this); 127*0Sstevel@tonic-gate errno = ENOMEM; 128*0Sstevel@tonic-gate return (NULL); 129*0Sstevel@tonic-gate } 130*0Sstevel@tonic-gate memset(pvt, 0, sizeof *pvt); 131*0Sstevel@tonic-gate pr->private = pvt; 132*0Sstevel@tonic-gate pr->close = pr_close; 133*0Sstevel@tonic-gate pr->byname = pr_byname; 134*0Sstevel@tonic-gate pr->bynumber = pr_bynumber; 135*0Sstevel@tonic-gate pr->next = pr_next; 136*0Sstevel@tonic-gate pr->rewind = pr_rewind; 137*0Sstevel@tonic-gate pr->minimize = pr_minimize; 138*0Sstevel@tonic-gate pr->res_get = NULL; 139*0Sstevel@tonic-gate pr->res_set = NULL; 140*0Sstevel@tonic-gate return (pr); 141*0Sstevel@tonic-gate } 142*0Sstevel@tonic-gate 143*0Sstevel@tonic-gate /* Methods */ 144*0Sstevel@tonic-gate 145*0Sstevel@tonic-gate static void 146*0Sstevel@tonic-gate pr_close(struct irs_pr *this) { 147*0Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 148*0Sstevel@tonic-gate 149*0Sstevel@tonic-gate if (pvt->fp) 150*0Sstevel@tonic-gate (void) fclose(pvt->fp); 151*0Sstevel@tonic-gate memput(pvt, sizeof *pvt); 152*0Sstevel@tonic-gate memput(this, sizeof *this); 153*0Sstevel@tonic-gate } 154*0Sstevel@tonic-gate 155*0Sstevel@tonic-gate static struct protoent * 156*0Sstevel@tonic-gate pr_byname(struct irs_pr *this, const char *name) { 157*0Sstevel@tonic-gate 158*0Sstevel@tonic-gate struct protoent *p; 159*0Sstevel@tonic-gate char **cp; 160*0Sstevel@tonic-gate 161*0Sstevel@tonic-gate pr_rewind(this); 162*0Sstevel@tonic-gate while ((p = pr_next(this))) { 163*0Sstevel@tonic-gate if (!strcmp(p->p_name, name)) 164*0Sstevel@tonic-gate goto found; 165*0Sstevel@tonic-gate for (cp = p->p_aliases; *cp; cp++) 166*0Sstevel@tonic-gate if (!strcmp(*cp, name)) 167*0Sstevel@tonic-gate goto found; 168*0Sstevel@tonic-gate } 169*0Sstevel@tonic-gate found: 170*0Sstevel@tonic-gate return (p); 171*0Sstevel@tonic-gate } 172*0Sstevel@tonic-gate 173*0Sstevel@tonic-gate static struct protoent * 174*0Sstevel@tonic-gate pr_bynumber(struct irs_pr *this, int proto) { 175*0Sstevel@tonic-gate struct protoent *p; 176*0Sstevel@tonic-gate 177*0Sstevel@tonic-gate pr_rewind(this); 178*0Sstevel@tonic-gate while ((p = pr_next(this))) 179*0Sstevel@tonic-gate if (p->p_proto == proto) 180*0Sstevel@tonic-gate break; 181*0Sstevel@tonic-gate return (p); 182*0Sstevel@tonic-gate } 183*0Sstevel@tonic-gate 184*0Sstevel@tonic-gate static void 185*0Sstevel@tonic-gate pr_rewind(struct irs_pr *this) { 186*0Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 187*0Sstevel@tonic-gate 188*0Sstevel@tonic-gate if (pvt->fp) { 189*0Sstevel@tonic-gate if (fseek(pvt->fp, 0L, SEEK_SET) == 0) 190*0Sstevel@tonic-gate return; 191*0Sstevel@tonic-gate (void)fclose(pvt->fp); 192*0Sstevel@tonic-gate } 193*0Sstevel@tonic-gate if (!(pvt->fp = fopen(_PATH_PROTOCOLS, "r" ))) 194*0Sstevel@tonic-gate return; 195*0Sstevel@tonic-gate if (fcntl(fileno(pvt->fp), F_SETFD, 1) < 0) { 196*0Sstevel@tonic-gate (void)fclose(pvt->fp); 197*0Sstevel@tonic-gate pvt->fp = NULL; 198*0Sstevel@tonic-gate } 199*0Sstevel@tonic-gate } 200*0Sstevel@tonic-gate 201*0Sstevel@tonic-gate static struct protoent * 202*0Sstevel@tonic-gate pr_next(struct irs_pr *this) { 203*0Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 204*0Sstevel@tonic-gate char *p, *cp, **q; 205*0Sstevel@tonic-gate char *bufp, *ndbuf, *dbuf = NULL; 206*0Sstevel@tonic-gate int c, bufsiz, offset; 207*0Sstevel@tonic-gate 208*0Sstevel@tonic-gate if (!pvt->fp) 209*0Sstevel@tonic-gate pr_rewind(this); 210*0Sstevel@tonic-gate if (!pvt->fp) 211*0Sstevel@tonic-gate return (NULL); 212*0Sstevel@tonic-gate bufp = pvt->line; 213*0Sstevel@tonic-gate bufsiz = BUFSIZ; 214*0Sstevel@tonic-gate offset = 0; 215*0Sstevel@tonic-gate again: 216*0Sstevel@tonic-gate if ((p = fgets(bufp + offset, bufsiz - offset, pvt->fp)) == NULL) { 217*0Sstevel@tonic-gate if (dbuf) 218*0Sstevel@tonic-gate free(dbuf); 219*0Sstevel@tonic-gate return (NULL); 220*0Sstevel@tonic-gate } 221*0Sstevel@tonic-gate if (!strchr(p, '\n') && !feof(pvt->fp)) { 222*0Sstevel@tonic-gate #define GROWBUF 1024 223*0Sstevel@tonic-gate /* allocate space for longer line */ 224*0Sstevel@tonic-gate if (dbuf == NULL) { 225*0Sstevel@tonic-gate if ((ndbuf = malloc(bufsiz + GROWBUF)) != NULL) 226*0Sstevel@tonic-gate strcpy(ndbuf, bufp); 227*0Sstevel@tonic-gate } else 228*0Sstevel@tonic-gate ndbuf = realloc(dbuf, bufsiz + GROWBUF); 229*0Sstevel@tonic-gate if (ndbuf) { 230*0Sstevel@tonic-gate dbuf = ndbuf; 231*0Sstevel@tonic-gate bufp = dbuf; 232*0Sstevel@tonic-gate bufsiz += GROWBUF; 233*0Sstevel@tonic-gate offset = strlen(dbuf); 234*0Sstevel@tonic-gate } else { 235*0Sstevel@tonic-gate /* allocation failed; skip this long line */ 236*0Sstevel@tonic-gate while ((c = getc(pvt->fp)) != EOF) 237*0Sstevel@tonic-gate if (c == '\n') 238*0Sstevel@tonic-gate break; 239*0Sstevel@tonic-gate if (c != EOF) 240*0Sstevel@tonic-gate ungetc(c, pvt->fp); 241*0Sstevel@tonic-gate } 242*0Sstevel@tonic-gate goto again; 243*0Sstevel@tonic-gate } 244*0Sstevel@tonic-gate 245*0Sstevel@tonic-gate p -= offset; 246*0Sstevel@tonic-gate offset = 0; 247*0Sstevel@tonic-gate 248*0Sstevel@tonic-gate if (*p == '#') 249*0Sstevel@tonic-gate goto again; 250*0Sstevel@tonic-gate cp = strpbrk(p, "#\n"); 251*0Sstevel@tonic-gate if (cp != NULL) 252*0Sstevel@tonic-gate *cp = '\0'; 253*0Sstevel@tonic-gate pvt->proto.p_name = p; 254*0Sstevel@tonic-gate cp = strpbrk(p, " \t"); 255*0Sstevel@tonic-gate if (cp == NULL) 256*0Sstevel@tonic-gate goto again; 257*0Sstevel@tonic-gate *cp++ = '\0'; 258*0Sstevel@tonic-gate while (*cp == ' ' || *cp == '\t') 259*0Sstevel@tonic-gate cp++; 260*0Sstevel@tonic-gate p = strpbrk(cp, " \t"); 261*0Sstevel@tonic-gate if (p != NULL) 262*0Sstevel@tonic-gate *p++ = '\0'; 263*0Sstevel@tonic-gate pvt->proto.p_proto = atoi(cp); 264*0Sstevel@tonic-gate q = pvt->proto.p_aliases = pvt->proto_aliases; 265*0Sstevel@tonic-gate if (p != NULL) { 266*0Sstevel@tonic-gate cp = p; 267*0Sstevel@tonic-gate while (cp && *cp) { 268*0Sstevel@tonic-gate if (*cp == ' ' || *cp == '\t') { 269*0Sstevel@tonic-gate cp++; 270*0Sstevel@tonic-gate continue; 271*0Sstevel@tonic-gate } 272*0Sstevel@tonic-gate if (q < &pvt->proto_aliases[MAXALIASES - 1]) 273*0Sstevel@tonic-gate *q++ = cp; 274*0Sstevel@tonic-gate cp = strpbrk(cp, " \t"); 275*0Sstevel@tonic-gate if (cp != NULL) 276*0Sstevel@tonic-gate *cp++ = '\0'; 277*0Sstevel@tonic-gate } 278*0Sstevel@tonic-gate } 279*0Sstevel@tonic-gate *q = NULL; 280*0Sstevel@tonic-gate return (&pvt->proto); 281*0Sstevel@tonic-gate } 282*0Sstevel@tonic-gate 283*0Sstevel@tonic-gate static void 284*0Sstevel@tonic-gate pr_minimize(struct irs_pr *this) { 285*0Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private; 286*0Sstevel@tonic-gate 287*0Sstevel@tonic-gate if (pvt->fp != NULL) { 288*0Sstevel@tonic-gate (void)fclose(pvt->fp); 289*0Sstevel@tonic-gate pvt->fp = NULL; 290*0Sstevel@tonic-gate } 291*0Sstevel@tonic-gate } 292