17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * Copyright (c) 1989, 1993, 1995
37c478bd9Sstevel@tonic-gate * The Regents of the University of California. All rights reserved.
47c478bd9Sstevel@tonic-gate *
57c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
67c478bd9Sstevel@tonic-gate * modification, are permitted provided that the following conditions
77c478bd9Sstevel@tonic-gate * are met:
87c478bd9Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
97c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
107c478bd9Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
117c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
127c478bd9Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
137c478bd9Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
147c478bd9Sstevel@tonic-gate * must display the following acknowledgement:
157c478bd9Sstevel@tonic-gate * This product includes software developed by the University of
167c478bd9Sstevel@tonic-gate * California, Berkeley and its contributors.
177c478bd9Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors
187c478bd9Sstevel@tonic-gate * may be used to endorse or promote products derived from this software
197c478bd9Sstevel@tonic-gate * without specific prior written permission.
207c478bd9Sstevel@tonic-gate *
217c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
227c478bd9Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
237c478bd9Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
247c478bd9Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
257c478bd9Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
267c478bd9Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
277c478bd9Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
287c478bd9Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
297c478bd9Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
307c478bd9Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
317c478bd9Sstevel@tonic-gate * SUCH DAMAGE.
327c478bd9Sstevel@tonic-gate */
337c478bd9Sstevel@tonic-gate
347c478bd9Sstevel@tonic-gate /*
359525b14bSRao Shoaib * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
367c478bd9Sstevel@tonic-gate * Portions Copyright (c) 1996,1999 by Internet Software Consortium.
377c478bd9Sstevel@tonic-gate *
387c478bd9Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any
397c478bd9Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above
407c478bd9Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies.
417c478bd9Sstevel@tonic-gate *
429525b14bSRao Shoaib * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
439525b14bSRao Shoaib * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
449525b14bSRao Shoaib * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
459525b14bSRao Shoaib * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
469525b14bSRao Shoaib * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
479525b14bSRao Shoaib * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
489525b14bSRao Shoaib * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
497c478bd9Sstevel@tonic-gate */
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate #include "port_before.h"
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate #include <sys/types.h>
547c478bd9Sstevel@tonic-gate #include <netinet/in.h>
557c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
567c478bd9Sstevel@tonic-gate #include <resolv.h>
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate #include <errno.h>
597c478bd9Sstevel@tonic-gate #include <fcntl.h>
607c478bd9Sstevel@tonic-gate #include <string.h>
617c478bd9Sstevel@tonic-gate #include <stdio.h>
627c478bd9Sstevel@tonic-gate #include <stdlib.h>
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate #include <irs.h>
657c478bd9Sstevel@tonic-gate #include <isc/memcluster.h>
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate #include "port_after.h"
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate #include "irs_p.h"
707c478bd9Sstevel@tonic-gate #include "lcl_p.h"
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate #ifndef _PATH_PROTOCOLS
737c478bd9Sstevel@tonic-gate #define _PATH_PROTOCOLS "/etc/protocols"
747c478bd9Sstevel@tonic-gate #endif
757c478bd9Sstevel@tonic-gate #define MAXALIASES 35
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate /* Types */
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate struct pvt {
807c478bd9Sstevel@tonic-gate FILE * fp;
817c478bd9Sstevel@tonic-gate char line[BUFSIZ+1];
829525b14bSRao Shoaib char * dbuf;
837c478bd9Sstevel@tonic-gate struct protoent proto;
847c478bd9Sstevel@tonic-gate char * proto_aliases[MAXALIASES];
857c478bd9Sstevel@tonic-gate };
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate /* Forward */
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate static void pr_close(struct irs_pr *);
907c478bd9Sstevel@tonic-gate static struct protoent * pr_next(struct irs_pr *);
917c478bd9Sstevel@tonic-gate static struct protoent * pr_byname(struct irs_pr *, const char *);
927c478bd9Sstevel@tonic-gate static struct protoent * pr_bynumber(struct irs_pr *, int);
937c478bd9Sstevel@tonic-gate static void pr_rewind(struct irs_pr *);
947c478bd9Sstevel@tonic-gate static void pr_minimize(struct irs_pr *);
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate /* Portability. */
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate #ifndef SEEK_SET
997c478bd9Sstevel@tonic-gate # define SEEK_SET 0
1007c478bd9Sstevel@tonic-gate #endif
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate /* Public */
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate struct irs_pr *
irs_lcl_pr(struct irs_acc * this)1057c478bd9Sstevel@tonic-gate irs_lcl_pr(struct irs_acc *this) {
1067c478bd9Sstevel@tonic-gate struct irs_pr *pr;
1077c478bd9Sstevel@tonic-gate struct pvt *pvt;
108*55fea89dSDan Cross
1097c478bd9Sstevel@tonic-gate if (!(pr = memget(sizeof *pr))) {
1107c478bd9Sstevel@tonic-gate errno = ENOMEM;
1117c478bd9Sstevel@tonic-gate return (NULL);
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate if (!(pvt = memget(sizeof *pvt))) {
1147c478bd9Sstevel@tonic-gate memput(pr, sizeof *this);
1157c478bd9Sstevel@tonic-gate errno = ENOMEM;
1167c478bd9Sstevel@tonic-gate return (NULL);
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate memset(pvt, 0, sizeof *pvt);
1197c478bd9Sstevel@tonic-gate pr->private = pvt;
1207c478bd9Sstevel@tonic-gate pr->close = pr_close;
1217c478bd9Sstevel@tonic-gate pr->byname = pr_byname;
1227c478bd9Sstevel@tonic-gate pr->bynumber = pr_bynumber;
1237c478bd9Sstevel@tonic-gate pr->next = pr_next;
1247c478bd9Sstevel@tonic-gate pr->rewind = pr_rewind;
1257c478bd9Sstevel@tonic-gate pr->minimize = pr_minimize;
1267c478bd9Sstevel@tonic-gate pr->res_get = NULL;
1277c478bd9Sstevel@tonic-gate pr->res_set = NULL;
1287c478bd9Sstevel@tonic-gate return (pr);
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate /* Methods */
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate static void
pr_close(struct irs_pr * this)1347c478bd9Sstevel@tonic-gate pr_close(struct irs_pr *this) {
1357c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate if (pvt->fp)
1387c478bd9Sstevel@tonic-gate (void) fclose(pvt->fp);
1399525b14bSRao Shoaib if (pvt->dbuf)
1409525b14bSRao Shoaib free(pvt->dbuf);
1417c478bd9Sstevel@tonic-gate memput(pvt, sizeof *pvt);
1427c478bd9Sstevel@tonic-gate memput(this, sizeof *this);
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate static struct protoent *
pr_byname(struct irs_pr * this,const char * name)1467c478bd9Sstevel@tonic-gate pr_byname(struct irs_pr *this, const char *name) {
147*55fea89dSDan Cross
1487c478bd9Sstevel@tonic-gate struct protoent *p;
1497c478bd9Sstevel@tonic-gate char **cp;
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate pr_rewind(this);
1527c478bd9Sstevel@tonic-gate while ((p = pr_next(this))) {
1537c478bd9Sstevel@tonic-gate if (!strcmp(p->p_name, name))
1547c478bd9Sstevel@tonic-gate goto found;
1557c478bd9Sstevel@tonic-gate for (cp = p->p_aliases; *cp; cp++)
1567c478bd9Sstevel@tonic-gate if (!strcmp(*cp, name))
1577c478bd9Sstevel@tonic-gate goto found;
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate found:
1607c478bd9Sstevel@tonic-gate return (p);
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate
1637c478bd9Sstevel@tonic-gate static struct protoent *
pr_bynumber(struct irs_pr * this,int proto)1647c478bd9Sstevel@tonic-gate pr_bynumber(struct irs_pr *this, int proto) {
1657c478bd9Sstevel@tonic-gate struct protoent *p;
1667c478bd9Sstevel@tonic-gate
1677c478bd9Sstevel@tonic-gate pr_rewind(this);
1687c478bd9Sstevel@tonic-gate while ((p = pr_next(this)))
1697c478bd9Sstevel@tonic-gate if (p->p_proto == proto)
1707c478bd9Sstevel@tonic-gate break;
1717c478bd9Sstevel@tonic-gate return (p);
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate
1747c478bd9Sstevel@tonic-gate static void
pr_rewind(struct irs_pr * this)1757c478bd9Sstevel@tonic-gate pr_rewind(struct irs_pr *this) {
1767c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
177*55fea89dSDan Cross
1787c478bd9Sstevel@tonic-gate if (pvt->fp) {
1797c478bd9Sstevel@tonic-gate if (fseek(pvt->fp, 0L, SEEK_SET) == 0)
1807c478bd9Sstevel@tonic-gate return;
1817c478bd9Sstevel@tonic-gate (void)fclose(pvt->fp);
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate if (!(pvt->fp = fopen(_PATH_PROTOCOLS, "r" )))
1847c478bd9Sstevel@tonic-gate return;
1857c478bd9Sstevel@tonic-gate if (fcntl(fileno(pvt->fp), F_SETFD, 1) < 0) {
1867c478bd9Sstevel@tonic-gate (void)fclose(pvt->fp);
1877c478bd9Sstevel@tonic-gate pvt->fp = NULL;
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate static struct protoent *
pr_next(struct irs_pr * this)1927c478bd9Sstevel@tonic-gate pr_next(struct irs_pr *this) {
1937c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
1947c478bd9Sstevel@tonic-gate char *p, *cp, **q;
1957c478bd9Sstevel@tonic-gate char *bufp, *ndbuf, *dbuf = NULL;
1967c478bd9Sstevel@tonic-gate int c, bufsiz, offset;
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate if (!pvt->fp)
1997c478bd9Sstevel@tonic-gate pr_rewind(this);
2007c478bd9Sstevel@tonic-gate if (!pvt->fp)
2017c478bd9Sstevel@tonic-gate return (NULL);
2029525b14bSRao Shoaib if (pvt->dbuf) {
2039525b14bSRao Shoaib free(pvt->dbuf);
2049525b14bSRao Shoaib pvt->dbuf = NULL;
2059525b14bSRao Shoaib }
2067c478bd9Sstevel@tonic-gate bufp = pvt->line;
2077c478bd9Sstevel@tonic-gate bufsiz = BUFSIZ;
2087c478bd9Sstevel@tonic-gate offset = 0;
2097c478bd9Sstevel@tonic-gate again:
2107c478bd9Sstevel@tonic-gate if ((p = fgets(bufp + offset, bufsiz - offset, pvt->fp)) == NULL) {
2117c478bd9Sstevel@tonic-gate if (dbuf)
2127c478bd9Sstevel@tonic-gate free(dbuf);
2137c478bd9Sstevel@tonic-gate return (NULL);
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate if (!strchr(p, '\n') && !feof(pvt->fp)) {
2167c478bd9Sstevel@tonic-gate #define GROWBUF 1024
2177c478bd9Sstevel@tonic-gate /* allocate space for longer line */
2187c478bd9Sstevel@tonic-gate if (dbuf == NULL) {
2197c478bd9Sstevel@tonic-gate if ((ndbuf = malloc(bufsiz + GROWBUF)) != NULL)
2207c478bd9Sstevel@tonic-gate strcpy(ndbuf, bufp);
2217c478bd9Sstevel@tonic-gate } else
2227c478bd9Sstevel@tonic-gate ndbuf = realloc(dbuf, bufsiz + GROWBUF);
2237c478bd9Sstevel@tonic-gate if (ndbuf) {
2247c478bd9Sstevel@tonic-gate dbuf = ndbuf;
2257c478bd9Sstevel@tonic-gate bufp = dbuf;
2267c478bd9Sstevel@tonic-gate bufsiz += GROWBUF;
2277c478bd9Sstevel@tonic-gate offset = strlen(dbuf);
2287c478bd9Sstevel@tonic-gate } else {
2297c478bd9Sstevel@tonic-gate /* allocation failed; skip this long line */
2307c478bd9Sstevel@tonic-gate while ((c = getc(pvt->fp)) != EOF)
2317c478bd9Sstevel@tonic-gate if (c == '\n')
2327c478bd9Sstevel@tonic-gate break;
2337c478bd9Sstevel@tonic-gate if (c != EOF)
2347c478bd9Sstevel@tonic-gate ungetc(c, pvt->fp);
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate goto again;
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate
2397c478bd9Sstevel@tonic-gate p -= offset;
2407c478bd9Sstevel@tonic-gate offset = 0;
2417c478bd9Sstevel@tonic-gate
2427c478bd9Sstevel@tonic-gate if (*p == '#')
2437c478bd9Sstevel@tonic-gate goto again;
2447c478bd9Sstevel@tonic-gate cp = strpbrk(p, "#\n");
2457c478bd9Sstevel@tonic-gate if (cp != NULL)
2467c478bd9Sstevel@tonic-gate *cp = '\0';
2477c478bd9Sstevel@tonic-gate pvt->proto.p_name = p;
2487c478bd9Sstevel@tonic-gate cp = strpbrk(p, " \t");
2497c478bd9Sstevel@tonic-gate if (cp == NULL)
2507c478bd9Sstevel@tonic-gate goto again;
2517c478bd9Sstevel@tonic-gate *cp++ = '\0';
2527c478bd9Sstevel@tonic-gate while (*cp == ' ' || *cp == '\t')
2537c478bd9Sstevel@tonic-gate cp++;
2547c478bd9Sstevel@tonic-gate p = strpbrk(cp, " \t");
2557c478bd9Sstevel@tonic-gate if (p != NULL)
2567c478bd9Sstevel@tonic-gate *p++ = '\0';
2577c478bd9Sstevel@tonic-gate pvt->proto.p_proto = atoi(cp);
2587c478bd9Sstevel@tonic-gate q = pvt->proto.p_aliases = pvt->proto_aliases;
2597c478bd9Sstevel@tonic-gate if (p != NULL) {
2607c478bd9Sstevel@tonic-gate cp = p;
2617c478bd9Sstevel@tonic-gate while (cp && *cp) {
2627c478bd9Sstevel@tonic-gate if (*cp == ' ' || *cp == '\t') {
2637c478bd9Sstevel@tonic-gate cp++;
2647c478bd9Sstevel@tonic-gate continue;
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate if (q < &pvt->proto_aliases[MAXALIASES - 1])
2677c478bd9Sstevel@tonic-gate *q++ = cp;
2687c478bd9Sstevel@tonic-gate cp = strpbrk(cp, " \t");
2697c478bd9Sstevel@tonic-gate if (cp != NULL)
2707c478bd9Sstevel@tonic-gate *cp++ = '\0';
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate *q = NULL;
2749525b14bSRao Shoaib pvt->dbuf = dbuf;
2757c478bd9Sstevel@tonic-gate return (&pvt->proto);
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate
2787c478bd9Sstevel@tonic-gate static void
pr_minimize(struct irs_pr * this)2797c478bd9Sstevel@tonic-gate pr_minimize(struct irs_pr *this) {
2807c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
2817c478bd9Sstevel@tonic-gate
2827c478bd9Sstevel@tonic-gate if (pvt->fp != NULL) {
2837c478bd9Sstevel@tonic-gate (void)fclose(pvt->fp);
2847c478bd9Sstevel@tonic-gate pvt->fp = NULL;
2857c478bd9Sstevel@tonic-gate }
2867c478bd9Sstevel@tonic-gate }
2879525b14bSRao Shoaib
2889525b14bSRao Shoaib /*! \file */
289