xref: /netbsd-src/lib/libc/net/getservbyport_r.c (revision c5e820cae412164fcbee52f470436200af5358ea)
1*c5e820caSchristos /*	$NetBSD: getservbyport_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[] = "@(#)getservbyport.c	8.1 (Berkeley) 6/4/93";
368059eed1Schristos #else
37*c5e820caSchristos __RCSID("$NetBSD: getservbyport_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"
42b9cf7d31Sjoerg #include <cdbr.h>
438059eed1Schristos #include <netdb.h>
448059eed1Schristos #include <string.h>
45754fca0eSchristos #include <stdlib.h>
468059eed1Schristos 
474e3cae58Skleink #include "servent.h"
484e3cae58Skleink 
498059eed1Schristos #ifdef __weak_alias
__weak_alias(getservbyport_r,_getservbyport_r)508059eed1Schristos __weak_alias(getservbyport_r,_getservbyport_r)
518059eed1Schristos #endif
528059eed1Schristos 
53754fca0eSchristos static struct servent *
54754fca0eSchristos _servent_getbyport(struct servent_data *sd, struct servent *sp, int port,
55754fca0eSchristos     const char *proto)
56754fca0eSchristos {
57b9cf7d31Sjoerg 
58b9cf7d31Sjoerg 	if ((sd->flags & (_SV_CDB | _SV_PLAINFILE)) == 0)
59efa6baa2Schristos 		return NULL;
60efa6baa2Schristos 
61b9cf7d31Sjoerg 	if (sd->flags & _SV_CDB) {
62b9cf7d31Sjoerg 		uint8_t buf[255 + 4];
63b9cf7d31Sjoerg 		size_t protolen;
64b9cf7d31Sjoerg 		const uint8_t *data;
65b9cf7d31Sjoerg 		const void *data_ptr;
66b9cf7d31Sjoerg 		size_t datalen;
67754fca0eSchristos 
68b9cf7d31Sjoerg 		port = be16toh(port);
69b9cf7d31Sjoerg 
708056bccdSchristos 		if (proto != NULL) {
71b9cf7d31Sjoerg 			protolen = strlen(proto);
728056bccdSchristos 			if (protolen == 0 || protolen > 255)
738056bccdSchristos 				return NULL;
748056bccdSchristos 		} else
75b9cf7d31Sjoerg 			protolen = 0;
76b9cf7d31Sjoerg 		if (port < 0 || port > 65536)
77754fca0eSchristos 			return NULL;
78754fca0eSchristos 
79b9cf7d31Sjoerg 		buf[0] = 0;
80*c5e820caSchristos 		buf[1] = (uint8_t)protolen;
81b9cf7d31Sjoerg 		be16enc(buf + 2, port);
82b9cf7d31Sjoerg 		memcpy(buf + 4, proto, protolen);
83b9cf7d31Sjoerg 
84b9cf7d31Sjoerg 		if (cdbr_find(sd->cdb, buf, 4 + protolen,
85b9cf7d31Sjoerg 		    &data_ptr, &datalen))
8655b31470Schristos 			return NULL;
8755b31470Schristos 
88b9cf7d31Sjoerg 		if (datalen < protolen + 4)
89b9cf7d31Sjoerg 			return NULL;
90754fca0eSchristos 
91b9cf7d31Sjoerg 		data = data_ptr;
92b9cf7d31Sjoerg 		if (be16dec(data) != port)
93b9cf7d31Sjoerg 			return NULL;
94b9cf7d31Sjoerg 		if (protolen) {
95b9cf7d31Sjoerg 			if (data[2] != protolen)
96b9cf7d31Sjoerg 				return NULL;
97b9cf7d31Sjoerg 			if (memcmp(data + 3, proto, protolen + 1))
98b9cf7d31Sjoerg 				return NULL;
99b9cf7d31Sjoerg 		}
100b9cf7d31Sjoerg 		return _servent_parsedb(sd, sp, data, datalen);
101754fca0eSchristos 	} else {
102754fca0eSchristos 		while (_servent_getline(sd) != -1) {
103754fca0eSchristos 			if (_servent_parseline(sd, sp) == NULL)
104754fca0eSchristos 				continue;
105754fca0eSchristos 			if (sp->s_port != port)
106754fca0eSchristos 				continue;
107754fca0eSchristos 			if (proto == NULL || strcmp(sp->s_proto, proto) == 0)
108754fca0eSchristos 				return sp;
109754fca0eSchristos 		}
110754fca0eSchristos 		return NULL;
111754fca0eSchristos 	}
112754fca0eSchristos }
113754fca0eSchristos 
1148059eed1Schristos struct servent *
getservbyport_r(int port,const char * proto,struct servent * sp,struct servent_data * sd)1158059eed1Schristos getservbyport_r(int port, const char *proto, struct servent *sp,
1168059eed1Schristos     struct servent_data *sd)
1178059eed1Schristos {
118754fca0eSchristos 	setservent_r(sd->flags & _SV_STAYOPEN, sd);
119754fca0eSchristos 	sp = _servent_getbyport(sd, sp, port, proto);
120754fca0eSchristos 	if (!(sd->flags & _SV_STAYOPEN))
121754fca0eSchristos 		_servent_close(sd);
122754fca0eSchristos 	return sp;
1238059eed1Schristos }
124