1*c5e820caSchristos /* $NetBSD: getservbyname_r.c,v 1.9 2012/03/13 21:13:41 christos Exp $ */
28059eed1Schristos
38059eed1Schristos /*
48059eed1Schristos * Copyright (c) 1983, 1993
58059eed1Schristos * The Regents of the University of California. All rights reserved.
68059eed1Schristos *
78059eed1Schristos * Redistribution and use in source and binary forms, with or without
88059eed1Schristos * modification, are permitted provided that the following conditions
98059eed1Schristos * are met:
108059eed1Schristos * 1. Redistributions of source code must retain the above copyright
118059eed1Schristos * notice, this list of conditions and the following disclaimer.
128059eed1Schristos * 2. Redistributions in binary form must reproduce the above copyright
138059eed1Schristos * notice, this list of conditions and the following disclaimer in the
148059eed1Schristos * documentation and/or other materials provided with the distribution.
158059eed1Schristos * 3. Neither the name of the University nor the names of its contributors
168059eed1Schristos * may be used to endorse or promote products derived from this software
178059eed1Schristos * without specific prior written permission.
188059eed1Schristos *
198059eed1Schristos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
208059eed1Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
218059eed1Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
228059eed1Schristos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
238059eed1Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
248059eed1Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
258059eed1Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
268059eed1Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
278059eed1Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
288059eed1Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
298059eed1Schristos * SUCH DAMAGE.
308059eed1Schristos */
318059eed1Schristos
328059eed1Schristos #include <sys/cdefs.h>
338059eed1Schristos #if defined(LIBC_SCCS) && !defined(lint)
348059eed1Schristos #if 0
358059eed1Schristos static char sccsid[] = "@(#)getservbyname.c 8.1 (Berkeley) 6/4/93";
368059eed1Schristos #else
37*c5e820caSchristos __RCSID("$NetBSD: getservbyname_r.c,v 1.9 2012/03/13 21:13:41 christos Exp $");
388059eed1Schristos #endif
398059eed1Schristos #endif /* LIBC_SCCS and not lint */
408059eed1Schristos
418059eed1Schristos #include "namespace.h"
428059eed1Schristos #include <assert.h>
43b9cf7d31Sjoerg #include <cdbr.h>
448059eed1Schristos #include <netdb.h>
45754fca0eSchristos #include <stdlib.h>
468059eed1Schristos #include <string.h>
478059eed1Schristos
484e3cae58Skleink #include "servent.h"
494e3cae58Skleink
508059eed1Schristos #ifdef __weak_alias
__weak_alias(getservbyname_r,_getservbyname_r)518059eed1Schristos __weak_alias(getservbyname_r,_getservbyname_r)
528059eed1Schristos #endif
538059eed1Schristos
54754fca0eSchristos static struct servent *
55754fca0eSchristos _servent_getbyname(struct servent_data *sd, struct servent *sp,
56754fca0eSchristos const char *name, const char *proto)
578059eed1Schristos {
58b9cf7d31Sjoerg
59b9cf7d31Sjoerg if ((sd->flags & (_SV_CDB | _SV_PLAINFILE)) == 0)
60efa6baa2Schristos return NULL;
61efa6baa2Schristos
62b9cf7d31Sjoerg if (sd->flags & _SV_CDB) {
63b9cf7d31Sjoerg uint8_t buf[255 * 2 + 2];
64b9cf7d31Sjoerg size_t namelen, protolen;
65b9cf7d31Sjoerg const uint8_t *data, *data_end;
66b9cf7d31Sjoerg const void *data_ptr;
67b9cf7d31Sjoerg size_t datalen;
68754fca0eSchristos
69b9cf7d31Sjoerg namelen = strlen(name);
70b9cf7d31Sjoerg if (namelen == 0 || namelen > 255)
71b9cf7d31Sjoerg return NULL;
728056bccdSchristos if (proto != NULL) {
73b9cf7d31Sjoerg protolen = strlen(proto);
748056bccdSchristos if (protolen == 0 || protolen > 255)
758056bccdSchristos return NULL;
768056bccdSchristos } else
77b9cf7d31Sjoerg protolen = 0;
788056bccdSchristos if (namelen + protolen > 255)
798056bccdSchristos return NULL;
80754fca0eSchristos
81*c5e820caSchristos buf[0] = (uint8_t)namelen;
82*c5e820caSchristos buf[1] = (uint8_t)protolen;
83b9cf7d31Sjoerg memcpy(buf + 2, name, namelen);
84b9cf7d31Sjoerg memcpy(buf + 2 + namelen, proto, protolen);
85b9cf7d31Sjoerg
86b9cf7d31Sjoerg if (cdbr_find(sd->cdb, buf, 2 + namelen + protolen,
87b9cf7d31Sjoerg &data_ptr, &datalen))
88754fca0eSchristos return NULL;
89754fca0eSchristos
90b9cf7d31Sjoerg if (datalen < namelen + protolen + 6)
9155b31470Schristos return NULL;
9255b31470Schristos
93b9cf7d31Sjoerg data = data_ptr;
94b9cf7d31Sjoerg data_end = data + datalen;
95b9cf7d31Sjoerg if (protolen) {
96b9cf7d31Sjoerg if (data[2] != protolen)
97b9cf7d31Sjoerg return NULL;
98b9cf7d31Sjoerg if (memcmp(data + 3, proto, protolen + 1))
99b9cf7d31Sjoerg return NULL;
100b9cf7d31Sjoerg }
101b9cf7d31Sjoerg data += 3 + data[2] + 1;
102b9cf7d31Sjoerg if (data > data_end)
103b9cf7d31Sjoerg return NULL;
104b9cf7d31Sjoerg while (data != data_end) {
105b9cf7d31Sjoerg if (*data == '\0')
106b9cf7d31Sjoerg return NULL;
107b9cf7d31Sjoerg if (data + data[0] + 2 > data_end)
108b9cf7d31Sjoerg return NULL;
109b9cf7d31Sjoerg if (data[0] == namelen &&
110b9cf7d31Sjoerg memcmp(data + 1, name, namelen + 1) == 0)
111b9cf7d31Sjoerg return _servent_parsedb(sd, sp, data_ptr,
112b9cf7d31Sjoerg datalen);
113b9cf7d31Sjoerg data += data[0] + 2;
114b9cf7d31Sjoerg }
115b9cf7d31Sjoerg return NULL;
116754fca0eSchristos } else {
117754fca0eSchristos while (_servent_getline(sd) != -1) {
1188059eed1Schristos char **cp;
119754fca0eSchristos if (_servent_parseline(sd, sp) == NULL)
120754fca0eSchristos continue;
1218059eed1Schristos
122754fca0eSchristos if (strcmp(name, sp->s_name) == 0)
1238059eed1Schristos goto gotname;
124754fca0eSchristos
125754fca0eSchristos for (cp = sp->s_aliases; *cp; cp++)
1268059eed1Schristos if (strcmp(name, *cp) == 0)
1278059eed1Schristos goto gotname;
1288059eed1Schristos continue;
1298059eed1Schristos gotname:
130754fca0eSchristos if (proto == NULL || strcmp(sp->s_proto, proto) == 0)
131754fca0eSchristos return sp;
1328059eed1Schristos }
133754fca0eSchristos return NULL;
134119b5f63Senami }
135754fca0eSchristos }
136754fca0eSchristos
137754fca0eSchristos struct servent *
getservbyname_r(const char * name,const char * proto,struct servent * sp,struct servent_data * sd)138754fca0eSchristos getservbyname_r(const char *name, const char *proto, struct servent *sp,
139754fca0eSchristos struct servent_data *sd)
140754fca0eSchristos {
141754fca0eSchristos _DIAGASSERT(name != NULL);
142754fca0eSchristos /* proto may be NULL */
143754fca0eSchristos
144754fca0eSchristos setservent_r(sd->flags & _SV_STAYOPEN, sd);
145754fca0eSchristos sp = _servent_getbyname(sd, sp, name, proto);
146754fca0eSchristos if (!(sd->flags & _SV_STAYOPEN))
147754fca0eSchristos _servent_close(sd);
148754fca0eSchristos return sp;
1498059eed1Schristos }
150