1*75e1de3cSAugustin Cavalier /* $NetBSD: getservbyname_r.c,v 1.9 2012/03/13 21:13:41 christos Exp $ */
2*75e1de3cSAugustin Cavalier
3*75e1de3cSAugustin Cavalier /*
4*75e1de3cSAugustin Cavalier * Copyright (c) 1983, 1993
5*75e1de3cSAugustin Cavalier * The Regents of the University of California. All rights reserved.
6*75e1de3cSAugustin Cavalier *
7*75e1de3cSAugustin Cavalier * Redistribution and use in source and binary forms, with or without
8*75e1de3cSAugustin Cavalier * modification, are permitted provided that the following conditions
9*75e1de3cSAugustin Cavalier * are met:
10*75e1de3cSAugustin Cavalier * 1. Redistributions of source code must retain the above copyright
11*75e1de3cSAugustin Cavalier * notice, this list of conditions and the following disclaimer.
12*75e1de3cSAugustin Cavalier * 2. Redistributions in binary form must reproduce the above copyright
13*75e1de3cSAugustin Cavalier * notice, this list of conditions and the following disclaimer in the
14*75e1de3cSAugustin Cavalier * documentation and/or other materials provided with the distribution.
15*75e1de3cSAugustin Cavalier * 3. Neither the name of the University nor the names of its contributors
16*75e1de3cSAugustin Cavalier * may be used to endorse or promote products derived from this software
17*75e1de3cSAugustin Cavalier * without specific prior written permission.
18*75e1de3cSAugustin Cavalier *
19*75e1de3cSAugustin Cavalier * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*75e1de3cSAugustin Cavalier * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*75e1de3cSAugustin Cavalier * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*75e1de3cSAugustin Cavalier * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*75e1de3cSAugustin Cavalier * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*75e1de3cSAugustin Cavalier * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*75e1de3cSAugustin Cavalier * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*75e1de3cSAugustin Cavalier * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*75e1de3cSAugustin Cavalier * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*75e1de3cSAugustin Cavalier * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*75e1de3cSAugustin Cavalier * SUCH DAMAGE.
30*75e1de3cSAugustin Cavalier */
31*75e1de3cSAugustin Cavalier
32*75e1de3cSAugustin Cavalier #include <sys/cdefs.h>
33*75e1de3cSAugustin Cavalier #if defined(LIBC_SCCS) && !defined(lint)
34*75e1de3cSAugustin Cavalier #if 0
35*75e1de3cSAugustin Cavalier static char sccsid[] = "@(#)getservbyname.c 8.1 (Berkeley) 6/4/93";
36*75e1de3cSAugustin Cavalier #else
37*75e1de3cSAugustin Cavalier __RCSID("$NetBSD: getservbyname_r.c,v 1.9 2012/03/13 21:13:41 christos Exp $");
38*75e1de3cSAugustin Cavalier #endif
39*75e1de3cSAugustin Cavalier #endif /* LIBC_SCCS and not lint */
40*75e1de3cSAugustin Cavalier
41*75e1de3cSAugustin Cavalier #include <assert.h>
42*75e1de3cSAugustin Cavalier #include <netdb.h>
43*75e1de3cSAugustin Cavalier #include <stdlib.h>
44*75e1de3cSAugustin Cavalier #include <string.h>
45*75e1de3cSAugustin Cavalier
46*75e1de3cSAugustin Cavalier #include "servent.h"
47*75e1de3cSAugustin Cavalier
48*75e1de3cSAugustin Cavalier #ifdef __weak_alias
__weak_alias(getservbyname_r,_getservbyname_r)49*75e1de3cSAugustin Cavalier __weak_alias(getservbyname_r,_getservbyname_r)
50*75e1de3cSAugustin Cavalier #endif
51*75e1de3cSAugustin Cavalier
52*75e1de3cSAugustin Cavalier static struct servent *
53*75e1de3cSAugustin Cavalier _servent_getbyname(struct servent_data *sd, struct servent *sp,
54*75e1de3cSAugustin Cavalier const char *name, const char *proto)
55*75e1de3cSAugustin Cavalier {
56*75e1de3cSAugustin Cavalier
57*75e1de3cSAugustin Cavalier if ((sd->flags & (_SV_CDB | _SV_PLAINFILE)) == 0)
58*75e1de3cSAugustin Cavalier return NULL;
59*75e1de3cSAugustin Cavalier
60*75e1de3cSAugustin Cavalier #if 0
61*75e1de3cSAugustin Cavalier if (sd->flags & _SV_CDB) {
62*75e1de3cSAugustin Cavalier uint8_t buf[255 * 2 + 2];
63*75e1de3cSAugustin Cavalier size_t namelen, protolen;
64*75e1de3cSAugustin Cavalier const uint8_t *data, *data_end;
65*75e1de3cSAugustin Cavalier const void *data_ptr;
66*75e1de3cSAugustin Cavalier size_t datalen;
67*75e1de3cSAugustin Cavalier
68*75e1de3cSAugustin Cavalier namelen = strlen(name);
69*75e1de3cSAugustin Cavalier if (namelen == 0 || namelen > 255)
70*75e1de3cSAugustin Cavalier return NULL;
71*75e1de3cSAugustin Cavalier if (proto != NULL) {
72*75e1de3cSAugustin Cavalier protolen = strlen(proto);
73*75e1de3cSAugustin Cavalier if (protolen == 0 || protolen > 255)
74*75e1de3cSAugustin Cavalier return NULL;
75*75e1de3cSAugustin Cavalier } else
76*75e1de3cSAugustin Cavalier protolen = 0;
77*75e1de3cSAugustin Cavalier if (namelen + protolen > 255)
78*75e1de3cSAugustin Cavalier return NULL;
79*75e1de3cSAugustin Cavalier
80*75e1de3cSAugustin Cavalier buf[0] = (uint8_t)namelen;
81*75e1de3cSAugustin Cavalier buf[1] = (uint8_t)protolen;
82*75e1de3cSAugustin Cavalier memcpy(buf + 2, name, namelen);
83*75e1de3cSAugustin Cavalier memcpy(buf + 2 + namelen, proto, protolen);
84*75e1de3cSAugustin Cavalier
85*75e1de3cSAugustin Cavalier if (cdbr_find(sd->cdb, buf, 2 + namelen + protolen,
86*75e1de3cSAugustin Cavalier &data_ptr, &datalen))
87*75e1de3cSAugustin Cavalier return NULL;
88*75e1de3cSAugustin Cavalier
89*75e1de3cSAugustin Cavalier if (datalen < namelen + protolen + 6)
90*75e1de3cSAugustin Cavalier return NULL;
91*75e1de3cSAugustin Cavalier
92*75e1de3cSAugustin Cavalier data = data_ptr;
93*75e1de3cSAugustin Cavalier data_end = data + datalen;
94*75e1de3cSAugustin Cavalier if (protolen) {
95*75e1de3cSAugustin Cavalier if (data[2] != protolen)
96*75e1de3cSAugustin Cavalier return NULL;
97*75e1de3cSAugustin Cavalier if (memcmp(data + 3, proto, protolen + 1))
98*75e1de3cSAugustin Cavalier return NULL;
99*75e1de3cSAugustin Cavalier }
100*75e1de3cSAugustin Cavalier data += 3 + data[2] + 1;
101*75e1de3cSAugustin Cavalier if (data > data_end)
102*75e1de3cSAugustin Cavalier return NULL;
103*75e1de3cSAugustin Cavalier while (data != data_end) {
104*75e1de3cSAugustin Cavalier if (*data == '\0')
105*75e1de3cSAugustin Cavalier return NULL;
106*75e1de3cSAugustin Cavalier if (data + data[0] + 2 > data_end)
107*75e1de3cSAugustin Cavalier return NULL;
108*75e1de3cSAugustin Cavalier if (data[0] == namelen &&
109*75e1de3cSAugustin Cavalier memcmp(data + 1, name, namelen + 1) == 0)
110*75e1de3cSAugustin Cavalier return _servent_parsedb(sd, sp, data_ptr,
111*75e1de3cSAugustin Cavalier datalen);
112*75e1de3cSAugustin Cavalier data += data[0] + 2;
113*75e1de3cSAugustin Cavalier }
114*75e1de3cSAugustin Cavalier return NULL;
115*75e1de3cSAugustin Cavalier } else
116*75e1de3cSAugustin Cavalier #endif
117*75e1de3cSAugustin Cavalier {
118*75e1de3cSAugustin Cavalier while (_servent_getline(sd) != -1) {
119*75e1de3cSAugustin Cavalier char **cp;
120*75e1de3cSAugustin Cavalier if (_servent_parseline(sd, sp) == NULL)
121*75e1de3cSAugustin Cavalier continue;
122*75e1de3cSAugustin Cavalier
123*75e1de3cSAugustin Cavalier if (strcmp(name, sp->s_name) == 0)
124*75e1de3cSAugustin Cavalier goto gotname;
125*75e1de3cSAugustin Cavalier
126*75e1de3cSAugustin Cavalier for (cp = sp->s_aliases; *cp; cp++)
127*75e1de3cSAugustin Cavalier if (strcmp(name, *cp) == 0)
128*75e1de3cSAugustin Cavalier goto gotname;
129*75e1de3cSAugustin Cavalier continue;
130*75e1de3cSAugustin Cavalier gotname:
131*75e1de3cSAugustin Cavalier if (proto == NULL || strcmp(sp->s_proto, proto) == 0)
132*75e1de3cSAugustin Cavalier return sp;
133*75e1de3cSAugustin Cavalier }
134*75e1de3cSAugustin Cavalier return NULL;
135*75e1de3cSAugustin Cavalier }
136*75e1de3cSAugustin Cavalier }
137*75e1de3cSAugustin Cavalier
138*75e1de3cSAugustin Cavalier struct servent *
getservbyname_r(const char * name,const char * proto,struct servent * sp,struct servent_data * sd)139*75e1de3cSAugustin Cavalier getservbyname_r(const char *name, const char *proto, struct servent *sp,
140*75e1de3cSAugustin Cavalier struct servent_data *sd)
141*75e1de3cSAugustin Cavalier {
142*75e1de3cSAugustin Cavalier assert(name != NULL);
143*75e1de3cSAugustin Cavalier /* proto may be NULL */
144*75e1de3cSAugustin Cavalier
145*75e1de3cSAugustin Cavalier setservent_r(sd->flags & _SV_STAYOPEN, sd);
146*75e1de3cSAugustin Cavalier sp = _servent_getbyname(sd, sp, name, proto);
147*75e1de3cSAugustin Cavalier if (!(sd->flags & _SV_STAYOPEN))
148*75e1de3cSAugustin Cavalier _servent_close(sd);
149*75e1de3cSAugustin Cavalier return sp;
150*75e1de3cSAugustin Cavalier }
151