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.6 2005/09/19 09:34:53 asmodai 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(line)
68 char buf[YPMAXRECORD + 2];
71 snprintf(buf, sizeof(buf), "%d/%s", ntohs(___getservbyport_yp),
72 ___getservbyproto_yp);
74 ___getservbyport_yp = 0;
75 ___getservbyproto_yp = NULL;
78 if(yp_get_default_domain(&yp_domain))
83 * We have to be a little flexible here. Ideally you're supposed
84 * to have both a services.byname and a services.byport map, but
85 * some systems have only services.byname. FreeBSD cheats a little
86 * by putting the services.byport information in the same map as
87 * services.byname so that either case will work. We allow for both
88 * possibilities here: if there is no services.byport map, we try
89 * services.byname instead.
91 if ((rv = yp_match(yp_domain, "services.byport", buf, strlen(buf),
92 &result, &resultlen))) {
93 if (rv == YPERR_MAP) {
94 if (yp_match(yp_domain, "services.byname", buf,
95 strlen(buf), &result, &resultlen))
101 /* getservent() expects lines terminated with \n -- make it happy */
102 snprintf(line, BUFSIZ, "%.*s\n", resultlen, result);
109 _getservbyname_yp(line)
114 char buf[YPMAXRECORD + 2];
117 if(yp_get_default_domain(&yp_domain))
121 snprintf(buf, sizeof(buf), "%s/%s", ___getservbyname_yp,
122 ___getservbyproto_yp);
124 ___getservbyname_yp = 0;
125 ___getservbyproto_yp = NULL;
127 if (yp_match(yp_domain, "services.byname", buf, strlen(buf),
128 &result, &resultlen)) {
132 /* getservent() expects lines terminated with \n -- make it happy */
133 snprintf(line, BUFSIZ, "%.*s\n", resultlen, result);
143 static char *key = NULL;
145 char *lastkey, *result;
150 if(yp_get_default_domain(&yp_domain))
154 if (!serv_stepping_yp) {
157 if ((rv = yp_first(yp_domain, "services.byname", &key, &keylen,
158 &result, &resultlen))) {
159 serv_stepping_yp = 0;
162 serv_stepping_yp = 1;
165 rv = yp_next(yp_domain, "services.byname", key, keylen, &key,
166 &keylen, &result, &resultlen);
169 serv_stepping_yp = 0;
174 /* getservent() expects lines terminated with \n -- make it happy */
175 snprintf(line, BUFSIZ, "%.*s\n", resultlen, result);
188 servf = fopen(_PATH_SERVICES, "r" );
211 if (serv_stepping_yp && _getservent_yp(line)) {
217 if (servf == NULL && (servf = fopen(_PATH_SERVICES, "r" )) == NULL)
220 if ((p = fgets(line, BUFSIZ, servf)) == NULL)
223 if (*p == '+' && _yp_check(NULL)) {
224 if (___getservbyname_yp != NULL) {
225 if (!_getservbyname_yp(line))
228 else if (___getservbyport_yp != 0) {
229 if (!_getservbyport_yp(line))
232 else if (!_getservent_yp(line))
239 cp = strpbrk(p, "#\n");
244 p = strpbrk(p, " \t");
248 while (*p == ' ' || *p == '\t')
250 cp = strpbrk(p, ",/");
254 serv.s_port = htons((u_short)atoi(p));
256 q = serv.s_aliases = serv_aliases;
257 cp = strpbrk(cp, " \t");
261 if (*cp == ' ' || *cp == '\t') {
265 if (q < &serv_aliases[MAXALIASES - 1])
267 cp = strpbrk(cp, " \t");