2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 * @(#)getservent.c 8.1 (Berkeley) 6/4/93
30 * $DragonFly: src/lib/libc/net/getservent.c,v 1.7 2005/11/13 02:04:47 swildner Exp $
33 #include <sys/types.h>
34 #include <sys/socket.h>
41 #include <rpcsvc/yp_prot.h>
42 #include <rpcsvc/ypclnt.h>
43 static int serv_stepping_yp = 0;
44 extern int _yp_check ( char ** );
50 static FILE *servf = NULL;
51 static char line[BUFSIZ+1];
52 static struct servent serv;
53 static char *serv_aliases[MAXALIASES];
57 char *___getservbyname_yp = NULL;
58 char *___getservbyproto_yp = NULL;
59 int ___getservbyport_yp = 0;
60 static char *yp_domain = NULL;
63 _getservbyport_yp(char *line)
67 char buf[YPMAXRECORD + 2];
70 snprintf(buf, sizeof(buf), "%d/%s", ntohs(___getservbyport_yp),
71 ___getservbyproto_yp);
73 ___getservbyport_yp = 0;
74 ___getservbyproto_yp = NULL;
77 if(yp_get_default_domain(&yp_domain))
82 * We have to be a little flexible here. Ideally you're supposed
83 * to have both a services.byname and a services.byport map, but
84 * some systems have only services.byname. FreeBSD cheats a little
85 * by putting the services.byport information in the same map as
86 * services.byname so that either case will work. We allow for both
87 * possibilities here: if there is no services.byport map, we try
88 * services.byname instead.
90 if ((rv = yp_match(yp_domain, "services.byport", buf, strlen(buf),
91 &result, &resultlen))) {
92 if (rv == YPERR_MAP) {
93 if (yp_match(yp_domain, "services.byname", buf,
94 strlen(buf), &result, &resultlen))
100 /* getservent() expects lines terminated with \n -- make it happy */
101 snprintf(line, BUFSIZ, "%.*s\n", resultlen, result);
108 _getservbyname_yp(char *line)
112 char buf[YPMAXRECORD + 2];
115 if(yp_get_default_domain(&yp_domain))
119 snprintf(buf, sizeof(buf), "%s/%s", ___getservbyname_yp,
120 ___getservbyproto_yp);
122 ___getservbyname_yp = 0;
123 ___getservbyproto_yp = NULL;
125 if (yp_match(yp_domain, "services.byname", buf, strlen(buf),
126 &result, &resultlen)) {
130 /* getservent() expects lines terminated with \n -- make it happy */
131 snprintf(line, BUFSIZ, "%.*s\n", resultlen, result);
138 _getservent_yp(char *line)
140 static char *key = NULL;
142 char *lastkey, *result;
147 if(yp_get_default_domain(&yp_domain))
151 if (!serv_stepping_yp) {
154 if ((rv = yp_first(yp_domain, "services.byname", &key, &keylen,
155 &result, &resultlen))) {
156 serv_stepping_yp = 0;
159 serv_stepping_yp = 1;
162 rv = yp_next(yp_domain, "services.byname", key, keylen, &key,
163 &keylen, &result, &resultlen);
166 serv_stepping_yp = 0;
171 /* getservent() expects lines terminated with \n -- make it happy */
172 snprintf(line, BUFSIZ, "%.*s\n", resultlen, result);
184 servf = fopen(_PATH_SERVICES, "r" );
207 if (serv_stepping_yp && _getservent_yp(line)) {
213 if (servf == NULL && (servf = fopen(_PATH_SERVICES, "r" )) == NULL)
216 if ((p = fgets(line, BUFSIZ, servf)) == NULL)
219 if (*p == '+' && _yp_check(NULL)) {
220 if (___getservbyname_yp != NULL) {
221 if (!_getservbyname_yp(line))
224 else if (___getservbyport_yp != 0) {
225 if (!_getservbyport_yp(line))
228 else if (!_getservent_yp(line))
235 cp = strpbrk(p, "#\n");
240 p = strpbrk(p, " \t");
244 while (*p == ' ' || *p == '\t')
246 cp = strpbrk(p, ",/");
250 serv.s_port = htons((u_short)atoi(p));
252 q = serv.s_aliases = serv_aliases;
253 cp = strpbrk(cp, " \t");
257 if (*cp == ' ' || *cp == '\t') {
261 if (q < &serv_aliases[MAXALIASES - 1])
263 cp = strpbrk(cp, " \t");